]>
Commit | Line | Data |
---|---|---|
eb7ee2cb LE |
1 | /* |
2 | * Options Visitor | |
3 | * | |
08f9541d | 4 | * Copyright Red Hat, Inc. 2012-2016 |
eb7ee2cb LE |
5 | * |
6 | * Author: Laszlo Ersek <[email protected]> | |
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 | ||
cbf21151 | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
f348b6d1 | 15 | #include "qemu/cutils.h" |
7b1b5d19 PB |
16 | #include "qapi/qmp/qerror.h" |
17 | #include "qapi/opts-visitor.h" | |
1de7afc9 PB |
18 | #include "qemu/queue.h" |
19 | #include "qemu/option_int.h" | |
7b1b5d19 | 20 | #include "qapi/visitor-impl.h" |
eb7ee2cb LE |
21 | |
22 | ||
d9570434 LE |
23 | enum ListMode |
24 | { | |
25 | LM_NONE, /* not traversing a list of repeated options */ | |
d8754f40 | 26 | |
863f195f AS |
27 | LM_IN_PROGRESS, /* |
28 | * opts_next_list() ready to be called. | |
d8754f40 LE |
29 | * |
30 | * Generating the next list link will consume the most | |
31 | * recently parsed QemuOpt instance of the repeated | |
32 | * option. | |
33 | * | |
34 | * Parsing a value into the list link will examine the | |
35 | * next QemuOpt instance of the repeated option, and | |
36 | * possibly enter LM_SIGNED_INTERVAL or | |
37 | * LM_UNSIGNED_INTERVAL. | |
38 | */ | |
39 | ||
863f195f AS |
40 | LM_SIGNED_INTERVAL, /* |
41 | * opts_next_list() has been called. | |
d8754f40 LE |
42 | * |
43 | * Generating the next list link will consume the most | |
44 | * recently stored element from the signed interval, | |
45 | * parsed from the most recent QemuOpt instance of the | |
46 | * repeated option. This may consume QemuOpt itself | |
47 | * and return to LM_IN_PROGRESS. | |
48 | * | |
49 | * Parsing a value into the list link will store the | |
50 | * next element of the signed interval. | |
51 | */ | |
52 | ||
863f195f AS |
53 | LM_UNSIGNED_INTERVAL, /* Same as above, only for an unsigned interval. */ |
54 | ||
55 | LM_TRAVERSED /* | |
56 | * opts_next_list() has been called. | |
57 | * | |
58 | * No more QemuOpt instance in the list. | |
59 | * The traversal has been completed. | |
60 | */ | |
d9570434 LE |
61 | }; |
62 | ||
63 | typedef enum ListMode ListMode; | |
64 | ||
eb7ee2cb LE |
65 | struct OptsVisitor |
66 | { | |
67 | Visitor visitor; | |
68 | ||
69 | /* Ownership remains with opts_visitor_new()'s caller. */ | |
70 | const QemuOpts *opts_root; | |
71 | ||
72 | unsigned depth; | |
73 | ||
74 | /* Non-null iff depth is positive. Each key is a QemuOpt name. Each value | |
75 | * is a non-empty GQueue, enumerating all QemuOpt occurrences with that | |
76 | * name. */ | |
77 | GHashTable *unprocessed_opts; | |
78 | ||
79 | /* The list currently being traversed with opts_start_list() / | |
80 | * opts_next_list(). The list must have a struct element type in the | |
81 | * schema, with a single mandatory scalar member. */ | |
d9570434 | 82 | ListMode list_mode; |
eb7ee2cb | 83 | GQueue *repeated_opts; |
eb7ee2cb | 84 | |
d8754f40 LE |
85 | /* When parsing a list of repeating options as integers, values of the form |
86 | * "a-b", representing a closed interval, are allowed. Elements in the | |
87 | * range are generated individually. | |
88 | */ | |
89 | union { | |
90 | int64_t s; | |
91 | uint64_t u; | |
92 | } range_next, range_limit; | |
93 | ||
eb7ee2cb LE |
94 | /* If "opts_root->id" is set, reinstantiate it as a fake QemuOpt for |
95 | * uniformity. Only its "name" and "str" fields are set. "fake_id_opt" does | |
96 | * not survive or escape the OptsVisitor object. | |
97 | */ | |
98 | QemuOpt *fake_id_opt; | |
99 | }; | |
100 | ||
101 | ||
d7bea75d EB |
102 | static OptsVisitor *to_ov(Visitor *v) |
103 | { | |
104 | return container_of(v, OptsVisitor, visitor); | |
105 | } | |
106 | ||
107 | ||
eb7ee2cb LE |
108 | static void |
109 | destroy_list(gpointer list) | |
110 | { | |
111 | g_queue_free(list); | |
112 | } | |
113 | ||
114 | ||
115 | static void | |
116 | opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt) | |
117 | { | |
118 | GQueue *list; | |
119 | ||
120 | list = g_hash_table_lookup(unprocessed_opts, opt->name); | |
121 | if (list == NULL) { | |
122 | list = g_queue_new(); | |
123 | ||
124 | /* GHashTable will never try to free the keys -- we supply NULL as | |
125 | * "key_destroy_func" in opts_start_struct(). Thus cast away key | |
126 | * const-ness in order to suppress gcc's warning. | |
127 | */ | |
128 | g_hash_table_insert(unprocessed_opts, (gpointer)opt->name, list); | |
129 | } | |
130 | ||
131 | /* Similarly, destroy_list() doesn't call g_queue_free_full(). */ | |
132 | g_queue_push_tail(list, (gpointer)opt); | |
133 | } | |
134 | ||
135 | ||
012d4c96 | 136 | static bool |
337283df | 137 | opts_start_struct(Visitor *v, const char *name, void **obj, |
0b2a0d6b | 138 | size_t size, Error **errp) |
eb7ee2cb | 139 | { |
d7bea75d | 140 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
141 | const QemuOpt *opt; |
142 | ||
b7745397 | 143 | if (obj) { |
e58d695e | 144 | *obj = g_malloc0(size); |
b7745397 | 145 | } |
eb7ee2cb | 146 | if (ov->depth++ > 0) { |
012d4c96 | 147 | return true; |
eb7ee2cb LE |
148 | } |
149 | ||
150 | ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal, | |
151 | NULL, &destroy_list); | |
152 | QTAILQ_FOREACH(opt, &ov->opts_root->head, next) { | |
153 | /* ensured by qemu-option.c::opts_do_parse() */ | |
154 | assert(strcmp(opt->name, "id") != 0); | |
155 | ||
156 | opts_visitor_insert(ov->unprocessed_opts, opt); | |
157 | } | |
158 | ||
159 | if (ov->opts_root->id != NULL) { | |
160 | ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt); | |
161 | ||
dc8622f2 CL |
162 | ov->fake_id_opt->name = g_strdup("id"); |
163 | ov->fake_id_opt->str = g_strdup(ov->opts_root->id); | |
eb7ee2cb LE |
164 | opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt); |
165 | } | |
012d4c96 | 166 | return true; |
eb7ee2cb LE |
167 | } |
168 | ||
169 | ||
012d4c96 | 170 | static bool |
15c2f669 | 171 | opts_check_struct(Visitor *v, Error **errp) |
eb7ee2cb | 172 | { |
d7bea75d | 173 | OptsVisitor *ov = to_ov(v); |
f96493b1 | 174 | GHashTableIter iter; |
eb7ee2cb LE |
175 | GQueue *any; |
176 | ||
21f88d02 | 177 | if (ov->depth > 1) { |
012d4c96 | 178 | return true; |
eb7ee2cb LE |
179 | } |
180 | ||
181 | /* we should have processed all (distinct) QemuOpt instances */ | |
f96493b1 EB |
182 | g_hash_table_iter_init(&iter, ov->unprocessed_opts); |
183 | if (g_hash_table_iter_next(&iter, NULL, (void **)&any)) { | |
eb7ee2cb LE |
184 | const QemuOpt *first; |
185 | ||
186 | first = g_queue_peek_head(any); | |
c6bd8c70 | 187 | error_setg(errp, QERR_INVALID_PARAMETER, first->name); |
012d4c96 | 188 | return false; |
eb7ee2cb | 189 | } |
012d4c96 | 190 | return true; |
15c2f669 EB |
191 | } |
192 | ||
193 | ||
194 | static void | |
1158bb2a | 195 | opts_end_struct(Visitor *v, void **obj) |
15c2f669 EB |
196 | { |
197 | OptsVisitor *ov = to_ov(v); | |
198 | ||
199 | if (--ov->depth > 0) { | |
200 | return; | |
201 | } | |
202 | ||
eb7ee2cb LE |
203 | g_hash_table_destroy(ov->unprocessed_opts); |
204 | ov->unprocessed_opts = NULL; | |
dc8622f2 CL |
205 | if (ov->fake_id_opt) { |
206 | g_free(ov->fake_id_opt->name); | |
207 | g_free(ov->fake_id_opt->str); | |
208 | g_free(ov->fake_id_opt); | |
209 | } | |
eb7ee2cb LE |
210 | ov->fake_id_opt = NULL; |
211 | } | |
212 | ||
213 | ||
214 | static GQueue * | |
215 | lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp) | |
216 | { | |
217 | GQueue *list; | |
218 | ||
219 | list = g_hash_table_lookup(ov->unprocessed_opts, name); | |
220 | if (!list) { | |
c6bd8c70 | 221 | error_setg(errp, QERR_MISSING_PARAMETER, name); |
eb7ee2cb LE |
222 | } |
223 | return list; | |
224 | } | |
225 | ||
226 | ||
012d4c96 | 227 | static bool |
d9f62dde EB |
228 | opts_start_list(Visitor *v, const char *name, GenericList **list, size_t size, |
229 | Error **errp) | |
eb7ee2cb | 230 | { |
d7bea75d | 231 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
232 | |
233 | /* we can't traverse a list in a list */ | |
d9570434 | 234 | assert(ov->list_mode == LM_NONE); |
d9f62dde EB |
235 | /* we don't support visits without a list */ |
236 | assert(list); | |
eb7ee2cb | 237 | ov->repeated_opts = lookup_distinct(ov, name, errp); |
012d4c96 | 238 | if (!ov->repeated_opts) { |
d9f62dde | 239 | *list = NULL; |
012d4c96 | 240 | return false; |
d9570434 | 241 | } |
012d4c96 MA |
242 | ov->list_mode = LM_IN_PROGRESS; |
243 | *list = g_malloc0(size); | |
244 | return true; | |
eb7ee2cb LE |
245 | } |
246 | ||
247 | ||
248 | static GenericList * | |
d9f62dde | 249 | opts_next_list(Visitor *v, GenericList *tail, size_t size) |
eb7ee2cb | 250 | { |
d7bea75d | 251 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb | 252 | |
d9570434 | 253 | switch (ov->list_mode) { |
863f195f AS |
254 | case LM_TRAVERSED: |
255 | return NULL; | |
d8754f40 LE |
256 | case LM_SIGNED_INTERVAL: |
257 | case LM_UNSIGNED_INTERVAL: | |
d8754f40 LE |
258 | if (ov->list_mode == LM_SIGNED_INTERVAL) { |
259 | if (ov->range_next.s < ov->range_limit.s) { | |
260 | ++ov->range_next.s; | |
261 | break; | |
262 | } | |
263 | } else if (ov->range_next.u < ov->range_limit.u) { | |
264 | ++ov->range_next.u; | |
265 | break; | |
266 | } | |
267 | ov->list_mode = LM_IN_PROGRESS; | |
268 | /* range has been completed, fall through in order to pop option */ | |
269 | ||
d9570434 | 270 | case LM_IN_PROGRESS: { |
eb7ee2cb LE |
271 | const QemuOpt *opt; |
272 | ||
273 | opt = g_queue_pop_head(ov->repeated_opts); | |
274 | if (g_queue_is_empty(ov->repeated_opts)) { | |
275 | g_hash_table_remove(ov->unprocessed_opts, opt->name); | |
863f195f AS |
276 | ov->repeated_opts = NULL; |
277 | ov->list_mode = LM_TRAVERSED; | |
eb7ee2cb LE |
278 | return NULL; |
279 | } | |
d9570434 LE |
280 | break; |
281 | } | |
282 | ||
283 | default: | |
284 | abort(); | |
eb7ee2cb LE |
285 | } |
286 | ||
d9f62dde EB |
287 | tail->next = g_malloc0(size); |
288 | return tail->next; | |
eb7ee2cb LE |
289 | } |
290 | ||
291 | ||
012d4c96 | 292 | static bool |
a4a1c70d MA |
293 | opts_check_list(Visitor *v, Error **errp) |
294 | { | |
295 | /* | |
21f88d02 EB |
296 | * Unvisited list elements will be reported later when checking |
297 | * whether unvisited struct members remain. | |
a4a1c70d | 298 | */ |
012d4c96 | 299 | return true; |
a4a1c70d MA |
300 | } |
301 | ||
302 | ||
eb7ee2cb | 303 | static void |
1158bb2a | 304 | opts_end_list(Visitor *v, void **obj) |
eb7ee2cb | 305 | { |
d7bea75d | 306 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb | 307 | |
d9f62dde | 308 | assert(ov->list_mode == LM_IN_PROGRESS || |
d8754f40 | 309 | ov->list_mode == LM_SIGNED_INTERVAL || |
863f195f AS |
310 | ov->list_mode == LM_UNSIGNED_INTERVAL || |
311 | ov->list_mode == LM_TRAVERSED); | |
eb7ee2cb | 312 | ov->repeated_opts = NULL; |
d9570434 | 313 | ov->list_mode = LM_NONE; |
eb7ee2cb LE |
314 | } |
315 | ||
316 | ||
317 | static const QemuOpt * | |
318 | lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp) | |
319 | { | |
d9570434 | 320 | if (ov->list_mode == LM_NONE) { |
eb7ee2cb LE |
321 | GQueue *list; |
322 | ||
323 | /* the last occurrence of any QemuOpt takes effect when queried by name | |
324 | */ | |
325 | list = lookup_distinct(ov, name, errp); | |
326 | return list ? g_queue_peek_tail(list) : NULL; | |
327 | } | |
863f195f AS |
328 | if (ov->list_mode == LM_TRAVERSED) { |
329 | error_setg(errp, "Fewer list elements than expected"); | |
330 | return NULL; | |
331 | } | |
d9570434 | 332 | assert(ov->list_mode == LM_IN_PROGRESS); |
eb7ee2cb LE |
333 | return g_queue_peek_head(ov->repeated_opts); |
334 | } | |
335 | ||
336 | ||
337 | static void | |
338 | processed(OptsVisitor *ov, const char *name) | |
339 | { | |
d9570434 | 340 | if (ov->list_mode == LM_NONE) { |
eb7ee2cb | 341 | g_hash_table_remove(ov->unprocessed_opts, name); |
d9570434 | 342 | return; |
eb7ee2cb | 343 | } |
d9570434 LE |
344 | assert(ov->list_mode == LM_IN_PROGRESS); |
345 | /* do nothing */ | |
eb7ee2cb LE |
346 | } |
347 | ||
348 | ||
012d4c96 | 349 | static bool |
0b2a0d6b | 350 | opts_type_str(Visitor *v, const char *name, char **obj, Error **errp) |
eb7ee2cb | 351 | { |
d7bea75d | 352 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
353 | const QemuOpt *opt; |
354 | ||
355 | opt = lookup_scalar(ov, name, errp); | |
356 | if (!opt) { | |
e58d695e | 357 | *obj = NULL; |
012d4c96 | 358 | return false; |
eb7ee2cb LE |
359 | } |
360 | *obj = g_strdup(opt->str ? opt->str : ""); | |
983f52d4 EB |
361 | /* Note that we consume a string even if this is called as part of |
362 | * an enum visit that later fails because the string is not a | |
363 | * valid enum value; this is harmless because tracking what gets | |
364 | * consumed only matters to visit_end_struct() as the final error | |
365 | * check if there were no other failures during the visit. */ | |
eb7ee2cb | 366 | processed(ov, name); |
012d4c96 | 367 | return true; |
eb7ee2cb LE |
368 | } |
369 | ||
370 | ||
012d4c96 | 371 | static bool |
0b2a0d6b | 372 | opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) |
eb7ee2cb | 373 | { |
d7bea75d | 374 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
375 | const QemuOpt *opt; |
376 | ||
377 | opt = lookup_scalar(ov, name, errp); | |
378 | if (!opt) { | |
012d4c96 | 379 | return false; |
eb7ee2cb | 380 | } |
eb7ee2cb | 381 | if (opt->str) { |
372bcb25 | 382 | if (!qapi_bool_parse(opt->name, opt->str, obj, errp)) { |
012d4c96 | 383 | return false; |
eb7ee2cb LE |
384 | } |
385 | } else { | |
386 | *obj = true; | |
387 | } | |
388 | ||
389 | processed(ov, name); | |
012d4c96 | 390 | return true; |
eb7ee2cb LE |
391 | } |
392 | ||
393 | ||
012d4c96 | 394 | static bool |
0b2a0d6b | 395 | opts_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp) |
eb7ee2cb | 396 | { |
d7bea75d | 397 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
398 | const QemuOpt *opt; |
399 | const char *str; | |
400 | long long val; | |
401 | char *endptr; | |
402 | ||
d8754f40 LE |
403 | if (ov->list_mode == LM_SIGNED_INTERVAL) { |
404 | *obj = ov->range_next.s; | |
012d4c96 | 405 | return true; |
d8754f40 LE |
406 | } |
407 | ||
eb7ee2cb LE |
408 | opt = lookup_scalar(ov, name, errp); |
409 | if (!opt) { | |
012d4c96 | 410 | return false; |
eb7ee2cb LE |
411 | } |
412 | str = opt->str ? opt->str : ""; | |
413 | ||
1e1c555a LE |
414 | /* we've gotten past lookup_scalar() */ |
415 | assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS); | |
416 | ||
eb7ee2cb LE |
417 | errno = 0; |
418 | val = strtoll(str, &endptr, 0); | |
1e1c555a LE |
419 | if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) { |
420 | if (*endptr == '\0') { | |
421 | *obj = val; | |
422 | processed(ov, name); | |
012d4c96 | 423 | return true; |
1e1c555a LE |
424 | } |
425 | if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) { | |
426 | long long val2; | |
427 | ||
428 | str = endptr + 1; | |
429 | val2 = strtoll(str, &endptr, 0); | |
430 | if (errno == 0 && endptr > str && *endptr == '\0' && | |
15a849be LE |
431 | INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 && |
432 | (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX || | |
433 | val2 < val + OPTS_VISITOR_RANGE_MAX)) { | |
1e1c555a LE |
434 | ov->range_next.s = val; |
435 | ov->range_limit.s = val2; | |
436 | ov->list_mode = LM_SIGNED_INTERVAL; | |
437 | ||
438 | /* as if entering on the top */ | |
439 | *obj = ov->range_next.s; | |
012d4c96 | 440 | return true; |
1e1c555a LE |
441 | } |
442 | } | |
eb7ee2cb | 443 | } |
c6bd8c70 MA |
444 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
445 | (ov->list_mode == LM_NONE) ? "an int64 value" : | |
446 | "an int64 value or range"); | |
012d4c96 | 447 | return false; |
eb7ee2cb LE |
448 | } |
449 | ||
450 | ||
012d4c96 | 451 | static bool |
0b2a0d6b | 452 | opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp) |
eb7ee2cb | 453 | { |
d7bea75d | 454 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
455 | const QemuOpt *opt; |
456 | const char *str; | |
62d090e2 | 457 | unsigned long long val; |
581a8a80 | 458 | char *endptr; |
eb7ee2cb | 459 | |
d8754f40 LE |
460 | if (ov->list_mode == LM_UNSIGNED_INTERVAL) { |
461 | *obj = ov->range_next.u; | |
012d4c96 | 462 | return true; |
d8754f40 LE |
463 | } |
464 | ||
eb7ee2cb LE |
465 | opt = lookup_scalar(ov, name, errp); |
466 | if (!opt) { | |
012d4c96 | 467 | return false; |
eb7ee2cb | 468 | } |
eb7ee2cb | 469 | str = opt->str; |
eb7ee2cb | 470 | |
581a8a80 LE |
471 | /* we've gotten past lookup_scalar() */ |
472 | assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS); | |
473 | ||
474 | if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) { | |
475 | if (*endptr == '\0') { | |
476 | *obj = val; | |
477 | processed(ov, name); | |
012d4c96 | 478 | return true; |
581a8a80 LE |
479 | } |
480 | if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) { | |
481 | unsigned long long val2; | |
482 | ||
483 | str = endptr + 1; | |
484 | if (parse_uint_full(str, &val2, 0) == 0 && | |
15a849be LE |
485 | val2 <= UINT64_MAX && val <= val2 && |
486 | val2 - val < OPTS_VISITOR_RANGE_MAX) { | |
581a8a80 LE |
487 | ov->range_next.u = val; |
488 | ov->range_limit.u = val2; | |
489 | ov->list_mode = LM_UNSIGNED_INTERVAL; | |
490 | ||
491 | /* as if entering on the top */ | |
492 | *obj = ov->range_next.u; | |
012d4c96 | 493 | return true; |
581a8a80 LE |
494 | } |
495 | } | |
eb7ee2cb | 496 | } |
c6bd8c70 MA |
497 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
498 | (ov->list_mode == LM_NONE) ? "a uint64 value" : | |
499 | "a uint64 value or range"); | |
012d4c96 | 500 | return false; |
eb7ee2cb LE |
501 | } |
502 | ||
503 | ||
012d4c96 | 504 | static bool |
0b2a0d6b | 505 | opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp) |
eb7ee2cb | 506 | { |
d7bea75d | 507 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb | 508 | const QemuOpt *opt; |
f17fd4fd | 509 | int err; |
eb7ee2cb LE |
510 | |
511 | opt = lookup_scalar(ov, name, errp); | |
512 | if (!opt) { | |
012d4c96 | 513 | return false; |
eb7ee2cb LE |
514 | } |
515 | ||
f46bfdbf | 516 | err = qemu_strtosz(opt->str ? opt->str : "", NULL, obj); |
f17fd4fd | 517 | if (err < 0) { |
c6bd8c70 | 518 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
f46bfdbf | 519 | "a size value"); |
012d4c96 | 520 | return false; |
eb7ee2cb | 521 | } |
cb45de67 | 522 | |
cb45de67 | 523 | processed(ov, name); |
012d4c96 | 524 | return true; |
eb7ee2cb LE |
525 | } |
526 | ||
527 | ||
528 | static void | |
0b2a0d6b | 529 | opts_optional(Visitor *v, const char *name, bool *present) |
eb7ee2cb | 530 | { |
d7bea75d | 531 | OptsVisitor *ov = to_ov(v); |
eb7ee2cb LE |
532 | |
533 | /* we only support a single mandatory scalar field in a list node */ | |
d9570434 | 534 | assert(ov->list_mode == LM_NONE); |
eb7ee2cb LE |
535 | *present = (lookup_distinct(ov, name, NULL) != NULL); |
536 | } | |
537 | ||
538 | ||
2c0ef9f4 EB |
539 | static void |
540 | opts_free(Visitor *v) | |
541 | { | |
542 | OptsVisitor *ov = to_ov(v); | |
543 | ||
09204eac EB |
544 | if (ov->unprocessed_opts != NULL) { |
545 | g_hash_table_destroy(ov->unprocessed_opts); | |
546 | } | |
547 | g_free(ov->fake_id_opt); | |
548 | g_free(ov); | |
2c0ef9f4 EB |
549 | } |
550 | ||
551 | ||
09204eac | 552 | Visitor * |
eb7ee2cb LE |
553 | opts_visitor_new(const QemuOpts *opts) |
554 | { | |
555 | OptsVisitor *ov; | |
556 | ||
f332e830 | 557 | assert(opts); |
eb7ee2cb LE |
558 | ov = g_malloc0(sizeof *ov); |
559 | ||
983f52d4 EB |
560 | ov->visitor.type = VISITOR_INPUT; |
561 | ||
eb7ee2cb | 562 | ov->visitor.start_struct = &opts_start_struct; |
15c2f669 | 563 | ov->visitor.check_struct = &opts_check_struct; |
eb7ee2cb LE |
564 | ov->visitor.end_struct = &opts_end_struct; |
565 | ||
566 | ov->visitor.start_list = &opts_start_list; | |
567 | ov->visitor.next_list = &opts_next_list; | |
a4a1c70d | 568 | ov->visitor.check_list = &opts_check_list; |
eb7ee2cb LE |
569 | ov->visitor.end_list = &opts_end_list; |
570 | ||
4c40314a | 571 | ov->visitor.type_int64 = &opts_type_int64; |
eb7ee2cb LE |
572 | ov->visitor.type_uint64 = &opts_type_uint64; |
573 | ov->visitor.type_size = &opts_type_size; | |
574 | ov->visitor.type_bool = &opts_type_bool; | |
575 | ov->visitor.type_str = &opts_type_str; | |
576 | ||
577 | /* type_number() is not filled in, but this is not the first visitor to | |
578 | * skip some mandatory methods... */ | |
579 | ||
e2cd0f4f | 580 | ov->visitor.optional = &opts_optional; |
2c0ef9f4 | 581 | ov->visitor.free = opts_free; |
eb7ee2cb LE |
582 | |
583 | ov->opts_root = opts; | |
584 | ||
eb7ee2cb LE |
585 | return &ov->visitor; |
586 | } |