qapi-commands.py 12 KB

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