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 void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
208 GHashTable
*h
= opaque
;
209 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
212 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
214 QObject
*obj
, void *qapi
)
217 StackObject
*tos
= g_new0(StackObject
, 1);
224 if (qobject_type(obj
) == QTYPE_QDICT
) {
225 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
226 qdict_iter(qobject_to(QDict
, obj
), qdict_add_key
, h
);
229 assert(qobject_type(obj
) == QTYPE_QLIST
);
230 tos
->entry
= qlist_first(qobject_to(QList
, obj
));
234 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
239 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
241 QObjectInputVisitor
*qiv
= to_qiv(v
);
242 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
246 assert(tos
&& !tos
->entry
);
248 g_hash_table_iter_init(&iter
, tos
->h
);
249 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
250 error_setg(errp
, "Parameter '%s' is unexpected",
251 full_name(qiv
, key
));
255 static void qobject_input_stack_object_free(StackObject
*tos
)
258 g_hash_table_unref(tos
->h
);
264 static void qobject_input_pop(Visitor
*v
, void **obj
)
266 QObjectInputVisitor
*qiv
= to_qiv(v
);
267 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
269 assert(tos
&& tos
->qapi
== obj
);
270 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
271 qobject_input_stack_object_free(tos
);
274 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
275 size_t size
, Error
**errp
)
277 QObjectInputVisitor
*qiv
= to_qiv(v
);
278 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
286 if (qobject_type(qobj
) != QTYPE_QDICT
) {
287 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
288 full_name(qiv
, name
), "object");
292 qobject_input_push(qiv
, name
, qobj
, obj
);
295 *obj
= g_malloc0(size
);
299 static void qobject_input_end_struct(Visitor
*v
, void **obj
)
301 QObjectInputVisitor
*qiv
= to_qiv(v
);
302 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
304 assert(qobject_type(tos
->obj
) == QTYPE_QDICT
&& tos
->h
);
305 qobject_input_pop(v
, obj
);
309 static void qobject_input_start_list(Visitor
*v
, const char *name
,
310 GenericList
**list
, size_t size
,
313 QObjectInputVisitor
*qiv
= to_qiv(v
);
314 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
315 const QListEntry
*entry
;
323 if (qobject_type(qobj
) != QTYPE_QLIST
) {
324 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
325 full_name(qiv
, name
), "array");
329 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
331 *list
= g_malloc0(size
);
335 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
338 QObjectInputVisitor
*qiv
= to_qiv(v
);
339 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
341 assert(tos
&& qobject_to(QList
, tos
->obj
));
346 tail
->next
= g_malloc0(size
);
350 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
352 QObjectInputVisitor
*qiv
= to_qiv(v
);
353 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
355 assert(tos
&& qobject_to(QList
, tos
->obj
));
358 error_setg(errp
, "Only %u list elements expected in %s",
359 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
363 static void qobject_input_end_list(Visitor
*v
, void **obj
)
365 QObjectInputVisitor
*qiv
= to_qiv(v
);
366 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
368 assert(qobject_type(tos
->obj
) == QTYPE_QLIST
&& !tos
->h
);
369 qobject_input_pop(v
, obj
);
372 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
373 GenericAlternate
**obj
, size_t size
,
376 QObjectInputVisitor
*qiv
= to_qiv(v
);
377 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
383 *obj
= g_malloc0(size
);
384 (*obj
)->type
= qobject_type(qobj
);
387 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
390 QObjectInputVisitor
*qiv
= to_qiv(v
);
391 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
397 qnum
= qobject_to(QNum
, qobj
);
398 if (!qnum
|| !qnum_get_try_int(qnum
, obj
)) {
399 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
400 full_name(qiv
, name
), "integer");
404 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
405 int64_t *obj
, Error
**errp
)
407 QObjectInputVisitor
*qiv
= to_qiv(v
);
408 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
414 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
415 /* TODO report -ERANGE more nicely */
416 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
417 full_name(qiv
, name
), "integer");
421 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
422 uint64_t *obj
, Error
**errp
)
424 QObjectInputVisitor
*qiv
= to_qiv(v
);
425 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
432 qnum
= qobject_to(QNum
, qobj
);
437 if (qnum_get_try_uint(qnum
, obj
)) {
441 /* Need to accept negative values for backward compatibility */
442 if (qnum_get_try_int(qnum
, &val
)) {
448 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
449 full_name(qiv
, name
), "uint64");
452 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
453 uint64_t *obj
, Error
**errp
)
455 QObjectInputVisitor
*qiv
= to_qiv(v
);
456 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
462 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
463 /* TODO report -ERANGE more nicely */
464 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
465 full_name(qiv
, name
), "integer");
469 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
472 QObjectInputVisitor
*qiv
= to_qiv(v
);
473 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
479 qbool
= qobject_to(QBool
, qobj
);
481 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
482 full_name(qiv
, name
), "boolean");
486 *obj
= qbool_get_bool(qbool
);
489 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
490 bool *obj
, Error
**errp
)
492 QObjectInputVisitor
*qiv
= to_qiv(v
);
493 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
499 if (!strcmp(str
, "on")) {
501 } else if (!strcmp(str
, "off")) {
504 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
505 full_name(qiv
, name
), "'on' or 'off'");
509 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
512 QObjectInputVisitor
*qiv
= to_qiv(v
);
513 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
520 qstr
= qobject_to(QString
, qobj
);
522 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
523 full_name(qiv
, name
), "string");
527 *obj
= g_strdup(qstring_get_str(qstr
));
530 static void qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
531 char **obj
, Error
**errp
)
533 QObjectInputVisitor
*qiv
= to_qiv(v
);
534 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
536 *obj
= g_strdup(str
);
539 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
542 QObjectInputVisitor
*qiv
= to_qiv(v
);
543 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
549 qnum
= qobject_to(QNum
, qobj
);
551 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
552 full_name(qiv
, name
), "number");
556 *obj
= qnum_get_double(qnum
);
559 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
560 double *obj
, Error
**errp
)
562 QObjectInputVisitor
*qiv
= to_qiv(v
);
563 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
570 if (qemu_strtod_finite(str
, NULL
, &val
)) {
571 /* TODO report -ERANGE more nicely */
572 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
573 full_name(qiv
, name
), "number");
580 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
583 QObjectInputVisitor
*qiv
= to_qiv(v
);
584 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
591 *obj
= qobject_ref(qobj
);
594 static void qobject_input_type_null(Visitor
*v
, const char *name
,
595 QNull
**obj
, Error
**errp
)
597 QObjectInputVisitor
*qiv
= to_qiv(v
);
598 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
605 if (qobject_type(qobj
) != QTYPE_QNULL
) {
606 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
607 full_name(qiv
, name
), "null");
613 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
614 uint64_t *obj
, Error
**errp
)
616 QObjectInputVisitor
*qiv
= to_qiv(v
);
617 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
623 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
624 /* TODO report -ERANGE more nicely */
625 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
626 full_name(qiv
, name
), "size");
630 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
632 QObjectInputVisitor
*qiv
= to_qiv(v
);
633 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
643 static void qobject_input_free(Visitor
*v
)
645 QObjectInputVisitor
*qiv
= to_qiv(v
);
647 while (!QSLIST_EMPTY(&qiv
->stack
)) {
648 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
650 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
651 qobject_input_stack_object_free(tos
);
654 qobject_unref(qiv
->root
);
656 g_string_free(qiv
->errname
, TRUE
);
661 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
663 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
667 v
->visitor
.type
= VISITOR_INPUT
;
668 v
->visitor
.start_struct
= qobject_input_start_struct
;
669 v
->visitor
.check_struct
= qobject_input_check_struct
;
670 v
->visitor
.end_struct
= qobject_input_end_struct
;
671 v
->visitor
.start_list
= qobject_input_start_list
;
672 v
->visitor
.next_list
= qobject_input_next_list
;
673 v
->visitor
.check_list
= qobject_input_check_list
;
674 v
->visitor
.end_list
= qobject_input_end_list
;
675 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
676 v
->visitor
.optional
= qobject_input_optional
;
677 v
->visitor
.free
= qobject_input_free
;
679 v
->root
= qobject_ref(obj
);
684 Visitor
*qobject_input_visitor_new(QObject
*obj
)
686 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
688 v
->visitor
.type_int64
= qobject_input_type_int64
;
689 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
690 v
->visitor
.type_bool
= qobject_input_type_bool
;
691 v
->visitor
.type_str
= qobject_input_type_str
;
692 v
->visitor
.type_number
= qobject_input_type_number
;
693 v
->visitor
.type_any
= qobject_input_type_any
;
694 v
->visitor
.type_null
= qobject_input_type_null
;
699 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
701 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
703 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
704 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
705 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
706 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
707 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
708 v
->visitor
.type_any
= qobject_input_type_any
;
709 v
->visitor
.type_null
= qobject_input_type_null
;
710 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
716 Visitor
*qobject_input_visitor_new_str(const char *str
,
717 const char *implied_key
,
720 bool is_json
= str
[0] == '{';
726 obj
= qobject_from_json(str
, errp
);
730 args
= qobject_to(QDict
, obj
);
732 v
= qobject_input_visitor_new(QOBJECT(args
));
734 args
= keyval_parse(str
, implied_key
, errp
);
738 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));