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 bool parse_option_number(const char *name
, const char *value
,
100 uint64_t *ret
, Error
**errp
)
105 err
= qemu_strtou64(value
, NULL
, 0, &number
);
106 if (err
== -ERANGE
) {
107 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
112 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
119 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
124 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
125 if (strcmp(desc
[i
].name
, name
) == 0) {
133 static const char *find_default_by_name(QemuOpts
*opts
, const char *name
)
135 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
137 return desc
? desc
->def_value_str
: NULL
;
140 bool parse_option_size(const char *name
, const char *value
,
141 uint64_t *ret
, Error
**errp
)
146 err
= qemu_strtosz(value
, NULL
, &size
);
147 if (err
== -ERANGE
) {
148 error_setg(errp
, "Value '%s' is out of range for parameter '%s'",
153 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
154 "a non-negative number below 2^64");
155 error_append_hint(errp
, "Optional suffix k, M, G, T, P or E means"
156 " kilo-, mega-, giga-, tera-, peta-\n"
157 "and exabytes, respectively.\n");
164 static const char *opt_type_to_string(enum QemuOptType type
)
167 case QEMU_OPT_STRING
:
170 return "bool (on/off)";
171 case QEMU_OPT_NUMBER
:
177 g_assert_not_reached();
181 * Print the list of options available in the given list. If
182 * @print_caption is true, a caption (including the list name, if it
183 * exists) is printed. The options itself will be indented, so
184 * @print_caption should only be set to false if the caller prints its
185 * own custom caption (so that the indentation makes sense).
187 void qemu_opts_print_help(QemuOptsList
*list
, bool print_caption
)
191 GPtrArray
*array
= g_ptr_array_new();
195 while (desc
&& desc
->name
) {
196 GString
*str
= g_string_new(NULL
);
197 g_string_append_printf(str
, " %s=<%s>", desc
->name
,
198 opt_type_to_string(desc
->type
));
201 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
203 g_string_append_printf(str
, " - %s", desc
->help
);
205 g_ptr_array_add(array
, g_string_free(str
, false));
209 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
210 if (print_caption
&& array
->len
> 0) {
212 printf("%s options:\n", list
->name
);
214 printf("Options:\n");
216 } else if (array
->len
== 0) {
218 printf("There are no options for %s.\n", list
->name
);
220 printf("No options available.\n");
223 for (i
= 0; i
< array
->len
; i
++) {
224 printf("%s\n", (char *)array
->pdata
[i
]);
226 g_ptr_array_set_free_func(array
, g_free
);
227 g_ptr_array_free(array
, true);
230 /* ------------------------------------------------------------------ */
232 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
236 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
237 if (strcmp(opt
->name
, name
) != 0)
244 static void qemu_opt_del(QemuOpt
*opt
)
246 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
252 /* qemu_opt_set allows many settings for the same option.
253 * This function deletes all settings for an option.
255 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
257 QemuOpt
*opt
, *next_opt
;
259 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
260 if (!strcmp(opt
->name
, name
)) {
266 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
274 opt
= qemu_opt_find(opts
, name
);
276 return find_default_by_name(opts
, name
);
282 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
285 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
289 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
291 QemuOpt
*ret
= iter
->opt
;
293 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
294 ret
= QTAILQ_NEXT(ret
, next
);
297 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
298 return ret
? ret
->str
: NULL
;
301 /* Get a known option (or its default) and remove it from the list
302 * all in one action. Return a malloced string of the option value.
303 * Result must be freed by caller with g_free().
305 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
314 opt
= qemu_opt_find(opts
, name
);
316 return g_strdup(find_default_by_name(opts
, name
));
320 qemu_opt_del_all(opts
, name
);
324 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
328 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
329 if (is_help_option(opt
->name
)) {
336 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
337 bool defval
, bool del
)
347 opt
= qemu_opt_find(opts
, name
);
349 def_val
= find_default_by_name(opts
, name
);
351 qapi_bool_parse(name
, def_val
, &ret
, &error_abort
);
355 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
356 ret
= opt
->value
.boolean
;
358 qemu_opt_del_all(opts
, name
);
363 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
365 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
368 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
370 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
373 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
374 uint64_t defval
, bool del
)
378 uint64_t ret
= defval
;
384 opt
= qemu_opt_find(opts
, name
);
386 def_val
= find_default_by_name(opts
, name
);
388 parse_option_number(name
, def_val
, &ret
, &error_abort
);
392 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
393 ret
= opt
->value
.uint
;
395 qemu_opt_del_all(opts
, name
);
400 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
402 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
405 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
408 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
411 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
412 uint64_t defval
, bool del
)
416 uint64_t ret
= defval
;
422 opt
= qemu_opt_find(opts
, name
);
424 def_val
= find_default_by_name(opts
, name
);
426 parse_option_size(name
, def_val
, &ret
, &error_abort
);
430 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
431 ret
= opt
->value
.uint
;
433 qemu_opt_del_all(opts
, name
);
438 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
440 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
443 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
446 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
449 static bool qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
451 if (opt
->desc
== NULL
)
454 switch (opt
->desc
->type
) {
455 case QEMU_OPT_STRING
:
459 return qapi_bool_parse(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
460 case QEMU_OPT_NUMBER
:
461 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
,
464 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
,
471 static bool opts_accepts_any(const QemuOpts
*opts
)
473 return opts
->list
->desc
[0].name
== NULL
;
476 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
478 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
480 assert(opts_accepts_any(opts
));
490 static QemuOpt
*opt_create(QemuOpts
*opts
, const char *name
, char *value
,
493 QemuOpt
*opt
= g_malloc0(sizeof(*opt
));
495 opt
->name
= g_strdup(name
);
499 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
501 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
507 static bool opt_validate(QemuOpt
*opt
, bool *help_wanted
,
510 const QemuOptDesc
*desc
;
512 desc
= find_desc_by_name(opt
->opts
->list
->desc
, opt
->name
);
513 if (!desc
&& !opts_accepts_any(opt
->opts
)) {
514 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
515 if (help_wanted
&& is_help_option(opt
->name
)) {
522 if (!qemu_opt_parse(opt
, errp
)) {
529 bool qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
532 QemuOpt
*opt
= opt_create(opts
, name
, g_strdup(value
), false);
534 if (!opt_validate(opt
, NULL
, errp
)) {
541 bool qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
545 const QemuOptDesc
*desc
;
547 desc
= find_desc_by_name(opts
->list
->desc
, name
);
548 if (!desc
&& !opts_accepts_any(opts
)) {
549 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
553 opt
= g_malloc0(sizeof(*opt
));
554 opt
->name
= g_strdup(name
);
557 opt
->value
.boolean
= !!val
;
558 opt
->str
= g_strdup(val
? "on" : "off");
559 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
563 bool qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
567 const QemuOptDesc
*desc
;
569 desc
= find_desc_by_name(opts
->list
->desc
, name
);
570 if (!desc
&& !opts_accepts_any(opts
)) {
571 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
575 opt
= g_malloc0(sizeof(*opt
));
576 opt
->name
= g_strdup(name
);
579 opt
->value
.uint
= val
;
580 opt
->str
= g_strdup_printf("%" PRId64
, val
);
581 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
586 * For each member of @opts, call @func(@opaque, name, value, @errp).
587 * @func() may store an Error through @errp, but must return non-zero then.
588 * When @func() returns non-zero, break the loop and return that value.
589 * Return zero when the loop completes.
591 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
597 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
598 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
602 assert(!errp
|| !*errp
);
607 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
611 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
612 if (!opts
->id
&& !id
) {
615 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
622 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
623 int fail_if_exists
, Error
**errp
)
625 QemuOpts
*opts
= NULL
;
628 if (!id_wellformed(id
)) {
629 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
631 error_append_hint(errp
, "Identifiers consist of letters, digits, "
632 "'-', '.', '_', starting with a letter.\n");
635 opts
= qemu_opts_find(list
, id
);
637 if (fail_if_exists
&& !list
->merge_lists
) {
638 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
644 } else if (list
->merge_lists
) {
645 opts
= qemu_opts_find(list
, NULL
);
650 opts
= g_malloc0(sizeof(*opts
));
651 opts
->id
= g_strdup(id
);
653 loc_save(&opts
->loc
);
654 QTAILQ_INIT(&opts
->head
);
655 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
659 void qemu_opts_reset(QemuOptsList
*list
)
661 QemuOpts
*opts
, *next_opts
;
663 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
668 void qemu_opts_loc_restore(QemuOpts
*opts
)
670 loc_restore(&opts
->loc
);
673 bool qemu_opts_set(QemuOptsList
*list
, const char *name
, const char *value
, Error
**errp
)
677 assert(list
->merge_lists
);
678 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
679 return qemu_opt_set(opts
, name
, value
, errp
);
682 const char *qemu_opts_id(QemuOpts
*opts
)
687 /* The id string will be g_free()d by qemu_opts_del */
688 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
693 void qemu_opts_del(QemuOpts
*opts
)
702 opt
= QTAILQ_FIRST(&opts
->head
);
707 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
712 /* print value, escaping any commas in value */
713 static void escaped_print(const char *value
)
717 for (ptr
= value
; *ptr
; ++ptr
) {
725 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
728 QemuOptDesc
*desc
= opts
->list
->desc
;
729 const char *sep
= "";
732 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
736 if (desc
[0].name
== NULL
) {
737 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
738 printf("%s%s=", sep
, opt
->name
);
739 escaped_print(opt
->str
);
744 for (; desc
&& desc
->name
; desc
++) {
746 opt
= qemu_opt_find(opts
, desc
->name
);
748 value
= opt
? opt
->str
: desc
->def_value_str
;
752 if (desc
->type
== QEMU_OPT_STRING
) {
753 printf("%s%s=", sep
, desc
->name
);
754 escaped_print(value
);
755 } else if ((desc
->type
== QEMU_OPT_SIZE
||
756 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
757 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
759 printf("%s%s=%s", sep
, desc
->name
, value
);
765 static const char *get_opt_name_value(const char *params
,
766 const char *firstname
,
767 char **name
, char **value
)
769 const char *p
, *pe
, *pc
;
771 pe
= strchr(params
, '=');
772 pc
= strchr(params
, ',');
774 if (!pe
|| (pc
&& pc
< pe
)) {
775 /* found "foo,more" */
777 /* implicitly named first option */
778 *name
= g_strdup(firstname
);
779 p
= get_opt_value(params
, value
);
781 /* option without value, must be a flag */
782 p
= get_opt_name(params
, name
, ',');
783 if (strncmp(*name
, "no", 2) == 0) {
784 memmove(*name
, *name
+ 2, strlen(*name
+ 2) + 1);
785 *value
= g_strdup("off");
787 *value
= g_strdup("on");
791 /* found "foo=bar,more" */
792 p
= get_opt_name(params
, name
, '=');
795 p
= get_opt_value(p
, value
);
798 assert(!*p
|| *p
== ',');
805 static bool opts_do_parse(QemuOpts
*opts
, const char *params
,
806 const char *firstname
, bool prepend
,
807 bool *help_wanted
, Error
**errp
)
809 char *option
, *value
;
813 for (p
= params
; *p
;) {
814 p
= get_opt_name_value(p
, firstname
, &option
, &value
);
817 if (!strcmp(option
, "id")) {
823 opt
= opt_create(opts
, option
, value
, prepend
);
825 if (!opt_validate(opt
, help_wanted
, errp
)) {
834 static char *opts_parse_id(const char *params
)
839 for (p
= params
; *p
;) {
840 p
= get_opt_name_value(p
, NULL
, &name
, &value
);
841 if (!strcmp(name
, "id")) {
852 bool has_help_option(const char *params
)
858 for (p
= params
; *p
;) {
859 p
= get_opt_name_value(p
, NULL
, &name
, &value
);
860 ret
= is_help_option(name
);
872 * Store options parsed from @params into @opts.
873 * If @firstname is non-null, the first key=value in @params may omit
874 * key=, and is treated as if key was @firstname.
875 * On error, store an error object through @errp if non-null.
877 bool qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
878 const char *firstname
, Error
**errp
)
880 return opts_do_parse(opts
, params
, firstname
, false, NULL
, errp
);
883 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
884 bool permit_abbrev
, bool defaults
,
885 bool *help_wanted
, Error
**errp
)
887 const char *firstname
;
888 char *id
= opts_parse_id(params
);
891 assert(!permit_abbrev
|| list
->implied_opt_name
);
892 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
895 * This code doesn't work for defaults && !list->merge_lists: when
896 * params has no id=, and list has an element with !opts->id, it
897 * appends a new element instead of returning the existing opts.
898 * However, we got no use for this case. Guard against possible
899 * (if unlikely) future misuse:
901 assert(!defaults
|| list
->merge_lists
);
902 opts
= qemu_opts_create(list
, id
, !defaults
, errp
);
908 if (!opts_do_parse(opts
, params
, firstname
, defaults
, help_wanted
,
918 * Create a QemuOpts in @list and with options parsed from @params.
919 * If @permit_abbrev, the first key=value in @params may omit key=,
920 * and is treated as if key was @list->implied_opt_name.
921 * On error, store an error object through @errp if non-null.
922 * Return the new QemuOpts on success, null pointer on error.
924 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
925 bool permit_abbrev
, Error
**errp
)
927 return opts_parse(list
, params
, permit_abbrev
, false, NULL
, errp
);
931 * Create a QemuOpts in @list and with options parsed from @params.
932 * If @permit_abbrev, the first key=value in @params may omit key=,
933 * and is treated as if key was @list->implied_opt_name.
934 * Report errors with error_report_err(). This is inappropriate in
935 * QMP context. Do not use this function there!
936 * Return the new QemuOpts on success, null pointer on error.
938 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
943 bool help_wanted
= false;
945 opts
= opts_parse(list
, params
, permit_abbrev
, false, &help_wanted
, &err
);
948 qemu_opts_print_help(list
, true);
951 error_report_err(err
);
957 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
962 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
, NULL
);
966 static bool qemu_opts_from_qdict_entry(QemuOpts
*opts
,
967 const QDictEntry
*entry
,
970 const char *key
= qdict_entry_key(entry
);
971 QObject
*obj
= qdict_entry_value(entry
);
973 g_autofree
char *tmp
= NULL
;
976 if (!strcmp(key
, "id")) {
980 switch (qobject_type(obj
)) {
982 value
= qstring_get_str(qobject_to(QString
, obj
));
985 tmp
= qnum_to_string(qobject_to(QNum
, obj
));
989 pstrcpy(buf
, sizeof(buf
),
990 qbool_get_bool(qobject_to(QBool
, obj
)) ? "on" : "off");
997 return qemu_opt_set(opts
, key
, value
, errp
);
1001 * Create QemuOpts from a QDict.
1002 * Use value of key "id" as ID if it exists and is a QString. Only
1003 * QStrings, QNums and QBools are copied. Entries with other types
1004 * are silently ignored.
1006 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1010 const QDictEntry
*entry
;
1012 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1, errp
);
1017 assert(opts
!= NULL
);
1019 for (entry
= qdict_first(qdict
);
1021 entry
= qdict_next(qdict
, entry
)) {
1022 if (!qemu_opts_from_qdict_entry(opts
, entry
, errp
)) {
1023 qemu_opts_del(opts
);
1032 * Adds all QDict entries to the QemuOpts that can be added and removes them
1033 * from the QDict. When this function returns, the QDict contains only those
1034 * entries that couldn't be added to the QemuOpts.
1036 bool qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1038 const QDictEntry
*entry
, *next
;
1040 entry
= qdict_first(qdict
);
1042 while (entry
!= NULL
) {
1043 next
= qdict_next(qdict
, entry
);
1045 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1046 if (!qemu_opts_from_qdict_entry(opts
, entry
, errp
)) {
1049 qdict_del(qdict
, entry
->key
);
1059 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1061 * If @list is given, only add those options to the QDict that are contained in
1062 * the list. If @del is true, any options added to the QDict are removed from
1063 * the QemuOpts, otherwise they remain there.
1065 * If two options in @opts have the same name, they are processed in order
1066 * so that the last one wins (consistent with the reverse iteration in
1067 * qemu_opt_find()), but all of them are deleted if @del is true.
1069 * TODO We'll want to use types appropriate for opt->desc->type, but
1070 * this is enough for now.
1072 QDict
*qemu_opts_to_qdict_filtered(QemuOpts
*opts
, QDict
*qdict
,
1073 QemuOptsList
*list
, bool del
)
1075 QemuOpt
*opt
, *next
;
1078 qdict
= qdict_new();
1081 qdict_put_str(qdict
, "id", opts
->id
);
1083 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next
) {
1087 for (desc
= list
->desc
; desc
->name
; desc
++) {
1088 if (!strcmp(desc
->name
, opt
->name
)) {
1097 qdict_put_str(qdict
, opt
->name
, opt
->str
);
1105 /* Copy all options in a QemuOpts to the given QDict. See
1106 * qemu_opts_to_qdict_filtered() for details. */
1107 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1109 return qemu_opts_to_qdict_filtered(opts
, qdict
, NULL
, false);
1112 /* Validate parsed opts against descriptions where no
1113 * descriptions were provided in the QemuOptsList.
1115 bool qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1119 assert(opts_accepts_any(opts
));
1121 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1122 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1124 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1128 if (!qemu_opt_parse(opt
, errp
)) {
1137 * For each member of @list, call @func(@opaque, member, @errp).
1138 * Call it with the current location temporarily set to the member's.
1139 * @func() may store an Error through @errp, but must return non-zero then.
1140 * When @func() returns non-zero, break the loop and return that value.
1141 * Return zero when the loop completes.
1143 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1144 void *opaque
, Error
**errp
)
1150 loc_push_none(&loc
);
1151 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1152 loc_restore(&opts
->loc
);
1153 rc
= func(opaque
, opts
, errp
);
1157 assert(!errp
|| !*errp
);
1163 static size_t count_opts_list(QemuOptsList
*list
)
1165 QemuOptDesc
*desc
= NULL
;
1166 size_t num_opts
= 0;
1173 while (desc
&& desc
->name
) {
1181 void qemu_opts_free(QemuOptsList
*list
)
1186 /* Realloc dst option list and append options from an option list (list)
1187 * to it. dst could be NULL or a malloced list.
1188 * The lifetime of dst must be shorter than the input list because the
1189 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1191 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1194 size_t num_opts
, num_dst_opts
;
1196 bool need_init
= false;
1197 bool need_head_update
;
1203 /* If dst is NULL, after realloc, some area of dst should be initialized
1204 * before adding options to it.
1208 need_head_update
= true;
1210 /* Moreover, even if dst is not NULL, the realloc may move it to a
1211 * different address in which case we may get a stale tail pointer
1213 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1216 num_opts
= count_opts_list(dst
);
1217 num_dst_opts
= num_opts
;
1218 num_opts
+= count_opts_list(list
);
1219 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1220 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1223 dst
->implied_opt_name
= NULL
;
1224 dst
->merge_lists
= false;
1226 if (need_head_update
) {
1227 QTAILQ_INIT(&dst
->head
);
1229 dst
->desc
[num_dst_opts
].name
= NULL
;
1231 /* append list->desc to dst->desc */
1234 while (desc
&& desc
->name
) {
1235 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1236 dst
->desc
[num_dst_opts
++] = *desc
;
1237 dst
->desc
[num_dst_opts
].name
= NULL
;