4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
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/qobject-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 typedef struct StackObject
26 QObject *obj; /* Object being visited */
27 void *qapi; /* sanity check that caller uses same pointer */
29 GHashTable *h; /* If obj is dict: unvisited keys */
30 const QListEntry *entry; /* If obj is list: unvisited tail */
32 QSLIST_ENTRY(StackObject) node;
35 struct QObjectInputVisitor
39 /* Root of visit at visitor creation. */
42 /* Stack of objects being visited (all entries will be either
44 QSLIST_HEAD(, StackObject) stack;
46 /* True to reject parse in visit_end_struct() if unvisited keys remain. */
50 static QObjectInputVisitor *to_qiv(Visitor *v)
52 return container_of(v, QObjectInputVisitor, visitor);
55 static QObject *qobject_input_get_object(QObjectInputVisitor *qiv,
57 bool consume, Error **errp)
63 if (QSLIST_EMPTY(&qiv->stack)) {
64 /* Starting at root, name is ignored. */
69 /* We are in a container; find the next element. */
70 tos = QSLIST_FIRST(&qiv->stack);
74 if (qobject_type(qobj) == QTYPE_QDICT) {
76 ret = qdict_get(qobject_to_qdict(qobj), name);
77 if (tos->h && consume && ret) {
78 bool removed = g_hash_table_remove(tos->h, name);
82 error_setg(errp, QERR_MISSING_PARAMETER, name);
85 assert(qobject_type(qobj) == QTYPE_QLIST);
87 ret = qlist_entry_obj(tos->entry);
90 tos->entry = qlist_next(tos->entry);
97 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
99 GHashTable *h = opaque;
100 g_hash_table_insert(h, (gpointer) key, NULL);
103 static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
104 QObject *obj, void *qapi)
107 StackObject *tos = g_new0(StackObject, 1);
113 if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) {
114 h = g_hash_table_new(g_str_hash, g_str_equal);
115 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
117 } else if (qobject_type(obj) == QTYPE_QLIST) {
118 tos->entry = qlist_first(qobject_to_qlist(obj));
121 QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
126 static void qobject_input_check_struct(Visitor *v, Error **errp)
128 QObjectInputVisitor *qiv = to_qiv(v);
129 StackObject *tos = QSLIST_FIRST(&qiv->stack);
131 assert(tos && !tos->entry);
133 GHashTable *const top_ht = tos->h;
138 g_hash_table_iter_init(&iter, top_ht);
139 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
140 error_setg(errp, "Parameter '%s' is unexpected", key);
146 static void qobject_input_stack_object_free(StackObject *tos)
149 g_hash_table_unref(tos->h);
155 static void qobject_input_pop(Visitor *v, void **obj)
157 QObjectInputVisitor *qiv = to_qiv(v);
158 StackObject *tos = QSLIST_FIRST(&qiv->stack);
160 assert(tos && tos->qapi == obj);
161 QSLIST_REMOVE_HEAD(&qiv->stack, node);
162 qobject_input_stack_object_free(tos);
165 static void qobject_input_start_struct(Visitor *v, const char *name, void **obj,
166 size_t size, Error **errp)
168 QObjectInputVisitor *qiv = to_qiv(v);
169 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
177 if (qobject_type(qobj) != QTYPE_QDICT) {
178 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
183 qobject_input_push(qiv, qobj, obj);
186 *obj = g_malloc0(size);
191 static void qobject_input_start_list(Visitor *v, const char *name,
192 GenericList **list, size_t size,
195 QObjectInputVisitor *qiv = to_qiv(v);
196 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
197 const QListEntry *entry;
202 if (qobject_type(qobj) != QTYPE_QLIST) {
206 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
211 entry = qobject_input_push(qiv, qobj, list);
214 *list = g_malloc0(size);
221 static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
224 QObjectInputVisitor *qiv = to_qiv(v);
225 StackObject *so = QSLIST_FIRST(&qiv->stack);
230 tail->next = g_malloc0(size);
235 static void qobject_input_start_alternate(Visitor *v, const char *name,
236 GenericAlternate **obj, size_t size,
237 bool promote_int, Error **errp)
239 QObjectInputVisitor *qiv = to_qiv(v);
240 QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
246 *obj = g_malloc0(size);
247 (*obj)->type = qobject_type(qobj);
248 if (promote_int && (*obj)->type == QTYPE_QINT) {
249 (*obj)->type = QTYPE_QFLOAT;
253 static void qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
256 QObjectInputVisitor *qiv = to_qiv(v);
257 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
263 qint = qobject_to_qint(qobj);
265 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
270 *obj = qint_get_int(qint);
273 static void qobject_input_type_uint64(Visitor *v, const char *name,
274 uint64_t *obj, Error **errp)
276 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
277 QObjectInputVisitor *qiv = to_qiv(v);
278 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
284 qint = qobject_to_qint(qobj);
286 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
291 *obj = qint_get_int(qint);
294 static void qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
297 QObjectInputVisitor *qiv = to_qiv(v);
298 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
304 qbool = qobject_to_qbool(qobj);
306 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
311 *obj = qbool_get_bool(qbool);
314 static void qobject_input_type_str(Visitor *v, const char *name, char **obj,
317 QObjectInputVisitor *qiv = to_qiv(v);
318 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
325 qstr = qobject_to_qstring(qobj);
327 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
332 *obj = g_strdup(qstring_get_str(qstr));
335 static void qobject_input_type_number(Visitor *v, const char *name, double *obj,
338 QObjectInputVisitor *qiv = to_qiv(v);
339 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
346 qint = qobject_to_qint(qobj);
348 *obj = qint_get_int(qobject_to_qint(qobj));
352 qfloat = qobject_to_qfloat(qobj);
354 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
358 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
362 static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
365 QObjectInputVisitor *qiv = to_qiv(v);
366 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
373 qobject_incref(qobj);
377 static void qobject_input_type_null(Visitor *v, const char *name, Error **errp)
379 QObjectInputVisitor *qiv = to_qiv(v);
380 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
386 if (qobject_type(qobj) != QTYPE_QNULL) {
387 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
392 static void qobject_input_optional(Visitor *v, const char *name, bool *present)
394 QObjectInputVisitor *qiv = to_qiv(v);
395 QObject *qobj = qobject_input_get_object(qiv, name, false, NULL);
405 static void qobject_input_free(Visitor *v)
407 QObjectInputVisitor *qiv = to_qiv(v);
408 while (!QSLIST_EMPTY(&qiv->stack)) {
409 StackObject *tos = QSLIST_FIRST(&qiv->stack);
411 QSLIST_REMOVE_HEAD(&qiv->stack, node);
412 qobject_input_stack_object_free(tos);
415 qobject_decref(qiv->root);
419 Visitor *qobject_input_visitor_new(QObject *obj, bool strict)
421 QObjectInputVisitor *v;
424 v = g_malloc0(sizeof(*v));
426 v->visitor.type = VISITOR_INPUT;
427 v->visitor.start_struct = qobject_input_start_struct;
428 v->visitor.check_struct = qobject_input_check_struct;
429 v->visitor.end_struct = qobject_input_pop;
430 v->visitor.start_list = qobject_input_start_list;
431 v->visitor.next_list = qobject_input_next_list;
432 v->visitor.end_list = qobject_input_pop;
433 v->visitor.start_alternate = qobject_input_start_alternate;
434 v->visitor.type_int64 = qobject_input_type_int64;
435 v->visitor.type_uint64 = qobject_input_type_uint64;
436 v->visitor.type_bool = qobject_input_type_bool;
437 v->visitor.type_str = qobject_input_type_str;
438 v->visitor.type_number = qobject_input_type_number;
439 v->visitor.type_any = qobject_input_type_any;
440 v->visitor.type_null = qobject_input_type_null;
441 v->visitor.optional = qobject_input_optional;
442 v->visitor.free = qobject_input_free;