client: Add command completion
authorPatrik Flykt <patrik.flykt@linux.intel.com>
Thu, 18 Apr 2013 12:28:21 +0000 (15:28 +0300)
committerPatrik Flykt <patrik.flykt@linux.intel.com>
Tue, 23 Apr 2013 09:36:09 +0000 (12:36 +0300)
Add completion support for connmanctl commands.

client/commands.c
client/commands.h
client/input.c

index 8b4ca53..32a8220 100644 (file)
@@ -1277,3 +1277,25 @@ int __connmanctl_commands(DBusConnection *dbus_conn, char *argv[], int argc)
        fprintf(stderr, "Error '%s': Unknown command\n", argv[0]);
        return -EINVAL;
 }
+
+char *__connmanctl_lookup_command(const char *text, int state)
+{
+       static int i = 0;
+       static int len = 0;
+
+       if (state == 0) {
+               i = 0;
+               len = strlen(text);
+       }
+
+       while (cmd_table[i].cmd != NULL) {
+               const char *command = cmd_table[i].cmd;
+
+               i++;
+
+               if (strncmp(text, command, len) == 0)
+                       return strdup(command);
+       }
+
+       return NULL;
+}
index ce9ceca..6ae9029 100644 (file)
@@ -23,3 +23,4 @@
 #include <dbus/dbus.h>
 
 int __connmanctl_commands(DBusConnection *connection, char *argv[], int argc);
+char *__connmanctl_lookup_command(const char *text, int state);
index f1aa0d3..20ce7f9 100644 (file)
@@ -122,6 +122,19 @@ static gboolean input_handler(GIOChannel *channel, GIOCondition condition,
        return TRUE;
 }
 
+static char **complete_command(const char *text, int start, int end)
+{
+       char **command = NULL;
+
+       rl_attempted_completion_over = 1;
+
+       if (start == 0)
+               command = rl_completion_matches(text,
+                               __connmanctl_lookup_command);
+
+       return command;
+}
+
 int __connmanctl_input_init(int argc, char *argv[])
 {
        char *help[] = {
@@ -153,6 +166,7 @@ int __connmanctl_input_init(int argc, char *argv[])
                g_io_channel_unref(channel);
 
                rl_callback_handler_install("connmanctl> ", rl_handler);
+               rl_attempted_completion_function = complete_command;
                err = -EINPROGRESS;
 
        } else {