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/qmp-input-visitor.h"
17 #include "qapi/visitor-impl.h"
18 #include "qemu/queue.h"
19 #include "qemu-common.h"
20 #include "qapi/qmp/types.h"
21 #include "qapi/qmp/qerror.h"
23 #define QIV_STACK_SIZE 1024
25 typedef struct StackObject
28 const QListEntry
*entry
;
32 struct QmpInputVisitor
35 StackObject stack
[QIV_STACK_SIZE
];
40 static QmpInputVisitor
*to_qiv(Visitor
*v
)
42 return container_of(v
, QmpInputVisitor
, visitor
);
45 static QObject
*qmp_input_get_object(QmpInputVisitor
*qiv
,
49 QObject
*qobj
= qiv
->stack
[qiv
->nb_stack
- 1].obj
;
52 if (name
&& qobject_type(qobj
) == QTYPE_QDICT
) {
53 if (qiv
->stack
[qiv
->nb_stack
- 1].h
&& consume
) {
54 g_hash_table_remove(qiv
->stack
[qiv
->nb_stack
- 1].h
, name
);
56 return qdict_get(qobject_to_qdict(qobj
), name
);
57 } else if (qiv
->stack
[qiv
->nb_stack
- 1].entry
) {
58 return qlist_entry_obj(qiv
->stack
[qiv
->nb_stack
- 1].entry
);
65 static void qdict_add_key(const char *key
, QObject
*obj
, void *opaque
)
67 GHashTable
*h
= opaque
;
68 g_hash_table_insert(h
, (gpointer
) key
, NULL
);
71 static void qmp_input_push(QmpInputVisitor
*qiv
, QObject
*obj
, Error
**errp
)
75 if (qiv
->nb_stack
>= QIV_STACK_SIZE
) {
76 error_setg(errp
, "An internal buffer overran");
80 qiv
->stack
[qiv
->nb_stack
].obj
= obj
;
81 qiv
->stack
[qiv
->nb_stack
].entry
= NULL
;
82 qiv
->stack
[qiv
->nb_stack
].h
= NULL
;
84 if (qiv
->strict
&& qobject_type(obj
) == QTYPE_QDICT
) {
85 h
= g_hash_table_new(g_str_hash
, g_str_equal
);
86 qdict_iter(qobject_to_qdict(obj
), qdict_add_key
, h
);
87 qiv
->stack
[qiv
->nb_stack
].h
= h
;
93 /** Only for qmp_input_pop. */
94 static gboolean
always_true(gpointer key
, gpointer val
, gpointer user_pkey
)
96 *(const char **)user_pkey
= (const char *)key
;
100 static void qmp_input_pop(QmpInputVisitor
*qiv
, Error
**errp
)
102 assert(qiv
->nb_stack
> 0);
105 GHashTable
* const top_ht
= qiv
->stack
[qiv
->nb_stack
- 1].h
;
107 if (g_hash_table_size(top_ht
)) {
109 g_hash_table_find(top_ht
, always_true
, &key
);
110 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, key
);
112 g_hash_table_unref(top_ht
);
119 static void qmp_input_start_struct(Visitor
*v
, const char *name
, void **obj
,
120 size_t size
, Error
**errp
)
122 QmpInputVisitor
*qiv
= to_qiv(v
);
123 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
126 if (!qobj
|| qobject_type(qobj
) != QTYPE_QDICT
) {
127 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
132 qmp_input_push(qiv
, qobj
, &err
);
134 error_propagate(errp
, err
);
139 *obj
= g_malloc0(size
);
143 static void qmp_input_end_struct(Visitor
*v
, Error
**errp
)
145 QmpInputVisitor
*qiv
= to_qiv(v
);
147 qmp_input_pop(qiv
, errp
);
150 static void qmp_input_start_implicit_struct(Visitor
*v
, void **obj
,
151 size_t size
, Error
**errp
)
154 *obj
= g_malloc0(size
);
158 static void qmp_input_start_list(Visitor
*v
, const char *name
, Error
**errp
)
160 QmpInputVisitor
*qiv
= to_qiv(v
);
161 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
163 if (!qobj
|| qobject_type(qobj
) != QTYPE_QLIST
) {
164 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
169 qmp_input_push(qiv
, qobj
, errp
);
172 static GenericList
*qmp_input_next_list(Visitor
*v
, GenericList
**list
)
174 QmpInputVisitor
*qiv
= to_qiv(v
);
176 StackObject
*so
= &qiv
->stack
[qiv
->nb_stack
- 1];
179 if (so
->entry
== NULL
) {
180 so
->entry
= qlist_first(qobject_to_qlist(so
->obj
));
183 so
->entry
= qlist_next(so
->entry
);
187 if (so
->entry
== NULL
) {
191 entry
= g_malloc0(sizeof(*entry
));
195 (*list
)->next
= entry
;
201 static void qmp_input_end_list(Visitor
*v
)
203 QmpInputVisitor
*qiv
= to_qiv(v
);
205 qmp_input_pop(qiv
, &error_abort
);
208 static void qmp_input_get_next_type(Visitor
*v
, const char *name
, QType
*type
,
209 bool promote_int
, Error
**errp
)
211 QmpInputVisitor
*qiv
= to_qiv(v
);
212 QObject
*qobj
= qmp_input_get_object(qiv
, name
, false);
215 error_setg(errp
, QERR_MISSING_PARAMETER
, name
? name
: "null");
218 *type
= qobject_type(qobj
);
219 if (promote_int
&& *type
== QTYPE_QINT
) {
220 *type
= QTYPE_QFLOAT
;
224 static void qmp_input_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
227 QmpInputVisitor
*qiv
= to_qiv(v
);
228 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
231 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
236 *obj
= qint_get_int(qint
);
239 static void qmp_input_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
242 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
243 QmpInputVisitor
*qiv
= to_qiv(v
);
244 QInt
*qint
= qobject_to_qint(qmp_input_get_object(qiv
, name
, true));
247 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
252 *obj
= qint_get_int(qint
);
255 static void qmp_input_type_bool(Visitor
*v
, const char *name
, bool *obj
,
258 QmpInputVisitor
*qiv
= to_qiv(v
);
259 QBool
*qbool
= qobject_to_qbool(qmp_input_get_object(qiv
, name
, true));
262 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
267 *obj
= qbool_get_bool(qbool
);
270 static void qmp_input_type_str(Visitor
*v
, const char *name
, char **obj
,
273 QmpInputVisitor
*qiv
= to_qiv(v
);
274 QString
*qstr
= qobject_to_qstring(qmp_input_get_object(qiv
, name
, true));
277 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
282 *obj
= g_strdup(qstring_get_str(qstr
));
285 static void qmp_input_type_number(Visitor
*v
, const char *name
, double *obj
,
288 QmpInputVisitor
*qiv
= to_qiv(v
);
289 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
293 qint
= qobject_to_qint(qobj
);
295 *obj
= qint_get_int(qobject_to_qint(qobj
));
299 qfloat
= qobject_to_qfloat(qobj
);
301 *obj
= qfloat_get_double(qobject_to_qfloat(qobj
));
305 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
309 static void qmp_input_type_any(Visitor
*v
, const char *name
, QObject
**obj
,
312 QmpInputVisitor
*qiv
= to_qiv(v
);
313 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
315 qobject_incref(qobj
);
319 static void qmp_input_optional(Visitor
*v
, const char *name
, bool *present
)
321 QmpInputVisitor
*qiv
= to_qiv(v
);
322 QObject
*qobj
= qmp_input_get_object(qiv
, name
, true);
332 Visitor
*qmp_input_get_visitor(QmpInputVisitor
*v
)
337 void qmp_input_visitor_cleanup(QmpInputVisitor
*v
)
339 qobject_decref(v
->stack
[0].obj
);
343 QmpInputVisitor
*qmp_input_visitor_new(QObject
*obj
)
347 v
= g_malloc0(sizeof(*v
));
349 v
->visitor
.start_struct
= qmp_input_start_struct
;
350 v
->visitor
.end_struct
= qmp_input_end_struct
;
351 v
->visitor
.start_implicit_struct
= qmp_input_start_implicit_struct
;
352 v
->visitor
.start_list
= qmp_input_start_list
;
353 v
->visitor
.next_list
= qmp_input_next_list
;
354 v
->visitor
.end_list
= qmp_input_end_list
;
355 v
->visitor
.type_enum
= input_type_enum
;
356 v
->visitor
.type_int64
= qmp_input_type_int64
;
357 v
->visitor
.type_uint64
= qmp_input_type_uint64
;
358 v
->visitor
.type_bool
= qmp_input_type_bool
;
359 v
->visitor
.type_str
= qmp_input_type_str
;
360 v
->visitor
.type_number
= qmp_input_type_number
;
361 v
->visitor
.type_any
= qmp_input_type_any
;
362 v
->visitor
.optional
= qmp_input_optional
;
363 v
->visitor
.get_next_type
= qmp_input_get_next_type
;
365 qmp_input_push(v
, obj
, NULL
);
371 QmpInputVisitor
*qmp_input_visitor_new_strict(QObject
*obj
)
375 v
= qmp_input_visitor_new(obj
);