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