blob: b4013ccfc531664bb304210da3e3d6cee5c2f480 [file] [log] [blame]
Michael Rothc40cc0a2011-07-19 14:50:33 -05001/*
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"
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +010015#include "qapi/qapi-visit-impl.h"
Michael Rothc40cc0a2011-07-19 14:50:33 -050016#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
23typedef struct StackObject
24{
25 const QObject *obj;
26 const QListEntry *entry;
27} StackObject;
28
29struct QmpInputVisitor
30{
31 Visitor visitor;
32 QObject *obj;
33 StackObject stack[QIV_STACK_SIZE];
34 int nb_stack;
35};
36
37static QmpInputVisitor *to_qiv(Visitor *v)
38{
39 return container_of(v, QmpInputVisitor, visitor);
40}
41
42static const QObject *qmp_input_get_object(QmpInputVisitor *qiv,
43 const char *name)
44{
45 const QObject *qobj;
46
47 if (qiv->nb_stack == 0) {
48 qobj = qiv->obj;
49 } else {
50 qobj = qiv->stack[qiv->nb_stack - 1].obj;
51 }
52
Paolo Bonzini47c6d3e2011-12-18 17:05:04 +010053 if (qobj) {
54 if (name && qobject_type(qobj) == QTYPE_QDICT) {
55 return qdict_get(qobject_to_qdict(qobj), name);
56 } else if (qiv->nb_stack > 0 && qobject_type(qobj) == QTYPE_QLIST) {
57 return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry);
58 }
Michael Rothc40cc0a2011-07-19 14:50:33 -050059 }
60
61 return qobj;
62}
63
64static void qmp_input_push(QmpInputVisitor *qiv, const QObject *obj, Error **errp)
65{
66 qiv->stack[qiv->nb_stack].obj = obj;
67 if (qobject_type(obj) == QTYPE_QLIST) {
68 qiv->stack[qiv->nb_stack].entry = qlist_first(qobject_to_qlist(obj));
69 }
70 qiv->nb_stack++;
71
72 if (qiv->nb_stack >= QIV_STACK_SIZE) {
73 error_set(errp, QERR_BUFFER_OVERRUN);
74 return;
75 }
76}
77
78static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
79{
Paolo Bonzini2c7ff932012-03-22 12:51:04 +010080 assert(qiv->nb_stack > 0);
Michael Rothc40cc0a2011-07-19 14:50:33 -050081 qiv->nb_stack--;
Michael Rothc40cc0a2011-07-19 14:50:33 -050082}
83
84static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,
85 const char *name, size_t size, Error **errp)
86{
87 QmpInputVisitor *qiv = to_qiv(v);
88 const QObject *qobj = qmp_input_get_object(qiv, name);
89
90 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
91 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
92 "QDict");
93 return;
94 }
95
96 qmp_input_push(qiv, qobj, errp);
97 if (error_is_set(errp)) {
98 return;
99 }
100
101 if (obj) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500102 *obj = g_malloc0(size);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500103 }
104}
105
106static void qmp_input_end_struct(Visitor *v, Error **errp)
107{
108 QmpInputVisitor *qiv = to_qiv(v);
109
110 qmp_input_pop(qiv, errp);
111}
112
113static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
114{
115 QmpInputVisitor *qiv = to_qiv(v);
116 const QObject *qobj = qmp_input_get_object(qiv, name);
117
118 if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
119 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
120 "list");
121 return;
122 }
123
124 qmp_input_push(qiv, qobj, errp);
125}
126
127static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
128 Error **errp)
129{
130 QmpInputVisitor *qiv = to_qiv(v);
131 GenericList *entry;
132 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
133
134 if (so->entry == NULL) {
135 return NULL;
136 }
137
Anthony Liguori7267c092011-08-20 22:09:37 -0500138 entry = g_malloc0(sizeof(*entry));
Michael Rothc40cc0a2011-07-19 14:50:33 -0500139 if (*list) {
140 so->entry = qlist_next(so->entry);
141 if (so->entry == NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500142 g_free(entry);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500143 return NULL;
144 }
145 (*list)->next = entry;
146 }
Michael Rothc40cc0a2011-07-19 14:50:33 -0500147
148 return entry;
149}
150
151static void qmp_input_end_list(Visitor *v, Error **errp)
152{
153 QmpInputVisitor *qiv = to_qiv(v);
154
155 qmp_input_pop(qiv, errp);
156}
157
158static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name,
159 Error **errp)
160{
161 QmpInputVisitor *qiv = to_qiv(v);
162 const QObject *qobj = qmp_input_get_object(qiv, name);
163
164 if (!qobj || qobject_type(qobj) != QTYPE_QINT) {
165 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
166 "integer");
167 return;
168 }
169
170 *obj = qint_get_int(qobject_to_qint(qobj));
171}
172
173static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name,
174 Error **errp)
175{
176 QmpInputVisitor *qiv = to_qiv(v);
177 const QObject *qobj = qmp_input_get_object(qiv, name);
178
179 if (!qobj || qobject_type(qobj) != QTYPE_QBOOL) {
180 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
181 "boolean");
182 return;
183 }
184
185 *obj = qbool_get_int(qobject_to_qbool(qobj));
186}
187
188static void qmp_input_type_str(Visitor *v, char **obj, const char *name,
189 Error **errp)
190{
191 QmpInputVisitor *qiv = to_qiv(v);
192 const QObject *qobj = qmp_input_get_object(qiv, name);
193
194 if (!qobj || qobject_type(qobj) != QTYPE_QSTRING) {
195 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
196 "string");
197 return;
198 }
199
Anthony Liguori7267c092011-08-20 22:09:37 -0500200 *obj = g_strdup(qstring_get_str(qobject_to_qstring(qobj)));
Michael Rothc40cc0a2011-07-19 14:50:33 -0500201}
202
203static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
204 Error **errp)
205{
206 QmpInputVisitor *qiv = to_qiv(v);
207 const QObject *qobj = qmp_input_get_object(qiv, name);
208
209 if (!qobj || qobject_type(qobj) != QTYPE_QFLOAT) {
210 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
211 "double");
212 return;
213 }
214
215 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
216}
217
Michael Rothc40cc0a2011-07-19 14:50:33 -0500218static void qmp_input_start_optional(Visitor *v, bool *present,
219 const char *name, Error **errp)
220{
221 QmpInputVisitor *qiv = to_qiv(v);
222 const QObject *qobj = qmp_input_get_object(qiv, name);
223
224 if (!qobj) {
225 *present = false;
226 return;
227 }
228
229 *present = true;
230}
231
Michael Rothc40cc0a2011-07-19 14:50:33 -0500232Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
233{
234 return &v->visitor;
235}
236
237void qmp_input_visitor_cleanup(QmpInputVisitor *v)
238{
239 qobject_decref(v->obj);
Anthony Liguori7267c092011-08-20 22:09:37 -0500240 g_free(v);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500241}
242
243QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
244{
245 QmpInputVisitor *v;
246
Anthony Liguori7267c092011-08-20 22:09:37 -0500247 v = g_malloc0(sizeof(*v));
Michael Rothc40cc0a2011-07-19 14:50:33 -0500248
249 v->visitor.start_struct = qmp_input_start_struct;
250 v->visitor.end_struct = qmp_input_end_struct;
251 v->visitor.start_list = qmp_input_start_list;
252 v->visitor.next_list = qmp_input_next_list;
253 v->visitor.end_list = qmp_input_end_list;
Paolo Bonzini0f71a1e2012-02-09 09:11:52 +0100254 v->visitor.type_enum = input_type_enum;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500255 v->visitor.type_int = qmp_input_type_int;
256 v->visitor.type_bool = qmp_input_type_bool;
257 v->visitor.type_str = qmp_input_type_str;
258 v->visitor.type_number = qmp_input_type_number;
259 v->visitor.start_optional = qmp_input_start_optional;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500260
261 v->obj = obj;
262 qobject_incref(v->obj);
263
264 return v;
265}