[libvirt] [PATCH v2 4/9] virsh: Add vshDomainCompleter

Michal Privoznik mprivozn at redhat.com
Thu Aug 22 11:46:32 UTC 2013


On 20.08.2013 22:02, Tomas Meszaros wrote:
> * global variable __my_conn renamed to vshConn
> * @name is now const char *
> * label cleanup renamed to error
> ---
>  tools/virsh.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  tools/virsh.h |  2 ++
>  2 files changed, 55 insertions(+)


You can be adding .copleter = vshDomainCompleter in this patch as well.
That is squash parts of 7/9, 8/9 and 9/9 which add the
vshDomainCompleter into this patch. In the end, They will probably end
up being empty, but that is okay.


> diff --git a/tools/virsh.c b/tools/virsh.c
> index af6e939..13c27df 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c
> @@ -88,6 +88,8 @@ static char *progname;
>  
>  static const vshCmdGrp cmdGroups[];
>  
> +virConnectPtr *vshConn;
> +
>  /* Bypass header poison */
>  #undef strdup
>  
> @@ -2503,6 +2505,51 @@ vshCloseLogFile(vshControl *ctl)
>  
>  #ifdef USE_READLINE
>  
> +/* -------------
> + * Completers
> + * -------------
> + */
> +
> +char **
> +vshDomainCompleter(unsigned int flags)
> +{
> +    virDomainPtr *domains;
> +    size_t i;
> +    char **names = NULL;
> +    int ndomains;
> +
> +    if (!*vshConn)
> +        return NULL;
> +
> +    ndomains = virConnectListAllDomains(*vshConn, &domains, flags);
> +
> +    if (ndomains < 0)
> +        return NULL;
> +
> +    names = vshMalloc(NULL, sizeof(char *) * (ndomains + 1));
> +
> +    if (!names)
> +        return NULL;

Well, we should never get to this situation. vshMalloc either fails and
exit() or return a valid pointer.

> +
> +    for (i = 0; i < ndomains; i++) {
> +        const char *name = virDomainGetName(domains[i]);
> +        if (VIR_STRDUP(names[i], name) < 0) {
> +            virDomainFree(domains[i]);
> +            goto error;
> +        }
> +        virDomainFree(domains[i]);
> +    }
> +    names[i] = NULL;
> +    VIR_FREE(domains);
> +    return names;
> +
> +error:
> +    for (i = 0; names[i]; i++)
> +        VIR_FREE(names[i]);
> +    VIR_FREE(names);
> +    return NULL;
> +}
> +
>  /* -----------------
>   * Readline stuff
>   * -----------------
> @@ -3510,6 +3557,7 @@ main(int argc, char **argv)
>      ctl->debug = VSH_DEBUG_DEFAULT;
>      ctl->escapeChar = "^]";     /* Same default as telnet */
>  
> +    vshConn = &ctl->conn;
>  
>      if (!setlocale(LC_ALL, "")) {
>          perror("setlocale");
> @@ -3571,6 +3619,11 @@ main(int argc, char **argv)
>              exit(EXIT_FAILURE);
>          }
>  
> +        /* Need to connect immediately after start in order to provide
> +         * autocompletion for the very first command.
> +         */
> +        vshReconnect(ctl);
> +

This has been discussed so see Eric's and Peter's replies.

>          do {
>              const char *prompt = ctl->readonly ? VSH_PROMPT_RO : VSH_PROMPT_RW;
>              ctl->cmdstr =
> diff --git a/tools/virsh.h b/tools/virsh.h
> index 064acde..68414e4 100644
> --- a/tools/virsh.h
> +++ b/tools/virsh.h
> @@ -255,6 +255,8 @@ struct _vshCmdGrp {
>      const vshCmdDef *commands;
>  };
>  
> +char **vshDomainCompleter(unsigned int flags);
> +
>  void vshError(vshControl *ctl, const char *format, ...)
>      ATTRIBUTE_FMT_PRINTF(2, 3);
>  void vshOpenLogFile(vshControl *ctl);
> 

Michal




More information about the libvir-list mailing list