diff options
author | Patrik Flykt <patrik.flykt@linux.intel.com> | 2013-02-12 21:57:23 +0200 |
---|---|---|
committer | Patrik Flykt <patrik.flykt@linux.intel.com> | 2013-02-21 16:22:25 +0200 |
commit | c27864e1c53eeddb5271eab60b4fa92e7938f837 (patch) | |
tree | 57f4c847549a1f212a064ae415f4f1553b3217ba | |
parent | 9cab759afb312f5c419de2ec8351717c92859248 (diff) | |
download | connman-c27864e1c53eeddb5271eab60b4fa92e7938f837.tar.gz connman-c27864e1c53eeddb5271eab60b4fa92e7938f837.tar.bz2 connman-c27864e1c53eeddb5271eab60b4fa92e7938f837.zip |
client: Fix up interactive option parsing
Use g_strsplit to get a NULL terminated array of chars and compute the
number of array items.
-rw-r--r-- | client/interactive.c | 63 |
1 files changed, 21 insertions, 42 deletions
diff --git a/client/interactive.c b/client/interactive.c index 5f9c77a5..200f3dcb 100644 --- a/client/interactive.c +++ b/client/interactive.c @@ -48,27 +48,10 @@ static DBusConnection *interactive_conn; -static char **parse_long(char *input, int *num_args) -{ - int i; - char **token = NULL; - - for (i = 0; input != NULL; i++) { - token = realloc(token, (i + 1) * sizeof(char *)); - if (token == NULL) - return NULL; - token[i] = strdup(input); - input = strtok(NULL, " "); - } - *num_args = i; - - return token; -} - static gboolean rl_handler(char *input) { - char **long_args; - int num_args, i, error; + char **long_args = NULL; + int num_args, error; num_args = 0; if (input == NULL) { @@ -77,33 +60,31 @@ static gboolean rl_handler(char *input) } add_history(input); - input = strtok(input, " "); + long_args = g_strsplit(input, " ", 0); - if (input == NULL) + if (long_args == NULL || long_args[0] == NULL) { + g_strfreev(long_args); + free(input); return FALSE; - long_args = parse_long(input, &num_args); + } - if (long_args == NULL) { - free(input); - exit(EXIT_FAILURE); - } else { - error = commands(interactive_conn, long_args, num_args); - if (error == -1) { - error = commands_no_options(interactive_conn, - long_args, num_args); - if (error == -1) - error = commands_options(interactive_conn, - long_args, num_args); - else - return error; - } + for (num_args = 0; long_args[num_args] != NULL; num_args++); + + error = commands(interactive_conn, long_args, num_args); + if (error == -1) { + error = commands_no_options(interactive_conn, long_args, + num_args); + if (error == -1) + error = commands_options(interactive_conn, long_args, + num_args); + else + return error; } + if ((strcmp(long_args[0], "quit") == 0) || (strcmp(long_args[0], "exit") == 0) || (strcmp(long_args[0], "q") == 0)) { - for (i = 0; i < num_args; i++) - free(long_args[i]); - free(long_args); + g_strfreev(long_args); exit(EXIT_SUCCESS); } if (error == -1) { @@ -111,9 +92,7 @@ static gboolean rl_handler(char *input) long_args[0]); } - for (i = 0; i < num_args; i++) - free(long_args[i]); - free(long_args); + g_strfreev(long_args); optind = 0; return TRUE; |