hw: Add support for LSI SAS1068 (mptsas) device
[qemu/rayw.git] / qapi / qmp-input-visitor.c
blob362a1a33a836dff0ba1e4b31d4ba93a2e5abb683
1 /*
2 * Input Visitor
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
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
27 QObject *obj;
28 const QListEntry *entry;
29 GHashTable *h;
30 } StackObject;
32 struct QmpInputVisitor
34 Visitor visitor;
35 StackObject stack[QIV_STACK_SIZE];
36 int nb_stack;
37 bool strict;
40 static QmpInputVisitor *to_qiv(Visitor *v)
42 return container_of(v, QmpInputVisitor, visitor);
45 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
46 const char *name,
47 bool consume)
49 QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj;
51 if (qobj) {
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);
62 return qobj;
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)
73 GHashTable *h;
75 if (qiv->nb_stack >= QIV_STACK_SIZE) {
76 error_setg(errp, "An internal buffer overran");
77 return;
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;
90 qiv->nb_stack++;
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;
97 return TRUE;
100 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
102 assert(qiv->nb_stack > 0);
104 if (qiv->strict) {
105 GHashTable * const top_ht = qiv->stack[qiv->nb_stack - 1].h;
106 if (top_ht) {
107 if (g_hash_table_size(top_ht)) {
108 const char *key;
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);
116 qiv->nb_stack--;
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);
124 Error *err = NULL;
126 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
127 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
128 "QDict");
129 return;
132 qmp_input_push(qiv, qobj, &err);
133 if (err) {
134 error_propagate(errp, err);
135 return;
138 if (obj) {
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)
153 if (obj) {
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",
165 "list");
166 return;
169 qmp_input_push(qiv, qobj, errp);
172 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list)
174 QmpInputVisitor *qiv = to_qiv(v);
175 GenericList *entry;
176 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
177 bool first;
179 if (so->entry == NULL) {
180 so->entry = qlist_first(qobject_to_qlist(so->obj));
181 first = true;
182 } else {
183 so->entry = qlist_next(so->entry);
184 first = false;
187 if (so->entry == NULL) {
188 return NULL;
191 entry = g_malloc0(sizeof(*entry));
192 if (first) {
193 *list = entry;
194 } else {
195 (*list)->next = entry;
198 return 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);
214 if (!qobj) {
215 error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
216 return;
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,
225 Error **errp)
227 QmpInputVisitor *qiv = to_qiv(v);
228 QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
230 if (!qint) {
231 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
232 "integer");
233 return;
236 *obj = qint_get_int(qint);
239 static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
240 Error **errp)
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));
246 if (!qint) {
247 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
248 "integer");
249 return;
252 *obj = qint_get_int(qint);
255 static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
256 Error **errp)
258 QmpInputVisitor *qiv = to_qiv(v);
259 QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
261 if (!qbool) {
262 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
263 "boolean");
264 return;
267 *obj = qbool_get_bool(qbool);
270 static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
271 Error **errp)
273 QmpInputVisitor *qiv = to_qiv(v);
274 QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
276 if (!qstr) {
277 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
278 "string");
279 return;
282 *obj = g_strdup(qstring_get_str(qstr));
285 static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
286 Error **errp)
288 QmpInputVisitor *qiv = to_qiv(v);
289 QObject *qobj = qmp_input_get_object(qiv, name, true);
290 QInt *qint;
291 QFloat *qfloat;
293 qint = qobject_to_qint(qobj);
294 if (qint) {
295 *obj = qint_get_int(qobject_to_qint(qobj));
296 return;
299 qfloat = qobject_to_qfloat(qobj);
300 if (qfloat) {
301 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
302 return;
305 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
306 "number");
309 static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
310 Error **errp)
312 QmpInputVisitor *qiv = to_qiv(v);
313 QObject *qobj = qmp_input_get_object(qiv, name, true);
315 qobject_incref(qobj);
316 *obj = 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);
324 if (!qobj) {
325 *present = false;
326 return;
329 *present = true;
332 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
334 return &v->visitor;
337 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
339 qobject_decref(v->stack[0].obj);
340 g_free(v);
343 QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
345 QmpInputVisitor *v;
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);
366 qobject_incref(obj);
368 return v;
371 QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
373 QmpInputVisitor *v;
375 v = qmp_input_visitor_new(obj);
376 v->strict = true;
378 return v;