4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
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/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 #define QIV_STACK_SIZE 1024
26 typedef struct StackObject
28 QObject
*obj
; /* Object being visited */
29 void *qapi
; /* sanity check that caller uses same pointer */
31 GHashTable
*h
; /* If obj is dict: unvisited keys */
32 const QListEntry
*entry
; /* If obj is list: unvisited tail */
34 QSLIST_ENTRY(StackObject
) node
;
37 struct QObjectInputVisitor
41 /* Root of visit at visitor creation. */
44 /* Stack of objects being visited (all entries will be either
46 QSLIST_HEAD(, StackObject
) stack
;
48 /* True to reject parse in visit_end_struct() if unvisited keys remain. */
52 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
54 return container_of(v
, QObjectInputVisitor
, visitor
);
57 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
59 bool consume
, Error
**errp
)
65 if (QSLIST_EMPTY(&qiv
->stack
)) {
66 /* Starting at root, name is ignored. */
71 /* We are in a container; find the next element. */
72 tos
= QSLIST_FIRST(&qiv
->stack
);
76 if (qobject_type(qobj
) == QTYPE_QDICT
) {
78 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
79 if (tos
->h
&& consume
&& ret
) {
80 bool removed
= g_hash_table_remove(tos
->h
, name
);
84 error_setg(errp
, QERR_MISSING_PARAMETER
, name
);
87 assert(qobject_type(qobj
) == QTYPE_QLIST
);
89 ret
= qlist_entry_obj(tos
->entry
);
92 tos
->entry
= qlist_next(tos
->entry
);
99 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
101 GHashTable
*h
= opaque
;
102 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
105 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
106 QObject
*obj
, void *qapi
,
110 StackObject
*tos
= g_new0(StackObject
, 1);
116 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
117 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
118 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
120 } else if (qobject_type(obj
) == QTYPE_QLIST
) {
121 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
124 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
129 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
131 QObjectInputVisitor
*qiv
= to_qiv(v
);
132 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
134 assert(tos
&& !tos
->entry
);
136 GHashTable
*const top_ht
= tos
->h
;
141 g_hash_table_iter_init(&iter
, top_ht
);
142 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
143 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
149 static void qobject_input_stack_object_free(StackObject
*tos
)
152 g_hash_table_unref(tos
->h
);
158 static void qobject_input_pop(Visitor
*v
, void **obj
)
160 QObjectInputVisitor
*qiv
= to_qiv(v
);
161 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
163 assert(tos
&& tos
->qapi
== obj
);
164 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
165 qobject_input_stack_object_free(tos
);
168 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
169 size_t size
, Error
**errp
)
171 QObjectInputVisitor
*qiv
= to_qiv(v
);
172 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
181 if (qobject_type(qobj
) != QTYPE_QDICT
) {
182 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
187 qobject_input_push(qiv
, qobj
, obj
, &err
);
189 error_propagate(errp
, err
);
194 *obj
= g_malloc0(size
);
199 static void qobject_input_start_list(Visitor
*v
, const char *name
,
200 GenericList
**list
, size_t size
,
203 QObjectInputVisitor
*qiv
= to_qiv(v
);
204 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
205 const QListEntry
*entry
;
210 if (qobject_type(qobj
) != QTYPE_QLIST
) {
214 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
219 entry
= qobject_input_push(qiv
, qobj
, list
, errp
);
222 *list
= g_malloc0(size
);
229 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
232 QObjectInputVisitor
*qiv
= to_qiv(v
);
233 StackObject
*so
= QSLIST_FIRST(&qiv
->stack
);
238 tail
->next
= g_malloc0(size
);
243 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
244 GenericAlternate
**obj
, size_t size
,
245 bool promote_int
, Error
**errp
)
247 QObjectInputVisitor
*qiv
= to_qiv(v
);
248 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
254 *obj
= g_malloc0(size
);
255 (*obj
)->type
= qobject_type(qobj
);
256 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
257 (*obj
)->type
= QTYPE_QFLOAT
;
261 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
264 QObjectInputVisitor
*qiv
= to_qiv(v
);
265 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
271 qint
= qobject_to_qint(qobj
);
273 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
278 *obj
= qint_get_int(qint
);
281 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
282 uint64_t *obj
, Error
**errp
)
284 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
285 QObjectInputVisitor
*qiv
= to_qiv(v
);
286 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
292 qint
= qobject_to_qint(qobj
);
294 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
299 *obj
= qint_get_int(qint
);
302 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
305 QObjectInputVisitor
*qiv
= to_qiv(v
);
306 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
312 qbool
= qobject_to_qbool(qobj
);
314 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
319 *obj
= qbool_get_bool(qbool
);
322 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
325 QObjectInputVisitor
*qiv
= to_qiv(v
);
326 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
333 qstr
= qobject_to_qstring(qobj
);
335 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
340 *obj
= g_strdup(qstring_get_str(qstr
));
343 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
346 QObjectInputVisitor
*qiv
= to_qiv(v
);
347 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
354 qint
= qobject_to_qint(qobj
);
356 *obj
= qint_get_int(qobject_to_qint(qobj
));
360 qfloat
= qobject_to_qfloat(qobj
);
362 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
366 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
370 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
373 QObjectInputVisitor
*qiv
= to_qiv(v
);
374 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
381 qobject_incref(qobj
);
385 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
387 QObjectInputVisitor
*qiv
= to_qiv(v
);
388 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
394 if (qobject_type(qobj
) != QTYPE_QNULL
) {
395 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
400 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
402 QObjectInputVisitor
*qiv
= to_qiv(v
);
403 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, NULL
);
413 static void qobject_input_free(Visitor
*v
)
415 QObjectInputVisitor
*qiv
= to_qiv(v
);
416 while (!QSLIST_EMPTY(&qiv
->stack
)) {
417 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
419 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
420 qobject_input_stack_object_free(tos
);
423 qobject_decref(qiv
->root
);
427 Visitor
*qobject_input_visitor_new(QObject
*obj
, bool strict
)
429 QObjectInputVisitor
*v
;
432 v
= g_malloc0(sizeof(*v
));
434 v
->visitor
.type
= VISITOR_INPUT
;
435 v
->visitor
.start_struct
= qobject_input_start_struct
;
436 v
->visitor
.check_struct
= qobject_input_check_struct
;
437 v
->visitor
.end_struct
= qobject_input_pop
;
438 v
->visitor
.start_list
= qobject_input_start_list
;
439 v
->visitor
.next_list
= qobject_input_next_list
;
440 v
->visitor
.end_list
= qobject_input_pop
;
441 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
442 v
->visitor
.type_int64
= qobject_input_type_int64
;
443 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
444 v
->visitor
.type_bool
= qobject_input_type_bool
;
445 v
->visitor
.type_str
= qobject_input_type_str
;
446 v
->visitor
.type_number
= qobject_input_type_number
;
447 v
->visitor
.type_any
= qobject_input_type_any
;
448 v
->visitor
.type_null
= qobject_input_type_null
;
449 v
->visitor
.optional
= qobject_input_optional
;
450 v
->visitor
.free
= qobject_input_free
;