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 "qmp-input-visitor.h"
15 #include "qapi/qapi-visit-impl.h"
16 #include "qemu-queue.h"
17 #include "qemu-common.h"
18 #include "qemu-objects.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
,
46 QObject
*qobj
= qiv
->stack
[qiv
->nb_stack
- 1].obj
;
49 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
50 if (qiv
->stack
[qiv
->nb_stack
- 1].h
) {
51 g_hash_table_remove(qiv
->stack
[qiv
->nb_stack
- 1].h
, name
);
53 return qdict_get(qobject_to_qdict(qobj
), name
);
54 } else if (qiv
->stack
[qiv
->nb_stack
- 1].entry
) {
55 return qlist_entry_obj(qiv
->stack
[qiv
->nb_stack
- 1].entry
);
62 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
64 GHashTable
*h
= opaque
;
65 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
68 static void qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
, Error
**errp
)
72 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
73 error_set(errp
, QERR_BUFFER_OVERRUN
);
77 qiv
->stack
[qiv
->nb_stack
].obj
= obj
;
78 qiv
->stack
[qiv
->nb_stack
].entry
= NULL
;
79 qiv
->stack
[qiv
->nb_stack
].h
= NULL
;
81 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
82 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
83 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
84 qiv
->stack
[qiv
->nb_stack
].h
= h
;
90 /** Only for qmp_input_pop. */
91 static gboolean
always_true(gpointer key
, gpointer val
, gpointer user_pkey
)
93 *(const char **)user_pkey
= (const char *)key
;
97 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
99 assert(qiv
->nb_stack
> 0);
102 GHashTable
* const top_ht
= qiv
->stack
[qiv
->nb_stack
- 1].h
;
104 if (g_hash_table_size(top_ht
)) {
106 g_hash_table_find(top_ht
, always_true
, &key
);
107 error_set(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
109 g_hash_table_unref(top_ht
);
116 static void qmp_input_start_struct(Visitor
*v
, void **obj
, const char *kind
,
117 const char *name
, size_t size
, Error
**errp
)
119 QmpInputVisitor
*qiv
= to_qiv(v
);
120 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
123 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
124 error_set(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
);
152 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
153 error_set(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(sizeof(*entry
));
185 (*list
)->next
= entry
;
191 static void qmp_input_end_list(Visitor
*v
, Error
**errp
)
193 QmpInputVisitor
*qiv
= to_qiv(v
);
195 qmp_input_pop(qiv
, errp
);
198 static void qmp_input_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
201 QmpInputVisitor
*qiv
= to_qiv(v
);
202 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
204 if (!qobj
|| qobject_type(qobj
) != QTYPE_QINT
) {
205 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
210 *obj
= qint_get_int(qobject_to_qint(qobj
));
213 static void qmp_input_type_bool(Visitor
*v
, bool *obj
, const char *name
,
216 QmpInputVisitor
*qiv
= to_qiv(v
);
217 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
219 if (!qobj
|| qobject_type(qobj
) != QTYPE_QBOOL
) {
220 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
225 *obj
= qbool_get_int(qobject_to_qbool(qobj
));
228 static void qmp_input_type_str(Visitor
*v
, char **obj
, const char *name
,
231 QmpInputVisitor
*qiv
= to_qiv(v
);
232 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
234 if (!qobj
|| qobject_type(qobj
) != QTYPE_QSTRING
) {
235 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
240 *obj
= g_strdup(qstring_get_str(qobject_to_qstring(qobj
)));
243 static void qmp_input_type_number(Visitor
*v
, double *obj
, const char *name
,
246 QmpInputVisitor
*qiv
= to_qiv(v
);
247 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
249 if (!qobj
|| (qobject_type(qobj
) != QTYPE_QFLOAT
&&
250 qobject_type(qobj
) != QTYPE_QINT
)) {
251 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
256 if (qobject_type(qobj
) == QTYPE_QINT
) {
257 *obj
= qint_get_int(qobject_to_qint(qobj
));
259 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
263 static void qmp_input_start_optional(Visitor
*v
, bool *present
,
264 const char *name
, Error
**errp
)
266 QmpInputVisitor
*qiv
= to_qiv(v
);
267 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
277 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
282 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
284 qobject_decref(v
->stack
[0].obj
);
288 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
292 v
= g_malloc0(sizeof(*v
));
294 v
->visitor
.start_struct
= qmp_input_start_struct
;
295 v
->visitor
.end_struct
= qmp_input_end_struct
;
296 v
->visitor
.start_list
= qmp_input_start_list
;
297 v
->visitor
.next_list
= qmp_input_next_list
;
298 v
->visitor
.end_list
= qmp_input_end_list
;
299 v
->visitor
.type_enum
= input_type_enum
;
300 v
->visitor
.type_int
= qmp_input_type_int
;
301 v
->visitor
.type_bool
= qmp_input_type_bool
;
302 v
->visitor
.type_str
= qmp_input_type_str
;
303 v
->visitor
.type_number
= qmp_input_type_number
;
304 v
->visitor
.start_optional
= qmp_input_start_optional
;
306 qmp_input_push(v
, obj
, NULL
);
312 QmpInputVisitor
*qmp_input_visitor_new_strict(QObject
*obj
)
316 v
= qmp_input_visitor_new(obj
);