Statistics
| Branch: | Revision:

root / scripts / qapi-commands.py @ 15e43e64

History | View | Annotate | Download (10.9 kB)

1 c17d9908 Michael Roth
#
2 c17d9908 Michael Roth
# QAPI command marshaller generator
3 c17d9908 Michael Roth
#
4 c17d9908 Michael Roth
# Copyright IBM, Corp. 2011
5 c17d9908 Michael Roth
#
6 c17d9908 Michael Roth
# Authors:
7 c17d9908 Michael Roth
#  Anthony Liguori <aliguori@us.ibm.com>
8 c17d9908 Michael Roth
#  Michael Roth    <mdroth@linux.vnet.ibm.com>
9 c17d9908 Michael Roth
#
10 c17d9908 Michael Roth
# This work is licensed under the terms of the GNU GPLv2.
11 c17d9908 Michael Roth
# See the COPYING.LIB file in the top-level directory.
12 c17d9908 Michael Roth
13 c17d9908 Michael Roth
from ordereddict import OrderedDict
14 c17d9908 Michael Roth
from qapi import *
15 c17d9908 Michael Roth
import sys
16 c17d9908 Michael Roth
import os
17 c17d9908 Michael Roth
import getopt
18 c17d9908 Michael Roth
import errno
19 c17d9908 Michael Roth
20 15e43e64 Anthony Liguori
def type_visitor(name):
21 15e43e64 Anthony Liguori
    if type(name) == list:
22 15e43e64 Anthony Liguori
        return 'visit_type_%sList' % name[0]
23 15e43e64 Anthony Liguori
    else:
24 15e43e64 Anthony Liguori
        return 'visit_type_%s' % name
25 15e43e64 Anthony Liguori
26 c17d9908 Michael Roth
def generate_decl_enum(name, members, genlist=True):
27 c17d9908 Michael Roth
    return mcgen('''
28 c17d9908 Michael Roth

29 15e43e64 Anthony Liguori
void %(visitor)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
30 c17d9908 Michael Roth
''',
31 15e43e64 Anthony Liguori
                 visitor=type_visitor(name))
32 c17d9908 Michael Roth
33 c17d9908 Michael Roth
def generate_command_decl(name, args, ret_type):
34 c17d9908 Michael Roth
    arglist=""
35 c17d9908 Michael Roth
    for argname, argtype, optional, structured in parse_args(args):
36 c17d9908 Michael Roth
        argtype = c_type(argtype)
37 c17d9908 Michael Roth
        if argtype == "char *":
38 c17d9908 Michael Roth
            argtype = "const char *"
39 c17d9908 Michael Roth
        if optional:
40 c17d9908 Michael Roth
            arglist += "bool has_%s, " % c_var(argname)
41 c17d9908 Michael Roth
        arglist += "%s %s, " % (argtype, c_var(argname))
42 c17d9908 Michael Roth
    return mcgen('''
43 c17d9908 Michael Roth
%(ret_type)s qmp_%(name)s(%(args)sError **errp);
44 c17d9908 Michael Roth
''',
45 c17d9908 Michael Roth
                 ret_type=c_type(ret_type), name=c_var(name), args=arglist).strip()
46 c17d9908 Michael Roth
47 c17d9908 Michael Roth
def gen_sync_call(name, args, ret_type, indent=0):
48 c17d9908 Michael Roth
    ret = ""
49 c17d9908 Michael Roth
    arglist=""
50 c17d9908 Michael Roth
    retval=""
51 c17d9908 Michael Roth
    if ret_type:
52 c17d9908 Michael Roth
        retval = "retval = "
53 c17d9908 Michael Roth
    for argname, argtype, optional, structured in parse_args(args):
54 c17d9908 Michael Roth
        if optional:
55 c17d9908 Michael Roth
            arglist += "has_%s, " % c_var(argname)
56 c17d9908 Michael Roth
        arglist += "%s, " % (c_var(argname))
57 c17d9908 Michael Roth
    push_indent(indent)
