w32: Fix regression caused by new g_poll implementation
[qemu/rayw.git] / scripts / qapi-commands.py
blob7d93d01ed2f7906ebc1217dce9d0918ede6543c9
2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014 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
18 import sys
19 import os
20 import getopt
21 import errno
23 def type_visitor(name):
24 if type(name) == list:
25 return 'visit_type_%sList' % name[0]
26 else:
27 return 'visit_type_%s' % name
29 def generate_command_decl(name, args, ret_type):
30 arglist=""
31 for argname, argtype, optional, structured in parse_args(args):
32 argtype = c_type(argtype)
33 if argtype == "char *":
34 argtype = "const char *"
35 if optional:
36 arglist += "bool has_%s, " % c_var(argname)
37 arglist += "%s %s, " % (argtype, c_var(argname))
38 return mcgen('''
39 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
40 ''',
41 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
43 def gen_err_check(errvar):
44 if errvar:
45 return mcgen('''
46 if (local_err) {
47 goto out;
49 ''')
50 return ''
52 def gen_sync_call(name, args, ret_type, indent=0):
53 ret = ""
54 arglist=""
55 retval=""
56 if ret_type:
57 retval = "retval = "
58 for argname, argtype, optional, structured in parse_args(args):
59 if optional:
60 arglist += "has_%s, " % c_var(argname)
61 arglist += "%s, " % (c_var(argname))
62 push_indent(indent)
63 ret = mcgen('''
64 %(retval)sqmp_%(name)s(%(args)s&local_err);
66 ''',
67 name=c_fun(name), args=arglist, retval=retval).rstrip()
68 if ret_type:
69 ret += "\n" + gen_err_check('local_err')
70 ret += "\n" + mcgen(''''
71 %(marshal_output_call)s
72 ''',
73 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
74 pop_indent(indent)
75 return ret.rstrip()
78 def gen_marshal_output_call(name, ret_type):
79 if not ret_type:
80 return ""
81 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name)
83 def gen_visitor_input_containers_decl(args, obj):
84 ret = ""
86 push_indent()
87 if len(args) > 0:
88 ret += mcgen('''
89 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
90 QapiDeallocVisitor *md;
91 Visitor *v;
92 ''',
93 obj=obj)
94 pop_indent()
96 return ret.rstrip()
98 def gen_visitor_input_vars_decl(args):
99 ret = ""
100 push_indent()
101 for argname, argtype, optional, structured in parse_args(args):
102 if optional:
103 ret += mcgen('''
104 bool has_%(argname)s = false;
105 ''',
106 argname=c_var(argname))
107 if c_type(argtype).endswith("*"):
108 ret += mcgen('''
109 %(argtype)s %(argname)s = NULL;
110 ''',
111 argname=c_var(argname), argtype=c_type(argtype))
112 else:
113 ret += mcgen('''
114 %(argtype)s %(argname)s = {0};
115 ''',
116 argname=c_var(argname), argtype=c_type(argtype))
118 pop_indent()
119 return ret.rstrip()
121 def gen_visitor_input_block(args, dealloc=False):
122 ret = ""
123 errparg = '&local_err'
124 errarg = 'local_err'
126 if len(args) == 0:
127 return ret
129 push_indent()
131 if dealloc:
132 errparg = 'NULL'
133 errarg = None;
134 ret += mcgen('''
135 qmp_input_visitor_cleanup(mi);
136 md = qapi_dealloc_visitor_new();
137 v = qapi_dealloc_get_visitor(md);
138 ''')
139 else:
140 ret += mcgen('''
141 v = qmp_input_get_visitor(mi);
142 ''')
144 for argname, argtype, optional, structured in parse_args(args):
145 if optional:
146 ret += mcgen('''
147 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
148 ''',
149 c_name=c_var(argname), name=argname, errp=errparg)
150 ret += gen_err_check(errarg)
151 ret += mcgen('''
152 if (has_%(c_name)s) {
153 ''',
154 c_name=c_var(argname))
155 push_indent()
156 ret += mcgen('''
157 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
158 ''',
159 c_name=c_var(argname), name=argname, argtype=argtype,
160 visitor=type_visitor(argtype), errp=errparg)
161 ret += gen_err_check(errarg)
162 if optional:
163 pop_indent()
164 ret += mcgen('''
166 ''')
168 if dealloc:
169 ret += mcgen('''
170 qapi_dealloc_visitor_cleanup(md);
171 ''')
172 pop_indent()
173 return ret.rstrip()
175 def gen_marshal_output(name, args, ret_type, middle_mode):
176 if not ret_type:
177 return ""
179 ret = mcgen('''
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;
185 Visitor *v;
187 v = qmp_output_get_visitor(mo);
188 %(visitor)s(v, &ret_in, "unused", &local_err);
189 if (local_err) {
190 goto out;
192 *ret_out = qmp_output_get_qobject(mo);
194 out:
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);
202 ''',
203 c_ret_type=c_type(ret_type), c_name=c_fun(name),
204 visitor=type_visitor(ret_type))
206 return ret
208 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
209 if middle_mode:
210 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
211 else:
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)
219 ret = mcgen('''
220 %(header)s
222 Error *local_err = NULL;
223 ''',
224 header=hdr)
226 if middle_mode:
227 ret += mcgen('''
228 QDict *args = (QDict *)qdict;
229 ''')
231 if ret_type:
232 if c_type(ret_type).endswith("*"):
233 retval = " %s retval = NULL;" % c_type(ret_type)
234 else:
235 retval = " %s retval;" % c_type(ret_type)
236 ret += mcgen('''
237 %(retval)s
238 ''',
239 retval=retval)
241 if len(args) > 0:
242 ret += mcgen('''
243 %(visitor_input_containers_decl)s
244 %(visitor_input_vars_decl)s
246 %(visitor_input_block)s
248 ''',
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))
252 else:
253 ret += mcgen('''
255 (void)args;
256 ''')
258 ret += mcgen('''
259 %(sync_call)s
260 ''',
261 sync_call=gen_sync_call(name, args, ret_type, indent=4))
262 if re.search('^ *goto out\\;', ret, re.MULTILINE):
263 ret += mcgen('''
265 out:
266 ''')
267 if not middle_mode:
268 ret += mcgen('''
269 error_propagate(errp, local_err);
270 ''')
271 ret += mcgen('''
272 %(visitor_input_block_cleanup)s
273 ''',
274 visitor_input_block_cleanup=gen_visitor_input_block(args,
275 dealloc=True))
277 if middle_mode:
278 ret += mcgen('''
280 if (local_err) {
281 qerror_report_err(local_err);
282 error_free(local_err);
283 return -1;
285 return 0;
286 ''')
287 else:
288 ret += mcgen('''
289 return;
290 ''')
292 ret += mcgen('''
294 ''')
296 return ret
298 def option_value_matches(opt, val, cmd):
299 if opt in cmd and cmd[opt] == val:
300 return True
301 return False
303 def gen_registry(commands):
304 registry=""
305 push_indent()
306 for cmd in 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);
313 ''',
314 name=cmd['command'], c_name=c_fun(cmd['command']),
315 opts=options)
316 pop_indent()
317 ret = mcgen('''
318 static void qmp_init_marshal(void)
320 %(registry)s
323 qapi_init(qmp_init_marshal);
324 ''',
325 registry=registry.rstrip())
326 return ret
328 def gen_command_decl_prologue(header, guard, prefix=""):
329 ret = mcgen('''
330 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
333 * schema-defined QAPI function prototypes
335 * Copyright IBM, Corp. 2011
337 * Authors:
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.
345 #ifndef %(guard)s
346 #define %(guard)s
348 #include "%(prefix)sqapi-types.h"
349 #include "qapi/qmp/qdict.h"
350 #include "qapi/error.h"
352 ''',
353 header=basename(header), guard=guardname(header), prefix=prefix)
354 return ret
356 def gen_command_def_prologue(prefix="", proxy=False):
357 ret = mcgen('''
358 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
361 * schema-defined QMP->QAPI command dispatch
363 * Copyright IBM, Corp. 2011
365 * Authors:
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"
385 ''',
386 prefix=prefix)
387 if not proxy:
388 ret += '#include "%sqmp-commands.h"' % prefix
389 return ret + "\n\n"
392 try:
393 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
394 ["source", "header", "prefix=",
395 "input-file=", "output-dir=",
396 "type=", "middle"])
397 except getopt.GetoptError, err:
398 print str(err)
399 sys.exit(1)
401 output_dir = ""
402 prefix = ""
403 dispatch_type = "sync"
404 c_file = 'qmp-marshal.c'
405 h_file = 'qmp-commands.h'
406 middle_mode = False
408 do_c = False
409 do_h = False
411 for o, a in opts:
412 if o in ("-p", "--prefix"):
413 prefix = a
414 elif o in ("-i", "--input-file"):
415 input_file = a
416 elif o in ("-o", "--output-dir"):
417 output_dir = a + "/"
418 elif o in ("-t", "--type"):
419 dispatch_type = a
420 elif o in ("-m", "--middle"):
421 middle_mode = True
422 elif o in ("-c", "--source"):
423 do_c = True
424 elif o in ("-h", "--header"):
425 do_h = True
427 if not do_c and not do_h:
428 do_c = True
429 do_h = True
431 c_file = output_dir + prefix + c_file
432 h_file = output_dir + prefix + h_file
434 def maybe_open(really, name, opt):
435 if really:
436 return open(name, opt)
437 else:
438 import StringIO
439 return StringIO.StringIO()
441 try:
442 os.makedirs(output_dir)
443 except os.error, e:
444 if e.errno != errno.EEXIST:
445 raise
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)
455 fdecl.write(ret)
456 ret = gen_command_def_prologue(prefix=prefix)
457 fdef.write(ret)
459 for cmd in commands:
460 arglist = []
461 ret_type = None
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"
467 fdecl.write(ret)
468 if ret_type:
469 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
470 fdef.write(ret)
472 if middle_mode:
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"
476 fdef.write(ret)
478 fdecl.write("\n#endif\n");
480 if not middle_mode:
481 ret = gen_registry(commands)
482 fdef.write(ret)
484 fdef.flush()
485 fdef.close()
486 fdecl.flush()
487 fdecl.close()