]> Git Repo - qemu.git/blob - qapi/qmp-output-visitor.c
Merge remote-tracking branch 'remotes/stsquad/tags/pull-build-test-20160209' into...
[qemu.git] / qapi / qmp-output-visitor.c
1 /*
2  * Core Definitions for QAPI/QMP Command Registry
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-output-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
22 typedef struct QStackEntry
23 {
24     QObject *value;
25     bool is_list_head;
26     QTAILQ_ENTRY(QStackEntry) node;
27 } QStackEntry;
28
29 typedef QTAILQ_HEAD(QStack, QStackEntry) QStack;
30
31 struct QmpOutputVisitor
32 {
33     Visitor visitor;
34     QStack stack; /* Stack of containers that haven't yet been finished */
35     QObject *root; /* Root of the output visit */
36 };
37
38 #define qmp_output_add(qov, name, value) \
39     qmp_output_add_obj(qov, name, QOBJECT(value))
40 #define qmp_output_push(qov, value) qmp_output_push_obj(qov, QOBJECT(value))
41
42 static QmpOutputVisitor *to_qov(Visitor *v)
43 {
44     return container_of(v, QmpOutputVisitor, visitor);
45 }
46
47 /* Push @value onto the stack of current QObjects being built */
48 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value)
49 {
50     QStackEntry *e = g_malloc0(sizeof(*e));
51
52     assert(qov->root);
53     assert(value);
54     e->value = value;
55     if (qobject_type(e->value) == QTYPE_QLIST) {
56         e->is_list_head = true;
57     }
58     QTAILQ_INSERT_HEAD(&qov->stack, e, node);
59 }
60
61 /* Pop a value off the stack of QObjects being built, and return it. */
62 static QObject *qmp_output_pop(QmpOutputVisitor *qov)
63 {
64     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
65     QObject *value;
66
67     assert(e);
68     QTAILQ_REMOVE(&qov->stack, e, node);
69     value = e->value;
70     assert(value);
71     g_free(e);
72     return value;
73 }
74
75 /* Add @value to the current QObject being built.
76  * If the stack is visiting a dictionary or list, @value is now owned
77  * by that container. Otherwise, @value is now the root.  */
78 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
79                                QObject *value)
80 {
81     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
82     QObject *cur = e ? e->value : NULL;
83
84     if (!cur) {
85         /* FIXME we should require the user to reset the visitor, rather
86          * than throwing away the previous root */
87         qobject_decref(qov->root);
88         qov->root = value;
89     } else {
90         switch (qobject_type(cur)) {
91         case QTYPE_QDICT:
92             assert(name);
93             qdict_put_obj(qobject_to_qdict(cur), name, value);
94             break;
95         case QTYPE_QLIST:
96             qlist_append_obj(qobject_to_qlist(cur), value);
97             break;
98         default:
99             g_assert_not_reached();
100         }
101     }
102 }
103
104 static void qmp_output_start_struct(Visitor *v, const char *name, void **obj,
105                                     size_t unused, Error **errp)
106 {
107     QmpOutputVisitor *qov = to_qov(v);
108     QDict *dict = qdict_new();
109
110     qmp_output_add(qov, name, dict);
111     qmp_output_push(qov, dict);
112 }
113
114 static void qmp_output_end_struct(Visitor *v, Error **errp)
115 {
116     QmpOutputVisitor *qov = to_qov(v);
117     qmp_output_pop(qov);
118 }
119
120 static void qmp_output_start_list(Visitor *v, const char *name, Error **errp)
121 {
122     QmpOutputVisitor *qov = to_qov(v);
123     QList *list = qlist_new();
124
125     qmp_output_add(qov, name, list);
126     qmp_output_push(qov, list);
127 }
128
129 static GenericList *qmp_output_next_list(Visitor *v, GenericList **listp)
130 {
131     GenericList *list = *listp;
132     QmpOutputVisitor *qov = to_qov(v);
133     QStackEntry *e = QTAILQ_FIRST(&qov->stack);
134
135     assert(e);
136     if (e->is_list_head) {
137         e->is_list_head = false;
138         return list;
139     }
140
141     return list ? list->next : NULL;
142 }
143
144 static void qmp_output_end_list(Visitor *v)
145 {
146     QmpOutputVisitor *qov = to_qov(v);
147     qmp_output_pop(qov);
148 }
149
150 static void qmp_output_type_int64(Visitor *v, const char *name, int64_t *obj,
151                                   Error **errp)
152 {
153     QmpOutputVisitor *qov = to_qov(v);
154     qmp_output_add(qov, name, qint_from_int(*obj));
155 }
156
157 static void qmp_output_type_uint64(Visitor *v, const char *name, uint64_t *obj,
158                                    Error **errp)
159 {
160     /* FIXME: QMP outputs values larger than INT64_MAX as negative */
161     QmpOutputVisitor *qov = to_qov(v);
162     qmp_output_add(qov, name, qint_from_int(*obj));
163 }
164
165 static void qmp_output_type_bool(Visitor *v, const char *name, bool *obj,
166                                  Error **errp)
167 {
168     QmpOutputVisitor *qov = to_qov(v);
169     qmp_output_add(qov, name, qbool_from_bool(*obj));
170 }
171
172 static void qmp_output_type_str(Visitor *v, const char *name, char **obj,
173                                 Error **errp)
174 {
175     QmpOutputVisitor *qov = to_qov(v);
176     if (*obj) {
177         qmp_output_add(qov, name, qstring_from_str(*obj));
178     } else {
179         qmp_output_add(qov, name, qstring_from_str(""));
180     }
181 }
182
183 static void qmp_output_type_number(Visitor *v, const char *name, double *obj,
184                                    Error **errp)
185 {
186     QmpOutputVisitor *qov = to_qov(v);
187     qmp_output_add(qov, name, qfloat_from_double(*obj));
188 }
189
190 static void qmp_output_type_any(Visitor *v, const char *name, QObject **obj,
191                                 Error **errp)
192 {
193     QmpOutputVisitor *qov = to_qov(v);
194     qobject_incref(*obj);
195     qmp_output_add_obj(qov, name, *obj);
196 }
197
198 /* Finish building, and return the root object. Will not be NULL. */
199 QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
200 {
201     /* FIXME: we should require that a visit occurred, and that it is
202      * complete (no starts without a matching end) */
203     QObject *obj = qov->root;
204     if (obj) {
205         qobject_incref(obj);
206     } else {
207         obj = qnull();
208     }
209     return obj;
210 }
211
212 Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
213 {
214     return &v->visitor;
215 }
216
217 void qmp_output_visitor_cleanup(QmpOutputVisitor *v)
218 {
219     QStackEntry *e, *tmp;
220
221     QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) {
222         QTAILQ_REMOVE(&v->stack, e, node);
223         g_free(e);
224     }
225
226     qobject_decref(v->root);
227     g_free(v);
228 }
229
230 QmpOutputVisitor *qmp_output_visitor_new(void)
231 {
232     QmpOutputVisitor *v;
233
234     v = g_malloc0(sizeof(*v));
235
236     v->visitor.start_struct = qmp_output_start_struct;
237     v->visitor.end_struct = qmp_output_end_struct;
238     v->visitor.start_list = qmp_output_start_list;
239     v->visitor.next_list = qmp_output_next_list;
240     v->visitor.end_list = qmp_output_end_list;
241     v->visitor.type_enum = output_type_enum;
242     v->visitor.type_int64 = qmp_output_type_int64;
243     v->visitor.type_uint64 = qmp_output_type_uint64;
244     v->visitor.type_bool = qmp_output_type_bool;
245     v->visitor.type_str = qmp_output_type_str;
246     v->visitor.type_number = qmp_output_type_number;
247     v->visitor.type_any = qmp_output_type_any;
248
249     QTAILQ_INIT(&v->stack);
250
251     return v;
252 }
This page took 0.038257 seconds and 4 git commands to generate.