cli: don't return empty strings in nmc_string_to_arg_array()

and unquote strings in the array if required.

(cherry picked from commit 661ef3cd46)
This commit is contained in:
Jiří Klimeš 2015-03-12 14:11:18 +01:00
parent a702b550f0
commit 12a68a523b
3 changed files with 27 additions and 9 deletions

View file

@ -2334,7 +2334,7 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
if (argc == 0) {
if (nmc->ask) {
line = nmc_readline (PROMPT_CONNECTION);
nmc_string_to_arg_array (line, "", &arg_arr, &arg_num);
nmc_string_to_arg_array (line, "", TRUE, &arg_arr, &arg_num);
g_free (line);
arg_ptr = arg_arr;
}
@ -8834,7 +8834,7 @@ do_connection_delete (NmCli *nmc, int argc, char **argv)
if (argc == 0) {
if (nmc->ask) {
char *line = nmc_readline (PROMPT_CONNECTION);
nmc_string_to_arg_array (line, "", &arg_arr, &arg_num);
nmc_string_to_arg_array (line, "", TRUE, &arg_arr, &arg_num);
g_free (line);
arg_ptr = arg_arr;
}

View file

@ -454,17 +454,34 @@ nmc_get_user_input (const char *ask_str)
* Split string in 'line' according to 'delim' to (argument) array.
*/
int
nmc_string_to_arg_array (const char *line, const char *delim, char ***argv, int *argc)
nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
char ***argv, int *argc)
{
int i = 0;
char **arr;
arr = g_strsplit_set (line ? line : "", delim ? delim : " \t", 0);
while (arr && arr[i])
i++;
arr = nmc_strsplit_set (line ? line : "", delim ? delim : " \t", 0);
if (unquote) {
int i = 0;
char *s;
size_t l;
const char *quotes = "\"'";
while (arr && arr[i]) {
s = arr[i];
l = strlen (s);
if (l >= 2) {
if (strchr (quotes, s[0]) && s[l-1] == s[0]) {
memmove (s, s+1, l-2);
s[l-2] = '\0';
}
}
i++;
}
}
*argc = i;
*argv = arr;
*argc = g_strv_length (arr);
return 0;
}

View file

@ -69,7 +69,8 @@ void nmc_terminal_show_progress (const char *str);
const char *nmc_term_color_sequence (NmcTermColor color);
char *nmc_colorize (NmcTermColor color, const char * fmt, ...);
char *nmc_get_user_input (const char *ask_str);
int nmc_string_to_arg_array (const char *line, const char *delim, char ***argv, int *argc);
int nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
char ***argv, int *argc);
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);
GSList *nmc_util_strv_to_slist (char **strv);
char * nmc_util_strv_for_display (const char **strv, gboolean brackets);