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 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
95 if (qiv
->strict
&& qiv
->stack
[qiv
->nb_stack
- 1].h
) {
96 g_hash_table_iter_init(&iter
, qiv
->stack
[qiv
->nb_stack
- 1].h
);
97 if (g_hash_table_iter_next(&iter
, &key
, NULL
)) {
98 error_set(errp
, QERR_QMP_EXTRA_MEMBER
, (char *) key
);
100 g_hash_table_unref(qiv
->stack
[qiv
->nb_stack
- 1].h
);
103 assert(qiv
->nb_stack
> 0);
107 static void qmp_input_start_struct(Visitor
*v
, void **obj
, const char *kind
,
108 const char *name
, size_t size
, Error
**errp
)
110 QmpInputVisitor
*qiv
= to_qiv(v
);
111 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
114 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
115 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
120 qmp_input_push(qiv
, qobj
, &err
);
122 error_propagate(errp
, err
);
127 *obj
= g_malloc0(size
);
131 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
133 QmpInputVisitor
*qiv
= to_qiv(v
);
135 qmp_input_pop(qiv
, errp
);
138 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
140 QmpInputVisitor
*qiv
= to_qiv(v
);
141 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
143 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
144 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
149 qmp_input_push(qiv
, qobj
, errp
);
152 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
,
155 QmpInputVisitor
*qiv
= to_qiv(v
);
157 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
160 if (so
->entry
== NULL
) {
161 so
->entry
= qlist_first(qobject_to_qlist(so
->obj
));
164 so
->entry
= qlist_next(so
->entry
);
168 if (so
->entry
== NULL
) {
172 entry
= g_malloc0(sizeof(*entry
));
176 (*list
)->next
= entry
;
182 static void qmp_input_end_list(Visitor
*v
, Error
**errp
)
184 QmpInputVisitor
*qiv
= to_qiv(v
);
186 qmp_input_pop(qiv
, errp
);
189 static void qmp_input_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
192 QmpInputVisitor
*qiv
= to_qiv(v
);
193 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
195 if (!qobj
|| qobject_type(qobj
) != QTYPE_QINT
) {
196 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
201 *obj
= qint_get_int(qobject_to_qint(qobj
));
204 static void qmp_input_type_bool(Visitor
*v
, bool *obj
, const char *name
,
207 QmpInputVisitor
*qiv
= to_qiv(v
);
208 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
210 if (!qobj
|| qobject_type(qobj
) != QTYPE_QBOOL
) {
211 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
216 *obj
= qbool_get_int(qobject_to_qbool(qobj
));
219 static void qmp_input_type_str(Visitor
*v
, char **obj
, const char *name
,
222 QmpInputVisitor
*qiv
= to_qiv(v
);
223 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
225 if (!qobj
|| qobject_type(qobj
) != QTYPE_QSTRING
) {
226 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
231 *obj
= g_strdup(qstring_get_str(qobject_to_qstring(qobj
)));
234 static void qmp_input_type_number(Visitor
*v
, double *obj
, const char *name
,
237 QmpInputVisitor
*qiv
= to_qiv(v
);
238 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
240 if (!qobj
|| qobject_type(qobj
) != QTYPE_QFLOAT
) {
241 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
246 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
249 static void qmp_input_start_optional(Visitor
*v
, bool *present
,
250 const char *name
, Error
**errp
)
252 QmpInputVisitor
*qiv
= to_qiv(v
);
253 QObject
*qobj
= qmp_input_get_object(qiv
, name
);
263 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
268 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
270 qobject_decref(v
->stack
[0].obj
);
274 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
278 v
= g_malloc0(sizeof(*v
));
280 v
->visitor
.start_struct
= qmp_input_start_struct
;
281 v
->visitor
.end_struct
= qmp_input_end_struct
;
282 v
->visitor
.start_list
= qmp_input_start_list
;
283 v
->visitor
.next_list
= qmp_input_next_list
;
284 v
->visitor
.end_list
= qmp_input_end_list
;
285 v
->visitor
.type_enum
= input_type_enum
;
286 v
->visitor
.type_int
= qmp_input_type_int
;
287 v
->visitor
.type_bool
= qmp_input_type_bool
;
288 v
->visitor
.type_str
= qmp_input_type_str
;
289 v
->visitor
.type_number
= qmp_input_type_number
;
290 v
->visitor
.start_optional
= qmp_input_start_optional
;
292 qmp_input_push(v
, obj
, NULL
);
298 QmpInputVisitor
*qmp_input_visitor_new_strict(QObject
*obj
)
302 v
= qmp_input_visitor_new(obj
);