From patchwork Thu Jun 27 03:27:44 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 254932 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 4450C2C009D for ; Thu, 27 Jun 2013 13:31:12 +1000 (EST) Received: from localhost ([::1]:51426 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Us2vN-00050y-9l for incoming@patchwork.ozlabs.org; Wed, 26 Jun 2013 23:31:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45358) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Us2ul-0004ui-FP for qemu-devel@nongnu.org; Wed, 26 Jun 2013 23:30:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Us2uj-0000yg-UG for qemu-devel@nongnu.org; Wed, 26 Jun 2013 23:30:31 -0400 Received: from e23smtp05.au.ibm.com ([202.81.31.147]:40280) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Us2uj-0000x5-98 for qemu-devel@nongnu.org; Wed, 26 Jun 2013 23:30:29 -0400 Received: from /spool/local by e23smtp05.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 27 Jun 2013 13:24:18 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp05.au.ibm.com (202.81.31.211) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 27 Jun 2013 13:24:14 +1000 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [9.190.235.21]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id C7E312CE8051 for ; Thu, 27 Jun 2013 13:30:23 +1000 (EST) Received: from d23av01.au.ibm.com (d23av01.au.ibm.com [9.190.234.96]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r5R3UEOA8585646 for ; Thu, 27 Jun 2013 13:30:14 +1000 Received: from d23av01.au.ibm.com (loopback [127.0.0.1]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r5R3UNg2030120 for ; Thu, 27 Jun 2013 13:30:23 +1000 Received: from RH63Wenchao.localdomain ([9.77.178.220]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r5R3RvIe024842; Thu, 27 Jun 2013 13:30:21 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Thu, 27 Jun 2013 11:27:44 +0800 Message-Id: <1372303666-12323-6-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1372303666-12323-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1372303666-12323-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13062703-1396-0000-0000-0000032D44F0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.147 Cc: pbonzini@redhat.com, Wenchao Xia , armbru@redhat.com, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH V3 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 aa641de..e0a4b67 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; } @@ -4179,17 +4194,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, (char *)args[i]); @@ -4212,12 +4228,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; } @@ -4226,6 +4242,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') { @@ -4252,13 +4274,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; @@ -4284,6 +4300,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;