From patchwork Tue Apr 17 19:36:18 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 153307 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 F2D46B707D for ; Wed, 18 Apr 2012 06:13:42 +1000 (EST) Received: from localhost ([::1]:34574 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SKEDI-0007FS-6n for incoming@patchwork.ozlabs.org; Tue, 17 Apr 2012 15:37:20 -0400 Received: from eggs.gnu.org ([208.118.235.92]:39923) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SKECm-0005nE-Fi for qemu-devel@nongnu.org; Tue, 17 Apr 2012 15:36:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SKECh-0008RV-29 for qemu-devel@nongnu.org; Tue, 17 Apr 2012 15:36:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:29661) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SKECg-0008R8-Qh for qemu-devel@nongnu.org; Tue, 17 Apr 2012 15:36:43 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q3HJachc019839 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 17 Apr 2012 15:36:39 -0400 Received: from localhost (ovpn-113-173.phx2.redhat.com [10.3.113.173]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q3HJabcA029108; Tue, 17 Apr 2012 15:36:38 -0400 From: Luiz Capitulino To: qemu-devel@nongnu.org Date: Tue, 17 Apr 2012 16:36:18 -0300 Message-Id: <1334691381-7666-16-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1334691381-7666-1-git-send-email-lcapitulino@redhat.com> References: <1334691381-7666-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: pbonzini@redhat.com, aliguori@us.ibm.com, mdroth@linux.vnet.ibm.com, armbru@redhat.com Subject: [Qemu-devel] [PATCH 15/18] qapi: implement support for variable argument list 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 The variable argument list allows QAPI functions to receive a string in the format "name1=value1,name2=value2 ... nameN=valueN". This is going to be used by QMP commands that accept options in QemuOpts format, like netdev_add. The variable argument list is represented by the QAPI type '**'. It's declared like this in the schema: { 'command': 'cmd-foo', 'data': { 'arglist': '**' } } and the code generator will generate the following signature for cmd-foo: void cmd_foo(const char *arglist, Error **errp); The argument list can be accessed in 'arglist'. Signed-off-by: Luiz Capitulino --- scripts/qapi-commands.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/qapi.py | 2 + 2 files changed, 93 insertions(+) diff --git a/scripts/qapi-commands.py b/scripts/qapi-commands.py index 0b4f0a0..1506263 100644 --- a/scripts/qapi-commands.py +++ b/scripts/qapi-commands.py @@ -122,6 +122,10 @@ bool has_%(argname)s = false; %(argtype)s %(argname)s; ''', argname=c_var(argname), argtype=c_type(argtype)) + if argtype == '**': + ret += mcgen(''' +QString *arglist = NULL; +''') pop_indent() return ret.rstrip() @@ -146,6 +150,8 @@ v = qmp_input_get_visitor(mi); obj=obj) for argname, argtype, optional, structured in parse_args(args): + if argtype == '**': + continue if optional: ret += mcgen(''' visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp); @@ -257,6 +263,86 @@ def gen_marshal_input(name, args, ret_type, middle_mode): if (error_is_set(errp)) { goto out; } +''') + + # '**' implementation + regular_args = [] + var_list_name = "" + for argname, argtype, optional, structured in parse_args(args): + if argtype == '**': + var_list_name = argname + else: + regular_args.append(argname) + + if var_list_name: + ret += mcgen(''' + else { + const QDictEntry *entry; + + arglist = qstring_new(); + for (entry = qdict_first(args); entry; + entry = qdict_next(qdict, entry)) { + const QObject *obj = qdict_entry_value(entry); + char buf[32]; + int n; + +''') + for argname in regular_args: + ret += mcgen(''' + if (strcmp(qdict_entry_key(entry), "%(argname)s") == 0) { + continue; + } + +''', argname=argname) + + ret += mcgen(''' + if (qstring_len(arglist) > 0) { + qstring_append(arglist, ","); + } + + qstring_append(arglist, qdict_entry_key(entry)); + qstring_append(arglist, "="); + + switch(qobject_type(obj)) { + case QTYPE_QSTRING: + qstring_append(arglist, + qstring_get_str(qobject_to_qstring(obj))); + break; + case QTYPE_QINT: + n = snprintf(buf, sizeof(buf), %(arg1)s, + qint_get_int(qobject_to_qint(obj))); + assert(n < sizeof(buf)); + qstring_append(arglist, buf); + break; + case QTYPE_QFLOAT: + n = snprintf(buf, sizeof(buf), %(arg2)s, + qfloat_get_double(qobject_to_qfloat(obj))); + qstring_append(arglist, buf); + break; + case QTYPE_QBOOL: + pstrcpy(buf, sizeof(buf), + qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off"); + qstring_append(arglist, buf); + break; + default: + QDECREF(arglist); + error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, + qdict_entry_key(entry), + "string, int, float or bool"); + goto out; + } + } + + if (qstring_len(arglist) > 0) { + has_%(var_list_name)s = true; + %(var_list_name)s = qstring_get_str(arglist); + } + } + +''', var_list_name=c_var(var_list_name), arg1='"%" PRId64', arg2='"%.17g"', + arg3='"%d"') + + ret += mcgen(''' %(sync_call)s ''', sync_call=gen_sync_call(name, args, ret_type, indent=4)) @@ -270,6 +356,11 @@ out: visitor_input_block_cleanup=gen_visitor_input_block(args, None, dealloc=True)) + if var_list_name: + ret += mcgen(''' + QDECREF(arglist); +''') + if middle_mode: ret += mcgen(''' diff --git a/scripts/qapi.py b/scripts/qapi.py index e062336..9bd6e95 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -163,6 +163,8 @@ def c_type(name): return 'bool' elif name == 'number': return 'double' + elif name == '**': + return 'const char *' elif type(name) == list: return '%s *' % c_list_type(name[0]) elif is_enum(name):