]> git.proxmox.com Git - mirror_qemu.git/blob - scripts/qapi-commands.py
qapi: Normalize marshalling's visitor initialization and cleanup
[mirror_qemu.git] / scripts / qapi-commands.py
1 #
2 # QAPI command marshaller generator
3 #
4 # Copyright IBM, Corp. 2011
5 #
6 # Authors:
7 # Anthony Liguori <aliguori@us.ibm.com>
8 # Michael Roth <mdroth@linux.vnet.ibm.com>
9 #
10 # This work is licensed under the terms of the GNU GPL, version 2.
11 # See the COPYING file in the top-level directory.
12
13 from ordereddict import OrderedDict
14 from qapi import *
15 import sys
16 import os
17 import getopt
18 import errno
19
20 def type_visitor(name):
21 if type(name) == list:
22 return 'visit_type_%sList' % name[0]
23 else:
24 return 'visit_type_%s' % name
25
26 def generate_command_decl(name, args, ret_type):
27 arglist=""
28 for argname, argtype, optional, structured in parse_args(args):
29 argtype = c_type(argtype)
30 if argtype == "char *":
31 argtype = "const char *"
32 if optional:
33 arglist += "bool has_%s, " % c_var(argname)
34 arglist += "%s %s, " % (argtype, c_var(argname))
35 return mcgen('''
36 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
37 ''',
38 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
39
40 def gen_sync_call(name, args, ret_type, indent=0):
41 ret = ""
42 arglist=""
43 retval=""
44 if ret_type:
45 retval = "retval = "
46 for argname, argtype, optional, structured in parse_args(args):
47 if optional:
48 arglist += "has_%s, " % c_var(argname)
49 arglist += "%s, " % (c_var(argname))
50 push_indent(indent)
51 ret = mcgen('''
52 %(retval)sqmp_%(name)s(%(args)serrp);
53
54 ''',
55 name=c_fun(name), args=arglist, retval=retval).rstrip()
56 if ret_type:
57 ret += "\n" + mcgen(''''
58 if (!error_is_set(errp)) {
59 %(marshal_output_call)s
60 }
61 ''',
62 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
63 pop_indent(indent)
64 return ret.rstrip()
65
66
67 def gen_marshal_output_call(name, ret_type):
68 if not ret_type:
69 return ""
70 return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name)
71
72 def gen_visitor_input_containers_decl(args, obj):
73 ret = ""
74
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()
84
85 return ret.rstrip()
86
87 def gen_visitor_input_vars_decl(args):
88 ret = ""
89 push_indent()
90 for argname, argtype, optional, structured in parse_args(args):
91 if optional:
92 ret += mcgen('''
93 bool has_%(argname)s = false;
94 ''',
95 argname=c_var(argname))
96 if c_type(argtype).endswith("*"):
97 ret += mcgen('''
98 %(argtype)s %(argname)s = NULL;
99 ''',
100 argname=c_var(argname), argtype=c_type(argtype))
101 else:
102 ret += mcgen('''
103 %(argtype)s %(argname)s;
104 ''',
105 argname=c_var(argname), argtype=c_type(argtype))
106
107 pop_indent()
108 return ret.rstrip()
109
110 def gen_visitor_input_block(args, dealloc=False):
111 ret = ""
112 errparg = 'errp'
113
114 if len(args) == 0:
115 return ret
116
117 push_indent()
118
119 if dealloc:
120 errparg = 'NULL'
121 ret += mcgen('''
122 qmp_input_visitor_cleanup(mi);
123 md = qapi_dealloc_visitor_new();
124 v = qapi_dealloc_get_visitor(md);
125 ''')
126 else:
127 ret += mcgen('''
128 v = qmp_input_get_visitor(mi);
129 ''')
130
131 for argname, argtype, optional, structured in parse_args(args):
132 if optional:
133 ret += mcgen('''
134 visit_start_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
135 if (has_%(c_name)s) {
136 ''',
137 c_name=c_var(argname), name=argname, errp=errparg)
138 push_indent()
139 ret += mcgen('''
140 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
141 ''',
142 c_name=c_var(argname), name=argname, argtype=argtype,
143 visitor=type_visitor(argtype), errp=errparg)
144 if optional:
145 pop_indent()
146 ret += mcgen('''
147 }
148 visit_end_optional(v, %(errp)s);
149 ''', errp=errparg)
150
151 if dealloc:
152 ret += mcgen('''
153 qapi_dealloc_visitor_cleanup(md);
154 ''')
155 pop_indent()
156 return ret.rstrip()
157
158 def gen_marshal_output(name, args, ret_type, middle_mode):
159 if not ret_type:
160 return ""
161
162 ret = mcgen('''
163 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
164 {
165 QmpOutputVisitor *mo = qmp_output_visitor_new();
166 QapiDeallocVisitor *md;
167 Visitor *v;
168
169 v = qmp_output_get_visitor(mo);
170 %(visitor)s(v, &ret_in, "unused", errp);
171 if (!error_is_set(errp)) {
172 *ret_out = qmp_output_get_qobject(mo);
173 }
174 qmp_output_visitor_cleanup(mo);
175 md = qapi_dealloc_visitor_new();
176 v = qapi_dealloc_get_visitor(md);
177 %(visitor)s(v, &ret_in, "unused", NULL);
178 qapi_dealloc_visitor_cleanup(md);
179 }
180 ''',
181 c_ret_type=c_type(ret_type), c_name=c_fun(name),
182 visitor=type_visitor(ret_type))
183
184 return ret
185
186 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
187 if middle_mode:
188 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
189 else:
190 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
191
192
193
194 def gen_marshal_input(name, args, ret_type, middle_mode):
195 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
196
197 ret = mcgen('''
198 %(header)s
199 {
200 ''',
201 header=hdr)
202
203 if middle_mode:
204 ret += mcgen('''
205 Error *local_err = NULL;
206 Error **errp = &local_err;
207 QDict *args = (QDict *)qdict;
208 ''')
209
210 if ret_type:
211 if c_type(ret_type).endswith("*"):
212 retval = " %s retval = NULL;" % c_type(ret_type)
213 else:
214 retval = " %s retval;" % c_type(ret_type)
215 ret += mcgen('''
216 %(retval)s
217 ''',
218 retval=retval)
219
220 if len(args) > 0:
221 ret += mcgen('''
222 %(visitor_input_containers_decl)s
223 %(visitor_input_vars_decl)s
224
225 %(visitor_input_block)s
226
227 ''',
228 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
229 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
230 visitor_input_block=gen_visitor_input_block(args))
231 else:
232 ret += mcgen('''
233 (void)args;
234 ''')
235
236 ret += mcgen('''
237 if (error_is_set(errp)) {
238 goto out;
239 }
240 %(sync_call)s
241 ''',
242 sync_call=gen_sync_call(name, args, ret_type, indent=4))
243 ret += mcgen('''
244
245 out:
246 ''')
247 ret += mcgen('''
248 %(visitor_input_block_cleanup)s
249 ''',
250 visitor_input_block_cleanup=gen_visitor_input_block(args,
251 dealloc=True))
252
253 if middle_mode:
254 ret += mcgen('''
255
256 if (local_err) {
257 qerror_report_err(local_err);
258 error_free(local_err);
259 return -1;
260 }
261 return 0;
262 ''')
263 else:
264 ret += mcgen('''
265 return;
266 ''')
267
268 ret += mcgen('''
269 }
270 ''')
271
272 return ret
273
274 def option_value_matches(opt, val, cmd):
275 if opt in cmd and cmd[opt] == val:
276 return True
277 return False
278
279 def gen_registry(commands):
280 registry=""
281 push_indent()
282 for cmd in commands:
283 options = 'QCO_NO_OPTIONS'
284 if option_value_matches('success-response', 'no', cmd):
285 options = 'QCO_NO_SUCCESS_RESP'
286
287 registry += mcgen('''
288 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
289 ''',
290 name=cmd['command'], c_name=c_fun(cmd['command']),
291 opts=options)
292 pop_indent()
293 ret = mcgen('''
294 static void qmp_init_marshal(void)
295 {
296 %(registry)s
297 }
298
299 qapi_init(qmp_init_marshal);
300 ''',
301 registry=registry.rstrip())
302 return ret
303
304 def gen_command_decl_prologue(header, guard, prefix=""):
305 ret = mcgen('''
306 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
307
308 /*
309 * schema-defined QAPI function prototypes
310 *
311 * Copyright IBM, Corp. 2011
312 *
313 * Authors:
314 * Anthony Liguori <aliguori@us.ibm.com>
315 *
316 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
317 * See the COPYING.LIB file in the top-level directory.
318 *
319 */
320
321 #ifndef %(guard)s
322 #define %(guard)s
323
324 #include "%(prefix)sqapi-types.h"
325 #include "qapi/qmp/qdict.h"
326 #include "qapi/error.h"
327
328 ''',
329 header=basename(header), guard=guardname(header), prefix=prefix)
330 return ret
331
332 def gen_command_def_prologue(prefix="", proxy=False):
333 ret = mcgen('''
334 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
335
336 /*
337 * schema-defined QMP->QAPI command dispatch
338 *
339 * Copyright IBM, Corp. 2011
340 *
341 * Authors:
342 * Anthony Liguori <aliguori@us.ibm.com>
343 *
344 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
345 * See the COPYING.LIB file in the top-level directory.
346 *
347 */
348
349 #include "qemu-common.h"
350 #include "qemu/module.h"
351 #include "qapi/qmp/qerror.h"
352 #include "qapi/qmp/types.h"
353 #include "qapi/qmp/dispatch.h"
354 #include "qapi/visitor.h"
355 #include "qapi/qmp-output-visitor.h"
356 #include "qapi/qmp-input-visitor.h"
357 #include "qapi/dealloc-visitor.h"
358 #include "%(prefix)sqapi-types.h"
359 #include "%(prefix)sqapi-visit.h"
360
361 ''',
362 prefix=prefix)
363 if not proxy:
364 ret += '#include "%sqmp-commands.h"' % prefix
365 return ret + "\n\n"
366
367
368 try:
369 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
370 ["source", "header", "prefix=",
371 "input-file=", "output-dir=",
372 "type=", "middle"])
373 except getopt.GetoptError, err:
374 print str(err)
375 sys.exit(1)
376
377 output_dir = ""
378 prefix = ""
379 dispatch_type = "sync"
380 c_file = 'qmp-marshal.c'
381 h_file = 'qmp-commands.h'
382 middle_mode = False
383
384 do_c = False
385 do_h = False
386
387 for o, a in opts:
388 if o in ("-p", "--prefix"):
389 prefix = a
390 elif o in ("-i", "--input-file"):
391 input_file = a
392 elif o in ("-o", "--output-dir"):
393 output_dir = a + "/"
394 elif o in ("-t", "--type"):
395 dispatch_type = a
396 elif o in ("-m", "--middle"):
397 middle_mode = True
398 elif o in ("-c", "--source"):
399 do_c = True
400 elif o in ("-h", "--header"):
401 do_h = True
402
403 if not do_c and not do_h:
404 do_c = True
405 do_h = True
406
407 c_file = output_dir + prefix + c_file
408 h_file = output_dir + prefix + h_file
409
410 def maybe_open(really, name, opt):
411 if really:
412 return open(name, opt)
413 else:
414 import StringIO
415 return StringIO.StringIO()
416
417 try:
418 os.makedirs(output_dir)
419 except os.error, e:
420 if e.errno != errno.EEXIST:
421 raise
422
423 exprs = parse_schema(input_file)
424 commands = filter(lambda expr: expr.has_key('command'), exprs)
425 commands = filter(lambda expr: not expr.has_key('gen'), commands)
426
427 if dispatch_type == "sync":
428 fdecl = maybe_open(do_h, h_file, 'w')
429 fdef = maybe_open(do_c, c_file, 'w')
430 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
431 fdecl.write(ret)
432 ret = gen_command_def_prologue(prefix=prefix)
433 fdef.write(ret)
434
435 for cmd in commands:
436 arglist = []
437 ret_type = None
438 if cmd.has_key('data'):
439 arglist = cmd['data']
440 if cmd.has_key('returns'):
441 ret_type = cmd['returns']
442 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
443 fdecl.write(ret)
444 if ret_type:
445 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
446 fdef.write(ret)
447
448 if middle_mode:
449 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
450
451 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
452 fdef.write(ret)
453
454 fdecl.write("\n#endif\n");
455
456 if not middle_mode:
457 ret = gen_registry(commands)
458 fdef.write(ret)
459
460 fdef.flush()
461 fdef.close()
462 fdecl.flush()
463 fdecl.close()