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-common.h"
15 #include "qapi/string-output-visitor.h"
16 #include "qapi/visitor-impl.h"
17 #include "qemu/host-utils.h"
19 #include "qemu/range.h"
22 LM_NONE
, /* not traversing a list of repeated options */
23 LM_STARTED
, /* next_list() ready to be called */
25 LM_IN_PROGRESS
, /* next_list() has been called.
27 * Generating the next list link will consume the most
28 * recently parsed QemuOpt instance of the repeated
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.
37 LM_SIGNED_INTERVAL
, /* next_list() has been called.
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.
45 * Parsing a value into the list link will store the
46 * next element of the signed interval.
49 LM_UNSIGNED_INTERVAL
,/* Same as above, only for an unsigned interval. */
51 LM_END
, /* next_list() called, about to see last element. */
54 typedef enum ListMode ListMode
;
56 struct StringOutputVisitor
66 } range_start
, range_end
;
68 void *list
; /* Only needed for sanity checking the caller */
71 static StringOutputVisitor
*to_sov(Visitor
*v
)
73 return container_of(v
, StringOutputVisitor
, visitor
);
76 static void string_output_set(StringOutputVisitor
*sov
, char *string
)
79 g_string_free(sov
->string
, true);
81 sov
->string
= g_string_new(string
);
85 static void string_output_append(StringOutputVisitor
*sov
, int64_t a
)
87 Range
*r
= g_malloc0(sizeof(*r
));
89 range_set_bounds(r
, a
, a
);
90 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
93 static void string_output_append_range(StringOutputVisitor
*sov
,
96 Range
*r
= g_malloc0(sizeof(*r
));
98 range_set_bounds(r
, s
, e
);
99 sov
->ranges
= range_list_insert(sov
->ranges
, r
);
102 static void format_string(StringOutputVisitor
*sov
, Range
*r
, bool next
,
105 if (range_lob(r
) != range_upb(r
)) {
107 g_string_append_printf(sov
->string
, "0x%" PRIx64
"-0x%" PRIx64
,
108 range_lob(r
), range_upb(r
));
111 g_string_append_printf(sov
->string
, "%" PRId64
"-%" PRId64
,
112 range_lob(r
), range_upb(r
));
116 g_string_append_printf(sov
->string
, "0x%" PRIx64
, range_lob(r
));
118 g_string_append_printf(sov
->string
, "%" PRId64
, range_lob(r
));
122 g_string_append(sov
->string
, ",");
126 static void print_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
129 StringOutputVisitor
*sov
= to_sov(v
);
132 switch (sov
->list_mode
) {
134 string_output_append(sov
, *obj
);
138 sov
->range_start
.s
= *obj
;
139 sov
->range_end
.s
= *obj
;
140 sov
->list_mode
= LM_IN_PROGRESS
;
144 if (sov
->range_end
.s
+ 1 == *obj
) {
147 if (sov
->range_start
.s
== sov
->range_end
.s
) {
148 string_output_append(sov
, sov
->range_end
.s
);
150 assert(sov
->range_start
.s
< sov
->range_end
.s
);
151 string_output_append_range(sov
, sov
->range_start
.s
,
155 sov
->range_start
.s
= *obj
;
156 sov
->range_end
.s
= *obj
;
161 if (sov
->range_end
.s
+ 1 == *obj
) {
163 assert(sov
->range_start
.s
< sov
->range_end
.s
);
164 string_output_append_range(sov
, sov
->range_start
.s
,
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
);
172 string_output_append_range(sov
, sov
->range_start
.s
,
175 string_output_append(sov
, *obj
);
186 format_string(sov
, r
, l
->next
!= NULL
, false);
192 g_string_append(sov
->string
, " (");
195 format_string(sov
, r
, l
->next
!= NULL
, true);
198 g_string_append(sov
->string
, ")");
202 static void print_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
205 /* FIXME: print_type_int64 mishandles values over INT64_MAX */
207 print_type_int64(v
, name
, &i
, errp
);
210 static void print_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
213 StringOutputVisitor
*sov
= to_sov(v
);
218 out
= g_strdup_printf("%"PRIu64
, *obj
);
219 string_output_set(sov
, out
);
224 psize
= size_to_str(val
);
225 out
= g_strdup_printf("%"PRIu64
" (%s)", val
, psize
);
226 string_output_set(sov
, out
);
231 static void print_type_bool(Visitor
*v
, const char *name
, bool *obj
,
234 StringOutputVisitor
*sov
= to_sov(v
);
235 string_output_set(sov
, g_strdup(*obj
? "true" : "false"));
238 static void print_type_str(Visitor
*v
, const char *name
, char **obj
,
241 StringOutputVisitor
*sov
= to_sov(v
);
245 out
= *obj
? g_strdup_printf("\"%s\"", *obj
) : g_strdup("<null>");
247 out
= g_strdup(*obj
? *obj
: "");
249 string_output_set(sov
, out
);
252 static void print_type_number(Visitor
*v
, const char *name
, double *obj
,
255 StringOutputVisitor
*sov
= to_sov(v
);
256 string_output_set(sov
, g_strdup_printf("%f", *obj
));
259 static void print_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
262 StringOutputVisitor
*sov
= to_sov(v
);
266 out
= g_strdup("<null>");
270 string_output_set(sov
, out
);
274 start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
277 StringOutputVisitor
*sov
= to_sov(v
);
279 /* we can't traverse a list in a list */
280 assert(sov
->list_mode
== LM_NONE
);
281 /* We don't support visits without a list */
284 /* List handling is only needed if there are at least two elements */
285 if (*list
&& (*list
)->next
) {
286 sov
->list_mode
= LM_STARTED
;
290 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
292 StringOutputVisitor
*sov
= to_sov(v
);
293 GenericList
*ret
= tail
->next
;
295 if (ret
&& !ret
->next
) {
296 sov
->list_mode
= LM_END
;
301 static void end_list(Visitor
*v
, void **obj
)
303 StringOutputVisitor
*sov
= to_sov(v
);
305 assert(sov
->list
== obj
);
306 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
;
313 static void string_output_complete(Visitor
*v
, void *opaque
)
315 StringOutputVisitor
*sov
= to_sov(v
);
317 assert(opaque
== sov
->result
);
318 *sov
->result
= g_string_free(sov
->string
, false);
322 static void free_range(void *range
, void *dummy
)
327 static void string_output_free(Visitor
*v
)
329 StringOutputVisitor
*sov
= to_sov(v
);
332 g_string_free(sov
->string
, true);
335 g_list_foreach(sov
->ranges
, free_range
, NULL
);
336 g_list_free(sov
->ranges
);
340 Visitor
*string_output_visitor_new(bool human
, char **result
)
342 StringOutputVisitor
*v
;
344 v
= g_malloc0(sizeof(*v
));
346 v
->string
= g_string_new(NULL
);
351 v
->visitor
.type
= VISITOR_OUTPUT
;
352 v
->visitor
.type_int64
= print_type_int64
;
353 v
->visitor
.type_uint64
= print_type_uint64
;
354 v
->visitor
.type_size
= print_type_size
;
355 v
->visitor
.type_bool
= print_type_bool
;
356 v
->visitor
.type_str
= print_type_str
;
357 v
->visitor
.type_number
= print_type_number
;
358 v
->visitor
.type_null
= print_type_null
;
359 v
->visitor
.start_list
= start_list
;
360 v
->visitor
.next_list
= next_list
;
361 v
->visitor
.end_list
= end_list
;
362 v
->visitor
.complete
= string_output_complete
;
363 v
->visitor
.free
= string_output_free
;