From nobody Sun Feb 8 19:59:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1611667762; cv=none; d=zohomail.com; s=zohoarc; b=HZeSLvL1zEUzxwjaEStiSh32fRwB8/T+WE8HMbFFIpIVgKCzwqhVTJI1ztcHYVqjMWD7uF+9zJDRnTQdTndpSesWaC7R95Pqi4f7/+hnGcbYuKG9hnwEcyvdhtAvWZOcC8k20nhgiXvJ8fJFJ3B1rEuG9yqI1PdgOgjdGwS6S+U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611667762; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=epfxPHHBbjexCRT74GAJwT7+b4PKdA3cOib3QlTBBvI=; b=Vxd0veaidl8WRCDH4kK2dq85iq2JOXolUiCCt4LNXPQWPgqwC/4toWajptVIQavkhAvupW12uVkCGNqqMonYJw5LZzmnM+KN317CB3dUZfYnCcGzahDrn+jGKVyGTXpfLnOBhnTz9wv8KpW3Lf2H+LQqfQ5EsS3lTQgKSfo5taI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1611667762096704.5744841931433; Tue, 26 Jan 2021 05:29:22 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-281-nbF20hb5MmWCmxbFtL-k2Q-1; Tue, 26 Jan 2021 08:27:46 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C6B22801AB6; Tue, 26 Jan 2021 13:27:37 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A5AF360CCE; Tue, 26 Jan 2021 13:27:37 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 6FA5E1809CA0; Tue, 26 Jan 2021 13:27:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 10QDRPj9001351 for ; Tue, 26 Jan 2021 08:27:25 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1EFD11880F; Tue, 26 Jan 2021 13:27:25 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.116]) by smtp.corp.redhat.com (Postfix) with ESMTP id 92CB85C276 for ; Tue, 26 Jan 2021 13:27:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611667761; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=epfxPHHBbjexCRT74GAJwT7+b4PKdA3cOib3QlTBBvI=; b=gojsMNZjsKUBBKGZt1ev1TTyqOqhyDDt7bmYSW5FAdIQ2k5+iS1HKcte/KFCkpbX4bhs5j uUOrh2dW1dAZ0v1zQ9Uqv7v9koKVMcUiEKBhbR5MnhPpfC9bhoRz59EMMqybvDn2YHOlvA ssL6/quvpD0TdYQndHT6ZT6AKUQ1xw8= X-MC-Unique: nbF20hb5MmWCmxbFtL-k2Q-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/9] vshReadlineParse: Bring some variables into !state block Date: Tue, 26 Jan 2021 14:27:07 +0100 Message-Id: <908ebac3ce28221f76d57617bf322862379d4ca3.1611667532.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" On readline completion vshReadlineCompletion() is called which does nothing more than calling rl_completion_matches() with vshReadlineParse() as a callback. This means, that vshReadlineParse() is called repeatedly, each time returning next completion candidate, until it returns NULL which is interpreted as the end of the list of candidates. The function takes two parameters: @text which is a portion of input line around cursor when TAB was pressed, and @state. The @state is an integer that is zero on the very first call and non-zero on each subsequent call (in fact, readline does @state++ on each call). Anyway, the idea is that the callback gets the whole list of candidates on @state =3D=3D 0 and returns one candidate at each call. And this is what vshReadlineParse() is doing but some variables (@parital, @cmd and @opt) are really used only in the @state =3D=3D 0 case but declared for whole function. We can limit their scope by declaring them inside the @state =3D=3D 0 body which also means that they don't have to be static anymore. Signed-off-by: Michal Privoznik --- tools/vsh.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index 9856088126..ba6299aae4 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2695,18 +2695,20 @@ vshCompleterFilter(char ***list, static char * vshReadlineParse(const char *text, int state) { - static vshCmd *partial; static char **list; static size_t list_index; - const vshCmdDef *cmd =3D NULL; - const vshCmdOptDef *opt =3D NULL; char *ret =3D NULL; =20 + /* Readline calls this function until NULL is returned. On + * the very first call @state is zero which means we should + * initialize those static variables above. On subsequent + * calls @state is non zero. */ if (!state) { + vshCmd *partial =3D NULL; + const vshCmdDef *cmd =3D NULL; + const vshCmdOptDef *opt =3D NULL; char *buf =3D g_strdup(rl_line_buffer); =20 - vshCommandFree(partial); - partial =3D NULL; g_strfreev(list); list =3D NULL; list_index =3D 0; @@ -2734,9 +2736,7 @@ vshReadlineParse(const char *text, int state) } =20 opt =3D vshReadlineCommandFindOpt(partial, text); - } =20 - if (!list) { if (!cmd) { list =3D vshReadlineCommandGenerator(text); } else { @@ -2759,10 +2759,12 @@ vshReadlineParse(const char *text, int state) (vshCompleterFilter(&completer_list, text) < 0 || virStringListMerge(&list, &completer_list) < 0)) { g_strfreev(completer_list); + vshCommandFree(partial); goto cleanup; } } } + vshCommandFree(partial); } =20 if (list) { @@ -2780,15 +2782,12 @@ vshReadlineParse(const char *text, int state) =20 cleanup: if (!ret) { - vshCommandFree(partial); - partial =3D NULL; g_strfreev(list); list =3D NULL; list_index =3D 0; } =20 return ret; - } =20 static char ** --=20 2.26.2