]> Git Repo - qemu.git/blob - qapi/qmp-input-visitor.c
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2016-02-09' into staging
[qemu.git] / qapi / qmp-input-visitor.c
1 /*
2  * Input Visitor
3  *
4  * Copyright (C) 2012-2016 Red Hat, Inc.
5  * Copyright IBM, Corp. 2011
6  *
7  * Authors:
8  *  Anthony Liguori   <[email protected]>
9  *
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.
12  *
13  */
14
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"
22
23 #define QIV_STACK_SIZE 1024
24
25 typedef struct StackObject
26 {
27     QObject *obj;
28     const QListEntry *entry;
29     GHashTable *h;
30 } StackObject;
31
32 struct QmpInputVisitor
33 {
34     Visitor visitor;
35     StackObject stack[QIV_STACK_SIZE];
36     int nb_stack;
37     bool strict;
38 };
39
40 static QmpInputVisitor *to_qiv(Visitor *v)
41 {
42     return container_of(v, QmpInputVisitor, visitor);
43 }
44
45 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
46                                      const char *name,
47                                      bool consume)
48 {
49     QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj;
50
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);
55             }
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);
59         }
60     }
61
62     return qobj;
63 }
64
65 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
66 {
67     GHashTable *h = opaque;
68     g_hash_table_insert(h, (gpointer) key, NULL);
69 }
70
71 static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
72 {
73     GHashTable *h;
74
75     if (qiv->nb_stack >= QIV_STACK_SIZE) {
76         error_setg(errp, "An internal buffer overran");
77         return;
78     }
79
80     qiv->stack[qiv->nb_stack].obj = obj;
81     qiv->stack[qiv->nb_stack].entry = NULL;
82     qiv->stack[qiv->nb_stack].h = NULL;
83
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;
88     }
89
90     qiv->nb_stack++;
91 }
92
93 /** Only for qmp_input_pop. */
94 static gboolean always_true(gpointer key, gpointer val, gpointer user_pkey)
95 {
96     *(const char **)user_pkey = (const char *)key;
97     return TRUE;
98 }
99
100 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
101 {
102     assert(qiv->nb_stack > 0);
103
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);
111             }
112             g_hash_table_unref(top_ht);
113         }
114     }
115
116     qiv->nb_stack--;
117 }
118
119 static void qmp_input_start_struct(Visitor *v, const char *name, void **obj,
120                                    size_t size, Error **errp)
121 {
122     QmpInputVisitor *qiv = to_qiv(v);
123     QObject *qobj = qmp_input_get_object(qiv, name, true);
124     Error *err = NULL;
125
126     if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
127         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
128                    "QDict");
129         return;
130     }
131
132     qmp_input_push(qiv, qobj, &err);
133     if (err) {
134         error_propagate(errp, err);
135         return;
136     }
137
138     if (obj) {
139         *obj = g_malloc0(size);
140     }
141 }
142
143 static void qmp_input_end_struct(Visitor *v, Error **errp)
144 {
145     QmpInputVisitor *qiv = to_qiv(v);
146
147     qmp_input_pop(qiv, errp);
148 }
149
150 static void qmp_input_start_implicit_struct(Visitor *v, void **obj,
151                                             size_t size, Error **errp)
152 {
153     if (obj) {
154         *obj = g_malloc0(size);
155     }
156 }
157
158 static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
159 {
160     QmpInputVisitor *qiv = to_qiv(v);
161     QObject *qobj = qmp_input_get_object(qiv, name, true);
162
163     if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
164         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
165                    "list");
166         return;
167     }
168
169     qmp_input_push(qiv, qobj, errp);
170 }
171
172 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list)
173 {
174     QmpInputVisitor *qiv = to_qiv(v);
175     GenericList *entry;
176     StackObject *so = &qiv->stack[qiv->nb_stack - 1];
177     bool first;
178
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;
185     }
186
187     if (so->entry == NULL) {
188         return NULL;
189     }
190
191     entry = g_malloc0(sizeof(*entry));
192     if (first) {
193         *list = entry;
194     } else {
195         (*list)->next = entry;
196     }
197
198     return entry;
199 }
200
201 static void qmp_input_end_list(Visitor *v)
202 {
203     QmpInputVisitor *qiv = to_qiv(v);
204
205     qmp_input_pop(qiv, &error_abort);
206 }
207
208 static void qmp_input_get_next_type(Visitor *v, const char *name, QType *type,
209                                     bool promote_int, Error **errp)
210 {
211     QmpInputVisitor *qiv = to_qiv(v);
212     QObject *qobj = qmp_input_get_object(qiv, name, false);
213
214     if (!qobj) {
215         error_setg(errp, QERR_MISSING_PARAMETER, name ? name : "null");
216         return;
217     }
218     *type = qobject_type(qobj);
219     if (promote_int && *type == QTYPE_QINT) {
220         *type = QTYPE_QFLOAT;
221     }
222 }
223
224 static void qmp_input_type_int64(Visitor *v, const char *name, int64_t *obj,
225                                  Error **errp)
226 {
227     QmpInputVisitor *qiv = to_qiv(v);
228     QInt *qint = qobject_to_qint(qmp_input_get_object(qiv, name, true));
229
230     if (!qint) {
231         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
232                    "integer");
233         return;
234     }
235
236     *obj = qint_get_int(qint);
237 }
238
239 static void qmp_input_type_uint64(Visitor *v, const char *name, uint64_t *obj,
240                                   Error **errp)
241 {
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));
245
246     if (!qint) {
247         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
248                    "integer");
249         return;
250     }
251
252     *obj = qint_get_int(qint);
253 }
254
255 static void qmp_input_type_bool(Visitor *v, const char *name, bool *obj,
256                                 Error **errp)
257 {
258     QmpInputVisitor *qiv = to_qiv(v);
259     QBool *qbool = qobject_to_qbool(qmp_input_get_object(qiv, name, true));
260
261     if (!qbool) {
262         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
263                    "boolean");
264         return;
265     }
266
267     *obj = qbool_get_bool(qbool);
268 }
269
270 static void qmp_input_type_str(Visitor *v, const char *name, char **obj,
271                                Error **errp)
272 {
273     QmpInputVisitor *qiv = to_qiv(v);
274     QString *qstr = qobject_to_qstring(qmp_input_get_object(qiv, name, true));
275
276     if (!qstr) {
277         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
278                    "string");
279         return;
280     }
281
282     *obj = g_strdup(qstring_get_str(qstr));
283 }
284
285 static void qmp_input_type_number(Visitor *v, const char *name, double *obj,
286                                   Error **errp)
287 {
288     QmpInputVisitor *qiv = to_qiv(v);
289     QObject *qobj = qmp_input_get_object(qiv, name, true);
290     QInt *qint;
291     QFloat *qfloat;
292
293     qint = qobject_to_qint(qobj);
294     if (qint) {
295         *obj = qint_get_int(qobject_to_qint(qobj));
296         return;
297     }
298
299     qfloat = qobject_to_qfloat(qobj);
300     if (qfloat) {
301         *obj = qfloat_get_double(qobject_to_qfloat(qobj));
302         return;
303     }
304
305     error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
306                "number");
307 }
308
309 static void qmp_input_type_any(Visitor *v, const char *name, QObject **obj,
310                                Error **errp)
311 {
312     QmpInputVisitor *qiv = to_qiv(v);
313     QObject *qobj = qmp_input_get_object(qiv, name, true);
314
315     qobject_incref(qobj);
316     *obj = qobj;
317 }
318
319 static void qmp_input_optional(Visitor *v, const char *name, bool *present)
320 {
321     QmpInputVisitor *qiv = to_qiv(v);
322     QObject *qobj = qmp_input_get_object(qiv, name, true);
323
324     if (!qobj) {
325         *present = false;
326         return;
327     }
328
329     *present = true;
330 }
331
332 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
333 {
334     return &v->visitor;
335 }
336
337 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
338 {
339     qobject_decref(v->stack[0].obj);
340     g_free(v);
341 }
342
343 QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
344 {
345     QmpInputVisitor *v;
346
347     v = g_malloc0(sizeof(*v));
348
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;
364
365     qmp_input_push(v, obj, NULL);
366     qobject_incref(obj);
367
368     return v;
369 }
370
371 QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
372 {
373     QmpInputVisitor *v;
374
375     v = qmp_input_visitor_new(obj);
376     v->strict = true;
377
378     return v;
379 }
This page took 0.045867 seconds and 4 git commands to generate.