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 "qemu-common.h"
22 #include "qapi/qmp/qjson.h"
23 #include "qapi/qmp/types.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qemu/cutils.h"
26 #include "qemu/option.h"
28 typedef struct StackObject
{
29 const char *name
; /* Name of @obj in its parent, if any */
30 QObject
*obj
; /* QDict or QList being visited */
31 void *qapi
; /* sanity check that caller uses same pointer */
33 GHashTable
*h
; /* If @obj is QDict: unvisited keys */
34 const QListEntry
*entry
; /* If @obj is QList: unvisited tail */
35 unsigned index
; /* If @obj is QList: list index of @entry */
37 QSLIST_ENTRY(StackObject
) node
; /* parent */
40 struct QObjectInputVisitor
{
43 /* Root of visit at visitor creation. */
45 bool keyval
; /* Assume @root made with keyval_parse() */
47 /* Stack of objects being visited (all entries will be either
49 QSLIST_HEAD(, StackObject
) stack
;
51 GString
*errname
; /* Accumulator for full_name() */
54 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
56 return container_of(v
, QObjectInputVisitor
, visitor
);
60 * Find the full name of something @qiv is currently visiting.
61 * @qiv is visiting something named @name in the stack of containers
63 * If @n is zero, return its full name.
64 * If @n is positive, return the full name of the @n-th container
65 * counting from the top. The stack of containers must have at least
67 * The returned string is valid until the next full_name_nth(@v) or
70 static const char *full_name_nth(QObjectInputVisitor
*qiv
, const char *name
,
77 g_string_truncate(qiv
->errname
, 0);
79 qiv
->errname
= g_string_new("");
82 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
85 } else if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
86 g_string_prepend(qiv
->errname
, name
?: "<anonymous>");
87 g_string_prepend_c(qiv
->errname
, '.');
89 snprintf(buf
, sizeof(buf
),
90 qiv
->keyval
? ".%u" : "[%u]",
92 g_string_prepend(qiv
->errname
, buf
);
99 g_string_prepend(qiv
->errname
, name
);
100 } else if (qiv
->errname
->str
[0] == '.') {
101 g_string_erase(qiv
->errname
, 0, 1);
102 } else if (!qiv
->errname
->str
[0]) {
103 return "<anonymous>";
106 return qiv
->errname
->str
;
109 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
111 return full_name_nth(qiv
, name
, 0);
114 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
122 if (QSLIST_EMPTY(&qiv
->stack
)) {
123 /* Starting at root, name is ignored. */
128 /* We are in a container; find the next element. */
129 tos
= QSLIST_FIRST(&qiv
->stack
);
133 if (qobject_type(qobj
) == QTYPE_QDICT
) {
135 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
136 if (tos
->h
&& consume
&& ret
) {
137 bool removed
= g_hash_table_remove(tos
->h
, name
);
141 assert(qobject_type(qobj
) == QTYPE_QLIST
);
144 ret
= qlist_entry_obj(tos
->entry
);
146 tos
->entry
= qlist_next(tos
->entry
);
159 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
161 bool consume
, Error
**errp
)
163 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
166 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
171 static const char *qobject_input_get_keyval(QObjectInputVisitor
*qiv
,
178 qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
183 qstr
= qobject_to_qstring(qobj
);
185 switch (qobject_type(qobj
)) {
188 error_setg(errp
, "Parameters '%s.*' are unexpected",
189 full_name(qiv
, name
));
192 /* Non-string scalar (should this be an assertion?) */
193 error_setg(errp
, "Internal error: parameter %s invalid",
194 full_name(qiv
, name
));
199 return qstring_get_str(qstr
);
202 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
204 GHashTable
*h
= opaque
;
205 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
208 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
210 QObject
*obj
, void *qapi
)
213 StackObject
*tos
= g_new0(StackObject
, 1);
220 if (qobject_type(obj
) == QTYPE_QDICT
) {
221 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
222 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
225 assert(qobject_type(obj
) == QTYPE_QLIST
);
226 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
230 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
235 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
237 QObjectInputVisitor
*qiv
= to_qiv(v
);
238 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
242 assert(tos
&& !tos
->entry
);
244 g_hash_table_iter_init(&iter
, tos
->h
);
245 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
246 error_setg(errp
, "Parameter '%s' is unexpected",
247 full_name(qiv
, key
));
251 static void qobject_input_stack_object_free(StackObject
*tos
)
254 g_hash_table_unref(tos
->h
);
260 static void qobject_input_pop(Visitor
*v
, void **obj
)
262 QObjectInputVisitor
*qiv
= to_qiv(v
);
263 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
265 assert(tos
&& tos
->qapi
== obj
);
266 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
267 qobject_input_stack_object_free(tos
);
270 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
271 size_t size
, Error
**errp
)
273 QObjectInputVisitor
*qiv
= to_qiv(v
);
274 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
282 if (qobject_type(qobj
) != QTYPE_QDICT
) {
283 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
284 full_name(qiv
, name
), "object");
288 qobject_input_push(qiv
, name
, qobj
, obj
);
291 *obj
= g_malloc0(size
);
295 static void qobject_input_end_struct(Visitor
*v
, void **obj
)
297 QObjectInputVisitor
*qiv
= to_qiv(v
);
298 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
300 assert(qobject_type(tos
->obj
) == QTYPE_QDICT
&& tos
->h
);
301 qobject_input_pop(v
, obj
);
305 static void qobject_input_start_list(Visitor
*v
, const char *name
,
306 GenericList
**list
, size_t size
,
309 QObjectInputVisitor
*qiv
= to_qiv(v
);
310 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
311 const QListEntry
*entry
;
319 if (qobject_type(qobj
) != QTYPE_QLIST
) {
320 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
321 full_name(qiv
, name
), "array");
325 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
327 *list
= g_malloc0(size
);
331 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
334 QObjectInputVisitor
*qiv
= to_qiv(v
);
335 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
337 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
342 tail
->next
= g_malloc0(size
);
346 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
348 QObjectInputVisitor
*qiv
= to_qiv(v
);
349 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
351 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
354 error_setg(errp
, "Only %u list elements expected in %s",
355 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
359 static void qobject_input_end_list(Visitor
*v
, void **obj
)
361 QObjectInputVisitor
*qiv
= to_qiv(v
);
362 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
364 assert(qobject_type(tos
->obj
) == QTYPE_QLIST
&& !tos
->h
);
365 qobject_input_pop(v
, obj
);
368 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
369 GenericAlternate
**obj
, size_t size
,
372 QObjectInputVisitor
*qiv
= to_qiv(v
);
373 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
379 *obj
= g_malloc0(size
);
380 (*obj
)->type
= qobject_type(qobj
);
383 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
386 QObjectInputVisitor
*qiv
= to_qiv(v
);
387 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
393 qnum
= qobject_to_qnum(qobj
);
394 if (!qnum
|| !qnum_get_try_int(qnum
, obj
)) {
395 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
396 full_name(qiv
, name
), "integer");
400 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
401 int64_t *obj
, Error
**errp
)
403 QObjectInputVisitor
*qiv
= to_qiv(v
);
404 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
410 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
411 /* TODO report -ERANGE more nicely */
412 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
413 full_name(qiv
, name
), "integer");
417 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
418 uint64_t *obj
, Error
**errp
)
420 QObjectInputVisitor
*qiv
= to_qiv(v
);
421 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
428 qnum
= qobject_to_qnum(qobj
);
433 if (qnum_get_try_uint(qnum
, obj
)) {
437 /* Need to accept negative values for backward compatibility */
438 if (qnum_get_try_int(qnum
, &val
)) {
444 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
445 full_name(qiv
, name
), "uint64");
448 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
449 uint64_t *obj
, Error
**errp
)
451 QObjectInputVisitor
*qiv
= to_qiv(v
);
452 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
458 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
459 /* TODO report -ERANGE more nicely */
460 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
461 full_name(qiv
, name
), "integer");
465 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
468 QObjectInputVisitor
*qiv
= to_qiv(v
);
469 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
475 qbool
= qobject_to_qbool(qobj
);
477 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
478 full_name(qiv
, name
), "boolean");
482 *obj
= qbool_get_bool(qbool
);
485 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
486 bool *obj
, Error
**errp
)
488 QObjectInputVisitor
*qiv
= to_qiv(v
);
489 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
495 if (!strcmp(str
, "on")) {
497 } else if (!strcmp(str
, "off")) {
500 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
501 full_name(qiv
, name
), "'on' or 'off'");
505 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
508 QObjectInputVisitor
*qiv
= to_qiv(v
);
509 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
516 qstr
= qobject_to_qstring(qobj
);
518 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
519 full_name(qiv
, name
), "string");
523 *obj
= g_strdup(qstring_get_str(qstr
));
526 static void qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
527 char **obj
, Error
**errp
)
529 QObjectInputVisitor
*qiv
= to_qiv(v
);
530 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
532 *obj
= g_strdup(str
);
535 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
538 QObjectInputVisitor
*qiv
= to_qiv(v
);
539 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
545 qnum
= qobject_to_qnum(qobj
);
547 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
548 full_name(qiv
, name
), "number");
552 *obj
= qnum_get_double(qnum
);
555 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
556 double *obj
, Error
**errp
)
558 QObjectInputVisitor
*qiv
= to_qiv(v
);
559 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
567 *obj
= strtod(str
, &endp
);
568 if (errno
|| endp
== str
|| *endp
|| !isfinite(*obj
)) {
569 /* TODO report -ERANGE more nicely */
570 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
571 full_name(qiv
, name
), "number");
575 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
578 QObjectInputVisitor
*qiv
= to_qiv(v
);
579 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
586 qobject_incref(qobj
);
590 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
592 QObjectInputVisitor
*qiv
= to_qiv(v
);
593 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
599 if (qobject_type(qobj
) != QTYPE_QNULL
) {
600 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
601 full_name(qiv
, name
), "null");
605 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
606 uint64_t *obj
, Error
**errp
)
608 QObjectInputVisitor
*qiv
= to_qiv(v
);
609 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
615 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
616 /* TODO report -ERANGE more nicely */
617 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
618 full_name(qiv
, name
), "size");
622 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
624 QObjectInputVisitor
*qiv
= to_qiv(v
);
625 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
635 static void qobject_input_free(Visitor
*v
)
637 QObjectInputVisitor
*qiv
= to_qiv(v
);
639 while (!QSLIST_EMPTY(&qiv
->stack
)) {
640 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
642 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
643 qobject_input_stack_object_free(tos
);
646 qobject_decref(qiv
->root
);
648 g_string_free(qiv
->errname
, TRUE
);
653 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
655 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
659 v
->visitor
.type
= VISITOR_INPUT
;
660 v
->visitor
.start_struct
= qobject_input_start_struct
;
661 v
->visitor
.check_struct
= qobject_input_check_struct
;
662 v
->visitor
.end_struct
= qobject_input_end_struct
;
663 v
->visitor
.start_list
= qobject_input_start_list
;
664 v
->visitor
.next_list
= qobject_input_next_list
;
665 v
->visitor
.check_list
= qobject_input_check_list
;
666 v
->visitor
.end_list
= qobject_input_end_list
;
667 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
668 v
->visitor
.optional
= qobject_input_optional
;
669 v
->visitor
.free
= qobject_input_free
;
677 Visitor
*qobject_input_visitor_new(QObject
*obj
)
679 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
681 v
->visitor
.type_int64
= qobject_input_type_int64
;
682 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
683 v
->visitor
.type_bool
= qobject_input_type_bool
;
684 v
->visitor
.type_str
= qobject_input_type_str
;
685 v
->visitor
.type_number
= qobject_input_type_number
;
686 v
->visitor
.type_any
= qobject_input_type_any
;
687 v
->visitor
.type_null
= qobject_input_type_null
;
692 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
694 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
696 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
697 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
698 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
699 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
700 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
701 v
->visitor
.type_any
= qobject_input_type_any
;
702 v
->visitor
.type_null
= qobject_input_type_null
;
703 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
709 Visitor
*qobject_input_visitor_new_str(const char *str
,
710 const char *implied_key
,
713 bool is_json
= str
[0] == '{';
719 obj
= qobject_from_json(str
, errp
);
721 /* Work around qobject_from_json() lossage TODO fix that */
722 if (errp
&& !*errp
) {
723 error_setg(errp
, "JSON parse error");
728 args
= qobject_to_qdict(obj
);
730 v
= qobject_input_visitor_new(QOBJECT(args
));
732 args
= keyval_parse(str
, implied_key
, errp
);
736 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));