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 "qemu-common.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/types.h"
31 #include "qapi/qmp/qerror.h"
32 #include "qemu/option_int.h"
35 * Extracts the name of an option from the parameter string (p points at the
36 * first byte of the option name)
38 * The option name is delimited by delim (usually , or =) or the string end
39 * and is copied into buf. If the option name is longer than buf_size, it is
40 * truncated. buf is always zero terminated.
42 * The return value is the position of the delimiter/zero byte after the option
45 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
50 while (*p
!= '\0' && *p
!= delim
) {
51 if (q
&& (q
- buf
) < buf_size
- 1)
62 * Extracts the value of an option from the parameter string p (p points at the
63 * first byte of the option value)
65 * This function is comparable to get_opt_name with the difference that the
66 * delimiter is fixed to be comma which starts a new option. To specify an
67 * option value that contains commas, double each comma.
69 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
80 if (q
&& (q
- buf
) < buf_size
- 1)
90 int get_next_param_value(char *buf
, int buf_size
,
91 const char *tag
, const char **pstr
)
98 p
= get_opt_name(option
, sizeof(option
), p
, '=');
102 if (!strcmp(tag
, option
)) {
103 *pstr
= get_opt_value(buf
, buf_size
, p
);
109 p
= get_opt_value(NULL
, 0, p
);
118 int get_param_value(char *buf
, int buf_size
,
119 const char *tag
, const char *str
)
121 return get_next_param_value(buf
, buf_size
, tag
, &str
);
124 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
128 if (!strcmp(value
, "on")) {
130 } else if (!strcmp(value
, "off")) {
133 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
134 name
, "'on' or 'off'");
141 static void parse_option_number(const char *name
, const char *value
,
142 uint64_t *ret
, Error
**errp
)
148 number
= strtoull(value
, &postfix
, 0);
149 if (*postfix
!= '\0') {
150 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
155 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
159 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
164 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
165 if (strcmp(desc
[i
].name
, name
) == 0) {
173 void parse_option_size(const char *name
, const char *value
,
174 uint64_t *ret
, Error
**errp
)
180 sizef
= strtod(value
, &postfix
);
181 if (sizef
< 0 || sizef
> UINT64_MAX
) {
182 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
183 "a non-negative number below 2^64");
202 *ret
= (uint64_t) sizef
;
205 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
206 error_append_hint(errp
, "You may use k, M, G or T suffixes for "
207 "kilobytes, megabytes, gigabytes and terabytes.\n");
211 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
215 bool has_help_option(const char *param
)
217 size_t buflen
= strlen(param
) + 1;
218 char *buf
= g_malloc(buflen
);
219 const char *p
= param
;
223 p
= get_opt_value(buf
, buflen
, p
);
228 if (is_help_option(buf
)) {
239 bool is_valid_option_list(const char *param
)
241 size_t buflen
= strlen(param
) + 1;
242 char *buf
= g_malloc(buflen
);
243 const char *p
= param
;
247 p
= get_opt_value(buf
, buflen
, p
);
253 if (!*buf
|| *buf
== ',') {
264 void qemu_opts_print_help(QemuOptsList
*list
)
270 printf("Supported options:\n");
271 while (desc
&& desc
->name
) {
272 printf("%-16s %s\n", desc
->name
,
273 desc
->help
? desc
->help
: "No description available");
277 /* ------------------------------------------------------------------ */
279 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
283 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
284 if (strcmp(opt
->name
, name
) != 0)
291 static void qemu_opt_del(QemuOpt
*opt
)
293 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
299 /* qemu_opt_set allows many settings for the same option.
300 * This function deletes all settings for an option.
302 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
304 QemuOpt
*opt
, *next_opt
;
306 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
307 if (!strcmp(opt
->name
, name
)) {
313 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
321 opt
= qemu_opt_find(opts
, name
);
323 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
324 if (desc
&& desc
->def_value_str
) {
325 return desc
->def_value_str
;
328 return opt
? opt
->str
: NULL
;
331 /* Get a known option (or its default) and remove it from the list
332 * all in one action. Return a malloced string of the option value.
333 * Result must be freed by caller with g_free().
335 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
338 const QemuOptDesc
*desc
;
345 opt
= qemu_opt_find(opts
, name
);
347 desc
= find_desc_by_name(opts
->list
->desc
, name
);
348 if (desc
&& desc
->def_value_str
) {
349 str
= g_strdup(desc
->def_value_str
);
355 qemu_opt_del_all(opts
, name
);
359 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
363 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
364 if (is_help_option(opt
->name
)) {
371 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
372 bool defval
, bool del
)
381 opt
= qemu_opt_find(opts
, name
);
383 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
384 if (desc
&& desc
->def_value_str
) {
385 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
389 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
390 ret
= opt
->value
.boolean
;
392 qemu_opt_del_all(opts
, name
);
397 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
399 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
402 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
404 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
407 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
408 uint64_t defval
, bool del
)
411 uint64_t ret
= defval
;
417 opt
= qemu_opt_find(opts
, name
);
419 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
420 if (desc
&& desc
->def_value_str
) {
421 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
425 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
426 ret
= opt
->value
.uint
;
428 qemu_opt_del_all(opts
, name
);
433 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
435 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
438 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
441 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
444 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
445 uint64_t defval
, bool del
)
448 uint64_t ret
= defval
;
454 opt
= qemu_opt_find(opts
, name
);
456 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
457 if (desc
&& desc
->def_value_str
) {
458 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
462 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
463 ret
= opt
->value
.uint
;
465 qemu_opt_del_all(opts
, name
);
470 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
472 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
475 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
478 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
481 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
483 if (opt
->desc
== NULL
)
486 switch (opt
->desc
->type
) {
487 case QEMU_OPT_STRING
:
491 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
493 case QEMU_OPT_NUMBER
:
494 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
497 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
504 static bool opts_accepts_any(const QemuOpts
*opts
)
506 return opts
->list
->desc
[0].name
== NULL
;
509 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
511 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
513 assert(opts_accepts_any(opts
));
523 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
524 bool prepend
, Error
**errp
)
527 const QemuOptDesc
*desc
;
528 Error
*local_err
= NULL
;
530 desc
= find_desc_by_name(opts
->list
->desc
, name
);
531 if (!desc
&& !opts_accepts_any(opts
)) {
532 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
536 opt
= g_malloc0(sizeof(*opt
));
537 opt
->name
= g_strdup(name
);
540 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
542 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
545 opt
->str
= g_strdup(value
);
546 qemu_opt_parse(opt
, &local_err
);
548 error_propagate(errp
, local_err
);
553 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
556 opt_set(opts
, name
, value
, false, errp
);
559 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
563 const QemuOptDesc
*desc
= opts
->list
->desc
;
565 opt
= g_malloc0(sizeof(*opt
));
566 opt
->desc
= find_desc_by_name(desc
, name
);
567 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
568 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
573 opt
->name
= g_strdup(name
);
575 opt
->value
.boolean
= !!val
;
576 opt
->str
= g_strdup(val
? "on" : "off");
577 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
580 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
584 const QemuOptDesc
*desc
= opts
->list
->desc
;
586 opt
= g_malloc0(sizeof(*opt
));
587 opt
->desc
= find_desc_by_name(desc
, name
);
588 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
589 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
594 opt
->name
= g_strdup(name
);
596 opt
->value
.uint
= val
;
597 opt
->str
= g_strdup_printf("%" PRId64
, val
);
598 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 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
690 const char *name
, const char *value
, Error
**errp
)
693 Error
*local_err
= NULL
;
695 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
697 error_propagate(errp
, local_err
);
700 qemu_opt_set(opts
, name
, value
, errp
);
703 const char *qemu_opts_id(QemuOpts
*opts
)
708 /* The id string will be g_free()d by qemu_opts_del */
709 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
714 void qemu_opts_del(QemuOpts
*opts
)
723 opt
= QTAILQ_FIRST(&opts
->head
);
728 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
733 /* print value, escaping any commas in value */
734 static void escaped_print(const char *value
)
738 for (ptr
= value
; *ptr
; ++ptr
) {
746 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
749 QemuOptDesc
*desc
= opts
->list
->desc
;
750 const char *sep
= "";
753 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
757 if (desc
[0].name
== NULL
) {
758 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
759 printf("%s%s=", sep
, opt
->name
);
760 escaped_print(opt
->str
);
765 for (; desc
&& desc
->name
; desc
++) {
767 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
769 value
= opt
? opt
->str
: desc
->def_value_str
;
773 if (desc
->type
== QEMU_OPT_STRING
) {
774 printf("%s%s=", sep
, desc
->name
);
775 escaped_print(value
);
776 } else if ((desc
->type
== QEMU_OPT_SIZE
||
777 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
778 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
780 printf("%s%s=%s", sep
, desc
->name
, value
);
786 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
787 const char *firstname
, bool prepend
, Error
**errp
)
789 char option
[128], value
[1024];
790 const char *p
,*pe
,*pc
;
791 Error
*local_err
= NULL
;
793 for (p
= params
; *p
!= '\0'; p
++) {
796 if (!pe
|| (pc
&& pc
< pe
)) {
797 /* found "foo,more" */
798 if (p
== params
&& firstname
) {
799 /* implicitly named first option */
800 pstrcpy(option
, sizeof(option
), firstname
);
801 p
= get_opt_value(value
, sizeof(value
), p
);
803 /* option without value, probably a flag */
804 p
= get_opt_name(option
, sizeof(option
), p
, ',');
805 if (strncmp(option
, "no", 2) == 0) {
806 memmove(option
, option
+2, strlen(option
+2)+1);
807 pstrcpy(value
, sizeof(value
), "off");
809 pstrcpy(value
, sizeof(value
), "on");
813 /* found "foo=bar,more" */
814 p
= get_opt_name(option
, sizeof(option
), p
, '=');
819 p
= get_opt_value(value
, sizeof(value
), p
);
821 if (strcmp(option
, "id") != 0) {
822 /* store and parse */
823 opt_set(opts
, option
, value
, prepend
, &local_err
);
825 error_propagate(errp
, local_err
);
836 * Store options parsed from @params into @opts.
837 * If @firstname is non-null, the first key=value in @params may omit
838 * key=, and is treated as if key was @firstname.
839 * On error, store an error object through @errp if non-null.
841 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
842 const char *firstname
, Error
**errp
)
844 opts_do_parse(opts
, params
, firstname
, false, errp
);
847 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
848 bool permit_abbrev
, bool defaults
, Error
**errp
)
850 const char *firstname
;
851 char value
[1024], *id
= NULL
;
854 Error
*local_err
= NULL
;
856 assert(!permit_abbrev
|| list
->implied_opt_name
);
857 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
859 if (strncmp(params
, "id=", 3) == 0) {
860 get_opt_value(value
, sizeof(value
), params
+3);
862 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
863 get_opt_value(value
, sizeof(value
), p
+4);
868 * This code doesn't work for defaults && !list->merge_lists: when
869 * params has no id=, and list has an element with !opts->id, it
870 * appends a new element instead of returning the existing opts.
871 * However, we got no use for this case. Guard against possible
872 * (if unlikely) future misuse:
874 assert(!defaults
|| list
->merge_lists
);
875 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
877 error_propagate(errp
, local_err
);
881 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
883 error_propagate(errp
, local_err
);
892 * Create a QemuOpts in @list and with options parsed from @params.
893 * If @permit_abbrev, the first key=value in @params may omit key=,
894 * and is treated as if key was @list->implied_opt_name.
895 * On error, store an error object through @errp if non-null.
896 * Return the new QemuOpts on success, null pointer on error.
898 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
899 bool permit_abbrev
, Error
**errp
)
901 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
905 * Create a QemuOpts in @list and with options parsed from @params.
906 * If @permit_abbrev, the first key=value in @params may omit key=,
907 * and is treated as if key was @list->implied_opt_name.
908 * Report errors with error_report_err(). This is inappropriate in
909 * QMP context. Do not use this function there!
910 * Return the new QemuOpts on success, null pointer on error.
912 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
918 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
920 error_report_err(err
);
925 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
930 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
934 typedef struct OptsFromQDictState
{
937 } OptsFromQDictState
;
939 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
941 OptsFromQDictState
*state
= opaque
;
946 if (!strcmp(key
, "id") || *state
->errp
) {
950 switch (qobject_type(obj
)) {
952 value
= qstring_get_str(qobject_to_qstring(obj
));
955 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
956 qint_get_int(qobject_to_qint(obj
)));
957 assert(n
< sizeof(buf
));
961 n
= snprintf(buf
, sizeof(buf
), "%.17g",
962 qfloat_get_double(qobject_to_qfloat(obj
)));
963 assert(n
< sizeof(buf
));
967 pstrcpy(buf
, sizeof(buf
),
968 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
975 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
979 * Create QemuOpts from a QDict.
980 * Use value of key "id" as ID if it exists and is a QString.
981 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
982 * other types are silently ignored.
984 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
987 OptsFromQDictState state
;
988 Error
*local_err
= NULL
;
991 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
994 error_propagate(errp
, local_err
);
998 assert(opts
!= NULL
);
1000 state
.errp
= &local_err
;
1002 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1004 error_propagate(errp
, local_err
);
1005 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 void 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 Error
*local_err
= NULL
;
1025 OptsFromQDictState state
= {
1030 next
= qdict_next(qdict
, entry
);
1032 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1033 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1035 error_propagate(errp
, local_err
);
1038 qdict_del(qdict
, entry
->key
);
1047 * Convert from QemuOpts to QDict.
1048 * The QDict values are of type QString.
1049 * TODO We'll want to use types appropriate for opt->desc->type, but
1050 * this is enough for now.
1052 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1058 qdict
= qdict_new();
1061 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1063 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1064 val
= QOBJECT(qstring_from_str(opt
->str
));
1065 qdict_put_obj(qdict
, opt
->name
, val
);
1070 /* Validate parsed opts against descriptions where no
1071 * descriptions were provided in the QemuOptsList.
1073 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1076 Error
*local_err
= NULL
;
1078 assert(opts_accepts_any(opts
));
1080 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1081 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1083 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1087 qemu_opt_parse(opt
, &local_err
);
1089 error_propagate(errp
, local_err
);
1096 * For each member of @list, call @func(@opaque, member, @errp).
1097 * Call it with the current location temporarily set to the member's.
1098 * @func() may store an Error through @errp, but must return non-zero then.
1099 * When @func() returns non-zero, break the loop and return that value.
1100 * Return zero when the loop completes.
1102 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1103 void *opaque
, Error
**errp
)
1109 loc_push_none(&loc
);
1110 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1111 loc_restore(&opts
->loc
);
1112 rc
= func(opaque
, opts
, errp
);
1116 assert(!errp
|| !*errp
);
1122 static size_t count_opts_list(QemuOptsList
*list
)
1124 QemuOptDesc
*desc
= NULL
;
1125 size_t num_opts
= 0;
1132 while (desc
&& desc
->name
) {
1140 void qemu_opts_free(QemuOptsList
*list
)
1145 /* Realloc dst option list and append options from an option list (list)
1146 * to it. dst could be NULL or a malloced list.
1147 * The lifetime of dst must be shorter than the input list because the
1148 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1150 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1153 size_t num_opts
, num_dst_opts
;
1155 bool need_init
= false;
1156 bool need_head_update
;
1162 /* If dst is NULL, after realloc, some area of dst should be initialized
1163 * before adding options to it.
1167 need_head_update
= true;
1169 /* Moreover, even if dst is not NULL, the realloc may move it to a
1170 * different address in which case we may get a stale tail pointer
1172 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1175 num_opts
= count_opts_list(dst
);
1176 num_dst_opts
= num_opts
;
1177 num_opts
+= count_opts_list(list
);
1178 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1179 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1182 dst
->implied_opt_name
= NULL
;
1183 dst
->merge_lists
= false;
1185 if (need_head_update
) {
1186 QTAILQ_INIT(&dst
->head
);
1188 dst
->desc
[num_dst_opts
].name
= NULL
;
1190 /* append list->desc to dst->desc */
1193 while (desc
&& desc
->name
) {
1194 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1195 dst
->desc
[num_dst_opts
++] = *desc
;
1196 dst
->desc
[num_dst_opts
].name
= NULL
;