qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: Wenchao Xia <xiawenc@linux.vnet.ibm.com>,
	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
Date: Fri, 11 Jan 2013 17:14:02 +0800	[thread overview]
Message-ID: <1357895645-30359-4-git-send-email-xiawenc@linux.vnet.ibm.com> (raw)
In-Reply-To: <1357895645-30359-1-git-send-email-xiawenc@linux.vnet.ibm.com>

  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 <xiawenc@linux.vnet.ibm.com>
---
 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;
 
-- 
1.7.1

  parent reply	other threads:[~2013-01-11  9:16 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-11  9:13 [Qemu-devel] [PATCH V5 0/6] HMP: allow parsing for sub command Wenchao Xia
2013-01-11  9:14 ` [Qemu-devel] [PATCH V5 1/6] HMP: add QDict to info callback handler Wenchao Xia
2013-01-11  9:14 ` [Qemu-devel] [PATCH V5 2/6] HMP: delete info handler Wenchao Xia
2013-01-11  9:14 ` Wenchao Xia [this message]
2013-01-11  9:14 ` [Qemu-devel] [PATCH V5 4/6] HMP: filter out space before check of sub-command Wenchao Xia
2013-01-11 20:12   ` Luiz Capitulino
2013-01-14  2:11     ` Wenchao Xia
2013-01-14 13:46       ` Markus Armbruster
2013-01-14 15:33         ` Luiz Capitulino
2013-01-15  3:06         ` Wenchao Xia
2013-01-11  9:14 ` [Qemu-devel] [PATCH V5 5/6] HMP: move define of mon_cmds Wenchao Xia
2013-01-11  9:14 ` [Qemu-devel] [PATCH V5 6/6] HMP: add sub command table to info 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=1357895645-30359-4-git-send-email-xiawenc@linux.vnet.ibm.com \
    --to=xiawenc@linux.vnet.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=armbru@redhat.com \
    --cc=chenwj@iis.sinica.edu.tw \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).