From b1eab47c2dc1a665710d16ff52ad051dc7f69f77 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Wed, 3 Feb 2021 07:08:19 +0100 Subject: [PATCH] vsh: Deduplicate filtering in vshReadlineCommandGenerator() Completer callbacks generate all possible outputs ignoring any partial input (e.g. prefix of a domain name) and then use vshCompleterFilter() to filter out those strings which don't fit the partial input (prefix). In contrast, vshReadlineCommandGenerator() does some internal filtering and only generates completions that match a given prefix. Rather than treating these scenarios differently, simply generate all possible options and filter them all at the end. Signed-off-by: Michal Privoznik Reviewed-by: Jonathon Jongsma --- tools/vsh.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tools/vsh.c b/tools/vsh.c index 74123cd9ec..42f6db3b32 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2561,11 +2561,10 @@ vshTreePrint(vshControl *ctl, vshTreeLookup lookup, void *opaque, * * Generator function for command completion. * - * Returns a string list of commands with @text prefix, - * NULL if there's no such command. + * Returns a string list of all commands, or NULL on failure. */ static char ** -vshReadlineCommandGenerator(const char *text) +vshReadlineCommandGenerator(const char *text G_GNUC_UNUSED) { size_t grp_list_index = 0, cmd_list_index = 0; const char *name; @@ -2587,15 +2586,13 @@ vshReadlineCommandGenerator(const char *text) if (cmds[cmd_list_index++].flags & VSH_CMD_FLAG_ALIAS) continue; - if (STRPREFIX(name, text)) { - if (VIR_REALLOC_N(ret, ret_size + 2) < 0) - return NULL; + if (VIR_REALLOC_N(ret, ret_size + 2) < 0) + return NULL; - ret[ret_size] = g_strdup(name); - ret_size++; - /* Terminate the string list properly. */ - ret[ret_size] = NULL; - } + ret[ret_size] = g_strdup(name); + ret_size++; + /* Terminate the string list properly. */ + ret[ret_size] = NULL; } } else { cmd_list_index = 0; @@ -2782,13 +2779,13 @@ vshReadlineParse(const char *text, int state) if (virStringListMerge(&list, &completer_list) < 0) goto cleanup; } - - /* For string list returned by completers we have to do - * filtering based on @text because completers returns all - * possible strings. */ - if (vshCompleterFilter(&list, text) < 0) - goto cleanup; } + + /* For string list returned by completers we have to do + * filtering based on @text because completers returns all + * possible strings. */ + if (vshCompleterFilter(&list, text) < 0) + goto cleanup; } if (list) {