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 switch (qobject_type(qobj
)) {
173 error_setg(errp
, "Parameters '%s.*' are unexpected",
174 full_name(qiv
, name
));
177 /* Non-string scalar (should this be an assertion?) */
178 error_setg(errp
, "Internal error: parameter %s invalid",
179 full_name(qiv
, name
));
184 return qstring_get_str(qstr
);
187 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
189 GHashTable
*h
= opaque
;
190 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
193 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
195 QObject
*obj
, void *qapi
)
198 StackObject
*tos
= g_new0(StackObject
, 1);
205 if (qobject_type(obj
) == QTYPE_QDICT
) {
206 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
207 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
210 assert(qobject_type(obj
) == QTYPE_QLIST
);
211 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
215 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
220 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
222 QObjectInputVisitor
*qiv
= to_qiv(v
);
223 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
227 assert(tos
&& !tos
->entry
);
229 g_hash_table_iter_init(&iter
, tos
->h
);
230 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
231 error_setg(errp
, "Parameter '%s' is unexpected",
232 full_name(qiv
, key
));
236 static void qobject_input_stack_object_free(StackObject
*tos
)
239 g_hash_table_unref(tos
->h
);
245 static void qobject_input_pop(Visitor
*v
, void **obj
)
247 QObjectInputVisitor
*qiv
= to_qiv(v
);
248 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
250 assert(tos
&& tos
->qapi
== obj
);
251 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
252 qobject_input_stack_object_free(tos
);
255 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
256 size_t size
, Error
**errp
)
258 QObjectInputVisitor
*qiv
= to_qiv(v
);
259 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
267 if (qobject_type(qobj
) != QTYPE_QDICT
) {
268 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
269 full_name(qiv
, name
), "object");
273 qobject_input_push(qiv
, name
, qobj
, obj
);
276 *obj
= g_malloc0(size
);
281 static void qobject_input_start_list(Visitor
*v
, const char *name
,
282 GenericList
**list
, size_t size
,
285 QObjectInputVisitor
*qiv
= to_qiv(v
);
286 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
287 const QListEntry
*entry
;
295 if (qobject_type(qobj
) != QTYPE_QLIST
) {
296 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
297 full_name(qiv
, name
), "array");
301 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
303 *list
= g_malloc0(size
);
307 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
310 QObjectInputVisitor
*qiv
= to_qiv(v
);
311 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
313 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
318 tail
->next
= g_malloc0(size
);
322 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
324 QObjectInputVisitor
*qiv
= to_qiv(v
);
325 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
327 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
330 error_setg(errp
, "Only %u list elements expected in %s",
331 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
336 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
337 GenericAlternate
**obj
, size_t size
,
338 bool promote_int
, Error
**errp
)
340 QObjectInputVisitor
*qiv
= to_qiv(v
);
341 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
347 *obj
= g_malloc0(size
);
348 (*obj
)->type
= qobject_type(qobj
);
349 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
350 (*obj
)->type
= QTYPE_QFLOAT
;
354 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
357 QObjectInputVisitor
*qiv
= to_qiv(v
);
358 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
364 qint
= qobject_to_qint(qobj
);
366 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
367 full_name(qiv
, name
), "integer");
371 *obj
= qint_get_int(qint
);
375 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
376 int64_t *obj
, Error
**errp
)
378 QObjectInputVisitor
*qiv
= to_qiv(v
);
379 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
385 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
386 /* TODO report -ERANGE more nicely */
387 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
388 full_name(qiv
, name
), "integer");
392 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
393 uint64_t *obj
, Error
**errp
)
395 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
396 QObjectInputVisitor
*qiv
= to_qiv(v
);
397 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
403 qint
= qobject_to_qint(qobj
);
405 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
406 full_name(qiv
, name
), "integer");
410 *obj
= qint_get_int(qint
);
413 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
414 uint64_t *obj
, Error
**errp
)
416 QObjectInputVisitor
*qiv
= to_qiv(v
);
417 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
423 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
424 /* TODO report -ERANGE more nicely */
425 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
426 full_name(qiv
, name
), "integer");
430 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
433 QObjectInputVisitor
*qiv
= to_qiv(v
);
434 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
440 qbool
= qobject_to_qbool(qobj
);
442 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
443 full_name(qiv
, name
), "boolean");
447 *obj
= qbool_get_bool(qbool
);
450 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
451 bool *obj
, Error
**errp
)
453 QObjectInputVisitor
*qiv
= to_qiv(v
);
454 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
460 if (!strcmp(str
, "on")) {
462 } else if (!strcmp(str
, "off")) {
465 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
466 full_name(qiv
, name
), "'on' or 'off'");
470 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
473 QObjectInputVisitor
*qiv
= to_qiv(v
);
474 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
481 qstr
= qobject_to_qstring(qobj
);
483 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
484 full_name(qiv
, name
), "string");
488 *obj
= g_strdup(qstring_get_str(qstr
));
491 static void qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
492 char **obj
, Error
**errp
)
494 QObjectInputVisitor
*qiv
= to_qiv(v
);
495 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
497 *obj
= g_strdup(str
);
500 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
503 QObjectInputVisitor
*qiv
= to_qiv(v
);
504 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
511 qint
= qobject_to_qint(qobj
);
513 *obj
= qint_get_int(qobject_to_qint(qobj
));
517 qfloat
= qobject_to_qfloat(qobj
);
519 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
523 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
524 full_name(qiv
, name
), "number");
527 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
528 double *obj
, Error
**errp
)
530 QObjectInputVisitor
*qiv
= to_qiv(v
);
531 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
539 *obj
= strtod(str
, &endp
);
540 if (errno
|| endp
== str
|| *endp
) {
541 /* TODO report -ERANGE more nicely */
542 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
543 full_name(qiv
, name
), "number");
547 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
550 QObjectInputVisitor
*qiv
= to_qiv(v
);
551 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
558 qobject_incref(qobj
);
562 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
564 QObjectInputVisitor
*qiv
= to_qiv(v
);
565 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
571 if (qobject_type(qobj
) != QTYPE_QNULL
) {
572 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
573 full_name(qiv
, name
), "null");
577 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
578 uint64_t *obj
, Error
**errp
)
580 QObjectInputVisitor
*qiv
= to_qiv(v
);
581 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
587 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
588 /* TODO report -ERANGE more nicely */
589 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
590 full_name(qiv
, name
), "size");
594 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
596 QObjectInputVisitor
*qiv
= to_qiv(v
);
597 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
607 static void qobject_input_free(Visitor
*v
)
609 QObjectInputVisitor
*qiv
= to_qiv(v
);
611 while (!QSLIST_EMPTY(&qiv
->stack
)) {
612 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
614 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
615 qobject_input_stack_object_free(tos
);
618 qobject_decref(qiv
->root
);
620 g_string_free(qiv
->errname
, TRUE
);
625 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
627 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
631 v
->visitor
.type
= VISITOR_INPUT
;
632 v
->visitor
.start_struct
= qobject_input_start_struct
;
633 v
->visitor
.check_struct
= qobject_input_check_struct
;
634 v
->visitor
.end_struct
= qobject_input_pop
;
635 v
->visitor
.start_list
= qobject_input_start_list
;
636 v
->visitor
.next_list
= qobject_input_next_list
;
637 v
->visitor
.check_list
= qobject_input_check_list
;
638 v
->visitor
.end_list
= qobject_input_pop
;
639 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
640 v
->visitor
.optional
= qobject_input_optional
;
641 v
->visitor
.free
= qobject_input_free
;
649 Visitor
*qobject_input_visitor_new(QObject
*obj
)
651 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
653 v
->visitor
.type_int64
= qobject_input_type_int64
;
654 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
655 v
->visitor
.type_bool
= qobject_input_type_bool
;
656 v
->visitor
.type_str
= qobject_input_type_str
;
657 v
->visitor
.type_number
= qobject_input_type_number
;
658 v
->visitor
.type_any
= qobject_input_type_any
;
659 v
->visitor
.type_null
= qobject_input_type_null
;
664 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
666 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
668 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
669 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
670 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
671 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
672 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
673 v
->visitor
.type_any
= qobject_input_type_any
;
674 v
->visitor
.type_null
= qobject_input_type_null
;
675 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
680 Visitor
*qobject_input_visitor_new_str(const char *str
,
681 const char *implied_key
,
684 bool is_json
= str
[0] == '{';
690 obj
= qobject_from_json(str
, errp
);
692 /* Work around qobject_from_json() lossage TODO fix that */
693 if (errp
&& !*errp
) {
694 error_setg(errp
, "JSON parse error");
699 args
= qobject_to_qdict(obj
);
701 v
= qobject_input_visitor_new(QOBJECT(args
));
703 args
= keyval_parse(str
, implied_key
, errp
);
707 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));