From nobody Sat May 4 06:13:31 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1516008883000177.10233531011284; Mon, 15 Jan 2018 01:34:43 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B91EB356D5; Mon, 15 Jan 2018 09:34:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 50D405D9C6; Mon, 15 Jan 2018 09:34:40 +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 C336E4ED37; Mon, 15 Jan 2018 09:34:38 +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 w0F9YaZZ023561 for ; Mon, 15 Jan 2018 04:34:36 -0500 Received: by smtp.corp.redhat.com (Postfix) id BC4BD5D977; Mon, 15 Jan 2018 09:34:36 +0000 (UTC) Received: from localhost.localdomain (ovpn-204-54.brq.redhat.com [10.40.204.54]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0A6C5D9C6; Mon, 15 Jan 2018 09:34:33 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 15 Jan 2018 10:34:29 +0100 Message-Id: <57ed7647dff9ea60b0a78f63610510850dc50398.1516008869.git.mprivozn@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] vshReadlineOptionsGenerator: Don't add already specified options to the list 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 15 Jan 2018 09:34:41 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The current state of art is as follows: 1) vshReadlineOptionsGenerator() generate all possible --options for given command, and then 2) vshReadlineOptionsPrune() clears out already provided ones from the list. Not only this brings needless memory complexity it is also not trivial to get right. We can switch to easier approach: just don't add already specified --options in the first step. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- tools/vsh.c | 80 +++++++++++++++------------------------------------------= ---- 1 file changed, 19 insertions(+), 61 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index 4426c08d6..a99d47bfc 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2663,7 +2663,9 @@ vshReadlineCommandGenerator(const char *text) } =20 static char ** -vshReadlineOptionsGenerator(const char *text, const vshCmdDef *cmd) +vshReadlineOptionsGenerator(const char *text, + const vshCmdDef *cmd, + vshCmd *last) { size_t list_index =3D 0; size_t len =3D strlen(text); @@ -2678,6 +2680,8 @@ vshReadlineOptionsGenerator(const char *text, const v= shCmdDef *cmd) return NULL; =20 while ((name =3D cmd->opts[list_index].name)) { + bool exists =3D false; + vshCmdOpt *opt =3D last->opts; size_t name_len; =20 list_index++; @@ -2692,6 +2696,18 @@ vshReadlineOptionsGenerator(const char *text, const = vshCmdDef *cmd) return NULL; } =20 + while (opt) { + if (STREQ(opt->def->name, name)) { + exists =3D true; + break; + } + + opt =3D opt->next; + } + + if (exists) + continue; + if (VIR_REALLOC_N(ret, ret_size + 2) < 0) { virStringListFree(ret); return NULL; @@ -2772,60 +2788,6 @@ vshCompleterFilter(char ***list, } =20 =20 -static int -vshReadlineOptionsPrune(char ***list, - vshCmd *last) -{ - char **newList =3D NULL; - size_t newList_len =3D 0; - size_t list_len; - size_t i; - - if (!list || !*list) - return -1; - - if (!last->opts) - return 0; - - list_len =3D virStringListLength((const char **) *list); - - if (VIR_ALLOC_N(newList, list_len + 1) < 0) - return -1; - - for (i =3D 0; i < list_len; i++) { - const char *list_opt =3D STRSKIP((*list)[i], "--"); - bool exist =3D false; - vshCmdOpt *opt =3D last->opts; - - /* Should never happen (TM) */ - if (!list_opt) - return -1; - - while (opt) { - if (STREQ(opt->def->name, list_opt)) { - exist =3D true; - break; - } - - opt =3D opt->next; - } - - if (exist) { - VIR_FREE((*list)[i]); - continue; - } - - VIR_STEAL_PTR(newList[newList_len], (*list)[i]); - newList_len++; - } - - ignore_value(VIR_REALLOC_N_QUIET(newList, newList_len + 1)); - VIR_FREE(*list); - *list =3D newList; - return 0; -} - - static char * vshReadlineParse(const char *text, int state) { @@ -2874,12 +2836,8 @@ vshReadlineParse(const char *text, int state) if (!cmd) { list =3D vshReadlineCommandGenerator(text); } else { - if (!opt || (opt->type !=3D VSH_OT_DATA && opt->type !=3D VSH_= OT_STRING)) { - list =3D vshReadlineOptionsGenerator(text, cmd); - - if (vshReadlineOptionsPrune(&list, partial) < 0) - goto cleanup; - } + if (!opt || (opt->type !=3D VSH_OT_DATA && opt->type !=3D VSH_= OT_STRING)) + list =3D vshReadlineOptionsGenerator(text, cmd, partial); =20 if (opt && opt->completer) { char **completer_list =3D opt->completer(autoCompleteOpaqu= e, --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list