From patchwork Fri Jan 11 09:14:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 211265 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1811F2C009A for ; Fri, 11 Jan 2013 20:53:03 +1100 (EST) Received: from localhost ([::1]:43521 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TtajE-0007G3-4G for incoming@patchwork.ozlabs.org; Fri, 11 Jan 2013 04:16:44 -0500 Received: from eggs.gnu.org ([208.118.235.92]:40346) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ttaiq-00071O-QQ for qemu-devel@nongnu.org; Fri, 11 Jan 2013 04:16:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ttaig-0005HU-Ud for qemu-devel@nongnu.org; Fri, 11 Jan 2013 04:16:20 -0500 Received: from e23smtp05.au.ibm.com ([202.81.31.147]:35408) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ttaig-0005H3-AF for qemu-devel@nongnu.org; Fri, 11 Jan 2013 04:16:10 -0500 Received: from /spool/local by e23smtp05.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 11 Jan 2013 19:12:56 +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; Fri, 11 Jan 2013 19:12:54 +1000 Received: from d23relay05.au.ibm.com (d23relay05.au.ibm.com [9.190.235.152]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id A215F2CE804F for ; Fri, 11 Jan 2013 20:16:04 +1100 (EST) Received: from d23av02.au.ibm.com (d23av02.au.ibm.com [9.190.235.138]) by d23relay05.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r0B94MBm63963362 for ; Fri, 11 Jan 2013 20:04:23 +1100 Received: from d23av02.au.ibm.com (loopback [127.0.0.1]) by d23av02.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r0B9G3mE014822 for ; Fri, 11 Jan 2013 20:16:03 +1100 Received: from RH63Wenchao (wenchaox.cn.ibm.com [9.115.122.237]) by d23av02.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r0B9ECoO011810; Fri, 11 Jan 2013 20:16:01 +1100 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Fri, 11 Jan 2013 17:14:02 +0800 Message-Id: <1357895645-30359-4-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1357895645-30359-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1357895645-30359-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13011109-1396-0000-0000-000002672B3C 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: Wenchao Xia , aliguori@us.ibm.com, armbru@redhat.com, chenwj@iis.sinica.edu.tw, lcapitulino@redhat.com Subject: [Qemu-devel] [PATCH V5 3/6] HMP: add infrastructure for sub command 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 make parsing of hmp command aware of that it may have sub command. Also discard simple encapsulation function monitor_find_command(). Signed-off-by: Wenchao Xia --- monitor.c | 48 +++++++++++++++++++++++++++++++++++++----------- 1 files changed, 37 insertions(+), 11 deletions(-) diff --git a/monitor.c b/monitor.c index 359f333..5435dc3 100644 --- a/monitor.c +++ b/monitor.c @@ -129,6 +129,11 @@ typedef struct mon_cmd_t { MonitorCompletion *cb, void *opaque); } mhandler; int flags; + /* @sub_table is a list of 2nd level of commands. If it do not exist, + * mhandler should be used. If it exist, sub_table[?].mhandler should be + * used, and mhandler of 1st level plays the role of help function. + */ + struct mon_cmd_t *sub_table; } mon_cmd_t; /* file descriptors passed via SCM_RIGHTS */ @@ -3533,21 +3538,30 @@ static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table, return NULL; } -static const mon_cmd_t *monitor_find_command(const char *cmdname) -{ - return search_dispatch_table(mon_cmds, cmdname); -} - static const mon_cmd_t *qmp_find_cmd(const char *cmdname) { return search_dispatch_table(qmp_cmds, cmdname); } +/* + * Parse @cmdline according to command table @table. + * If @cmdline is blank, return NULL. + * If it can't be parsed, report to @mon, and return NULL. + * Else, insert command arguments into @qdict, and return the command. + * If sub-command table exist, and if @cmdline contains addtional string for + * sub-command, this function will try search sub-command table. if no + * addtional string for sub-command exist, this function will return the found + * one in @table. + * Do not assume the returned command points into @table! It doesn't + * when the command is a sub-command. + */ static const mon_cmd_t *monitor_parse_command(Monitor *mon, const char *cmdline, + int start, + mon_cmd_t *table, QDict *qdict) { - const char *p, *typestr; + const char *p, *p1, *typestr; int c; const mon_cmd_t *cmd; char cmdname[256]; @@ -3555,20 +3569,32 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, char *key; #ifdef DEBUG - monitor_printf(mon, "command='%s'\n", cmdline); + monitor_printf(mon, "command='%s', start='%d'\n", cmdline, start); #endif /* extract the command name */ - p = get_command_name(cmdline, cmdname, sizeof(cmdname)); + p = get_command_name(cmdline + start, cmdname, sizeof(cmdname)); if (!p) return NULL; - cmd = monitor_find_command(cmdname); + cmd = search_dispatch_table(table, cmdname); if (!cmd) { - monitor_printf(mon, "unknown command: '%s'\n", cmdname); + monitor_printf(mon, "unknown command: '%.*s'\n", + (int)(p - cmdline), cmdline); return NULL; } + /* search sub command */ + if (cmd->sub_table != NULL) { + p1 = p; + /* check if user set additional command */ + if (*p1 == '\0') { + return cmd; + } + return monitor_parse_command(mon, cmdline, p1 - cmdline, + cmd->sub_table, qdict); + } + /* parse the parameters */ typestr = cmd->args_type; for(;;) { @@ -3924,7 +3950,7 @@ static void handle_user_command(Monitor *mon, const char *cmdline) qdict = qdict_new(); - cmd = monitor_parse_command(mon, cmdline, qdict); + cmd = monitor_parse_command(mon, cmdline, 0, mon_cmds, qdict); if (!cmd) goto out;