From patchwork Thu Oct 1 15:50:36 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 34739 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A68A7B7B96 for ; Fri, 2 Oct 2009 02:05:24 +1000 (EST) Received: from localhost ([127.0.0.1]:50964 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MtO9f-0003gj-Vd for incoming@patchwork.ozlabs.org; Thu, 01 Oct 2009 12:05:20 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MtNwG-0007Ro-AX for qemu-devel@nongnu.org; Thu, 01 Oct 2009 11:51:28 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MtNwA-0007J3-8T for qemu-devel@nongnu.org; Thu, 01 Oct 2009 11:51:27 -0400 Received: from [199.232.76.173] (port=56558 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MtNwA-0007In-1g for qemu-devel@nongnu.org; Thu, 01 Oct 2009 11:51:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44250) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MtNw9-0003d4-Eu for qemu-devel@nongnu.org; Thu, 01 Oct 2009 11:51:21 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id n91FpKKC026334; Thu, 1 Oct 2009 11:51:20 -0400 Received: from localhost (vpn-12-143.rdu.redhat.com [10.11.12.143]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id n91FpJCU008761; Thu, 1 Oct 2009 11:51:20 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Thu, 1 Oct 2009 12:50:36 -0300 Message-Id: <1254412245-10452-6-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1254412245-10452-1-git-send-email-lcapitulino@redhat.com> References: <1254412245-10452-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: aliguori@us.ibm.com, avi@redhat.com Subject: [Qemu-devel] [PATCH 05/14] monitor: Initial MonitorError usage X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This commit converts errors generated by monitor_parse_command() to use the new MonitorError style. The MonitorError used is setup by the newly introduced monitor_parse_error(). It allocates a QDict to store the command name which trigged the error and any 'extra information'. The error is printed by monitor_print_parsing_err(), it knows how to format the error message properly. As a result of this change errors are always returned and printed in only one place. Also, parsing error messages will the following standard format: : This is good, because current code does not have a defined "standard". There is a side effect though: some error messages will change. NOTE: Not all errors have been converted. Signed-off-by: Luiz Capitulino --- monitor.c | 79 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 58 insertions(+), 21 deletions(-) diff --git a/monitor.c b/monitor.c index 3f97926..2bfc592 100644 --- a/monitor.c +++ b/monitor.c @@ -217,6 +217,32 @@ static int monitor_handler_ported(const mon_cmd_t *cmd) return cmd->user_print != NULL; } +static void monitor_print_parsing_err(Monitor *mon, MonitorError *error) +{ + QDict *qdict; + + if (!monitor_has_error(error)) + return; + + assert(qobject_type(error->data) == QTYPE_QDICT); + qdict = qobject_to_qdict(error->data); + + /* Handle exceptions first */ + if (qint_get_int(error->code) == MON_ERR_UNCMD) { + monitor_printf(mon, "%s: '%s'\n", qstring_get_str(error->desc), + qdict_get_str(qdict, "name")); + return; + } + + /* standard format */ + monitor_printf(mon, "%s: ", qdict_get_str(qdict, "name")); + monitor_printf(mon, "%s", qstring_get_str(error->desc)); + if (qdict_haskey(qdict, "extra")) + monitor_printf(mon, " '%c'", (int) qdict_get_int(qdict, "extra")); + + monitor_puts(mon, "\n"); +} + static int compare_cmd(const char *name, const char *list) { const char *p, *pstart; @@ -2834,6 +2860,19 @@ static char *key_get_info(const char *type, char **key) return ++p; } +static void monitor_parse_error(MonitorError *error, int code, + const char *cmdname, int extra) +{ + QDict *qdict; + + qdict = qdict_new(); + qdict_put(qdict, "name", qstring_from_str(cmdname)); + if (extra != -1) + qdict_put(qdict, "extra", qint_from_int(extra)); + + monitor_error_set(error, code, QOBJECT(qdict), NULL); +} + static int default_fmt_format = 'x'; static int default_fmt_size = 4; @@ -2841,7 +2880,8 @@ static int default_fmt_size = 4; static const mon_cmd_t *monitor_parse_command(Monitor *mon, const char *cmdline, - QDict *qdict) + QDict *qdict, + MonitorError *error) { const char *p, *typestr; int c; @@ -2866,7 +2906,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, } if (cmd->name == NULL) { - monitor_printf(mon, "unknown command: '%s'\n", cmdname); + monitor_parse_error(error, MON_ERR_UNCMD, cmdname, -1); return NULL; } @@ -2883,7 +2923,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, case 'B': case 's': { - int ret; + int err; while (qemu_isspace(*p)) p++; @@ -2894,21 +2934,20 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, break; } } - ret = get_str(buf, sizeof(buf), &p); - if (ret < 0) { + err = get_str(buf, sizeof(buf), &p); + if (err < 0) { switch(c) { case 'F': - monitor_printf(mon, "%s: filename expected\n", - cmdname); + err = MON_ERR_EXPFILE; break; case 'B': - monitor_printf(mon, "%s: block device name expected\n", - cmdname); + err = MON_ERR_EXPBLK; break; default: - monitor_printf(mon, "%s: string expected\n", cmdname); + err = MON_ERR_EXPSTR; break; } + monitor_parse_error(error, err, cmdname, -1); goto fail; } qdict_put(qdict, key, qstring_from_str(buf)); @@ -2966,8 +3005,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, } next: if (*p != '\0' && !qemu_isspace(*p)) { - monitor_printf(mon, "invalid char in format: '%c'\n", - *p); + monitor_parse_error(error, MON_ERR_INVCHAR, cmdname,*p); goto fail; } if (format < 0) @@ -3022,8 +3060,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, goto fail; /* Check if 'i' is greater than 32-bit */ if ((c == 'i') && ((val >> 32) & 0xffffffff)) { - monitor_printf(mon, "\'%s\' has failed: ", cmdname); - monitor_printf(mon, "integer is for 32-bit values\n"); + monitor_parse_error(error, MON_ERR_INVINT, cmdname, -1); goto fail; } qdict_put(qdict, key, qint_from_int(val)); @@ -3043,8 +3080,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, if (*p == '-') { p++; if (*p != c) { - monitor_printf(mon, "%s: unsupported option -%c\n", - cmdname, *p); + monitor_parse_error(error, MON_ERR_UNSOPT, cmdname, *p); goto fail; } p++; @@ -3055,7 +3091,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, break; default: bad_type: - monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c); + monitor_parse_error(error, MON_ERR_UNTYPE, cmdname, c); goto fail; } qemu_free(key); @@ -3065,8 +3101,7 @@ static const mon_cmd_t *monitor_parse_command(Monitor *mon, while (qemu_isspace(*p)) p++; if (*p != '\0') { - monitor_printf(mon, "%s: extraneous characters at the end of line\n", - cmdname); + monitor_parse_error(error, MON_ERR_EXTCHAR, cmdname, -1); goto fail; } @@ -3086,9 +3121,11 @@ static void monitor_handle_command(Monitor *mon, const char *cmdline) qdict = qdict_new(); error = monitor_error_new(); - cmd = monitor_parse_command(mon, cmdline, qdict); - if (!cmd) + cmd = monitor_parse_command(mon, cmdline, qdict, error); + if (!cmd) { + monitor_print_parsing_err(mon, error); goto out; + } qemu_errors_to_mon(mon);