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
);
126 ret
= qlist_entry_obj(tos
->entry
);
128 tos
->entry
= qlist_next(tos
->entry
);
141 static QObject
*qobject_input_get_object(QObjectInputVisitor
*qiv
,
143 bool consume
, Error
**errp
)
145 QObject
*obj
= qobject_input_try_get_object(qiv
, name
, consume
);
148 error_setg(errp
, QERR_MISSING_PARAMETER
, full_name(qiv
, name
));
153 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
155 GHashTable
*h
= opaque
;
156 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
159 static const QListEntry
*qobject_input_push(QObjectInputVisitor
*qiv
,
161 QObject
*obj
, void *qapi
)
164 StackObject
*tos
= g_new0(StackObject
, 1);
171 if (qobject_type(obj
) == QTYPE_QDICT
) {
172 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
173 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
176 assert(qobject_type(obj
) == QTYPE_QLIST
);
177 tos
->entry
= qlist_first(qobject_to_qlist(obj
));
181 QSLIST_INSERT_HEAD(&qiv
->stack
, tos
, node
);
186 static void qobject_input_check_struct(Visitor
*v
, Error
**errp
)
188 QObjectInputVisitor
*qiv
= to_qiv(v
);
189 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
193 assert(tos
&& !tos
->entry
);
195 g_hash_table_iter_init(&iter
, tos
->h
);
196 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
197 error_setg(errp
, "Parameter '%s' is unexpected",
198 full_name(qiv
, key
));
202 static void qobject_input_stack_object_free(StackObject
*tos
)
205 g_hash_table_unref(tos
->h
);
211 static void qobject_input_pop(Visitor
*v
, void **obj
)
213 QObjectInputVisitor
*qiv
= to_qiv(v
);
214 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
216 assert(tos
&& tos
->qapi
== obj
);
217 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
218 qobject_input_stack_object_free(tos
);
221 static void qobject_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
222 size_t size
, Error
**errp
)
224 QObjectInputVisitor
*qiv
= to_qiv(v
);
225 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
233 if (qobject_type(qobj
) != QTYPE_QDICT
) {
234 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
235 full_name(qiv
, name
), "object");
239 qobject_input_push(qiv
, name
, qobj
, obj
);
242 *obj
= g_malloc0(size
);
247 static void qobject_input_start_list(Visitor
*v
, const char *name
,
248 GenericList
**list
, size_t size
,
251 QObjectInputVisitor
*qiv
= to_qiv(v
);
252 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
253 const QListEntry
*entry
;
261 if (qobject_type(qobj
) != QTYPE_QLIST
) {
262 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
263 full_name(qiv
, name
), "array");
267 entry
= qobject_input_push(qiv
, name
, qobj
, list
);
269 *list
= g_malloc0(size
);
273 static GenericList
*qobject_input_next_list(Visitor
*v
, GenericList
*tail
,
276 QObjectInputVisitor
*qiv
= to_qiv(v
);
277 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
279 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
284 tail
->next
= g_malloc0(size
);
288 static void qobject_input_check_list(Visitor
*v
, Error
**errp
)
290 QObjectInputVisitor
*qiv
= to_qiv(v
);
291 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
293 assert(tos
&& tos
->obj
&& qobject_type(tos
->obj
) == QTYPE_QLIST
);
296 error_setg(errp
, "Only %u list elements expected in %s",
297 tos
->index
+ 1, full_name_nth(qiv
, NULL
, 1));
302 static void qobject_input_start_alternate(Visitor
*v
, const char *name
,
303 GenericAlternate
**obj
, size_t size
,
304 bool promote_int
, Error
**errp
)
306 QObjectInputVisitor
*qiv
= to_qiv(v
);
307 QObject
*qobj
= qobject_input_get_object(qiv
, name
, false, errp
);
313 *obj
= g_malloc0(size
);
314 (*obj
)->type
= qobject_type(qobj
);
315 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
316 (*obj
)->type
= QTYPE_QFLOAT
;
320 static void qobject_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
323 QObjectInputVisitor
*qiv
= to_qiv(v
);
324 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
330 qint
= qobject_to_qint(qobj
);
332 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
333 full_name(qiv
, name
), "integer");
337 *obj
= qint_get_int(qint
);
340 static void qobject_input_type_uint64(Visitor
*v
, const char *name
,
341 uint64_t *obj
, Error
**errp
)
343 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
344 QObjectInputVisitor
*qiv
= to_qiv(v
);
345 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
351 qint
= qobject_to_qint(qobj
);
353 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
354 full_name(qiv
, name
), "integer");
358 *obj
= qint_get_int(qint
);
361 static void qobject_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
364 QObjectInputVisitor
*qiv
= to_qiv(v
);
365 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
371 qbool
= qobject_to_qbool(qobj
);
373 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
374 full_name(qiv
, name
), "boolean");
378 *obj
= qbool_get_bool(qbool
);
381 static void qobject_input_type_str(Visitor
*v
, const char *name
, char **obj
,
384 QObjectInputVisitor
*qiv
= to_qiv(v
);
385 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
392 qstr
= qobject_to_qstring(qobj
);
394 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
395 full_name(qiv
, name
), "string");
399 *obj
= g_strdup(qstring_get_str(qstr
));
402 static void qobject_input_type_number(Visitor
*v
, const char *name
, double *obj
,
405 QObjectInputVisitor
*qiv
= to_qiv(v
);
406 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
413 qint
= qobject_to_qint(qobj
);
415 *obj
= qint_get_int(qobject_to_qint(qobj
));
419 qfloat
= qobject_to_qfloat(qobj
);
421 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
425 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
426 full_name(qiv
, name
), "number");
429 static void qobject_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
432 QObjectInputVisitor
*qiv
= to_qiv(v
);
433 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
440 qobject_incref(qobj
);
444 static void qobject_input_type_null(Visitor
*v
, const char *name
, Error
**errp
)
446 QObjectInputVisitor
*qiv
= to_qiv(v
);
447 QObject
*qobj
= qobject_input_get_object(qiv
, name
, true, errp
);
453 if (qobject_type(qobj
) != QTYPE_QNULL
) {
454 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
455 full_name(qiv
, name
), "null");
459 static void qobject_input_optional(Visitor
*v
, const char *name
, bool *present
)
461 QObjectInputVisitor
*qiv
= to_qiv(v
);
462 QObject
*qobj
= qobject_input_try_get_object(qiv
, name
, false);
472 static void qobject_input_free(Visitor
*v
)
474 QObjectInputVisitor
*qiv
= to_qiv(v
);
476 while (!QSLIST_EMPTY(&qiv
->stack
)) {
477 StackObject
*tos
= QSLIST_FIRST(&qiv
->stack
);
479 QSLIST_REMOVE_HEAD(&qiv
->stack
, node
);
480 qobject_input_stack_object_free(tos
);
483 qobject_decref(qiv
->root
);
485 g_string_free(qiv
->errname
, TRUE
);
490 Visitor
*qobject_input_visitor_new(QObject
*obj
)
492 QObjectInputVisitor
*v
;
495 v
= g_malloc0(sizeof(*v
));
497 v
->visitor
.type
= VISITOR_INPUT
;
498 v
->visitor
.start_struct
= qobject_input_start_struct
;
499 v
->visitor
.check_struct
= qobject_input_check_struct
;
500 v
->visitor
.end_struct
= qobject_input_pop
;
501 v
->visitor
.start_list
= qobject_input_start_list
;
502 v
->visitor
.next_list
= qobject_input_next_list
;
503 v
->visitor
.check_list
= qobject_input_check_list
;
504 v
->visitor
.end_list
= qobject_input_pop
;
505 v
->visitor
.start_alternate
= qobject_input_start_alternate
;
506 v
->visitor
.type_int64
= qobject_input_type_int64
;
507 v
->visitor
.type_uint64
= qobject_input_type_uint64
;
508 v
->visitor
.type_bool
= qobject_input_type_bool
;
509 v
->visitor
.type_str
= qobject_input_type_str
;
510 v
->visitor
.type_number
= qobject_input_type_number
;
511 v
->visitor
.type_any
= qobject_input_type_any
;
512 v
->visitor
.type_null
= qobject_input_type_null
;
513 v
->visitor
.optional
= qobject_input_optional
;
514 v
->visitor
.free
= qobject_input_free
;