From nobody Mon Apr 29 13:33:35 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; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1560865595; cv=none; d=zoho.com; s=zohoarc; b=geos0FbsSWUpPhMfui2J6VHz9tXVO2XNy8Tdk8OTJLO7VD6zlpicHBGBfGBxOcImPrJ5ECbzFlDz/Eo4d1+Jf+doW+e+jfapUwnMKzh9+YdnSGgSdz1BugvpcJP9A4quAcwoLQ7tlPSV4UdyHKVdfBbMuPb0Kcqd0jMsPRq5Xqw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560865595; 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:ARC-Authentication-Results; bh=aAr8ycP+IgHf1isAgMVEDoC7X0A1zfYusToRcU2+NTU=; b=egNXHkrhlMoljaFGRVcGBHUYTazdOmqFKp8ZXooR/RFl2dF3Mc+6tF6UeUuquEp0Rl++tbcQX0jdNmCuMgkFJJez6PsUXiOOsDFjTYEx0F3uyNz4MMzklbaflqnIHXTPwhxovPMfP+iTlAciWyL4EU2II4fFdcg3WitHV+4Izwc= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1560865595068661.684825370538; Tue, 18 Jun 2019 06:46:35 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CC7A3308792C; Tue, 18 Jun 2019 13:46:28 +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 999CB69A23; Tue, 18 Jun 2019 13:46:25 +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 351C2206D3; Tue, 18 Jun 2019 13:46:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x5IDkKYu025977 for ; Tue, 18 Jun 2019 09:46:20 -0400 Received: by smtp.corp.redhat.com (Postfix) id BEFF77D504; Tue, 18 Jun 2019 13:46:20 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A0C57D571 for ; Tue, 18 Jun 2019 13:46:20 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 18 Jun 2019 15:46:15 +0200 Message-Id: <745274be2a8b7d811a5510564d34742ae63fd61e.1560865527.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/2] virsh-completer: Separate comma list construction into a function 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: , 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Tue, 18 Jun 2019 13:46:31 +0000 (UTC) Content-Type: text/plain; charset="utf-8" There are more arguments than 'shutdown --mode' that accept a list of strings separated by commas. 'nodedev-list --cap' is one of them. To avoid duplicating code, let's separate interesting bits of virshDomainShutdownModeCompleter() into a function that can then be reused. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety Reviewed-by: J=C3=A1n Tomko --- tools/virsh-completer.c | 120 ++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 43 deletions(-) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index 7d5cf8cb90..ef2f39320e 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -69,6 +69,79 @@ */ =20 =20 +/** + * virshCommaStringListComplete: + * @input: user input so far + * @options: ALL options available for argument + * + * Some arguments to our commands accept the following form: + * + * virsh command --arg str1,str2,str3 + * + * This does not play nicely with our completer funtions, because + * they have to return strings prepended with user's input. For + * instance: + * + * str1,str2,str3,strA + * str1,str2,str3,strB + * str1,str2,str3,strC + * + * This helper function takes care of that. In this specific case + * it would be called as follows: + * + * virshCommaStringListComplete("str1,str2,str3", + * {"strA", "strB", "strC", NULL}); + * + * Returns: string list of completions on success, + * NULL otherwise. + */ +static char ** +virshCommaStringListComplete(const char *input, + const char **options) +{ + const size_t optionsLen =3D virStringListLength(options); + VIR_AUTOFREE(char *) inputCopy =3D NULL; + VIR_AUTOSTRINGLIST inputList =3D NULL; + VIR_AUTOSTRINGLIST ret =3D NULL; + size_t nret =3D 0; + size_t i; + + if (STREQ_NULLABLE(input, " ")) + input =3D NULL; + + if (input) { + char *comma =3D NULL; + + if (VIR_STRDUP(inputCopy, input) < 0) + return NULL; + + if ((comma =3D strrchr(inputCopy, ','))) + *comma =3D '\0'; + else + VIR_FREE(inputCopy); + } + + if (inputCopy && !(inputList =3D virStringSplit(inputCopy, ",", 0))) + return NULL; + + if (VIR_ALLOC_N(ret, optionsLen + 1) < 0) + return NULL; + + for (i =3D 0; i < optionsLen; i++) { + if (virStringListHasString((const char **)inputList, options[i])) + continue; + + if ((inputCopy && virAsprintf(&ret[nret], "%s,%s", inputCopy, opti= ons[i]) < 0) || + (!inputCopy && VIR_STRDUP(ret[nret], options[i]) < 0)) + return NULL; + + nret++; + } + + VIR_RETURN_PTR(ret); +} + + char ** virshDomainNameCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, @@ -993,52 +1066,13 @@ virshDomainShutdownModeCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags) { - const char *modes[] =3D {"acpi", "agent", "initctl", "signal", "paravi= rt"}; - size_t i; - char **ret =3D NULL; - size_t ntmp =3D 0; - VIR_AUTOSTRINGLIST tmp =3D NULL; - const char *modeConst =3D NULL; - VIR_AUTOFREE(char *) mode =3D NULL; - VIR_AUTOSTRINGLIST modesSpecified =3D NULL; + const char *modes[] =3D {"acpi", "agent", "initctl", "signal", "paravi= rt", NULL}; + const char *mode =3D NULL; =20 virCheckFlags(0, NULL); =20 - if (vshCommandOptStringQuiet(ctl, cmd, "mode", &modeConst) < 0) + if (vshCommandOptStringQuiet(ctl, cmd, "mode", &mode) < 0) return NULL; =20 - if (STREQ_NULLABLE(modeConst, " ")) - modeConst =3D NULL; - - if (modeConst) { - char *modeTmp =3D NULL; - - if (VIR_STRDUP(mode, modeConst) < 0) - return NULL; - - if ((modeTmp =3D strrchr(mode, ','))) - *modeTmp =3D '\0'; - else - VIR_FREE(mode); - } - - if (mode && !(modesSpecified =3D virStringSplit(mode, ",", 0))) - return NULL; - - if (VIR_ALLOC_N(tmp, ARRAY_CARDINALITY(modes) + 1) < 0) - return NULL; - - for (i =3D 0; i < ARRAY_CARDINALITY(modes); i++) { - if (virStringListHasString((const char **)modesSpecified, modes[i]= )) - continue; - - if ((mode && virAsprintf(&tmp[ntmp], "%s,%s", mode, modes[i]) < 0)= || - (!mode && VIR_STRDUP(tmp[ntmp], modes[i]) < 0)) - return NULL; - - ntmp++; - } - - VIR_STEAL_PTR(ret, tmp); - return ret; + return virshCommaStringListComplete(mode, modes); } --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Mon Apr 29 13:33:35 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; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1560865606; cv=none; d=zoho.com; s=zohoarc; b=JeVG9bspaFop5jeCIQnPy6W5Fl9ABLvKYVB/pHsANi/Qecv+ZtzCgag2ULFIMRjpdp8F74710NYkNIpJ8tXLvhbnHm1tDBGYCJ8atDBgHGWLKXhAaUyZFAPkKOHovKkyTSA8G4j6hZcpCoyvl3bYnBvarEUZsLb+2SJmkxJiFU4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560865606; 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:ARC-Authentication-Results; bh=1FRctxCXA53LtcStz2ATq2uux1llI4aOxIUh1zMbcEQ=; b=U1tK8s11XLM5xHU4q9FLti6pFwVIzVsdswCHIB32/XGYBfgn88jTIaZGVuseCYlj6Z9o62lU856N19lb7ZBB4ixRTKPxLqiphgKHjOkQpWyBT9gZ16I06rSjsmWeRNDbNNbFwuIK0TtGpwTCYz+Ti+q3IM0zxVg4a+Q/hZTMsBo= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1560865606873522.8197198682797; Tue, 18 Jun 2019 06:46:46 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BC67D356C4; Tue, 18 Jun 2019 13:46:44 +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 880577D506; Tue, 18 Jun 2019 13:46:44 +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 466DB206D8; Tue, 18 Jun 2019 13:46:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x5IDkNeS025992 for ; Tue, 18 Jun 2019 09:46:23 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1B7917D504; Tue, 18 Jun 2019 13:46:23 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9AEAA7D571 for ; Tue, 18 Jun 2019 13:46:20 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 18 Jun 2019 15:46:16 +0200 Message-Id: <38c13a87a81fa629453d0b377a13038fe1dc0adc.1560865527.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/2] tools: Introduce virshNodedevCapabilityNameCompleter 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: , 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 18 Jun 2019 13:46:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" This is a very simple completer for completing --cap argument of nodedev-list command. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety Reviewed-by: J=C3=A1n Tomko --- tools/virsh-completer.c | 27 +++++++++++++++++++++++++++ tools/virsh-completer.h | 4 ++++ tools/virsh-nodedev.c | 1 + 3 files changed, 32 insertions(+) diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c index ef2f39320e..37f946d4b6 100644 --- a/tools/virsh-completer.c +++ b/tools/virsh-completer.c @@ -34,6 +34,7 @@ #include "virmacaddr.h" #include "virstring.h" #include "virxml.h" +#include "conf/node_device_conf.h" =20 =20 /** @@ -975,6 +976,32 @@ virshNodedevEventNameCompleter(vshControl *ctl ATTRIBU= TE_UNUSED, } =20 =20 +char ** +virshNodedevCapabilityNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + VIR_AUTOSTRINGLIST tmp =3D NULL; + const char *cap_str =3D NULL; + size_t i =3D 0; + + virCheckFlags(0, NULL); + + if (vshCommandOptStringQuiet(ctl, cmd, "cap", &cap_str) < 0) + return NULL; + + if (VIR_ALLOC_N(tmp, VIR_NODE_DEV_CAP_LAST + 1) < 0) + return NULL; + + for (i =3D 0; i < VIR_NODE_DEV_CAP_LAST; i++) { + if (VIR_STRDUP(tmp[i], virNodeDevCapTypeToString(i)) < 0) + return NULL; + } + + return virshCommaStringListComplete(cap_str, (const char **)tmp); +} + + char ** virshCellnoCompleter(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED, diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h index cd3cc9ecae..d6da6e69f3 100644 --- a/tools/virsh-completer.h +++ b/tools/virsh-completer.h @@ -107,6 +107,10 @@ char ** virshNodedevEventNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); =20 +char ** virshNodedevCapabilityNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); + char ** virshDomainDeviceAliasCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c index 0dfefe3a2f..e8372a4daf 100644 --- a/tools/virsh-nodedev.c +++ b/tools/virsh-nodedev.c @@ -375,6 +375,7 @@ static const vshCmdOptDef opts_node_list_devices[] =3D { }, {.name =3D "cap", .type =3D VSH_OT_STRING, + .completer =3D virshNodedevCapabilityNameCompleter, .help =3D N_("capability names, separated by comma") }, {.name =3D NULL} --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list