2
0

qapi-commands.py 12 KB

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