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
);
295 static void qobject_input_start_list(Visitor
*v
, const char *name
,
296 GenericList
**list
, size_t size
,
299 QObjectInputVisitor
*qiv
= to_qiv(v
);
300 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
301 const QListEntry
*entry
;
309 if (qobject_type(qobj
) != QTYPE_QLIST
) {
310 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
311 full_name(qiv
, name
), "array");
315 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
317 *list
= g_malloc0(size
);
321 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
324 QObjectInputVisitor
*qiv
= to_qiv(v
);
325 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
327 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
332 tail
->next
= g_malloc0(size
);
336 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
338 QObjectInputVisitor
*qiv
= to_qiv(v
);
339 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
341 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
344 error_setg(errp
, "Only %u list elements expected in %s",
345 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
350 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
351 GenericAlternate
**obj
, size_t size
,
352 bool promote_int
, Error
**errp
)
354 QObjectInputVisitor
*qiv
= to_qiv(v
);
355 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
361 *obj
= g_malloc0(size
);
362 (*obj
)->type
= qobject_type(qobj
);
363 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
364 (*obj
)->type
= QTYPE_QFLOAT
;
368 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
371 QObjectInputVisitor
*qiv
= to_qiv(v
);
372 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
378 qint
= qobject_to_qint(qobj
);
380 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
381 full_name(qiv
, name
), "integer");
385 *obj
= qint_get_int(qint
);
389 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
390 int64_t *obj
, Error
**errp
)
392 QObjectInputVisitor
*qiv
= to_qiv(v
);
393 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
399 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
400 /* TODO report -ERANGE more nicely */
401 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
402 full_name(qiv
, name
), "integer");
406 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
407 uint64_t *obj
, Error
**errp
)
409 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
410 QObjectInputVisitor
*qiv
= to_qiv(v
);
411 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
417 qint
= qobject_to_qint(qobj
);
419 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
420 full_name(qiv
, name
), "integer");
424 *obj
= qint_get_int(qint
);
427 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
428 uint64_t *obj
, Error
**errp
)
430 QObjectInputVisitor
*qiv
= to_qiv(v
);
431 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
437 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
438 /* TODO report -ERANGE more nicely */
439 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
440 full_name(qiv
, name
), "integer");
444 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
447 QObjectInputVisitor
*qiv
= to_qiv(v
);
448 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
454 qbool
= qobject_to_qbool(qobj
);
456 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
457 full_name(qiv
, name
), "boolean");
461 *obj
= qbool_get_bool(qbool
);
464 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
465 bool *obj
, Error
**errp
)
467 QObjectInputVisitor
*qiv
= to_qiv(v
);
468 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
474 if (!strcmp(str
, "on")) {
476 } else if (!strcmp(str
, "off")) {
479 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
480 full_name(qiv
, name
), "'on' or 'off'");
484 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
487 QObjectInputVisitor
*qiv
= to_qiv(v
);
488 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
495 qstr
= qobject_to_qstring(qobj
);
497 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
498 full_name(qiv
, name
), "string");
502 *obj
= g_strdup(qstring_get_str(qstr
));
505 static void qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
506 char **obj
, Error
**errp
)
508 QObjectInputVisitor
*qiv
= to_qiv(v
);
509 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
511 *obj
= g_strdup(str
);
514 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
517 QObjectInputVisitor
*qiv
= to_qiv(v
);
518 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
525 qint
= qobject_to_qint(qobj
);
527 *obj
= qint_get_int(qobject_to_qint(qobj
));
531 qfloat
= qobject_to_qfloat(qobj
);
533 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
537 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
538 full_name(qiv
, name
), "number");
541 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
542 double *obj
, Error
**errp
)
544 QObjectInputVisitor
*qiv
= to_qiv(v
);
545 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
553 *obj
= strtod(str
, &endp
);
554 if (errno
|| endp
== str
|| *endp
) {
555 /* TODO report -ERANGE more nicely */
556 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
557 full_name(qiv
, name
), "number");
561 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
564 QObjectInputVisitor
*qiv
= to_qiv(v
);
565 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
572 qobject_incref(qobj
);
576 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
578 QObjectInputVisitor
*qiv
= to_qiv(v
);
579 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
585 if (qobject_type(qobj
) != QTYPE_QNULL
) {
586 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
587 full_name(qiv
, name
), "null");
591 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
592 uint64_t *obj
, Error
**errp
)
594 QObjectInputVisitor
*qiv
= to_qiv(v
);
595 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
601 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
602 /* TODO report -ERANGE more nicely */
603 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
604 full_name(qiv
, name
), "size");
608 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
610 QObjectInputVisitor
*qiv
= to_qiv(v
);
611 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
621 static void qobject_input_free(Visitor
*v
)
623 QObjectInputVisitor
*qiv
= to_qiv(v
);
625 while (!QSLIST_EMPTY(&qiv
->stack
)) {
626 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
628 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
629 qobject_input_stack_object_free(tos
);
632 qobject_decref(qiv
->root
);
634 g_string_free(qiv
->errname
, TRUE
);
639 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
641 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
645 v
->visitor
.type
= VISITOR_INPUT
;
646 v
->visitor
.start_struct
= qobject_input_start_struct
;
647 v
->visitor
.check_struct
= qobject_input_check_struct
;
648 v
->visitor
.end_struct
= qobject_input_pop
;
649 v
->visitor
.start_list
= qobject_input_start_list
;
650 v
->visitor
.next_list
= qobject_input_next_list
;
651 v
->visitor
.check_list
= qobject_input_check_list
;
652 v
->visitor
.end_list
= qobject_input_pop
;
653 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
654 v
->visitor
.optional
= qobject_input_optional
;
655 v
->visitor
.free
= qobject_input_free
;
663 Visitor
*qobject_input_visitor_new(QObject
*obj
)
665 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
667 v
->visitor
.type_int64
= qobject_input_type_int64
;
668 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
669 v
->visitor
.type_bool
= qobject_input_type_bool
;
670 v
->visitor
.type_str
= qobject_input_type_str
;
671 v
->visitor
.type_number
= qobject_input_type_number
;
672 v
->visitor
.type_any
= qobject_input_type_any
;
673 v
->visitor
.type_null
= qobject_input_type_null
;
678 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
680 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
682 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
683 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
684 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
685 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
686 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
687 v
->visitor
.type_any
= qobject_input_type_any
;
688 v
->visitor
.type_null
= qobject_input_type_null
;
689 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
695 Visitor
*qobject_input_visitor_new_str(const char *str
,
696 const char *implied_key
,
699 bool is_json
= str
[0] == '{';
705 obj
= qobject_from_json(str
, errp
);
707 /* Work around qobject_from_json() lossage TODO fix that */
708 if (errp
&& !*errp
) {
709 error_setg(errp
, "JSON parse error");
714 args
= qobject_to_qdict(obj
);
716 v
= qobject_input_visitor_new(QOBJECT(args
));
718 args
= keyval_parse(str
, implied_key
, errp
);
722 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));