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"
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/qjson.h"
22 #include "qapi/qmp/types.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qemu/cutils.h"
25 #include "qemu/option.h"
27 typedef struct StackObject
{
28 const char *name
; /* Name of @obj in its parent, if any */
29 QObject
*obj
; /* QDict or QList being visited */
30 void *qapi
; /* sanity check that caller uses same pointer */
32 GHashTable
*h
; /* If @obj is QDict: unvisited keys */
33 const QListEntry
*entry
; /* If @obj is QList: unvisited tail */
34 unsigned index
; /* If @obj is QList: list index of @entry */
36 QSLIST_ENTRY(StackObject
) node
; /* parent */
39 struct QObjectInputVisitor
{
42 /* Root of visit at visitor creation. */
44 bool keyval
; /* Assume @root made with keyval_parse() */
46 /* Stack of objects being visited (all entries will be either
48 QSLIST_HEAD(, StackObject
) stack
;
50 GString
*errname
; /* Accumulator for full_name() */
53 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
55 return container_of(v
, QObjectInputVisitor
, visitor
);
59 * Find the full name of something @qiv is currently visiting.
60 * @qiv is visiting something named @name in the stack of containers
62 * If @n is zero, return its full name.
63 * If @n is positive, return the full name of the @n-th container
64 * counting from the top. The stack of containers must have at least
66 * The returned string is valid until the next full_name_nth(@v) or
69 static const char *full_name_nth(QObjectInputVisitor
*qiv
, const char *name
,
76 g_string_truncate(qiv
->errname
, 0);
78 qiv
->errname
= g_string_new("");
81 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
84 } else if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
85 g_string_prepend(qiv
->errname
, name
?: "<anonymous>");
86 g_string_prepend_c(qiv
->errname
, '.');
88 snprintf(buf
, sizeof(buf
),
89 qiv
->keyval
? ".%u" : "[%u]",
91 g_string_prepend(qiv
->errname
, buf
);
98 g_string_prepend(qiv
->errname
, name
);
99 } else if (qiv
->errname
->str
[0] == '.') {
100 g_string_erase(qiv
->errname
, 0, 1);
101 } else if (!qiv
->errname
->str
[0]) {
102 return "<anonymous>";
105 return qiv
->errname
->str
;
108 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
110 return full_name_nth(qiv
, name
, 0);
113 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
121 if (QSLIST_EMPTY(&qiv
->stack
)) {
122 /* Starting at root, name is ignored. */
127 /* We are in a container; find the next element. */
128 tos
= QSLIST_FIRST(&qiv
->stack
);
132 if (qobject_type(qobj
) == QTYPE_QDICT
) {
134 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
135 if (tos
->h
&& consume
&& ret
) {
136 bool removed
= g_hash_table_remove(tos
->h
, name
);
140 assert(qobject_type(qobj
) == QTYPE_QLIST
);
143 ret
= qlist_entry_obj(tos
->entry
);
145 tos
->entry
= qlist_next(tos
->entry
);
158 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
160 bool consume
, Error
**errp
)
162 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
165 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
170 static const char *qobject_input_get_keyval(QObjectInputVisitor
*qiv
,
177 qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
182 qstr
= qobject_to_qstring(qobj
);
184 switch (qobject_type(qobj
)) {
187 error_setg(errp
, "Parameters '%s.*' are unexpected",
188 full_name(qiv
, name
));
191 /* Non-string scalar (should this be an assertion?) */
192 error_setg(errp
, "Internal error: parameter %s invalid",
193 full_name(qiv
, name
));
198 return qstring_get_str(qstr
);
201 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
203 GHashTable
*h
= opaque
;
204 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
207 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
209 QObject
*obj
, void *qapi
)
212 StackObject
*tos
= g_new0(StackObject
, 1);
219 if (qobject_type(obj
) == QTYPE_QDICT
) {
220 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
221 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
224 assert(qobject_type(obj
) == QTYPE_QLIST
);
225 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
229 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
234 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
236 QObjectInputVisitor
*qiv
= to_qiv(v
);
237 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
241 assert(tos
&& !tos
->entry
);
243 g_hash_table_iter_init(&iter
, tos
->h
);
244 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
245 error_setg(errp
, "Parameter '%s' is unexpected",
246 full_name(qiv
, key
));
250 static void qobject_input_stack_object_free(StackObject
*tos
)
253 g_hash_table_unref(tos
->h
);
259 static void qobject_input_pop(Visitor
*v
, void **obj
)
261 QObjectInputVisitor
*qiv
= to_qiv(v
);
262 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
264 assert(tos
&& tos
->qapi
== obj
);
265 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
266 qobject_input_stack_object_free(tos
);
269 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
270 size_t size
, Error
**errp
)
272 QObjectInputVisitor
*qiv
= to_qiv(v
);
273 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
281 if (qobject_type(qobj
) != QTYPE_QDICT
) {
282 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
283 full_name(qiv
, name
), "object");
287 qobject_input_push(qiv
, name
, qobj
, obj
);
290 *obj
= g_malloc0(size
);
294 static void qobject_input_end_struct(Visitor
*v
, void **obj
)
296 QObjectInputVisitor
*qiv
= to_qiv(v
);
297 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
299 assert(qobject_type(tos
->obj
) == QTYPE_QDICT
&& tos
->h
);
300 qobject_input_pop(v
, obj
);
304 static void qobject_input_start_list(Visitor
*v
, const char *name
,
305 GenericList
**list
, size_t size
,
308 QObjectInputVisitor
*qiv
= to_qiv(v
);
309 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
310 const QListEntry
*entry
;
318 if (qobject_type(qobj
) != QTYPE_QLIST
) {
319 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
320 full_name(qiv
, name
), "array");
324 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
326 *list
= g_malloc0(size
);
330 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
333 QObjectInputVisitor
*qiv
= to_qiv(v
);
334 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
336 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
341 tail
->next
= g_malloc0(size
);
345 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
347 QObjectInputVisitor
*qiv
= to_qiv(v
);
348 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
350 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
353 error_setg(errp
, "Only %u list elements expected in %s",
354 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
358 static void qobject_input_end_list(Visitor
*v
, void **obj
)
360 QObjectInputVisitor
*qiv
= to_qiv(v
);
361 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
363 assert(qobject_type(tos
->obj
) == QTYPE_QLIST
&& !tos
->h
);
364 qobject_input_pop(v
, obj
);
367 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
368 GenericAlternate
**obj
, size_t size
,
369 bool promote_int
, Error
**errp
)
371 QObjectInputVisitor
*qiv
= to_qiv(v
);
372 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
378 *obj
= g_malloc0(size
);
379 (*obj
)->type
= qobject_type(qobj
);
380 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
381 (*obj
)->type
= QTYPE_QFLOAT
;
385 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
388 QObjectInputVisitor
*qiv
= to_qiv(v
);
389 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
395 qint
= qobject_to_qint(qobj
);
397 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
398 full_name(qiv
, name
), "integer");
402 *obj
= qint_get_int(qint
);
406 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
407 int64_t *obj
, Error
**errp
)
409 QObjectInputVisitor
*qiv
= to_qiv(v
);
410 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
416 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
417 /* TODO report -ERANGE more nicely */
418 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
419 full_name(qiv
, name
), "integer");
423 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
424 uint64_t *obj
, Error
**errp
)
426 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
427 QObjectInputVisitor
*qiv
= to_qiv(v
);
428 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
434 qint
= qobject_to_qint(qobj
);
436 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
437 full_name(qiv
, name
), "integer");
441 *obj
= qint_get_int(qint
);
444 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
445 uint64_t *obj
, Error
**errp
)
447 QObjectInputVisitor
*qiv
= to_qiv(v
);
448 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
454 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
455 /* TODO report -ERANGE more nicely */
456 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
457 full_name(qiv
, name
), "integer");
461 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
464 QObjectInputVisitor
*qiv
= to_qiv(v
);
465 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
471 qbool
= qobject_to_qbool(qobj
);
473 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
474 full_name(qiv
, name
), "boolean");
478 *obj
= qbool_get_bool(qbool
);
481 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
482 bool *obj
, Error
**errp
)
484 QObjectInputVisitor
*qiv
= to_qiv(v
);
485 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
491 if (!strcmp(str
, "on")) {
493 } else if (!strcmp(str
, "off")) {
496 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
497 full_name(qiv
, name
), "'on' or 'off'");
501 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
504 QObjectInputVisitor
*qiv
= to_qiv(v
);
505 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
512 qstr
= qobject_to_qstring(qobj
);
514 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
515 full_name(qiv
, name
), "string");
519 *obj
= g_strdup(qstring_get_str(qstr
));
522 static void qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
523 char **obj
, Error
**errp
)
525 QObjectInputVisitor
*qiv
= to_qiv(v
);
526 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
528 *obj
= g_strdup(str
);
531 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
534 QObjectInputVisitor
*qiv
= to_qiv(v
);
535 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
542 qint
= qobject_to_qint(qobj
);
544 *obj
= qint_get_int(qobject_to_qint(qobj
));
548 qfloat
= qobject_to_qfloat(qobj
);
550 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
554 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
555 full_name(qiv
, name
), "number");
558 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
559 double *obj
, Error
**errp
)
561 QObjectInputVisitor
*qiv
= to_qiv(v
);
562 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
570 *obj
= strtod(str
, &endp
);
571 if (errno
|| endp
== str
|| *endp
) {
572 /* TODO report -ERANGE more nicely */
573 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
574 full_name(qiv
, name
), "number");
578 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
581 QObjectInputVisitor
*qiv
= to_qiv(v
);
582 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
589 qobject_incref(qobj
);
593 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
595 QObjectInputVisitor
*qiv
= to_qiv(v
);
596 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
602 if (qobject_type(qobj
) != QTYPE_QNULL
) {
603 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
604 full_name(qiv
, name
), "null");
608 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
609 uint64_t *obj
, Error
**errp
)
611 QObjectInputVisitor
*qiv
= to_qiv(v
);
612 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
618 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
619 /* TODO report -ERANGE more nicely */
620 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
621 full_name(qiv
, name
), "size");
625 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
627 QObjectInputVisitor
*qiv
= to_qiv(v
);
628 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
638 static void qobject_input_free(Visitor
*v
)
640 QObjectInputVisitor
*qiv
= to_qiv(v
);
642 while (!QSLIST_EMPTY(&qiv
->stack
)) {
643 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
645 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
646 qobject_input_stack_object_free(tos
);
649 qobject_decref(qiv
->root
);
651 g_string_free(qiv
->errname
, TRUE
);
656 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
658 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
662 v
->visitor
.type
= VISITOR_INPUT
;
663 v
->visitor
.start_struct
= qobject_input_start_struct
;
664 v
->visitor
.check_struct
= qobject_input_check_struct
;
665 v
->visitor
.end_struct
= qobject_input_end_struct
;
666 v
->visitor
.start_list
= qobject_input_start_list
;
667 v
->visitor
.next_list
= qobject_input_next_list
;
668 v
->visitor
.check_list
= qobject_input_check_list
;
669 v
->visitor
.end_list
= qobject_input_end_list
;
670 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
671 v
->visitor
.optional
= qobject_input_optional
;
672 v
->visitor
.free
= qobject_input_free
;
680 Visitor
*qobject_input_visitor_new(QObject
*obj
)
682 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
684 v
->visitor
.type_int64
= qobject_input_type_int64
;
685 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
686 v
->visitor
.type_bool
= qobject_input_type_bool
;
687 v
->visitor
.type_str
= qobject_input_type_str
;
688 v
->visitor
.type_number
= qobject_input_type_number
;
689 v
->visitor
.type_any
= qobject_input_type_any
;
690 v
->visitor
.type_null
= qobject_input_type_null
;
695 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
697 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
699 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
700 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
701 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
702 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
703 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
704 v
->visitor
.type_any
= qobject_input_type_any
;
705 v
->visitor
.type_null
= qobject_input_type_null
;
706 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
712 Visitor
*qobject_input_visitor_new_str(const char *str
,
713 const char *implied_key
,
716 bool is_json
= str
[0] == '{';
722 obj
= qobject_from_json(str
, errp
);
724 /* Work around qobject_from_json() lossage TODO fix that */
725 if (errp
&& !*errp
) {
726 error_setg(errp
, "JSON parse error");
731 args
= qobject_to_qdict(obj
);
733 v
= qobject_input_visitor_new(QOBJECT(args
));
735 args
= keyval_parse(str
, implied_key
, errp
);
739 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));