1910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#
2910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner# QAPI command marshaller generator
3910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#
4910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner# Copyright IBM, Corp. 2011
5910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#
6910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner# Authors:
7910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#  Anthony Liguori <aliguori@us.ibm.com>
8910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#  Michael Roth    <mdroth@linux.vnet.ibm.com>
9910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#
10910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner# This work is licensed under the terms of the GNU GPLv2.
11910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner# See the COPYING.LIB file in the top-level directory.
12910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
13910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerfrom ordereddict import OrderedDict
14910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerfrom qapi import *
15910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerimport sys
16910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerimport os
17910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerimport getopt
18910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerimport errno
19910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
20910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef type_visitor(name):
21910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if type(name) == list:
22910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return 'visit_type_%sList' % name[0]
23910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
24910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return 'visit_type_%s' % name
25910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
26910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef generate_decl_enum(name, members, genlist=True):
27910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return mcgen('''
28910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
29910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnervoid %(visitor)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
30910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
31910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                 visitor=type_visitor(name))
32910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
33910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef generate_command_decl(name, args, ret_type):
34910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    arglist=""
35910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    for argname, argtype, optional, structured in parse_args(args):
36910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        argtype = c_type(argtype)
37910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if argtype == "char *":
38910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            argtype = "const char *"
39910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if optional:
40910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            arglist += "bool has_%s, " % c_var(argname)
41910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        arglist += "%s %s, " % (argtype, c_var(argname))
42910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return mcgen('''
43910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(ret_type)s qmp_%(name)s(%(args)sError **errp);
44910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
45910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
46910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
47910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_sync_call(name, args, ret_type, indent=0):
48910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = ""
49910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    arglist=""
50910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    retval=""
51910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if ret_type:
52910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        retval = "retval = "
53910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    for argname, argtype, optional, structured in parse_args(args):
54910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if optional:
55910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            arglist += "has_%s, " % c_var(argname)
56910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        arglist += "%s, " % (c_var(argname))
57910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    push_indent(indent)
58910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = mcgen('''
59910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(retval)sqmp_%(name)s(%(args)serrp);
60910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
61910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
62910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                name=c_fun(name), args=arglist, retval=retval).rstrip()
63910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if ret_type:
64910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += "\n" + mcgen(''''
65910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerif (!error_is_set(errp)) {
66910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    %(marshal_output_call)s
67910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner}
68910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
69910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                            marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
70910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    pop_indent(indent)
71910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret.rstrip()
72910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
73910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
74910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_marshal_output_call(name, ret_type):
75910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if not ret_type:
76910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return ""
77910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name)
78910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
79910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_visitor_output_containers_decl(ret_type):
80910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = ""
81910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    push_indent()
82910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if ret_type:
83910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
84910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' TurnerQmpOutputVisitor *mo;
85910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' TurnerQapiDeallocVisitor *md;
86910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' TurnerVisitor *v;
87910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
88910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    pop_indent()
89910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
90910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret
91910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
92910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_visitor_input_containers_decl(args):
93910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = ""
94910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
95910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    push_indent()
96910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if len(args) > 0:
97910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
98910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' TurnerQmpInputVisitor *mi;
99910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' TurnerQapiDeallocVisitor *md;
100910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' TurnerVisitor *v;
101910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
102910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    pop_indent()
103910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
104910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret.rstrip()
105910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
106910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_visitor_input_vars_decl(args):
107910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = ""
108910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    push_indent()
109910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    for argname, argtype, optional, structured in parse_args(args):
110910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if optional:
111910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret += mcgen('''
112910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerbool has_%(argname)s = false;
113910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
114910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                         argname=c_var(argname))
115910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if c_type(argtype).endswith("*"):
116910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret += mcgen('''
117910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(argtype)s %(argname)s = NULL;
118910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
119910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                         argname=c_var(argname), argtype=c_type(argtype))
120910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        else:
121910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret += mcgen('''
122910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(argtype)s %(argname)s;
123910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
124910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                         argname=c_var(argname), argtype=c_type(argtype))
125910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
126910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    pop_indent()
127910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret.rstrip()
128910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
129910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_visitor_input_block(args, obj, dealloc=False):
130910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = ""
131910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    errparg = 'errp'
132910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
133910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if len(args) == 0:
134910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return ret
135910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
136910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    push_indent()
137910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
138910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if dealloc:
139910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        errparg = 'NULL'
140910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
141910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnermd = qapi_dealloc_visitor_new();
142910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerv = qapi_dealloc_get_visitor(md);
143910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
144910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
145910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
146910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnermi = qmp_input_visitor_new_strict(%(obj)s);
147910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerv = qmp_input_get_visitor(mi);
148910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
149910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     obj=obj)
150910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
151910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    for argname, argtype, optional, structured in parse_args(args):
152910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if optional:
153910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret += mcgen('''
154910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnervisit_start_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
155910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerif (has_%(c_name)s) {
156910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
157910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                         c_name=c_var(argname), name=argname, errp=errparg)
158910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            push_indent()
159910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
160910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
161910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
162910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     c_name=c_var(argname), name=argname, argtype=argtype,
163910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     visitor=type_visitor(argtype), errp=errparg)
164910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if optional:
165910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            pop_indent()
166910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret += mcgen('''
167910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner}
168910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnervisit_end_optional(v, %(errp)s);
169910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''', errp=errparg)
170910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
171910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if dealloc:
172910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
173910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerqapi_dealloc_visitor_cleanup(md);
174910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
175910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
176910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
177910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerqmp_input_visitor_cleanup(mi);
178910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
179910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    pop_indent()
180910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret.rstrip()
181910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
182910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_marshal_output(name, args, ret_type, middle_mode):
183910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if not ret_type:
184910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return ""
185910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
186910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = mcgen('''
187910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerstatic void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
188910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner{
189910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
190910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    QmpOutputVisitor *mo = qmp_output_visitor_new();
191910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    Visitor *v;
192910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
193910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    v = qmp_output_get_visitor(mo);
194910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    %(visitor)s(v, &ret_in, "unused", errp);
195910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if (!error_is_set(errp)) {
196910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        *ret_out = qmp_output_get_qobject(mo);
197910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    }
198910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    qmp_output_visitor_cleanup(mo);
199910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    v = qapi_dealloc_get_visitor(md);
200910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    %(visitor)s(v, &ret_in, "unused", NULL);
201910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    qapi_dealloc_visitor_cleanup(md);
202910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner}
203910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
204910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                c_ret_type=c_type(ret_type), c_name=c_fun(name),
205910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                visitor=type_visitor(ret_type))
206910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
207910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret
208910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
209910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_marshal_input_decl(name, args, ret_type, middle_mode):
210910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if middle_mode:
211910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
212910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
213910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
214910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
215910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
216910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
217910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_marshal_input(name, args, ret_type, middle_mode):
218910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
219910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
220910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = mcgen('''
221910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(header)s
222910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner{
223910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
224910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                header=hdr)
225910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
226910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if middle_mode:
227910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
228910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    Error *local_err = NULL;
229910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    Error **errp = &local_err;
230910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    QDict *args = (QDict *)qdict;
231910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
232910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
233910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if ret_type:
234910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if c_type(ret_type).endswith("*"):
235910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            retval = "    %s retval = NULL;" % c_type(ret_type)
236910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        else:
237910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            retval = "    %s retval;" % c_type(ret_type)
238910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
239910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(retval)s
240910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
241910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     retval=retval)
242910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
243910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if len(args) > 0:
244910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
245910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(visitor_input_containers_decl)s
246910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(visitor_input_vars_decl)s
247910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
248910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(visitor_input_block)s
249910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
250910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
251910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
252910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
253910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
254910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
255910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
256910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    (void)args;
257910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
258910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
259910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret += mcgen('''
260910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if (error_is_set(errp)) {
261910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        goto out;
262910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    }
263910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(sync_call)s
264910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
265910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                 sync_call=gen_sync_call(name, args, ret_type, indent=4))
266910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret += mcgen('''
267910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
268910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerout:
269910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
270910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret += mcgen('''
271910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(visitor_input_block_cleanup)s
272910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
273910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                 visitor_input_block_cleanup=gen_visitor_input_block(args, None,
274910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                                                                     dealloc=True))
275910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
276910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if middle_mode:
277910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
278910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
279910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if (local_err) {
280910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        qerror_report_err(local_err);
281910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        error_free(local_err);
282910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return -1;
283910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    }
284910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return 0;
285910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
286910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
287910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += mcgen('''
288910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return;
289910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
290910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
291910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret += mcgen('''
292910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner}
293910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''')
294910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
295910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret
296910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
297910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef option_value_matches(opt, val, cmd):
298910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if opt in cmd and cmd[opt] == val:
299910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return True
300910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return False
301910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
302910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_registry(commands):
303910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    registry=""
304910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    push_indent()
305910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    for cmd in commands:
306910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        options = 'QCO_NO_OPTIONS'
307910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if option_value_matches('success-response', 'no', cmd):
308910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            options = 'QCO_NO_SUCCESS_RESP'
309910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
310910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        registry += mcgen('''
311910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerqmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
312910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
313910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     name=cmd['command'], c_name=c_fun(cmd['command']),
314910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                     opts=options)
315910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    pop_indent()
316910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = mcgen('''
317910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerstatic void qmp_init_marshal(void)
318910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner{
319910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner%(registry)s
320910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner}
321910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
322910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerqapi_init(qmp_init_marshal);
323910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
324910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                registry=registry.rstrip())
325910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret
326910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
327910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_command_decl_prologue(header, guard, prefix=""):
328910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = mcgen('''
329910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
330910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
331910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner/*
332910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * schema-defined QAPI function prototypes
333910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
334910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * Copyright IBM, Corp. 2011
335910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
336910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * Authors:
337910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *  Anthony Liguori   <aliguori@us.ibm.com>
338910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
339910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
340910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * See the COPYING.LIB file in the top-level directory.
341910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
342910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner */
343910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
344910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#ifndef %(guard)s
345910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#define %(guard)s
346910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
347910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "%(prefix)sqapi-types.h"
348910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/qmp/qdict.h"
349910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/error.h"
350910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
351910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
352910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                 header=basename(header), guard=guardname(header), prefix=prefix)
353910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret
354910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
355910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef gen_command_def_prologue(prefix="", proxy=False):
356910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = mcgen('''
357910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
358910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
359910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner/*
360910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * schema-defined QMP->QAPI command dispatch
361910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
362910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * Copyright IBM, Corp. 2011
363910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
364910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * Authors:
365910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *  Anthony Liguori   <aliguori@us.ibm.com>
366910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
367910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
368910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner * See the COPYING.LIB file in the top-level directory.
369910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner *
370910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner */
371910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
372910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qemu-common.h"
373910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qemu/module.h"
374910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/qmp/qerror.h"
375910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/qmp/types.h"
376910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/qmp/dispatch.h"
377910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/visitor.h"
378910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/qmp-output-visitor.h"
379910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/qmp-input-visitor.h"
380910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "qapi/dealloc-visitor.h"
381910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "%(prefix)sqapi-types.h"
382910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner#include "%(prefix)sqapi-visit.h"
383910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
384910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner''',
385910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                prefix=prefix)
386910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if not proxy:
387910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret += '#include "%sqmp-commands.h"' % prefix
388910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    return ret + "\n\n"
389910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
390910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
391910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnertry:
392910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:m",
393910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                                   ["source", "header", "prefix=",
394910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner                                    "output-dir=", "type=", "middle"])
395910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerexcept getopt.GetoptError, err:
396910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    print str(err)
397910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    sys.exit(1)
398910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
399910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turneroutput_dir = ""
400910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerprefix = ""
401910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdispatch_type = "sync"
402910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerc_file = 'qmp-marshal.c'
403910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerh_file = 'qmp-commands.h'
404910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnermiddle_mode = False
405910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
406910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdo_c = False
407910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdo_h = False
408910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
409910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerfor o, a in opts:
410910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if o in ("-p", "--prefix"):
411910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        prefix = a
412910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    elif o in ("-o", "--output-dir"):
413910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        output_dir = a + "/"
414910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    elif o in ("-t", "--type"):
415910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        dispatch_type = a
416910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    elif o in ("-m", "--middle"):
417910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        middle_mode = True
418910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    elif o in ("-c", "--source"):
419910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        do_c = True
420910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    elif o in ("-h", "--header"):
421910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        do_h = True
422910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
423910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerif not do_c and not do_h:
424910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    do_c = True
425910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    do_h = True
426910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
427910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerc_file = output_dir + prefix + c_file
428910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerh_file = output_dir + prefix + h_file
429910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
430910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerdef maybe_open(really, name, opt):
431910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if really:
432910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return open(name, opt)
433910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    else:
434910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        import StringIO
435910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        return StringIO.StringIO()
436910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
437910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnertry:
438910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    os.makedirs(output_dir)
439910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerexcept os.error, e:
440910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if e.errno != errno.EEXIST:
441910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        raise
442910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
443910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerexprs = parse_schema(sys.stdin)
444910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnercommands = filter(lambda expr: expr.has_key('command'), exprs)
445910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnercommands = filter(lambda expr: not expr.has_key('gen'), commands)
446910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
447910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turnerif dispatch_type == "sync":
448910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdecl = maybe_open(do_h, h_file, 'w')
449910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdef = maybe_open(do_c, c_file, 'w')
450910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
451910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdecl.write(ret)
452910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    ret = gen_command_def_prologue(prefix=prefix)
453910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdef.write(ret)
454910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
455910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    for cmd in commands:
456910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        arglist = []
457910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret_type = None
458910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if cmd.has_key('data'):
459910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            arglist = cmd['data']
460910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if cmd.has_key('returns'):
461910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret_type = cmd['returns']
462910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
463910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        fdecl.write(ret)
464910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if ret_type:
465910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
466910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            fdef.write(ret)
467910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
468910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        if middle_mode:
469910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner            fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
470910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
471910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
472910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        fdef.write(ret)
473910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
474910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdecl.write("\n#endif\n");
475910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
476910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    if not middle_mode:
477910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        ret = gen_registry(commands)
478910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner        fdef.write(ret)
479910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner
480910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdef.flush()
481910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdef.close()
482910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdecl.flush()
483910aea96b67d7f0357f586c47f20848ec435aa1bDavid 'Digit' Turner    fdecl.close()
484