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 "qapi/qmp-input-visitor.h"
15 #include "qapi/visitor-impl.h"
16 #include "qemu/queue.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/types.h"
19 #include "qapi/qmp/qerror.h"
21 #define QIV_STACK_SIZE 1024
23 typedef struct StackObject
26 const QListEntry
*entry
;
30 struct QmpInputVisitor
33 StackObject stack
[QIV_STACK_SIZE
];
38 static QmpInputVisitor
*to_qiv(Visitor
*v
)
40 return container_of(v
, QmpInputVisitor
, visitor
);
43 static QObject
*qmp_input_get_object(QmpInputVisitor
*qiv
,
47 QObject
*qobj
= qiv
->stack
[qiv
->nb_stack
- 1].obj
;
50 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
51 if (qiv
->stack
[qiv
->nb_stack
- 1].h
&& consume
) {
52 g_hash_table_remove(qiv
->stack
[qiv
->nb_stack
- 1].h
, name
);
54 return qdict_get(qobject_to_qdict(qobj
), name
);
55 } else if (qiv
->stack
[qiv
->nb_stack
- 1].entry
) {
56 return qlist_entry_obj(qiv
->stack
[qiv
->nb_stack
- 1].entry
);
63 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
65 GHashTable
*h
= opaque
;
66 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
69 static void qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
, Error
**errp
)
73 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
74 error_setg(errp
, "An internal buffer overran");
78 qiv
->stack
[qiv
->nb_stack
].obj
= obj
;
79 qiv
->stack
[qiv
->nb_stack
].entry
= NULL
;
80 qiv
->stack
[qiv
->nb_stack
].h
= NULL
;
82 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
83 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
84 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
85 qiv
->stack
[qiv
->nb_stack
].h
= h
;
91 /** Only for qmp_input_pop. */
92 static gboolean
always_true(gpointer key
, gpointer val
, gpointer user_pkey
)
94 *(const char **)user_pkey
= (const char *)key
;
98 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
100 assert(qiv
->nb_stack
> 0);
103 GHashTable
* const top_ht
= qiv
->stack
[qiv
->nb_stack
- 1].h
;
105 if (g_hash_table_size(top_ht
)) {
107 g_hash_table_find(top_ht
, always_true
, &key
);
108 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
110 g_hash_table_unref(top_ht
);
117 static void qmp_input_start_struct(Visitor
*v
, void **obj
, const char *kind
,
118 const char *name
, size_t size
, Error
**errp
)
120 QmpInputVisitor
*qiv
= to_qiv(v
);
121 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
124 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
125 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
130 qmp_input_push(qiv
, qobj
, &err
);
132 error_propagate(errp
, err
);
137 *obj
= g_malloc0(size
);
141 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
143 QmpInputVisitor
*qiv
= to_qiv(v
);
145 qmp_input_pop(qiv
, errp
);
148 static void qmp_input_start_implicit_struct(Visitor
*v
, void **obj
,
149 size_t size
, Error
**errp
)
152 *obj
= g_malloc0(size
);
156 static void qmp_input_end_implicit_struct(Visitor
*v
, Error
**errp
)
160 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
162 QmpInputVisitor
*qiv
= to_qiv(v
);
163 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
165 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
166 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
171 qmp_input_push(qiv
, qobj
, errp
);
174 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
,
177 QmpInputVisitor
*qiv
= to_qiv(v
);
179 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
182 if (so
->entry
== NULL
) {
183 so
->entry
= qlist_first(qobject_to_qlist(so
->obj
));
186 so
->entry
= qlist_next(so
->entry
);
190 if (so
->entry
== NULL
) {
194 entry
= g_malloc0(sizeof(*entry
));
198 (*list
)->next
= entry
;
204 static void qmp_input_end_list(Visitor
*v
, Error
**errp
)
206 QmpInputVisitor
*qiv
= to_qiv(v
);
208 qmp_input_pop(qiv
, errp
);
211 static void qmp_input_get_next_type(Visitor
*v
, QType
*type
, bool promote_int
,
212 const char *name
, Error
**errp
)
214 QmpInputVisitor
*qiv
= to_qiv(v
);
215 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false);
218 error_setg(errp
, QERR_MISSING_PARAMETER
, name
? name
: "null");
221 *type
= qobject_type(qobj
);
222 if (promote_int
&& *type
== QTYPE_QINT
) {
223 *type
= QTYPE_QFLOAT
;
227 static void qmp_input_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
230 QmpInputVisitor
*qiv
= to_qiv(v
);
231 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
234 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
239 *obj
= qint_get_int(qint
);
242 static void qmp_input_type_bool(Visitor
*v
, bool *obj
, const char *name
,
245 QmpInputVisitor
*qiv
= to_qiv(v
);
246 QBool
*qbool
= qobject_to_qbool(qmp_input_get_object(qiv
, name
, true));
249 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
254 *obj
= qbool_get_bool(qbool
);
257 static void qmp_input_type_str(Visitor
*v
, char **obj
, const char *name
,
260 QmpInputVisitor
*qiv
= to_qiv(v
);
261 QString
*qstr
= qobject_to_qstring(qmp_input_get_object(qiv
, name
, true));
264 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
269 *obj
= g_strdup(qstring_get_str(qstr
));
272 static void qmp_input_type_number(Visitor
*v
, double *obj
, const char *name
,
275 QmpInputVisitor
*qiv
= to_qiv(v
);
276 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
280 qint
= qobject_to_qint(qobj
);
282 *obj
= qint_get_int(qobject_to_qint(qobj
));
286 qfloat
= qobject_to_qfloat(qobj
);
288 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
292 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
296 static void qmp_input_type_any(Visitor
*v
, QObject
**obj
, const char *name
,
299 QmpInputVisitor
*qiv
= to_qiv(v
);
300 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
302 qobject_incref(qobj
);
306 static void qmp_input_optional(Visitor
*v
, bool *present
, const char *name
)
308 QmpInputVisitor
*qiv
= to_qiv(v
);
309 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
319 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
324 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
326 qobject_decref(v
->stack
[0].obj
);
330 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
334 v
= g_malloc0(sizeof(*v
));
336 v
->visitor
.start_struct
= qmp_input_start_struct
;
337 v
->visitor
.end_struct
= qmp_input_end_struct
;
338 v
->visitor
.start_implicit_struct
= qmp_input_start_implicit_struct
;
339 v
->visitor
.end_implicit_struct
= qmp_input_end_implicit_struct
;
340 v
->visitor
.start_list
= qmp_input_start_list
;
341 v
->visitor
.next_list
= qmp_input_next_list
;
342 v
->visitor
.end_list
= qmp_input_end_list
;
343 v
->visitor
.type_enum
= input_type_enum
;
344 v
->visitor
.type_int
= qmp_input_type_int
;
345 v
->visitor
.type_bool
= qmp_input_type_bool
;
346 v
->visitor
.type_str
= qmp_input_type_str
;
347 v
->visitor
.type_number
= qmp_input_type_number
;
348 v
->visitor
.type_any
= qmp_input_type_any
;
349 v
->visitor
.optional
= qmp_input_optional
;
350 v
->visitor
.get_next_type
= qmp_input_get_next_type
;
352 qmp_input_push(v
, obj
, NULL
);
358 QmpInputVisitor
*qmp_input_visitor_new_strict(QObject
*obj
)
362 v
= qmp_input_visitor_new(obj
);