Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * String printing Visitor |
| 3 | * |
Eric Blake | 08f9541 | 2016-01-29 06:48:59 -0700 | [diff] [blame] | 4 | * Copyright Red Hat, Inc. 2012-2016 |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 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 | |
Peter Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | 31e4041 | 2019-09-03 14:05:55 +0200 | [diff] [blame] | 14 | #include "qemu/cutils.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 15 | #include "qapi/string-output-visitor.h" |
| 16 | #include "qapi/visitor-impl.h" |
Paolo Bonzini | e41b509 | 2014-02-08 11:01:57 +0100 | [diff] [blame] | 17 | #include <math.h> |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 18 | #include "qemu/range.h" |
| 19 | |
| 20 | enum ListMode { |
| 21 | LM_NONE, /* not traversing a list of repeated options */ |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 22 | LM_STARTED, /* next_list() ready to be called */ |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 23 | |
| 24 | LM_IN_PROGRESS, /* next_list() has been called. |
| 25 | * |
| 26 | * Generating the next list link will consume the most |
| 27 | * recently parsed QemuOpt instance of the repeated |
| 28 | * option. |
| 29 | * |
| 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. |
| 34 | */ |
| 35 | |
| 36 | LM_SIGNED_INTERVAL, /* next_list() has been called. |
| 37 | * |
| 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. |
| 43 | * |
| 44 | * Parsing a value into the list link will store the |
| 45 | * next element of the signed interval. |
| 46 | */ |
| 47 | |
| 48 | LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */ |
| 49 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 50 | LM_END, /* next_list() called, about to see last element. */ |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | typedef enum ListMode ListMode; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 54 | |
| 55 | struct StringOutputVisitor |
| 56 | { |
| 57 | Visitor visitor; |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 58 | bool human; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 59 | GString *string; |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 60 | char **result; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 61 | ListMode list_mode; |
| 62 | union { |
| 63 | int64_t s; |
| 64 | uint64_t u; |
| 65 | } range_start, range_end; |
| 66 | GList *ranges; |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 67 | void *list; /* Only needed for sanity checking the caller */ |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 68 | unsigned int struct_nesting; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 71 | static StringOutputVisitor *to_sov(Visitor *v) |
| 72 | { |
| 73 | return container_of(v, StringOutputVisitor, visitor); |
| 74 | } |
| 75 | |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 76 | static void string_output_set(StringOutputVisitor *sov, char *string) |
| 77 | { |
Kevin Wolf | ea7ec15 | 2023-11-21 18:34:16 +0100 | [diff] [blame] | 78 | switch (sov->list_mode) { |
| 79 | case LM_STARTED: |
| 80 | sov->list_mode = LM_IN_PROGRESS; |
| 81 | /* fall through */ |
| 82 | case LM_NONE: |
| 83 | if (sov->string) { |
| 84 | g_string_free(sov->string, true); |
| 85 | } |
| 86 | sov->string = g_string_new(string); |
| 87 | g_free(string); |
| 88 | break; |
| 89 | |
| 90 | case LM_IN_PROGRESS: |
| 91 | case LM_END: |
| 92 | g_string_append(sov->string, ", "); |
| 93 | g_string_append(sov->string, string); |
| 94 | break; |
| 95 | |
| 96 | default: |
| 97 | abort(); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 98 | } |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void string_output_append(StringOutputVisitor *sov, int64_t a) |
| 102 | { |
| 103 | Range *r = g_malloc0(sizeof(*r)); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 104 | |
| 105 | range_set_bounds(r, a, a); |
Eric Blake | 7c47959 | 2016-05-31 10:41:29 -0600 | [diff] [blame] | 106 | sov->ranges = range_list_insert(sov->ranges, r); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void string_output_append_range(StringOutputVisitor *sov, |
| 110 | int64_t s, int64_t e) |
| 111 | { |
| 112 | Range *r = g_malloc0(sizeof(*r)); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 113 | |
| 114 | range_set_bounds(r, s, e); |
Eric Blake | 7c47959 | 2016-05-31 10:41:29 -0600 | [diff] [blame] | 115 | sov->ranges = range_list_insert(sov->ranges, r); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void format_string(StringOutputVisitor *sov, Range *r, bool next, |
| 119 | bool human) |
| 120 | { |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 121 | if (range_lob(r) != range_upb(r)) { |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 122 | if (human) { |
Hu Tao | 684531a | 2014-06-20 13:55:42 +0800 | [diff] [blame] | 123 | g_string_append_printf(sov->string, "0x%" PRIx64 "-0x%" PRIx64, |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 124 | range_lob(r), range_upb(r)); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 125 | |
| 126 | } else { |
| 127 | g_string_append_printf(sov->string, "%" PRId64 "-%" PRId64, |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 128 | range_lob(r), range_upb(r)); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 129 | } |
| 130 | } else { |
| 131 | if (human) { |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 132 | g_string_append_printf(sov->string, "0x%" PRIx64, range_lob(r)); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 133 | } else { |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 134 | g_string_append_printf(sov->string, "%" PRId64, range_lob(r)); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | if (next) { |
| 138 | g_string_append(sov->string, ","); |
| 139 | } |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 142 | static bool print_type_int64(Visitor *v, const char *name, int64_t *obj, |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 143 | Error **errp) |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 144 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 145 | StringOutputVisitor *sov = to_sov(v); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 146 | GList *l; |
| 147 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 148 | if (sov->struct_nesting) { |
| 149 | return true; |
| 150 | } |
| 151 | |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 152 | switch (sov->list_mode) { |
| 153 | case LM_NONE: |
| 154 | string_output_append(sov, *obj); |
| 155 | break; |
| 156 | |
| 157 | case LM_STARTED: |
| 158 | sov->range_start.s = *obj; |
| 159 | sov->range_end.s = *obj; |
| 160 | sov->list_mode = LM_IN_PROGRESS; |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 161 | return true; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 162 | |
| 163 | case LM_IN_PROGRESS: |
| 164 | if (sov->range_end.s + 1 == *obj) { |
| 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 | string_output_append_range(sov, sov->range_start.s, |
| 172 | sov->range_end.s); |
| 173 | } |
| 174 | |
| 175 | sov->range_start.s = *obj; |
| 176 | sov->range_end.s = *obj; |
| 177 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 178 | return true; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 179 | |
| 180 | case LM_END: |
| 181 | if (sov->range_end.s + 1 == *obj) { |
| 182 | sov->range_end.s++; |
| 183 | assert(sov->range_start.s < sov->range_end.s); |
| 184 | string_output_append_range(sov, sov->range_start.s, |
| 185 | sov->range_end.s); |
| 186 | } else { |
| 187 | if (sov->range_start.s == sov->range_end.s) { |
| 188 | string_output_append(sov, sov->range_end.s); |
| 189 | } else { |
| 190 | assert(sov->range_start.s < sov->range_end.s); |
| 191 | |
| 192 | string_output_append_range(sov, sov->range_start.s, |
| 193 | sov->range_end.s); |
| 194 | } |
| 195 | string_output_append(sov, *obj); |
| 196 | } |
| 197 | break; |
| 198 | |
| 199 | default: |
| 200 | abort(); |
| 201 | } |
| 202 | |
| 203 | l = sov->ranges; |
| 204 | while (l) { |
| 205 | Range *r = l->data; |
| 206 | format_string(sov, r, l->next != NULL, false); |
| 207 | l = l->next; |
| 208 | } |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 209 | |
| 210 | if (sov->human) { |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 211 | l = sov->ranges; |
| 212 | g_string_append(sov->string, " ("); |
| 213 | while (l) { |
| 214 | Range *r = l->data; |
Michael S. Tsirkin | 56fdfb6 | 2014-06-18 17:59:51 +0300 | [diff] [blame] | 215 | format_string(sov, r, l->next != NULL, true); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 216 | l = l->next; |
| 217 | } |
| 218 | g_string_append(sov->string, ")"); |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 219 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 220 | |
| 221 | return true; |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 224 | static bool print_type_uint64(Visitor *v, const char *name, uint64_t *obj, |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 225 | Error **errp) |
| 226 | { |
| 227 | /* FIXME: print_type_int64 mishandles values over INT64_MAX */ |
| 228 | int64_t i = *obj; |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 229 | return print_type_int64(v, name, &i, errp); |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 232 | static bool print_type_size(Visitor *v, const char *name, uint64_t *obj, |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 233 | Error **errp) |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 234 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 235 | StringOutputVisitor *sov = to_sov(v); |
Peter Xu | 22951aa | 2017-05-12 12:17:40 +0800 | [diff] [blame] | 236 | uint64_t val; |
| 237 | char *out, *psize; |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 238 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 239 | if (sov->struct_nesting) { |
| 240 | return true; |
| 241 | } |
| 242 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 243 | if (!sov->human) { |
Paolo Bonzini | e41b509 | 2014-02-08 11:01:57 +0100 | [diff] [blame] | 244 | out = g_strdup_printf("%"PRIu64, *obj); |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 245 | string_output_set(sov, out); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 246 | return true; |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | val = *obj; |
Peter Xu | 22951aa | 2017-05-12 12:17:40 +0800 | [diff] [blame] | 250 | psize = size_to_str(val); |
| 251 | out = g_strdup_printf("%"PRIu64" (%s)", val, psize); |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 252 | string_output_set(sov, out); |
Peter Xu | 22951aa | 2017-05-12 12:17:40 +0800 | [diff] [blame] | 253 | |
| 254 | g_free(psize); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 255 | return true; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 258 | static bool print_type_bool(Visitor *v, const char *name, bool *obj, |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 259 | Error **errp) |
| 260 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 261 | StringOutputVisitor *sov = to_sov(v); |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 262 | |
| 263 | if (sov->struct_nesting) { |
| 264 | return true; |
| 265 | } |
| 266 | |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 267 | string_output_set(sov, g_strdup(*obj ? "true" : "false")); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 268 | return true; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 271 | static bool print_type_str(Visitor *v, const char *name, char **obj, |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 272 | Error **errp) |
| 273 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 274 | StringOutputVisitor *sov = to_sov(v); |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 275 | char *out; |
| 276 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 277 | if (sov->struct_nesting) { |
| 278 | return true; |
| 279 | } |
| 280 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 281 | if (sov->human) { |
| 282 | out = *obj ? g_strdup_printf("\"%s\"", *obj) : g_strdup("<null>"); |
| 283 | } else { |
| 284 | out = g_strdup(*obj ? *obj : ""); |
| 285 | } |
| 286 | string_output_set(sov, out); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 287 | return true; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 290 | static bool print_type_number(Visitor *v, const char *name, double *obj, |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 291 | Error **errp) |
| 292 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 293 | StringOutputVisitor *sov = to_sov(v); |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 294 | |
| 295 | if (sov->struct_nesting) { |
| 296 | return true; |
| 297 | } |
| 298 | |
Markus Armbruster | 54addb0 | 2020-12-10 17:14:50 +0100 | [diff] [blame] | 299 | string_output_set(sov, g_strdup_printf("%.17g", *obj)); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 300 | return true; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 303 | static bool print_type_null(Visitor *v, const char *name, QNull **obj, |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 304 | Error **errp) |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 305 | { |
| 306 | StringOutputVisitor *sov = to_sov(v); |
| 307 | char *out; |
| 308 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 309 | if (sov->struct_nesting) { |
| 310 | return true; |
| 311 | } |
| 312 | |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 313 | if (sov->human) { |
| 314 | out = g_strdup("<null>"); |
| 315 | } else { |
| 316 | out = g_strdup(""); |
| 317 | } |
| 318 | string_output_set(sov, out); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 319 | return true; |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 320 | } |
| 321 | |
Stefan Hajnoczi | ff32bb5 | 2023-12-12 08:49:34 -0500 | [diff] [blame] | 322 | static bool start_struct(Visitor *v, const char *name, void **obj, |
| 323 | size_t size, Error **errp) |
| 324 | { |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 325 | StringOutputVisitor *sov = to_sov(v); |
| 326 | |
| 327 | sov->struct_nesting++; |
Stefan Hajnoczi | ff32bb5 | 2023-12-12 08:49:34 -0500 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | |
| 331 | static void end_struct(Visitor *v, void **obj) |
| 332 | { |
| 333 | StringOutputVisitor *sov = to_sov(v); |
| 334 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 335 | if (--sov->struct_nesting) { |
| 336 | return; |
| 337 | } |
| 338 | |
Stefan Hajnoczi | ff32bb5 | 2023-12-12 08:49:34 -0500 | [diff] [blame] | 339 | /* TODO actually print struct fields */ |
| 340 | string_output_set(sov, g_strdup("<omitted>")); |
| 341 | } |
| 342 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 343 | static bool |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 344 | start_list(Visitor *v, const char *name, GenericList **list, size_t size, |
| 345 | Error **errp) |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 346 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 347 | StringOutputVisitor *sov = to_sov(v); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 348 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 349 | if (sov->struct_nesting) { |
| 350 | return true; |
| 351 | } |
| 352 | |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 353 | /* we can't traverse a list in a list */ |
| 354 | assert(sov->list_mode == LM_NONE); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 355 | /* We don't support visits without a list */ |
| 356 | assert(list); |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 357 | sov->list = list; |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 358 | /* List handling is only needed if there are at least two elements */ |
| 359 | if (*list && (*list)->next) { |
| 360 | sov->list_mode = LM_STARTED; |
| 361 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 362 | return true; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 365 | static GenericList *next_list(Visitor *v, GenericList *tail, size_t size) |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 366 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 367 | StringOutputVisitor *sov = to_sov(v); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 368 | GenericList *ret = tail->next; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 369 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 370 | if (sov->struct_nesting) { |
| 371 | return ret; |
| 372 | } |
| 373 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 374 | if (ret && !ret->next) { |
| 375 | sov->list_mode = LM_END; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 376 | } |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 377 | return ret; |
| 378 | } |
| 379 | |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 380 | static void end_list(Visitor *v, void **obj) |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 381 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 382 | StringOutputVisitor *sov = to_sov(v); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 383 | |
Kevin Wolf | 014b99a | 2024-01-09 19:17:17 +0100 | [diff] [blame] | 384 | if (sov->struct_nesting) { |
| 385 | return; |
| 386 | } |
| 387 | |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 388 | assert(sov->list == obj); |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 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; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 396 | static void string_output_complete(Visitor *v, void *opaque) |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 397 | { |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 398 | StringOutputVisitor *sov = to_sov(v); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 399 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 400 | assert(opaque == sov->result); |
| 401 | *sov->result = g_string_free(sov->string, false); |
| 402 | sov->string = NULL; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Michael S. Tsirkin | 0d15668 | 2014-06-16 18:07:03 +0300 | [diff] [blame] | 405 | static void free_range(void *range, void *dummy) |
| 406 | { |
| 407 | g_free(range); |
| 408 | } |
| 409 | |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 410 | static void string_output_free(Visitor *v) |
| 411 | { |
| 412 | StringOutputVisitor *sov = to_sov(v); |
| 413 | |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 414 | if (sov->string) { |
| 415 | g_string_free(sov->string, true); |
| 416 | } |
| 417 | |
Michael S. Tsirkin | 0d15668 | 2014-06-16 18:07:03 +0300 | [diff] [blame] | 418 | g_list_foreach(sov->ranges, free_range, NULL); |
| 419 | g_list_free(sov->ranges); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 420 | g_free(sov); |
| 421 | } |
| 422 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 423 | Visitor *string_output_visitor_new(bool human, char **result) |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 424 | { |
| 425 | StringOutputVisitor *v; |
| 426 | |
| 427 | v = g_malloc0(sizeof(*v)); |
| 428 | |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 429 | v->string = g_string_new(NULL); |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 430 | v->human = human; |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 431 | v->result = result; |
| 432 | *result = NULL; |
| 433 | |
Eric Blake | 983f52d | 2016-04-28 15:45:09 -0600 | [diff] [blame] | 434 | v->visitor.type = VISITOR_OUTPUT; |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 435 | v->visitor.type_int64 = print_type_int64; |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 436 | v->visitor.type_uint64 = print_type_uint64; |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 437 | v->visitor.type_size = print_type_size; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 438 | v->visitor.type_bool = print_type_bool; |
| 439 | v->visitor.type_str = print_type_str; |
| 440 | v->visitor.type_number = print_type_number; |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 441 | v->visitor.type_null = print_type_null; |
Stefan Hajnoczi | ff32bb5 | 2023-12-12 08:49:34 -0500 | [diff] [blame] | 442 | v->visitor.start_struct = start_struct; |
| 443 | v->visitor.end_struct = end_struct; |
Hu Tao | 69e2556 | 2014-06-10 19:15:28 +0800 | [diff] [blame] | 444 | v->visitor.start_list = start_list; |
| 445 | v->visitor.next_list = next_list; |
| 446 | v->visitor.end_list = end_list; |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 447 | v->visitor.complete = string_output_complete; |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 448 | v->visitor.free = string_output_free; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 449 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 450 | return &v->visitor; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 451 | } |