4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qapi/qmp-input-visitor.h"
16 #include "qapi/visitor-impl.h"
17 #include "qemu/queue.h"
18 #include "qemu-common.h"
19 #include "qapi/qmp/types.h"
20 #include "qapi/qmp/qerror.h"
22 #define QIV_STACK_SIZE 1024
24 typedef struct StackObject
27 const QListEntry
*entry
;
31 struct QmpInputVisitor
34 StackObject stack
[QIV_STACK_SIZE
];
39 static QmpInputVisitor
*to_qiv(Visitor
*v
)
41 return container_of(v
, QmpInputVisitor
, visitor
);
44 static QObject
*qmp_input_get_object(QmpInputVisitor
*qiv
,
48 QObject
*qobj
= qiv
->stack
[qiv
->nb_stack
- 1].obj
;
51 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
52 if (qiv
->stack
[qiv
->nb_stack
- 1].h
&& consume
) {
53 g_hash_table_remove(qiv
->stack
[qiv
->nb_stack
- 1].h
, name
);
55 return qdict_get(qobject_to_qdict(qobj
), name
);
56 } else if (qiv
->stack
[qiv
->nb_stack
- 1].entry
) {
57 return qlist_entry_obj(qiv
->stack
[qiv
->nb_stack
- 1].entry
);
64 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
66 GHashTable
*h
= opaque
;
67 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
70 static void qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
, Error
**errp
)
74 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
75 error_setg(errp
, "An internal buffer overran");
79 qiv
->stack
[qiv
->nb_stack
].obj
= obj
;
80 qiv
->stack
[qiv
->nb_stack
].entry
= NULL
;
81 qiv
->stack
[qiv
->nb_stack
].h
= NULL
;
83 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
84 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
85 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
86 qiv
->stack
[qiv
->nb_stack
].h
= h
;
92 /** Only for qmp_input_pop. */
93 static gboolean
always_true(gpointer key
, gpointer val
, gpointer user_pkey
)
95 *(const char **)user_pkey
= (const char *)key
;
99 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
101 assert(qiv
->nb_stack
> 0);
104 GHashTable
* const top_ht
= qiv
->stack
[qiv
->nb_stack
- 1].h
;
106 if (g_hash_table_size(top_ht
)) {
108 g_hash_table_find(top_ht
, always_true
, &key
);
109 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
111 g_hash_table_unref(top_ht
);
118 static void qmp_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
119 size_t size
, Error
**errp
)
121 QmpInputVisitor
*qiv
= to_qiv(v
);
122 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
125 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
126 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
131 qmp_input_push(qiv
, qobj
, &err
);
133 error_propagate(errp
, err
);
138 *obj
= g_malloc0(size
);
142 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
144 QmpInputVisitor
*qiv
= to_qiv(v
);
146 qmp_input_pop(qiv
, errp
);
149 static void qmp_input_start_implicit_struct(Visitor
*v
, void **obj
,
150 size_t size
, Error
**errp
)
153 *obj
= g_malloc0(size
);
157 static void qmp_input_end_implicit_struct(Visitor
*v
, Error
**errp
)
161 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
163 QmpInputVisitor
*qiv
= to_qiv(v
);
164 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
166 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
167 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
172 qmp_input_push(qiv
, qobj
, errp
);
175 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
,
178 QmpInputVisitor
*qiv
= to_qiv(v
);
180 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
183 if (so
->entry
== NULL
) {
184 so
->entry
= qlist_first(qobject_to_qlist(so
->obj
));
187 so
->entry
= qlist_next(so
->entry
);
191 if (so
->entry
== NULL
) {
195 entry
= g_malloc0(sizeof(*entry
));
199 (*list
)->next
= entry
;
205 static void qmp_input_end_list(Visitor
*v
, Error
**errp
)
207 QmpInputVisitor
*qiv
= to_qiv(v
);
209 qmp_input_pop(qiv
, &error_abort
);
212 static void qmp_input_get_next_type(Visitor
*v
, const char *name
, QType
*type
,
213 bool promote_int
, Error
**errp
)
215 QmpInputVisitor
*qiv
= to_qiv(v
);
216 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false);
219 error_setg(errp
, QERR_MISSING_PARAMETER
, name
? name
: "null");
222 *type
= qobject_type(qobj
);
223 if (promote_int
&& *type
== QTYPE_QINT
) {
224 *type
= QTYPE_QFLOAT
;
228 static void qmp_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
231 QmpInputVisitor
*qiv
= to_qiv(v
);
232 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
235 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
240 *obj
= qint_get_int(qint
);
243 static void qmp_input_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
246 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
247 QmpInputVisitor
*qiv
= to_qiv(v
);
248 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
251 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
256 *obj
= qint_get_int(qint
);
259 static void qmp_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
262 QmpInputVisitor
*qiv
= to_qiv(v
);
263 QBool
*qbool
= qobject_to_qbool(qmp_input_get_object(qiv
, name
, true));
266 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
271 *obj
= qbool_get_bool(qbool
);
274 static void qmp_input_type_str(Visitor
*v
, const char *name
, char **obj
,
277 QmpInputVisitor
*qiv
= to_qiv(v
);
278 QString
*qstr
= qobject_to_qstring(qmp_input_get_object(qiv
, name
, true));
281 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
286 *obj
= g_strdup(qstring_get_str(qstr
));
289 static void qmp_input_type_number(Visitor
*v
, const char *name
, double *obj
,
292 QmpInputVisitor
*qiv
= to_qiv(v
);
293 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
297 qint
= qobject_to_qint(qobj
);
299 *obj
= qint_get_int(qobject_to_qint(qobj
));
303 qfloat
= qobject_to_qfloat(qobj
);
305 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
309 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
313 static void qmp_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
316 QmpInputVisitor
*qiv
= to_qiv(v
);
317 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
319 qobject_incref(qobj
);
323 static void qmp_input_optional(Visitor
*v
, const char *name
, bool *present
)
325 QmpInputVisitor
*qiv
= to_qiv(v
);
326 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
336 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
341 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
343 qobject_decref(v
->stack
[0].obj
);
347 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
351 v
= g_malloc0(sizeof(*v
));
353 v
->visitor
.start_struct
= qmp_input_start_struct
;
354 v
->visitor
.end_struct
= qmp_input_end_struct
;
355 v
->visitor
.start_implicit_struct
= qmp_input_start_implicit_struct
;
356 v
->visitor
.end_implicit_struct
= qmp_input_end_implicit_struct
;
357 v
->visitor
.start_list
= qmp_input_start_list
;
358 v
->visitor
.next_list
= qmp_input_next_list
;
359 v
->visitor
.end_list
= qmp_input_end_list
;
360 v
->visitor
.type_enum
= input_type_enum
;
361 v
->visitor
.type_int64
= qmp_input_type_int64
;
362 v
->visitor
.type_uint64
= qmp_input_type_uint64
;
363 v
->visitor
.type_bool
= qmp_input_type_bool
;
364 v
->visitor
.type_str
= qmp_input_type_str
;
365 v
->visitor
.type_number
= qmp_input_type_number
;
366 v
->visitor
.type_any
= qmp_input_type_any
;
367 v
->visitor
.optional
= qmp_input_optional
;
368 v
->visitor
.get_next_type
= qmp_input_get_next_type
;
370 qmp_input_push(v
, obj
, NULL
);
376 QmpInputVisitor
*qmp_input_visitor_new_strict(QObject
*obj
)
380 v
= qmp_input_visitor_new(obj
);