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
;
29 struct QmpInputVisitor
33 StackObject stack
[QIV_STACK_SIZE
];
37 static QmpInputVisitor
*to_qiv(Visitor
*v
)
39 return container_of(v
, QmpInputVisitor
, visitor
);
42 static const QObject
*qmp_input_get_object(QmpInputVisitor
*qiv
,
47 if (qiv
->nb_stack
== 0) {
50 qobj
= qiv
->stack
[qiv
->nb_stack
- 1].obj
;
54 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
55 return qdict_get(qobject_to_qdict(qobj
), name
);
56 } else if (qiv
->nb_stack
> 0 && qobject_type(qobj
) == QTYPE_QLIST
) {
57 return qlist_entry_obj(qiv
->stack
[qiv
->nb_stack
- 1].entry
);
64 static void qmp_input_push(QmpInputVisitor
*qiv
, const QObject
*obj
, Error
**errp
)
66 qiv
->stack
[qiv
->nb_stack
].obj
= obj
;
67 if (qobject_type(obj
) == QTYPE_QLIST
) {
68 qiv
->stack
[qiv
->nb_stack
].entry
= qlist_first(qobject_to_qlist(obj
));
72 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
73 error_set(errp
, QERR_BUFFER_OVERRUN
);
78 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
81 if (qiv
->nb_stack
< 0) {
82 error_set(errp
, QERR_BUFFER_OVERRUN
);
87 static void qmp_input_start_struct(Visitor
*v
, void **obj
, const char *kind
,
88 const char *name
, size_t size
, Error
**errp
)
90 QmpInputVisitor
*qiv
= to_qiv(v
);
91 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
93 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
94 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
99 qmp_input_push(qiv
, qobj
, errp
);
100 if (error_is_set(errp
)) {
105 *obj
= g_malloc0(size
);
109 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
111 QmpInputVisitor
*qiv
= to_qiv(v
);
113 qmp_input_pop(qiv
, errp
);
116 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
118 QmpInputVisitor
*qiv
= to_qiv(v
);
119 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
121 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
122 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
127 qmp_input_push(qiv
, qobj
, errp
);
130 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
,
133 QmpInputVisitor
*qiv
= to_qiv(v
);
135 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
137 if (so
->entry
== NULL
) {
141 entry
= g_malloc0(sizeof(*entry
));
143 so
->entry
= qlist_next(so
->entry
);
144 if (so
->entry
== NULL
) {
148 (*list
)->next
= entry
;
154 static void qmp_input_end_list(Visitor
*v
, Error
**errp
)
156 QmpInputVisitor
*qiv
= to_qiv(v
);
158 qmp_input_pop(qiv
, errp
);
161 static void qmp_input_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
164 QmpInputVisitor
*qiv
= to_qiv(v
);
165 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
167 if (!qobj
|| qobject_type(qobj
) != QTYPE_QINT
) {
168 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
173 *obj
= qint_get_int(qobject_to_qint(qobj
));
176 static void qmp_input_type_bool(Visitor
*v
, bool *obj
, const char *name
,
179 QmpInputVisitor
*qiv
= to_qiv(v
);
180 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
182 if (!qobj
|| qobject_type(qobj
) != QTYPE_QBOOL
) {
183 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
188 *obj
= qbool_get_int(qobject_to_qbool(qobj
));
191 static void qmp_input_type_str(Visitor
*v
, char **obj
, const char *name
,
194 QmpInputVisitor
*qiv
= to_qiv(v
);
195 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
197 if (!qobj
|| qobject_type(qobj
) != QTYPE_QSTRING
) {
198 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
203 *obj
= g_strdup(qstring_get_str(qobject_to_qstring(qobj
)));
206 static void qmp_input_type_number(Visitor
*v
, double *obj
, const char *name
,
209 QmpInputVisitor
*qiv
= to_qiv(v
);
210 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
212 if (!qobj
|| qobject_type(qobj
) != QTYPE_QFLOAT
) {
213 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
218 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
221 static void qmp_input_start_optional(Visitor
*v
, bool *present
,
222 const char *name
, Error
**errp
)
224 QmpInputVisitor
*qiv
= to_qiv(v
);
225 const QObject
*qobj
= qmp_input_get_object(qiv
, name
);
235 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
240 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
242 qobject_decref(v
->obj
);
246 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
250 v
= g_malloc0(sizeof(*v
));
252 v
->visitor
.start_struct
= qmp_input_start_struct
;
253 v
->visitor
.end_struct
= qmp_input_end_struct
;
254 v
->visitor
.start_list
= qmp_input_start_list
;
255 v
->visitor
.next_list
= qmp_input_next_list
;
256 v
->visitor
.end_list
= qmp_input_end_list
;
257 v
->visitor
.type_enum
= input_type_enum
;
258 v
->visitor
.type_int
= qmp_input_type_int
;
259 v
->visitor
.type_bool
= qmp_input_type_bool
;
260 v
->visitor
.type_str
= qmp_input_type_str
;
261 v
->visitor
.type_number
= qmp_input_type_number
;
262 v
->visitor
.start_optional
= qmp_input_start_optional
;
265 qobject_incref(v
->obj
);