Update the command argument completion callback function
__connmanctl_get_lookup_func() to accept the full command line typed
so far. In addition to comparing the input line against each command
in turn, check also that the input line ends with a space after the
command. This allows removal of the string duplication when looking
for the command.

Also, allow completion if there is a command argument completion
callback function. If none found, there is nothing to complete.
---
 client/commands.c | 14 ++++++++++++--
 client/input.c    | 24 +-----------------------
 2 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/client/commands.c b/client/commands.c
index 57a72e8..23b6de8 100644
--- a/client/commands.c
+++ b/client/commands.c
@@ -1970,10 +1970,20 @@ static int cmd_help(char *args[], int num, struct 
connman_option *options)
 
 __connmanctl_lookup_cb __connmanctl_get_lookup_func(const char *text)
 {
-       int i;
+       int i, cmdlen, textlen;
+
+       if (!text)
+               return NULL;
+
+       textlen = strlen(text);
 
        for (i = 0; cmd_table[i].cmd; i++) {
-               if (g_strcmp0(cmd_table[i].cmd, text) == 0)
+               cmdlen = strlen(cmd_table[i].cmd);
+
+               if (textlen > cmdlen && text[cmdlen] != ' ')
+                       continue;
+
+               if (strncmp(cmd_table[i].cmd, text, cmdlen) == 0)
                        return cmd_table[i].cb;
        }
 
diff --git a/client/input.c b/client/input.c
index 719101d..0af1fd4 100644
--- a/client/input.c
+++ b/client/input.c
@@ -155,18 +155,6 @@ static int calc_level(char *line)
        return count;
 }
 
-static char *get_command_name(char *line)
-{
-       char *start, *ptr;
-
-       start = ptr = line;
-
-       while (*ptr && *ptr != ' ')
-               ptr++;
-
-       return g_strndup(start, ptr - start);
-}
-
 static char **complete_command(const char *text, int start, int end)
 {
        if (start == 0) {
@@ -175,24 +163,14 @@ static char **complete_command(const char *text, int 
start, int end)
 
        } else {
                __connmanctl_lookup_cb cb;
-               char *current_command;
                char **str = NULL;
 
-               if (calc_level(rl_line_buffer) > 1) {
-                       rl_attempted_completion_over = 1;
-                       return NULL;
-               }
-
-               current_command = get_command_name(rl_line_buffer);
-
-               cb = __connmanctl_get_lookup_func(current_command);
+               cb = __connmanctl_get_lookup_func(rl_line_buffer);
                if (cb)
                        str = rl_completion_matches(text, cb);
                else
                        rl_attempted_completion_over = 1;
 
-               g_free(current_command);
-
                return str;
        }
 }
-- 
1.8.5.2

_______________________________________________
connman mailing list
connman@connman.net
https://lists.connman.net/mailman/listinfo/connman

Reply via email to