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/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 */
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 QmpInputVisitor
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 QmpInputVisitor
*to_qiv(Visitor
*v
)
54 return container_of(v
, QmpInputVisitor
, visitor
);
57 static QObject
*qmp_input_get_object(QmpInputVisitor
*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
*qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
,
106 void *qapi
, Error
**errp
)
109 StackObject
*tos
= g_new0(StackObject
, 1);
115 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
116 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
117 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
119 } else if (qobject_type(obj
) == QTYPE_QLIST
) {
120 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
123 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
128 static void qmp_input_check_struct(Visitor
*v
, Error
**errp
)
130 QmpInputVisitor
*qiv
= to_qiv(v
);
131 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
133 assert(tos
&& !tos
->entry
);
135 GHashTable
*const top_ht
= tos
->h
;
140 g_hash_table_iter_init(&iter
, top_ht
);
141 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
142 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
148 static void qmp_input_stack_object_free(StackObject
*tos
)
151 g_hash_table_unref(tos
->h
);
157 static void qmp_input_pop(Visitor
*v
, void **obj
)
159 QmpInputVisitor
*qiv
= to_qiv(v
);
160 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
162 assert(tos
&& tos
->qapi
== obj
);
163 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
164 qmp_input_stack_object_free(tos
);
167 static void qmp_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
168 size_t size
, Error
**errp
)
170 QmpInputVisitor
*qiv
= to_qiv(v
);
171 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
180 if (qobject_type(qobj
) != QTYPE_QDICT
) {
181 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
186 qmp_input_push(qiv
, qobj
, obj
, &err
);
188 error_propagate(errp
, err
);
193 *obj
= g_malloc0(size
);
198 static void qmp_input_start_list(Visitor
*v
, const char *name
,
199 GenericList
**list
, size_t size
, Error
**errp
)
201 QmpInputVisitor
*qiv
= to_qiv(v
);
202 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
203 const QListEntry
*entry
;
208 if (qobject_type(qobj
) != QTYPE_QLIST
) {
212 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
217 entry
= qmp_input_push(qiv
, qobj
, list
, errp
);
220 *list
= g_malloc0(size
);
227 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
*tail
,
230 QmpInputVisitor
*qiv
= to_qiv(v
);
231 StackObject
*so
= QSLIST_FIRST(&qiv
->stack
);
236 tail
->next
= g_malloc0(size
);
241 static void qmp_input_start_alternate(Visitor
*v
, const char *name
,
242 GenericAlternate
**obj
, size_t size
,
243 bool promote_int
, Error
**errp
)
245 QmpInputVisitor
*qiv
= to_qiv(v
);
246 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false, errp
);
252 *obj
= g_malloc0(size
);
253 (*obj
)->type
= qobject_type(qobj
);
254 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
255 (*obj
)->type
= QTYPE_QFLOAT
;
259 static void qmp_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
262 QmpInputVisitor
*qiv
= to_qiv(v
);
263 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
269 qint
= qobject_to_qint(qobj
);
271 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
276 *obj
= qint_get_int(qint
);
279 static void qmp_input_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
282 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
283 QmpInputVisitor
*qiv
= to_qiv(v
);
284 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
290 qint
= qobject_to_qint(qobj
);
292 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
297 *obj
= qint_get_int(qint
);
300 static void qmp_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
303 QmpInputVisitor
*qiv
= to_qiv(v
);
304 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
310 qbool
= qobject_to_qbool(qobj
);
312 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
317 *obj
= qbool_get_bool(qbool
);
320 static void qmp_input_type_str(Visitor
*v
, const char *name
, char **obj
,
323 QmpInputVisitor
*qiv
= to_qiv(v
);
324 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
331 qstr
= qobject_to_qstring(qobj
);
333 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
338 *obj
= g_strdup(qstring_get_str(qstr
));
341 static void qmp_input_type_number(Visitor
*v
, const char *name
, double *obj
,
344 QmpInputVisitor
*qiv
= to_qiv(v
);
345 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
352 qint
= qobject_to_qint(qobj
);
354 *obj
= qint_get_int(qobject_to_qint(qobj
));
358 qfloat
= qobject_to_qfloat(qobj
);
360 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
364 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
368 static void qmp_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
371 QmpInputVisitor
*qiv
= to_qiv(v
);
372 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
379 qobject_incref(qobj
);
383 static void qmp_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
385 QmpInputVisitor
*qiv
= to_qiv(v
);
386 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true, errp
);
392 if (qobject_type(qobj
) != QTYPE_QNULL
) {
393 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
398 static void qmp_input_optional(Visitor
*v
, const char *name
, bool *present
)
400 QmpInputVisitor
*qiv
= to_qiv(v
);
401 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false, NULL
);
411 static void qmp_input_free(Visitor
*v
)
413 QmpInputVisitor
*qiv
= to_qiv(v
);
414 while (!QSLIST_EMPTY(&qiv
->stack
)) {
415 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
417 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
418 qmp_input_stack_object_free(tos
);
421 qobject_decref(qiv
->root
);
425 Visitor
*qmp_input_visitor_new(QObject
*obj
, bool strict
)
430 v
= g_malloc0(sizeof(*v
));
432 v
->visitor
.type
= VISITOR_INPUT
;
433 v
->visitor
.start_struct
= qmp_input_start_struct
;
434 v
->visitor
.check_struct
= qmp_input_check_struct
;
435 v
->visitor
.end_struct
= qmp_input_pop
;
436 v
->visitor
.start_list
= qmp_input_start_list
;
437 v
->visitor
.next_list
= qmp_input_next_list
;
438 v
->visitor
.end_list
= qmp_input_pop
;
439 v
->visitor
.start_alternate
= qmp_input_start_alternate
;
440 v
->visitor
.type_int64
= qmp_input_type_int64
;
441 v
->visitor
.type_uint64
= qmp_input_type_uint64
;
442 v
->visitor
.type_bool
= qmp_input_type_bool
;
443 v
->visitor
.type_str
= qmp_input_type_str
;
444 v
->visitor
.type_number
= qmp_input_type_number
;
445 v
->visitor
.type_any
= qmp_input_type_any
;
446 v
->visitor
.type_null
= qmp_input_type_null
;
447 v
->visitor
.optional
= qmp_input_optional
;
448 v
->visitor
.free
= qmp_input_free
;