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
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"
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 delimited by delim (usually , or =) or the string end
45 * and is copied into option. The caller is responsible for free'ing option
46 * when no longer required.
48 * The return value is the position of the delimiter/zero byte after the option
51 static const char *get_opt_name(const char *p
, char **option
, char delim
)
53 char *offset
= strchr(p
, delim
);
56 *option
= g_strndup(p
, offset
- p
);
59 *option
= g_strdup(p
);
65 * Extracts the value of an option from the parameter string p (p points at the
66 * first byte of the option value)
68 * This function is comparable to get_opt_name with the difference that the
69 * delimiter is fixed to be comma which starts a new option. To specify an
70 * option value that contains commas, double each comma.
72 const char *get_opt_value(const char *p
, char **value
)
74 size_t capacity
= 0, length
;
79 offset
= qemu_strchrnul(p
, ',');
81 if (*offset
!= '\0' && *(offset
+ 1) == ',') {
84 *value
= g_renew(char, *value
, capacity
+ length
+ 1);
85 strncpy(*value
+ capacity
, p
, length
);
86 (*value
)[capacity
+ length
] = '\0';
88 if (*offset
== '\0' ||
89 *(offset
+ 1) != ',') {
93 p
+= (offset
- p
) + 2;
99 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
102 if (!strcmp(value
, "on")) {
104 } else if (!strcmp(value
, "off")) {
107 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
108 name
, "'on' or 'off'");
112 static void parse_option_number(const char *name
, const char *value
,
113 uint64_t *ret
, Error
**errp
)
118 err
= qemu_strtou64(value
, NULL
, 0, &number
);
119 if (err
== -ERANGE
) {
120 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
125 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
131 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
136 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
137 if (strcmp(desc
[i
].name
, name
) == 0) {
145 static const char *find_default_by_name(QemuOpts
*opts
, const char *name
)
147 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
149 return desc
? desc
->def_value_str
: NULL
;
152 void parse_option_size(const char *name
, const char *value
,
153 uint64_t *ret
, Error
**errp
)
158 err
= qemu_strtosz(value
, NULL
, &size
);
159 if (err
== -ERANGE
) {
160 error_setg(errp
, "Value '%s' is out of range for parameter '%s'",
165 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
166 "a non-negative number below 2^64");
167 error_append_hint(errp
, "Optional suffix k, M, G, T, P or E means"
168 " kilo-, mega-, giga-, tera-, peta-\n"
169 "and exabytes, respectively.\n");
175 static const char *opt_type_to_string(enum QemuOptType type
)
178 case QEMU_OPT_STRING
:
181 return "bool (on/off)";
182 case QEMU_OPT_NUMBER
:
188 g_assert_not_reached();
192 * Print the list of options available in the given list. If
193 * @print_caption is true, a caption (including the list name, if it
194 * exists) is printed. The options itself will be indented, so
195 * @print_caption should only be set to false if the caller prints its
196 * own custom caption (so that the indentation makes sense).
198 void qemu_opts_print_help(QemuOptsList
*list
, bool print_caption
)
202 GPtrArray
*array
= g_ptr_array_new();
206 while (desc
&& desc
->name
) {
207 GString
*str
= g_string_new(NULL
);
208 g_string_append_printf(str
, " %s=<%s>", desc
->name
,
209 opt_type_to_string(desc
->type
));
212 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
214 g_string_append_printf(str
, " - %s", desc
->help
);
216 g_ptr_array_add(array
, g_string_free(str
, false));
220 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
221 if (print_caption
&& array
->len
> 0) {
223 printf("%s options:\n", list
->name
);
225 printf("Options:\n");
227 } else if (array
->len
== 0) {
229 printf("There are no options for %s.\n", list
->name
);
231 printf("No options available.\n");
234 for (i
= 0; i
< array
->len
; i
++) {
235 printf("%s\n", (char *)array
->pdata
[i
]);
237 g_ptr_array_set_free_func(array
, g_free
);
238 g_ptr_array_free(array
, true);
241 /* ------------------------------------------------------------------ */
243 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
247 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
248 if (strcmp(opt
->name
, name
) != 0)
255 static void qemu_opt_del(QemuOpt
*opt
)
257 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
263 /* qemu_opt_set allows many settings for the same option.
264 * This function deletes all settings for an option.
266 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
268 QemuOpt
*opt
, *next_opt
;
270 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
271 if (!strcmp(opt
->name
, name
)) {
277 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
285 opt
= qemu_opt_find(opts
, name
);
287 return find_default_by_name(opts
, name
);
293 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
296 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
300 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
302 QemuOpt
*ret
= iter
->opt
;
304 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
305 ret
= QTAILQ_NEXT(ret
, next
);
308 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
309 return ret
? ret
->str
: NULL
;
312 /* Get a known option (or its default) and remove it from the list
313 * all in one action. Return a malloced string of the option value.
314 * Result must be freed by caller with g_free().
316 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
325 opt
= qemu_opt_find(opts
, name
);
327 return g_strdup(find_default_by_name(opts
, name
));
331 qemu_opt_del_all(opts
, name
);
335 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
339 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
340 if (is_help_option(opt
->name
)) {
347 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
348 bool defval
, bool del
)
358 opt
= qemu_opt_find(opts
, name
);
360 def_val
= find_default_by_name(opts
, name
);
362 parse_option_bool(name
, def_val
, &ret
, &error_abort
);
366 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
367 ret
= opt
->value
.boolean
;
369 qemu_opt_del_all(opts
, name
);
374 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
376 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
379 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
381 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
384 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
385 uint64_t defval
, bool del
)
389 uint64_t ret
= defval
;
395 opt
= qemu_opt_find(opts
, name
);
397 def_val
= find_default_by_name(opts
, name
);
399 parse_option_number(name
, def_val
, &ret
, &error_abort
);
403 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
404 ret
= opt
->value
.uint
;
406 qemu_opt_del_all(opts
, name
);
411 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
413 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
416 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
419 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
422 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
423 uint64_t defval
, bool del
)
427 uint64_t ret
= defval
;
433 opt
= qemu_opt_find(opts
, name
);
435 def_val
= find_default_by_name(opts
, name
);
437 parse_option_size(name
, def_val
, &ret
, &error_abort
);
441 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
442 ret
= opt
->value
.uint
;
444 qemu_opt_del_all(opts
, name
);
449 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
451 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
454 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
457 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
460 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
462 if (opt
->desc
== NULL
)
465 switch (opt
->desc
->type
) {
466 case QEMU_OPT_STRING
:
470 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
472 case QEMU_OPT_NUMBER
:
473 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
476 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
483 static bool opts_accepts_any(const QemuOpts
*opts
)
485 return opts
->list
->desc
[0].name
== NULL
;
488 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
490 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
492 assert(opts_accepts_any(opts
));
502 static QemuOpt
*opt_create(QemuOpts
*opts
, const char *name
, char *value
,
505 QemuOpt
*opt
= g_malloc0(sizeof(*opt
));
507 opt
->name
= g_strdup(name
);
511 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
513 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
519 static void opt_set(QemuOpts
*opts
, const char *name
, char *value
,
520 bool prepend
, bool *help_wanted
, Error
**errp
)
523 const QemuOptDesc
*desc
;
524 Error
*local_err
= NULL
;
526 desc
= find_desc_by_name(opts
->list
->desc
, name
);
527 if (!desc
&& !opts_accepts_any(opts
)) {
529 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
530 if (help_wanted
&& is_help_option(name
)) {
536 opt
= opt_create(opts
, name
, value
, prepend
);
538 qemu_opt_parse(opt
, &local_err
);
540 error_propagate(errp
, local_err
);
545 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
548 opt_set(opts
, name
, g_strdup(value
), false, NULL
, errp
);
551 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
555 const QemuOptDesc
*desc
;
557 desc
= find_desc_by_name(opts
->list
->desc
, name
);
558 if (!desc
&& !opts_accepts_any(opts
)) {
559 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
563 opt
= g_malloc0(sizeof(*opt
));
564 opt
->name
= g_strdup(name
);
567 opt
->value
.boolean
= !!val
;
568 opt
->str
= g_strdup(val
? "on" : "off");
569 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
572 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
576 const QemuOptDesc
*desc
;
578 desc
= find_desc_by_name(opts
->list
->desc
, name
);
579 if (!desc
&& !opts_accepts_any(opts
)) {
580 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
584 opt
= g_malloc0(sizeof(*opt
));
585 opt
->name
= g_strdup(name
);
588 opt
->value
.uint
= val
;
589 opt
->str
= g_strdup_printf("%" PRId64
, val
);
590 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
594 * For each member of @opts, call @func(@opaque, name, value, @errp).
595 * @func() may store an Error through @errp, but must return non-zero then.
596 * When @func() returns non-zero, break the loop and return that value.
597 * Return zero when the loop completes.
599 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
605 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
606 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
610 assert(!errp
|| !*errp
);
615 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
619 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
620 if (!opts
->id
&& !id
) {
623 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
630 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
631 int fail_if_exists
, Error
**errp
)
633 QemuOpts
*opts
= NULL
;
636 if (!id_wellformed(id
)) {
637 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
639 error_append_hint(errp
, "Identifiers consist of letters, digits, "
640 "'-', '.', '_', starting with a letter.\n");
643 opts
= qemu_opts_find(list
, id
);
645 if (fail_if_exists
&& !list
->merge_lists
) {
646 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
652 } else if (list
->merge_lists
) {
653 opts
= qemu_opts_find(list
, NULL
);
658 opts
= g_malloc0(sizeof(*opts
));
659 opts
->id
= g_strdup(id
);
661 loc_save(&opts
->loc
);
662 QTAILQ_INIT(&opts
->head
);
663 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
667 void qemu_opts_reset(QemuOptsList
*list
)
669 QemuOpts
*opts
, *next_opts
;
671 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
676 void qemu_opts_loc_restore(QemuOpts
*opts
)
678 loc_restore(&opts
->loc
);
681 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
682 const char *name
, const char *value
, Error
**errp
)
686 opts
= qemu_opts_create(list
, id
, 1, errp
);
690 qemu_opt_set(opts
, name
, value
, errp
);
693 const char *qemu_opts_id(QemuOpts
*opts
)
698 /* The id string will be g_free()d by qemu_opts_del */
699 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
704 void qemu_opts_del(QemuOpts
*opts
)
713 opt
= QTAILQ_FIRST(&opts
->head
);
718 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
723 /* print value, escaping any commas in value */
724 static void escaped_print(const char *value
)
728 for (ptr
= value
; *ptr
; ++ptr
) {
736 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
739 QemuOptDesc
*desc
= opts
->list
->desc
;
740 const char *sep
= "";
743 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
747 if (desc
[0].name
== NULL
) {
748 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
749 printf("%s%s=", sep
, opt
->name
);
750 escaped_print(opt
->str
);
755 for (; desc
&& desc
->name
; desc
++) {
757 opt
= qemu_opt_find(opts
, desc
->name
);
759 value
= opt
? opt
->str
: desc
->def_value_str
;
763 if (desc
->type
== QEMU_OPT_STRING
) {
764 printf("%s%s=", sep
, desc
->name
);
765 escaped_print(value
);
766 } else if ((desc
->type
== QEMU_OPT_SIZE
||
767 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
768 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
770 printf("%s%s=%s", sep
, desc
->name
, value
);
776 static const char *get_opt_name_value(const char *params
,
777 const char *firstname
,
778 char **name
, char **value
)
780 const char *p
, *pe
, *pc
;
782 pe
= strchr(params
, '=');
783 pc
= strchr(params
, ',');
785 if (!pe
|| (pc
&& pc
< pe
)) {
786 /* found "foo,more" */
788 /* implicitly named first option */
789 *name
= g_strdup(firstname
);
790 p
= get_opt_value(params
, value
);
792 /* option without value, must be a flag */
793 p
= get_opt_name(params
, name
, ',');
794 if (strncmp(*name
, "no", 2) == 0) {
795 memmove(*name
, *name
+ 2, strlen(*name
+ 2) + 1);
796 *value
= g_strdup("off");
798 *value
= g_strdup("on");
802 /* found "foo=bar,more" */
803 p
= get_opt_name(params
, name
, '=');
806 p
= get_opt_value(p
, value
);
809 assert(!*p
|| *p
== ',');
816 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
817 const char *firstname
, bool prepend
,
818 bool *help_wanted
, Error
**errp
)
820 Error
*local_err
= NULL
;
821 char *option
, *value
;
824 for (p
= params
; *p
;) {
825 p
= get_opt_name_value(p
, firstname
, &option
, &value
);
828 if (!strcmp(option
, "id")) {
834 opt_set(opts
, option
, value
, prepend
, help_wanted
, &local_err
);
837 error_propagate(errp
, local_err
);
843 static char *opts_parse_id(const char *params
)
848 for (p
= params
; *p
;) {
849 p
= get_opt_name_value(p
, NULL
, &name
, &value
);
850 if (!strcmp(name
, "id")) {
861 bool has_help_option(const char *params
)
867 for (p
= params
; *p
;) {
868 p
= get_opt_name_value(p
, NULL
, &name
, &value
);
869 ret
= is_help_option(name
);
881 * Store options parsed from @params into @opts.
882 * If @firstname is non-null, the first key=value in @params may omit
883 * key=, and is treated as if key was @firstname.
884 * On error, store an error object through @errp if non-null.
886 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
887 const char *firstname
, Error
**errp
)
889 opts_do_parse(opts
, params
, firstname
, false, NULL
, errp
);
892 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
893 bool permit_abbrev
, bool defaults
,
894 bool *help_wanted
, Error
**errp
)
896 const char *firstname
;
897 char *id
= opts_parse_id(params
);
899 Error
*local_err
= NULL
;
901 assert(!permit_abbrev
|| list
->implied_opt_name
);
902 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
905 * This code doesn't work for defaults && !list->merge_lists: when
906 * params has no id=, and list has an element with !opts->id, it
907 * appends a new element instead of returning the existing opts.
908 * However, we got no use for this case. Guard against possible
909 * (if unlikely) future misuse:
911 assert(!defaults
|| list
->merge_lists
);
912 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
915 error_propagate(errp
, local_err
);
919 opts_do_parse(opts
, params
, firstname
, defaults
, help_wanted
, &local_err
);
921 error_propagate(errp
, local_err
);
930 * Create a QemuOpts in @list and with options parsed from @params.
931 * If @permit_abbrev, the first key=value in @params may omit key=,
932 * and is treated as if key was @list->implied_opt_name.
933 * On error, store an error object through @errp if non-null.
934 * Return the new QemuOpts on success, null pointer on error.
936 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
937 bool permit_abbrev
, Error
**errp
)
939 return opts_parse(list
, params
, permit_abbrev
, false, NULL
, errp
);
943 * Create a QemuOpts in @list and with options parsed from @params.
944 * If @permit_abbrev, the first key=value in @params may omit key=,
945 * and is treated as if key was @list->implied_opt_name.
946 * Report errors with error_report_err(). This is inappropriate in
947 * QMP context. Do not use this function there!
948 * Return the new QemuOpts on success, null pointer on error.
950 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
955 bool help_wanted
= false;
957 opts
= opts_parse(list
, params
, permit_abbrev
, false, &help_wanted
, &err
);
960 qemu_opts_print_help(list
, true);
963 error_report_err(err
);
969 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
974 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
, NULL
);
978 static void qemu_opts_from_qdict_entry(QemuOpts
*opts
,
979 const QDictEntry
*entry
,
982 const char *key
= qdict_entry_key(entry
);
983 QObject
*obj
= qdict_entry_value(entry
);
984 char buf
[32], *tmp
= NULL
;
987 if (!strcmp(key
, "id")) {
991 switch (qobject_type(obj
)) {
993 value
= qstring_get_str(qobject_to(QString
, obj
));
996 tmp
= qnum_to_string(qobject_to(QNum
, obj
));
1000 pstrcpy(buf
, sizeof(buf
),
1001 qbool_get_bool(qobject_to(QBool
, obj
)) ? "on" : "off");
1008 qemu_opt_set(opts
, key
, value
, errp
);
1013 * Create QemuOpts from a QDict.
1014 * Use value of key "id" as ID if it exists and is a QString. Only
1015 * QStrings, QNums and QBools are copied. Entries with other types
1016 * are silently ignored.
1018 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1021 Error
*local_err
= NULL
;
1023 const QDictEntry
*entry
;
1025 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1, errp
);
1030 assert(opts
!= NULL
);
1032 for (entry
= qdict_first(qdict
);
1034 entry
= qdict_next(qdict
, entry
)) {
1035 qemu_opts_from_qdict_entry(opts
, entry
, &local_err
);
1037 error_propagate(errp
, local_err
);
1038 qemu_opts_del(opts
);
1047 * Adds all QDict entries to the QemuOpts that can be added and removes them
1048 * from the QDict. When this function returns, the QDict contains only those
1049 * entries that couldn't be added to the QemuOpts.
1051 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1053 const QDictEntry
*entry
, *next
;
1055 entry
= qdict_first(qdict
);
1057 while (entry
!= NULL
) {
1058 Error
*local_err
= NULL
;
1060 next
= qdict_next(qdict
, entry
);
1062 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1063 qemu_opts_from_qdict_entry(opts
, entry
, &local_err
);
1065 error_propagate(errp
, local_err
);
1068 qdict_del(qdict
, entry
->key
);
1076 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1078 * If @list is given, only add those options to the QDict that are contained in
1079 * the list. If @del is true, any options added to the QDict are removed from
1080 * the QemuOpts, otherwise they remain there.
1082 * If two options in @opts have the same name, they are processed in order
1083 * so that the last one wins (consistent with the reverse iteration in
1084 * qemu_opt_find()), but all of them are deleted if @del is true.
1086 * TODO We'll want to use types appropriate for opt->desc->type, but
1087 * this is enough for now.
1089 QDict
*qemu_opts_to_qdict_filtered(QemuOpts
*opts
, QDict
*qdict
,
1090 QemuOptsList
*list
, bool del
)
1092 QemuOpt
*opt
, *next
;
1095 qdict
= qdict_new();
1098 qdict_put_str(qdict
, "id", opts
->id
);
1100 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next
) {
1104 for (desc
= list
->desc
; desc
->name
; desc
++) {
1105 if (!strcmp(desc
->name
, opt
->name
)) {
1114 qdict_put_str(qdict
, opt
->name
, opt
->str
);
1122 /* Copy all options in a QemuOpts to the given QDict. See
1123 * qemu_opts_to_qdict_filtered() for details. */
1124 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1126 return qemu_opts_to_qdict_filtered(opts
, qdict
, NULL
, false);
1129 /* Validate parsed opts against descriptions where no
1130 * descriptions were provided in the QemuOptsList.
1132 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1135 Error
*local_err
= NULL
;
1137 assert(opts_accepts_any(opts
));
1139 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1140 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1142 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1146 qemu_opt_parse(opt
, &local_err
);
1148 error_propagate(errp
, local_err
);
1155 * For each member of @list, call @func(@opaque, member, @errp).
1156 * Call it with the current location temporarily set to the member's.
1157 * @func() may store an Error through @errp, but must return non-zero then.
1158 * When @func() returns non-zero, break the loop and return that value.
1159 * Return zero when the loop completes.
1161 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1162 void *opaque
, Error
**errp
)
1168 loc_push_none(&loc
);
1169 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1170 loc_restore(&opts
->loc
);
1171 rc
= func(opaque
, opts
, errp
);
1175 assert(!errp
|| !*errp
);
1181 static size_t count_opts_list(QemuOptsList
*list
)
1183 QemuOptDesc
*desc
= NULL
;
1184 size_t num_opts
= 0;
1191 while (desc
&& desc
->name
) {
1199 void qemu_opts_free(QemuOptsList
*list
)
1204 /* Realloc dst option list and append options from an option list (list)
1205 * to it. dst could be NULL or a malloced list.
1206 * The lifetime of dst must be shorter than the input list because the
1207 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1209 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1212 size_t num_opts
, num_dst_opts
;
1214 bool need_init
= false;
1215 bool need_head_update
;
1221 /* If dst is NULL, after realloc, some area of dst should be initialized
1222 * before adding options to it.
1226 need_head_update
= true;
1228 /* Moreover, even if dst is not NULL, the realloc may move it to a
1229 * different address in which case we may get a stale tail pointer
1231 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1234 num_opts
= count_opts_list(dst
);
1235 num_dst_opts
= num_opts
;
1236 num_opts
+= count_opts_list(list
);
1237 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1238 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1241 dst
->implied_opt_name
= NULL
;
1242 dst
->merge_lists
= false;
1244 if (need_head_update
) {
1245 QTAILQ_INIT(&dst
->head
);
1247 dst
->desc
[num_dst_opts
].name
= NULL
;
1249 /* append list->desc to dst->desc */
1252 while (desc
&& desc
->name
) {
1253 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1254 dst
->desc
[num_dst_opts
++] = *desc
;
1255 dst
->desc
[num_dst_opts
].name
= NULL
;