configure: remove useless code to check for Xen PCI passthrough
[qemu/ar7.git] / util / qemu-option.c
blobc88e159f1876dfd5522a0cac68bab2cd615ab613
1 /*
2 * Commandline option parsing functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/qbool.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qapi/qmp/qerror.h"
35 #include "qemu/option_int.h"
36 #include "qemu/cutils.h"
37 #include "qemu/id.h"
38 #include "qemu/help_option.h"
41 * Extracts the name of an option from the parameter string (@p points at the
42 * first byte of the option name)
44 * The option name is @len characters long and is copied into @option. The
45 * caller is responsible for free'ing @option when no longer required.
47 * The return value is the position of the delimiter/zero byte after the option
48 * name in @p.
50 static const char *get_opt_name(const char *p, char **option, size_t len)
52 *option = g_strndup(p, len);
53 return p + len;
57 * Extracts the value of an option from the parameter string p (p points at the
58 * first byte of the option value)
60 * This function is comparable to get_opt_name with the difference that the
61 * delimiter is fixed to be comma which starts a new option. To specify an
62 * option value that contains commas, double each comma.
64 const char *get_opt_value(const char *p, char **value)
66 size_t capacity = 0, length;
67 const char *offset;
69 *value = NULL;
70 while (1) {
71 offset = qemu_strchrnul(p, ',');
72 length = offset - p;
73 if (*offset != '\0' && *(offset + 1) == ',') {
74 length++;
76 *value = g_renew(char, *value, capacity + length + 1);
77 strncpy(*value + capacity, p, length);
78 (*value)[capacity + length] = '\0';
79 capacity += length;
80 if (*offset == '\0' ||
81 *(offset + 1) != ',') {
82 break;
85 p += (offset - p) + 2;
88 return offset;
91 static bool parse_option_number(const char *name, const char *value,
92 uint64_t *ret, Error **errp)
94 uint64_t number;
95 int err;
97 err = qemu_strtou64(value, NULL, 0, &number);
98 if (err == -ERANGE) {
99 error_setg(errp, "Value '%s' is too large for parameter '%s'",
100 value, name);
101 return false;
103 if (err) {
104 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
105 return false;
107 *ret = number;
108 return true;
111 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
112 const char *name)
114 int i;
116 for (i = 0; desc[i].name != NULL; i++) {
117 if (strcmp(desc[i].name, name) == 0) {
118 return &desc[i];
122 return NULL;
125 static const char *find_default_by_name(QemuOpts *opts, const char *name)
127 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
129 return desc ? desc->def_value_str : NULL;
132 bool parse_option_size(const char *name, const char *value,
133 uint64_t *ret, Error **errp)
135 uint64_t size;
136 int err;
138 err = qemu_strtosz(value, NULL, &size);
139 if (err == -ERANGE) {
140 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
141 value, name);
142 return false;
144 if (err) {
145 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
146 "a non-negative number below 2^64");
147 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
148 " kilo-, mega-, giga-, tera-, peta-\n"
149 "and exabytes, respectively.\n");
150 return false;
152 *ret = size;
153 return true;
156 static const char *opt_type_to_string(enum QemuOptType type)
158 switch (type) {
159 case QEMU_OPT_STRING:
160 return "str";
161 case QEMU_OPT_BOOL:
162 return "bool (on/off)";
163 case QEMU_OPT_NUMBER:
164 return "num";
165 case QEMU_OPT_SIZE:
166 return "size";
169 g_assert_not_reached();
173 * Print the list of options available in the given list. If
174 * @print_caption is true, a caption (including the list name, if it
175 * exists) is printed. The options itself will be indented, so
176 * @print_caption should only be set to false if the caller prints its
177 * own custom caption (so that the indentation makes sense).
179 void qemu_opts_print_help(QemuOptsList *list, bool print_caption)
181 QemuOptDesc *desc;
182 int i;
183 GPtrArray *array = g_ptr_array_new();
185 assert(list);
186 desc = list->desc;
187 while (desc && desc->name) {
188 GString *str = g_string_new(NULL);
189 g_string_append_printf(str, " %s=<%s>", desc->name,
190 opt_type_to_string(desc->type));
191 if (desc->help) {
192 if (str->len < 24) {
193 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
195 g_string_append_printf(str, " - %s", desc->help);
197 g_ptr_array_add(array, g_string_free(str, false));
198 desc++;
201 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
202 if (print_caption && array->len > 0) {
203 if (list->name) {
204 printf("%s options:\n", list->name);
205 } else {
206 printf("Options:\n");
208 } else if (array->len == 0) {
209 if (list->name) {
210 printf("There are no options for %s.\n", list->name);
211 } else {
212 printf("No options available.\n");
215 for (i = 0; i < array->len; i++) {
216 printf("%s\n", (char *)array->pdata[i]);
218 g_ptr_array_set_free_func(array, g_free);
219 g_ptr_array_free(array, true);
222 /* ------------------------------------------------------------------ */
224 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
226 QemuOpt *opt;
228 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
229 if (strcmp(opt->name, name) != 0)
230 continue;
231 return opt;
233 return NULL;
236 static void qemu_opt_del(QemuOpt *opt)
238 QTAILQ_REMOVE(&opt->opts->head, opt, next);
239 g_free(opt->name);
240 g_free(opt->str);
241 g_free(opt);
244 /* qemu_opt_set allows many settings for the same option.
245 * This function deletes all settings for an option.
247 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
249 QemuOpt *opt, *next_opt;
251 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
252 if (!strcmp(opt->name, name)) {
253 qemu_opt_del(opt);
258 const char *qemu_opt_get(QemuOpts *opts, const char *name)
260 QemuOpt *opt;
262 if (opts == NULL) {
263 return NULL;
266 opt = qemu_opt_find(opts, name);
267 if (!opt) {
268 return find_default_by_name(opts, name);
271 return opt->str;
274 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
276 iter->opts = opts;
277 iter->opt = QTAILQ_FIRST(&opts->head);
278 iter->name = name;
281 const char *qemu_opt_iter_next(QemuOptsIter *iter)
283 QemuOpt *ret = iter->opt;
284 if (iter->name) {
285 while (ret && !g_str_equal(iter->name, ret->name)) {
286 ret = QTAILQ_NEXT(ret, next);
289 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
290 return ret ? ret->str : NULL;
293 /* Get a known option (or its default) and remove it from the list
294 * all in one action. Return a malloced string of the option value.
295 * Result must be freed by caller with g_free().
297 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
299 QemuOpt *opt;
300 char *str;
302 if (opts == NULL) {
303 return NULL;
306 opt = qemu_opt_find(opts, name);
307 if (!opt) {
308 return g_strdup(find_default_by_name(opts, name));
310 str = opt->str;
311 opt->str = NULL;
312 qemu_opt_del_all(opts, name);
313 return str;
316 bool qemu_opt_has_help_opt(QemuOpts *opts)
318 QemuOpt *opt;
320 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
321 if (is_help_option(opt->name)) {
322 return true;
325 return false;
328 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
329 bool defval, bool del)
331 QemuOpt *opt;
332 const char *def_val;
333 bool ret = defval;
335 if (opts == NULL) {
336 return ret;
339 opt = qemu_opt_find(opts, name);
340 if (opt == NULL) {
341 def_val = find_default_by_name(opts, name);
342 if (def_val) {
343 qapi_bool_parse(name, def_val, &ret, &error_abort);
345 return ret;
347 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
348 ret = opt->value.boolean;
349 if (del) {
350 qemu_opt_del_all(opts, name);
352 return ret;
355 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
357 return qemu_opt_get_bool_helper(opts, name, defval, false);
360 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
362 return qemu_opt_get_bool_helper(opts, name, defval, true);
365 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
366 uint64_t defval, bool del)
368 QemuOpt *opt;
369 const char *def_val;
370 uint64_t ret = defval;
372 if (opts == NULL) {
373 return ret;
376 opt = qemu_opt_find(opts, name);
377 if (opt == NULL) {
378 def_val = find_default_by_name(opts, name);
379 if (def_val) {
380 parse_option_number(name, def_val, &ret, &error_abort);
382 return ret;
384 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
385 ret = opt->value.uint;
386 if (del) {
387 qemu_opt_del_all(opts, name);
389 return ret;
392 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
394 return qemu_opt_get_number_helper(opts, name, defval, false);
397 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
398 uint64_t defval)
400 return qemu_opt_get_number_helper(opts, name, defval, true);
403 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
404 uint64_t defval, bool del)
406 QemuOpt *opt;
407 const char *def_val;
408 uint64_t ret = defval;
410 if (opts == NULL) {
411 return ret;
414 opt = qemu_opt_find(opts, name);
415 if (opt == NULL) {
416 def_val = find_default_by_name(opts, name);
417 if (def_val) {
418 parse_option_size(name, def_val, &ret, &error_abort);
420 return ret;
422 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
423 ret = opt->value.uint;
424 if (del) {
425 qemu_opt_del_all(opts, name);
427 return ret;
430 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
432 return qemu_opt_get_size_helper(opts, name, defval, false);
435 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
436 uint64_t defval)
438 return qemu_opt_get_size_helper(opts, name, defval, true);
441 static bool qemu_opt_parse(QemuOpt *opt, Error **errp)
443 if (opt->desc == NULL)
444 return true;
446 switch (opt->desc->type) {
447 case QEMU_OPT_STRING:
448 /* nothing */
449 return true;
450 case QEMU_OPT_BOOL:
451 return qapi_bool_parse(opt->name, opt->str, &opt->value.boolean, errp);
452 case QEMU_OPT_NUMBER:
453 return parse_option_number(opt->name, opt->str, &opt->value.uint,
454 errp);
455 case QEMU_OPT_SIZE:
456 return parse_option_size(opt->name, opt->str, &opt->value.uint,
457 errp);
458 default:
459 abort();
463 static bool opts_accepts_any(const QemuOptsList *list)
465 return list->desc[0].name == NULL;
468 int qemu_opt_unset(QemuOpts *opts, const char *name)
470 QemuOpt *opt = qemu_opt_find(opts, name);
472 assert(opts_accepts_any(opts->list));
474 if (opt == NULL) {
475 return -1;
476 } else {
477 qemu_opt_del(opt);
478 return 0;
482 static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
483 bool prepend)
485 QemuOpt *opt = g_malloc0(sizeof(*opt));
487 opt->name = g_strdup(name);
488 opt->str = value;
489 opt->opts = opts;
490 if (prepend) {
491 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
492 } else {
493 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
496 return opt;
499 static bool opt_validate(QemuOpt *opt, bool *help_wanted,
500 Error **errp)
502 const QemuOptDesc *desc;
503 const QemuOptsList *list = opt->opts->list;
505 desc = find_desc_by_name(list->desc, opt->name);
506 if (!desc && !opts_accepts_any(list)) {
507 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
508 if (help_wanted && is_help_option(opt->name)) {
509 *help_wanted = true;
511 return false;
514 opt->desc = desc;
515 if (!qemu_opt_parse(opt, errp)) {
516 return false;
519 return true;
522 bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
523 Error **errp)
525 QemuOpt *opt = opt_create(opts, name, g_strdup(value), false);
527 if (!opt_validate(opt, NULL, errp)) {
528 qemu_opt_del(opt);
529 return false;
531 return true;
534 bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
535 Error **errp)
537 QemuOpt *opt;
538 const QemuOptDesc *desc;
539 const QemuOptsList *list = opts->list;
541 desc = find_desc_by_name(list->desc, name);
542 if (!desc && !opts_accepts_any(list)) {
543 error_setg(errp, QERR_INVALID_PARAMETER, name);
544 return false;
547 opt = g_malloc0(sizeof(*opt));
548 opt->name = g_strdup(name);
549 opt->opts = opts;
550 opt->desc = desc;
551 opt->value.boolean = !!val;
552 opt->str = g_strdup(val ? "on" : "off");
553 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
554 return true;
557 bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
558 Error **errp)
560 QemuOpt *opt;
561 const QemuOptDesc *desc;
562 const QemuOptsList *list = opts->list;
564 desc = find_desc_by_name(list->desc, name);
565 if (!desc && !opts_accepts_any(list)) {
566 error_setg(errp, QERR_INVALID_PARAMETER, name);
567 return false;
570 opt = g_malloc0(sizeof(*opt));
571 opt->name = g_strdup(name);
572 opt->opts = opts;
573 opt->desc = desc;
574 opt->value.uint = val;
575 opt->str = g_strdup_printf("%" PRId64, val);
576 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
577 return true;
581 * For each member of @opts, call @func(@opaque, name, value, @errp).
582 * @func() may store an Error through @errp, but must return non-zero then.
583 * When @func() returns non-zero, break the loop and return that value.
584 * Return zero when the loop completes.
586 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
587 Error **errp)
589 QemuOpt *opt;
590 int rc;
592 QTAILQ_FOREACH(opt, &opts->head, next) {
593 rc = func(opaque, opt->name, opt->str, errp);
594 if (rc) {
595 return rc;
597 assert(!errp || !*errp);
599 return 0;
602 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
604 QemuOpts *opts;
606 QTAILQ_FOREACH(opts, &list->head, next) {
607 if (!opts->id && !id) {
608 return opts;
610 if (opts->id && id && !strcmp(opts->id, id)) {
611 return opts;
614 return NULL;
617 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
618 int fail_if_exists, Error **errp)
620 QemuOpts *opts = NULL;
622 if (id) {
623 if (!id_wellformed(id)) {
624 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
625 "an identifier");
626 error_append_hint(errp, "Identifiers consist of letters, digits, "
627 "'-', '.', '_', starting with a letter.\n");
628 return NULL;
630 opts = qemu_opts_find(list, id);
631 if (opts != NULL) {
632 if (fail_if_exists && !list->merge_lists) {
633 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
634 return NULL;
635 } else {
636 return opts;
639 } else if (list->merge_lists) {
640 opts = qemu_opts_find(list, NULL);
641 if (opts) {
642 return opts;
645 opts = g_malloc0(sizeof(*opts));
646 opts->id = g_strdup(id);
647 opts->list = list;
648 loc_save(&opts->loc);
649 QTAILQ_INIT(&opts->head);
650 QTAILQ_INSERT_TAIL(&list->head, opts, next);
651 return opts;
654 void qemu_opts_reset(QemuOptsList *list)
656 QemuOpts *opts, *next_opts;
658 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
659 qemu_opts_del(opts);
663 void qemu_opts_loc_restore(QemuOpts *opts)
665 loc_restore(&opts->loc);
668 bool qemu_opts_set(QemuOptsList *list, const char *name, const char *value, Error **errp)
670 QemuOpts *opts;
672 assert(list->merge_lists);
673 opts = qemu_opts_create(list, NULL, 0, &error_abort);
674 return qemu_opt_set(opts, name, value, errp);
677 const char *qemu_opts_id(QemuOpts *opts)
679 return opts->id;
682 /* The id string will be g_free()d by qemu_opts_del */
683 void qemu_opts_set_id(QemuOpts *opts, char *id)
685 opts->id = id;
688 void qemu_opts_del(QemuOpts *opts)
690 QemuOpt *opt;
692 if (opts == NULL) {
693 return;
696 for (;;) {
697 opt = QTAILQ_FIRST(&opts->head);
698 if (opt == NULL)
699 break;
700 qemu_opt_del(opt);
702 QTAILQ_REMOVE(&opts->list->head, opts, next);
703 g_free(opts->id);
704 g_free(opts);
707 /* print value, escaping any commas in value */
708 static void escaped_print(const char *value)
710 const char *ptr;
712 for (ptr = value; *ptr; ++ptr) {
713 if (*ptr == ',') {
714 putchar(',');
716 putchar(*ptr);
720 void qemu_opts_print(QemuOpts *opts, const char *separator)
722 QemuOpt *opt;
723 QemuOptDesc *desc = opts->list->desc;
724 const char *sep = "";
726 if (opts->id) {
727 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
728 sep = separator;
731 if (desc[0].name == NULL) {
732 QTAILQ_FOREACH(opt, &opts->head, next) {
733 printf("%s%s=", sep, opt->name);
734 escaped_print(opt->str);
735 sep = separator;
737 return;
739 for (; desc && desc->name; desc++) {
740 const char *value;
741 opt = qemu_opt_find(opts, desc->name);
743 value = opt ? opt->str : desc->def_value_str;
744 if (!value) {
745 continue;
747 if (desc->type == QEMU_OPT_STRING) {
748 printf("%s%s=", sep, desc->name);
749 escaped_print(value);
750 } else if ((desc->type == QEMU_OPT_SIZE ||
751 desc->type == QEMU_OPT_NUMBER) && opt) {
752 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
753 } else {
754 printf("%s%s=%s", sep, desc->name, value);
756 sep = separator;
760 static const char *get_opt_name_value(const char *params,
761 const char *firstname,
762 char **name, char **value)
764 const char *p;
765 size_t len;
767 len = strcspn(params, "=,");
768 if (params[len] != '=') {
769 /* found "foo,more" */
770 if (firstname) {
771 /* implicitly named first option */
772 *name = g_strdup(firstname);
773 p = get_opt_value(params, value);
774 } else {
775 /* option without value, must be a flag */
776 p = get_opt_name(params, name, len);
777 if (strncmp(*name, "no", 2) == 0) {
778 memmove(*name, *name + 2, strlen(*name + 2) + 1);
779 *value = g_strdup("off");
780 } else {
781 *value = g_strdup("on");
784 } else {
785 /* found "foo=bar,more" */
786 p = get_opt_name(params, name, len);
787 assert(*p == '=');
788 p++;
789 p = get_opt_value(p, value);
792 assert(!*p || *p == ',');
793 if (*p == ',') {
794 p++;
796 return p;
799 static bool opts_do_parse(QemuOpts *opts, const char *params,
800 const char *firstname, bool prepend,
801 bool *help_wanted, Error **errp)
803 char *option, *value;
804 const char *p;
805 QemuOpt *opt;
807 for (p = params; *p;) {
808 p = get_opt_name_value(p, firstname, &option, &value);
809 firstname = NULL;
811 if (!strcmp(option, "id")) {
812 g_free(option);
813 g_free(value);
814 continue;
817 opt = opt_create(opts, option, value, prepend);
818 g_free(option);
819 if (!opt_validate(opt, help_wanted, errp)) {
820 qemu_opt_del(opt);
821 return false;
825 return true;
828 static char *opts_parse_id(const char *params)
830 const char *p;
831 char *name, *value;
833 for (p = params; *p;) {
834 p = get_opt_name_value(p, NULL, &name, &value);
835 if (!strcmp(name, "id")) {
836 g_free(name);
837 return value;
839 g_free(name);
840 g_free(value);
843 return NULL;
846 bool has_help_option(const char *params)
848 const char *p;
849 char *name, *value;
850 bool ret;
852 for (p = params; *p;) {
853 p = get_opt_name_value(p, NULL, &name, &value);
854 ret = is_help_option(name);
855 g_free(name);
856 g_free(value);
857 if (ret) {
858 return true;
862 return false;
866 * Store options parsed from @params into @opts.
867 * If @firstname is non-null, the first key=value in @params may omit
868 * key=, and is treated as if key was @firstname.
869 * On error, store an error object through @errp if non-null.
871 bool qemu_opts_do_parse(QemuOpts *opts, const char *params,
872 const char *firstname, Error **errp)
874 return opts_do_parse(opts, params, firstname, false, NULL, errp);
877 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
878 bool permit_abbrev, bool defaults,
879 bool *help_wanted, Error **errp)
881 const char *firstname;
882 char *id = opts_parse_id(params);
883 QemuOpts *opts;
885 assert(!permit_abbrev || list->implied_opt_name);
886 firstname = permit_abbrev ? list->implied_opt_name : NULL;
889 * This code doesn't work for defaults && !list->merge_lists: when
890 * params has no id=, and list has an element with !opts->id, it
891 * appends a new element instead of returning the existing opts.
892 * However, we got no use for this case. Guard against possible
893 * (if unlikely) future misuse:
895 assert(!defaults || list->merge_lists);
896 opts = qemu_opts_create(list, id, !defaults, errp);
897 g_free(id);
898 if (opts == NULL) {
899 return NULL;
902 if (!opts_do_parse(opts, params, firstname, defaults, help_wanted,
903 errp)) {
904 qemu_opts_del(opts);
905 return NULL;
908 return opts;
912 * Create a QemuOpts in @list and with options parsed from @params.
913 * If @permit_abbrev, the first key=value in @params may omit key=,
914 * and is treated as if key was @list->implied_opt_name.
915 * On error, store an error object through @errp if non-null.
916 * Return the new QemuOpts on success, null pointer on error.
918 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
919 bool permit_abbrev, Error **errp)
921 return opts_parse(list, params, permit_abbrev, false, NULL, errp);
925 * Create a QemuOpts in @list and with options parsed from @params.
926 * If @permit_abbrev, the first key=value in @params may omit key=,
927 * and is treated as if key was @list->implied_opt_name.
928 * Report errors with error_report_err(). This is inappropriate in
929 * QMP context. Do not use this function there!
930 * Return the new QemuOpts on success, null pointer on error.
932 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
933 bool permit_abbrev)
935 Error *err = NULL;
936 QemuOpts *opts;
937 bool help_wanted = false;
939 opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err);
940 if (err) {
941 if (help_wanted) {
942 qemu_opts_print_help(list, true);
943 error_free(err);
944 } else {
945 error_report_err(err);
948 return opts;
951 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
952 int permit_abbrev)
954 QemuOpts *opts;
956 opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL);
957 assert(opts);
960 static bool qemu_opts_from_qdict_entry(QemuOpts *opts,
961 const QDictEntry *entry,
962 Error **errp)
964 const char *key = qdict_entry_key(entry);
965 QObject *obj = qdict_entry_value(entry);
966 char buf[32];
967 g_autofree char *tmp = NULL;
968 const char *value;
970 if (!strcmp(key, "id")) {
971 return true;
974 switch (qobject_type(obj)) {
975 case QTYPE_QSTRING:
976 value = qstring_get_str(qobject_to(QString, obj));
977 break;
978 case QTYPE_QNUM:
979 tmp = qnum_to_string(qobject_to(QNum, obj));
980 value = tmp;
981 break;
982 case QTYPE_QBOOL:
983 pstrcpy(buf, sizeof(buf),
984 qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
985 value = buf;
986 break;
987 default:
988 return true;
991 return qemu_opt_set(opts, key, value, errp);
995 * Create QemuOpts from a QDict.
996 * Use value of key "id" as ID if it exists and is a QString. Only
997 * QStrings, QNums and QBools are copied. Entries with other types
998 * are silently ignored.
1000 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1001 Error **errp)
1003 QemuOpts *opts;
1004 const QDictEntry *entry;
1006 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
1007 if (!opts) {
1008 return NULL;
1011 assert(opts != NULL);
1013 for (entry = qdict_first(qdict);
1014 entry;
1015 entry = qdict_next(qdict, entry)) {
1016 if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
1017 qemu_opts_del(opts);
1018 return NULL;
1022 return opts;
1026 * Adds all QDict entries to the QemuOpts that can be added and removes them
1027 * from the QDict. When this function returns, the QDict contains only those
1028 * entries that couldn't be added to the QemuOpts.
1030 bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1032 const QDictEntry *entry, *next;
1034 entry = qdict_first(qdict);
1036 while (entry != NULL) {
1037 next = qdict_next(qdict, entry);
1039 if (find_desc_by_name(opts->list->desc, entry->key)) {
1040 if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
1041 return false;
1043 qdict_del(qdict, entry->key);
1046 entry = next;
1049 return true;
1053 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1055 * If @list is given, only add those options to the QDict that are contained in
1056 * the list. If @del is true, any options added to the QDict are removed from
1057 * the QemuOpts, otherwise they remain there.
1059 * If two options in @opts have the same name, they are processed in order
1060 * so that the last one wins (consistent with the reverse iteration in
1061 * qemu_opt_find()), but all of them are deleted if @del is true.
1063 * TODO We'll want to use types appropriate for opt->desc->type, but
1064 * this is enough for now.
1066 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1067 QemuOptsList *list, bool del)
1069 QemuOpt *opt, *next;
1071 if (!qdict) {
1072 qdict = qdict_new();
1074 if (opts->id) {
1075 qdict_put_str(qdict, "id", opts->id);
1077 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1078 if (list) {
1079 QemuOptDesc *desc;
1080 bool found = false;
1081 for (desc = list->desc; desc->name; desc++) {
1082 if (!strcmp(desc->name, opt->name)) {
1083 found = true;
1084 break;
1087 if (!found) {
1088 continue;
1091 qdict_put_str(qdict, opt->name, opt->str);
1092 if (del) {
1093 qemu_opt_del(opt);
1096 return qdict;
1099 /* Copy all options in a QemuOpts to the given QDict. See
1100 * qemu_opts_to_qdict_filtered() for details. */
1101 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1103 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1106 /* Validate parsed opts against descriptions where no
1107 * descriptions were provided in the QemuOptsList.
1109 bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1111 QemuOpt *opt;
1113 assert(opts_accepts_any(opts->list));
1115 QTAILQ_FOREACH(opt, &opts->head, next) {
1116 opt->desc = find_desc_by_name(desc, opt->name);
1117 if (!opt->desc) {
1118 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1119 return false;
1122 if (!qemu_opt_parse(opt, errp)) {
1123 return false;
1127 return true;
1131 * For each member of @list, call @func(@opaque, member, @errp).
1132 * Call it with the current location temporarily set to the member's.
1133 * @func() may store an Error through @errp, but must return non-zero then.
1134 * When @func() returns non-zero, break the loop and return that value.
1135 * Return zero when the loop completes.
1137 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1138 void *opaque, Error **errp)
1140 Location loc;
1141 QemuOpts *opts;
1142 int rc = 0;
1144 loc_push_none(&loc);
1145 QTAILQ_FOREACH(opts, &list->head, next) {
1146 loc_restore(&opts->loc);
1147 rc = func(opaque, opts, errp);
1148 if (rc) {
1149 break;
1151 assert(!errp || !*errp);
1153 loc_pop(&loc);
1154 return rc;
1157 static size_t count_opts_list(QemuOptsList *list)
1159 QemuOptDesc *desc = NULL;
1160 size_t num_opts = 0;
1162 if (!list) {
1163 return 0;
1166 desc = list->desc;
1167 while (desc && desc->name) {
1168 num_opts++;
1169 desc++;
1172 return num_opts;
1175 void qemu_opts_free(QemuOptsList *list)
1177 g_free(list);
1180 /* Realloc dst option list and append options from an option list (list)
1181 * to it. dst could be NULL or a malloced list.
1182 * The lifetime of dst must be shorter than the input list because the
1183 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1185 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1186 QemuOptsList *list)
1188 size_t num_opts, num_dst_opts;
1189 QemuOptDesc *desc;
1190 bool need_init = false;
1191 bool need_head_update;
1193 if (!list) {
1194 return dst;
1197 /* If dst is NULL, after realloc, some area of dst should be initialized
1198 * before adding options to it.
1200 if (!dst) {
1201 need_init = true;
1202 need_head_update = true;
1203 } else {
1204 /* Moreover, even if dst is not NULL, the realloc may move it to a
1205 * different address in which case we may get a stale tail pointer
1206 * in dst->head. */
1207 need_head_update = QTAILQ_EMPTY(&dst->head);
1210 num_opts = count_opts_list(dst);
1211 num_dst_opts = num_opts;
1212 num_opts += count_opts_list(list);
1213 dst = g_realloc(dst, sizeof(QemuOptsList) +
1214 (num_opts + 1) * sizeof(QemuOptDesc));
1215 if (need_init) {
1216 dst->name = NULL;
1217 dst->implied_opt_name = NULL;
1218 dst->merge_lists = false;
1220 if (need_head_update) {
1221 QTAILQ_INIT(&dst->head);
1223 dst->desc[num_dst_opts].name = NULL;
1225 /* append list->desc to dst->desc */
1226 if (list) {
1227 desc = list->desc;
1228 while (desc && desc->name) {
1229 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1230 dst->desc[num_dst_opts++] = *desc;
1231 dst->desc[num_dst_opts].name = NULL;
1233 desc++;
1237 return dst;