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/qmp-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 #define QIV_STACK_SIZE 1024
26 typedef struct StackObject
29 const QListEntry
*entry
;
33 struct QmpInputVisitor
36 StackObject stack
[QIV_STACK_SIZE
];
41 static QmpInputVisitor
*to_qiv(Visitor
*v
)
43 return container_of(v
, QmpInputVisitor
, visitor
);
46 static QObject
*qmp_input_get_object(QmpInputVisitor
*qiv
,
50 QObject
*qobj
= qiv
->stack
[qiv
->nb_stack
- 1].obj
;
53 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
54 if (qiv
->stack
[qiv
->nb_stack
- 1].h
&& consume
) {
55 g_hash_table_remove(qiv
->stack
[qiv
->nb_stack
- 1].h
, name
);
57 return qdict_get(qobject_to_qdict(qobj
), name
);
58 } else if (qiv
->stack
[qiv
->nb_stack
- 1].entry
) {
59 return qlist_entry_obj(qiv
->stack
[qiv
->nb_stack
- 1].entry
);
66 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
68 GHashTable
*h
= opaque
;
69 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
72 static void qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
, Error
**errp
)
76 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
77 error_setg(errp
, "An internal buffer overran");
81 qiv
->stack
[qiv
->nb_stack
].obj
= obj
;
82 qiv
->stack
[qiv
->nb_stack
].entry
= NULL
;
83 qiv
->stack
[qiv
->nb_stack
].h
= NULL
;
85 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
86 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
87 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
88 qiv
->stack
[qiv
->nb_stack
].h
= h
;
95 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
97 assert(qiv
->nb_stack
> 0);
100 GHashTable
* const top_ht
= qiv
->stack
[qiv
->nb_stack
- 1].h
;
105 g_hash_table_iter_init(&iter
, top_ht
);
106 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
107 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
109 g_hash_table_unref(top_ht
);
116 static void qmp_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
117 size_t size
, Error
**errp
)
119 QmpInputVisitor
*qiv
= to_qiv(v
);
120 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
123 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
124 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
129 qmp_input_push(qiv
, qobj
, &err
);
131 error_propagate(errp
, err
);
136 *obj
= g_malloc0(size
);
140 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
142 QmpInputVisitor
*qiv
= to_qiv(v
);
144 qmp_input_pop(qiv
, errp
);
147 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
149 QmpInputVisitor
*qiv
= to_qiv(v
);
150 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
152 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
153 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
158 qmp_input_push(qiv
, qobj
, errp
);
161 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
,
164 QmpInputVisitor
*qiv
= to_qiv(v
);
166 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
169 if (so
->entry
== NULL
) {
170 so
->entry
= qlist_first(qobject_to_qlist(so
->obj
));
173 so
->entry
= qlist_next(so
->entry
);
177 if (so
->entry
== NULL
) {
181 entry
= g_malloc0(size
);
185 (*list
)->next
= entry
;
191 static void qmp_input_end_list(Visitor
*v
)
193 QmpInputVisitor
*qiv
= to_qiv(v
);
195 qmp_input_pop(qiv
, &error_abort
);
198 static void qmp_input_start_alternate(Visitor
*v
, const char *name
,
199 GenericAlternate
**obj
, size_t size
,
200 bool promote_int
, Error
**errp
)
202 QmpInputVisitor
*qiv
= to_qiv(v
);
203 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false);
207 error_setg(errp
, QERR_MISSING_PARAMETER
, name
? name
: "null");
210 *obj
= g_malloc0(size
);
211 (*obj
)->type
= qobject_type(qobj
);
212 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
213 (*obj
)->type
= QTYPE_QFLOAT
;
217 static void qmp_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
220 QmpInputVisitor
*qiv
= to_qiv(v
);
221 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
224 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
229 *obj
= qint_get_int(qint
);
232 static void qmp_input_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
235 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
236 QmpInputVisitor
*qiv
= to_qiv(v
);
237 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
240 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
245 *obj
= qint_get_int(qint
);
248 static void qmp_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
251 QmpInputVisitor
*qiv
= to_qiv(v
);
252 QBool
*qbool
= qobject_to_qbool(qmp_input_get_object(qiv
, name
, true));
255 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
260 *obj
= qbool_get_bool(qbool
);
263 static void qmp_input_type_str(Visitor
*v
, const char *name
, char **obj
,
266 QmpInputVisitor
*qiv
= to_qiv(v
);
267 QString
*qstr
= qobject_to_qstring(qmp_input_get_object(qiv
, name
, true));
270 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
275 *obj
= g_strdup(qstring_get_str(qstr
));
278 static void qmp_input_type_number(Visitor
*v
, const char *name
, double *obj
,
281 QmpInputVisitor
*qiv
= to_qiv(v
);
282 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
286 qint
= qobject_to_qint(qobj
);
288 *obj
= qint_get_int(qobject_to_qint(qobj
));
292 qfloat
= qobject_to_qfloat(qobj
);
294 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
298 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
302 static void qmp_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
305 QmpInputVisitor
*qiv
= to_qiv(v
);
306 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
308 qobject_incref(qobj
);
312 static void qmp_input_optional(Visitor
*v
, const char *name
, bool *present
)
314 QmpInputVisitor
*qiv
= to_qiv(v
);
315 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
325 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
330 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
332 qobject_decref(v
->stack
[0].obj
);
336 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
340 v
= g_malloc0(sizeof(*v
));
342 v
->visitor
.start_struct
= qmp_input_start_struct
;
343 v
->visitor
.end_struct
= qmp_input_end_struct
;
344 v
->visitor
.start_list
= qmp_input_start_list
;
345 v
->visitor
.next_list
= qmp_input_next_list
;
346 v
->visitor
.end_list
= qmp_input_end_list
;
347 v
->visitor
.start_alternate
= qmp_input_start_alternate
;
348 v
->visitor
.type_enum
= input_type_enum
;
349 v
->visitor
.type_int64
= qmp_input_type_int64
;
350 v
->visitor
.type_uint64
= qmp_input_type_uint64
;
351 v
->visitor
.type_bool
= qmp_input_type_bool
;
352 v
->visitor
.type_str
= qmp_input_type_str
;
353 v
->visitor
.type_number
= qmp_input_type_number
;
354 v
->visitor
.type_any
= qmp_input_type_any
;
355 v
->visitor
.optional
= qmp_input_optional
;
357 qmp_input_push(v
, obj
, NULL
);
363 QmpInputVisitor
*qmp_input_visitor_new_strict(QObject
*obj
)
367 v
= qmp_input_visitor_new(obj
);