4 * Copyright (C) 2012-2017 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"
17 #include "qapi/error.h"
18 #include "qapi/qobject-input-visitor.h"
19 #include "qapi/visitor-impl.h"
20 #include "qemu/queue.h"
21 #include "qapi/qmp/qjson.h"
22 #include "qapi/qmp/qbool.h"
23 #include "qapi/qmp/qdict.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qlist.h"
26 #include "qapi/qmp/qnull.h"
27 #include "qapi/qmp/qnum.h"
28 #include "qapi/qmp/qstring.h"
29 #include "qemu/cutils.h"
30 #include "qemu/option.h"
32 typedef struct StackObject
{
33 const char *name
; /* Name of @obj in its parent, if any */
34 QObject
*obj
; /* QDict or QList being visited */
35 void *qapi
; /* sanity check that caller uses same pointer */
37 GHashTable
*h
; /* If @obj is QDict: unvisited keys */
38 const QListEntry
*entry
; /* If @obj is QList: unvisited tail */
39 unsigned index
; /* If @obj is QList: list index of @entry */
41 QSLIST_ENTRY(StackObject
) node
; /* parent */
44 struct QObjectInputVisitor
{
47 /* Root of visit at visitor creation. */
49 bool keyval
; /* Assume @root made with keyval_parse() */
51 /* Stack of objects being visited (all entries will be either
53 QSLIST_HEAD(, StackObject
) stack
;
55 GString
*errname
; /* Accumulator for full_name() */
58 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
60 return container_of(v
, QObjectInputVisitor
, visitor
);
64 * Find the full name of something @qiv is currently visiting.
65 * @qiv is visiting something named @name in the stack of containers
67 * If @n is zero, return its full name.
68 * If @n is positive, return the full name of the @n-th container
69 * counting from the top. The stack of containers must have at least
71 * The returned string is valid until the next full_name_nth(@v) or
74 static const char *full_name_nth(QObjectInputVisitor
*qiv
, const char *name
,
81 g_string_truncate(qiv
->errname
, 0);
83 qiv
->errname
= g_string_new("");
86 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
89 } else if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
90 g_string_prepend(qiv
->errname
, name
?: "<anonymous>");
91 g_string_prepend_c(qiv
->errname
, '.');
93 snprintf(buf
, sizeof(buf
),
94 qiv
->keyval
? ".%u" : "[%u]",
96 g_string_prepend(qiv
->errname
, buf
);
103 g_string_prepend(qiv
->errname
, name
);
104 } else if (qiv
->errname
->str
[0] == '.') {
105 g_string_erase(qiv
->errname
, 0, 1);
106 } else if (!qiv
->errname
->str
[0]) {
107 return "<anonymous>";
110 return qiv
->errname
->str
;
113 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
115 return full_name_nth(qiv
, name
, 0);
118 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
126 if (QSLIST_EMPTY(&qiv
->stack
)) {
127 /* Starting at root, name is ignored. */
132 /* We are in a container; find the next element. */
133 tos
= QSLIST_FIRST(&qiv
->stack
);
137 if (qobject_type(qobj
) == QTYPE_QDICT
) {
139 ret
= qdict_get(qobject_to(QDict
, qobj
), name
);
140 if (tos
->h
&& consume
&& ret
) {
141 bool removed
= g_hash_table_remove(tos
->h
, name
);
145 assert(qobject_type(qobj
) == QTYPE_QLIST
);
148 ret
= qlist_entry_obj(tos
->entry
);
150 tos
->entry
= qlist_next(tos
->entry
);
163 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
165 bool consume
, Error
**errp
)
167 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
170 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
175 static const char *qobject_input_get_keyval(QObjectInputVisitor
*qiv
,
182 qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
187 qstr
= qobject_to(QString
, qobj
);
189 switch (qobject_type(qobj
)) {
192 error_setg(errp
, "Parameters '%s.*' are unexpected",
193 full_name(qiv
, name
));
196 /* Non-string scalar (should this be an assertion?) */
197 error_setg(errp
, "Internal error: parameter %s invalid",
198 full_name(qiv
, name
));
203 return qstring_get_str(qstr
);
206 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
208 QObject
*obj
, void *qapi
)
211 StackObject
*tos
= g_new0(StackObject
, 1);
212 QDict
*qdict
= qobject_to(QDict
, obj
);
213 QList
*qlist
= qobject_to(QList
, obj
);
214 const QDictEntry
*entry
;
222 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
223 for (entry
= qdict_first(qdict
);
225 entry
= qdict_next(qdict
, entry
)) {
226 g_hash_table_insert(h
, (void *)qdict_entry_key(entry
), NULL
);
231 tos
->entry
= qlist_first(qlist
);
235 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
240 static bool qobject_input_check_struct(Visitor
*v
, Error
**errp
)
242 QObjectInputVisitor
*qiv
= to_qiv(v
);
243 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
247 assert(tos
&& !tos
->entry
);
249 g_hash_table_iter_init(&iter
, tos
->h
);
250 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
251 error_setg(errp
, "Parameter '%s' is unexpected",
252 full_name(qiv
, key
));
258 static void qobject_input_stack_object_free(StackObject
*tos
)
261 g_hash_table_unref(tos
->h
);
267 static void qobject_input_pop(Visitor
*v
, void **obj
)
269 QObjectInputVisitor
*qiv
= to_qiv(v
);
270 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
272 assert(tos
&& tos
->qapi
== obj
);
273 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
274 qobject_input_stack_object_free(tos
);
277 static bool qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
278 size_t size
, Error
**errp
)
280 QObjectInputVisitor
*qiv
= to_qiv(v
);
281 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
289 if (qobject_type(qobj
) != QTYPE_QDICT
) {
290 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
291 full_name(qiv
, name
), "object");
295 qobject_input_push(qiv
, name
, qobj
, obj
);
298 *obj
= g_malloc0(size
);
303 static void qobject_input_end_struct(Visitor
*v
, void **obj
)
305 QObjectInputVisitor
*qiv
= to_qiv(v
);
306 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
308 assert(qobject_type(tos
->obj
) == QTYPE_QDICT
&& tos
->h
);
309 qobject_input_pop(v
, obj
);
313 static bool qobject_input_start_list(Visitor
*v
, const char *name
,
314 GenericList
**list
, size_t size
,
317 QObjectInputVisitor
*qiv
= to_qiv(v
);
318 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
319 const QListEntry
*entry
;
327 if (qobject_type(qobj
) != QTYPE_QLIST
) {
328 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
329 full_name(qiv
, name
), "array");
333 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
335 *list
= g_malloc0(size
);
340 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
343 QObjectInputVisitor
*qiv
= to_qiv(v
);
344 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
346 assert(tos
&& qobject_to(QList
, tos
->obj
));
351 tail
->next
= g_malloc0(size
);
355 static bool qobject_input_check_list(Visitor
*v
, Error
**errp
)
357 QObjectInputVisitor
*qiv
= to_qiv(v
);
358 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
360 assert(tos
&& qobject_to(QList
, tos
->obj
));
363 error_setg(errp
, "Only %u list elements expected in %s",
364 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
370 static void qobject_input_end_list(Visitor
*v
, void **obj
)
372 QObjectInputVisitor
*qiv
= to_qiv(v
);
373 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
375 assert(qobject_type(tos
->obj
) == QTYPE_QLIST
&& !tos
->h
);
376 qobject_input_pop(v
, obj
);
379 static bool qobject_input_start_alternate(Visitor
*v
, const char *name
,
380 GenericAlternate
**obj
, size_t size
,
383 QObjectInputVisitor
*qiv
= to_qiv(v
);
384 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
390 *obj
= g_malloc0(size
);
391 (*obj
)->type
= qobject_type(qobj
);
395 static bool qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
398 QObjectInputVisitor
*qiv
= to_qiv(v
);
399 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
405 qnum
= qobject_to(QNum
, qobj
);
406 if (!qnum
|| !qnum_get_try_int(qnum
, obj
)) {
407 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
408 full_name(qiv
, name
), "integer");
414 static bool qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
415 int64_t *obj
, Error
**errp
)
417 QObjectInputVisitor
*qiv
= to_qiv(v
);
418 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
424 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
425 /* TODO report -ERANGE more nicely */
426 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
427 full_name(qiv
, name
), "integer");
433 static bool qobject_input_type_uint64(Visitor
*v
, const char *name
,
434 uint64_t *obj
, Error
**errp
)
436 QObjectInputVisitor
*qiv
= to_qiv(v
);
437 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
444 qnum
= qobject_to(QNum
, qobj
);
449 if (qnum_get_try_uint(qnum
, obj
)) {
453 /* Need to accept negative values for backward compatibility */
454 if (qnum_get_try_int(qnum
, &val
)) {
460 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
461 full_name(qiv
, name
), "uint64");
465 static bool qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
466 uint64_t *obj
, Error
**errp
)
468 QObjectInputVisitor
*qiv
= to_qiv(v
);
469 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
475 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
476 /* TODO report -ERANGE more nicely */
477 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
478 full_name(qiv
, name
), "integer");
484 static bool qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
487 QObjectInputVisitor
*qiv
= to_qiv(v
);
488 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
494 qbool
= qobject_to(QBool
, qobj
);
496 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
497 full_name(qiv
, name
), "boolean");
501 *obj
= qbool_get_bool(qbool
);
505 static bool qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
506 bool *obj
, Error
**errp
)
508 QObjectInputVisitor
*qiv
= to_qiv(v
);
509 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
515 if (!strcmp(str
, "on")) {
517 } else if (!strcmp(str
, "off")) {
520 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
521 full_name(qiv
, name
), "'on' or 'off'");
527 static bool qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
530 QObjectInputVisitor
*qiv
= to_qiv(v
);
531 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
538 qstr
= qobject_to(QString
, qobj
);
540 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
541 full_name(qiv
, name
), "string");
545 *obj
= g_strdup(qstring_get_str(qstr
));
549 static bool qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
550 char **obj
, Error
**errp
)
552 QObjectInputVisitor
*qiv
= to_qiv(v
);
553 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
555 *obj
= g_strdup(str
);
559 static bool qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
562 QObjectInputVisitor
*qiv
= to_qiv(v
);
563 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
569 qnum
= qobject_to(QNum
, qobj
);
571 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
572 full_name(qiv
, name
), "number");
576 *obj
= qnum_get_double(qnum
);
580 static bool qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
581 double *obj
, Error
**errp
)
583 QObjectInputVisitor
*qiv
= to_qiv(v
);
584 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
591 if (qemu_strtod_finite(str
, NULL
, &val
)) {
592 /* TODO report -ERANGE more nicely */
593 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
594 full_name(qiv
, name
), "number");
602 static bool qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
605 QObjectInputVisitor
*qiv
= to_qiv(v
);
606 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
613 *obj
= qobject_ref(qobj
);
617 static bool qobject_input_type_null(Visitor
*v
, const char *name
,
618 QNull
**obj
, Error
**errp
)
620 QObjectInputVisitor
*qiv
= to_qiv(v
);
621 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
628 if (qobject_type(qobj
) != QTYPE_QNULL
) {
629 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
630 full_name(qiv
, name
), "null");
637 static bool qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
638 uint64_t *obj
, Error
**errp
)
640 QObjectInputVisitor
*qiv
= to_qiv(v
);
641 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
647 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
648 /* TODO report -ERANGE more nicely */
649 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
650 full_name(qiv
, name
), "size");
656 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
658 QObjectInputVisitor
*qiv
= to_qiv(v
);
659 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
669 static void qobject_input_free(Visitor
*v
)
671 QObjectInputVisitor
*qiv
= to_qiv(v
);
673 while (!QSLIST_EMPTY(&qiv
->stack
)) {
674 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
676 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
677 qobject_input_stack_object_free(tos
);
680 qobject_unref(qiv
->root
);
682 g_string_free(qiv
->errname
, TRUE
);
687 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
689 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
693 v
->visitor
.type
= VISITOR_INPUT
;
694 v
->visitor
.start_struct
= qobject_input_start_struct
;
695 v
->visitor
.check_struct
= qobject_input_check_struct
;
696 v
->visitor
.end_struct
= qobject_input_end_struct
;
697 v
->visitor
.start_list
= qobject_input_start_list
;
698 v
->visitor
.next_list
= qobject_input_next_list
;
699 v
->visitor
.check_list
= qobject_input_check_list
;
700 v
->visitor
.end_list
= qobject_input_end_list
;
701 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
702 v
->visitor
.optional
= qobject_input_optional
;
703 v
->visitor
.free
= qobject_input_free
;
705 v
->root
= qobject_ref(obj
);
710 Visitor
*qobject_input_visitor_new(QObject
*obj
)
712 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
714 v
->visitor
.type_int64
= qobject_input_type_int64
;
715 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
716 v
->visitor
.type_bool
= qobject_input_type_bool
;
717 v
->visitor
.type_str
= qobject_input_type_str
;
718 v
->visitor
.type_number
= qobject_input_type_number
;
719 v
->visitor
.type_any
= qobject_input_type_any
;
720 v
->visitor
.type_null
= qobject_input_type_null
;
725 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
727 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
729 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
730 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
731 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
732 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
733 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
734 v
->visitor
.type_any
= qobject_input_type_any
;
735 v
->visitor
.type_null
= qobject_input_type_null
;
736 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
742 Visitor
*qobject_input_visitor_new_str(const char *str
,
743 const char *implied_key
,
746 bool is_json
= str
[0] == '{';
752 obj
= qobject_from_json(str
, errp
);
756 args
= qobject_to(QDict
, obj
);
758 v
= qobject_input_visitor_new(QOBJECT(args
));
760 args
= keyval_parse(str
, implied_key
, NULL
, errp
);
764 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));