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_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'");
114 static bool parse_option_number(const char *name
, const char *value
,
115 uint64_t *ret
, Error
**errp
)
120 err
= qemu_strtou64(value
, NULL
, 0, &number
);
121 if (err
== -ERANGE
) {
122 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
127 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
134 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
139 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
140 if (strcmp(desc
[i
].name
, name
) == 0) {
148 static const char *find_default_by_name(QemuOpts
*opts
, const char *name
)
150 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
152 return desc
? desc
->def_value_str
: NULL
;
155 bool parse_option_size(const char *name
, const char *value
,
156 uint64_t *ret
, Error
**errp
)
161 err
= qemu_strtosz(value
, NULL
, &size
);
162 if (err
== -ERANGE
) {
163 error_setg(errp
, "Value '%s' is out of range for parameter '%s'",
168 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
169 "a non-negative number below 2^64");
170 error_append_hint(errp
, "Optional suffix k, M, G, T, P or E means"
171 " kilo-, mega-, giga-, tera-, peta-\n"
172 "and exabytes, respectively.\n");
179 static const char *opt_type_to_string(enum QemuOptType type
)
182 case QEMU_OPT_STRING
:
185 return "bool (on/off)";
186 case QEMU_OPT_NUMBER
:
192 g_assert_not_reached();
196 * Print the list of options available in the given list. If
197 * @print_caption is true, a caption (including the list name, if it
198 * exists) is printed. The options itself will be indented, so
199 * @print_caption should only be set to false if the caller prints its
200 * own custom caption (so that the indentation makes sense).
202 void qemu_opts_print_help(QemuOptsList
*list
, bool print_caption
)
206 GPtrArray
*array
= g_ptr_array_new();
210 while (desc
&& desc
->name
) {
211 GString
*str
= g_string_new(NULL
);
212 g_string_append_printf(str
, " %s=<%s>", desc
->name
,
213 opt_type_to_string(desc
->type
));
216 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
218 g_string_append_printf(str
, " - %s", desc
->help
);
220 g_ptr_array_add(array
, g_string_free(str
, false));
224 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
225 if (print_caption
&& array
->len
> 0) {
227 printf("%s options:\n", list
->name
);
229 printf("Options:\n");
231 } else if (array
->len
== 0) {
233 printf("There are no options for %s.\n", list
->name
);
235 printf("No options available.\n");
238 for (i
= 0; i
< array
->len
; i
++) {
239 printf("%s\n", (char *)array
->pdata
[i
]);
241 g_ptr_array_set_free_func(array
, g_free
);
242 g_ptr_array_free(array
, true);
245 /* ------------------------------------------------------------------ */
247 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
251 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
252 if (strcmp(opt
->name
, name
) != 0)
259 static void qemu_opt_del(QemuOpt
*opt
)
261 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
267 /* qemu_opt_set allows many settings for the same option.
268 * This function deletes all settings for an option.
270 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
272 QemuOpt
*opt
, *next_opt
;
274 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
275 if (!strcmp(opt
->name
, name
)) {
281 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
289 opt
= qemu_opt_find(opts
, name
);
291 return find_default_by_name(opts
, name
);
297 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
300 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
304 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
306 QemuOpt
*ret
= iter
->opt
;
308 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
309 ret
= QTAILQ_NEXT(ret
, next
);
312 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
313 return ret
? ret
->str
: NULL
;
316 /* Get a known option (or its default) and remove it from the list
317 * all in one action. Return a malloced string of the option value.
318 * Result must be freed by caller with g_free().
320 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
329 opt
= qemu_opt_find(opts
, name
);
331 return g_strdup(find_default_by_name(opts
, name
));
335 qemu_opt_del_all(opts
, name
);
339 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
343 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, next
) {
344 if (is_help_option(opt
->name
)) {
351 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
352 bool defval
, bool del
)
362 opt
= qemu_opt_find(opts
, name
);
364 def_val
= find_default_by_name(opts
, name
);
366 parse_option_bool(name
, def_val
, &ret
, &error_abort
);
370 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
371 ret
= opt
->value
.boolean
;
373 qemu_opt_del_all(opts
, name
);
378 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
380 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
383 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
385 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
388 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
389 uint64_t defval
, bool del
)
393 uint64_t ret
= defval
;
399 opt
= qemu_opt_find(opts
, name
);
401 def_val
= find_default_by_name(opts
, name
);
403 parse_option_number(name
, def_val
, &ret
, &error_abort
);
407 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
408 ret
= opt
->value
.uint
;
410 qemu_opt_del_all(opts
, name
);
415 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
417 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
420 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
423 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
426 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
427 uint64_t defval
, bool del
)
431 uint64_t ret
= defval
;
437 opt
= qemu_opt_find(opts
, name
);
439 def_val
= find_default_by_name(opts
, name
);
441 parse_option_size(name
, def_val
, &ret
, &error_abort
);
445 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
446 ret
= opt
->value
.uint
;
448 qemu_opt_del_all(opts
, name
);
453 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
455 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
458 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
461 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
464 static bool qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
466 if (opt
->desc
== NULL
)
469 switch (opt
->desc
->type
) {
470 case QEMU_OPT_STRING
:
474 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
,
476 case QEMU_OPT_NUMBER
:
477 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
,
480 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
,
487 static bool opts_accepts_any(const QemuOpts
*opts
)
489 return opts
->list
->desc
[0].name
== NULL
;
492 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
494 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
496 assert(opts_accepts_any(opts
));
506 static QemuOpt
*opt_create(QemuOpts
*opts
, const char *name
, char *value
,
509 QemuOpt
*opt
= g_malloc0(sizeof(*opt
));
511 opt
->name
= g_strdup(name
);
515 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
517 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
523 static bool opt_validate(QemuOpt
*opt
, bool *help_wanted
,
526 const QemuOptDesc
*desc
;
528 desc
= find_desc_by_name(opt
->opts
->list
->desc
, opt
->name
);
529 if (!desc
&& !opts_accepts_any(opt
->opts
)) {
530 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
531 if (help_wanted
&& is_help_option(opt
->name
)) {
538 if (!qemu_opt_parse(opt
, errp
)) {
545 bool qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
548 QemuOpt
*opt
= opt_create(opts
, name
, g_strdup(value
), false);
550 if (!opt_validate(opt
, NULL
, errp
)) {
557 bool qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
561 const QemuOptDesc
*desc
;
563 desc
= find_desc_by_name(opts
->list
->desc
, name
);
564 if (!desc
&& !opts_accepts_any(opts
)) {
565 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
569 opt
= g_malloc0(sizeof(*opt
));
570 opt
->name
= g_strdup(name
);
573 opt
->value
.boolean
= !!val
;
574 opt
->str
= g_strdup(val
? "on" : "off");
575 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
579 bool qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
583 const QemuOptDesc
*desc
;
585 desc
= find_desc_by_name(opts
->list
->desc
, name
);
586 if (!desc
&& !opts_accepts_any(opts
)) {
587 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
591 opt
= g_malloc0(sizeof(*opt
));
592 opt
->name
= g_strdup(name
);
595 opt
->value
.uint
= val
;
596 opt
->str
= g_strdup_printf("%" PRId64
, val
);
597 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
602 * For each member of @opts, call @func(@opaque, name, value, @errp).
603 * @func() may store an Error through @errp, but must return non-zero then.
604 * When @func() returns non-zero, break the loop and return that value.
605 * Return zero when the loop completes.
607 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
613 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
614 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
618 assert(!errp
|| !*errp
);
623 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
627 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
628 if (!opts
->id
&& !id
) {
631 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
638 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
639 int fail_if_exists
, Error
**errp
)
641 QemuOpts
*opts
= NULL
;
644 if (!id_wellformed(id
)) {
645 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
647 error_append_hint(errp
, "Identifiers consist of letters, digits, "
648 "'-', '.', '_', starting with a letter.\n");
651 opts
= qemu_opts_find(list
, id
);
653 if (fail_if_exists
&& !list
->merge_lists
) {
654 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
660 } else if (list
->merge_lists
) {
661 opts
= qemu_opts_find(list
, NULL
);
666 opts
= g_malloc0(sizeof(*opts
));
667 opts
->id
= g_strdup(id
);
669 loc_save(&opts
->loc
);
670 QTAILQ_INIT(&opts
->head
);
671 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
675 void qemu_opts_reset(QemuOptsList
*list
)
677 QemuOpts
*opts
, *next_opts
;
679 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
684 void qemu_opts_loc_restore(QemuOpts
*opts
)
686 loc_restore(&opts
->loc
);
689 bool qemu_opts_set(QemuOptsList
*list
, const char *id
,
690 const char *name
, const char *value
, Error
**errp
)
694 opts
= qemu_opts_create(list
, id
, 1, errp
);
698 return qemu_opt_set(opts
, name
, value
, errp
);
701 const char *qemu_opts_id(QemuOpts
*opts
)
706 /* The id string will be g_free()d by qemu_opts_del */
707 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
712 void qemu_opts_del(QemuOpts
*opts
)
721 opt
= QTAILQ_FIRST(&opts
->head
);
726 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
731 /* print value, escaping any commas in value */
732 static void escaped_print(const char *value
)
736 for (ptr
= value
; *ptr
; ++ptr
) {
744 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
747 QemuOptDesc
*desc
= opts
->list
->desc
;
748 const char *sep
= "";
751 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
755 if (desc
[0].name
== NULL
) {
756 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
757 printf("%s%s=", sep
, opt
->name
);
758 escaped_print(opt
->str
);
763 for (; desc
&& desc
->name
; desc
++) {
765 opt
= qemu_opt_find(opts
, desc
->name
);
767 value
= opt
? opt
->str
: desc
->def_value_str
;
771 if (desc
->type
== QEMU_OPT_STRING
) {
772 printf("%s%s=", sep
, desc
->name
);
773 escaped_print(value
);
774 } else if ((desc
->type
== QEMU_OPT_SIZE
||
775 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
776 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
778 printf("%s%s=%s", sep
, desc
->name
, value
);
784 static const char *get_opt_name_value(const char *params
,
785 const char *firstname
,
786 char **name
, char **value
)
788 const char *p
, *pe
, *pc
;
790 pe
= strchr(params
, '=');
791 pc
= strchr(params
, ',');
793 if (!pe
|| (pc
&& pc
< pe
)) {
794 /* found "foo,more" */
796 /* implicitly named first option */
797 *name
= g_strdup(firstname
);
798 p
= get_opt_value(params
, value
);
800 /* option without value, must be a flag */
801 p
= get_opt_name(params
, name
, ',');
802 if (strncmp(*name
, "no", 2) == 0) {
803 memmove(*name
, *name
+ 2, strlen(*name
+ 2) + 1);
804 *value
= g_strdup("off");
806 *value
= g_strdup("on");
810 /* found "foo=bar,more" */
811 p
= get_opt_name(params
, name
, '=');
814 p
= get_opt_value(p
, value
);
817 assert(!*p
|| *p
== ',');
824 static bool opts_do_parse(QemuOpts
*opts
, const char *params
,
825 const char *firstname
, bool prepend
,
826 bool *help_wanted
, Error
**errp
)
828 char *option
, *value
;
832 for (p
= params
; *p
;) {
833 p
= get_opt_name_value(p
, firstname
, &option
, &value
);
836 if (!strcmp(option
, "id")) {
842 opt
= opt_create(opts
, option
, value
, prepend
);
844 if (!opt_validate(opt
, help_wanted
, errp
)) {
853 static char *opts_parse_id(const char *params
)
858 for (p
= params
; *p
;) {
859 p
= get_opt_name_value(p
, NULL
, &name
, &value
);
860 if (!strcmp(name
, "id")) {
871 bool has_help_option(const char *params
)
877 for (p
= params
; *p
;) {
878 p
= get_opt_name_value(p
, NULL
, &name
, &value
);
879 ret
= is_help_option(name
);
891 * Store options parsed from @params into @opts.
892 * If @firstname is non-null, the first key=value in @params may omit
893 * key=, and is treated as if key was @firstname.
894 * On error, store an error object through @errp if non-null.
896 bool qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
897 const char *firstname
, Error
**errp
)
899 return opts_do_parse(opts
, params
, firstname
, false, NULL
, errp
);
902 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
903 bool permit_abbrev
, bool defaults
,
904 bool *help_wanted
, Error
**errp
)
906 const char *firstname
;
907 char *id
= opts_parse_id(params
);
910 assert(!permit_abbrev
|| list
->implied_opt_name
);
911 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
914 * This code doesn't work for defaults && !list->merge_lists: when
915 * params has no id=, and list has an element with !opts->id, it
916 * appends a new element instead of returning the existing opts.
917 * However, we got no use for this case. Guard against possible
918 * (if unlikely) future misuse:
920 assert(!defaults
|| list
->merge_lists
);
921 opts
= qemu_opts_create(list
, id
, !defaults
, errp
);
927 if (!opts_do_parse(opts
, params
, firstname
, defaults
, help_wanted
,
937 * Create a QemuOpts in @list and with options parsed from @params.
938 * If @permit_abbrev, the first key=value in @params may omit key=,
939 * and is treated as if key was @list->implied_opt_name.
940 * On error, store an error object through @errp if non-null.
941 * Return the new QemuOpts on success, null pointer on error.
943 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
944 bool permit_abbrev
, Error
**errp
)
946 return opts_parse(list
, params
, permit_abbrev
, false, NULL
, errp
);
950 * Create a QemuOpts in @list and with options parsed from @params.
951 * If @permit_abbrev, the first key=value in @params may omit key=,
952 * and is treated as if key was @list->implied_opt_name.
953 * Report errors with error_report_err(). This is inappropriate in
954 * QMP context. Do not use this function there!
955 * Return the new QemuOpts on success, null pointer on error.
957 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
962 bool help_wanted
= false;
964 opts
= opts_parse(list
, params
, permit_abbrev
, false, &help_wanted
, &err
);
967 qemu_opts_print_help(list
, true);
970 error_report_err(err
);
976 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
981 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
, NULL
);
985 static bool qemu_opts_from_qdict_entry(QemuOpts
*opts
,
986 const QDictEntry
*entry
,
989 const char *key
= qdict_entry_key(entry
);
990 QObject
*obj
= qdict_entry_value(entry
);
992 g_autofree
char *tmp
= NULL
;
995 if (!strcmp(key
, "id")) {
999 switch (qobject_type(obj
)) {
1001 value
= qstring_get_str(qobject_to(QString
, obj
));
1004 tmp
= qnum_to_string(qobject_to(QNum
, obj
));
1008 pstrcpy(buf
, sizeof(buf
),
1009 qbool_get_bool(qobject_to(QBool
, obj
)) ? "on" : "off");
1016 return qemu_opt_set(opts
, key
, value
, errp
);
1020 * Create QemuOpts from a QDict.
1021 * Use value of key "id" as ID if it exists and is a QString. Only
1022 * QStrings, QNums and QBools are copied. Entries with other types
1023 * are silently ignored.
1025 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1029 const QDictEntry
*entry
;
1031 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1, errp
);
1036 assert(opts
!= NULL
);
1038 for (entry
= qdict_first(qdict
);
1040 entry
= qdict_next(qdict
, entry
)) {
1041 if (!qemu_opts_from_qdict_entry(opts
, entry
, errp
)) {
1042 qemu_opts_del(opts
);
1051 * Adds all QDict entries to the QemuOpts that can be added and removes them
1052 * from the QDict. When this function returns, the QDict contains only those
1053 * entries that couldn't be added to the QemuOpts.
1055 bool qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1057 const QDictEntry
*entry
, *next
;
1059 entry
= qdict_first(qdict
);
1061 while (entry
!= NULL
) {
1062 next
= qdict_next(qdict
, entry
);
1064 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1065 if (!qemu_opts_from_qdict_entry(opts
, entry
, errp
)) {
1068 qdict_del(qdict
, entry
->key
);
1078 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1080 * If @list is given, only add those options to the QDict that are contained in
1081 * the list. If @del is true, any options added to the QDict are removed from
1082 * the QemuOpts, otherwise they remain there.
1084 * If two options in @opts have the same name, they are processed in order
1085 * so that the last one wins (consistent with the reverse iteration in
1086 * qemu_opt_find()), but all of them are deleted if @del is true.
1088 * TODO We'll want to use types appropriate for opt->desc->type, but
1089 * this is enough for now.
1091 QDict
*qemu_opts_to_qdict_filtered(QemuOpts
*opts
, QDict
*qdict
,
1092 QemuOptsList
*list
, bool del
)
1094 QemuOpt
*opt
, *next
;
1097 qdict
= qdict_new();
1100 qdict_put_str(qdict
, "id", opts
->id
);
1102 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next
) {
1106 for (desc
= list
->desc
; desc
->name
; desc
++) {
1107 if (!strcmp(desc
->name
, opt
->name
)) {
1116 qdict_put_str(qdict
, opt
->name
, opt
->str
);
1124 /* Copy all options in a QemuOpts to the given QDict. See
1125 * qemu_opts_to_qdict_filtered() for details. */
1126 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1128 return qemu_opts_to_qdict_filtered(opts
, qdict
, NULL
, false);
1131 /* Validate parsed opts against descriptions where no
1132 * descriptions were provided in the QemuOptsList.
1134 bool qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1138 assert(opts_accepts_any(opts
));
1140 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1141 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1143 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1147 if (!qemu_opt_parse(opt
, errp
)) {
1156 * For each member of @list, call @func(@opaque, member, @errp).
1157 * Call it with the current location temporarily set to the member's.
1158 * @func() may store an Error through @errp, but must return non-zero then.
1159 * When @func() returns non-zero, break the loop and return that value.
1160 * Return zero when the loop completes.
1162 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1163 void *opaque
, Error
**errp
)
1169 loc_push_none(&loc
);
1170 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1171 loc_restore(&opts
->loc
);
1172 rc
= func(opaque
, opts
, errp
);
1176 assert(!errp
|| !*errp
);
1182 static size_t count_opts_list(QemuOptsList
*list
)
1184 QemuOptDesc
*desc
= NULL
;
1185 size_t num_opts
= 0;
1192 while (desc
&& desc
->name
) {
1200 void qemu_opts_free(QemuOptsList
*list
)
1205 /* Realloc dst option list and append options from an option list (list)
1206 * to it. dst could be NULL or a malloced list.
1207 * The lifetime of dst must be shorter than the input list because the
1208 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1210 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1213 size_t num_opts
, num_dst_opts
;
1215 bool need_init
= false;
1216 bool need_head_update
;
1222 /* If dst is NULL, after realloc, some area of dst should be initialized
1223 * before adding options to it.
1227 need_head_update
= true;
1229 /* Moreover, even if dst is not NULL, the realloc may move it to a
1230 * different address in which case we may get a stale tail pointer
1232 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1235 num_opts
= count_opts_list(dst
);
1236 num_dst_opts
= num_opts
;
1237 num_opts
+= count_opts_list(list
);
1238 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1239 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1242 dst
->implied_opt_name
= NULL
;
1243 dst
->merge_lists
= false;
1245 if (need_head_update
) {
1246 QTAILQ_INIT(&dst
->head
);
1248 dst
->desc
[num_dst_opts
].name
= NULL
;
1250 /* append list->desc to dst->desc */
1253 while (desc
&& desc
->name
) {
1254 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1255 dst
->desc
[num_dst_opts
++] = *desc
;
1256 dst
->desc
[num_dst_opts
].name
= NULL
;