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 @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
50 static const char *get_opt_name(const char *p
, char **option
, size_t len
)
52 *option
= g_strndup(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
;
71 offset
= qemu_strchrnul(p
, ',');
73 if (*offset
!= '\0' && *(offset
+ 1) == ',') {
76 *value
= g_renew(char, *value
, capacity
+ length
+ 1);
77 strncpy(*value
+ capacity
, p
, length
);
78 (*value
)[capacity
+ length
] = '\0';
80 if (*offset
== '\0' ||
81 *(offset
+ 1) != ',') {
85 p
+= (offset
- p
) + 2;
91 static bool parse_option_number(const char *name
, const char *value
,
92 uint64_t *ret
, Error
**errp
)
97 err
= qemu_strtou64(value
, NULL
, 0, &number
);
99 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
104 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
111 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
116 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
117 if (strcmp(desc
[i
].name
, name
) == 0) {
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
)
138 err
= qemu_strtosz(value
, NULL
, &size
);
139 if (err
== -ERANGE
) {
140 error_setg(errp
, "Value '%s' is out of range for parameter '%s'",
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");
156 static const char *opt_type_to_string(enum QemuOptType type
)
159 case QEMU_OPT_STRING
:
162 return "bool (on/off)";
163 case QEMU_OPT_NUMBER
:
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
)
183 GPtrArray
*array
= g_ptr_array_new();
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
));
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));
201 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
202 if (print_caption
&& array
->len
> 0) {
204 printf("%s options:\n", list
->name
);
206 printf("Options:\n");
208 } else if (array
->len
== 0) {
210 printf("There are no options for %s.\n", list
->name
);
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
)
228 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
229 if (strcmp(opt
->name
, name
) != 0)
236 static void qemu_opt_del(QemuOpt
*opt
)
238 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
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
)) {
258 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
266 opt
= qemu_opt_find(opts
, name
);
268 return find_default_by_name(opts
, name
);
274 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
277 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
281 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
283 QemuOpt
*ret
= iter
->opt
;
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
)
306 opt
= qemu_opt_find(opts
, name
);
308 return g_strdup(find_default_by_name(opts
, name
));
312 qemu_opt_del_all(opts
, name
);
316 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
320 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
321 if (is_help_option(opt
->name
)) {
328 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
329 bool defval
, bool del
)
339 opt
= qemu_opt_find(opts
, name
);
341 def_val
= find_default_by_name(opts
, name
);
343 qapi_bool_parse(name
, def_val
, &ret
, &error_abort
);
347 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
348 ret
= opt
->value
.boolean
;
350 qemu_opt_del_all(opts
, name
);
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
)
370 uint64_t ret
= defval
;
376 opt
= qemu_opt_find(opts
, name
);
378 def_val
= find_default_by_name(opts
, name
);
380 parse_option_number(name
, def_val
, &ret
, &error_abort
);
384 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
385 ret
= opt
->value
.uint
;
387 qemu_opt_del_all(opts
, name
);
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
,
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
)
408 uint64_t ret
= defval
;
414 opt
= qemu_opt_find(opts
, name
);
416 def_val
= find_default_by_name(opts
, name
);
418 parse_option_size(name
, def_val
, &ret
, &error_abort
);
422 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
423 ret
= opt
->value
.uint
;
425 qemu_opt_del_all(opts
, name
);
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
,
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
)
446 switch (opt
->desc
->type
) {
447 case QEMU_OPT_STRING
:
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
,
456 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
,
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
));
482 static QemuOpt
*opt_create(QemuOpts
*opts
, const char *name
, char *value
)
484 QemuOpt
*opt
= g_malloc0(sizeof(*opt
));
486 opt
->name
= g_strdup(name
);
489 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
494 static bool opt_validate(QemuOpt
*opt
, Error
**errp
)
496 const QemuOptDesc
*desc
;
497 const QemuOptsList
*list
= opt
->opts
->list
;
499 desc
= find_desc_by_name(list
->desc
, opt
->name
);
500 if (!desc
&& !opts_accepts_any(list
)) {
501 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
506 if (!qemu_opt_parse(opt
, errp
)) {
513 bool qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
516 QemuOpt
*opt
= opt_create(opts
, name
, g_strdup(value
));
518 if (!opt_validate(opt
, errp
)) {
525 bool qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
529 const QemuOptDesc
*desc
;
530 const QemuOptsList
*list
= opts
->list
;
532 desc
= find_desc_by_name(list
->desc
, name
);
533 if (!desc
&& !opts_accepts_any(list
)) {
534 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
538 opt
= g_malloc0(sizeof(*opt
));
539 opt
->name
= g_strdup(name
);
542 opt
->value
.boolean
= !!val
;
543 opt
->str
= g_strdup(val
? "on" : "off");
544 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
548 bool qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
552 const QemuOptDesc
*desc
;
553 const QemuOptsList
*list
= opts
->list
;
555 desc
= find_desc_by_name(list
->desc
, name
);
556 if (!desc
&& !opts_accepts_any(list
)) {
557 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
561 opt
= g_malloc0(sizeof(*opt
));
562 opt
->name
= g_strdup(name
);
565 opt
->value
.uint
= val
;
566 opt
->str
= g_strdup_printf("%" PRId64
, val
);
567 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
572 * For each member of @opts, call @func(@opaque, name, value, @errp).
573 * @func() may store an Error through @errp, but must return non-zero then.
574 * When @func() returns non-zero, break the loop and return that value.
575 * Return zero when the loop completes.
577 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
583 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
584 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
588 assert(!errp
|| !*errp
);
593 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
597 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
598 if (!opts
->id
&& !id
) {
601 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
608 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
609 int fail_if_exists
, Error
**errp
)
611 QemuOpts
*opts
= NULL
;
613 if (list
->merge_lists
) {
615 error_setg(errp
, QERR_INVALID_PARAMETER
, "id");
618 opts
= qemu_opts_find(list
, NULL
);
623 assert(fail_if_exists
);
624 if (!id_wellformed(id
)) {
625 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
627 error_append_hint(errp
, "Identifiers consist of letters, digits, "
628 "'-', '.', '_', starting with a letter.\n");
631 opts
= qemu_opts_find(list
, id
);
633 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
637 opts
= g_malloc0(sizeof(*opts
));
638 opts
->id
= g_strdup(id
);
640 loc_save(&opts
->loc
);
641 QTAILQ_INIT(&opts
->head
);
642 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
646 void qemu_opts_reset(QemuOptsList
*list
)
648 QemuOpts
*opts
, *next_opts
;
650 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
655 void qemu_opts_loc_restore(QemuOpts
*opts
)
657 loc_restore(&opts
->loc
);
660 const char *qemu_opts_id(QemuOpts
*opts
)
665 /* The id string will be g_free()d by qemu_opts_del */
666 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
671 void qemu_opts_del(QemuOpts
*opts
)
680 opt
= QTAILQ_FIRST(&opts
->head
);
685 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
690 /* print value, escaping any commas in value */
691 static void escaped_print(const char *value
)
695 for (ptr
= value
; *ptr
; ++ptr
) {
703 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
706 QemuOptDesc
*desc
= opts
->list
->desc
;
707 const char *sep
= "";
710 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
714 if (desc
[0].name
== NULL
) {
715 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
716 printf("%s%s=", sep
, opt
->name
);
717 escaped_print(opt
->str
);
722 for (; desc
&& desc
->name
; desc
++) {
724 opt
= qemu_opt_find(opts
, desc
->name
);
726 value
= opt
? opt
->str
: desc
->def_value_str
;
730 if (desc
->type
== QEMU_OPT_STRING
) {
731 printf("%s%s=", sep
, desc
->name
);
732 escaped_print(value
);
733 } else if ((desc
->type
== QEMU_OPT_SIZE
||
734 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
735 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
737 printf("%s%s=%s", sep
, desc
->name
, value
);
743 static const char *get_opt_name_value(const char *params
,
744 const char *firstname
,
747 char **name
, char **value
)
750 const char *prefix
= "";
752 bool is_help
= false;
754 len
= strcspn(params
, "=,");
755 if (params
[len
] != '=') {
756 /* found "foo,more" */
758 /* implicitly named first option */
759 *name
= g_strdup(firstname
);
760 p
= get_opt_value(params
, value
);
762 /* option without value, must be a flag */
763 p
= get_opt_name(params
, name
, len
);
764 if (strncmp(*name
, "no", 2) == 0) {
765 memmove(*name
, *name
+ 2, strlen(*name
+ 2) + 1);
766 *value
= g_strdup("off");
769 *value
= g_strdup("on");
770 is_help
= is_help_option(*name
);
772 if (!is_help
&& warn_on_flag
) {
773 warn_report("short-form boolean option '%s%s' deprecated", prefix
, *name
);
774 if (g_str_equal(*name
, "delay")) {
775 error_printf("Please use nodelay=%s instead\n", prefix
[0] ? "on" : "off");
777 error_printf("Please use %s=%s instead\n", *name
, *value
);
782 /* found "foo=bar,more" */
783 p
= get_opt_name(params
, name
, len
);
786 p
= get_opt_value(p
, value
);
789 assert(!*p
|| *p
== ',');
790 if (help_wanted
&& is_help
) {
799 static bool opts_do_parse(QemuOpts
*opts
, const char *params
,
800 const char *firstname
,
801 bool warn_on_flag
, bool *help_wanted
, Error
**errp
)
803 char *option
, *value
;
807 for (p
= params
; *p
;) {
808 p
= get_opt_name_value(p
, firstname
, warn_on_flag
, help_wanted
, &option
, &value
);
809 if (help_wanted
&& *help_wanted
) {
816 if (!strcmp(option
, "id")) {
822 opt
= opt_create(opts
, option
, value
);
824 if (!opt_validate(opt
, errp
)) {
833 static char *opts_parse_id(const char *params
)
838 for (p
= params
; *p
;) {
839 p
= get_opt_name_value(p
, NULL
, false, NULL
, &name
, &value
);
840 if (!strcmp(name
, "id")) {
851 bool has_help_option(const char *params
)
857 for (p
= params
; *p
;) {
858 p
= get_opt_name_value(p
, NULL
, false, &ret
, &name
, &value
);
870 * Store options parsed from @params into @opts.
871 * If @firstname is non-null, the first key=value in @params may omit
872 * key=, and is treated as if key was @firstname.
873 * On error, store an error object through @errp if non-null.
875 bool qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
876 const char *firstname
, Error
**errp
)
878 return opts_do_parse(opts
, params
, firstname
, false, NULL
, errp
);
881 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
883 bool warn_on_flag
, bool *help_wanted
, Error
**errp
)
885 const char *firstname
;
886 char *id
= opts_parse_id(params
);
889 assert(!permit_abbrev
|| list
->implied_opt_name
);
890 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
892 opts
= qemu_opts_create(list
, id
, !list
->merge_lists
, errp
);
898 if (!opts_do_parse(opts
, params
, firstname
,
899 warn_on_flag
, help_wanted
, errp
)) {
908 * Create a QemuOpts in @list and with options parsed from @params.
909 * If @permit_abbrev, the first key=value in @params may omit key=,
910 * and is treated as if key was @list->implied_opt_name.
911 * On error, store an error object through @errp if non-null.
912 * Return the new QemuOpts on success, null pointer on error.
914 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
915 bool permit_abbrev
, Error
**errp
)
917 return opts_parse(list
, params
, permit_abbrev
, false, NULL
, errp
);
921 * Create a QemuOpts in @list and with options parsed from @params.
922 * If @permit_abbrev, the first key=value in @params may omit key=,
923 * and is treated as if key was @list->implied_opt_name.
924 * Report errors with error_report_err(). This is inappropriate in
925 * QMP context. Do not use this function there!
926 * Return the new QemuOpts on success, null pointer on error.
928 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
933 bool help_wanted
= false;
935 opts
= opts_parse(list
, params
, permit_abbrev
, true,
936 opts_accepts_any(list
) ? NULL
: &help_wanted
,
939 assert(!!err
+ !!help_wanted
== 1);
941 qemu_opts_print_help(list
, true);
943 error_report_err(err
);
949 static bool qemu_opts_from_qdict_entry(QemuOpts
*opts
,
950 const QDictEntry
*entry
,
953 const char *key
= qdict_entry_key(entry
);
954 QObject
*obj
= qdict_entry_value(entry
);
956 g_autofree
char *tmp
= NULL
;
959 if (!strcmp(key
, "id")) {
963 switch (qobject_type(obj
)) {
965 value
= qstring_get_str(qobject_to(QString
, obj
));
968 tmp
= qnum_to_string(qobject_to(QNum
, obj
));
972 pstrcpy(buf
, sizeof(buf
),
973 qbool_get_bool(qobject_to(QBool
, obj
)) ? "on" : "off");
980 return qemu_opt_set(opts
, key
, value
, errp
);
984 * Create QemuOpts from a QDict.
985 * Use value of key "id" as ID if it exists and is a QString. Only
986 * QStrings, QNums and QBools are copied. Entries with other types
987 * are silently ignored.
989 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
993 const QDictEntry
*entry
;
995 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1, errp
);
1000 for (entry
= qdict_first(qdict
);
1002 entry
= qdict_next(qdict
, entry
)) {
1003 if (!qemu_opts_from_qdict_entry(opts
, entry
, errp
)) {
1004 qemu_opts_del(opts
);
1013 * Adds all QDict entries to the QemuOpts that can be added and removes them
1014 * from the QDict. When this function returns, the QDict contains only those
1015 * entries that couldn't be added to the QemuOpts.
1017 bool qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1019 const QDictEntry
*entry
, *next
;
1021 entry
= qdict_first(qdict
);
1023 while (entry
!= NULL
) {
1024 next
= qdict_next(qdict
, entry
);
1026 if (opts_accepts_any(opts
->list
) ||
1027 find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1028 if (!qemu_opts_from_qdict_entry(opts
, entry
, errp
)) {
1031 qdict_del(qdict
, entry
->key
);
1041 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1043 * If @list is given, only add those options to the QDict that are contained in
1044 * the list. If @del is true, any options added to the QDict are removed from
1045 * the QemuOpts, otherwise they remain there.
1047 * If two options in @opts have the same name, they are processed in order
1048 * so that the last one wins (consistent with the reverse iteration in
1049 * qemu_opt_find()), but all of them are deleted if @del is true.
1051 * TODO We'll want to use types appropriate for opt->desc->type, but
1052 * this is enough for now.
1054 QDict
*qemu_opts_to_qdict_filtered(QemuOpts
*opts
, QDict
*qdict
,
1055 QemuOptsList
*list
, bool del
)
1057 QemuOpt
*opt
, *next
;
1060 qdict
= qdict_new();
1063 qdict_put_str(qdict
, "id", opts
->id
);
1065 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next
) {
1069 for (desc
= list
->desc
; desc
->name
; desc
++) {
1070 if (!strcmp(desc
->name
, opt
->name
)) {
1079 qdict_put_str(qdict
, opt
->name
, opt
->str
);
1087 /* Copy all options in a QemuOpts to the given QDict. See
1088 * qemu_opts_to_qdict_filtered() for details. */
1089 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1091 return qemu_opts_to_qdict_filtered(opts
, qdict
, NULL
, false);
1094 /* Validate parsed opts against descriptions where no
1095 * descriptions were provided in the QemuOptsList.
1097 bool qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1101 assert(opts_accepts_any(opts
->list
));
1103 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1104 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1106 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1110 if (!qemu_opt_parse(opt
, errp
)) {
1119 * For each member of @list, call @func(@opaque, member, @errp).
1120 * Call it with the current location temporarily set to the member's.
1121 * @func() may store an Error through @errp, but must return non-zero then.
1122 * When @func() returns non-zero, break the loop and return that value.
1123 * Return zero when the loop completes.
1125 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1126 void *opaque
, Error
**errp
)
1129 QemuOpts
*opts
, *next
;
1132 loc_push_none(&loc
);
1133 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next
) {
1134 loc_restore(&opts
->loc
);
1135 rc
= func(opaque
, opts
, errp
);
1139 assert(!errp
|| !*errp
);
1145 static size_t count_opts_list(QemuOptsList
*list
)
1147 QemuOptDesc
*desc
= NULL
;
1148 size_t num_opts
= 0;
1155 while (desc
&& desc
->name
) {
1163 void qemu_opts_free(QemuOptsList
*list
)
1168 /* Realloc dst option list and append options from an option list (list)
1169 * to it. dst could be NULL or a malloced list.
1170 * The lifetime of dst must be shorter than the input list because the
1171 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1173 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1176 size_t num_opts
, num_dst_opts
;
1178 bool need_init
= false;
1179 bool need_head_update
;
1185 /* If dst is NULL, after realloc, some area of dst should be initialized
1186 * before adding options to it.
1190 need_head_update
= true;
1192 /* Moreover, even if dst is not NULL, the realloc may move it to a
1193 * different address in which case we may get a stale tail pointer
1195 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1198 num_opts
= count_opts_list(dst
);
1199 num_dst_opts
= num_opts
;
1200 num_opts
+= count_opts_list(list
);
1201 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1202 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1205 dst
->implied_opt_name
= NULL
;
1206 dst
->merge_lists
= false;
1208 if (need_head_update
) {
1209 QTAILQ_INIT(&dst
->head
);
1211 dst
->desc
[num_dst_opts
].name
= NULL
;
1213 /* append list->desc to dst->desc */
1216 while (desc
&& desc
->name
) {
1217 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1218 dst
->desc
[num_dst_opts
++] = *desc
;
1219 dst
->desc
[num_dst_opts
].name
= NULL
;