[PATCH 09/12] vsh: Deduplicate filtering in vshReadlineCommandGenerator()

Michal Privoznik mprivozn at redhat.com
Thu Feb 4 14:13:34 UTC 2021


This is what we do for completer callbacks: we let them 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).

The same algorithm is implemented in
vshReadlineCommandGenerator().  There is no need to have the same
code twice.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/vsh.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/tools/vsh.c b/tools/vsh.c
index 5f082f1a35..e0dbda04c8 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) {
-- 
2.26.2




More information about the libvir-list mailing list