58 c17d9908 Michael Roth
    ret = mcgen('''
59 c17d9908 Michael Roth
%(retval)sqmp_%(name)s(%(args)serrp);
60 c17d9908 Michael Roth

61 c17d9908 Michael Roth
''',
62 c17d9908 Michael Roth
                name=c_var(name), args=arglist, retval=retval).rstrip()
63 c17d9908 Michael Roth
    if ret_type:
64 c17d9908 Michael Roth
        ret += "\n" + mcgen(''''
65 c17d9908 Michael Roth
%(marshal_output_call)s
66 c17d9908 Michael Roth
''',
67 c17d9908 Michael Roth
                            marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
68 c17d9908 Michael Roth
    pop_indent(indent)
69 c17d9908 Michael Roth
    return ret.rstrip()
70 c17d9908 Michael Roth
71 c17d9908 Michael Roth
72 c17d9908 Michael Roth
def gen_marshal_output_call(name, ret_type):
73 c17d9908 Michael Roth
    if not ret_type:
74 c17d9908 Michael Roth
        return ""
75 c17d9908 Michael Roth
    return "qmp_marshal_output_%s(retval, ret, errp);" % c_var(name)
76 c17d9908 Michael Roth
77 c17d9908 Michael Roth
def gen_visitor_output_containers_decl(ret_type):
78 c17d9908 Michael Roth
    ret = ""
79 c17d9908 Michael Roth
    push_indent()
80 c17d9908 Michael Roth
    if ret_type:
81 c17d9908 Michael Roth
        ret += mcgen('''
82 c17d9908 Michael Roth
QmpOutputVisitor *mo;
83 c17d9908 Michael Roth
QapiDeallocVisitor *md;
84 c17d9908 Michael Roth
Visitor *v;
85 c17d9908 Michael Roth
''')
86 c17d9908 Michael Roth
    pop_indent()
87 c17d9908 Michael Roth
88 c17d9908 Michael Roth
    return ret
89 c17d9908 Michael Roth
90 c17d9908 Michael Roth
def gen_visitor_input_containers_decl(args):
91 c17d9908 Michael Roth
    ret = ""
92 c17d9908 Michael Roth
93 c17d9908 Michael Roth
    push_indent()
94 c17d9908 Michael Roth
    if len(args) > 0:
95 c17d9908 Michael Roth
        ret += mcgen('''
96 c17d9908 Michael Roth
QmpInputVisitor *mi;
97 c17d9908 Michael Roth
QapiDeallocVisitor *md;
98 c17d9908 Michael Roth
Visitor *v;
99 c17d9908 Michael Roth
''')
100 c17d9908 Michael Roth
    pop_indent()
101 c17d9908 Michael Roth
102 c17d9908 Michael Roth
    return ret.rstrip()
103 c17d9908 Michael Roth
104 c17d9908 Michael Roth
def gen_visitor_input_vars_decl(args):
105 c17d9908 Michael Roth
    ret = ""
106 c17d9908 Michael Roth
    push_indent()
107 c17d9908 Michael Roth
    for argname, argtype, optional, structured in parse_args(args):
108 c17d9908 Michael Roth
        if optional:
109 c17d9908 Michael Roth
            ret += mcgen('''
110 c17d9908 Michael Roth
bool has_%(argname)s = false;
111 c17d9908 Michael Roth
''',
112 c17d9908 Michael Roth
                         argname=c_var(argname))
113 c17d9908 Michael Roth
        if c_type(argtype).endswith("*"):
114 c17d9908 Michael Roth
            ret += mcgen('''
115 c17d9908 Michael Roth
%(argtype)s %(argname)s = NULL;
116 c17d9908 Michael Roth
''',
117 c17d9908 Michael Roth
                         argname=c_var(argname), argtype=c_type(argtype))
118 c17d9908 Michael Roth
        else:
119 c17d9908 Michael Roth
            ret += mcgen('''
120 c17d9908 Michael Roth
%(argtype)s %(argname)s;
121 c17d9908 Michael Roth
''',
122 c17d9908 Michael Roth
                         argname=c_var(argname), argtype=c_type(argtype))
123 c17d9908 Michael Roth
124 c17d9908 Michael Roth
    pop_indent()
125 c17d9908 Michael Roth
    return ret.rstrip()
126 c17d9908 Michael Roth
127 c17d9908 Michael Roth
def gen_visitor_input_block(args, obj, dealloc=False):
128 c17d9908 Michael Roth
    ret = ""
129 c17d9908 Michael Roth
    if len(args) == 0:
130 c17d9908 Michael Roth
        return ret
131 c17d9908 Michael Roth
132 c17d9908 Michael Roth
    push_indent()
133 c17d9908 Michael Roth
134 c17d9908 Michael Roth
    if dealloc:
135 c17d9908 Michael Roth
        ret += mcgen('''
136 c17d9908 Michael Roth
md = qapi_dealloc_visitor_new();
137 c17d9908 Michael Roth
v = qapi_dealloc_get_visitor(md);
138 c17d9908 Michael Roth
''')
139 c17d9908 Michael Roth
    else:
140 c17d9908 Michael Roth
        ret += mcgen('''
141 c17d9908 Michael Roth
mi = qmp_input_visitor_new(%(obj)s);
142 c17d9908 Michael Roth
v = qmp_input_get_visitor(mi);
143 c17d9908 Michael Roth
''',
144 c17d9908 Michael Roth
                     obj=obj)
145 c17d9908 Michael Roth
146 c17d9908 Michael Roth
    for argname, argtype, optional, structured in parse_args(args):
147 c17d9908 Michael Roth
        if optional:
148 c17d9908 Michael Roth
            ret += mcgen('''
149 c17d9908 Michael Roth
visit_start_optional(v, &has_%(c_name)s, "%(name)s", errp);
150 c17d9908 Michael Roth
if (has_%(c_name)s) {
151 c17d9908 Michael Roth
''',
152 c17d9908 Michael Roth
                         c_name=c_var(argname), name=argname)
153 c17d9908 Michael Roth
            push_indent()
154 c17d9908 Michael Roth
        ret += mcgen('''
155 15e43e64 Anthony Liguori
%(visitor)s(v, &%(c_name)s, "%(name)s", errp);
156 c17d9908 Michael Roth
''',
157 15e43e64 Anthony Liguori
                     c_name=c_var(argname), name=argname, argtype=argtype,
158 15e43e64 Anthony Liguori
                     visitor=type_visitor(argtype))
159 c17d9908 Michael Roth
        if optional:
160 c17d9908 Michael Roth
            pop_indent()
161 c17d9908 Michael Roth
            ret += mcgen('''
162 c17d9908 Michael Roth
}
163 c17d9908 Michael Roth
visit_end_optional(v, errp);
164 c17d9908 Michael Roth
''')
165 c17d9908 Michael Roth
166 c17d9908 Michael Roth
    if dealloc:
167 c17d9908 Michael Roth
        ret += mcgen('''
168 c17d9908 Michael Roth
qapi_dealloc_visitor_cleanup(md);
169 c17d9908 Michael Roth
''')
170 c17d9908 Michael Roth
    else:
171 c17d9908 Michael Roth
        ret += mcgen('''
172 c17d9908 Michael Roth
qmp_input_visitor_cleanup(mi);
173 c17d9908 Michael Roth
''')
174 c17d9908 Michael Roth
    pop_indent()
175 c17d9908 Michael Roth
    return ret.rstrip()
176 c17d9908 Michael Roth
177 776574d6 Anthony Liguori
def gen_marshal_output(name, args, ret_type, middle_mode):
178 c17d9908 Michael Roth
    if not ret_type:
179 c17d9908 Michael Roth
        return ""
180 776574d6 Anthony Liguori
181 c17d9908 Michael Roth
    ret = mcgen('''
182 c17d9908 Michael Roth
static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
183 c17d9908 Michael Roth
{
184 c17d9908 Michael Roth
    QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
185 c17d9908 Michael Roth
    QmpOutputVisitor *mo = qmp_output_visitor_new();
186 c17d9908 Michael Roth
    Visitor *v;
187 c17d9908 Michael Roth

188 c17d9908 Michael Roth
    v = qmp_output_get_visitor(mo);
189 15e43e64 Anthony Liguori
    %(visitor)s(v, &ret_in, "unused", errp);
190 c17d9908 Michael Roth
    if (!error_is_set(errp)) {
191 c17d9908 Michael Roth
        *ret_out = qmp_output_get_qobject(mo);
192 c17d9908 Michael Roth
    }
193 c17d9908 Michael Roth
    qmp_output_visitor_cleanup(mo);
194 c17d9908 Michael Roth
    v = qapi_dealloc_get_visitor(md);
195 15e43e64 Anthony Liguori
    %(visitor)s(v, &ret_in, "unused", errp);
196 c17d9908 Michael Roth
    qapi_dealloc_visitor_cleanup(md);
197 c17d9908 Michael Roth
}
198 c17d9908 Michael Roth
''',
199 15e43e64 Anthony Liguori
                c_ret_type=c_type(ret_type), c_name=c_var(name),
200 15e43e64 Anthony Liguori
                visitor=type_visitor(ret_type))
201 c17d9908 Michael Roth
202 c17d9908 Michael Roth
    return ret
203 c17d9908 Michael Roth
204 776574d6 Anthony Liguori
def gen_marshal_input_decl(name, args, ret_type, middle_mode):
205 776574d6 Anthony Liguori
    if middle_mode:
206 776574d6 Anthony Liguori
        return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_var(name)
207 776574d6 Anthony Liguori
    else:
208 776574d6 Anthony Liguori
        return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_var(name)
209 776574d6 Anthony Liguori
210 776574d6 Anthony Liguori
211 776574d6 Anthony Liguori
212 776574d6 Anthony Liguori
def gen_marshal_input(name, args, ret_type, middle_mode):
213 776574d6 Anthony Liguori
    hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
214 776574d6 Anthony Liguori
215 c17d9908 Michael Roth
    ret = mcgen('''
216 776574d6 Anthony Liguori
%(header)s
217 c17d9908 Michael Roth
{
218 c17d9908 Michael Roth
''',
219 776574d6 Anthony Liguori
                header=hdr)
220 776574d6 Anthony Liguori
221 776574d6 Anthony Liguori
    if middle_mode:
222 776574d6 Anthony Liguori
        ret += mcgen('''
223 776574d6 Anthony Liguori
    Error *local_err = NULL;
224 776574d6 Anthony Liguori
    Error **errp = &local_err;
225 776574d6 Anthony Liguori
    QDict *args = (QDict *)qdict;
226 776574d6 Anthony Liguori
''')
227 c17d9908 Michael Roth
228 c17d9908 Michael Roth
    if ret_type:
229 c17d9908 Michael Roth
        if c_type(ret_type).endswith("*"):
230 c17d9908 Michael Roth
            retval = "    %s retval = NULL;" % c_type(ret_type)
231 c17d9908 Michael Roth
        else:
232 c17d9908 Michael Roth
            retval = "    %s retval;" % c_type(ret_type)
233 c17d9908 Michael Roth
        ret += mcgen('''
234 c17d9908 Michael Roth
%(retval)s
235 c17d9908 Michael Roth
''',
236 c17d9908 Michael Roth
                     retval=retval)
237 c17d9908 Michael Roth
238 c17d9908 Michael Roth
    if len(args) > 0:
239 c17d9908 Michael Roth
        ret += mcgen('''
240 c17d9908 Michael Roth
%(visitor_input_containers_decl)s
241 c17d9908 Michael Roth
%(visitor_input_vars_decl)s
242 c17d9908 Michael Roth

243 c17d9908 Michael Roth
%(visitor_input_block)s
244 c17d9908 Michael Roth

245 c17d9908 Michael Roth
''',
246 c17d9908 Michael Roth
                     visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
247 c17d9908 Michael Roth
                     visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
248 c17d9908 Michael Roth
                     visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
249 776574d6 Anthony Liguori
    else:
250 776574d6 Anthony Liguori
        ret += mcgen('''
251 776574d6 Anthony Liguori
    (void)args;
252 776574d6 Anthony Liguori
''')
253 c17d9908 Michael Roth
254 c17d9908 Michael Roth
    ret += mcgen('''
255 c17d9908 Michael Roth
    if (error_is_set(errp)) {
256 c17d9908 Michael Roth
        goto out;
257 c17d9908 Michael Roth
    }
258 c17d9908 Michael Roth
%(sync_call)s
259 c17d9908 Michael Roth
''',
260 c17d9908 Michael Roth
                 sync_call=gen_sync_call(name, args, ret_type, indent=4))
261 c17d9908 Michael Roth
    ret += mcgen('''
262 c17d9908 Michael Roth

263 c17d9908 Michael Roth
out:
264 c17d9908 Michael Roth
''')
265 c17d9908 Michael Roth
    ret += mcgen('''
266 c17d9908 Michael Roth
%(visitor_input_block_cleanup)s
267 776574d6 Anthony Liguori
''',
268 776574d6 Anthony Liguori
                 visitor_input_block_cleanup=gen_visitor_input_block(args, None,
269 776574d6 Anthony Liguori
                                                                     dealloc=True))
270 776574d6 Anthony Liguori
271 776574d6 Anthony Liguori
    if middle_mode:
272 776574d6 Anthony Liguori
        ret += mcgen('''
273 776574d6 Anthony Liguori

274 776574d6 Anthony Liguori
    if (local_err) {
275 776574d6 Anthony Liguori
        qerror_report_err(local_err);
276 776574d6 Anthony Liguori
        error_free(local_err);
277 776574d6 Anthony Liguori
        return -1;
278 776574d6 Anthony Liguori
    }
279 776574d6 Anthony Liguori
    return 0;
280 776574d6 Anthony Liguori
''')
281 776574d6 Anthony Liguori
    else:
282 776574d6 Anthony Liguori
        ret += mcgen('''
283 c17d9908 Michael Roth
    return;
284 776574d6 Anthony Liguori
''')
285 776574d6 Anthony Liguori
286 776574d6 Anthony Liguori
    ret += mcgen('''
287 c17d9908 Michael Roth
}
288 776574d6 Anthony Liguori
''')
289 776574d6 Anthony Liguori
290 c17d9908 Michael Roth
    return ret
291 c17d9908 Michael Roth
292 c17d9908 Michael Roth
def gen_registry(commands):
293 c17d9908 Michael Roth
    registry=""
294 c17d9908 Michael Roth
    push_indent()
295 c17d9908 Michael Roth
    for cmd in commands:
296 c17d9908 Michael Roth
        registry += mcgen('''
297 c17d9908 Michael Roth
qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s);
298 c17d9908 Michael Roth
''',
299 c17d9908 Michael Roth
                     name=cmd['command'], c_name=c_var(cmd['command']))
300 c17d9908 Michael Roth
    pop_indent()
301 c17d9908 Michael Roth
    ret = mcgen('''
302 c17d9908 Michael Roth
static void qmp_init_marshal(void)
303 c17d9908 Michael Roth
{
304 c17d9908 Michael Roth
%(registry)s
305 c17d9908 Michael Roth
}
306 c17d9908 Michael Roth

307 c17d9908 Michael Roth
qapi_init(qmp_init_marshal);
308 c17d9908 Michael Roth
''',
309 c17d9908 Michael Roth
                registry=registry.rstrip())
310 c17d9908 Michael Roth
    return ret
311 c17d9908 Michael Roth
312 c17d9908 Michael Roth
def gen_command_decl_prologue(header, guard, prefix=""):
313 c17d9908 Michael Roth
    ret = mcgen('''
314 c17d9908 Michael Roth
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
315 c17d9908 Michael Roth

316 c17d9908 Michael Roth
/*
317 c17d9908 Michael Roth
 * schema-defined QAPI function prototypes
318 c17d9908 Michael Roth
 *
319 c17d9908 Michael Roth
 * Copyright IBM, Corp. 2011
320 c17d9908 Michael Roth
 *
321 c17d9908 Michael Roth
 * Authors:
322 c17d9908 Michael Roth
 *  Anthony Liguori   <aliguori@us.ibm.com>
323 c17d9908 Michael Roth
 *
324 c17d9908 Michael Roth
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
325 c17d9908 Michael Roth
 * See the COPYING.LIB file in the top-level directory.
326 c17d9908 Michael Roth
 *
327 c17d9908 Michael Roth
 */
328 c17d9908 Michael Roth

329 c17d9908 Michael Roth
#ifndef %(guard)s
330 c17d9908 Michael Roth
#define %(guard)s
331 c17d9908 Michael Roth

332 c17d9908 Michael Roth
#include "%(prefix)sqapi-types.h"
333 c17d9908 Michael Roth
#include "error.h"
334 c17d9908 Michael Roth

335 c17d9908 Michael Roth
''',
336 776574d6 Anthony Liguori
                 header=basename(header), guard=guardname(header), prefix=prefix)
337 c17d9908 Michael Roth
    return ret
338 c17d9908 Michael Roth
339 c17d9908 Michael Roth
def gen_command_def_prologue(prefix="", proxy=False):
340 c17d9908 Michael Roth
    ret = mcgen('''
341 c17d9908 Michael Roth
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
342 c17d9908 Michael Roth

343 c17d9908 Michael Roth
/*
344 c17d9908 Michael Roth
 * schema-defined QMP->QAPI command dispatch
345 c17d9908 Michael Roth
 *
346 c17d9908 Michael Roth
 * Copyright IBM, Corp. 2011
347 c17d9908 Michael Roth
 *
348 c17d9908 Michael Roth
 * Authors:
349 c17d9908 Michael Roth
 *  Anthony Liguori   <aliguori@us.ibm.com>
350 c17d9908 Michael Roth
 *
351 c17d9908 Michael Roth
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
352 c17d9908 Michael Roth
 * See the COPYING.LIB file in the top-level directory.
353 c17d9908 Michael Roth
 *
354 c17d9908 Michael Roth
 */
355 c17d9908 Michael Roth

356 c17d9908 Michael Roth
#include "qemu-objects.h"
357 c17d9908 Michael Roth
#include "qapi/qmp-core.h"
358 c17d9908 Michael Roth
#include "qapi/qapi-visit-core.h"
359 c17d9908 Michael Roth
#include "qapi/qmp-output-visitor.h"
360 c17d9908 Michael Roth
#include "qapi/qmp-input-visitor.h"
361 c17d9908 Michael Roth
#include "qapi/qapi-dealloc-visitor.h"
362 c17d9908 Michael Roth
#include "%(prefix)sqapi-types.h"
363 c17d9908 Michael Roth
#include "%(prefix)sqapi-visit.h"
364 c17d9908 Michael Roth

365 c17d9908 Michael Roth
''',
366 c17d9908 Michael Roth
                prefix=prefix)
367 c17d9908 Michael Roth
    if not proxy:
368 c17d9908 Michael Roth
        ret += '#include "%sqmp-commands.h"' % prefix
369 776574d6 Anthony Liguori
    return ret + "\n\n"
370 c17d9908 Michael Roth
371 c17d9908 Michael Roth
372 c17d9908 Michael Roth
try:
373 776574d6 Anthony Liguori
    opts, args = getopt.gnu_getopt(sys.argv[1:], "p:o:m", ["prefix=", "output-dir=", "type=", "middle"])
374 c17d9908 Michael Roth
except getopt.GetoptError, err:
375 c17d9908 Michael Roth
    print str(err)
376 c17d9908 Michael Roth
    sys.exit(1)
377 c17d9908 Michael Roth
378 c17d9908 Michael Roth
output_dir = ""
379 c17d9908 Michael Roth
prefix = ""
380 c17d9908 Michael Roth
dispatch_type = "sync"
381 c17d9908 Michael Roth
c_file = 'qmp-marshal.c'
382 c17d9908 Michael Roth
h_file = 'qmp-commands.h'
383 776574d6 Anthony Liguori
middle_mode = False
384 c17d9908 Michael Roth
385 c17d9908 Michael Roth
for o, a in opts:
386 c17d9908 Michael Roth
    if o in ("-p", "--prefix"):
387 c17d9908 Michael Roth
        prefix = a
388 c17d9908 Michael Roth
    elif o in ("-o", "--output-dir"):
389 c17d9908 Michael Roth
        output_dir = a + "/"
390 c17d9908 Michael Roth
    elif o in ("-t", "--type"):
391 c17d9908 Michael Roth
        dispatch_type = a
392 776574d6 Anthony Liguori
    elif o in ("-m", "--middle"):
393 776574d6 Anthony Liguori
        middle_mode = True
394 c17d9908 Michael Roth
395 c17d9908 Michael Roth
c_file = output_dir + prefix + c_file
396 c17d9908 Michael Roth
h_file = output_dir + prefix + h_file
397 c17d9908 Michael Roth
398 c17d9908 Michael Roth
try:
399 c17d9908 Michael Roth
    os.makedirs(output_dir)
400 c17d9908 Michael Roth
except os.error, e:
401 c17d9908 Michael Roth
    if e.errno != errno.EEXIST:
402 c17d9908 Michael Roth
        raise
403 c17d9908 Michael Roth
404 c17d9908 Michael Roth
exprs = parse_schema(sys.stdin)
405 c17d9908 Michael Roth
commands = filter(lambda expr: expr.has_key('command'), exprs)
406 c17d9908 Michael Roth
407 c17d9908 Michael Roth
if dispatch_type == "sync":
408 c17d9908 Michael Roth
    fdecl = open(h_file, 'w')
409 c17d9908 Michael Roth
    fdef = open(c_file, 'w')
410 c17d9908 Michael Roth
    ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
411 c17d9908 Michael Roth
    fdecl.write(ret)
412 c17d9908 Michael Roth
    ret = gen_command_def_prologue(prefix=prefix)
413 c17d9908 Michael Roth
    fdef.write(ret)
414 c17d9908 Michael Roth
415 c17d9908 Michael Roth
    for cmd in commands:
416 c17d9908 Michael Roth
        arglist = []
417 c17d9908 Michael Roth
        ret_type = None
418 c17d9908 Michael Roth
        if cmd.has_key('data'):
419 c17d9908 Michael Roth
            arglist = cmd['data']
420 c17d9908 Michael Roth
        if cmd.has_key('returns'):
421 c17d9908 Michael Roth
            ret_type = cmd['returns']
422 c17d9908 Michael Roth
        ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
423 c17d9908 Michael Roth
        fdecl.write(ret)
424 c17d9908 Michael Roth
        if ret_type:
425 776574d6 Anthony Liguori
            ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
426 c17d9908 Michael Roth
            fdef.write(ret)
427 776574d6 Anthony Liguori
428 776574d6 Anthony Liguori
        if middle_mode:
429 776574d6 Anthony Liguori
            fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
430 776574d6 Anthony Liguori
431 776574d6 Anthony Liguori
        ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
432 c17d9908 Michael Roth
        fdef.write(ret)
433 c17d9908 Michael Roth
434 7534ba01 Michael Roth
    fdecl.write("\n#endif\n");
435 776574d6 Anthony Liguori
436 776574d6 Anthony Liguori
    if not middle_mode:
437 776574d6 Anthony Liguori
        ret = gen_registry(commands)
438 776574d6 Anthony Liguori
        fdef.write(ret)
439 c17d9908 Michael Roth
440 c17d9908 Michael Roth
    fdef.flush()
441 c17d9908 Michael Roth
    fdef.close()
442 c17d9908 Michael Roth
    fdecl.flush()
443 c17d9908 Michael Roth
    fdecl.close()