2 * String printing Visitor
4 * Copyright Red Hat, Inc. 2012-2016
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qemu/cutils.h"
15 #include "qapi/string-output-visitor.h"
16 #include "qapi/visitor-impl.h"
18 #include "qemu/range.h"
21 LM_NONE
, /* not traversing a list of repeated options */
22 LM_STARTED
, /* next_list() ready to be called */
24 LM_IN_PROGRESS
, /* next_list() has been called.
26 * Generating the next list link will consume the most
27 * recently parsed QemuOpt instance of the repeated
30 * Parsing a value into the list link will examine the
31 * next QemuOpt instance of the repeated option, and
32 * possibly enter LM_SIGNED_INTERVAL or
33 * LM_UNSIGNED_INTERVAL.
36 LM_SIGNED_INTERVAL
, /* next_list() has been called.
38 * Generating the next list link will consume the most
39 * recently stored element from the signed interval,
40 * parsed from the most recent QemuOpt instance of the
41 * repeated option. This may consume QemuOpt itself
42 * and return to LM_IN_PROGRESS.
44 * Parsing a value into the list link will store the
45 * next element of the signed interval.
48 LM_UNSIGNED_INTERVAL
,/* Same as above, only for an unsigned interval. */
50 LM_END
, /* next_list() called, about to see last element. */
53 typedef enum ListMode ListMode
;
55 struct StringOutputVisitor
65 } range_start
, range_end
;
67 void *list
; /* Only needed for sanity checking the caller */
70 static StringOutputVisitor
*to_sov(Visitor
*v
)
72 return container_of(v
, StringOutputVisitor
, visitor
);
75 static void string_output_set(StringOutputVisitor
*sov
, char *string
)
77 switch (sov
->list_mode
) {
79 sov
->list_mode
= LM_IN_PROGRESS
;
83 g_string_free(sov
->string
, true);
85 sov
->string
= g_string_new(string
);
91 g_string_append(sov
->string
, ", ");
92 g_string_append(sov
->string
, string
);
100 static void string_output_append(StringOutputVisitor
*sov
, int64_t a
)
102 Range
*r
= g_malloc0(sizeof(*r
));
104 range_set_bounds(r
, a
, a
);
105 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
108 static void string_output_append_range(StringOutputVisitor
*sov
,
109 int64_t s
, int64_t e
)
111 Range
*r
= g_malloc0(sizeof(*r
));
113 range_set_bounds(r
, s
, e
);
114 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
117 static void format_string(StringOutputVisitor
*sov
, Range
*r
, bool next
,
120 if (range_lob(r
) != range_upb(r
)) {
122 g_string_append_printf(sov
->string
, "0x%" PRIx64
"-0x%" PRIx64
,
123 range_lob(r
), range_upb(r
));
126 g_string_append_printf(sov
->string
, "%" PRId64
"-%" PRId64
,
127 range_lob(r
), range_upb(r
));
131 g_string_append_printf(sov
->string
, "0x%" PRIx64
, range_lob(r
));
133 g_string_append_printf(sov
->string
, "%" PRId64
, range_lob(r
));
137 g_string_append(sov
->string
, ",");
141 static bool print_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
144 StringOutputVisitor
*sov
= to_sov(v
);
147 switch (sov
->list_mode
) {
149 string_output_append(sov
, *obj
);
153 sov
->range_start
.s
= *obj
;
154 sov
->range_end
.s
= *obj
;
155 sov
->list_mode
= LM_IN_PROGRESS
;
159 if (sov
->range_end
.s
+ 1 == *obj
) {
162 if (sov
->range_start
.s
== sov
->range_end
.s
) {
163 string_output_append(sov
, sov
->range_end
.s
);
165 assert(sov
->range_start
.s
< sov
->range_end
.s
);
166 string_output_append_range(sov
, sov
->range_start
.s
,
170 sov
->range_start
.s
= *obj
;
171 sov
->range_end
.s
= *obj
;
176 if (sov
->range_end
.s
+ 1 == *obj
) {
178 assert(sov
->range_start
.s
< sov
->range_end
.s
);
179 string_output_append_range(sov
, sov
->range_start
.s
,
182 if (sov
->range_start
.s
== sov
->range_end
.s
) {
183 string_output_append(sov
, sov
->range_end
.s
);
185 assert(sov
->range_start
.s
< sov
->range_end
.s
);
187 string_output_append_range(sov
, sov
->range_start
.s
,
190 string_output_append(sov
, *obj
);
201 format_string(sov
, r
, l
->next
!= NULL
, false);
207 g_string_append(sov
->string
, " (");
210 format_string(sov
, r
, l
->next
!= NULL
, true);
213 g_string_append(sov
->string
, ")");
219 static bool print_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
222 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
224 return print_type_int64(v
, name
, &i
, errp
);
227 static bool print_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
230 StringOutputVisitor
*sov
= to_sov(v
);
235 out
= g_strdup_printf("%"PRIu64
, *obj
);
236 string_output_set(sov
, out
);
241 psize
= size_to_str(val
);
242 out
= g_strdup_printf("%"PRIu64
" (%s)", val
, psize
);
243 string_output_set(sov
, out
);
249 static bool print_type_bool(Visitor
*v
, const char *name
, bool *obj
,
252 StringOutputVisitor
*sov
= to_sov(v
);
253 string_output_set(sov
, g_strdup(*obj
? "true" : "false"));
257 static bool print_type_str(Visitor
*v
, const char *name
, char **obj
,
260 StringOutputVisitor
*sov
= to_sov(v
);
264 out
= *obj
? g_strdup_printf("\"%s\"", *obj
) : g_strdup("<null>");
266 out
= g_strdup(*obj
? *obj
: "");
268 string_output_set(sov
, out
);
272 static bool print_type_number(Visitor
*v
, const char *name
, double *obj
,
275 StringOutputVisitor
*sov
= to_sov(v
);
276 string_output_set(sov
, g_strdup_printf("%.17g", *obj
));
280 static bool print_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
283 StringOutputVisitor
*sov
= to_sov(v
);
287 out
= g_strdup("<null>");
291 string_output_set(sov
, out
);
295 static bool start_struct(Visitor
*v
, const char *name
, void **obj
,
296 size_t size
, Error
**errp
)
301 static void end_struct(Visitor
*v
, void **obj
)
303 StringOutputVisitor
*sov
= to_sov(v
);
305 /* TODO actually print struct fields */
306 string_output_set(sov
, g_strdup("<omitted>"));
310 start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
313 StringOutputVisitor
*sov
= to_sov(v
);
315 /* we can't traverse a list in a list */
316 assert(sov
->list_mode
== LM_NONE
);
317 /* We don't support visits without a list */
320 /* List handling is only needed if there are at least two elements */
321 if (*list
&& (*list
)->next
) {
322 sov
->list_mode
= LM_STARTED
;
327 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
329 StringOutputVisitor
*sov
= to_sov(v
);
330 GenericList
*ret
= tail
->next
;
332 if (ret
&& !ret
->next
) {
333 sov
->list_mode
= LM_END
;
338 static void end_list(Visitor
*v
, void **obj
)
340 StringOutputVisitor
*sov
= to_sov(v
);
342 assert(sov
->list
== obj
);
343 assert(sov
->list_mode
== LM_STARTED
||
344 sov
->list_mode
== LM_END
||
345 sov
->list_mode
== LM_NONE
||
346 sov
->list_mode
== LM_IN_PROGRESS
);
347 sov
->list_mode
= LM_NONE
;
350 static void string_output_complete(Visitor
*v
, void *opaque
)
352 StringOutputVisitor
*sov
= to_sov(v
);
354 assert(opaque
== sov
->result
);
355 *sov
->result
= g_string_free(sov
->string
, false);
359 static void free_range(void *range
, void *dummy
)
364 static void string_output_free(Visitor
*v
)
366 StringOutputVisitor
*sov
= to_sov(v
);
369 g_string_free(sov
->string
, true);
372 g_list_foreach(sov
->ranges
, free_range
, NULL
);
373 g_list_free(sov
->ranges
);
377 Visitor
*string_output_visitor_new(bool human
, char **result
)
379 StringOutputVisitor
*v
;
381 v
= g_malloc0(sizeof(*v
));
383 v
->string
= g_string_new(NULL
);
388 v
->visitor
.type
= VISITOR_OUTPUT
;
389 v
->visitor
.type_int64
= print_type_int64
;
390 v
->visitor
.type_uint64
= print_type_uint64
;
391 v
->visitor
.type_size
= print_type_size
;
392 v
->visitor
.type_bool
= print_type_bool
;
393 v
->visitor
.type_str
= print_type_str
;
394 v
->visitor
.type_number
= print_type_number
;
395 v
->visitor
.type_null
= print_type_null
;
396 v
->visitor
.start_struct
= start_struct
;
397 v
->visitor
.end_struct
= end_struct
;
398 v
->visitor
.start_list
= start_list
;
399 v
->visitor
.next_list
= next_list
;
400 v
->visitor
.end_list
= end_list
;
401 v
->visitor
.complete
= string_output_complete
;
402 v
->visitor
.free
= string_output_free
;