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. */
45 /* Stack of objects being visited (all entries will be either
47 QSLIST_HEAD(, StackObject
) stack
;
49 GString
*errname
; /* Accumulator for full_name() */
52 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
54 return container_of(v
, QObjectInputVisitor
, visitor
);
57 static const char *full_name_nth(QObjectInputVisitor
*qiv
, const char *name
,
64 g_string_truncate(qiv
->errname
, 0);
66 qiv
->errname
= g_string_new("");
69 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
72 } else if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
73 g_string_prepend(qiv
->errname
, name
?: "<anonymous>");
74 g_string_prepend_c(qiv
->errname
, '.');
76 snprintf(buf
, sizeof(buf
), "[%u]", so
->index
);
77 g_string_prepend(qiv
->errname
, buf
);
84 g_string_prepend(qiv
->errname
, name
);
85 } else if (qiv
->errname
->str
[0] == '.') {
86 g_string_erase(qiv
->errname
, 0, 1);
87 } else if (!qiv
->errname
->str
[0]) {
91 return qiv
->errname
->str
;
94 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
96 return full_name_nth(qiv
, name
, 0);
99 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
107 if (QSLIST_EMPTY(&qiv
->stack
)) {
108 /* Starting at root, name is ignored. */
113 /* We are in a container; find the next element. */
114 tos
= QSLIST_FIRST(&qiv
->stack
);
118 if (qobject_type(qobj
) == QTYPE_QDICT
) {
120 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
121 if (tos
->h
&& consume
&& ret
) {
122 bool removed
= g_hash_table_remove(tos
->h
, name
);
126 assert(qobject_type(qobj
) == QTYPE_QLIST
);
129 ret
= qlist_entry_obj(tos
->entry
);
131 tos
->entry
= qlist_next(tos
->entry
);
144 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
146 bool consume
, Error
**errp
)
148 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
151 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
156 static const char *qobject_input_get_keyval(QObjectInputVisitor
*qiv
,
163 qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
168 qstr
= qobject_to_qstring(qobj
);
170 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
171 full_name(qiv
, name
), "string");
175 return qstring_get_str(qstr
);
178 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
180 GHashTable
*h
= opaque
;
181 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
184 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
186 QObject
*obj
, void *qapi
)
189 StackObject
*tos
= g_new0(StackObject
, 1);
196 if (qobject_type(obj
) == QTYPE_QDICT
) {
197 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
198 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
201 assert(qobject_type(obj
) == QTYPE_QLIST
);
202 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
206 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
211 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
213 QObjectInputVisitor
*qiv
= to_qiv(v
);
214 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
218 assert(tos
&& !tos
->entry
);
220 g_hash_table_iter_init(&iter
, tos
->h
);
221 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
222 error_setg(errp
, "Parameter '%s' is unexpected",
223 full_name(qiv
, key
));
227 static void qobject_input_stack_object_free(StackObject
*tos
)
230 g_hash_table_unref(tos
->h
);
236 static void qobject_input_pop(Visitor
*v
, void **obj
)
238 QObjectInputVisitor
*qiv
= to_qiv(v
);
239 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
241 assert(tos
&& tos
->qapi
== obj
);
242 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
243 qobject_input_stack_object_free(tos
);
246 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
247 size_t size
, Error
**errp
)
249 QObjectInputVisitor
*qiv
= to_qiv(v
);
250 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
258 if (qobject_type(qobj
) != QTYPE_QDICT
) {
259 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
260 full_name(qiv
, name
), "object");
264 qobject_input_push(qiv
, name
, qobj
, obj
);
267 *obj
= g_malloc0(size
);
272 static void qobject_input_start_list(Visitor
*v
, const char *name
,
273 GenericList
**list
, size_t size
,
276 QObjectInputVisitor
*qiv
= to_qiv(v
);
277 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
278 const QListEntry
*entry
;
286 if (qobject_type(qobj
) != QTYPE_QLIST
) {
287 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
288 full_name(qiv
, name
), "array");
292 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
294 *list
= g_malloc0(size
);
298 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
301 QObjectInputVisitor
*qiv
= to_qiv(v
);
302 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
304 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
309 tail
->next
= g_malloc0(size
);
313 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
315 QObjectInputVisitor
*qiv
= to_qiv(v
);
316 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
318 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
321 error_setg(errp
, "Only %u list elements expected in %s",
322 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
327 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
328 GenericAlternate
**obj
, size_t size
,
329 bool promote_int
, Error
**errp
)
331 QObjectInputVisitor
*qiv
= to_qiv(v
);
332 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
338 *obj
= g_malloc0(size
);
339 (*obj
)->type
= qobject_type(qobj
);
340 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
341 (*obj
)->type
= QTYPE_QFLOAT
;
345 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
348 QObjectInputVisitor
*qiv
= to_qiv(v
);
349 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
355 qint
= qobject_to_qint(qobj
);
357 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
358 full_name(qiv
, name
), "integer");
362 *obj
= qint_get_int(qint
);
366 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
367 int64_t *obj
, Error
**errp
)
369 QObjectInputVisitor
*qiv
= to_qiv(v
);
370 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
376 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
377 /* TODO report -ERANGE more nicely */
378 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
379 full_name(qiv
, name
), "integer");
383 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
384 uint64_t *obj
, Error
**errp
)
386 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
387 QObjectInputVisitor
*qiv
= to_qiv(v
);
388 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
394 qint
= qobject_to_qint(qobj
);
396 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
397 full_name(qiv
, name
), "integer");
401 *obj
= qint_get_int(qint
);
404 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
405 uint64_t *obj
, Error
**errp
)
407 QObjectInputVisitor
*qiv
= to_qiv(v
);
408 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
414 if (qemu_strtou64(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_bool(Visitor
*v
, const char *name
, bool *obj
,
424 QObjectInputVisitor
*qiv
= to_qiv(v
);
425 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
431 qbool
= qobject_to_qbool(qobj
);
433 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
434 full_name(qiv
, name
), "boolean");
438 *obj
= qbool_get_bool(qbool
);
441 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
442 bool *obj
, Error
**errp
)
444 QObjectInputVisitor
*qiv
= to_qiv(v
);
445 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
451 if (!strcmp(str
, "on")) {
453 } else if (!strcmp(str
, "off")) {
456 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
457 full_name(qiv
, name
), "'on' or 'off'");
461 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
464 QObjectInputVisitor
*qiv
= to_qiv(v
);
465 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
472 qstr
= qobject_to_qstring(qobj
);
474 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
475 full_name(qiv
, name
), "string");
479 *obj
= g_strdup(qstring_get_str(qstr
));
482 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
485 QObjectInputVisitor
*qiv
= to_qiv(v
);
486 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
493 qint
= qobject_to_qint(qobj
);
495 *obj
= qint_get_int(qobject_to_qint(qobj
));
499 qfloat
= qobject_to_qfloat(qobj
);
501 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
505 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
506 full_name(qiv
, name
), "number");
509 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
510 double *obj
, Error
**errp
)
512 QObjectInputVisitor
*qiv
= to_qiv(v
);
513 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
521 *obj
= strtod(str
, &endp
);
522 if (errno
|| endp
== str
|| *endp
) {
523 /* TODO report -ERANGE more nicely */
524 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
525 full_name(qiv
, name
), "number");
529 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
532 QObjectInputVisitor
*qiv
= to_qiv(v
);
533 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
540 qobject_incref(qobj
);
544 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
546 QObjectInputVisitor
*qiv
= to_qiv(v
);
547 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
553 if (qobject_type(qobj
) != QTYPE_QNULL
) {
554 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
555 full_name(qiv
, name
), "null");
559 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
560 uint64_t *obj
, Error
**errp
)
562 QObjectInputVisitor
*qiv
= to_qiv(v
);
563 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
569 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
570 /* TODO report -ERANGE more nicely */
571 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
572 full_name(qiv
, name
), "size");
576 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
578 QObjectInputVisitor
*qiv
= to_qiv(v
);
579 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
589 static void qobject_input_free(Visitor
*v
)
591 QObjectInputVisitor
*qiv
= to_qiv(v
);
593 while (!QSLIST_EMPTY(&qiv
->stack
)) {
594 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
596 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
597 qobject_input_stack_object_free(tos
);
600 qobject_decref(qiv
->root
);
602 g_string_free(qiv
->errname
, TRUE
);
607 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
609 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
613 v
->visitor
.type
= VISITOR_INPUT
;
614 v
->visitor
.start_struct
= qobject_input_start_struct
;
615 v
->visitor
.check_struct
= qobject_input_check_struct
;
616 v
->visitor
.end_struct
= qobject_input_pop
;
617 v
->visitor
.start_list
= qobject_input_start_list
;
618 v
->visitor
.next_list
= qobject_input_next_list
;
619 v
->visitor
.check_list
= qobject_input_check_list
;
620 v
->visitor
.end_list
= qobject_input_pop
;
621 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
622 v
->visitor
.optional
= qobject_input_optional
;
623 v
->visitor
.free
= qobject_input_free
;
631 Visitor
*qobject_input_visitor_new(QObject
*obj
)
633 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
635 v
->visitor
.type_int64
= qobject_input_type_int64
;
636 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
637 v
->visitor
.type_bool
= qobject_input_type_bool
;
638 v
->visitor
.type_str
= qobject_input_type_str
;
639 v
->visitor
.type_number
= qobject_input_type_number
;
640 v
->visitor
.type_any
= qobject_input_type_any
;
641 v
->visitor
.type_null
= qobject_input_type_null
;
646 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
648 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
650 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
651 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
652 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
653 v
->visitor
.type_str
= qobject_input_type_str
;
654 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
655 v
->visitor
.type_any
= qobject_input_type_any
;
656 v
->visitor
.type_null
= qobject_input_type_null
;
657 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
662 Visitor
*qobject_input_visitor_new_str(const char *str
,
663 const char *implied_key
,
666 bool is_json
= str
[0] == '{';
672 obj
= qobject_from_json(str
, errp
);
674 /* Work around qobject_from_json() lossage TODO fix that */
675 if (errp
&& !*errp
) {
676 error_setg(errp
, "JSON parse error");
681 args
= qobject_to_qdict(obj
);
683 v
= qobject_input_visitor_new(QOBJECT(args
));
685 args
= keyval_parse(str
, implied_key
, errp
);
689 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));