blob: 7ab64468d9a58a6a4529dba6a08435a17d07af36 [file] [log] [blame]
Paolo Bonzinia020f982012-02-09 09:36:37 +01001/*
2 * String printing Visitor
3 *
Eric Blake08f95412016-01-29 06:48:59 -07004 * Copyright Red Hat, Inc. 2012-2016
Paolo Bonzinia020f982012-02-09 09:36:37 +01005 *
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
Peter Maydellcbf21152016-01-29 17:49:57 +000013#include "qemu/osdep.h"
Paolo Bonzinia020f982012-02-09 09:36:37 +010014#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/string-output-visitor.h"
16#include "qapi/visitor-impl.h"
Paolo Bonzini0b7593e2014-02-08 11:01:50 +010017#include "qemu/host-utils.h"
Paolo Bonzinie41b5092014-02-08 11:01:57 +010018#include <math.h>
Hu Tao69e25562014-06-10 19:15:28 +080019#include "qemu/range.h"
20
21enum ListMode {
22 LM_NONE, /* not traversing a list of repeated options */
Eric Blaked9f62dd2016-04-28 15:45:31 -060023 LM_STARTED, /* next_list() ready to be called */
Hu Tao69e25562014-06-10 19:15:28 +080024
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
Eric Blaked9f62dd2016-04-28 15:45:31 -060051 LM_END, /* next_list() called, about to see last element. */
Hu Tao69e25562014-06-10 19:15:28 +080052};
53
54typedef enum ListMode ListMode;
Paolo Bonzinia020f982012-02-09 09:36:37 +010055
56struct StringOutputVisitor
57{
58 Visitor visitor;
Paolo Bonzini0b7593e2014-02-08 11:01:50 +010059 bool human;
Hu Tao69e25562014-06-10 19:15:28 +080060 GString *string;
Eric Blake3b098d52016-06-09 10:48:43 -060061 char **result;
Hu Tao69e25562014-06-10 19:15:28 +080062 ListMode list_mode;
63 union {
64 int64_t s;
65 uint64_t u;
66 } range_start, range_end;
67 GList *ranges;
Eric Blake1158bb22016-06-09 10:48:34 -060068 void *list; /* Only needed for sanity checking the caller */
Paolo Bonzinia020f982012-02-09 09:36:37 +010069};
70
Eric Blaked7bea752016-01-29 06:48:38 -070071static StringOutputVisitor *to_sov(Visitor *v)
72{
73 return container_of(v, StringOutputVisitor, visitor);
74}
75
Paolo Bonzinia020f982012-02-09 09:36:37 +010076static void string_output_set(StringOutputVisitor *sov, char *string)
77{
Hu Tao69e25562014-06-10 19:15:28 +080078 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));
Markus Armbrustera0efbf12016-07-01 13:47:47 +020088
89 range_set_bounds(r, a, a);
Eric Blake7c479592016-05-31 10:41:29 -060090 sov->ranges = range_list_insert(sov->ranges, r);
Hu Tao69e25562014-06-10 19:15:28 +080091}
92
93static void string_output_append_range(StringOutputVisitor *sov,
94 int64_t s, int64_t e)
95{
96 Range *r = g_malloc0(sizeof(*r));
Markus Armbrustera0efbf12016-07-01 13:47:47 +020097
98 range_set_bounds(r, s, e);
Eric Blake7c479592016-05-31 10:41:29 -060099 sov->ranges = range_list_insert(sov->ranges, r);
Hu Tao69e25562014-06-10 19:15:28 +0800100}
101
102static void format_string(StringOutputVisitor *sov, Range *r, bool next,
103 bool human)
104{
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200105 if (range_lob(r) != range_upb(r)) {
Hu Tao69e25562014-06-10 19:15:28 +0800106 if (human) {
Hu Tao684531a2014-06-20 13:55:42 +0800107 g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64,
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200108 range_lob(r), range_upb(r));
Hu Tao69e25562014-06-10 19:15:28 +0800109
110 } else {
111 g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64,
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200112 range_lob(r), range_upb(r));
Hu Tao69e25562014-06-10 19:15:28 +0800113 }
114 } else {
115 if (human) {
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200116 g_string_append_printf(sov->string, "0x%" PRIx64, range_lob(r));
Hu Tao69e25562014-06-10 19:15:28 +0800117 } else {
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200118 g_string_append_printf(sov->string, "%" PRId64, range_lob(r));
Hu Tao69e25562014-06-10 19:15:28 +0800119 }
120 }
121 if (next) {
122 g_string_append(sov->string, ",");
123 }
Paolo Bonzinia020f982012-02-09 09:36:37 +0100124}
125
Eric Blake0b2a0d62016-01-29 06:48:56 -0700126static void print_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake4c403142016-01-29 06:48:49 -0700127 Error **errp)
Paolo Bonzinia020f982012-02-09 09:36:37 +0100128{
Eric Blaked7bea752016-01-29 06:48:38 -0700129 StringOutputVisitor *sov = to_sov(v);
Hu Tao69e25562014-06-10 19:15:28 +0800130 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 }
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100189
190 if (sov->human) {
Hu Tao69e25562014-06-10 19:15:28 +0800191 l = sov->ranges;
192 g_string_append(sov->string, " (");
193 while (l) {
194 Range *r = l->data;
Michael S. Tsirkin56fdfb62014-06-18 17:59:51 +0300195 format_string(sov, r, l->next != NULL, true);
Hu Tao69e25562014-06-10 19:15:28 +0800196 l = l->next;
197 }
198 g_string_append(sov->string, ")");
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100199 }
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100200}
201
Eric Blake0b2a0d62016-01-29 06:48:56 -0700202static void print_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blakef755dea2016-01-29 06:48:50 -0700203 Error **errp)
204{
205 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
206 int64_t i = *obj;
Eric Blake0b2a0d62016-01-29 06:48:56 -0700207 print_type_int64(v, name, &i, errp);
Eric Blakef755dea2016-01-29 06:48:50 -0700208}
209
Eric Blake0b2a0d62016-01-29 06:48:56 -0700210static void print_type_size(Visitor *v, const char *name, uint64_t *obj,
211 Error **errp)
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100212{
Eric Blaked7bea752016-01-29 06:48:38 -0700213 StringOutputVisitor *sov = to_sov(v);
Peter Xu22951aa2017-05-12 12:17:40 +0800214 uint64_t val;
215 char *out, *psize;
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100216
217 if (!sov->human) {
Paolo Bonzinie41b5092014-02-08 11:01:57 +0100218 out = g_strdup_printf("%"PRIu64, *obj);
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100219 string_output_set(sov, out);
220 return;
221 }
222
223 val = *obj;
Peter Xu22951aa2017-05-12 12:17:40 +0800224 psize = size_to_str(val);
225 out = g_strdup_printf("%"PRIu64" (%s)", val, psize);
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100226 string_output_set(sov, out);
Peter Xu22951aa2017-05-12 12:17:40 +0800227
228 g_free(psize);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100229}
230
Eric Blake0b2a0d62016-01-29 06:48:56 -0700231static void print_type_bool(Visitor *v, const char *name, bool *obj,
Paolo Bonzinia020f982012-02-09 09:36:37 +0100232 Error **errp)
233{
Eric Blaked7bea752016-01-29 06:48:38 -0700234 StringOutputVisitor *sov = to_sov(v);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100235 string_output_set(sov, g_strdup(*obj ? "true" : "false"));
236}
237
Eric Blake0b2a0d62016-01-29 06:48:56 -0700238static void print_type_str(Visitor *v, const char *name, char **obj,
Paolo Bonzinia020f982012-02-09 09:36:37 +0100239 Error **errp)
240{
Eric Blaked7bea752016-01-29 06:48:38 -0700241 StringOutputVisitor *sov = to_sov(v);
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100242 char *out;
243
244 if (sov->human) {
245 out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>");
246 } else {
247 out = g_strdup(*obj ? *obj : "");
248 }
249 string_output_set(sov, out);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100250}
251
Eric Blake0b2a0d62016-01-29 06:48:56 -0700252static void print_type_number(Visitor *v, const char *name, double *obj,
Paolo Bonzinia020f982012-02-09 09:36:37 +0100253 Error **errp)
254{
Eric Blaked7bea752016-01-29 06:48:38 -0700255 StringOutputVisitor *sov = to_sov(v);
Michael Roth173bbb72012-04-30 09:33:30 -0500256 string_output_set(sov, g_strdup_printf("%f", *obj));
Paolo Bonzinia020f982012-02-09 09:36:37 +0100257}
258
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200259static void print_type_null(Visitor *v, const char *name, QNull **obj,
260 Error **errp)
Greg Kurza7333712016-12-16 16:26:09 +0100261{
262 StringOutputVisitor *sov = to_sov(v);
263 char *out;
264
265 if (sov->human) {
266 out = g_strdup("<null>");
267 } else {
268 out = g_strdup("");
269 }
270 string_output_set(sov, out);
271}
272
Hu Tao69e25562014-06-10 19:15:28 +0800273static void
Eric Blaked9f62dd2016-04-28 15:45:31 -0600274start_list(Visitor *v, const char *name, GenericList **list, size_t size,
275 Error **errp)
Hu Tao69e25562014-06-10 19:15:28 +0800276{
Eric Blaked7bea752016-01-29 06:48:38 -0700277 StringOutputVisitor *sov = to_sov(v);
Hu Tao69e25562014-06-10 19:15:28 +0800278
279 /* we can't traverse a list in a list */
280 assert(sov->list_mode == LM_NONE);
Eric Blaked9f62dd2016-04-28 15:45:31 -0600281 /* We don't support visits without a list */
282 assert(list);
Eric Blake1158bb22016-06-09 10:48:34 -0600283 sov->list = list;
Eric Blaked9f62dd2016-04-28 15:45:31 -0600284 /* List handling is only needed if there are at least two elements */
285 if (*list && (*list)->next) {
286 sov->list_mode = LM_STARTED;
287 }
Hu Tao69e25562014-06-10 19:15:28 +0800288}
289
Eric Blaked9f62dd2016-04-28 15:45:31 -0600290static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
Hu Tao69e25562014-06-10 19:15:28 +0800291{
Eric Blaked7bea752016-01-29 06:48:38 -0700292 StringOutputVisitor *sov = to_sov(v);
Eric Blaked9f62dd2016-04-28 15:45:31 -0600293 GenericList *ret = tail->next;
Hu Tao69e25562014-06-10 19:15:28 +0800294
Eric Blaked9f62dd2016-04-28 15:45:31 -0600295 if (ret && !ret->next) {
296 sov->list_mode = LM_END;
Hu Tao69e25562014-06-10 19:15:28 +0800297 }
Hu Tao69e25562014-06-10 19:15:28 +0800298 return ret;
299}
300
Eric Blake1158bb22016-06-09 10:48:34 -0600301static void end_list(Visitor *v, void **obj)
Hu Tao69e25562014-06-10 19:15:28 +0800302{
Eric Blaked7bea752016-01-29 06:48:38 -0700303 StringOutputVisitor *sov = to_sov(v);
Hu Tao69e25562014-06-10 19:15:28 +0800304
Eric Blake1158bb22016-06-09 10:48:34 -0600305 assert(sov->list == obj);
Hu Tao69e25562014-06-10 19:15:28 +0800306 assert(sov->list_mode == LM_STARTED ||
307 sov->list_mode == LM_END ||
308 sov->list_mode == LM_NONE ||
309 sov->list_mode == LM_IN_PROGRESS);
310 sov->list_mode = LM_NONE;
Hu Tao69e25562014-06-10 19:15:28 +0800311}
312
Eric Blake3b098d52016-06-09 10:48:43 -0600313static void string_output_complete(Visitor *v, void *opaque)
Paolo Bonzinia020f982012-02-09 09:36:37 +0100314{
Eric Blake3b098d52016-06-09 10:48:43 -0600315 StringOutputVisitor *sov = to_sov(v);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100316
Eric Blake3b098d52016-06-09 10:48:43 -0600317 assert(opaque == sov->result);
318 *sov->result = g_string_free(sov->string, false);
319 sov->string = NULL;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100320}
321
Michael S. Tsirkin0d156682014-06-16 18:07:03 +0300322static void free_range(void *range, void *dummy)
323{
324 g_free(range);
325}
326
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600327static void string_output_free(Visitor *v)
328{
329 StringOutputVisitor *sov = to_sov(v);
330
Hu Tao69e25562014-06-10 19:15:28 +0800331 if (sov->string) {
332 g_string_free(sov->string, true);
333 }
334
Michael S. Tsirkin0d156682014-06-16 18:07:03 +0300335 g_list_foreach(sov->ranges, free_range, NULL);
336 g_list_free(sov->ranges);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100337 g_free(sov);
338}
339
Eric Blake3b098d52016-06-09 10:48:43 -0600340Visitor *string_output_visitor_new(bool human, char **result)
Paolo Bonzinia020f982012-02-09 09:36:37 +0100341{
342 StringOutputVisitor *v;
343
344 v = g_malloc0(sizeof(*v));
345
Hu Tao69e25562014-06-10 19:15:28 +0800346 v->string = g_string_new(NULL);
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100347 v->human = human;
Eric Blake3b098d52016-06-09 10:48:43 -0600348 v->result = result;
349 *result = NULL;
350
Eric Blake983f52d2016-04-28 15:45:09 -0600351 v->visitor.type = VISITOR_OUTPUT;
Eric Blake4c403142016-01-29 06:48:49 -0700352 v->visitor.type_int64 = print_type_int64;
Eric Blakef755dea2016-01-29 06:48:50 -0700353 v->visitor.type_uint64 = print_type_uint64;
Paolo Bonzini0b7593e2014-02-08 11:01:50 +0100354 v->visitor.type_size = print_type_size;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100355 v->visitor.type_bool = print_type_bool;
356 v->visitor.type_str = print_type_str;
357 v->visitor.type_number = print_type_number;
Greg Kurza7333712016-12-16 16:26:09 +0100358 v->visitor.type_null = print_type_null;
Hu Tao69e25562014-06-10 19:15:28 +0800359 v->visitor.start_list = start_list;
360 v->visitor.next_list = next_list;
361 v->visitor.end_list = end_list;
Eric Blake3b098d52016-06-09 10:48:43 -0600362 v->visitor.complete = string_output_complete;
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600363 v->visitor.free = string_output_free;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100364
Eric Blake3b098d52016-06-09 10:48:43 -0600365 return &v->visitor;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100366}