From nobody Tue Feb 10 05:26:29 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=1612448107; cv=none; d=zohomail.com; s=zohoarc; b=XFBBs1XkJSfFc/C83JvWcsHY4nps7Lv/+436UCIb/4CbwB8gfDNAgmMrjUOpTxirDG12J/UuJFJjrdfQo5akDM6p4O7mfZeOwUfsPCSq7uBk5nv4G/cL+BGvo6tYwzlmi2Jwzm3yMRki41d5N07eVKHr0f8/otF4HU4hwbqtzQM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612448107; 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=xyRN55/7CFxzAhhDg8AFxO5+2H9hKq2jeJQNtb7FKWY=; b=lHvtskS8eF7Zuk039+E7jbRYDJ94c6wImzBJxfCoSaBHp4raA7luZu22ioYUdg2WPOipiCEq2315Apk+QGrU1CS9GBSevTSBlrfaCBXRFaDM89UDKBrzBrlxhGb55NHyF65+JG3cm2k8AQGBHWNIbWlq9kooqNOB3bl7siHJsZQ= 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 1612448107884367.7377972930075; Thu, 4 Feb 2021 06:15:07 -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-425-ZbbyttewNyeFMas7rqpULw-1; Thu, 04 Feb 2021 09:14:27 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C4C3F107ACE8; Thu, 4 Feb 2021 14:14:21 +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 A091671CA4; Thu, 4 Feb 2021 14:14:21 +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 684F018095CB; Thu, 4 Feb 2021 14:14:21 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 114EDur7031643 for ; Thu, 4 Feb 2021 09:13:56 -0500 Received: by smtp.corp.redhat.com (Postfix) id 31F945D9F4; Thu, 4 Feb 2021 14:13:56 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.193.72]) by smtp.corp.redhat.com (Postfix) with ESMTP id A81D75D9C9 for ; Thu, 4 Feb 2021 14:13:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612448106; 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=xyRN55/7CFxzAhhDg8AFxO5+2H9hKq2jeJQNtb7FKWY=; b=d3Eq48RPpG0RMeXa9D+TZn3ho2cpEuIHXDVLkgec5C4UoFil7kpTufZKt08DX2h9b0bGdk YxkoG3tyFfQQBmj7ar4ev2XeJ/49v3pYVnTQiNIxhikmDy4CBeJ8fXUqLWrJXdq7jJjH5R z+X0Msx5/DrwSE4MSE6fkeDPmZHNS+M= X-MC-Unique: ZbbyttewNyeFMas7rqpULw-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 09/12] vsh: Deduplicate filtering in vshReadlineCommandGenerator() Date: Thu, 4 Feb 2021 15:13:34 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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.16 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" This is what we do for completer callbacks: we let them generate all possible outputs ignoring any partial input (e.g. prefix of a domain name) and then use vshCompleterFilter() to filter out those strings which don't fit the partial input (prefix). The same algorithm is implemented in vshReadlineCommandGenerator(). There is no need to have the same code twice. Signed-off-by: Michal Privoznik Reviewed-by: Jonathon Jongsma --- tools/vsh.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index 5f082f1a35..e0dbda04c8 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2561,11 +2561,10 @@ vshTreePrint(vshControl *ctl, vshTreeLookup lookup,= void *opaque, * * Generator function for command completion. * - * Returns a string list of commands with @text prefix, - * NULL if there's no such command. + * Returns a string list of all commands, or NULL on failure. */ static char ** -vshReadlineCommandGenerator(const char *text) +vshReadlineCommandGenerator(const char *text G_GNUC_UNUSED) { size_t grp_list_index =3D 0, cmd_list_index =3D 0; const char *name; @@ -2587,15 +2586,13 @@ vshReadlineCommandGenerator(const char *text) if (cmds[cmd_list_index++].flags & VSH_CMD_FLAG_ALIAS) continue; =20 - if (STRPREFIX(name, text)) { - if (VIR_REALLOC_N(ret, ret_size + 2) < 0) - return NULL; + if (VIR_REALLOC_N(ret, ret_size + 2) < 0) + return NULL; =20 - ret[ret_size] =3D g_strdup(name); - ret_size++; - /* Terminate the string list properly. */ - ret[ret_size] =3D NULL; - } + ret[ret_size] =3D g_strdup(name); + ret_size++; + /* Terminate the string list properly. */ + ret[ret_size] =3D NULL; } } else { cmd_list_index =3D 0; @@ -2782,13 +2779,13 @@ vshReadlineParse(const char *text, int state) if (virStringListMerge(&list, &completer_list) < 0) goto cleanup; } - - /* For string list returned by completers we have to do - * filtering based on @text because completers returns all - * possible strings. */ - if (vshCompleterFilter(&list, text) < 0) - goto cleanup; } + + /* For string list returned by completers we have to do + * filtering based on @text because completers returns all + * possible strings. */ + if (vshCompleterFilter(&list, text) < 0) + goto cleanup; } =20 if (list) { --=20 2.26.2