tap: Permit incremental conversion of tap_open() to Error
[qemu.git] / scripts / qapi-event.py
blob56bc602a6d8892a83579e5c1d7664a84bc4a3513
2 # QAPI event generator
4 # Copyright (c) 2014 Wenchao Xia
6 # Authors:
7 # Wenchao Xia <wenchaoqemu@gmail.com>
9 # This work is licensed under the terms of the GNU GPL, version 2.
10 # See the COPYING file in the top-level directory.
12 from ordereddict import OrderedDict
13 from qapi import *
15 def _generate_event_api_name(event_name, params):
16 api_name = "void qapi_event_send_%s(" % c_name(event_name).lower();
17 l = len(api_name)
19 if params:
20 for argname, argentry, optional in parse_args(params):
21 if optional:
22 api_name += "bool has_%s,\n" % c_name(argname)
23 api_name += "".ljust(l)
25 api_name += "%s %s,\n" % (c_type(argentry, is_param=True),
26 c_name(argname))
27 api_name += "".ljust(l)
29 api_name += "Error **errp)"
30 return api_name;
33 # Following are the core functions that generate C APIs to emit event.
35 def generate_event_declaration(api_name):
36 return mcgen('''
38 %(api_name)s;
39 ''',
40 api_name = api_name)
42 def generate_event_implement(api_name, event_name, params):
43 # step 1: declare any variables
44 ret = mcgen("""
46 %(api_name)s
48 QDict *qmp;
49 Error *local_err = NULL;
50 QMPEventFuncEmit emit;
51 """,
52 api_name = api_name)
54 if params:
55 ret += mcgen("""
56 QmpOutputVisitor *qov;
57 Visitor *v;
58 QObject *obj;
60 """)
62 # step 2: check emit function, create a dict
63 ret += mcgen("""
64 emit = qmp_event_get_func_emit();
65 if (!emit) {
66 return;
69 qmp = qmp_event_build_dict("%(event_name)s");
71 """,
72 event_name = event_name)
74 # step 3: visit the params if params != None
75 if params:
76 ret += mcgen("""
77 qov = qmp_output_visitor_new();
78 g_assert(qov);
80 v = qmp_output_get_visitor(qov);
81 g_assert(v);
83 /* Fake visit, as if all members are under a structure */
84 visit_start_struct(v, NULL, "", "%(event_name)s", 0, &local_err);
85 if (local_err) {
86 goto clean;
89 """,
90 event_name = event_name)
92 for argname, argentry, optional in parse_args(params):
93 if optional:
94 ret += mcgen("""
95 if (has_%(var)s) {
96 """,
97 var = c_name(argname))
98 push_indent()
100 if argentry == "str":
101 var_type = "(char **)"
102 else:
103 var_type = ""
105 ret += mcgen("""
106 visit_type_%(type)s(v, %(var_type)s&%(var)s, "%(name)s", &local_err);
107 if (local_err) {
108 goto clean;
110 """,
111 var_type = var_type,
112 var = c_name(argname),
113 type = type_name(argentry),
114 name = argname)
116 if optional:
117 pop_indent()
118 ret += mcgen("""
120 """)
122 ret += mcgen("""
124 visit_end_struct(v, &local_err);
125 if (local_err) {
126 goto clean;
129 obj = qmp_output_get_qobject(qov);
130 g_assert(obj != NULL);
132 qdict_put_obj(qmp, "data", obj);
133 """)
135 # step 4: call qmp event api
136 ret += mcgen("""
137 emit(%(event_enum_value)s, qmp, &local_err);
139 """,
140 event_enum_value = event_enum_value)
142 # step 5: clean up
143 if params:
144 ret += mcgen("""
145 clean:
146 qmp_output_visitor_cleanup(qov);
147 """)
148 ret += mcgen("""
149 error_propagate(errp, local_err);
150 QDECREF(qmp);
152 """)
154 return ret
157 # Following are the functions that generate an enum type for all defined
158 # events, similar to qapi-types.py. Here we already have enum name and
159 # values which were generated before and recorded in event_enum_*. It also
160 # works around the issue that "import qapi-types" can't work.
162 def generate_event_enum_decl(event_enum_name, event_enum_values):
163 lookup_decl = mcgen('''
165 extern const char *%(event_enum_name)s_lookup[];
166 ''',
167 event_enum_name = event_enum_name)
169 enum_decl = mcgen('''
170 typedef enum %(event_enum_name)s
172 ''',
173 event_enum_name = event_enum_name)
175 # append automatically generated _MAX value
176 enum_max_value = c_enum_const(event_enum_name, "MAX")
177 enum_values = event_enum_values + [ enum_max_value ]
179 i = 0
180 for value in enum_values:
181 enum_decl += mcgen('''
182 %(value)s = %(i)d,
183 ''',
184 value = value,
185 i = i)
186 i += 1
188 enum_decl += mcgen('''
189 } %(event_enum_name)s;
190 ''',
191 event_enum_name = event_enum_name)
193 return lookup_decl + enum_decl
195 def generate_event_enum_lookup(event_enum_name, event_enum_strings):
196 ret = mcgen('''
198 const char *%(event_enum_name)s_lookup[] = {
199 ''',
200 event_enum_name = event_enum_name)
202 i = 0
203 for string in event_enum_strings:
204 ret += mcgen('''
205 "%(string)s",
206 ''',
207 string = string)
209 ret += mcgen('''
210 NULL,
212 ''')
213 return ret
215 (input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
217 c_comment = '''
219 * schema-defined QAPI event functions
221 * Copyright (c) 2014 Wenchao Xia
223 * Authors:
224 * Wenchao Xia <wenchaoqemu@gmail.com>
226 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
227 * See the COPYING.LIB file in the top-level directory.
231 h_comment = '''
233 * schema-defined QAPI event functions
235 * Copyright (c) 2014 Wenchao Xia
237 * Authors:
238 * Wenchao Xia <wenchaoqemu@gmail.com>
240 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
241 * See the COPYING.LIB file in the top-level directory.
246 (fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
247 'qapi-event.c', 'qapi-event.h',
248 c_comment, h_comment)
250 fdef.write(mcgen('''
251 #include "qemu-common.h"
252 #include "%(prefix)sqapi-event.h"
253 #include "%(prefix)sqapi-visit.h"
254 #include "qapi/qmp-output-visitor.h"
255 #include "qapi/qmp-event.h"
257 ''',
258 prefix=prefix))
260 fdecl.write(mcgen('''
261 #include "qapi/error.h"
262 #include "qapi/qmp/qdict.h"
263 #include "%(prefix)sqapi-types.h"
265 ''',
266 prefix=prefix))
268 exprs = parse_schema(input_file)
270 event_enum_name = prefix.upper().replace('-', '_') + "QAPIEvent"
271 event_enum_values = []
272 event_enum_strings = []
274 for expr in exprs:
275 if expr.has_key('event'):
276 event_name = expr['event']
277 params = expr.get('data')
278 if params and len(params) == 0:
279 params = None
281 api_name = _generate_event_api_name(event_name, params)
282 ret = generate_event_declaration(api_name)
283 fdecl.write(ret)
285 # We need an enum value per event
286 event_enum_value = c_enum_const(event_enum_name, event_name)
287 ret = generate_event_implement(api_name, event_name, params)
288 fdef.write(ret)
290 # Record it, and generate enum later
291 event_enum_values.append(event_enum_value)
292 event_enum_strings.append(event_name)
294 ret = generate_event_enum_decl(event_enum_name, event_enum_values)
295 fdecl.write(ret)
296 ret = generate_event_enum_lookup(event_enum_name, event_enum_strings)
297 fdef.write(ret)
299 close_output(fdef, fdecl)