From 9a77f93f6fb1d1fc9be314c1d91704e8cffc60d3 Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Thu, 13 Dec 2012 17:56:32 +0100 Subject: [PATCH] client: Add command completion support --- client/main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/client/main.c b/client/main.c index 6026e09cd..ea13165fc 100644 --- a/client/main.c +++ b/client/main.c @@ -223,11 +223,43 @@ static const struct { { "info", cmd_info, "Controller info" }, { "quit", cmd_quit, "Quit program" }, { "exit", cmd_quit }, + { "help" }, { } }; +static char *cmd_generator(const char *text, int state) +{ + static int index, len; + const char *str; + + if (!state) { + index = 0; + len = strlen(text); + } + + while ((str = cmd_table[index].str)) { + index++; + + if (!strncmp(str, text, len)) + return strdup(str); + } + + return NULL; +} + +static char **cmd_completion(const char *text, int start, int end) +{ + if (start > 0) { + rl_attempted_completion_over = 1; + return NULL; + } + + return rl_completion_matches(text, cmd_generator); +} + static void rl_handler(char *input) { + char *cmd; int i; if (!input) { @@ -243,15 +275,21 @@ static void rl_handler(char *input) add_history(input); + cmd = strtok(input, " "); + if (cmd == NULL) + return; + for (i = 0; cmd_table[i].str; i++) { - if (strcmp(input, cmd_table[i].str)) + if (strcmp(cmd, cmd_table[i].str)) continue; - cmd_table[i].func(cmd_table[i].str); - return; + if (cmd_table[i].func) { + cmd_table[i].func(input); + return; + } } - if (strcmp(input, "help")) { + if (strcmp(cmd, "help")) { printf("Invalid command\n"); return; } @@ -403,6 +441,8 @@ int main(int argc, char *argv[]) main_loop = g_main_loop_new(NULL, FALSE); dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL); + rl_attempted_completion_function = cmd_completion; + rl_erase_empty_line = 1; rl_callback_handler_install(NULL, rl_handler); -- 2.47.3