From: Luiz Capitulino <lcapitulino@redhat.com>
To: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Cc: pbonzini@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com
Subject: Re: [Qemu-devel] [PATCH V6 08/13] monitor: refine parse_cmdline()
Date: Wed, 17 Jul 2013 15:39:53 -0400 [thread overview]
Message-ID: <20130717153953.414cca1e@redhat.com> (raw)
In-Reply-To: <1373512429-17865-9-git-send-email-xiawenc@linux.vnet.ibm.com>
On Thu, 11 Jul 2013 11:13:44 +0800
Wenchao Xia <xiawenc@linux.vnet.ibm.com> wrote:
> 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 to as paired function to free the result.
>
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
> monitor.c | 52 ++++++++++++++++++++++++++++++++++++++--------------
> 1 files changed, 38 insertions(+), 14 deletions(-)
>
> diff --git a/monitor.c b/monitor.c
> index db63223..2d4f699 100644
> --- a/monitor.c
> +++ b/monitor.c
> @@ -801,9 +801,31 @@ static int get_str(char *buf, int buf_size, const char **pp)
>
> #define MAX_ARGS 16
>
> -/* NOTE: this parser is an approximate form of the real command parser */
> -static void parse_cmdline(const char *cmdline,
> - int *pnb_args, char **args)
> +static void free_cmdline_args(char **args, int nb_args)
> +{
> + int i;
> +
> + nb_args = nb_args < MAX_ARGS ? nb_args : MAX_ARGS;
Why is this needed? nb_args is guaranteed to be at most MAX_ARGS,
isn't it? If you really want to ensure it, then you can assert() it.
> + 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.
> + */
> +static int parse_cmdline(const char *cmdline,
> + int *pnb_args, char **args)
> {
> const char *p;
> int nb_args, ret;
> @@ -811,24 +833,26 @@ static void parse_cmdline(const char *cmdline,
>
> p = cmdline;
> nb_args = 0;
> - for (;;) {
> + while (nb_args < MAX_ARGS) {
I think it would be better to fail if nb_args > MAX_ARGS. Well, ideally
we shouldn't have any artificial limit, but I'd guess that dropping
MAX_ARGS goes a bit to far for this series' scope.
> while (qemu_isspace(*p)) {
> p++;
> }
> if (*p == '\0') {
> break;
> }
> - if (nb_args >= MAX_ARGS) {
> - break;
> - }
> ret = get_str(buf, sizeof(buf), &p);
> - args[nb_args] = g_strdup(buf);
> - nb_args++;
> if (ret < 0) {
> - break;
> + goto fail;
> }
> + args[nb_args] = g_strdup(buf);
> + 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,
> @@ -4144,7 +4168,9 @@ static void monitor_find_completion(Monitor *mon,
> const mon_cmd_t *cmd;
> MonitorBlockComplete mbs;
>
> - parse_cmdline(cmdline, &nb_args, args);
> + if (parse_cmdline(cmdline, &nb_args, args) < 0) {
> + return;
> + }
> #ifdef DEBUG_COMPLETION
> for (i = 0; i < nb_args; i++) {
> monitor_printf(mon, "arg%d = '%s'\n", i, args[i]);
> @@ -4234,9 +4260,7 @@ static void monitor_find_completion(Monitor *mon,
> }
>
> cleanup:
> - for (i = 0; i < nb_args; i++) {
> - g_free(args[i]);
> - }
> + free_cmdline_args(args, nb_args);
> }
>
> static int monitor_can_read(void *opaque)
next prev parent reply other threads:[~2013-07-17 19:51 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-11 3:13 [Qemu-devel] [PATCH V6 00/13] monitor: support sub command group in auto completion and help Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 01/13] monitor: avoid use of global *cur_mon in cmd_completion() Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 02/13] monitor: avoid use of global *cur_mon in file_completion() Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 03/13] monitor: avoid use of global *cur_mon in block_completion_it() Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 04/13] monitor: avoid use of global *cur_mon in monitor_find_completion() Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 05/13] monitor: avoid use of global *cur_mon in readline_completion() Wenchao Xia
2013-07-17 19:34 ` Luiz Capitulino
2013-07-18 1:55 ` Wenchao Xia
2013-07-18 15:02 ` Luiz Capitulino
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 06/13] monitor: avoid direct use of global variable *mon_cmds Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 07/13] monitor: code move for parse_cmdline() Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 08/13] monitor: refine parse_cmdline() Wenchao Xia
2013-07-17 19:39 ` Luiz Capitulino [this message]
2013-07-18 2:01 ` Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 09/13] monitor: support sub command in help Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 10/13] monitor: refine monitor_find_completion() Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 11/13] monitor: support sub command in auto completion Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 12/13] monitor: allow "help" show message for single command in sub group Wenchao Xia
2013-07-11 3:13 ` [Qemu-devel] [PATCH V6 13/13] monitor: improve auto complete of "help" " Wenchao Xia
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130717153953.414cca1e@redhat.com \
--to=lcapitulino@redhat.com \
--cc=armbru@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=xiawenc@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).