4 * Copyright Red Hat, Inc. 2012
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-common.h"
14 #include "opts-visitor.h"
15 #include "qemu-queue.h"
16 #include "qemu-option-internal.h"
17 #include "qapi-visit-impl.h"
24 /* Ownership remains with opts_visitor_new()'s caller. */
25 const QemuOpts *opts_root;
29 /* Non-null iff depth is positive. Each key is a QemuOpt name. Each value
30 * is a non-empty GQueue, enumerating all QemuOpt occurrences with that
32 GHashTable *unprocessed_opts;
34 /* The list currently being traversed with opts_start_list() /
35 * opts_next_list(). The list must have a struct element type in the
36 * schema, with a single mandatory scalar member. */
37 GQueue *repeated_opts;
38 bool repeated_opts_first;
40 /* If "opts_root->id" is set, reinstantiate it as a fake QemuOpt for
41 * uniformity. Only its "name" and "str" fields are set. "fake_id_opt" does
42 * not survive or escape the OptsVisitor object.
49 destroy_list(gpointer list)
56 opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt)
60 list = g_hash_table_lookup(unprocessed_opts, opt->name);
64 /* GHashTable will never try to free the keys -- we supply NULL as
65 * "key_destroy_func" in opts_start_struct(). Thus cast away key
66 * const-ness in order to suppress gcc's warning.
68 g_hash_table_insert(unprocessed_opts, (gpointer)opt->name, list);
71 /* Similarly, destroy_list() doesn't call g_queue_free_full(). */
72 g_queue_push_tail(list, (gpointer)opt);
77 opts_start_struct(Visitor *v, void **obj, const char *kind,
78 const char *name, size_t size, Error **errp)
80 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
83 *obj = g_malloc0(size > 0 ? size : 1);
84 if (ov->depth++ > 0) {
88 ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal,
90 QTAILQ_FOREACH(opt, &ov->opts_root->head, next) {
91 /* ensured by qemu-option.c::opts_do_parse() */
92 assert(strcmp(opt->name, "id") != 0);
94 opts_visitor_insert(ov->unprocessed_opts, opt);
97 if (ov->opts_root->id != NULL) {
98 ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt);
100 ov->fake_id_opt->name = "id";
101 ov->fake_id_opt->str = ov->opts_root->id;
102 opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt);
108 ghr_true(gpointer ign_key, gpointer ign_value, gpointer ign_user_data)
115 opts_end_struct(Visitor *v, Error **errp)
117 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
120 if (--ov->depth > 0) {
124 /* we should have processed all (distinct) QemuOpt instances */
125 any = g_hash_table_find(ov->unprocessed_opts, &ghr_true, NULL);
127 const QemuOpt *first;
129 first = g_queue_peek_head(any);
130 error_set(errp, QERR_INVALID_PARAMETER, first->name);
132 g_hash_table_destroy(ov->unprocessed_opts);
133 ov->unprocessed_opts = NULL;
134 g_free(ov->fake_id_opt);
135 ov->fake_id_opt = NULL;
140 lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp)
144 list = g_hash_table_lookup(ov->unprocessed_opts, name);
146 error_set(errp, QERR_MISSING_PARAMETER, name);
153 opts_start_list(Visitor *v, const char *name, Error **errp)
155 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
157 /* we can't traverse a list in a list */
158 assert(ov->repeated_opts == NULL);
159 ov->repeated_opts = lookup_distinct(ov, name, errp);
160 ov->repeated_opts_first = (ov->repeated_opts != NULL);
165 opts_next_list(Visitor *v, GenericList **list, Error **errp)
167 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
170 if (ov->repeated_opts_first) {
171 ov->repeated_opts_first = false;
176 opt = g_queue_pop_head(ov->repeated_opts);
177 if (g_queue_is_empty(ov->repeated_opts)) {
178 g_hash_table_remove(ov->unprocessed_opts, opt->name);
181 link = &(*list)->next;
184 *link = g_malloc0(sizeof **link);
190 opts_end_list(Visitor *v, Error **errp)
192 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
194 ov->repeated_opts = NULL;
198 static const QemuOpt *
199 lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp)
201 if (ov->repeated_opts == NULL) {
204 /* the last occurrence of any QemuOpt takes effect when queried by name
206 list = lookup_distinct(ov, name, errp);
207 return list ? g_queue_peek_tail(list) : NULL;
209 return g_queue_peek_head(ov->repeated_opts);
214 processed(OptsVisitor *ov, const char *name)
216 if (ov->repeated_opts == NULL) {
217 g_hash_table_remove(ov->unprocessed_opts, name);
223 opts_type_str(Visitor *v, char **obj, const char *name, Error **errp)
225 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
228 opt = lookup_scalar(ov, name, errp);
232 *obj = g_strdup(opt->str ? opt->str : "");
237 /* mimics qemu-option.c::parse_option_bool() */
239 opts_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
241 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
244 opt = lookup_scalar(ov, name, errp);
250 if (strcmp(opt->str, "on") == 0 ||
251 strcmp(opt->str, "yes") == 0 ||
252 strcmp(opt->str, "y") == 0) {
254 } else if (strcmp(opt->str, "off") == 0 ||
255 strcmp(opt->str, "no") == 0 ||
256 strcmp(opt->str, "n") == 0) {
259 error_set(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
260 "on|yes|y|off|no|n");
272 opts_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
274 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
280 opt = lookup_scalar(ov, name, errp);
284 str = opt->str ? opt->str : "";
287 val = strtoll(str, &endptr, 0);
288 if (*str != '\0' && *endptr == '\0' && errno == 0 && INT64_MIN <= val &&
294 error_set(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, "an int64 value");
299 opts_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
301 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
305 opt = lookup_scalar(ov, name, errp);
312 while (isspace((unsigned char)*str)) {
316 if (*str != '-' && *str != '\0') {
317 unsigned long long val;
320 /* non-empty, non-negative subject sequence */
322 val = strtoull(str, &endptr, 0);
323 if (*endptr == '\0' && errno == 0 && val <= UINT64_MAX) {
330 error_set(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
336 opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
338 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
343 opt = lookup_scalar(ov, name, errp);
348 val = strtosz_suffix(opt->str ? opt->str : "", &endptr,
349 STRTOSZ_DEFSUFFIX_B);
350 if (val != -1 && *endptr == '\0') {
355 error_set(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
356 "a size value representible as a non-negative int64");
361 opts_start_optional(Visitor *v, bool *present, const char *name,
364 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
366 /* we only support a single mandatory scalar field in a list node */
367 assert(ov->repeated_opts == NULL);
368 *present = (lookup_distinct(ov, name, NULL) != NULL);
373 opts_visitor_new(const QemuOpts *opts)
377 ov = g_malloc0(sizeof *ov);
379 ov->visitor.start_struct = &opts_start_struct;
380 ov->visitor.end_struct = &opts_end_struct;
382 ov->visitor.start_list = &opts_start_list;
383 ov->visitor.next_list = &opts_next_list;
384 ov->visitor.end_list = &opts_end_list;
386 /* input_type_enum() covers both "normal" enums and union discriminators.
387 * The union discriminator field is always generated as "type"; it should
388 * match the "type" QemuOpt child of any QemuOpts.
390 * input_type_enum() will remove the looked-up key from the
391 * "unprocessed_opts" hash even if the lookup fails, because the removal is
392 * done earlier in opts_type_str(). This should be harmless.
394 ov->visitor.type_enum = &input_type_enum;
396 ov->visitor.type_int = &opts_type_int;
397 ov->visitor.type_uint64 = &opts_type_uint64;
398 ov->visitor.type_size = &opts_type_size;
399 ov->visitor.type_bool = &opts_type_bool;
400 ov->visitor.type_str = &opts_type_str;
402 /* type_number() is not filled in, but this is not the first visitor to
403 * skip some mandatory methods... */
405 ov->visitor.start_optional = &opts_start_optional;
407 ov->opts_root = opts;
414 opts_visitor_cleanup(OptsVisitor *ov)
416 if (ov->unprocessed_opts != NULL) {
417 g_hash_table_destroy(ov->unprocessed_opts);
419 g_free(ov->fake_id_opt);
425 opts_get_visitor(OptsVisitor *ov)