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 */
68 unsigned int struct_nesting
;
71 static StringOutputVisitor
*to_sov(Visitor
*v
)
73 return container_of(v
, StringOutputVisitor
, visitor
);
76 static void string_output_set(StringOutputVisitor
*sov
, char *string
)
78 switch (sov
->list_mode
) {
80 sov
->list_mode
= LM_IN_PROGRESS
;
84 g_string_free(sov
->string
, true);
86 sov
->string
= g_string_new(string
);
92 g_string_append(sov
->string
, ", ");
93 g_string_append(sov
->string
, string
);
101 static void string_output_append(StringOutputVisitor
*sov
, int64_t a
)
103 Range
*r
= g_malloc0(sizeof(*r
));
105 range_set_bounds(r
, a
, a
);
106 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
109 static void string_output_append_range(StringOutputVisitor
*sov
,
110 int64_t s
, int64_t e
)
112 Range
*r
= g_malloc0(sizeof(*r
));
114 range_set_bounds(r
, s
, e
);
115 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
118 static void format_string(StringOutputVisitor
*sov
, Range
*r
, bool next
,
121 if (range_lob(r
) != range_upb(r
)) {
123 g_string_append_printf(sov
->string
, "0x%" PRIx64
"-0x%" PRIx64
,
124 range_lob(r
), range_upb(r
));
127 g_string_append_printf(sov
->string
, "%" PRId64
"-%" PRId64
,
128 range_lob(r
), range_upb(r
));
132 g_string_append_printf(sov
->string
, "0x%" PRIx64
, range_lob(r
));
134 g_string_append_printf(sov
->string
, "%" PRId64
, range_lob(r
));
138 g_string_append(sov
->string
, ",");
142 static bool print_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
145 StringOutputVisitor
*sov
= to_sov(v
);
148 if (sov
->struct_nesting
) {
152 switch (sov
->list_mode
) {
154 string_output_append(sov
, *obj
);
158 sov
->range_start
.s
= *obj
;
159 sov
->range_end
.s
= *obj
;
160 sov
->list_mode
= LM_IN_PROGRESS
;
164 if (sov
->range_end
.s
+ 1 == *obj
) {
167 if (sov
->range_start
.s
== sov
->range_end
.s
) {
168 string_output_append(sov
, sov
->range_end
.s
);
170 assert(sov
->range_start
.s
< sov
->range_end
.s
);
171 string_output_append_range(sov
, sov
->range_start
.s
,
175 sov
->range_start
.s
= *obj
;
176 sov
->range_end
.s
= *obj
;
181 if (sov
->range_end
.s
+ 1 == *obj
) {
183 assert(sov
->range_start
.s
< sov
->range_end
.s
);
184 string_output_append_range(sov
, sov
->range_start
.s
,
187 if (sov
->range_start
.s
== sov
->range_end
.s
) {
188 string_output_append(sov
, sov
->range_end
.s
);
190 assert(sov
->range_start
.s
< sov
->range_end
.s
);
192 string_output_append_range(sov
, sov
->range_start
.s
,
195 string_output_append(sov
, *obj
);
206 format_string(sov
, r
, l
->next
!= NULL
, false);
212 g_string_append(sov
->string
, " (");
215 format_string(sov
, r
, l
->next
!= NULL
, true);
218 g_string_append(sov
->string
, ")");
224 static bool print_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
227 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
229 return print_type_int64(v
, name
, &i
, errp
);
232 static bool print_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
235 StringOutputVisitor
*sov
= to_sov(v
);
239 if (sov
->struct_nesting
) {
244 out
= g_strdup_printf("%"PRIu64
, *obj
);
245 string_output_set(sov
, out
);
250 psize
= size_to_str(val
);
251 out
= g_strdup_printf("%"PRIu64
" (%s)", val
, psize
);
252 string_output_set(sov
, out
);
258 static bool print_type_bool(Visitor
*v
, const char *name
, bool *obj
,
261 StringOutputVisitor
*sov
= to_sov(v
);
263 if (sov
->struct_nesting
) {
267 string_output_set(sov
, g_strdup(*obj
? "true" : "false"));
271 static bool print_type_str(Visitor
*v
, const char *name
, char **obj
,
274 StringOutputVisitor
*sov
= to_sov(v
);
277 if (sov
->struct_nesting
) {
282 out
= *obj
? g_strdup_printf("\"%s\"", *obj
) : g_strdup("<null>");
284 out
= g_strdup(*obj
? *obj
: "");
286 string_output_set(sov
, out
);
290 static bool print_type_number(Visitor
*v
, const char *name
, double *obj
,
293 StringOutputVisitor
*sov
= to_sov(v
);
295 if (sov
->struct_nesting
) {
299 string_output_set(sov
, g_strdup_printf("%.17g", *obj
));
303 static bool print_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
306 StringOutputVisitor
*sov
= to_sov(v
);
309 if (sov
->struct_nesting
) {
314 out
= g_strdup("<null>");
318 string_output_set(sov
, out
);
322 static bool start_struct(Visitor
*v
, const char *name
, void **obj
,
323 size_t size
, Error
**errp
)
325 StringOutputVisitor
*sov
= to_sov(v
);
327 sov
->struct_nesting
++;
331 static void end_struct(Visitor
*v
, void **obj
)
333 StringOutputVisitor
*sov
= to_sov(v
);
335 if (--sov
->struct_nesting
) {
339 /* TODO actually print struct fields */
340 string_output_set(sov
, g_strdup("<omitted>"));
344 start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
347 StringOutputVisitor
*sov
= to_sov(v
);
349 if (sov
->struct_nesting
) {
353 /* we can't traverse a list in a list */
354 assert(sov
->list_mode
== LM_NONE
);
355 /* We don't support visits without a list */
358 /* List handling is only needed if there are at least two elements */
359 if (*list
&& (*list
)->next
) {
360 sov
->list_mode
= LM_STARTED
;
365 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
367 StringOutputVisitor
*sov
= to_sov(v
);
368 GenericList
*ret
= tail
->next
;
370 if (sov
->struct_nesting
) {
374 if (ret
&& !ret
->next
) {
375 sov
->list_mode
= LM_END
;
380 static void end_list(Visitor
*v
, void **obj
)
382 StringOutputVisitor
*sov
= to_sov(v
);
384 if (sov
->struct_nesting
) {
388 assert(sov
->list
== obj
);
389 assert(sov
->list_mode
== LM_STARTED
||
390 sov
->list_mode
== LM_END
||
391 sov
->list_mode
== LM_NONE
||
392 sov
->list_mode
== LM_IN_PROGRESS
);
393 sov
->list_mode
= LM_NONE
;
396 static void string_output_complete(Visitor
*v
, void *opaque
)
398 StringOutputVisitor
*sov
= to_sov(v
);
400 assert(opaque
== sov
->result
);
401 *sov
->result
= g_string_free(sov
->string
, false);
405 static void free_range(void *range
, void *dummy
)
410 static void string_output_free(Visitor
*v
)
412 StringOutputVisitor
*sov
= to_sov(v
);
415 g_string_free(sov
->string
, true);
418 g_list_foreach(sov
->ranges
, free_range
, NULL
);
419 g_list_free(sov
->ranges
);
423 Visitor
*string_output_visitor_new(bool human
, char **result
)
425 StringOutputVisitor
*v
;
427 v
= g_malloc0(sizeof(*v
));
429 v
->string
= g_string_new(NULL
);
434 v
->visitor
.type
= VISITOR_OUTPUT
;
435 v
->visitor
.type_int64
= print_type_int64
;
436 v
->visitor
.type_uint64
= print_type_uint64
;
437 v
->visitor
.type_size
= print_type_size
;
438 v
->visitor
.type_bool
= print_type_bool
;
439 v
->visitor
.type_str
= print_type_str
;
440 v
->visitor
.type_number
= print_type_number
;
441 v
->visitor
.type_null
= print_type_null
;
442 v
->visitor
.start_struct
= start_struct
;
443 v
->visitor
.end_struct
= end_struct
;
444 v
->visitor
.start_list
= start_list
;
445 v
->visitor
.next_list
= next_list
;
446 v
->visitor
.end_list
= end_list
;
447 v
->visitor
.complete
= string_output_complete
;
448 v
->visitor
.free
= string_output_free
;