2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
7 # Anthony Liguori <aliguori@us.ibm.com>
8 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # This work is licensed under the terms of the GNU GPLv2.
11 # See the COPYING.LIB file in the top-level directory.
13 from ordereddict
import OrderedDict
20 def type_visitor(name
):
21 if type(name
) == list:
22 return 'visit_type_%sList' % name
[0]
24 return 'visit_type_%s' % name
26 def generate_decl_enum(name
, members
, genlist
=True):
29 void %(visitor)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
31 visitor
=type_visitor(name
))
33 def generate_command_decl(name
, args
, ret_type
):
35 for argname
, argtype
, optional
, structured
in parse_args(args
):
36 argtype
= c_type(argtype
)
37 if argtype
== "char *":
38 argtype
= "const char *"
40 arglist
+= "bool has_%s, " % c_var(argname
)
41 arglist
+= "%s %s, " % (argtype
, c_var(argname
))
43 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
45 ret_type
=c_type(ret_type
), name
=c_fun(name
), args
=arglist
).strip()
47 def gen_sync_call(name
, args
, ret_type
, indent
=0):
53 for argname
, argtype
, optional
, structured
in parse_args(args
):
55 arglist
+= "has_%s, " % c_var(argname
)
56 arglist
+= "%s, " % (c_var(argname
))
59 %(retval)sqmp_%(name)s(%(args)serrp);
62 name
=c_fun(name
), args
=arglist
, retval
=retval
).rstrip()
64 ret
+= "\n" + mcgen(''''
65 if (!error_is_set(errp)) {
66 %(marshal_output_call)s
69 marshal_output_call
=gen_marshal_output_call(name
, ret_type
)).rstrip()
74 def gen_marshal_output_call(name
, ret_type
):
77 return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name
)
79 def gen_visitor_output_containers_decl(ret_type
):
85 QapiDeallocVisitor *md;
92 def gen_visitor_input_containers_decl(args
):
99 QapiDeallocVisitor *md;
106 def gen_visitor_input_vars_decl(args
):
109 for argname
, argtype
, optional
, structured
in parse_args(args
):
112 bool has_%(argname)s = false;
114 argname
=c_var(argname
))
115 if c_type(argtype
).endswith("*"):
117 %(argtype)s %(argname)s = NULL;
119 argname
=c_var(argname
), argtype
=c_type(argtype
))
122 %(argtype)s %(argname)s;
124 argname
=c_var(argname
), argtype
=c_type(argtype
))
129 def gen_visitor_input_block(args
, obj
, dealloc
=False):
138 md = qapi_dealloc_visitor_new();
139 v = qapi_dealloc_get_visitor(md);
143 mi = qmp_input_visitor_new_strict(%(obj)s);
144 v = qmp_input_get_visitor(mi);
148 for argname
, argtype
, optional
, structured
in parse_args(args
):
151 visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp);
152 if (has_%(c_name)s) {
154 c_name
=c_var(argname
), name
=argname
)
157 %(visitor)s(v, &%(c_name)s, "%(name)s", errp);
159 c_name
=c_var(argname
), name
=argname
, argtype
=argtype
,
160 visitor
=type_visitor(argtype
))
165 visit_end_optional(v, errp);
170 qapi_dealloc_visitor_cleanup(md);
174 qmp_input_visitor_cleanup(mi);
179 def gen_marshal_output(name
, args
, ret_type
, middle_mode
):
184 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
186 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
187 QmpOutputVisitor *mo = qmp_output_visitor_new();
190 v = qmp_output_get_visitor(mo);
191 %(visitor)s(v, &ret_in, "unused", errp);
192 if (!error_is_set(errp)) {
193 *ret_out = qmp_output_get_qobject(mo);
195 qmp_output_visitor_cleanup(mo);
196 v = qapi_dealloc_get_visitor(md);
197 %(visitor)s(v, &ret_in, "unused", errp);
198 qapi_dealloc_visitor_cleanup(md);
201 c_ret_type
=c_type(ret_type
), c_name
=c_fun(name
),
202 visitor
=type_visitor(ret_type
))
206 def gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
):
208 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name
)
210 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name
)
214 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
215 hdr
= gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
)
225 Error *local_err = NULL;
226 Error **errp = &local_err;
227 QDict *args = (QDict *)qdict;
231 if c_type(ret_type
).endswith("*"):
232 retval
= " %s retval = NULL;" % c_type(ret_type
)
234 retval
= " %s retval;" % c_type(ret_type
)
242 %(visitor_input_containers_decl)s
243 %(visitor_input_vars_decl)s
245 %(visitor_input_block)s
248 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
),
249 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
250 visitor_input_block
=gen_visitor_input_block(args
, "QOBJECT(args)"))
257 if (error_is_set(errp)) {
262 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
268 %(visitor_input_block_cleanup)s
270 visitor_input_block_cleanup
=gen_visitor_input_block(args
, None,
277 qerror_report_err(local_err);
278 error_free(local_err);
294 def option_value_matches(opt
, val
, cmd
):
295 if opt
in cmd
and cmd
[opt
] == val
:
299 def gen_registry(commands
):
303 options
= 'QCO_NO_OPTIONS'
304 if option_value_matches('success-response', 'no', cmd
):
305 options
= 'QCO_NO_SUCCESS_RESP'
307 registry
+= mcgen('''
308 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
310 name
=cmd
['command'], c_name
=c_fun(cmd
['command']),
314 static void qmp_init_marshal(void)
319 qapi_init(qmp_init_marshal);
321 registry
=registry
.rstrip())
324 def gen_command_decl_prologue(header
, guard
, prefix
=""):
326 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
329 * schema-defined QAPI function prototypes
331 * Copyright IBM, Corp. 2011
334 * Anthony Liguori <aliguori@us.ibm.com>
336 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
337 * See the COPYING.LIB file in the top-level directory.
344 #include "%(prefix)sqapi-types.h"
345 #include "qapi/qmp/qdict.h"
346 #include "qapi/error.h"
349 header
=basename(header
), guard
=guardname(header
), prefix
=prefix
)
352 def gen_command_def_prologue(prefix
="", proxy
=False):
354 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
357 * schema-defined QMP->QAPI command dispatch
359 * Copyright IBM, Corp. 2011
362 * Anthony Liguori <aliguori@us.ibm.com>
364 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
365 * See the COPYING.LIB file in the top-level directory.
369 #include "qemu-common.h"
370 #include "qemu/module.h"
371 #include "qapi/qmp/qerror.h"
372 #include "qapi/qmp/types.h"
373 #include "qapi/qmp/dispatch.h"
374 #include "qapi/visitor.h"
375 #include "qapi/qmp-output-visitor.h"
376 #include "qapi/qmp-input-visitor.h"
377 #include "qapi/dealloc-visitor.h"
378 #include "%(prefix)sqapi-types.h"
379 #include "%(prefix)sqapi-visit.h"
384 ret
+= '#include "%sqmp-commands.h"' % prefix
389 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:m",
390 ["source", "header", "prefix=",
391 "output-dir=", "type=", "middle"])
392 except getopt
.GetoptError
, err
:
398 dispatch_type
= "sync"
399 c_file
= 'qmp-marshal.c'
400 h_file
= 'qmp-commands.h'
407 if o
in ("-p", "--prefix"):
409 elif o
in ("-o", "--output-dir"):
411 elif o
in ("-t", "--type"):
413 elif o
in ("-m", "--middle"):
415 elif o
in ("-c", "--source"):
417 elif o
in ("-h", "--header"):
420 if not do_c
and not do_h
:
424 c_file
= output_dir
+ prefix
+ c_file
425 h_file
= output_dir
+ prefix
+ h_file
427 def maybe_open(really
, name
, opt
):
429 return open(name
, opt
)
432 return StringIO
.StringIO()
435 os
.makedirs(output_dir
)
437 if e
.errno
!= errno
.EEXIST
:
440 exprs
= parse_schema(sys
.stdin
)
441 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
442 commands
= filter(lambda expr
: not expr
.has_key('gen'), commands
)
444 if dispatch_type
== "sync":
445 fdecl
= maybe_open(do_h
, h_file
, 'w')
446 fdef
= maybe_open(do_c
, c_file
, 'w')
447 ret
= gen_command_decl_prologue(header
=basename(h_file
), guard
=guardname(h_file
), prefix
=prefix
)
449 ret
= gen_command_def_prologue(prefix
=prefix
)
455 if cmd
.has_key('data'):
456 arglist
= cmd
['data']
457 if cmd
.has_key('returns'):
458 ret_type
= cmd
['returns']
459 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
) + "\n"
462 ret
= gen_marshal_output(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
466 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], arglist
, ret_type
, middle_mode
))
468 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
471 fdecl
.write("\n#endif\n");
474 ret
= gen_registry(commands
)