From nobody Thu May 15 09:41:26 2025 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 1510589219141182.50993345132304; Mon, 13 Nov 2017 08:06:59 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 634132DACF2; Mon, 13 Nov 2017 16:06:57 +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 237A6832A7; Mon, 13 Nov 2017 16:06:57 +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 E2D4814B2F; Mon, 13 Nov 2017 16:06:56 +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 vADG6op7016769 for ; Mon, 13 Nov 2017 11:06:50 -0500 Received: by smtp.corp.redhat.com (Postfix) id 37D9B83865; Mon, 13 Nov 2017 16:06:50 +0000 (UTC) Received: from localhost.localdomain (ovpn-204-194.brq.redhat.com [10.40.204.194]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DAF583876 for ; Mon, 13 Nov 2017 16:06:49 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Mon, 13 Nov 2017 17:06:28 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 08/12] vsh: Introduce complete command 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 13 Nov 2017 16:06:58 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This command is going to be called from bash completion script in the following form: virsh complete "start --domain" Its only purpose is to return list of possible strings for completion. Note that this is a 'hidden', unlisted command and therefore there's no documentation to it. Signed-off-by: Michal Privoznik --- tools/virsh.c | 1 + tools/virt-admin.c | 1 + tools/vsh.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ tools/vsh.h | 14 ++++++++++ 4 files changed, 94 insertions(+) diff --git a/tools/virsh.c b/tools/virsh.c index d0c135016..0d44eb301 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -831,6 +831,7 @@ static const vshCmdDef virshCmds[] =3D { VSH_CMD_PWD, VSH_CMD_QUIT, VSH_CMD_SELF_TEST, + VSH_CMD_COMPLETE, {.name =3D "connect", .handler =3D cmdConnect, .opts =3D opts_connect, diff --git a/tools/virt-admin.c b/tools/virt-admin.c index b8b33af19..ef5bada63 100644 --- a/tools/virt-admin.c +++ b/tools/virt-admin.c @@ -1351,6 +1351,7 @@ static const vshCmdDef vshAdmCmds[] =3D { VSH_CMD_PWD, VSH_CMD_QUIT, VSH_CMD_SELF_TEST, + VSH_CMD_COMPLETE, {.name =3D "uri", .handler =3D cmdURI, .opts =3D NULL, diff --git a/tools/vsh.c b/tools/vsh.c index 9cf85b180..2f0914c59 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -3446,3 +3446,81 @@ cmdSelfTest(vshControl *ctl ATTRIBUTE_UNUSED, =20 return true; } + +/* ---------------------- + * Autocompletion command + * ---------------------- */ + +const vshCmdOptDef opts_complete[] =3D { + {.name =3D "string", + .type =3D VSH_OT_ARGV, + .flags =3D VSH_OFLAG_EMPTY_OK, + .help =3D N_("partial string to autocomplete") + }, + {.name =3D NULL} +}; + +const vshCmdInfo info_complete[] =3D { + {.name =3D "help", + .data =3D N_("internal command for autocompletion") + }, + {.name =3D "desc", + .data =3D N_("internal use only") + }, + {.name =3D NULL} +}; + +bool +cmdComplete(vshControl *ctl, const vshCmd *cmd) +{ + bool ret =3D false; +#ifdef WITH_READLINE + const vshClientHooks *hooks =3D ctl->hooks; + int stdin_fileno =3D STDIN_FILENO; + const char *arg =3D ""; + const vshCmdOpt *opt =3D NULL; + char **matches =3D NULL, *tmp =3D NULL, **iter; + virBuffer buf =3D VIR_BUFFER_INITIALIZER; + + if (vshCommandOptStringQuiet(ctl, cmd, "string", &arg) <=3D 0) + goto cleanup; + + /* This command is flagged VSH_CMD_FLAG_NOCONNECT because we + * need to prevent auth hooks reading any input. Therefore we + * have to close stdin and then connect ourselves. */ + VIR_FORCE_CLOSE(stdin_fileno); + + if (!(hooks && hooks->connHandler && hooks->connHandler(ctl))) + goto cleanup; + + while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) { + if (virBufferUse(&buf) !=3D 0) + virBufferAddChar(&buf, ' '); + virBufferAddStr(&buf, opt->data); + arg =3D opt->data; + } + + if (virBufferCheckError(&buf) < 0) + goto cleanup; + + vshReadlineInit(ctl); + + if (!(rl_line_buffer =3D virBufferContentAndReset(&buf)) && + VIR_STRDUP(rl_line_buffer, "") < 0) + goto cleanup; + + if (!(matches =3D vshReadlineCompletion(arg, 0, 0))) + goto cleanup; + + for (iter =3D matches; *iter; iter++) + printf("%s\n", *iter); + + ret =3D true; + cleanup: + virBufferFreeAndReset(&buf); + for (iter =3D matches; iter && *iter; iter++) + VIR_FREE(*iter); + VIR_FREE(matches); +#endif /* WITH_READLINE */ + return ret; +} diff --git a/tools/vsh.h b/tools/vsh.h index ae40fb4e8..6894700d9 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -382,6 +382,8 @@ extern const vshCmdInfo info_echo[]; extern const vshCmdInfo info_pwd[]; extern const vshCmdInfo info_quit[]; extern const vshCmdInfo info_selftest[]; +extern const vshCmdOptDef opts_complete[]; +extern const vshCmdInfo info_complete[]; =20 bool cmdHelp(vshControl *ctl, const vshCmd *cmd); bool cmdCd(vshControl *ctl, const vshCmd *cmd); @@ -389,6 +391,7 @@ bool cmdEcho(vshControl *ctl, const vshCmd *cmd); bool cmdPwd(vshControl *ctl, const vshCmd *cmd); bool cmdQuit(vshControl *ctl, const vshCmd *cmd); bool cmdSelfTest(vshControl *ctl, const vshCmd *cmd); +bool cmdComplete(vshControl *ctl, const vshCmd *cmd); =20 # define VSH_CMD_CD \ { \ @@ -454,6 +457,17 @@ bool cmdSelfTest(vshControl *ctl, const vshCmd *cmd); .alias =3D "self-test" \ } =20 +# define VSH_CMD_COMPLETE \ + { \ + .name =3D "complete", \ + .handler =3D cmdComplete, \ + .opts =3D opts_complete, \ + .info =3D info_complete, \ + .flags =3D VSH_CMD_FLAG_NOCONNECT | VSH_CMD_FLAG_ALIAS, \ + .alias =3D "complete" \ + } + + =20 /* readline */ char * vshReadline(vshControl *ctl, const char *prompt); --=20 2.13.6 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list