2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
10 # This work is licensed under the terms of the GNU GPL, version 2.
11 # See the COPYING 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_command_decl(name, args, ret_type):
28 for argname, argtype, optional, structured in parse_args(args):
29 argtype = c_type(argtype)
30 if argtype == "char *":
31 argtype = "const char *"
33 arglist += "bool has_%s, " % c_var(argname)
34 arglist += "%s %s, " % (argtype, c_var(argname))
36 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
38 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
40 def gen_sync_call(name, args, ret_type, indent=0):
46 for argname, argtype, optional, structured in parse_args(args):
48 arglist += "has_%s, " % c_var(argname)
49 arglist += "%s, " % (c_var(argname))
52 %(retval)sqmp_%(name)s(%(args)serrp);
55 name=c_fun(name), args=arglist, retval=retval).rstrip()
57 ret += "\n" + mcgen(''''
58 if (!error_is_set(errp)) {
59 %(marshal_output_call)s
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
67 def gen_marshal_output_call(name, ret_type):
70 return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name)
72 def gen_visitor_input_containers_decl(args):
79 QapiDeallocVisitor *md;
86 def gen_visitor_input_vars_decl(args):
89 for argname, argtype, optional, structured in parse_args(args):
92 bool has_%(argname)s = false;
94 argname=c_var(argname))
95 if c_type(argtype).endswith("*"):
97 %(argtype)s %(argname)s = NULL;
99 argname=c_var(argname), argtype=c_type(argtype))
102 %(argtype)s %(argname)s;
104 argname=c_var(argname), argtype=c_type(argtype))
109 def gen_visitor_input_block(args, obj, dealloc=False):
121 md = qapi_dealloc_visitor_new();
122 v = qapi_dealloc_get_visitor(md);
126 mi = qmp_input_visitor_new_strict(%(obj)s);
127 v = qmp_input_get_visitor(mi);
131 for argname, argtype, optional, structured in parse_args(args):
134 visit_start_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
135 if (has_%(c_name)s) {
137 c_name=c_var(argname), name=argname, errp=errparg)
140 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
142 c_name=c_var(argname), name=argname, argtype=argtype,
143 visitor=type_visitor(argtype), errp=errparg)
148 visit_end_optional(v, %(errp)s);
153 qapi_dealloc_visitor_cleanup(md);
157 qmp_input_visitor_cleanup(mi);
162 def gen_marshal_output(name, args, ret_type, middle_mode):
167 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
169 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
170 QmpOutputVisitor *mo = qmp_output_visitor_new();
173 v = qmp_output_get_visitor(mo);
174 %(visitor)s(v, &ret_in, "unused", errp);
175 if (!error_is_set(errp)) {
176 *ret_out = qmp_output_get_qobject(mo);
178 qmp_output_visitor_cleanup(mo);
179 v = qapi_dealloc_get_visitor(md);
180 %(visitor)s(v, &ret_in, "unused", NULL);
181 qapi_dealloc_visitor_cleanup(md);
184 c_ret_type=c_type(ret_type), c_name=c_fun(name),
185 visitor=type_visitor(ret_type))
189 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
191 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
193 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
197 def gen_marshal_input(name, args, ret_type, middle_mode):
198 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
208 Error *local_err = NULL;
209 Error **errp = &local_err;
210 QDict *args = (QDict *)qdict;
214 if c_type(ret_type).endswith("*"):
215 retval = " %s retval = NULL;" % c_type(ret_type)
217 retval = " %s retval;" % c_type(ret_type)
225 %(visitor_input_containers_decl)s
226 %(visitor_input_vars_decl)s
228 %(visitor_input_block)s
231 visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
232 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
233 visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
240 if (error_is_set(errp)) {
245 sync_call=gen_sync_call(name, args, ret_type, indent=4))
251 %(visitor_input_block_cleanup)s
253 visitor_input_block_cleanup=gen_visitor_input_block(args, None,
260 qerror_report_err(local_err);
261 error_free(local_err);
277 def option_value_matches(opt, val, cmd):
278 if opt in cmd and cmd[opt] == val:
282 def gen_registry(commands):
286 options = 'QCO_NO_OPTIONS'
287 if option_value_matches('success-response', 'no', cmd):
288 options = 'QCO_NO_SUCCESS_RESP'
290 registry += mcgen('''
291 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
293 name=cmd['command'], c_name=c_fun(cmd['command']),
297 static void qmp_init_marshal(void)
302 qapi_init(qmp_init_marshal);
304 registry=registry.rstrip())
307 def gen_command_decl_prologue(header, guard, prefix=""):
309 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
312 * schema-defined QAPI function prototypes
314 * Copyright IBM, Corp. 2011
319 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
320 * See the COPYING.LIB file in the top-level directory.
327 #include "%(prefix)sqapi-types.h"
328 #include "qapi/qmp/qdict.h"
329 #include "qapi/error.h"
332 header=basename(header), guard=guardname(header), prefix=prefix)
335 def gen_command_def_prologue(prefix="", proxy=False):
337 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
340 * schema-defined QMP->QAPI command dispatch
342 * Copyright IBM, Corp. 2011
347 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
348 * See the COPYING.LIB file in the top-level directory.
352 #include "qemu-common.h"
353 #include "qemu/module.h"
354 #include "qapi/qmp/qerror.h"
355 #include "qapi/qmp/types.h"
356 #include "qapi/qmp/dispatch.h"
357 #include "qapi/visitor.h"
358 #include "qapi/qmp-output-visitor.h"
359 #include "qapi/qmp-input-visitor.h"
360 #include "qapi/dealloc-visitor.h"
361 #include "%(prefix)sqapi-types.h"
362 #include "%(prefix)sqapi-visit.h"
367 ret += '#include "%sqmp-commands.h"' % prefix
372 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
373 ["source", "header", "prefix=",
374 "input-file=", "output-dir=",
376 except getopt.GetoptError, err:
382 dispatch_type = "sync"
383 c_file = 'qmp-marshal.c'
384 h_file = 'qmp-commands.h'
391 if o in ("-p", "--prefix"):
393 elif o in ("-i", "--input-file"):
395 elif o in ("-o", "--output-dir"):
397 elif o in ("-t", "--type"):
399 elif o in ("-m", "--middle"):
401 elif o in ("-c", "--source"):
403 elif o in ("-h", "--header"):
406 if not do_c and not do_h:
410 c_file = output_dir + prefix + c_file
411 h_file = output_dir + prefix + h_file
413 def maybe_open(really, name, opt):
415 return open(name, opt)
418 return StringIO.StringIO()
421 os.makedirs(output_dir)
423 if e.errno != errno.EEXIST:
426 exprs = parse_schema(input_file)
427 commands = filter(lambda expr: expr.has_key('command'), exprs)
428 commands = filter(lambda expr: not expr.has_key('gen'), commands)
430 if dispatch_type == "sync":
431 fdecl = maybe_open(do_h, h_file, 'w')
432 fdef = maybe_open(do_c, c_file, 'w')
433 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
435 ret = gen_command_def_prologue(prefix=prefix)
441 if cmd.has_key('data'):
442 arglist = cmd['data']
443 if cmd.has_key('returns'):
444 ret_type = cmd['returns']
445 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
448 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
452 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
454 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
457 fdecl.write("\n#endif\n");
460 ret = gen_registry(commands)