From patchwork Fri Jun 28 03:39:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 255274 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BB0042C02A4 for ; Fri, 28 Jun 2013 14:07:55 +1000 (EST) Received: from localhost ([::1]:49351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsPav-0001Aj-71 for incoming@patchwork.ozlabs.org; Thu, 27 Jun 2013 23:43:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39058) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsPYr-0006dM-3A for qemu-devel@nongnu.org; Thu, 27 Jun 2013 23:41:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UsPYp-0007Ys-PO for qemu-devel@nongnu.org; Thu, 27 Jun 2013 23:41:25 -0400 Received: from e28smtp06.in.ibm.com ([122.248.162.6]:49725) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UsPYp-0007YX-4k for qemu-devel@nongnu.org; Thu, 27 Jun 2013 23:41:23 -0400 Received: from /spool/local by e28smtp06.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 28 Jun 2013 09:03:47 +0530 Received: from d28dlp01.in.ibm.com (9.184.220.126) by e28smtp06.in.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 28 Jun 2013 09:03:45 +0530 Received: from d28relay04.in.ibm.com (d28relay04.in.ibm.com [9.184.220.61]) by d28dlp01.in.ibm.com (Postfix) with ESMTP id BCE68E0053 for ; Fri, 28 Jun 2013 09:10:52 +0530 (IST) Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay04.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r5S3fDfq26345652 for ; Fri, 28 Jun 2013 09:11:13 +0530 Received: from d28av02.in.ibm.com (loopback [127.0.0.1]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r5S3fGi8026525 for ; Fri, 28 Jun 2013 13:41:17 +1000 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.127]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r5S3dhBV020808; Fri, 28 Jun 2013 13:41:15 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Fri, 28 Jun 2013 11:39:29 +0800 Message-Id: <1372390771-11157-6-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1372390771-11157-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1372390771-11157-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13062803-9574-0000-0000-0000087EAE70 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.6 Cc: pbonzini@redhat.com, Wenchao Xia , armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH V4 5/7] monitor: support sub commands in auto completion X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch allow auto completion work normal for sub command case, "info block [DEVICE]" can auto complete now, by re-enter the completion function. Also, original "info" is treated as a special case, now it is treated as a sub command group, global variable info_cmds is not used in any more. "help" command is still treated as a special case, since it is not a sub command group but want to auto complete command in root command table. parse_cmdline() takes another parameter now to tell next valid parameter's position in original cmdline. Signed-off-by: Wenchao Xia --- monitor.c | 62 +++++++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 42 insertions(+), 20 deletions(-) diff --git a/monitor.c b/monitor.c index 7cfda75..5a66fb0 100644 --- a/monitor.c +++ b/monitor.c @@ -801,9 +801,24 @@ 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 */ +/* + * 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. + * @args_cmdline: location to store char position in @cmdline correspond to + * @args include '\0', can be NULL. + * + * For example, if @cmdline = " ab" and @args_cmdline != NULL, result will be: + * pnb_args = 1, args[0] = "ab", args_cmdline[0] = "ab", + * args_cmdline[1] = "\0". + * + * NOTE: this parser is an approximate form of the real command parser. Number + * of args have a limit of MAX_ARGS. + */ static void parse_cmdline(const char *cmdline, - int *pnb_args, char **args) + int *pnb_args, char **args, const char **args_cmdline) { const char *p; int nb_args, ret; @@ -811,14 +826,14 @@ static void parse_cmdline(const char *cmdline, p = cmdline; nb_args = 0; - for (;;) { + while (nb_args < MAX_ARGS) { while (qemu_isspace(*p)) { p++; } - if (*p == '\0') { - break; + if (args_cmdline) { + args_cmdline[nb_args] = p; } - if (nb_args >= MAX_ARGS) { + if (*p == '\0') { break; } ret = get_str(buf, sizeof(buf), &p); @@ -888,7 +903,7 @@ static void help_cmd(Monitor *mon, const char *name) return; } - parse_cmdline(name, &nb_args, args); + parse_cmdline(name, &nb_args, args, NULL); if (nb_args >= MAX_ARGS) { goto cleanup; } @@ -4180,17 +4195,18 @@ static const char *next_arg_type(const char *typestr) return (p != NULL ? ++p : typestr); } -static void monitor_find_completion(Monitor *mon, - const char *cmdline) +static void monitor_find_completion_by_table(Monitor *mon, + const mon_cmd_t *cmd_table, + const char *cmdline) { - const char *cmdname; + const char *cmdname, *args_cmdline[MAX_ARGS]; char *args[MAX_ARGS]; int nb_args, i, len; const char *ptype, *str; const mon_cmd_t *cmd; MonitorBlockComplete mbs; - parse_cmdline(cmdline, &nb_args, args); + parse_cmdline(cmdline, &nb_args, args, args_cmdline); #ifdef DEBUG_COMPLETION for (i = 0; i < nb_args; i++) { monitor_printf(mon, "arg%d = '%s'\n", i, args[i]); @@ -4213,12 +4229,12 @@ static void monitor_find_completion(Monitor *mon, else cmdname = args[0]; readline_set_completion_index(mon->rs, strlen(cmdname)); - for (cmd = mon->cmd_table; cmd->name != NULL; cmd++) { + for (cmd = cmd_table; cmd->name != NULL; cmd++) { cmd_completion(mon, cmdname, cmd->name); } } else { /* find the command */ - for (cmd = mon->cmd_table; cmd->name != NULL; cmd++) { + for (cmd = cmd_table; cmd->name != NULL; cmd++) { if (compare_cmd(args[0], cmd->name)) { break; } @@ -4227,6 +4243,12 @@ static void monitor_find_completion(Monitor *mon, goto cleanup; } + if (cmd->sub_table) { + monitor_find_completion_by_table(mon, cmd->sub_table, + args_cmdline[1]); + goto cleanup; + } + ptype = next_arg_type(cmd->args_type); for(i = 0; i < nb_args - 2; i++) { if (*ptype != '\0') { @@ -4253,13 +4275,7 @@ static void monitor_find_completion(Monitor *mon, bdrv_iterate(block_completion_it, &mbs); break; case 's': - /* XXX: more generic ? */ - if (!strcmp(cmd->name, "info")) { - readline_set_completion_index(mon->rs, strlen(str)); - for(cmd = info_cmds; cmd->name != NULL; cmd++) { - cmd_completion(mon, str, cmd->name); - } - } else if (!strcmp(cmd->name, "sendkey")) { + if (!strcmp(cmd->name, "sendkey")) { char *sep = strrchr(str, '-'); if (sep) str = sep + 1; @@ -4285,6 +4301,12 @@ cleanup: } } +static void monitor_find_completion(Monitor *mon, + const char *cmdline) +{ + return monitor_find_completion_by_table(mon, mon->cmd_table, cmdline); +} + static int monitor_can_read(void *opaque) { Monitor *mon = opaque;