4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp-input-visitor.h"
18 #include "qapi/visitor-impl.h"
19 #include "qemu/queue.h"
20 #include "qemu-common.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qerror.h"
24 #define QIV_STACK_SIZE 1024
26 typedef struct StackObject
28 QObject
*obj
; /* Object being visited */
30 GHashTable
*h
; /* If obj is dict: unvisited keys */
31 const QListEntry
*entry
; /* If obj is list: unvisited tail */
34 struct QmpInputVisitor
38 /* Stack of objects being visited. stack[0] is root of visit,
39 * stack[1..] records the nesting of start_struct()/end_struct()
40 * and start_list()/end_list() pairs. */
41 StackObject stack
[QIV_STACK_SIZE
];
44 /* True to reject parse in visit_end_struct() if unvisited keys remain. */
48 static QmpInputVisitor
*to_qiv(Visitor
*v
)
50 return container_of(v
, QmpInputVisitor
, visitor
);
53 static QObject
*qmp_input_get_object(QmpInputVisitor
*qiv
,
57 StackObject
*tos
= &qiv
->stack
[qiv
->nb_stack
- 1];
58 QObject
*qobj
= tos
->obj
;
62 /* If we have a name, and we're in a dictionary, then return that
64 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
65 if (tos
->h
&& consume
) {
66 g_hash_table_remove(tos
->h
, name
);
68 return qdict_get(qobject_to_qdict(qobj
), name
);
71 /* If we are in the middle of a list, then return the next element
74 assert(qobject_type(qobj
) == QTYPE_QLIST
);
75 return qlist_entry_obj(tos
->entry
);
78 /* Otherwise, we are at the root of the visit or the start of a
79 * list, and return the object as-is. */
83 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
85 GHashTable
*h
= opaque
;
86 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
89 static void qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
, Error
**errp
)
92 StackObject
*tos
= &qiv
->stack
[qiv
->nb_stack
];
95 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
96 error_setg(errp
, "An internal buffer overran");
104 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
105 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
106 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
114 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
116 assert(qiv
->nb_stack
> 0);
119 GHashTable
* const top_ht
= qiv
->stack
[qiv
->nb_stack
- 1].h
;
124 g_hash_table_iter_init(&iter
, top_ht
);
125 if (g_hash_table_iter_next(&iter
, (void **)&key
, NULL
)) {
126 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
128 g_hash_table_unref(top_ht
);
135 static void qmp_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
136 size_t size
, Error
**errp
)
138 QmpInputVisitor
*qiv
= to_qiv(v
);
139 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
145 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
146 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
151 qmp_input_push(qiv
, qobj
, &err
);
153 error_propagate(errp
, err
);
158 *obj
= g_malloc0(size
);
162 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
164 QmpInputVisitor
*qiv
= to_qiv(v
);
166 qmp_input_pop(qiv
, errp
);
169 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
171 QmpInputVisitor
*qiv
= to_qiv(v
);
172 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
174 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
175 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
180 qmp_input_push(qiv
, qobj
, errp
);
183 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
,
186 QmpInputVisitor
*qiv
= to_qiv(v
);
188 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
191 if (so
->entry
== NULL
) {
192 so
->entry
= qlist_first(qobject_to_qlist(so
->obj
));
195 so
->entry
= qlist_next(so
->entry
);
199 if (so
->entry
== NULL
) {
203 entry
= g_malloc0(size
);
207 (*list
)->next
= entry
;
213 static void qmp_input_end_list(Visitor
*v
)
215 QmpInputVisitor
*qiv
= to_qiv(v
);
217 qmp_input_pop(qiv
, &error_abort
);
220 static void qmp_input_start_alternate(Visitor
*v
, const char *name
,
221 GenericAlternate
**obj
, size_t size
,
222 bool promote_int
, Error
**errp
)
224 QmpInputVisitor
*qiv
= to_qiv(v
);
225 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false);
229 error_setg(errp
, QERR_MISSING_PARAMETER
, name
? name
: "null");
232 *obj
= g_malloc0(size
);
233 (*obj
)->type
= qobject_type(qobj
);
234 if (promote_int
&& (*obj
)->type
== QTYPE_QINT
) {
235 (*obj
)->type
= QTYPE_QFLOAT
;
239 static void qmp_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
242 QmpInputVisitor
*qiv
= to_qiv(v
);
243 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
246 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
251 *obj
= qint_get_int(qint
);
254 static void qmp_input_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
257 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
258 QmpInputVisitor
*qiv
= to_qiv(v
);
259 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
262 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
267 *obj
= qint_get_int(qint
);
270 static void qmp_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
273 QmpInputVisitor
*qiv
= to_qiv(v
);
274 QBool
*qbool
= qobject_to_qbool(qmp_input_get_object(qiv
, name
, true));
277 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
282 *obj
= qbool_get_bool(qbool
);
285 static void qmp_input_type_str(Visitor
*v
, const char *name
, char **obj
,
288 QmpInputVisitor
*qiv
= to_qiv(v
);
289 QString
*qstr
= qobject_to_qstring(qmp_input_get_object(qiv
, name
, true));
293 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
298 *obj
= g_strdup(qstring_get_str(qstr
));
301 static void qmp_input_type_number(Visitor
*v
, const char *name
, double *obj
,
304 QmpInputVisitor
*qiv
= to_qiv(v
);
305 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
309 qint
= qobject_to_qint(qobj
);
311 *obj
= qint_get_int(qobject_to_qint(qobj
));
315 qfloat
= qobject_to_qfloat(qobj
);
317 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
321 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
325 static void qmp_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
328 QmpInputVisitor
*qiv
= to_qiv(v
);
329 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
331 qobject_incref(qobj
);
335 static void qmp_input_optional(Visitor
*v
, const char *name
, bool *present
)
337 QmpInputVisitor
*qiv
= to_qiv(v
);
338 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
348 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
353 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
355 qobject_decref(v
->stack
[0].obj
);
359 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
, bool strict
)
363 v
= g_malloc0(sizeof(*v
));
365 v
->visitor
.type
= VISITOR_INPUT
;
366 v
->visitor
.start_struct
= qmp_input_start_struct
;
367 v
->visitor
.end_struct
= qmp_input_end_struct
;
368 v
->visitor
.start_list
= qmp_input_start_list
;
369 v
->visitor
.next_list
= qmp_input_next_list
;
370 v
->visitor
.end_list
= qmp_input_end_list
;
371 v
->visitor
.start_alternate
= qmp_input_start_alternate
;
372 v
->visitor
.type_int64
= qmp_input_type_int64
;
373 v
->visitor
.type_uint64
= qmp_input_type_uint64
;
374 v
->visitor
.type_bool
= qmp_input_type_bool
;
375 v
->visitor
.type_str
= qmp_input_type_str
;
376 v
->visitor
.type_number
= qmp_input_type_number
;
377 v
->visitor
.type_any
= qmp_input_type_any
;
378 v
->visitor
.optional
= qmp_input_optional
;
381 qmp_input_push(v
, obj
, NULL
);