[libvirt] [PATCH v2 3/9] virsh: Improve readline generators and readline completion

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


On 20.08.2013 22:02, Tomas Meszaros wrote:
> * vshMalloc is now used in vshDetermineCommandName
> * vshStrdup instead of vshMalloc + snprintf
> * joined if's in vshReadlineOptionsCompletionGenerator
> ---
>  tools/virsh.c | 395 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 373 insertions(+), 22 deletions(-)
> 
> diff --git a/tools/virsh.c b/tools/virsh.c
> index 15f529e..af6e939 100644
> --- a/tools/virsh.c
> +++ b/tools/virsh.c

> @@ -2605,22 +2613,365 @@ vshReadlineOptionsGenerator(const char *text, int state)
>      return NULL;
>  }
>  
> +/*
> + * Generator function for command completion, but unlike
> + * the vshRaadlineCommandGenerator which completes command name, this function

s/vshRaadlineCommandGenerator/tools/vshReadlineCommandGenerator/  (Raad
vs Read).

> + * provides more advanced completion for commands by calling specific command
> + * completers (e.g. vshDomainCompleter).
> + */
> +static char *
> +vshReadlineCommandCompletionGenerator(const char *text, int state)
> +{
> +    static const vshCmdDef *cmd = NULL;

> +/*
> + * Returns current valid command option name present in the rl_line_buffer.
> + */
> +static char *
> +vshCurrentOpt(const char *cmd_name)
> +{
> +    const vshCmdDef *cmd = NULL;
> +    const char *name;
> +    unsigned long int opt_index = 0;
> +    size_t cmd_opt_list_index;
> +    char *found_opt = NULL;
> +    char *ptr = NULL;
> +
> +    if (!cmd_name)
> +        return NULL;
> +
> +    cmd = vshCmddefSearch(cmd_name);
> +
> +    if (!cmd)
> +        return NULL;
> +
> +    if (!cmd->opts)
> +        return NULL;
> +
> +    cmd_opt_list_index = 0;
> +
> +    while ((name = cmd->opts[cmd_opt_list_index].name)) {
> +        cmd_opt_list_index++;
> +
> +        if ((ptr = strstr(rl_line_buffer, name))) {
> +            if (opt_index < (ptr - rl_line_buffer)) {
> +                opt_index = ptr - rl_line_buffer;
> +                if (VIR_STRDUP(found_opt, name) < 0)
> +                    return NULL;
> +            }
> +        }
> +    }
> +
> +    if (!found_opt)
> +        return NULL;

this if is useless.

> +
> +    return found_opt;
> +}
> +

Michal




More information about the libvir-list mailing list