2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 Red Hat, Inc.
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
15 from ordereddict
import OrderedDict
23 def type_visitor(name
):
24 if type(name
) == list:
25 return 'visit_type_%sList' % name
[0]
27 return 'visit_type_%s' % name
29 def generate_command_decl(name
, args
, ret_type
):
31 for argname
, argtype
, optional
in parse_args(args
):
32 argtype
= c_type(argtype
, is_param
=True)
34 arglist
+= "bool has_%s, " % c_var(argname
)
35 arglist
+= "%s %s, " % (argtype
, c_var(argname
))
37 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
39 ret_type
=c_type(ret_type
), name
=c_fun(name
), args
=arglist
).strip()
41 def gen_err_check(errvar
):
50 def gen_sync_call(name
, args
, ret_type
, indent
=0):
56 for argname
, argtype
, optional
in parse_args(args
):
58 arglist
+= "has_%s, " % c_var(argname
)
59 arglist
+= "%s, " % (c_var(argname
))
62 %(retval)sqmp_%(name)s(%(args)s&local_err);
65 name
=c_fun(name
), args
=arglist
, retval
=retval
).rstrip()
67 ret
+= "\n" + gen_err_check('local_err')
68 ret
+= "\n" + mcgen(''''
69 %(marshal_output_call)s
71 marshal_output_call
=gen_marshal_output_call(name
, ret_type
)).rstrip()
76 def gen_marshal_output_call(name
, ret_type
):
79 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name
)
81 def gen_visitor_input_containers_decl(args
, obj
):
87 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
88 QapiDeallocVisitor *md;
96 def gen_visitor_input_vars_decl(args
):
99 for argname
, argtype
, optional
in parse_args(args
):
102 bool has_%(argname)s = false;
104 argname
=c_var(argname
))
105 if is_c_ptr(argtype
):
107 %(argtype)s %(argname)s = NULL;
109 argname
=c_var(argname
), argtype
=c_type(argtype
))
112 %(argtype)s %(argname)s = {0};
114 argname
=c_var(argname
), argtype
=c_type(argtype
))
119 def gen_visitor_input_block(args
, dealloc
=False):
121 errparg
= '&local_err'
133 qmp_input_visitor_cleanup(mi);
134 md = qapi_dealloc_visitor_new();
135 v = qapi_dealloc_get_visitor(md);
139 v = qmp_input_get_visitor(mi);
142 for argname
, argtype
, optional
in parse_args(args
):
145 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
147 c_name
=c_var(argname
), name
=argname
, errp
=errparg
)
148 ret
+= gen_err_check(errarg
)
150 if (has_%(c_name)s) {
152 c_name
=c_var(argname
))
155 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
157 c_name
=c_var(argname
), name
=argname
, argtype
=argtype
,
158 visitor
=type_visitor(argtype
), errp
=errparg
)
159 ret
+= gen_err_check(errarg
)
168 qapi_dealloc_visitor_cleanup(md);
173 def gen_marshal_output(name
, args
, ret_type
, middle_mode
):
178 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
180 Error *local_err = NULL;
181 QmpOutputVisitor *mo = qmp_output_visitor_new();
182 QapiDeallocVisitor *md;
185 v = qmp_output_get_visitor(mo);
186 %(visitor)s(v, &ret_in, "unused", &local_err);
190 *ret_out = qmp_output_get_qobject(mo);
193 error_propagate(errp, local_err);
194 qmp_output_visitor_cleanup(mo);
195 md = qapi_dealloc_visitor_new();
196 v = qapi_dealloc_get_visitor(md);
197 %(visitor)s(v, &ret_in, "unused", NULL);
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
)
220 Error *local_err = NULL;
226 QDict *args = (QDict *)qdict;
230 if is_c_ptr(ret_type
):
231 retval
= " %s retval = NULL;" % c_type(ret_type
)
233 retval
= " %s retval;" % c_type(ret_type
)
241 %(visitor_input_containers_decl)s
242 %(visitor_input_vars_decl)s
244 %(visitor_input_block)s
247 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
, "QOBJECT(args)"),
248 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
249 visitor_input_block
=gen_visitor_input_block(args
))
259 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
260 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
267 error_propagate(errp, local_err);
270 %(visitor_input_block_cleanup)s
272 visitor_input_block_cleanup
=gen_visitor_input_block(args
,
279 qerror_report_err(local_err);
280 error_free(local_err);
296 def gen_registry(commands
):
300 options
= 'QCO_NO_OPTIONS'
301 if not cmd
.get('success-response', True):
302 options
= 'QCO_NO_SUCCESS_RESP'
304 registry
+= mcgen('''
305 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
307 name
=cmd
['command'], c_name
=c_fun(cmd
['command']),
311 static void qmp_init_marshal(void)
316 qapi_init(qmp_init_marshal);
318 registry
=registry
.rstrip())
321 def gen_command_decl_prologue(header
, guard
, prefix
=""):
323 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
326 * schema-defined QAPI function prototypes
328 * Copyright IBM, Corp. 2011
331 * Anthony Liguori <aliguori@us.ibm.com>
333 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
334 * See the COPYING.LIB file in the top-level directory.
341 #include "%(prefix)sqapi-types.h"
342 #include "qapi/qmp/qdict.h"
343 #include "qapi/error.h"
346 header
=basename(header
), guard
=guardname(header
), prefix
=prefix
)
349 def gen_command_def_prologue(prefix
="", proxy
=False):
351 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
354 * schema-defined QMP->QAPI command dispatch
356 * Copyright IBM, Corp. 2011
359 * Anthony Liguori <aliguori@us.ibm.com>
361 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
362 * See the COPYING.LIB file in the top-level directory.
366 #include "qemu-common.h"
367 #include "qemu/module.h"
368 #include "qapi/qmp/qerror.h"
369 #include "qapi/qmp/types.h"
370 #include "qapi/qmp/dispatch.h"
371 #include "qapi/visitor.h"
372 #include "qapi/qmp-output-visitor.h"
373 #include "qapi/qmp-input-visitor.h"
374 #include "qapi/dealloc-visitor.h"
375 #include "%(prefix)sqapi-types.h"
376 #include "%(prefix)sqapi-visit.h"
381 ret
+= '#include "%sqmp-commands.h"' % prefix
386 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:i:o:m",
387 ["source", "header", "prefix=",
388 "input-file=", "output-dir=",
390 except getopt
.GetoptError
, err
:
396 dispatch_type
= "sync"
397 c_file
= 'qmp-marshal.c'
398 h_file
= 'qmp-commands.h'
405 if o
in ("-p", "--prefix"):
407 elif o
in ("-i", "--input-file"):
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(input_file
)
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
)