blob: 8c125cac1f7ca7086bb8b65a00b621f5c29a790c [file] [log] [blame]
Michael Rothc17d9902011-07-19 14:50:42 -05001#
2# QAPI command marshaller generator
3#
4# Copyright IBM, Corp. 2011
Eric Blaked708cdb2015-05-04 09:05:19 -06005# Copyright (C) 2014-2015 Red Hat, Inc.
Michael Rothc17d9902011-07-19 14:50:42 -05006#
7# Authors:
8# Anthony Liguori <aliguori@us.ibm.com>
9# Michael Roth <mdroth@linux.vnet.ibm.com>
Markus Armbruster297a3642014-05-07 09:53:54 +020010# Markus Armbruster <armbru@redhat.com>
Michael Rothc17d9902011-07-19 14:50:42 -050011#
Markus Armbruster678e48a2014-03-01 08:40:34 +010012# This work is licensed under the terms of the GNU GPL, version 2.
13# See the COPYING file in the top-level directory.
Michael Rothc17d9902011-07-19 14:50:42 -050014
15from ordereddict import OrderedDict
16from qapi import *
Markus Armbruster297a3642014-05-07 09:53:54 +020017import re
Michael Rothc17d9902011-07-19 14:50:42 -050018import sys
19import os
20import getopt
21import errno
22
Anthony Liguori15e43e62011-09-14 14:30:00 -050023def type_visitor(name):
24 if type(name) == list:
25 return 'visit_type_%sList' % name[0]
26 else:
27 return 'visit_type_%s' % name
28
Michael Rothc17d9902011-07-19 14:50:42 -050029def generate_command_decl(name, args, ret_type):
30 arglist=""
Eric Blake6b5abc72015-05-04 09:05:33 -060031 for argname, argtype, optional in parse_args(args):
Amos Kong0d14eeb2014-06-10 19:25:52 +080032 argtype = c_type(argtype, is_param=True)
Michael Rothc17d9902011-07-19 14:50:42 -050033 if optional:
Eric Blake18df5152015-05-14 06:50:48 -060034 arglist += "bool has_%s, " % c_name(argname)
35 arglist += "%s %s, " % (argtype, c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -050036 return mcgen('''
37%(ret_type)s qmp_%(name)s(%(args)sError **errp);
38''',
Eric Blake18df5152015-05-14 06:50:48 -060039 ret_type=c_type(ret_type), name=c_name(name),
40 args=arglist).strip()
Michael Rothc17d9902011-07-19 14:50:42 -050041
Markus Armbruster297a3642014-05-07 09:53:54 +020042def gen_err_check(errvar):
43 if errvar:
44 return mcgen('''
45if (local_err) {
46 goto out;
47}
48''')
49 return ''
50
Michael Rothc17d9902011-07-19 14:50:42 -050051def gen_sync_call(name, args, ret_type, indent=0):
52 ret = ""
53 arglist=""
54 retval=""
55 if ret_type:
56 retval = "retval = "
Eric Blake6b5abc72015-05-04 09:05:33 -060057 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -050058 if optional:
Eric Blake18df5152015-05-14 06:50:48 -060059 arglist += "has_%s, " % c_name(argname)
60 arglist += "%s, " % (c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -050061 push_indent(indent)
62 ret = mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +020063%(retval)sqmp_%(name)s(%(args)s&local_err);
Michael Rothc17d9902011-07-19 14:50:42 -050064
65''',
Eric Blake18df5152015-05-14 06:50:48 -060066 name=c_name(name), args=arglist, retval=retval).rstrip()
Michael Rothc17d9902011-07-19 14:50:42 -050067 if ret_type:
Markus Armbruster297a3642014-05-07 09:53:54 +020068 ret += "\n" + gen_err_check('local_err')
Michael Rothc17d9902011-07-19 14:50:42 -050069 ret += "\n" + mcgen(''''
Markus Armbruster297a3642014-05-07 09:53:54 +020070%(marshal_output_call)s
Michael Rothc17d9902011-07-19 14:50:42 -050071''',
72 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
73 pop_indent(indent)
74 return ret.rstrip()
75
76
77def gen_marshal_output_call(name, ret_type):
78 if not ret_type:
79 return ""
Eric Blake18df5152015-05-14 06:50:48 -060080 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_name(name)
Michael Rothc17d9902011-07-19 14:50:42 -050081
Markus Armbrusterf9bee752014-05-07 09:53:44 +020082def gen_visitor_input_containers_decl(args, obj):
Michael Rothc17d9902011-07-19 14:50:42 -050083 ret = ""
84
85 push_indent()
86 if len(args) > 0:
87 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +020088QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
Michael Rothc17d9902011-07-19 14:50:42 -050089QapiDeallocVisitor *md;
90Visitor *v;
Markus Armbrusterf9bee752014-05-07 09:53:44 +020091''',
92 obj=obj)
Michael Rothc17d9902011-07-19 14:50:42 -050093 pop_indent()
94
95 return ret.rstrip()
96
97def gen_visitor_input_vars_decl(args):
98 ret = ""
99 push_indent()
Eric Blake6b5abc72015-05-04 09:05:33 -0600100 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -0500101 if optional:
102 ret += mcgen('''
103bool has_%(argname)s = false;
104''',
Eric Blake18df5152015-05-14 06:50:48 -0600105 argname=c_name(argname))
Amos Kong05dfb262014-06-10 19:25:53 +0800106 if is_c_ptr(argtype):
Michael Rothc17d9902011-07-19 14:50:42 -0500107 ret += mcgen('''
108%(argtype)s %(argname)s = NULL;
109''',
Eric Blake18df5152015-05-14 06:50:48 -0600110 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500111 else:
112 ret += mcgen('''
Michael Rothfc13d932014-05-20 12:20:39 -0500113%(argtype)s %(argname)s = {0};
Michael Rothc17d9902011-07-19 14:50:42 -0500114''',
Eric Blake18df5152015-05-14 06:50:48 -0600115 argname=c_name(argname), argtype=c_type(argtype))
Michael Rothc17d9902011-07-19 14:50:42 -0500116
117 pop_indent()
118 return ret.rstrip()
119
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200120def gen_visitor_input_block(args, dealloc=False):
Michael Rothc17d9902011-07-19 14:50:42 -0500121 ret = ""
Markus Armbruster297a3642014-05-07 09:53:54 +0200122 errparg = '&local_err'
123 errarg = 'local_err'
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400124
Michael Rothc17d9902011-07-19 14:50:42 -0500125 if len(args) == 0:
126 return ret
127
128 push_indent()
129
130 if dealloc:
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400131 errparg = 'NULL'
Markus Armbruster297a3642014-05-07 09:53:54 +0200132 errarg = None;
Michael Rothc17d9902011-07-19 14:50:42 -0500133 ret += mcgen('''
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200134qmp_input_visitor_cleanup(mi);
Michael Rothc17d9902011-07-19 14:50:42 -0500135md = qapi_dealloc_visitor_new();
136v = qapi_dealloc_get_visitor(md);
137''')
138 else:
139 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500140v = qmp_input_get_visitor(mi);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200141''')
Michael Rothc17d9902011-07-19 14:50:42 -0500142
Eric Blake6b5abc72015-05-04 09:05:33 -0600143 for argname, argtype, optional in parse_args(args):
Michael Rothc17d9902011-07-19 14:50:42 -0500144 if optional:
145 ret += mcgen('''
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200146visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500147''',
Eric Blake18df5152015-05-14 06:50:48 -0600148 c_name=c_name(argname), name=argname, errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200149 ret += gen_err_check(errarg)
150 ret += mcgen('''
151if (has_%(c_name)s) {
152''',
Eric Blake18df5152015-05-14 06:50:48 -0600153 c_name=c_name(argname))
Michael Rothc17d9902011-07-19 14:50:42 -0500154 push_indent()
155 ret += mcgen('''
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400156%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500157''',
Eric Blake18df5152015-05-14 06:50:48 -0600158 c_name=c_name(argname), name=argname, argtype=argtype,
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400159 visitor=type_visitor(argtype), errp=errparg)
Markus Armbruster297a3642014-05-07 09:53:54 +0200160 ret += gen_err_check(errarg)
Michael Rothc17d9902011-07-19 14:50:42 -0500161 if optional:
162 pop_indent()
163 ret += mcgen('''
164}
Markus Armbrustere2cd0f42014-05-07 09:53:46 +0200165''')
Michael Rothc17d9902011-07-19 14:50:42 -0500166
167 if dealloc:
168 ret += mcgen('''
169qapi_dealloc_visitor_cleanup(md);
170''')
Michael Rothc17d9902011-07-19 14:50:42 -0500171 pop_indent()
172 return ret.rstrip()
173
Anthony Liguori776574d2011-09-02 12:34:46 -0500174def gen_marshal_output(name, args, ret_type, middle_mode):
Michael Rothc17d9902011-07-19 14:50:42 -0500175 if not ret_type:
176 return ""
Anthony Liguori776574d2011-09-02 12:34:46 -0500177
Michael Rothc17d9902011-07-19 14:50:42 -0500178 ret = mcgen('''
179static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
180{
Markus Armbruster297a3642014-05-07 09:53:54 +0200181 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500182 QmpOutputVisitor *mo = qmp_output_visitor_new();
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200183 QapiDeallocVisitor *md;
Michael Rothc17d9902011-07-19 14:50:42 -0500184 Visitor *v;
185
186 v = qmp_output_get_visitor(mo);
Markus Armbruster297a3642014-05-07 09:53:54 +0200187 %(visitor)s(v, &ret_in, "unused", &local_err);
188 if (local_err) {
189 goto out;
Michael Rothc17d9902011-07-19 14:50:42 -0500190 }
Markus Armbruster297a3642014-05-07 09:53:54 +0200191 *ret_out = qmp_output_get_qobject(mo);
192
193out:
194 error_propagate(errp, local_err);
Michael Rothc17d9902011-07-19 14:50:42 -0500195 qmp_output_visitor_cleanup(mo);
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200196 md = qapi_dealloc_visitor_new();
Michael Rothc17d9902011-07-19 14:50:42 -0500197 v = qapi_dealloc_get_visitor(md);
Luiz Capitulino8f91ad82013-07-11 14:26:56 -0400198 %(visitor)s(v, &ret_in, "unused", NULL);
Michael Rothc17d9902011-07-19 14:50:42 -0500199 qapi_dealloc_visitor_cleanup(md);
200}
201''',
Eric Blake18df5152015-05-14 06:50:48 -0600202 c_ret_type=c_type(ret_type), c_name=c_name(name),
Anthony Liguori15e43e62011-09-14 14:30:00 -0500203 visitor=type_visitor(ret_type))
Michael Rothc17d9902011-07-19 14:50:42 -0500204
205 return ret
206
Anthony Liguori776574d2011-09-02 12:34:46 -0500207def gen_marshal_input_decl(name, args, ret_type, middle_mode):
208 if middle_mode:
Eric Blake18df5152015-05-14 06:50:48 -0600209 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_name(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500210 else:
Eric Blake18df5152015-05-14 06:50:48 -0600211 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
Anthony Liguori776574d2011-09-02 12:34:46 -0500212
213
214
215def gen_marshal_input(name, args, ret_type, middle_mode):
216 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
217
Michael Rothc17d9902011-07-19 14:50:42 -0500218 ret = mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500219%(header)s
Michael Rothc17d9902011-07-19 14:50:42 -0500220{
Markus Armbruster297a3642014-05-07 09:53:54 +0200221 Error *local_err = NULL;
Michael Rothc17d9902011-07-19 14:50:42 -0500222''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500223 header=hdr)
224
225 if middle_mode:
226 ret += mcgen('''
Anthony Liguori776574d2011-09-02 12:34:46 -0500227 QDict *args = (QDict *)qdict;
228''')
Michael Rothc17d9902011-07-19 14:50:42 -0500229
230 if ret_type:
Amos Kong05dfb262014-06-10 19:25:53 +0800231 if is_c_ptr(ret_type):
Michael Rothc17d9902011-07-19 14:50:42 -0500232 retval = " %s retval = NULL;" % c_type(ret_type)
233 else:
234 retval = " %s retval;" % c_type(ret_type)
235 ret += mcgen('''
236%(retval)s
237''',
238 retval=retval)
239
240 if len(args) > 0:
241 ret += mcgen('''
242%(visitor_input_containers_decl)s
243%(visitor_input_vars_decl)s
244
245%(visitor_input_block)s
246
247''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200248 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
Michael Rothc17d9902011-07-19 14:50:42 -0500249 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200250 visitor_input_block=gen_visitor_input_block(args))
Anthony Liguori776574d2011-09-02 12:34:46 -0500251 else:
252 ret += mcgen('''
Markus Armbruster297a3642014-05-07 09:53:54 +0200253
Anthony Liguori776574d2011-09-02 12:34:46 -0500254 (void)args;
255''')
Michael Rothc17d9902011-07-19 14:50:42 -0500256
257 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500258%(sync_call)s
259''',
260 sync_call=gen_sync_call(name, args, ret_type, indent=4))
Markus Armbruster297a3642014-05-07 09:53:54 +0200261 if re.search('^ *goto out\\;', ret, re.MULTILINE):
262 ret += mcgen('''
Michael Rothc17d9902011-07-19 14:50:42 -0500263
264out:
265''')
Markus Armbruster297a3642014-05-07 09:53:54 +0200266 if not middle_mode:
267 ret += mcgen('''
268 error_propagate(errp, local_err);
269''')
Michael Rothc17d9902011-07-19 14:50:42 -0500270 ret += mcgen('''
271%(visitor_input_block_cleanup)s
Michael Rothc17d9902011-07-19 14:50:42 -0500272''',
Markus Armbrusterf9bee752014-05-07 09:53:44 +0200273 visitor_input_block_cleanup=gen_visitor_input_block(args,
Anthony Liguori776574d2011-09-02 12:34:46 -0500274 dealloc=True))
275
276 if middle_mode:
277 ret += mcgen('''
278
279 if (local_err) {
280 qerror_report_err(local_err);
281 error_free(local_err);
282 return -1;
283 }
284 return 0;
285''')
286 else:
287 ret += mcgen('''
288 return;
289''')
290
291 ret += mcgen('''
292}
293''')
294
Michael Rothc17d9902011-07-19 14:50:42 -0500295 return ret
296
297def gen_registry(commands):
298 registry=""
299 push_indent()
300 for cmd in commands:
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300301 options = 'QCO_NO_OPTIONS'
Eric Blaked708cdb2015-05-04 09:05:19 -0600302 if not cmd.get('success-response', True):
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300303 options = 'QCO_NO_SUCCESS_RESP'
304
Michael Rothc17d9902011-07-19 14:50:42 -0500305 registry += mcgen('''
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300306qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
Michael Rothc17d9902011-07-19 14:50:42 -0500307''',
Eric Blake18df5152015-05-14 06:50:48 -0600308 name=cmd['command'], c_name=c_name(cmd['command']),
Luiz Capitulinod34b8672012-05-08 14:24:44 -0300309 opts=options)
Michael Rothc17d9902011-07-19 14:50:42 -0500310 pop_indent()
311 ret = mcgen('''
312static void qmp_init_marshal(void)
313{
314%(registry)s
315}
316
317qapi_init(qmp_init_marshal);
318''',
319 registry=registry.rstrip())
320 return ret
321
322def gen_command_decl_prologue(header, guard, prefix=""):
323 ret = mcgen('''
324/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
325
326/*
327 * schema-defined QAPI function prototypes
328 *
329 * Copyright IBM, Corp. 2011
330 *
331 * Authors:
332 * Anthony Liguori <aliguori@us.ibm.com>
333 *
334 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
335 * See the COPYING.LIB file in the top-level directory.
336 *
337 */
338
339#ifndef %(guard)s
340#define %(guard)s
341
342#include "%(prefix)sqapi-types.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100343#include "qapi/qmp/qdict.h"
344#include "qapi/error.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500345
346''',
Anthony Liguori776574d2011-09-02 12:34:46 -0500347 header=basename(header), guard=guardname(header), prefix=prefix)
Michael Rothc17d9902011-07-19 14:50:42 -0500348 return ret
349
350def gen_command_def_prologue(prefix="", proxy=False):
351 ret = mcgen('''
352/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
353
354/*
355 * schema-defined QMP->QAPI command dispatch
356 *
357 * Copyright IBM, Corp. 2011
358 *
359 * Authors:
360 * Anthony Liguori <aliguori@us.ibm.com>
361 *
362 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
363 * See the COPYING.LIB file in the top-level directory.
364 *
365 */
366
Paolo Bonzini79ee7df2012-12-06 11:22:34 +0100367#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100368#include "qemu/module.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100369#include "qapi/qmp/qerror.h"
370#include "qapi/qmp/types.h"
371#include "qapi/qmp/dispatch.h"
372#include "qapi/visitor.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500373#include "qapi/qmp-output-visitor.h"
374#include "qapi/qmp-input-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +0100375#include "qapi/dealloc-visitor.h"
Michael Rothc17d9902011-07-19 14:50:42 -0500376#include "%(prefix)sqapi-types.h"
377#include "%(prefix)sqapi-visit.h"
378
379''',
380 prefix=prefix)
381 if not proxy:
382 ret += '#include "%sqmp-commands.h"' % prefix
Anthony Liguori776574d2011-09-02 12:34:46 -0500383 return ret + "\n\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500384
385
386try:
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200387 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
Avi Kivity8d3bc512011-12-27 16:02:16 +0200388 ["source", "header", "prefix=",
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200389 "input-file=", "output-dir=",
390 "type=", "middle"])
Michael Rothc17d9902011-07-19 14:50:42 -0500391except getopt.GetoptError, err:
392 print str(err)
393 sys.exit(1)
394
395output_dir = ""
396prefix = ""
397dispatch_type = "sync"
398c_file = 'qmp-marshal.c'
399h_file = 'qmp-commands.h'
Anthony Liguori776574d2011-09-02 12:34:46 -0500400middle_mode = False
Michael Rothc17d9902011-07-19 14:50:42 -0500401
Avi Kivity8d3bc512011-12-27 16:02:16 +0200402do_c = False
403do_h = False
404
Michael Rothc17d9902011-07-19 14:50:42 -0500405for o, a in opts:
406 if o in ("-p", "--prefix"):
407 prefix = a
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200408 elif o in ("-i", "--input-file"):
409 input_file = a
Michael Rothc17d9902011-07-19 14:50:42 -0500410 elif o in ("-o", "--output-dir"):
411 output_dir = a + "/"
412 elif o in ("-t", "--type"):
413 dispatch_type = a
Anthony Liguori776574d2011-09-02 12:34:46 -0500414 elif o in ("-m", "--middle"):
415 middle_mode = True
Avi Kivity8d3bc512011-12-27 16:02:16 +0200416 elif o in ("-c", "--source"):
Avi Kivity8d3bc512011-12-27 16:02:16 +0200417 do_c = True
Avi Kivity19bf7c82011-12-28 12:26:58 +0200418 elif o in ("-h", "--header"):
419 do_h = True
Avi Kivity8d3bc512011-12-27 16:02:16 +0200420
421if not do_c and not do_h:
422 do_c = True
423 do_h = True
Michael Rothc17d9902011-07-19 14:50:42 -0500424
425c_file = output_dir + prefix + c_file
426h_file = output_dir + prefix + h_file
427
Avi Kivity8d3bc512011-12-27 16:02:16 +0200428def maybe_open(really, name, opt):
Avi Kivity8d3bc512011-12-27 16:02:16 +0200429 if really:
430 return open(name, opt)
431 else:
Avi Kivity19bf7c82011-12-28 12:26:58 +0200432 import StringIO
433 return StringIO.StringIO()
Avi Kivity8d3bc512011-12-27 16:02:16 +0200434
Michael Rothc17d9902011-07-19 14:50:42 -0500435try:
436 os.makedirs(output_dir)
437except os.error, e:
438 if e.errno != errno.EEXIST:
439 raise
440
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200441exprs = parse_schema(input_file)
Michael Rothc17d9902011-07-19 14:50:42 -0500442commands = filter(lambda expr: expr.has_key('command'), exprs)
Anthony Liguori5dbee472011-12-12 14:29:33 -0600443commands = filter(lambda expr: not expr.has_key('gen'), commands)
Michael Rothc17d9902011-07-19 14:50:42 -0500444
445if dispatch_type == "sync":
Avi Kivity8d3bc512011-12-27 16:02:16 +0200446 fdecl = maybe_open(do_h, h_file, 'w')
447 fdef = maybe_open(do_c, c_file, 'w')
Michael Rothc17d9902011-07-19 14:50:42 -0500448 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
449 fdecl.write(ret)
450 ret = gen_command_def_prologue(prefix=prefix)
451 fdef.write(ret)
452
453 for cmd in commands:
454 arglist = []
455 ret_type = None
456 if cmd.has_key('data'):
457 arglist = cmd['data']
458 if cmd.has_key('returns'):
459 ret_type = cmd['returns']
460 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
461 fdecl.write(ret)
462 if ret_type:
Anthony Liguori776574d2011-09-02 12:34:46 -0500463 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500464 fdef.write(ret)
Anthony Liguori776574d2011-09-02 12:34:46 -0500465
466 if middle_mode:
467 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
468
469 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
Michael Rothc17d9902011-07-19 14:50:42 -0500470 fdef.write(ret)
471
Michael Roth7534ba02011-08-10 13:10:51 -0500472 fdecl.write("\n#endif\n");
Anthony Liguori776574d2011-09-02 12:34:46 -0500473
474 if not middle_mode:
475 ret = gen_registry(commands)
476 fdef.write(ret)
Michael Rothc17d9902011-07-19 14:50:42 -0500477
478 fdef.flush()
479 fdef.close()
480 fdecl.flush()
481 fdecl.close()