]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/string-output-visitor.c
qapi: Add parameter to visit_end_*
[mirror_qemu.git] / qapi / string-output-visitor.c
CommitLineData
a020f980
PB
1/*
2 * String printing Visitor
3 *
08f9541d 4 * Copyright Red Hat, Inc. 2012-2016
a020f980
PB
5 *
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
7 *
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
10 *
11 */
12
cbf21151 13#include "qemu/osdep.h"
a020f980 14#include "qemu-common.h"
7b1b5d19
PB
15#include "qapi/string-output-visitor.h"
16#include "qapi/visitor-impl.h"
0b7593e0 17#include "qemu/host-utils.h"
e41b509d 18#include <math.h>
69e25563
HT
19#include "qemu/range.h"
20
21enum ListMode {
22 LM_NONE, /* not traversing a list of repeated options */
d9f62dde 23 LM_STARTED, /* next_list() ready to be called */
69e25563
HT
24
25 LM_IN_PROGRESS, /* next_list() has been called.
26 *
27 * Generating the next list link will consume the most
28 * recently parsed QemuOpt instance of the repeated
29 * option.
30 *
31 * Parsing a value into the list link will examine the
32 * next QemuOpt instance of the repeated option, and
33 * possibly enter LM_SIGNED_INTERVAL or
34 * LM_UNSIGNED_INTERVAL.
35 */
36
37 LM_SIGNED_INTERVAL, /* next_list() has been called.
38 *
39 * Generating the next list link will consume the most
40 * recently stored element from the signed interval,
41 * parsed from the most recent QemuOpt instance of the
42 * repeated option. This may consume QemuOpt itself
43 * and return to LM_IN_PROGRESS.
44 *
45 * Parsing a value into the list link will store the
46 * next element of the signed interval.
47 */
48
49 LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */
50
d9f62dde 51 LM_END, /* next_list() called, about to see last element. */
69e25563
HT
52};
53
54typedef enum ListMode ListMode;
a020f980
PB
55
56struct StringOutputVisitor
57{
58 Visitor visitor;
0b7593e0 59 bool human;
69e25563 60 GString *string;
69e25563
HT
61 ListMode list_mode;
62 union {
63 int64_t s;
64 uint64_t u;
65 } range_start, range_end;
66 GList *ranges;
1158bb2a 67 void *list; /* Only needed for sanity checking the caller */
a020f980
PB
68};
69
d7bea75d
EB
70static StringOutputVisitor *to_sov(Visitor *v)
71{
72 return container_of(v, StringOutputVisitor, visitor);
73}
74
a020f980
PB
75static void string_output_set(StringOutputVisitor *sov, char *string)
76{
69e25563
HT
77 if (sov->string) {
78 g_string_free(sov->string, true);
79 }
80 sov->string = g_string_new(string);
81 g_free(string);
82}
83
84static void string_output_append(StringOutputVisitor *sov, int64_t a)
85{
86 Range *r = g_malloc0(sizeof(*r));
a0efbf16
MA
87
88 range_set_bounds(r, a, a);
7c47959d 89 sov->ranges = range_list_insert(sov->ranges, r);
69e25563
HT
90}
91
92static void string_output_append_range(StringOutputVisitor *sov,
93 int64_t s, int64_t e)
94{
95 Range *r = g_malloc0(sizeof(*r));
a0efbf16
MA
96
97 range_set_bounds(r, s, e);
7c47959d 98 sov->ranges = range_list_insert(sov->ranges, r);
69e25563
HT
99}
100
101static void format_string(StringOutputVisitor *sov, Range *r, bool next,
102 bool human)
103{
a0efbf16 104 if (range_lob(r) != range_upb(r)) {
69e25563 105 if (human) {
684531ad 106 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
a0efbf16 107 range_lob(r), range_upb(r));
69e25563
HT
108
109 } else {
110 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
a0efbf16 111 range_lob(r), range_upb(r));
69e25563
HT
112 }
113 } else {
114 if (human) {
a0efbf16 115 g_string_append_printf(sov->string, "0x%" PRIx64, range_lob(r));
69e25563 116 } else {
a0efbf16 117 g_string_append_printf(sov->string, "%" PRId64, range_lob(r));
69e25563
HT
118 }
119 }
120 if (next) {
121 g_string_append(sov->string, ",");
122 }
a020f980
PB
123}
124
0b2a0d6b 125static void print_type_int64(Visitor *v, const char *name, int64_t *obj,
4c40314a 126 Error **errp)
a020f980 127{
d7bea75d 128 StringOutputVisitor *sov = to_sov(v);
69e25563
HT
129 GList *l;
130
131 switch (sov->list_mode) {
132 case LM_NONE:
133 string_output_append(sov, *obj);
134 break;
135
136 case LM_STARTED:
137 sov->range_start.s = *obj;
138 sov->range_end.s = *obj;
139 sov->list_mode = LM_IN_PROGRESS;
140 return;
141
142 case LM_IN_PROGRESS:
143 if (sov->range_end.s + 1 == *obj) {
144 sov->range_end.s++;
145 } else {
146 if (sov->range_start.s == sov->range_end.s) {
147 string_output_append(sov, sov->range_end.s);
148 } else {
149 assert(sov->range_start.s < sov->range_end.s);
150 string_output_append_range(sov, sov->range_start.s,
151 sov->range_end.s);
152 }
153
154 sov->range_start.s = *obj;
155 sov->range_end.s = *obj;
156 }
157 return;
158
159 case LM_END:
160 if (sov->range_end.s + 1 == *obj) {
161 sov->range_end.s++;
162 assert(sov->range_start.s < sov->range_end.s);
163 string_output_append_range(sov, sov->range_start.s,
164 sov->range_end.s);
165 } else {
166 if (sov->range_start.s == sov->range_end.s) {
167 string_output_append(sov, sov->range_end.s);
168 } else {
169 assert(sov->range_start.s < sov->range_end.s);
170
171 string_output_append_range(sov, sov->range_start.s,
172 sov->range_end.s);
173 }
174 string_output_append(sov, *obj);
175 }
176 break;
177
178 default:
179 abort();
180 }
181
182 l = sov->ranges;
183 while (l) {
184 Range *r = l->data;
185 format_string(sov, r, l->next != NULL, false);
186 l = l->next;
187 }
0b7593e0
PB
188
189 if (sov->human) {
69e25563
HT
190 l = sov->ranges;
191 g_string_append(sov->string, " (");
192 while (l) {
193 Range *r = l->data;
56fdfb61 194 format_string(sov, r, l->next != NULL, true);
69e25563
HT
195 l = l->next;
196 }
197 g_string_append(sov->string, ")");
0b7593e0 198 }
0b7593e0
PB
199}
200
0b2a0d6b 201static void print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
f755dea7
EB
202 Error **errp)
203{
204 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
205 int64_t i = *obj;
0b2a0d6b 206 print_type_int64(v, name, &i, errp);
f755dea7
EB
207}
208
0b2a0d6b
EB
209static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
210 Error **errp)
0b7593e0 211{
d7bea75d 212 StringOutputVisitor *sov = to_sov(v);
e41b509d 213 static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
0b7593e0
PB
214 uint64_t div, val;
215 char *out;
216 int i;
217
218 if (!sov->human) {
e41b509d 219 out = g_strdup_printf("%"PRIu64, *obj);
0b7593e0
PB
220 string_output_set(sov, out);
221 return;
222 }
223
224 val = *obj;
225
e41b509d
PB
226 /* The exponent (returned in i) minus one gives us
227 * floor(log2(val * 1024 / 1000). The correction makes us
228 * switch to the higher power when the integer part is >= 1000.
229 */
230 frexp(val / (1000.0 / 1024.0), &i);
231 i = (i - 1) / 10;
232 assert(i < ARRAY_SIZE(suffixes));
0b7593e0
PB
233 div = 1ULL << (i * 10);
234
e41b509d
PB
235 out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
236 (double)val/div, suffixes[i], i ? "iB" : "");
0b7593e0 237 string_output_set(sov, out);
a020f980
PB
238}
239
0b2a0d6b 240static void print_type_bool(Visitor *v, const char *name, bool *obj,
a020f980
PB
241 Error **errp)
242{
d7bea75d 243 StringOutputVisitor *sov = to_sov(v);
a020f980
PB
244 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
245}
246
0b2a0d6b 247static void print_type_str(Visitor *v, const char *name, char **obj,
a020f980
PB
248 Error **errp)
249{
d7bea75d 250 StringOutputVisitor *sov = to_sov(v);
0b7593e0
PB
251 char *out;
252
253 if (sov->human) {
254 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
255 } else {
256 out = g_strdup(*obj ? *obj : "");
257 }
258 string_output_set(sov, out);
a020f980
PB
259}
260
0b2a0d6b 261static void print_type_number(Visitor *v, const char *name, double *obj,
a020f980
PB
262 Error **errp)
263{
d7bea75d 264 StringOutputVisitor *sov = to_sov(v);
173bbb75 265 string_output_set(sov, g_strdup_printf("%f", *obj));
a020f980
PB
266}
267
69e25563 268static void
d9f62dde
EB
269start_list(Visitor *v, const char *name, GenericList **list, size_t size,
270 Error **errp)
69e25563 271{
d7bea75d 272 StringOutputVisitor *sov = to_sov(v);
69e25563
HT
273
274 /* we can't traverse a list in a list */
275 assert(sov->list_mode == LM_NONE);
d9f62dde
EB
276 /* We don't support visits without a list */
277 assert(list);
1158bb2a 278 sov->list = list;
d9f62dde
EB
279 /* List handling is only needed if there are at least two elements */
280 if (*list && (*list)->next) {
281 sov->list_mode = LM_STARTED;
282 }
69e25563
HT
283}
284
d9f62dde 285static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
69e25563 286{
d7bea75d 287 StringOutputVisitor *sov = to_sov(v);
d9f62dde 288 GenericList *ret = tail->next;
69e25563 289
d9f62dde
EB
290 if (ret && !ret->next) {
291 sov->list_mode = LM_END;
69e25563 292 }
69e25563
HT
293 return ret;
294}
295
1158bb2a 296static void end_list(Visitor *v, void **obj)
69e25563 297{
d7bea75d 298 StringOutputVisitor *sov = to_sov(v);
69e25563 299
1158bb2a 300 assert(sov->list == obj);
69e25563
HT
301 assert(sov->list_mode == LM_STARTED ||
302 sov->list_mode == LM_END ||
303 sov->list_mode == LM_NONE ||
304 sov->list_mode == LM_IN_PROGRESS);
305 sov->list_mode = LM_NONE;
69e25563
HT
306}
307
a020f980
PB
308char *string_output_get_string(StringOutputVisitor *sov)
309{
69e25563 310 char *string = g_string_free(sov->string, false);
a020f980
PB
311 sov->string = NULL;
312 return string;
313}
314
315Visitor *string_output_get_visitor(StringOutputVisitor *sov)
316{
317 return &sov->visitor;
318}
319
0d156683
MT
320static void free_range(void *range, void *dummy)
321{
322 g_free(range);
323}
324
a020f980
PB
325void string_output_visitor_cleanup(StringOutputVisitor *sov)
326{
69e25563
HT
327 if (sov->string) {
328 g_string_free(sov->string, true);
329 }
330
0d156683
MT
331 g_list_foreach(sov->ranges, free_range, NULL);
332 g_list_free(sov->ranges);
a020f980
PB
333 g_free(sov);
334}
335
0b7593e0 336StringOutputVisitor *string_output_visitor_new(bool human)
a020f980
PB
337{
338 StringOutputVisitor *v;
339
340 v = g_malloc0(sizeof(*v));
341
69e25563 342 v->string = g_string_new(NULL);
0b7593e0 343 v->human = human;
983f52d4 344 v->visitor.type = VISITOR_OUTPUT;
4c40314a 345 v->visitor.type_int64 = print_type_int64;
f755dea7 346 v->visitor.type_uint64 = print_type_uint64;
0b7593e0 347 v->visitor.type_size = print_type_size;
a020f980
PB
348 v->visitor.type_bool = print_type_bool;
349 v->visitor.type_str = print_type_str;
350 v->visitor.type_number = print_type_number;
69e25563
HT
351 v->visitor.start_list = start_list;
352 v->visitor.next_list = next_list;
353 v->visitor.end_list = end_list;
a020f980
PB
354
355 return v;
356}