]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/string-output-visitor.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[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;
3b098d56 61 char **result;
69e25563
HT
62 ListMode list_mode;
63 union {
64 int64_t s;
65 uint64_t u;
66 } range_start, range_end;
67 GList *ranges;
1158bb2a 68 void *list; /* Only needed for sanity checking the caller */
a020f980
PB
69};
70
d7bea75d
EB
71static StringOutputVisitor *to_sov(Visitor *v)
72{
73 return container_of(v, StringOutputVisitor, visitor);
74}
75
a020f980
PB
76static void string_output_set(StringOutputVisitor *sov, char *string)
77{
69e25563
HT
78 if (sov->string) {
79 g_string_free(sov->string, true);
80 }
81 sov->string = g_string_new(string);
82 g_free(string);
83}
84
85static void string_output_append(StringOutputVisitor *sov, int64_t a)
86{
87 Range *r = g_malloc0(sizeof(*r));
a0efbf16
MA
88
89 range_set_bounds(r, a, a);
7c47959d 90 sov->ranges = range_list_insert(sov->ranges, r);
69e25563
HT
91}
92
93static void string_output_append_range(StringOutputVisitor *sov,
94 int64_t s, int64_t e)
95{
96 Range *r = g_malloc0(sizeof(*r));
a0efbf16
MA
97
98 range_set_bounds(r, s, e);
7c47959d 99 sov->ranges = range_list_insert(sov->ranges, r);
69e25563
HT
100}
101
102static void format_string(StringOutputVisitor *sov, Range *r, bool next,
103 bool human)
104{
a0efbf16 105 if (range_lob(r) != range_upb(r)) {
69e25563 106 if (human) {
684531ad 107 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
a0efbf16 108 range_lob(r), range_upb(r));
69e25563
HT
109
110 } else {
111 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
a0efbf16 112 range_lob(r), range_upb(r));
69e25563
HT
113 }
114 } else {
115 if (human) {
a0efbf16 116 g_string_append_printf(sov->string, "0x%" PRIx64, range_lob(r));
69e25563 117 } else {
a0efbf16 118 g_string_append_printf(sov->string, "%" PRId64, range_lob(r));
69e25563
HT
119 }
120 }
121 if (next) {
122 g_string_append(sov->string, ",");
123 }
a020f980
PB
124}
125
0b2a0d6b 126static void print_type_int64(Visitor *v, const char *name, int64_t *obj,
4c40314a 127 Error **errp)
a020f980 128{
d7bea75d 129 StringOutputVisitor *sov = to_sov(v);
69e25563
HT
130 GList *l;
131
132 switch (sov->list_mode) {
133 case LM_NONE:
134 string_output_append(sov, *obj);
135 break;
136
137 case LM_STARTED:
138 sov->range_start.s = *obj;
139 sov->range_end.s = *obj;
140 sov->list_mode = LM_IN_PROGRESS;
141 return;
142
143 case LM_IN_PROGRESS:
144 if (sov->range_end.s + 1 == *obj) {
145 sov->range_end.s++;
146 } else {
147 if (sov->range_start.s == sov->range_end.s) {
148 string_output_append(sov, sov->range_end.s);
149 } else {
150 assert(sov->range_start.s < sov->range_end.s);
151 string_output_append_range(sov, sov->range_start.s,
152 sov->range_end.s);
153 }
154
155 sov->range_start.s = *obj;
156 sov->range_end.s = *obj;
157 }
158 return;
159
160 case LM_END:
161 if (sov->range_end.s + 1 == *obj) {
162 sov->range_end.s++;
163 assert(sov->range_start.s < sov->range_end.s);
164 string_output_append_range(sov, sov->range_start.s,
165 sov->range_end.s);
166 } else {
167 if (sov->range_start.s == sov->range_end.s) {
168 string_output_append(sov, sov->range_end.s);
169 } else {
170 assert(sov->range_start.s < sov->range_end.s);
171
172 string_output_append_range(sov, sov->range_start.s,
173 sov->range_end.s);
174 }
175 string_output_append(sov, *obj);
176 }
177 break;
178
179 default:
180 abort();
181 }
182
183 l = sov->ranges;
184 while (l) {
185 Range *r = l->data;
186 format_string(sov, r, l->next != NULL, false);
187 l = l->next;
188 }
0b7593e0
PB
189
190 if (sov->human) {
69e25563
HT
191 l = sov->ranges;
192 g_string_append(sov->string, " (");
193 while (l) {
194 Range *r = l->data;
56fdfb61 195 format_string(sov, r, l->next != NULL, true);
69e25563
HT
196 l = l->next;
197 }
198 g_string_append(sov->string, ")");
0b7593e0 199 }
0b7593e0
PB
200}
201
0b2a0d6b 202static void print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
f755dea7
EB
203 Error **errp)
204{
205 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
206 int64_t i = *obj;
0b2a0d6b 207 print_type_int64(v, name, &i, errp);
f755dea7
EB
208}
209
0b2a0d6b
EB
210static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
211 Error **errp)
0b7593e0 212{
d7bea75d 213 StringOutputVisitor *sov = to_sov(v);
e41b509d 214 static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
0b7593e0
PB
215 uint64_t div, val;
216 char *out;
217 int i;
218
219 if (!sov->human) {
e41b509d 220 out = g_strdup_printf("%"PRIu64, *obj);
0b7593e0
PB
221 string_output_set(sov, out);
222 return;
223 }
224
225 val = *obj;
226
e41b509d
PB
227 /* The exponent (returned in i) minus one gives us
228 * floor(log2(val * 1024 / 1000). The correction makes us
229 * switch to the higher power when the integer part is >= 1000.
230 */
231 frexp(val / (1000.0 / 1024.0), &i);
232 i = (i - 1) / 10;
233 assert(i < ARRAY_SIZE(suffixes));
0b7593e0
PB
234 div = 1ULL << (i * 10);
235
e41b509d
PB
236 out = g_strdup_printf("%"PRIu64" (%0.3g %c%s)", val,
237 (double)val/div, suffixes[i], i ? "iB" : "");
0b7593e0 238 string_output_set(sov, out);
a020f980
PB
239}
240
0b2a0d6b 241static void print_type_bool(Visitor *v, const char *name, bool *obj,
a020f980
PB
242 Error **errp)
243{
d7bea75d 244 StringOutputVisitor *sov = to_sov(v);
a020f980
PB
245 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
246}
247
0b2a0d6b 248static void print_type_str(Visitor *v, const char *name, char **obj,
a020f980
PB
249 Error **errp)
250{
d7bea75d 251 StringOutputVisitor *sov = to_sov(v);
0b7593e0
PB
252 char *out;
253
254 if (sov->human) {
255 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
256 } else {
257 out = g_strdup(*obj ? *obj : "");
258 }
259 string_output_set(sov, out);
a020f980
PB
260}
261
0b2a0d6b 262static void print_type_number(Visitor *v, const char *name, double *obj,
a020f980
PB
263 Error **errp)
264{
d7bea75d 265 StringOutputVisitor *sov = to_sov(v);
173bbb75 266 string_output_set(sov, g_strdup_printf("%f", *obj));
a020f980
PB
267}
268
69e25563 269static void
d9f62dde
EB
270start_list(Visitor *v, const char *name, GenericList **list, size_t size,
271 Error **errp)
69e25563 272{
d7bea75d 273 StringOutputVisitor *sov = to_sov(v);
69e25563
HT
274
275 /* we can't traverse a list in a list */
276 assert(sov->list_mode == LM_NONE);
d9f62dde
EB
277 /* We don't support visits without a list */
278 assert(list);
1158bb2a 279 sov->list = list;
d9f62dde
EB
280 /* List handling is only needed if there are at least two elements */
281 if (*list && (*list)->next) {
282 sov->list_mode = LM_STARTED;
283 }
69e25563
HT
284}
285
d9f62dde 286static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
69e25563 287{
d7bea75d 288 StringOutputVisitor *sov = to_sov(v);
d9f62dde 289 GenericList *ret = tail->next;
69e25563 290
d9f62dde
EB
291 if (ret && !ret->next) {
292 sov->list_mode = LM_END;
69e25563 293 }
69e25563
HT
294 return ret;
295}
296
1158bb2a 297static void end_list(Visitor *v, void **obj)
69e25563 298{
d7bea75d 299 StringOutputVisitor *sov = to_sov(v);
69e25563 300
1158bb2a 301 assert(sov->list == obj);
69e25563
HT
302 assert(sov->list_mode == LM_STARTED ||
303 sov->list_mode == LM_END ||
304 sov->list_mode == LM_NONE ||
305 sov->list_mode == LM_IN_PROGRESS);
306 sov->list_mode = LM_NONE;
69e25563
HT
307}
308
3b098d56 309static void string_output_complete(Visitor *v, void *opaque)
a020f980 310{
3b098d56 311 StringOutputVisitor *sov = to_sov(v);
a020f980 312
3b098d56
EB
313 assert(opaque == sov->result);
314 *sov->result = g_string_free(sov->string, false);
315 sov->string = NULL;
a020f980
PB
316}
317
0d156683
MT
318static void free_range(void *range, void *dummy)
319{
320 g_free(range);
321}
322
2c0ef9f4
EB
323static void string_output_free(Visitor *v)
324{
325 StringOutputVisitor *sov = to_sov(v);
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
3b098d56 336Visitor *string_output_visitor_new(bool human, char **result)
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;
3b098d56
EB
344 v->result = result;
345 *result = NULL;
346
983f52d4 347 v->visitor.type = VISITOR_OUTPUT;
4c40314a 348 v->visitor.type_int64 = print_type_int64;
f755dea7 349 v->visitor.type_uint64 = print_type_uint64;
0b7593e0 350 v->visitor.type_size = print_type_size;
a020f980
PB
351 v->visitor.type_bool = print_type_bool;
352 v->visitor.type_str = print_type_str;
353 v->visitor.type_number = print_type_number;
69e25563
HT
354 v->visitor.start_list = start_list;
355 v->visitor.next_list = next_list;
356 v->visitor.end_list = end_list;
3b098d56 357 v->visitor.complete = string_output_complete;
2c0ef9f4 358 v->visitor.free = string_output_free;
a020f980 359
3b098d56 360 return &v->visitor;
a020f980 361}