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 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
;
205 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
);
213 *list
= g_malloc0(size
);
217 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
220 QObjectInputVisitor
*qiv
= to_qiv(v
);
221 StackObject
*so
= QSLIST_FIRST(&qiv
->stack
);
226 tail
->next
= g_malloc0(size
);
231 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
232 GenericAlternate
**obj
, size_t size
,
233 bool promote_int
, Error
**errp
)
235 QObjectInputVisitor
*qiv
= to_qiv(v
);
236 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
242 *obj
= g_malloc0(size
);
243 (*obj
)->type
= qobject_type(qobj
);
244 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
245 (*obj
)->type
= QTYPE_QFLOAT
;
249 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
252 QObjectInputVisitor
*qiv
= to_qiv(v
);
253 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
259 qint
= qobject_to_qint(qobj
);
261 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
266 *obj
= qint_get_int(qint
);
269 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
270 uint64_t *obj
, Error
**errp
)
272 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
273 QObjectInputVisitor
*qiv
= to_qiv(v
);
274 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
280 qint
= qobject_to_qint(qobj
);
282 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
287 *obj
= qint_get_int(qint
);
290 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
293 QObjectInputVisitor
*qiv
= to_qiv(v
);
294 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
300 qbool
= qobject_to_qbool(qobj
);
302 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
307 *obj
= qbool_get_bool(qbool
);
310 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
313 QObjectInputVisitor
*qiv
= to_qiv(v
);
314 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
321 qstr
= qobject_to_qstring(qobj
);
323 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
328 *obj
= g_strdup(qstring_get_str(qstr
));
331 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
334 QObjectInputVisitor
*qiv
= to_qiv(v
);
335 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
342 qint
= qobject_to_qint(qobj
);
344 *obj
= qint_get_int(qobject_to_qint(qobj
));
348 qfloat
= qobject_to_qfloat(qobj
);
350 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
354 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
358 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
361 QObjectInputVisitor
*qiv
= to_qiv(v
);
362 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
369 qobject_incref(qobj
);
373 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
375 QObjectInputVisitor
*qiv
= to_qiv(v
);
376 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
382 if (qobject_type(qobj
) != QTYPE_QNULL
) {
383 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
388 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
390 QObjectInputVisitor
*qiv
= to_qiv(v
);
391 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, NULL
);
401 static void qobject_input_free(Visitor
*v
)
403 QObjectInputVisitor
*qiv
= to_qiv(v
);
404 while (!QSLIST_EMPTY(&qiv
->stack
)) {
405 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
407 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
408 qobject_input_stack_object_free(tos
);
411 qobject_decref(qiv
->root
);
415 Visitor
*qobject_input_visitor_new(QObject
*obj
, bool strict
)
417 QObjectInputVisitor
*v
;
420 v
= g_malloc0(sizeof(*v
));
422 v
->visitor
.type
= VISITOR_INPUT
;
423 v
->visitor
.start_struct
= qobject_input_start_struct
;
424 v
->visitor
.check_struct
= qobject_input_check_struct
;
425 v
->visitor
.end_struct
= qobject_input_pop
;
426 v
->visitor
.start_list
= qobject_input_start_list
;
427 v
->visitor
.next_list
= qobject_input_next_list
;
428 v
->visitor
.end_list
= qobject_input_pop
;
429 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
430 v
->visitor
.type_int64
= qobject_input_type_int64
;
431 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
432 v
->visitor
.type_bool
= qobject_input_type_bool
;
433 v
->visitor
.type_str
= qobject_input_type_str
;
434 v
->visitor
.type_number
= qobject_input_type_number
;
435 v
->visitor
.type_any
= qobject_input_type_any
;
436 v
->visitor
.type_null
= qobject_input_type_null
;
437 v
->visitor
.optional
= qobject_input_optional
;
438 v
->visitor
.free
= qobject_input_free
;