Add MAINTAINERS entry for virtio-input
[qemu/ar7.git] / scripts / qapi-commands.py
blob1c1d3aa029a6dc59addb1ac1843ac38d7b642967
2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014-2015 Red Hat, Inc.
7 # Authors:
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
16 from qapi import *
17 import re
19 def generate_command_decl(name, args, ret_type):
20 arglist=""
21 for argname, argtype, optional in parse_args(args):
22 argtype = c_type(argtype, is_param=True)
23 if optional:
24 arglist += "bool has_%s, " % c_name(argname)
25 arglist += "%s %s, " % (argtype, c_name(argname))
26 return mcgen('''
27 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
28 ''',
29 ret_type=c_type(ret_type), name=c_name(name),
30 args=arglist).strip()
32 def gen_err_check(errvar):
33 if errvar:
34 return mcgen('''
35 if (local_err) {
36 goto out;
38 ''')
39 return ''
41 def gen_sync_call(name, args, ret_type, indent=0):
42 ret = ""
43 arglist=""
44 retval=""
45 if ret_type:
46 retval = "retval = "
47 for argname, argtype, optional in parse_args(args):
48 if optional:
49 arglist += "has_%s, " % c_name(argname)
50 arglist += "%s, " % (c_name(argname))
51 push_indent(indent)
52 ret = mcgen('''
53 %(retval)sqmp_%(name)s(%(args)s&local_err);
55 ''',
56 name=c_name(name), args=arglist, retval=retval).rstrip()
57 if ret_type:
58 ret += "\n" + gen_err_check('local_err')
59 ret += "\n" + mcgen(''''
60 %(marshal_output_call)s
61 ''',
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
63 pop_indent(indent)
64 return ret.rstrip()
67 def gen_marshal_output_call(name, ret_type):
68 if not ret_type:
69 return ""
70 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
72 def gen_visitor_input_containers_decl(args, obj):
73 ret = ""
75 push_indent()
76 if len(args) > 0:
77 ret += mcgen('''
78 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
79 QapiDeallocVisitor *md;
80 Visitor *v;
81 ''',
82 obj=obj)
83 pop_indent()
85 return ret.rstrip()
87 def gen_visitor_input_vars_decl(args):
88 ret = ""
89 push_indent()
90 for argname, argtype, optional in parse_args(args):
91 if optional:
92 ret += mcgen('''
93 bool has_%(argname)s = false;
94 ''',
95 argname=c_name(argname))
96 if is_c_ptr(argtype):
97 ret += mcgen('''
98 %(argtype)s %(argname)s = NULL;
99 ''',
100 argname=c_name(argname), argtype=c_type(argtype))
101 else:
102 ret += mcgen('''
103 %(argtype)s %(argname)s = {0};
104 ''',
105 argname=c_name(argname), argtype=c_type(argtype))
107 pop_indent()
108 return ret.rstrip()
110 def gen_visitor_input_block(args, dealloc=False):
111 ret = ""
112 errparg = '&local_err'
113 errarg = 'local_err'
115 if len(args) == 0:
116 return ret
118 push_indent()
120 if dealloc:
121 errparg = 'NULL'
122 errarg = None;
123 ret += mcgen('''
124 qmp_input_visitor_cleanup(mi);
125 md = qapi_dealloc_visitor_new();
126 v = qapi_dealloc_get_visitor(md);
127 ''')
128 else:
129 ret += mcgen('''
130 v = qmp_input_get_visitor(mi);
131 ''')
133 for argname, argtype, optional in parse_args(args):
134 if optional:
135 ret += mcgen('''
136 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
137 ''',
138 c_name=c_name(argname), name=argname, errp=errparg)
139 ret += gen_err_check(errarg)
140 ret += mcgen('''
141 if (has_%(c_name)s) {
142 ''',
143 c_name=c_name(argname))
144 push_indent()
145 ret += mcgen('''
146 visit_type_%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
147 ''',
148 c_name=c_name(argname), name=argname, argtype=argtype,
149 visitor=type_name(argtype), errp=errparg)
150 ret += gen_err_check(errarg)
151 if optional:
152 pop_indent()
153 ret += mcgen('''
155 ''')
157 if dealloc:
158 ret += mcgen('''
159 qapi_dealloc_visitor_cleanup(md);
160 ''')
161 pop_indent()
162 return ret.rstrip()
164 def gen_marshal_output(name, args, ret_type, middle_mode):
165 if not ret_type:
166 return ""
168 ret = mcgen('''
169 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
171 Error *local_err = NULL;
172 QmpOutputVisitor *mo = qmp_output_visitor_new();
173 QapiDeallocVisitor *md;
174 Visitor *v;
176 v = qmp_output_get_visitor(mo);
177 visit_type_%(visitor)s(v, &ret_in, "unused", &local_err);
178 if (local_err) {
179 goto out;
181 *ret_out = qmp_output_get_qobject(mo);
183 out:
184 error_propagate(errp, local_err);
185 qmp_output_visitor_cleanup(mo);
186 md = qapi_dealloc_visitor_new();
187 v = qapi_dealloc_get_visitor(md);
188 visit_type_%(visitor)s(v, &ret_in, "unused", NULL);
189 qapi_dealloc_visitor_cleanup(md);
191 ''',
192 c_ret_type=c_type(ret_type), c_name=c_name(name),
193 visitor=type_name(ret_type))
195 return ret
197 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
198 if middle_mode:
199 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name)
200 else:
201 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
205 def gen_marshal_input(name, args, ret_type, middle_mode):
206 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
208 ret = mcgen('''
209 %(header)s
211 Error *local_err = NULL;
212 ''',
213 header=hdr)
215 if middle_mode:
216 ret += mcgen('''
217 QDict *args = (QDict *)qdict;
218 ''')
220 if ret_type:
221 if is_c_ptr(ret_type):
222 retval = " %s retval = NULL;" % c_type(ret_type)
223 else:
224 retval = " %s retval;" % c_type(ret_type)
225 ret += mcgen('''
226 %(retval)s
227 ''',
228 retval=retval)
230 if len(args) > 0:
231 ret += mcgen('''
232 %(visitor_input_containers_decl)s
233 %(visitor_input_vars_decl)s
235 %(visitor_input_block)s
237 ''',
238 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
239 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
240 visitor_input_block=gen_visitor_input_block(args))
241 else:
242 ret += mcgen('''
244 (void)args;
245 ''')
247 ret += mcgen('''
248 %(sync_call)s
249 ''',
250 sync_call=gen_sync_call(name, args, ret_type, indent=4))
251 if re.search('^ *goto out\\;', ret, re.MULTILINE):
252 ret += mcgen('''
254 out:
255 ''')
256 if not middle_mode:
257 ret += mcgen('''
258 error_propagate(errp, local_err);
259 ''')
260 ret += mcgen('''
261 %(visitor_input_block_cleanup)s
262 ''',
263 visitor_input_block_cleanup=gen_visitor_input_block(args,
264 dealloc=True))
266 if middle_mode:
267 ret += mcgen('''
269 if (local_err) {
270 qerror_report_err(local_err);
271 error_free(local_err);
272 return -1;
274 return 0;
275 ''')
276 else:
277 ret += mcgen('''
278 return;
279 ''')
281 ret += mcgen('''
283 ''')
285 return ret
287 def gen_registry(commands):
288 registry=""
289 push_indent()
290 for cmd in commands:
291 options = 'QCO_NO_OPTIONS'
292 if not cmd.get('success-response', True):
293 options = 'QCO_NO_SUCCESS_RESP'
295 registry += mcgen('''
296 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
297 ''',
298 name=cmd['command'], c_name=c_name(cmd['command']),
299 opts=options)
300 pop_indent()
301 ret = mcgen('''
302 static void qmp_init_marshal(void)
304 %(registry)s
307 qapi_init(qmp_init_marshal);
308 ''',
309 registry=registry.rstrip())
310 return ret
312 middle_mode = False
314 (input_file, output_dir, do_c, do_h, prefix, opts) = \
315 parse_command_line("m", ["middle"])
317 for o, a in opts:
318 if o in ("-m", "--middle"):
319 middle_mode = True
321 exprs = parse_schema(input_file)
322 commands = filter(lambda expr: expr.has_key('command'), exprs)
323 commands = filter(lambda expr: not expr.has_key('gen'), commands)
325 c_comment = '''
327 * schema-defined QMP->QAPI command dispatch
329 * Copyright IBM, Corp. 2011
331 * Authors:
332 * Anthony Liguori <aliguori@us.ibm.com>
334 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
335 * See the COPYING.LIB file in the top-level directory.
339 h_comment = '''
341 * schema-defined QAPI function prototypes
343 * Copyright IBM, Corp. 2011
345 * Authors:
346 * Anthony Liguori <aliguori@us.ibm.com>
348 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
349 * See the COPYING.LIB file in the top-level directory.
354 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
355 'qmp-marshal.c', 'qmp-commands.h',
356 c_comment, h_comment)
358 fdef.write(mcgen('''
359 #include "qemu-common.h"
360 #include "qemu/module.h"
361 #include "qapi/qmp/qerror.h"
362 #include "qapi/qmp/types.h"
363 #include "qapi/qmp/dispatch.h"
364 #include "qapi/visitor.h"
365 #include "qapi/qmp-output-visitor.h"
366 #include "qapi/qmp-input-visitor.h"
367 #include "qapi/dealloc-visitor.h"
368 #include "%(prefix)sqapi-types.h"
369 #include "%(prefix)sqapi-visit.h"
370 #include "%(prefix)sqmp-commands.h"
372 ''',
373 prefix=prefix))
375 fdecl.write(mcgen('''
376 #include "%(prefix)sqapi-types.h"
377 #include "qapi/qmp/qdict.h"
378 #include "qapi/error.h"
380 ''',
381 prefix=prefix))
383 for cmd in commands:
384 arglist = []
385 ret_type = None
386 if cmd.has_key('data'):
387 arglist = cmd['data']
388 if cmd.has_key('returns'):
389 ret_type = cmd['returns']
390 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
391 fdecl.write(ret)
392 if ret_type:
393 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
394 fdef.write(ret)
396 if middle_mode:
397 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
399 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
400 fdef.write(ret)
402 if not middle_mode:
403 ret = gen_registry(commands)
404 fdef.write(ret)
406 close_output(fdef, fdecl)