]> xenbits.xensource.com Git - libvirt.git/commitdiff
vsh: Deduplicate filtering in vshReadlineCommandGenerator()
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 3 Feb 2021 06:08:19 +0000 (07:08 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 10 Feb 2021 10:51:59 +0000 (11:51 +0100)
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 <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
tools/vsh.c

index 74123cd9ece98286d7c9763c413df238417ec4b8..42f6db3b3270f058f0bcbdf7edb3f664ae431075 100644 (file)
@@ -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) {