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
{
25 const char *name
; /* Name of @obj in its parent, if any */
26 QObject
*obj
; /* QDict or QList being visited */
27 void *qapi
; /* sanity check that caller uses same pointer */
29 GHashTable
*h
; /* If @obj is QDict: unvisited keys */
30 const QListEntry
*entry
; /* If @obj is QList: unvisited tail */
31 unsigned index
; /* If @obj is QList: list index of @entry */
33 QSLIST_ENTRY(StackObject
) node
; /* parent */
36 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. */
49 GString
*errname
; /* Accumulator for full_name() */
52 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
54 return container_of(v
, QObjectInputVisitor
, visitor
);
57 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
63 g_string_truncate(qiv
->errname
, 0);
65 qiv
->errname
= g_string_new("");
68 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
69 if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
70 g_string_prepend(qiv
->errname
, name
);
71 g_string_prepend_c(qiv
->errname
, '.');
73 snprintf(buf
, sizeof(buf
), "[%u]", so
->index
);
74 g_string_prepend(qiv
->errname
, buf
);
80 g_string_prepend(qiv
->errname
, name
);
81 } else if (qiv
->errname
->str
[0] == '.') {
82 g_string_erase(qiv
->errname
, 0, 1);
87 return qiv
->errname
->str
;
90 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
98 if (QSLIST_EMPTY(&qiv
->stack
)) {
99 /* Starting at root, name is ignored. */
104 /* We are in a container; find the next element. */
105 tos
= QSLIST_FIRST(&qiv
->stack
);
109 if (qobject_type(qobj
) == QTYPE_QDICT
) {
111 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
112 if (tos
->h
&& consume
&& ret
) {
113 bool removed
= g_hash_table_remove(tos
->h
, name
);
117 assert(qobject_type(qobj
) == QTYPE_QLIST
);
119 ret
= qlist_entry_obj(tos
->entry
);
122 tos
->entry
= qlist_next(tos
->entry
);
130 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
132 bool consume
, Error
**errp
)
134 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
137 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
142 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
144 GHashTable
*h
= opaque
;
145 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
148 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
150 QObject
*obj
, void *qapi
)
153 StackObject
*tos
= g_new0(StackObject
, 1);
160 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
161 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
162 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
164 } else if (qobject_type(obj
) == QTYPE_QLIST
) {
165 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
169 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
174 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
176 QObjectInputVisitor
*qiv
= to_qiv(v
);
177 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
179 assert(tos
&& !tos
->entry
);
181 GHashTable
*const top_ht
= tos
->h
;
186 g_hash_table_iter_init(&iter
, top_ht
);
187 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
188 error_setg(errp
, "Parameter '%s' is unexpected",
189 full_name(qiv
, key
));
195 static void qobject_input_stack_object_free(StackObject
*tos
)
198 g_hash_table_unref(tos
->h
);
204 static void qobject_input_pop(Visitor
*v
, void **obj
)
206 QObjectInputVisitor
*qiv
= to_qiv(v
);
207 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
209 assert(tos
&& tos
->qapi
== obj
);
210 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
211 qobject_input_stack_object_free(tos
);
214 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
215 size_t size
, Error
**errp
)
217 QObjectInputVisitor
*qiv
= to_qiv(v
);
218 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
226 if (qobject_type(qobj
) != QTYPE_QDICT
) {
227 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
228 full_name(qiv
, name
), "object");
232 qobject_input_push(qiv
, name
, qobj
, obj
);
235 *obj
= g_malloc0(size
);
240 static void qobject_input_start_list(Visitor
*v
, const char *name
,
241 GenericList
**list
, size_t size
,
244 QObjectInputVisitor
*qiv
= to_qiv(v
);
245 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
246 const QListEntry
*entry
;
254 if (qobject_type(qobj
) != QTYPE_QLIST
) {
255 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
256 full_name(qiv
, name
), "array");
260 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
262 *list
= g_malloc0(size
);
266 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
269 QObjectInputVisitor
*qiv
= to_qiv(v
);
270 StackObject
*so
= QSLIST_FIRST(&qiv
->stack
);
275 tail
->next
= g_malloc0(size
);
280 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
281 GenericAlternate
**obj
, size_t size
,
282 bool promote_int
, Error
**errp
)
284 QObjectInputVisitor
*qiv
= to_qiv(v
);
285 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
291 *obj
= g_malloc0(size
);
292 (*obj
)->type
= qobject_type(qobj
);
293 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
294 (*obj
)->type
= QTYPE_QFLOAT
;
298 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
301 QObjectInputVisitor
*qiv
= to_qiv(v
);
302 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
308 qint
= qobject_to_qint(qobj
);
310 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
311 full_name(qiv
, name
), "integer");
315 *obj
= qint_get_int(qint
);
318 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
319 uint64_t *obj
, Error
**errp
)
321 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
322 QObjectInputVisitor
*qiv
= to_qiv(v
);
323 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
329 qint
= qobject_to_qint(qobj
);
331 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
332 full_name(qiv
, name
), "integer");
336 *obj
= qint_get_int(qint
);
339 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
342 QObjectInputVisitor
*qiv
= to_qiv(v
);
343 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
349 qbool
= qobject_to_qbool(qobj
);
351 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
352 full_name(qiv
, name
), "boolean");
356 *obj
= qbool_get_bool(qbool
);
359 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
362 QObjectInputVisitor
*qiv
= to_qiv(v
);
363 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
370 qstr
= qobject_to_qstring(qobj
);
372 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
373 full_name(qiv
, name
), "string");
377 *obj
= g_strdup(qstring_get_str(qstr
));
380 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
383 QObjectInputVisitor
*qiv
= to_qiv(v
);
384 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
391 qint
= qobject_to_qint(qobj
);
393 *obj
= qint_get_int(qobject_to_qint(qobj
));
397 qfloat
= qobject_to_qfloat(qobj
);
399 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
403 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
404 full_name(qiv
, name
), "number");
407 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
410 QObjectInputVisitor
*qiv
= to_qiv(v
);
411 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
418 qobject_incref(qobj
);
422 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
424 QObjectInputVisitor
*qiv
= to_qiv(v
);
425 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
431 if (qobject_type(qobj
) != QTYPE_QNULL
) {
432 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
433 full_name(qiv
, name
), "null");
437 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
439 QObjectInputVisitor
*qiv
= to_qiv(v
);
440 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
450 static void qobject_input_free(Visitor
*v
)
452 QObjectInputVisitor
*qiv
= to_qiv(v
);
454 while (!QSLIST_EMPTY(&qiv
->stack
)) {
455 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
457 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
458 qobject_input_stack_object_free(tos
);
461 qobject_decref(qiv
->root
);
463 g_string_free(qiv
->errname
, TRUE
);
468 Visitor
*qobject_input_visitor_new(QObject
*obj
, bool strict
)
470 QObjectInputVisitor
*v
;
473 v
= g_malloc0(sizeof(*v
));
475 v
->visitor
.type
= VISITOR_INPUT
;
476 v
->visitor
.start_struct
= qobject_input_start_struct
;
477 v
->visitor
.check_struct
= qobject_input_check_struct
;
478 v
->visitor
.end_struct
= qobject_input_pop
;
479 v
->visitor
.start_list
= qobject_input_start_list
;
480 v
->visitor
.next_list
= qobject_input_next_list
;
481 v
->visitor
.end_list
= qobject_input_pop
;
482 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
483 v
->visitor
.type_int64
= qobject_input_type_int64
;
484 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
485 v
->visitor
.type_bool
= qobject_input_type_bool
;
486 v
->visitor
.type_str
= qobject_input_type_str
;
487 v
->visitor
.type_number
= qobject_input_type_number
;
488 v
->visitor
.type_any
= qobject_input_type_any
;
489 v
->visitor
.type_null
= qobject_input_type_null
;
490 v
->visitor
.optional
= qobject_input_optional
;
491 v
->visitor
.free
= qobject_input_free
;