diff mbox

[04/14] monitor: Handle new and old style handlers

Message ID 1254412245-10452-5-git-send-email-lcapitulino@redhat.com
State Superseded
Headers show

Commit Message

Luiz Capitulino Oct. 1, 2009, 3:50 p.m. UTC
This commit changes monitor_handle_command() to support old style
_and_ new style handlers.

New style handlers are protocol independent, they return their data
to the Monitor, which in turn decides how to print them
(ie. user protocol vs. machine protocol).

Old style handlers are called as usual.

Converted handlers will use both the 'user_print' and 'user_error'
members of 'mon_cmd_t' to define its user protocol functions, which
will be called to print data in the user protocol format.

Also note that we are allocating a MonitoError but not using it
yet, this will be done by the next commits.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)
diff mbox

Patch

diff --git a/monitor.c b/monitor.c
index 5cb6e81..3f97926 100644
--- a/monitor.c
+++ b/monitor.c
@@ -212,6 +212,11 @@  static int monitor_fprintf(FILE *stream, const char *fmt, ...)
     return 0;
 }
 
+static int monitor_handler_ported(const mon_cmd_t *cmd)
+{
+    return cmd->user_print != NULL;
+}
+
 static int compare_cmd(const char *name, const char *list)
 {
     const char *p, *pstart;
@@ -3075,23 +3080,44 @@  fail:
 static void monitor_handle_command(Monitor *mon, const char *cmdline)
 {
     QDict *qdict;
+    MonitorError *error;
     const mon_cmd_t *cmd;
 
     qdict = qdict_new();
+    error = monitor_error_new();
 
     cmd = monitor_parse_command(mon, cmdline, qdict);
-    if (cmd) {
-        void (*handler)(Monitor *mon, const QDict *qdict);
+    if (!cmd)
+        goto out;
 
-        qemu_errors_to_mon(mon);
+    qemu_errors_to_mon(mon);
 
-        handler = cmd->handler;
-        handler(mon, qdict);
+    if (monitor_handler_ported(cmd)) {
+        QObject *data = NULL;
+        void (*handler_new)(Monitor *mon, const QDict *params,
+                            QObject **ret_data, MonitorError *error);
 
-        qemu_errors_to_previous();
+        handler_new = cmd->handler;
+        handler_new(mon, qdict, &data, error);
+
+        if (monitor_has_error(error)) {
+            cmd->user_error(mon, error);
+        } else {
+            cmd->user_print(mon, data);
+        }
+
+        qobject_decref(data);
+    } else {
+        void (*handler_old)(Monitor *mon, const QDict *qdict);
+        handler_old = cmd->handler;
+        handler_old(mon, qdict);
     }
 
+   qemu_errors_to_previous();
+
+out:
     QDECREF(qdict);
+    monitor_error_free(error);
 }
 
 static void cmd_completion(const char *name, const char *list)