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