提交 dcc70cdf 编写于 作者: W Wenchao Xia 提交者: Luiz Capitulino

monitor: refine parse_cmdline()

Since this function will be used by help_cmd() later, so improve
it to make it more generic and easier to use. free_cmdline_args()
is added too as paired function to free the result.

One change of this function is that, when the valid args in input
exceed the limit of MAX_ARGS, it fails now, instead of return with
MAX_ARGS of parsed args in old code. This should not impact much
since it is rare that user input many args in monitor's "help" and
auto complete scenario.
Signed-off-by: NWenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Signed-off-by: NLuiz Capitulino <lcapitulino@redhat.com>
上级 f5438c05
...@@ -821,9 +821,33 @@ static int get_str(char *buf, int buf_size, const char **pp) ...@@ -821,9 +821,33 @@ static int get_str(char *buf, int buf_size, const char **pp)
#define MAX_ARGS 16 #define MAX_ARGS 16
/* NOTE: this parser is an approximate form of the real command parser */ static void free_cmdline_args(char **args, int nb_args)
static void parse_cmdline(const char *cmdline, {
int *pnb_args, char **args) int i;
assert(nb_args <= MAX_ARGS);
for (i = 0; i < nb_args; i++) {
g_free(args[i]);
}
}
/*
* Parse the command line to get valid args.
* @cmdline: command line to be parsed.
* @pnb_args: location to store the number of args, must NOT be NULL.
* @args: location to store the args, which should be freed by caller, must
* NOT be NULL.
*
* Returns 0 on success, negative on failure.
*
* NOTE: this parser is an approximate form of the real command parser. Number
* of args have a limit of MAX_ARGS. If cmdline contains more, it will
* return with failure.
*/
static int parse_cmdline(const char *cmdline,
int *pnb_args, char **args)
{ {
const char *p; const char *p;
int nb_args, ret; int nb_args, ret;
...@@ -839,16 +863,21 @@ static void parse_cmdline(const char *cmdline, ...@@ -839,16 +863,21 @@ static void parse_cmdline(const char *cmdline,
break; break;
} }
if (nb_args >= MAX_ARGS) { if (nb_args >= MAX_ARGS) {
break; goto fail;
} }
ret = get_str(buf, sizeof(buf), &p); ret = get_str(buf, sizeof(buf), &p);
args[nb_args] = g_strdup(buf);
nb_args++;
if (ret < 0) { if (ret < 0) {
break; goto fail;
} }
args[nb_args] = g_strdup(buf);
nb_args++;
} }
*pnb_args = nb_args; *pnb_args = nb_args;
return 0;
fail:
free_cmdline_args(args, nb_args);
return -1;
} }
static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds, static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
...@@ -4169,7 +4198,9 @@ static void monitor_find_completion(Monitor *mon, ...@@ -4169,7 +4198,9 @@ static void monitor_find_completion(Monitor *mon,
const mon_cmd_t *cmd; const mon_cmd_t *cmd;
MonitorBlockComplete mbs; MonitorBlockComplete mbs;
parse_cmdline(cmdline, &nb_args, args); if (parse_cmdline(cmdline, &nb_args, args) < 0) {
return;
}
#ifdef DEBUG_COMPLETION #ifdef DEBUG_COMPLETION
for (i = 0; i < nb_args; i++) { for (i = 0; i < nb_args; i++) {
monitor_printf(mon, "arg%d = '%s'\n", i, args[i]); monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
...@@ -4259,9 +4290,7 @@ static void monitor_find_completion(Monitor *mon, ...@@ -4259,9 +4290,7 @@ static void monitor_find_completion(Monitor *mon,
} }
cleanup: cleanup:
for (i = 0; i < nb_args; i++) { free_cmdline_args(args, nb_args);
g_free(args[i]);
}
} }
static int monitor_can_read(void *opaque) static int monitor_can_read(void *opaque)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册