qapi-commands.py 12 KB

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