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):
141 md = qapi_dealloc_visitor_new();
142 v = qapi_dealloc_get_visitor(md);
146 mi = qmp_input_visitor_new_strict(%(obj)s);
147 v = qmp_input_get_visitor(mi);
151 for argname
, argtype
, optional
, structured
in parse_args(args
):
154 visit_start_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
155 if (has_%(c_name)s) {
157 c_name
=c_var(argname
), name
=argname
, errp
=errparg
)
160 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
162 c_name
=c_var(argname
), name
=argname
, argtype
=argtype
,
163 visitor
=type_visitor(argtype
), errp
=errparg
)
168 visit_end_optional(v, %(errp)s);
173 qapi_dealloc_visitor_cleanup(md);
177 qmp_input_visitor_cleanup(mi);
182 def gen_marshal_output(name
, args
, ret_type
, middle_mode
):
187 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
189 QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
190 QmpOutputVisitor *mo = qmp_output_visitor_new();
193 v = qmp_output_get_visitor(mo);
194 %(visitor)s(v, &ret_in, "unused", errp);
195 if (!error_is_set(errp)) {
196 *ret_out = qmp_output_get_qobject(mo);
198 qmp_output_visitor_cleanup(mo);
199 v = qapi_dealloc_get_visitor(md);
200 %(visitor)s(v, &ret_in, "unused", NULL);
201 qapi_dealloc_visitor_cleanup(md);
204 c_ret_type
=c_type(ret_type
), c_name
=c_fun(name
),
205 visitor
=type_visitor(ret_type
))
209 def gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
):
211 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name
)
213 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name
)
217 def gen_marshal_input(name
, args
, ret_type
, middle_mode
):
218 hdr
= gen_marshal_input_decl(name
, args
, ret_type
, middle_mode
)
228 Error *local_err = NULL;
229 Error **errp = &local_err;
230 QDict *args = (QDict *)qdict;
234 if c_type(ret_type
).endswith("*"):
235 retval
= " %s retval = NULL;" % c_type(ret_type
)
237 retval
= " %s retval;" % c_type(ret_type
)
245 %(visitor_input_containers_decl)s
246 %(visitor_input_vars_decl)s
248 %(visitor_input_block)s
251 visitor_input_containers_decl
=gen_visitor_input_containers_decl(args
),
252 visitor_input_vars_decl
=gen_visitor_input_vars_decl(args
),
253 visitor_input_block
=gen_visitor_input_block(args
, "QOBJECT(args)"))
260 if (error_is_set(errp)) {
265 sync_call
=gen_sync_call(name
, args
, ret_type
, indent
=4))
271 %(visitor_input_block_cleanup)s
273 visitor_input_block_cleanup
=gen_visitor_input_block(args
, None,
280 qerror_report_err(local_err);
281 error_free(local_err);
297 def option_value_matches(opt
, val
, cmd
):
298 if opt
in cmd
and cmd
[opt
] == val
:
302 def gen_registry(commands
):
306 options
= 'QCO_NO_OPTIONS'
307 if option_value_matches('success-response', 'no', cmd
):
308 options
= 'QCO_NO_SUCCESS_RESP'
310 registry
+= mcgen('''
311 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
313 name
=cmd
['command'], c_name
=c_fun(cmd
['command']),
317 static void qmp_init_marshal(void)
322 qapi_init(qmp_init_marshal);
324 registry
=registry
.rstrip())
327 def gen_command_decl_prologue(header
, guard
, prefix
=""):
329 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
332 * schema-defined QAPI function prototypes
334 * Copyright IBM, Corp. 2011
337 * Anthony Liguori <aliguori@us.ibm.com>
339 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
340 * See the COPYING.LIB file in the top-level directory.
347 #include "%(prefix)sqapi-types.h"
348 #include "qapi/qmp/qdict.h"
349 #include "qapi/error.h"
352 header
=basename(header
), guard
=guardname(header
), prefix
=prefix
)
355 def gen_command_def_prologue(prefix
="", proxy
=False):
357 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
360 * schema-defined QMP->QAPI command dispatch
362 * Copyright IBM, Corp. 2011
365 * Anthony Liguori <aliguori@us.ibm.com>
367 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
368 * See the COPYING.LIB file in the top-level directory.
372 #include "qemu-common.h"
373 #include "qemu/module.h"
374 #include "qapi/qmp/qerror.h"
375 #include "qapi/qmp/types.h"
376 #include "qapi/qmp/dispatch.h"
377 #include "qapi/visitor.h"
378 #include "qapi/qmp-output-visitor.h"
379 #include "qapi/qmp-input-visitor.h"
380 #include "qapi/dealloc-visitor.h"
381 #include "%(prefix)sqapi-types.h"
382 #include "%(prefix)sqapi-visit.h"
387 ret
+= '#include "%sqmp-commands.h"' % prefix
392 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:], "chp:o:m",
393 ["source", "header", "prefix=",
394 "output-dir=", "type=", "middle"])
395 except getopt
.GetoptError
, err
:
401 dispatch_type
= "sync"
402 c_file
= 'qmp-marshal.c'
403 h_file
= 'qmp-commands.h'
410 if o
in ("-p", "--prefix"):
412 elif o
in ("-o", "--output-dir"):
414 elif o
in ("-t", "--type"):
416 elif o
in ("-m", "--middle"):
418 elif o
in ("-c", "--source"):
420 elif o
in ("-h", "--header"):
423 if not do_c
and not do_h
:
427 c_file
= output_dir
+ prefix
+ c_file
428 h_file
= output_dir
+ prefix
+ h_file
430 def maybe_open(really
, name
, opt
):
432 return open(name
, opt
)
435 return StringIO
.StringIO()
438 os
.makedirs(output_dir
)
440 if e
.errno
!= errno
.EEXIST
:
443 exprs
= parse_schema(sys
.stdin
)
444 commands
= filter(lambda expr
: expr
.has_key('command'), exprs
)
445 commands
= filter(lambda expr
: not expr
.has_key('gen'), commands
)
447 if dispatch_type
== "sync":
448 fdecl
= maybe_open(do_h
, h_file
, 'w')
449 fdef
= maybe_open(do_c
, c_file
, 'w')
450 ret
= gen_command_decl_prologue(header
=basename(h_file
), guard
=guardname(h_file
), prefix
=prefix
)
452 ret
= gen_command_def_prologue(prefix
=prefix
)
458 if cmd
.has_key('data'):
459 arglist
= cmd
['data']
460 if cmd
.has_key('returns'):
461 ret_type
= cmd
['returns']
462 ret
= generate_command_decl(cmd
['command'], arglist
, ret_type
) + "\n"
465 ret
= gen_marshal_output(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
469 fdecl
.write('%s;\n' % gen_marshal_input_decl(cmd
['command'], arglist
, ret_type
, middle_mode
))
471 ret
= gen_marshal_input(cmd
['command'], arglist
, ret_type
, middle_mode
) + "\n"
474 fdecl
.write("\n#endif\n");
477 ret
= gen_registry(commands
)