Merge branch 'memory/core' of git://git.kernel.org/pub/scm/virt/kvm/qemu-kvm
[sdk/emulator/qemu.git] / qapi / qmp-input-visitor.c
1 /*
2  * Input Visitor
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
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.
11  *
12  */
13
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"
19 #include "qerror.h"
20
21 #define QIV_STACK_SIZE 1024
22
23 typedef struct StackObject
24 {
25     QObject *obj;
26     const QListEntry *entry;
27     GHashTable *h;
28 } StackObject;
29
30 struct QmpInputVisitor
31 {
32     Visitor visitor;
33     StackObject stack[QIV_STACK_SIZE];
34     int nb_stack;
35     bool strict;
36 };
37
38 static QmpInputVisitor *to_qiv(Visitor *v)
39 {
40     return container_of(v, QmpInputVisitor, visitor);
41 }
42
43 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
44                                      const char *name)
45 {
46     QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj;
47
48     if (qobj) {
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);
52             }
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);
56         }
57     }
58
59     return qobj;
60 }
61
62 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
63 {
64     GHashTable *h = opaque;
65     g_hash_table_insert(h, (gpointer) key, NULL);
66 }
67
68 static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
69 {
70     GHashTable *h;
71
72     if (qiv->nb_stack >= QIV_STACK_SIZE) {
73         error_set(errp, QERR_BUFFER_OVERRUN);
74         return;
75     }
76
77     qiv->stack[qiv->nb_stack].obj = obj;
78     qiv->stack[qiv->nb_stack].entry = NULL;
79     qiv->stack[qiv->nb_stack].h = NULL;
80
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;
85     }
86
87     qiv->nb_stack++;
88 }
89
90 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
91 {
92     GHashTableIter iter;
93     gpointer key;
94
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);
99         }
100         g_hash_table_unref(qiv->stack[qiv->nb_stack - 1].h);
101     }
102
103     assert(qiv->nb_stack > 0);
104     qiv->nb_stack--;
105 }
106
107 static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,
108                                    const char *name, size_t size, Error **errp)
109 {
110     QmpInputVisitor *qiv = to_qiv(v);
111     QObject *qobj = qmp_input_get_object(qiv, name);
112     Error *err = NULL;
113
114     if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
115         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
116                   "QDict");
117         return;
118     }
119
120     qmp_input_push(qiv, qobj, &err);
121     if (err) {
122         error_propagate(errp, err);
123         return;
124     }
125
126     if (obj) {
127         *obj = g_malloc0(size);
128     }
129 }
130
131 static void qmp_input_end_struct(Visitor *v, Error **errp)
132 {
133     QmpInputVisitor *qiv = to_qiv(v);
134
135     qmp_input_pop(qiv, errp);
136 }
137
138 static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
139 {
140     QmpInputVisitor *qiv = to_qiv(v);
141     QObject *qobj = qmp_input_get_object(qiv, name);
142
143     if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
144         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
145                   "list");
146         return;
147     }
148
149     qmp_input_push(qiv, qobj, errp);
150 }
151
152 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
153                                         Error **errp)
154 {
155     QmpInputVisitor *qiv = to_qiv(v);
156     GenericList *entry;
157     StackObject *so = &qiv->stack[qiv->nb_stack - 1];
158     bool first;
159
160     if (so->entry == NULL) {
161         so->entry = qlist_first(qobject_to_qlist(so->obj));
162         first = true;
163     } else {
164         so->entry = qlist_next(so->entry);
165         first = false;
166     }
167
168     if (so->entry == NULL) {
169         return NULL;
170     }
171
172     entry = g_malloc0(sizeof(*entry));
173     if (first) {
174         *list = entry;
175     } else {
176         (*list)->next = entry;
177     }
178
179     return entry;
180 }
181
182 static void qmp_input_end_list(Visitor *v, Error **errp)
183 {
184     QmpInputVisitor *qiv = to_qiv(v);
185
186     qmp_input_pop(qiv, errp);
187 }
188
189 static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name,
190                                Error **errp)
191 {
192     QmpInputVisitor *qiv = to_qiv(v);
193     QObject *qobj = qmp_input_get_object(qiv, name);
194
195     if (!qobj || qobject_type(qobj) != QTYPE_QINT) {
196         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
197                   "integer");
198         return;
199     }
200
201     *obj = qint_get_int(qobject_to_qint(qobj));
202 }
203
204 static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name,
205                                 Error **errp)
206 {
207     QmpInputVisitor *qiv = to_qiv(v);
208     QObject *qobj = qmp_input_get_object(qiv, name);
209
210     if (!qobj || qobject_type(qobj) != QTYPE_QBOOL) {
211         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
212                   "boolean");
213         return;
214     }
215
216     *obj = qbool_get_int(qobject_to_qbool(qobj));
217 }
218
219 static void qmp_input_type_str(Visitor *v, char **obj, const char *name,
220                                Error **errp)
221 {
222     QmpInputVisitor *qiv = to_qiv(v);
223     QObject *qobj = qmp_input_get_object(qiv, name);
224
225     if (!qobj || qobject_type(qobj) != QTYPE_QSTRING) {
226         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
227                   "string");
228         return;
229     }
230
231     *obj = g_strdup(qstring_get_str(qobject_to_qstring(qobj)));
232 }
233
234 static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
235                                   Error **errp)
236 {
237     QmpInputVisitor *qiv = to_qiv(v);
238     QObject *qobj = qmp_input_get_object(qiv, name);
239
240     if (!qobj || qobject_type(qobj) != QTYPE_QFLOAT) {
241         error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
242                   "double");
243         return;
244     }
245
246     *obj = qfloat_get_double(qobject_to_qfloat(qobj));
247 }
248
249 static void qmp_input_start_optional(Visitor *v, bool *present,
250                                      const char *name, Error **errp)
251 {
252     QmpInputVisitor *qiv = to_qiv(v);
253     QObject *qobj = qmp_input_get_object(qiv, name);
254
255     if (!qobj) {
256         *present = false;
257         return;
258     }
259
260     *present = true;
261 }
262
263 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
264 {
265     return &v->visitor;
266 }
267
268 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
269 {
270     qobject_decref(v->stack[0].obj);
271     g_free(v);
272 }
273
274 QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
275 {
276     QmpInputVisitor *v;
277
278     v = g_malloc0(sizeof(*v));
279
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;
291
292     qmp_input_push(v, obj, NULL);
293     qobject_incref(obj);
294
295     return v;
296 }
297
298 QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
299 {
300     QmpInputVisitor *v;
301
302     v = qmp_input_visitor_new(obj);
303     v->strict = true;
304
305     return v;
306 }