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
);
58 static const char *full_name_nth(QObjectInputVisitor
*qiv
, const char *name
,
65 g_string_truncate(qiv
->errname
, 0);
67 qiv
->errname
= g_string_new("");
70 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
73 } else if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
74 g_string_prepend(qiv
->errname
, name
?: "<anonymous>");
75 g_string_prepend_c(qiv
->errname
, '.');
77 snprintf(buf
, sizeof(buf
),
78 qiv
->keyval
? ".%u" : "[%u]",
80 g_string_prepend(qiv
->errname
, buf
);
87 g_string_prepend(qiv
->errname
, name
);
88 } else if (qiv
->errname
->str
[0] == '.') {
89 g_string_erase(qiv
->errname
, 0, 1);
90 } else if (!qiv
->errname
->str
[0]) {
94 return qiv
->errname
->str
;
97 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
99 return full_name_nth(qiv
, name
, 0);
102 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
110 if (QSLIST_EMPTY(&qiv
->stack
)) {
111 /* Starting at root, name is ignored. */
116 /* We are in a container; find the next element. */
117 tos
= QSLIST_FIRST(&qiv
->stack
);
121 if (qobject_type(qobj
) == QTYPE_QDICT
) {
123 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
124 if (tos
->h
&& consume
&& ret
) {
125 bool removed
= g_hash_table_remove(tos
->h
, name
);
129 assert(qobject_type(qobj
) == QTYPE_QLIST
);
132 ret
= qlist_entry_obj(tos
->entry
);
134 tos
->entry
= qlist_next(tos
->entry
);
147 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
149 bool consume
, Error
**errp
)
151 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
154 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
159 static const char *qobject_input_get_keyval(QObjectInputVisitor
*qiv
,
166 qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
171 qstr
= qobject_to_qstring(qobj
);
173 switch (qobject_type(qobj
)) {
176 error_setg(errp
, "Parameters '%s.*' are unexpected",
177 full_name(qiv
, name
));
180 /* Non-string scalar (should this be an assertion?) */
181 error_setg(errp
, "Internal error: parameter %s invalid",
182 full_name(qiv
, name
));
187 return qstring_get_str(qstr
);
190 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
192 GHashTable
*h
= opaque
;
193 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
196 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
198 QObject
*obj
, void *qapi
)
201 StackObject
*tos
= g_new0(StackObject
, 1);
208 if (qobject_type(obj
) == QTYPE_QDICT
) {
209 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
210 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
213 assert(qobject_type(obj
) == QTYPE_QLIST
);
214 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
218 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
223 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
225 QObjectInputVisitor
*qiv
= to_qiv(v
);
226 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
230 assert(tos
&& !tos
->entry
);
232 g_hash_table_iter_init(&iter
, tos
->h
);
233 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
234 error_setg(errp
, "Parameter '%s' is unexpected",
235 full_name(qiv
, key
));
239 static void qobject_input_stack_object_free(StackObject
*tos
)
242 g_hash_table_unref(tos
->h
);
248 static void qobject_input_pop(Visitor
*v
, void **obj
)
250 QObjectInputVisitor
*qiv
= to_qiv(v
);
251 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
253 assert(tos
&& tos
->qapi
== obj
);
254 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
255 qobject_input_stack_object_free(tos
);
258 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
259 size_t size
, Error
**errp
)
261 QObjectInputVisitor
*qiv
= to_qiv(v
);
262 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
270 if (qobject_type(qobj
) != QTYPE_QDICT
) {
271 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
272 full_name(qiv
, name
), "object");
276 qobject_input_push(qiv
, name
, qobj
, obj
);
279 *obj
= g_malloc0(size
);
284 static void qobject_input_start_list(Visitor
*v
, const char *name
,
285 GenericList
**list
, size_t size
,
288 QObjectInputVisitor
*qiv
= to_qiv(v
);
289 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
290 const QListEntry
*entry
;
298 if (qobject_type(qobj
) != QTYPE_QLIST
) {
299 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
300 full_name(qiv
, name
), "array");
304 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
306 *list
= g_malloc0(size
);
310 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
313 QObjectInputVisitor
*qiv
= to_qiv(v
);
314 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
316 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
321 tail
->next
= g_malloc0(size
);
325 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
327 QObjectInputVisitor
*qiv
= to_qiv(v
);
328 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
330 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
333 error_setg(errp
, "Only %u list elements expected in %s",
334 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
339 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
340 GenericAlternate
**obj
, size_t size
,
341 bool promote_int
, Error
**errp
)
343 QObjectInputVisitor
*qiv
= to_qiv(v
);
344 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
350 *obj
= g_malloc0(size
);
351 (*obj
)->type
= qobject_type(qobj
);
352 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
353 (*obj
)->type
= QTYPE_QFLOAT
;
357 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
360 QObjectInputVisitor
*qiv
= to_qiv(v
);
361 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
367 qint
= qobject_to_qint(qobj
);
369 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
370 full_name(qiv
, name
), "integer");
374 *obj
= qint_get_int(qint
);
378 static void qobject_input_type_int64_keyval(Visitor
*v
, const char *name
,
379 int64_t *obj
, Error
**errp
)
381 QObjectInputVisitor
*qiv
= to_qiv(v
);
382 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
388 if (qemu_strtoi64(str
, NULL
, 0, obj
) < 0) {
389 /* TODO report -ERANGE more nicely */
390 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
391 full_name(qiv
, name
), "integer");
395 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
396 uint64_t *obj
, Error
**errp
)
398 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
399 QObjectInputVisitor
*qiv
= to_qiv(v
);
400 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
406 qint
= qobject_to_qint(qobj
);
408 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
409 full_name(qiv
, name
), "integer");
413 *obj
= qint_get_int(qint
);
416 static void qobject_input_type_uint64_keyval(Visitor
*v
, const char *name
,
417 uint64_t *obj
, Error
**errp
)
419 QObjectInputVisitor
*qiv
= to_qiv(v
);
420 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
426 if (qemu_strtou64(str
, NULL
, 0, obj
) < 0) {
427 /* TODO report -ERANGE more nicely */
428 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
429 full_name(qiv
, name
), "integer");
433 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
436 QObjectInputVisitor
*qiv
= to_qiv(v
);
437 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
443 qbool
= qobject_to_qbool(qobj
);
445 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
446 full_name(qiv
, name
), "boolean");
450 *obj
= qbool_get_bool(qbool
);
453 static void qobject_input_type_bool_keyval(Visitor
*v
, const char *name
,
454 bool *obj
, Error
**errp
)
456 QObjectInputVisitor
*qiv
= to_qiv(v
);
457 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
463 if (!strcmp(str
, "on")) {
465 } else if (!strcmp(str
, "off")) {
468 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
469 full_name(qiv
, name
), "'on' or 'off'");
473 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
476 QObjectInputVisitor
*qiv
= to_qiv(v
);
477 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
484 qstr
= qobject_to_qstring(qobj
);
486 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
487 full_name(qiv
, name
), "string");
491 *obj
= g_strdup(qstring_get_str(qstr
));
494 static void qobject_input_type_str_keyval(Visitor
*v
, const char *name
,
495 char **obj
, Error
**errp
)
497 QObjectInputVisitor
*qiv
= to_qiv(v
);
498 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
500 *obj
= g_strdup(str
);
503 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
506 QObjectInputVisitor
*qiv
= to_qiv(v
);
507 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
514 qint
= qobject_to_qint(qobj
);
516 *obj
= qint_get_int(qobject_to_qint(qobj
));
520 qfloat
= qobject_to_qfloat(qobj
);
522 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
526 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
527 full_name(qiv
, name
), "number");
530 static void qobject_input_type_number_keyval(Visitor
*v
, const char *name
,
531 double *obj
, Error
**errp
)
533 QObjectInputVisitor
*qiv
= to_qiv(v
);
534 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
542 *obj
= strtod(str
, &endp
);
543 if (errno
|| endp
== str
|| *endp
) {
544 /* TODO report -ERANGE more nicely */
545 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
546 full_name(qiv
, name
), "number");
550 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
553 QObjectInputVisitor
*qiv
= to_qiv(v
);
554 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
561 qobject_incref(qobj
);
565 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
567 QObjectInputVisitor
*qiv
= to_qiv(v
);
568 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
574 if (qobject_type(qobj
) != QTYPE_QNULL
) {
575 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
576 full_name(qiv
, name
), "null");
580 static void qobject_input_type_size_keyval(Visitor
*v
, const char *name
,
581 uint64_t *obj
, Error
**errp
)
583 QObjectInputVisitor
*qiv
= to_qiv(v
);
584 const char *str
= qobject_input_get_keyval(qiv
, name
, errp
);
590 if (qemu_strtosz(str
, NULL
, obj
) < 0) {
591 /* TODO report -ERANGE more nicely */
592 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
593 full_name(qiv
, name
), "size");
597 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
599 QObjectInputVisitor
*qiv
= to_qiv(v
);
600 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
610 static void qobject_input_free(Visitor
*v
)
612 QObjectInputVisitor
*qiv
= to_qiv(v
);
614 while (!QSLIST_EMPTY(&qiv
->stack
)) {
615 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
617 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
618 qobject_input_stack_object_free(tos
);
621 qobject_decref(qiv
->root
);
623 g_string_free(qiv
->errname
, TRUE
);
628 static QObjectInputVisitor
*qobject_input_visitor_base_new(QObject
*obj
)
630 QObjectInputVisitor
*v
= g_malloc0(sizeof(*v
));
634 v
->visitor
.type
= VISITOR_INPUT
;
635 v
->visitor
.start_struct
= qobject_input_start_struct
;
636 v
->visitor
.check_struct
= qobject_input_check_struct
;
637 v
->visitor
.end_struct
= qobject_input_pop
;
638 v
->visitor
.start_list
= qobject_input_start_list
;
639 v
->visitor
.next_list
= qobject_input_next_list
;
640 v
->visitor
.check_list
= qobject_input_check_list
;
641 v
->visitor
.end_list
= qobject_input_pop
;
642 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
643 v
->visitor
.optional
= qobject_input_optional
;
644 v
->visitor
.free
= qobject_input_free
;
652 Visitor
*qobject_input_visitor_new(QObject
*obj
)
654 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
656 v
->visitor
.type_int64
= qobject_input_type_int64
;
657 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
658 v
->visitor
.type_bool
= qobject_input_type_bool
;
659 v
->visitor
.type_str
= qobject_input_type_str
;
660 v
->visitor
.type_number
= qobject_input_type_number
;
661 v
->visitor
.type_any
= qobject_input_type_any
;
662 v
->visitor
.type_null
= qobject_input_type_null
;
667 Visitor
*qobject_input_visitor_new_keyval(QObject
*obj
)
669 QObjectInputVisitor
*v
= qobject_input_visitor_base_new(obj
);
671 v
->visitor
.type_int64
= qobject_input_type_int64_keyval
;
672 v
->visitor
.type_uint64
= qobject_input_type_uint64_keyval
;
673 v
->visitor
.type_bool
= qobject_input_type_bool_keyval
;
674 v
->visitor
.type_str
= qobject_input_type_str_keyval
;
675 v
->visitor
.type_number
= qobject_input_type_number_keyval
;
676 v
->visitor
.type_any
= qobject_input_type_any
;
677 v
->visitor
.type_null
= qobject_input_type_null
;
678 v
->visitor
.type_size
= qobject_input_type_size_keyval
;
684 Visitor
*qobject_input_visitor_new_str(const char *str
,
685 const char *implied_key
,
688 bool is_json
= str
[0] == '{';
694 obj
= qobject_from_json(str
, errp
);
696 /* Work around qobject_from_json() lossage TODO fix that */
697 if (errp
&& !*errp
) {
698 error_setg(errp
, "JSON parse error");
703 args
= qobject_to_qdict(obj
);
705 v
= qobject_input_visitor_new(QOBJECT(args
));
707 args
= keyval_parse(str
, implied_key
, errp
);
711 v
= qobject_input_visitor_new_keyval(QOBJECT(args
));