4 * Copyright (C) 2012-2016 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/types.h"
22 #include "qapi/qmp/qerror.h"
24 typedef struct StackObject
{
25 const char *name
; /* Name of @obj in its parent, if any */
26 QObject
*obj
; /* QDict or QList being visited */
27 void *qapi
; /* sanity check that caller uses same pointer */
29 GHashTable
*h
; /* If @obj is QDict: unvisited keys */
30 const QListEntry
*entry
; /* If @obj is QList: unvisited tail */
31 unsigned index
; /* If @obj is QList: list index of @entry */
33 QSLIST_ENTRY(StackObject
) node
; /* parent */
36 struct QObjectInputVisitor
{
39 /* Root of visit at visitor creation. */
42 /* Stack of objects being visited (all entries will be either
44 QSLIST_HEAD(, StackObject
) stack
;
46 GString
*errname
; /* Accumulator for full_name() */
49 static QObjectInputVisitor
*to_qiv(Visitor
*v
)
51 return container_of(v
, QObjectInputVisitor
, visitor
);
54 static const char *full_name_nth(QObjectInputVisitor
*qiv
, const char *name
,
61 g_string_truncate(qiv
->errname
, 0);
63 qiv
->errname
= g_string_new("");
66 QSLIST_FOREACH(so
, &qiv
->stack
, node
) {
69 } else if (qobject_type(so
->obj
) == QTYPE_QDICT
) {
70 g_string_prepend(qiv
->errname
, name
?: "<anonymous>");
71 g_string_prepend_c(qiv
->errname
, '.');
73 snprintf(buf
, sizeof(buf
), "[%u]", so
->index
);
74 g_string_prepend(qiv
->errname
, buf
);
81 g_string_prepend(qiv
->errname
, name
);
82 } else if (qiv
->errname
->str
[0] == '.') {
83 g_string_erase(qiv
->errname
, 0, 1);
84 } else if (!qiv
->errname
->str
[0]) {
88 return qiv
->errname
->str
;
91 static const char *full_name(QObjectInputVisitor
*qiv
, const char *name
)
93 return full_name_nth(qiv
, name
, 0);
96 static QObject
*qobject_input_try_get_object(QObjectInputVisitor
*qiv
,
104 if (QSLIST_EMPTY(&qiv
->stack
)) {
105 /* Starting at root, name is ignored. */
110 /* We are in a container; find the next element. */
111 tos
= QSLIST_FIRST(&qiv
->stack
);
115 if (qobject_type(qobj
) == QTYPE_QDICT
) {
117 ret
= qdict_get(qobject_to_qdict(qobj
), name
);
118 if (tos
->h
&& consume
&& ret
) {
119 bool removed
= g_hash_table_remove(tos
->h
, name
);
123 assert(qobject_type(qobj
) == QTYPE_QLIST
);
125 ret
= qlist_entry_obj(tos
->entry
);
128 tos
->entry
= qlist_next(tos
->entry
);
136 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
138 bool consume
, Error
**errp
)
140 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
143 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
148 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
150 GHashTable
*h
= opaque
;
151 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
154 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
156 QObject
*obj
, void *qapi
)
159 StackObject
*tos
= g_new0(StackObject
, 1);
166 if (qobject_type(obj
) == QTYPE_QDICT
) {
167 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
168 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
171 assert(qobject_type(obj
) == QTYPE_QLIST
);
172 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
176 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
181 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
183 QObjectInputVisitor
*qiv
= to_qiv(v
);
184 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
188 assert(tos
&& !tos
->entry
);
190 g_hash_table_iter_init(&iter
, tos
->h
);
191 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
192 error_setg(errp
, "Parameter '%s' is unexpected",
193 full_name(qiv
, key
));
197 static void qobject_input_stack_object_free(StackObject
*tos
)
200 g_hash_table_unref(tos
->h
);
206 static void qobject_input_pop(Visitor
*v
, void **obj
)
208 QObjectInputVisitor
*qiv
= to_qiv(v
);
209 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
211 assert(tos
&& tos
->qapi
== obj
);
212 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
213 qobject_input_stack_object_free(tos
);
216 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
217 size_t size
, Error
**errp
)
219 QObjectInputVisitor
*qiv
= to_qiv(v
);
220 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
228 if (qobject_type(qobj
) != QTYPE_QDICT
) {
229 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
230 full_name(qiv
, name
), "object");
234 qobject_input_push(qiv
, name
, qobj
, obj
);
237 *obj
= g_malloc0(size
);
242 static void qobject_input_start_list(Visitor
*v
, const char *name
,
243 GenericList
**list
, size_t size
,
246 QObjectInputVisitor
*qiv
= to_qiv(v
);
247 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
248 const QListEntry
*entry
;
256 if (qobject_type(qobj
) != QTYPE_QLIST
) {
257 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
258 full_name(qiv
, name
), "array");
262 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
264 *list
= g_malloc0(size
);
268 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
271 QObjectInputVisitor
*qiv
= to_qiv(v
);
272 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
274 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
279 tail
->next
= g_malloc0(size
);
283 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
285 QObjectInputVisitor
*qiv
= to_qiv(v
);
286 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
288 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
291 error_setg(errp
, "Only %u list elements expected in %s",
292 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
297 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
298 GenericAlternate
**obj
, size_t size
,
299 bool promote_int
, Error
**errp
)
301 QObjectInputVisitor
*qiv
= to_qiv(v
);
302 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
308 *obj
= g_malloc0(size
);
309 (*obj
)->type
= qobject_type(qobj
);
310 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
311 (*obj
)->type
= QTYPE_QFLOAT
;
315 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
318 QObjectInputVisitor
*qiv
= to_qiv(v
);
319 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
325 qint
= qobject_to_qint(qobj
);
327 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
328 full_name(qiv
, name
), "integer");
332 *obj
= qint_get_int(qint
);
335 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
336 uint64_t *obj
, Error
**errp
)
338 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
339 QObjectInputVisitor
*qiv
= to_qiv(v
);
340 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
346 qint
= qobject_to_qint(qobj
);
348 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
349 full_name(qiv
, name
), "integer");
353 *obj
= qint_get_int(qint
);
356 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
359 QObjectInputVisitor
*qiv
= to_qiv(v
);
360 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
366 qbool
= qobject_to_qbool(qobj
);
368 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
369 full_name(qiv
, name
), "boolean");
373 *obj
= qbool_get_bool(qbool
);
376 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
379 QObjectInputVisitor
*qiv
= to_qiv(v
);
380 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
387 qstr
= qobject_to_qstring(qobj
);
389 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
390 full_name(qiv
, name
), "string");
394 *obj
= g_strdup(qstring_get_str(qstr
));
397 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
400 QObjectInputVisitor
*qiv
= to_qiv(v
);
401 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
408 qint
= qobject_to_qint(qobj
);
410 *obj
= qint_get_int(qobject_to_qint(qobj
));
414 qfloat
= qobject_to_qfloat(qobj
);
416 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
420 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
421 full_name(qiv
, name
), "number");
424 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
427 QObjectInputVisitor
*qiv
= to_qiv(v
);
428 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
435 qobject_incref(qobj
);
439 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
441 QObjectInputVisitor
*qiv
= to_qiv(v
);
442 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
448 if (qobject_type(qobj
) != QTYPE_QNULL
) {
449 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
450 full_name(qiv
, name
), "null");
454 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
456 QObjectInputVisitor
*qiv
= to_qiv(v
);
457 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
467 static void qobject_input_free(Visitor
*v
)
469 QObjectInputVisitor
*qiv
= to_qiv(v
);
471 while (!QSLIST_EMPTY(&qiv
->stack
)) {
472 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
474 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
475 qobject_input_stack_object_free(tos
);
478 qobject_decref(qiv
->root
);
480 g_string_free(qiv
->errname
, TRUE
);
485 Visitor
*qobject_input_visitor_new(QObject
*obj
)
487 QObjectInputVisitor
*v
;
490 v
= g_malloc0(sizeof(*v
));
492 v
->visitor
.type
= VISITOR_INPUT
;
493 v
->visitor
.start_struct
= qobject_input_start_struct
;
494 v
->visitor
.check_struct
= qobject_input_check_struct
;
495 v
->visitor
.end_struct
= qobject_input_pop
;
496 v
->visitor
.start_list
= qobject_input_start_list
;
497 v
->visitor
.next_list
= qobject_input_next_list
;
498 v
->visitor
.check_list
= qobject_input_check_list
;
499 v
->visitor
.end_list
= qobject_input_pop
;
500 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
501 v
->visitor
.type_int64
= qobject_input_type_int64
;
502 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
503 v
->visitor
.type_bool
= qobject_input_type_bool
;
504 v
->visitor
.type_str
= qobject_input_type_str
;
505 v
->visitor
.type_number
= qobject_input_type_number
;
506 v
->visitor
.type_any
= qobject_input_type_any
;
507 v
->visitor
.type_null
= qobject_input_type_null
;
508 v
->visitor
.optional
= qobject_input_optional
;
509 v
->visitor
.free
= qobject_input_free
;