qapi: Consolidate QMP input visitor creation
[qemu/kevin.git] / qapi / qmp-input-visitor.c
blobc3c3271b1be632fe7b0ed0df1f2a7b816024d332
1 /*
2 * Input Visitor
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp-input-visitor.h"
18 #include "qapi/visitor-impl.h"
19 #include "qemu/queue.h"
20 #include "qemu-common.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qerror.h"
24 #define QIV_STACK_SIZE 1024
26 typedef struct StackObject
28 QObject *obj; /* Object being visited */
30 GHashTable *h; /* If obj is dict: unvisited keys */
31 const QListEntry *entry; /* If obj is list: unvisited tail */
32 } StackObject;
34 struct QmpInputVisitor
36 Visitor visitor;
38 /* Stack of objects being visited. stack[0] is root of visit,
39 * stack[1..] records the nesting of start_struct()/end_struct()
40 * and start_list()/end_list() pairs. */
41 StackObject stack[QIV_STACK_SIZE];
42 int nb_stack;
44 /* True to reject parse in visit_end_struct() if unvisited keys remain. */
45 bool strict;
48 static QmpInputVisitor *to_qiv(Visitor *v)
50 return container_of(v, QmpInputVisitor, visitor);
53 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
54 const char *name,
55 bool consume)
57 StackObject *tos = &qiv->stack[qiv->nb_stack - 1];
58 QObject *qobj = tos->obj;
60 assert(qobj);
62 /* If we have a name, and we're in a dictionary, then return that
63 * value. */
64 if (name && qobject_type(qobj) == QTYPE_QDICT) {
65 if (tos->h && consume) {
66 g_hash_table_remove(tos->h, name);
68 return qdict_get(qobject_to_qdict(qobj), name);
71 /* If we are in the middle of a list, then return the next element
72 * of the list. */
73 if (tos->entry) {
74 assert(qobject_type(qobj) == QTYPE_QLIST);
75 return qlist_entry_obj(tos->entry);
78 /* Otherwise, we are at the root of the visit or the start of a
79 * list, and return the object as-is. */
80 return qobj;
83 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
85 GHashTable *h = opaque;
86 g_hash_table_insert(h, (gpointer) key, NULL);
89 static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
91 GHashTable *h;
92 StackObject *tos = &qiv->stack[qiv->nb_stack];
94 assert(obj);
95 if (qiv->nb_stack >= QIV_STACK_SIZE) {
96 error_setg(errp, "An internal buffer overran");
97 return;
100 tos->obj = obj;
101 tos->entry = NULL;
102 tos->h = NULL;
104 if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) {
105 h = g_hash_table_new(g_str_hash, g_str_equal);
106 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
107 tos->h = h;
110 qiv->nb_stack++;
114 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
116 assert(qiv->nb_stack > 0);
118 if (qiv->strict) {
119 GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
120 if (top_ht) {
121 GHashTableIter iter;
122 const char *key;
124 g_hash_table_iter_init(&iter, top_ht);
125 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
126 error_setg(errp, QERR_QMP_EXTRA_MEMBER, key);
128 g_hash_table_unref(top_ht);
132 qiv->nb_stack--;
135 static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
136 size_t size, Error **errp)
138 QmpInputVisitor *qiv = to_qiv(v);
139 QObject *qobj = qmp_input_get_object(qiv, name, true);
140 Error *err = NULL;
142 if (obj) {
143 *obj = NULL;
145 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
146 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
147 "QDict");
148 return;
151 qmp_input_push(qiv, qobj, &err);
152 if (err) {
153 error_propagate(errp, err);
154 return;
157 if (obj) {
158 *obj = g_malloc0(size);
162 static void qmp_input_end_struct(Visitor *v, Error **errp)
164 QmpInputVisitor *qiv = to_qiv(v);
166 qmp_input_pop(qiv, errp);
169 static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
171 QmpInputVisitor *qiv = to_qiv(v);
172 QObject *qobj = qmp_input_get_object(qiv, name, true);
174 if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
175 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
176 "list");
177 return;
180 qmp_input_push(qiv, qobj, errp);
183 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
184 size_t size)
186 QmpInputVisitor *qiv = to_qiv(v);
187 GenericList *entry;
188 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
189 bool first;
191 if (so->entry == NULL) {
192 so->entry = qlist_first(qobject_to_qlist(so->obj));
193 first = true;
194 } else {
195 so->entry = qlist_next(so->entry);
196 first = false;
199 if (so->entry == NULL) {
200 return NULL;
203 entry = g_malloc0(size);
204 if (first) {
205 *list = entry;
206 } else {
207 (*list)->next = entry;
210 return entry;
213 static void qmp_input_end_list(Visitor *v)
215 QmpInputVisitor *qiv = to_qiv(v);
217 qmp_input_pop(qiv, &error_abort);
220 static void qmp_input_start_alternate(Visitor *v, const char *name,
221 GenericAlternate **obj, size_t size,
222 bool promote_int, Error **errp)
224 QmpInputVisitor *qiv = to_qiv(v);
225 QObject *qobj = qmp_input_get_object(qiv, name, false);
227 if (!qobj) {
228 *obj = NULL;
229 error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
230 return;
232 *obj = g_malloc0(size);
233 (*obj)->type = qobject_type(qobj);
234 if (promote_int && (*obj)->type == QTYPE_QINT) {
235 (*obj)->type = QTYPE_QFLOAT;
239 static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
240 Error **errp)
242 QmpInputVisitor *qiv = to_qiv(v);
243 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
245 if (!qint) {
246 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
247 "integer");
248 return;
251 *obj = qint_get_int(qint);
254 static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
255 Error **errp)
257 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
258 QmpInputVisitor *qiv = to_qiv(v);
259 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
261 if (!qint) {
262 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
263 "integer");
264 return;
267 *obj = qint_get_int(qint);
270 static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
271 Error **errp)
273 QmpInputVisitor *qiv = to_qiv(v);
274 QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
276 if (!qbool) {
277 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
278 "boolean");
279 return;
282 *obj = qbool_get_bool(qbool);
285 static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
286 Error **errp)
288 QmpInputVisitor *qiv = to_qiv(v);
289 QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
291 if (!qstr) {
292 *obj = NULL;
293 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
294 "string");
295 return;
298 *obj = g_strdup(qstring_get_str(qstr));
301 static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
302 Error **errp)
304 QmpInputVisitor *qiv = to_qiv(v);
305 QObject *qobj = qmp_input_get_object(qiv, name, true);
306 QInt *qint;
307 QFloat *qfloat;
309 qint = qobject_to_qint(qobj);
310 if (qint) {
311 *obj = qint_get_int(qobject_to_qint(qobj));
312 return;
315 qfloat = qobject_to_qfloat(qobj);
316 if (qfloat) {
317 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
318 return;
321 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
322 "number");
325 static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
326 Error **errp)
328 QmpInputVisitor *qiv = to_qiv(v);
329 QObject *qobj = qmp_input_get_object(qiv, name, true);
331 qobject_incref(qobj);
332 *obj = qobj;
335 static void qmp_input_optional(Visitor *v, const char *name, bool *present)
337 QmpInputVisitor *qiv = to_qiv(v);
338 QObject *qobj = qmp_input_get_object(qiv, name, true);
340 if (!qobj) {
341 *present = false;
342 return;
345 *present = true;
348 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
350 return &v->visitor;
353 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
355 qobject_decref(v->stack[0].obj);
356 g_free(v);
359 QmpInputVisitor *qmp_input_visitor_new(QObject *obj, bool strict)
361 QmpInputVisitor *v;
363 v = g_malloc0(sizeof(*v));
365 v->visitor.type = VISITOR_INPUT;
366 v->visitor.start_struct = qmp_input_start_struct;
367 v->visitor.end_struct = qmp_input_end_struct;
368 v->visitor.start_list = qmp_input_start_list;
369 v->visitor.next_list = qmp_input_next_list;
370 v->visitor.end_list = qmp_input_end_list;
371 v->visitor.start_alternate = qmp_input_start_alternate;
372 v->visitor.type_int64 = qmp_input_type_int64;
373 v->visitor.type_uint64 = qmp_input_type_uint64;
374 v->visitor.type_bool = qmp_input_type_bool;
375 v->visitor.type_str = qmp_input_type_str;
376 v->visitor.type_number = qmp_input_type_number;
377 v->visitor.type_any = qmp_input_type_any;
378 v->visitor.optional = qmp_input_optional;
379 v->strict = strict;
381 qmp_input_push(v, obj, NULL);
382 qobject_incref(obj);
384 return v;