2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014 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
, structured
in parse_args(args
):
32 argtype
= c_type(argtype
)
33 if argtype
== "char *":
34 argtype
= "const char *"
36 arglist
+= "bool has_%s, " % c_var(argname
)
37 arglist
+= "%s %s, " % (argtype
, c_var(argname
))
39 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
41 ret_type
=c_type(ret_type
), name
=c_fun(name
), args
=arglist
).strip()
43 def gen_err_check(errvar
):
52 def gen_sync_call(name
, args
, ret_type
, indent
=0):
58 for argname
, argtype
, optional
, structured
in parse_args(args
):
60 arglist
+= "has_%s, " % c_var(argname
)
61 arglist
+= "%s, " % (c_var(argname
))
64 %(retval)sqmp_%(name)s(%(args)s&local_err);
67 name
=c_fun(name
), args
=arglist
, retval
=retval
).rstrip()
69 ret
+= "\n" + gen_err_check('local_err')
70 ret
+= "\n" + mcgen(''''
71 %(marshal_output_call)s
73 marshal_output_call
=gen_marshal_output_call(name
, ret_type
)).rstrip()
78 def gen_marshal_output_call(name
, ret_type
):
81 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name
)
83 def gen_visitor_input_containers_decl(args
, obj
):
89 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
90 QapiDeallocVisitor *md;
98 def gen_visitor_input_vars_decl(args
):
101 for argname
, argtype
, optional
, structured
in parse_args(args
):
104 bool has_%(argname)s = false;
106 argname
=c_var(argname
))
107 if c_type(argtype
).endswith("*"):
109 %(argtype)s %(argname)s = NULL;
111 argname
=c_var(argname
), argtype
=c_type(argtype
))
114 %(argtype)s %(argname)s = {0};
116 argname
=c_var(argname
), argtype
=c_type(argtype
))
121 def gen_visitor_input_block(args
, dealloc
=False):
123 errparg
= '&local_err'
135 qmp_input_visitor_cleanup(mi);
136 md = qapi_dealloc_visitor_new();
137 v = qapi_dealloc_get_visitor(md);
141 v = qmp_input_get_visitor(mi);
144 for argname
, argtype
, optional
, structured
in parse_args(args
):
147 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
149 c_name
=c_var(argname
), name
=argname
, errp
=errparg
)
150 ret
+= gen_err_check(errarg
)
152 if (has_%(c_name)s) {
154 c_name
=c_var(argname
))
157 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
159 c_name
=c_var(argname
), name
=argname
, argtype
=argtype
,
160 visitor
=type_visitor(argtype
), errp
=errparg
)
161 ret
+= gen_err_check(errarg
)
170 qapi_dealloc_visitor_cleanup(md);
175 def gen_marshal_output(name
, args
, ret_type
, middle_mode
):
180 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
182 Error *local_err = NULL;
183 QmpOutputVisitor *mo = qmp_output_visitor_new();
184 QapiDeallocVisitor *md;
187 v = qmp_output_get_visitor(mo);
188 %(visitor)s(v, &ret_in, "unused", &local_err);
192 *ret_out = qmp_output_get_qobject(mo);
195 error_propagate(errp, local_err);
196 qmp_output_visitor_cleanup(mo);
197 md = qapi_dealloc_visitor_new();
198 v = qapi_dealloc_get_visitor(md);
199 %(visitor)s(v, &ret_in, "unused", NULL);
200 qapi_dealloc_visitor_cleanup(md);
203 c_ret_type
=c_type(ret_type
), c_name
=c_fun(name
),
204 visitor
=type_visitor(ret_type
))
208 def gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
):
210 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name
)
212 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name
)
216 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
217 hdr
= gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
)
222 Error *local_err = NULL;
228 QDict *args = (QDict *)qdict;
232 if c_type(ret_type
).endswith("*"):
233 retval
= " %s retval = NULL;" % c_type(ret_type
)
235 retval
= " %s retval;" % c_type(ret_type
)
243 %(visitor_input_containers_decl)s
244 %(visitor_input_vars_decl)s
246 %(visitor_input_block)s
249 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
, "QOBJECT(args)"),
250 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
251 visitor_input_block
=gen_visitor_input_block(args
))
261 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
262 if re
.search('^ *goto out\\;', ret
, re
.MULTILINE
):
269 error_propagate(errp, local_err);
272 %(visitor_input_block_cleanup)s
274 visitor_input_block_cleanup
=gen_visitor_input_block(args
,
281 qerror_report_err(local_err);
282 error_free(local_err);
298 def option_value_matches(opt
, val
, cmd
):
299 if opt
in cmd
and cmd
[opt
] == val
:
303 def gen_registry(commands
):
307 options
= 'QCO_NO_OPTIONS'
308 if option_value_matches('success-response', 'no', cmd
):
309 options
= 'QCO_NO_SUCCESS_RESP'
311 registry
+= mcgen('''
312 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
314 name
=cmd
['command'], c_name
=c_fun(cmd
['command']),
318 static void qmp_init_marshal(void)
323 qapi_init(qmp_init_marshal);
325 registry
=registry
.rstrip())
328 def gen_command_decl_prologue(header
, guard
, prefix
=""):
330 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
333 * schema-defined QAPI function prototypes
335 * Copyright IBM, Corp. 2011
338 * Anthony Liguori <aliguori@us.ibm.com>
340 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
341 * See the COPYING.LIB file in the top-level directory.
348 #include "%(prefix)sqapi-types.h"
349 #include "qapi/qmp/qdict.h"
350 #include "qapi/error.h"
353 header
=basename(header
), guard
=guardname(header
), prefix
=prefix
)
356 def gen_command_def_prologue(prefix
="", proxy
=False):
358 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
361 * schema-defined QMP->QAPI command dispatch
363 * Copyright IBM, Corp. 2011
366 * Anthony Liguori <aliguori@us.ibm.com>
368 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
369 * See the COPYING.LIB file in the top-level directory.
373 #include "qemu-common.h"
374 #include "qemu/module.h"
375 #include "qapi/qmp/qerror.h"
376 #include "qapi/qmp/types.h"
377 #include "qapi/qmp/dispatch.h"
378 #include "qapi/visitor.h"
379 #include "qapi/qmp-output-visitor.h"
380 #include "qapi/qmp-input-visitor.h"
381 #include "qapi/dealloc-visitor.h"
382 #include "%(prefix)sqapi-types.h"
383 #include "%(prefix)sqapi-visit.h"
388 ret
+= '#include "%sqmp-commands.h"' % prefix
393 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:i:o:m",
394 ["source", "header", "prefix=",
395 "input-file=", "output-dir=",
397 except getopt
.GetoptError
, err
:
403 dispatch_type
= "sync"
404 c_file
= 'qmp-marshal.c'
405 h_file
= 'qmp-commands.h'
412 if o
in ("-p", "--prefix"):
414 elif o
in ("-i", "--input-file"):
416 elif o
in ("-o", "--output-dir"):
418 elif o
in ("-t", "--type"):
420 elif o
in ("-m", "--middle"):
422 elif o
in ("-c", "--source"):
424 elif o
in ("-h", "--header"):
427 if not do_c
and not do_h
:
431 c_file
= output_dir
+ prefix
+ c_file
432 h_file
= output_dir
+ prefix
+ h_file
434 def maybe_open(really
, name
, opt
):
436 return open(name
, opt
)
439 return StringIO
.StringIO()
442 os
.makedirs(output_dir
)
444 if e
.errno
!= errno
.EEXIST
:
447 exprs
= parse_schema(input_file
)
448 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
449 commands
= filter(lambda expr
: not expr
.has_key('gen'), commands
)
451 if dispatch_type
== "sync":
452 fdecl
= maybe_open(do_h
, h_file
, 'w')
453 fdef
= maybe_open(do_c
, c_file
, 'w')
454 ret
= gen_command_decl_prologue(header
=basename(h_file
), guard
=guardname(h_file
), prefix
=prefix
)
456 ret
= gen_command_def_prologue(prefix
=prefix
)
462 if cmd
.has_key('data'):
463 arglist
= cmd
['data']
464 if cmd
.has_key('returns'):
465 ret_type
= cmd
['returns']
466 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
) + "\n"
469 ret
= gen_marshal_output(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
473 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], arglist
, ret_type
, middle_mode
))
475 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
478 fdecl
.write("\n#endif\n");
481 ret
= gen_registry(commands
)