4 * Copyright Red Hat, Inc. 2012-2016
6 * Author: Laszlo Ersek <lersek@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 "qapi/error.h"
15 #include "qemu/cutils.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qapi/opts-visitor.h"
18 #include "qemu/queue.h"
19 #include "qemu/option_int.h"
20 #include "qapi/visitor-impl.h"
25 LM_NONE
, /* not traversing a list of repeated options */
28 * opts_next_list() ready to be called.
30 * Generating the next list link will consume the most
31 * recently parsed QemuOpt instance of the repeated
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.
40 LM_SIGNED_INTERVAL
, /*
41 * opts_next_list() has been called.
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.
49 * Parsing a value into the list link will store the
50 * next element of the signed interval.
53 LM_UNSIGNED_INTERVAL
, /* Same as above, only for an unsigned interval. */
56 * opts_next_list() has been called.
58 * No more QemuOpt instance in the list.
59 * The traversal has been completed.
63 typedef enum ListMode ListMode
;
69 /* Ownership remains with opts_visitor_new()'s caller. */
70 const QemuOpts
*opts_root
;
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
77 GHashTable
*unprocessed_opts
;
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. */
83 GQueue
*repeated_opts
;
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.
92 } range_next
, range_limit
;
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.
102 static OptsVisitor
*to_ov(Visitor
*v
)
104 return container_of(v
, OptsVisitor
, visitor
);
109 destroy_list(gpointer list
)
116 opts_visitor_insert(GHashTable
*unprocessed_opts
, const QemuOpt
*opt
)
120 list
= g_hash_table_lookup(unprocessed_opts
, opt
->name
);
122 list
= g_queue_new();
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.
128 g_hash_table_insert(unprocessed_opts
, (gpointer
)opt
->name
, list
);
131 /* Similarly, destroy_list() doesn't call g_queue_free_full(). */
132 g_queue_push_tail(list
, (gpointer
)opt
);
137 opts_start_struct(Visitor
*v
, const char *name
, void **obj
,
138 size_t size
, Error
**errp
)
140 OptsVisitor
*ov
= to_ov(v
);
144 *obj
= g_malloc0(size
);
146 if (ov
->depth
++ > 0) {
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);
156 opts_visitor_insert(ov
->unprocessed_opts
, opt
);
159 if (ov
->opts_root
->id
!= NULL
) {
160 ov
->fake_id_opt
= g_malloc0(sizeof *ov
->fake_id_opt
);
162 ov
->fake_id_opt
->name
= g_strdup("id");
163 ov
->fake_id_opt
->str
= g_strdup(ov
->opts_root
->id
);
164 opts_visitor_insert(ov
->unprocessed_opts
, ov
->fake_id_opt
);
170 opts_check_struct(Visitor
*v
, Error
**errp
)
172 OptsVisitor
*ov
= to_ov(v
);
180 /* we should have processed all (distinct) QemuOpt instances */
181 g_hash_table_iter_init(&iter
, ov
->unprocessed_opts
);
182 if (g_hash_table_iter_next(&iter
, NULL
, (void **)&any
)) {
183 const QemuOpt
*first
;
185 first
= g_queue_peek_head(any
);
186 error_setg(errp
, QERR_INVALID_PARAMETER
, first
->name
);
192 opts_end_struct(Visitor
*v
, void **obj
)
194 OptsVisitor
*ov
= to_ov(v
);
196 if (--ov
->depth
> 0) {
200 g_hash_table_destroy(ov
->unprocessed_opts
);
201 ov
->unprocessed_opts
= NULL
;
202 if (ov
->fake_id_opt
) {
203 g_free(ov
->fake_id_opt
->name
);
204 g_free(ov
->fake_id_opt
->str
);
205 g_free(ov
->fake_id_opt
);
207 ov
->fake_id_opt
= NULL
;
212 lookup_distinct(const OptsVisitor
*ov
, const char *name
, Error
**errp
)
216 list
= g_hash_table_lookup(ov
->unprocessed_opts
, name
);
218 error_setg(errp
, QERR_MISSING_PARAMETER
, name
);
225 opts_start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
228 OptsVisitor
*ov
= to_ov(v
);
230 /* we can't traverse a list in a list */
231 assert(ov
->list_mode
== LM_NONE
);
232 /* we don't support visits without a list */
234 ov
->repeated_opts
= lookup_distinct(ov
, name
, errp
);
235 if (ov
->repeated_opts
) {
236 ov
->list_mode
= LM_IN_PROGRESS
;
237 *list
= g_malloc0(size
);
245 opts_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
247 OptsVisitor
*ov
= to_ov(v
);
249 switch (ov
->list_mode
) {
252 case LM_SIGNED_INTERVAL
:
253 case LM_UNSIGNED_INTERVAL
:
254 if (ov
->list_mode
== LM_SIGNED_INTERVAL
) {
255 if (ov
->range_next
.s
< ov
->range_limit
.s
) {
259 } else if (ov
->range_next
.u
< ov
->range_limit
.u
) {
263 ov
->list_mode
= LM_IN_PROGRESS
;
264 /* range has been completed, fall through in order to pop option */
266 case LM_IN_PROGRESS
: {
269 opt
= g_queue_pop_head(ov
->repeated_opts
);
270 if (g_queue_is_empty(ov
->repeated_opts
)) {
271 g_hash_table_remove(ov
->unprocessed_opts
, opt
->name
);
272 ov
->repeated_opts
= NULL
;
273 ov
->list_mode
= LM_TRAVERSED
;
283 tail
->next
= g_malloc0(size
);
289 opts_check_list(Visitor
*v
, Error
**errp
)
292 * Unvisited list elements will be reported later when checking
293 * whether unvisited struct members remain.
299 opts_end_list(Visitor
*v
, void **obj
)
301 OptsVisitor
*ov
= to_ov(v
);
303 assert(ov
->list_mode
== LM_IN_PROGRESS
||
304 ov
->list_mode
== LM_SIGNED_INTERVAL
||
305 ov
->list_mode
== LM_UNSIGNED_INTERVAL
||
306 ov
->list_mode
== LM_TRAVERSED
);
307 ov
->repeated_opts
= NULL
;
308 ov
->list_mode
= LM_NONE
;
312 static const QemuOpt
*
313 lookup_scalar(const OptsVisitor
*ov
, const char *name
, Error
**errp
)
315 if (ov
->list_mode
== LM_NONE
) {
318 /* the last occurrence of any QemuOpt takes effect when queried by name
320 list
= lookup_distinct(ov
, name
, errp
);
321 return list
? g_queue_peek_tail(list
) : NULL
;
323 if (ov
->list_mode
== LM_TRAVERSED
) {
324 error_setg(errp
, "Fewer list elements than expected");
327 assert(ov
->list_mode
== LM_IN_PROGRESS
);
328 return g_queue_peek_head(ov
->repeated_opts
);
333 processed(OptsVisitor
*ov
, const char *name
)
335 if (ov
->list_mode
== LM_NONE
) {
336 g_hash_table_remove(ov
->unprocessed_opts
, name
);
339 assert(ov
->list_mode
== LM_IN_PROGRESS
);
345 opts_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
347 OptsVisitor
*ov
= to_ov(v
);
350 opt
= lookup_scalar(ov
, name
, errp
);
355 *obj
= g_strdup(opt
->str
? opt
->str
: "");
356 /* Note that we consume a string even if this is called as part of
357 * an enum visit that later fails because the string is not a
358 * valid enum value; this is harmless because tracking what gets
359 * consumed only matters to visit_end_struct() as the final error
360 * check if there were no other failures during the visit. */
365 /* mimics qemu-option.c::parse_option_bool() */
367 opts_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
369 OptsVisitor
*ov
= to_ov(v
);
372 opt
= lookup_scalar(ov
, name
, errp
);
378 if (strcmp(opt
->str
, "on") == 0 ||
379 strcmp(opt
->str
, "yes") == 0 ||
380 strcmp(opt
->str
, "y") == 0) {
382 } else if (strcmp(opt
->str
, "off") == 0 ||
383 strcmp(opt
->str
, "no") == 0 ||
384 strcmp(opt
->str
, "n") == 0) {
387 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, opt
->name
,
388 "on|yes|y|off|no|n");
400 opts_type_int64(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
402 OptsVisitor
*ov
= to_ov(v
);
408 if (ov
->list_mode
== LM_SIGNED_INTERVAL
) {
409 *obj
= ov
->range_next
.s
;
413 opt
= lookup_scalar(ov
, name
, errp
);
417 str
= opt
->str
? opt
->str
: "";
419 /* we've gotten past lookup_scalar() */
420 assert(ov
->list_mode
== LM_NONE
|| ov
->list_mode
== LM_IN_PROGRESS
);
423 val
= strtoll(str
, &endptr
, 0);
424 if (errno
== 0 && endptr
> str
&& INT64_MIN
<= val
&& val
<= INT64_MAX
) {
425 if (*endptr
== '\0') {
430 if (*endptr
== '-' && ov
->list_mode
== LM_IN_PROGRESS
) {
434 val2
= strtoll(str
, &endptr
, 0);
435 if (errno
== 0 && endptr
> str
&& *endptr
== '\0' &&
436 INT64_MIN
<= val2
&& val2
<= INT64_MAX
&& val
<= val2
&&
437 (val
> INT64_MAX
- OPTS_VISITOR_RANGE_MAX
||
438 val2
< val
+ OPTS_VISITOR_RANGE_MAX
)) {
439 ov
->range_next
.s
= val
;
440 ov
->range_limit
.s
= val2
;
441 ov
->list_mode
= LM_SIGNED_INTERVAL
;
443 /* as if entering on the top */
444 *obj
= ov
->range_next
.s
;
449 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, opt
->name
,
450 (ov
->list_mode
== LM_NONE
) ? "an int64 value" :
451 "an int64 value or range");
456 opts_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
, Error
**errp
)
458 OptsVisitor
*ov
= to_ov(v
);
461 unsigned long long val
;
464 if (ov
->list_mode
== LM_UNSIGNED_INTERVAL
) {
465 *obj
= ov
->range_next
.u
;
469 opt
= lookup_scalar(ov
, name
, errp
);
475 /* we've gotten past lookup_scalar() */
476 assert(ov
->list_mode
== LM_NONE
|| ov
->list_mode
== LM_IN_PROGRESS
);
478 if (parse_uint(str
, &val
, &endptr
, 0) == 0 && val
<= UINT64_MAX
) {
479 if (*endptr
== '\0') {
484 if (*endptr
== '-' && ov
->list_mode
== LM_IN_PROGRESS
) {
485 unsigned long long val2
;
488 if (parse_uint_full(str
, &val2
, 0) == 0 &&
489 val2
<= UINT64_MAX
&& val
<= val2
&&
490 val2
- val
< OPTS_VISITOR_RANGE_MAX
) {
491 ov
->range_next
.u
= val
;
492 ov
->range_limit
.u
= val2
;
493 ov
->list_mode
= LM_UNSIGNED_INTERVAL
;
495 /* as if entering on the top */
496 *obj
= ov
->range_next
.u
;
501 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, opt
->name
,
502 (ov
->list_mode
== LM_NONE
) ? "a uint64 value" :
503 "a uint64 value or range");
508 opts_type_size(Visitor
*v
, const char *name
, uint64_t *obj
, Error
**errp
)
510 OptsVisitor
*ov
= to_ov(v
);
514 opt
= lookup_scalar(ov
, name
, errp
);
519 err
= qemu_strtosz(opt
->str
? opt
->str
: "", NULL
, obj
);
521 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, opt
->name
,
531 opts_optional(Visitor
*v
, const char *name
, bool *present
)
533 OptsVisitor
*ov
= to_ov(v
);
535 /* we only support a single mandatory scalar field in a list node */
536 assert(ov
->list_mode
== LM_NONE
);
537 *present
= (lookup_distinct(ov
, name
, NULL
) != NULL
);
542 opts_free(Visitor
*v
)
544 OptsVisitor
*ov
= to_ov(v
);
546 if (ov
->unprocessed_opts
!= NULL
) {
547 g_hash_table_destroy(ov
->unprocessed_opts
);
549 g_free(ov
->fake_id_opt
);
555 opts_visitor_new(const QemuOpts
*opts
)
560 ov
= g_malloc0(sizeof *ov
);
562 ov
->visitor
.type
= VISITOR_INPUT
;
564 ov
->visitor
.start_struct
= &opts_start_struct
;
565 ov
->visitor
.check_struct
= &opts_check_struct
;
566 ov
->visitor
.end_struct
= &opts_end_struct
;
568 ov
->visitor
.start_list
= &opts_start_list
;
569 ov
->visitor
.next_list
= &opts_next_list
;
570 ov
->visitor
.check_list
= &opts_check_list
;
571 ov
->visitor
.end_list
= &opts_end_list
;
573 ov
->visitor
.type_int64
= &opts_type_int64
;
574 ov
->visitor
.type_uint64
= &opts_type_uint64
;
575 ov
->visitor
.type_size
= &opts_type_size
;
576 ov
->visitor
.type_bool
= &opts_type_bool
;
577 ov
->visitor
.type_str
= &opts_type_str
;
579 /* type_number() is not filled in, but this is not the first visitor to
580 * skip some mandatory methods... */
582 ov
->visitor
.optional
= &opts_optional
;
583 ov
->visitor
.free
= opts_free
;
585 ov
->opts_root
= opts
;