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
29 #include "qemu-common.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/types.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qemu/option_int.h"
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
40 * The option name is delimited by delim (usually , or =) or the string end
41 * and is copied into buf. If the option name is longer than buf_size, it is
42 * truncated. buf is always zero terminated.
44 * The return value is the position of the delimiter/zero byte after the option
47 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
52 while (*p
!= '\0' && *p
!= delim
) {
53 if (q
&& (q
- buf
) < buf_size
- 1)
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
67 * This function is comparable to get_opt_name with the difference that the
68 * delimiter is fixed to be comma which starts a new option. To specify an
69 * option value that contains commas, double each comma.
71 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
82 if (q
&& (q
- buf
) < buf_size
- 1)
92 int get_next_param_value(char *buf
, int buf_size
,
93 const char *tag
, const char **pstr
)
100 p
= get_opt_name(option
, sizeof(option
), p
, '=');
104 if (!strcmp(tag
, option
)) {
105 *pstr
= get_opt_value(buf
, buf_size
, p
);
111 p
= get_opt_value(NULL
, 0, p
);
120 int get_param_value(char *buf
, int buf_size
,
121 const char *tag
, const char *str
)
123 return get_next_param_value(buf
, buf_size
, tag
, &str
);
127 * Searches an option list for an option with the given name
129 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
132 while (list
&& list
->name
) {
133 if (!strcmp(list
->name
, name
)) {
142 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
146 if (!strcmp(value
, "on")) {
148 } else if (!strcmp(value
, "off")) {
151 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, name
, "'on' or 'off'");
158 static void parse_option_number(const char *name
, const char *value
,
159 uint64_t *ret
, Error
**errp
)
165 number
= strtoull(value
, &postfix
, 0);
166 if (*postfix
!= '\0') {
167 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
172 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
176 static void parse_option_size(const char *name
, const char *value
,
177 uint64_t *ret
, Error
**errp
)
183 sizef
= strtod(value
, &postfix
);
200 *ret
= (uint64_t) sizef
;
203 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
204 #if 0 /* conversion from qerror_report() to error_set() broke this: */
205 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
206 "kilobytes, megabytes, gigabytes and terabytes.\n");
211 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
216 * Sets the value of a parameter in a given option list. The parsing of the
217 * value depends on the type of option:
219 * OPT_FLAG (uses value.n):
220 * If no value is given, the flag is set to 1.
221 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
223 * OPT_STRING (uses value.s):
224 * value is strdup()ed and assigned as option value
226 * OPT_SIZE (uses value.n):
227 * The value is converted to an integer. Suffixes for kilobytes etc. are
228 * allowed (powers of 1024).
230 * Returns 0 on succes, -1 in error cases
232 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
236 Error
*local_err
= NULL
;
238 // Find a matching parameter
239 list
= get_option_parameter(list
, name
);
241 fprintf(stderr
, "Unknown option '%s'\n", name
);
246 switch (list
->type
) {
248 parse_option_bool(name
, value
, &flag
, &local_err
);
249 if (!error_is_set(&local_err
)) {
250 list
->value
.n
= flag
;
256 list
->value
.s
= g_strdup(value
);
258 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
264 parse_option_size(name
, value
, &list
->value
.n
, &local_err
);
268 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
272 if (error_is_set(&local_err
)) {
273 qerror_report_err(local_err
);
274 error_free(local_err
);
282 * Sets the given parameter to an integer instead of a string.
283 * This function cannot be used to set string options.
285 * Returns 0 on success, -1 in error cases
287 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
290 // Find a matching parameter
291 list
= get_option_parameter(list
, name
);
293 fprintf(stderr
, "Unknown option '%s'\n", name
);
298 switch (list
->type
) {
302 list
->value
.n
= value
;
313 * Frees a option list. If it contains strings, the strings are freed as well.
315 void free_option_parameters(QEMUOptionParameter
*list
)
317 QEMUOptionParameter
*cur
= list
;
319 while (cur
&& cur
->name
) {
320 if (cur
->type
== OPT_STRING
) {
321 g_free(cur
->value
.s
);
330 * Count valid options in list
332 static size_t count_option_parameters(QEMUOptionParameter
*list
)
334 size_t num_options
= 0;
336 while (list
&& list
->name
) {
345 * Append an option list (list) to an option list (dest).
347 * If dest is NULL, a new copy of list is created.
349 * Returns a pointer to the first element of dest (or the newly allocated copy)
351 QEMUOptionParameter
*append_option_parameters(QEMUOptionParameter
*dest
,
352 QEMUOptionParameter
*list
)
354 size_t num_options
, num_dest_options
;
356 num_options
= count_option_parameters(dest
);
357 num_dest_options
= num_options
;
359 num_options
+= count_option_parameters(list
);
361 dest
= g_realloc(dest
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
362 dest
[num_dest_options
].name
= NULL
;
364 while (list
&& list
->name
) {
365 if (get_option_parameter(dest
, list
->name
) == NULL
) {
366 dest
[num_dest_options
++] = *list
;
367 dest
[num_dest_options
].name
= NULL
;
376 * Parses a parameter string (param) into an option list (dest).
378 * list is the template option list. If dest is NULL, a new copy of list is
379 * created. If list is NULL, this function fails.
381 * A parameter string consists of one or more parameters, separated by commas.
382 * Each parameter consists of its name and possibly of a value. In the latter
383 * case, the value is delimited by an = character. To specify a value which
384 * contains commas, double each comma so it won't be recognized as the end of
387 * For more details of the parsing see above.
389 * Returns a pointer to the first element of dest (or the newly allocated copy)
390 * or NULL in error cases
392 QEMUOptionParameter
*parse_option_parameters(const char *param
,
393 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
395 QEMUOptionParameter
*allocated
= NULL
;
398 char *param_delim
, *value_delim
;
406 dest
= allocated
= append_option_parameters(NULL
, list
);
411 // Find parameter name and value in the string
412 param_delim
= strchr(param
, ',');
413 value_delim
= strchr(param
, '=');
415 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
422 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
424 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
426 if (*param
!= '\0') {
431 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
439 // Only free the list if it was newly allocated
440 free_option_parameters(allocated
);
445 * Prints all options of a list that have a value to stdout
447 void print_option_parameters(QEMUOptionParameter
*list
)
449 while (list
&& list
->name
) {
450 switch (list
->type
) {
452 if (list
->value
.s
!= NULL
) {
453 printf("%s='%s' ", list
->name
, list
->value
.s
);
457 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
461 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
464 printf("%s=(unknown type) ", list
->name
);
472 * Prints an overview of all available options
474 void print_option_help(QEMUOptionParameter
*list
)
476 printf("Supported options:\n");
477 while (list
&& list
->name
) {
478 printf("%-16s %s\n", list
->name
,
479 list
->help
? list
->help
: "No description available");
484 /* ------------------------------------------------------------------ */
486 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
490 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
491 if (strcmp(opt
->name
, name
) != 0)
498 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
500 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
501 return opt
? opt
->str
: NULL
;
504 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
508 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
509 if (is_help_option(opt
->name
)) {
516 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
518 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
522 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
523 return opt
->value
.boolean
;
526 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
528 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
532 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
533 return opt
->value
.uint
;
536 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
538 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
542 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
543 return opt
->value
.uint
;
546 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
548 if (opt
->desc
== NULL
)
551 switch (opt
->desc
->type
) {
552 case QEMU_OPT_STRING
:
556 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
558 case QEMU_OPT_NUMBER
:
559 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
562 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
569 static void qemu_opt_del(QemuOpt
*opt
)
571 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
572 g_free((/* !const */ char*)opt
->name
);
573 g_free((/* !const */ char*)opt
->str
);
577 static bool opts_accepts_any(const QemuOpts
*opts
)
579 return opts
->list
->desc
[0].name
== NULL
;
582 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
587 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
588 if (strcmp(desc
[i
].name
, name
) == 0) {
596 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
597 bool prepend
, Error
**errp
)
600 const QemuOptDesc
*desc
;
601 Error
*local_err
= NULL
;
603 desc
= find_desc_by_name(opts
->list
->desc
, name
);
604 if (!desc
&& !opts_accepts_any(opts
)) {
605 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
609 opt
= g_malloc0(sizeof(*opt
));
610 opt
->name
= g_strdup(name
);
613 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
615 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
618 opt
->str
= g_strdup(value
);
619 qemu_opt_parse(opt
, &local_err
);
620 if (error_is_set(&local_err
)) {
621 error_propagate(errp
, local_err
);
626 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
628 Error
*local_err
= NULL
;
630 opt_set(opts
, name
, value
, false, &local_err
);
631 if (error_is_set(&local_err
)) {
632 qerror_report_err(local_err
);
633 error_free(local_err
);
640 void qemu_opt_set_err(QemuOpts
*opts
, const char *name
, const char *value
,
643 opt_set(opts
, name
, value
, false, errp
);
646 int qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
)
649 const QemuOptDesc
*desc
= opts
->list
->desc
;
651 opt
= g_malloc0(sizeof(*opt
));
652 opt
->desc
= find_desc_by_name(desc
, name
);
653 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
654 qerror_report(QERR_INVALID_PARAMETER
, name
);
659 opt
->name
= g_strdup(name
);
661 opt
->value
.boolean
= !!val
;
662 opt
->str
= g_strdup(val
? "on" : "off");
663 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
668 int qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
)
671 const QemuOptDesc
*desc
= opts
->list
->desc
;
673 opt
= g_malloc0(sizeof(*opt
));
674 opt
->desc
= find_desc_by_name(desc
, name
);
675 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
676 qerror_report(QERR_INVALID_PARAMETER
, name
);
681 opt
->name
= g_strdup(name
);
683 opt
->value
.uint
= val
;
684 opt
->str
= g_strdup_printf("%" PRId64
, val
);
685 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
690 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
691 int abort_on_failure
)
696 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
697 rc
= func(opt
->name
, opt
->str
, opaque
);
698 if (abort_on_failure
&& rc
!= 0)
704 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
708 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
709 if (!opts
->id
&& !id
) {
712 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
719 static int id_wellformed(const char *id
)
723 if (!qemu_isalpha(id
[0])) {
726 for (i
= 1; id
[i
]; i
++) {
727 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
734 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
735 int fail_if_exists
, Error
**errp
)
737 QemuOpts
*opts
= NULL
;
740 if (!id_wellformed(id
)) {
741 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
742 #if 0 /* conversion from qerror_report() to error_set() broke this: */
743 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
747 opts
= qemu_opts_find(list
, id
);
749 if (fail_if_exists
&& !list
->merge_lists
) {
750 error_set(errp
, QERR_DUPLICATE_ID
, id
, list
->name
);
756 } else if (list
->merge_lists
) {
757 opts
= qemu_opts_find(list
, NULL
);
762 opts
= g_malloc0(sizeof(*opts
));
763 opts
->id
= g_strdup(id
);
765 loc_save(&opts
->loc
);
766 QTAILQ_INIT(&opts
->head
);
767 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
771 QemuOpts
*qemu_opts_create_nofail(QemuOptsList
*list
)
775 opts
= qemu_opts_create(list
, NULL
, 0, &errp
);
776 assert_no_error(errp
);
780 void qemu_opts_reset(QemuOptsList
*list
)
782 QemuOpts
*opts
, *next_opts
;
784 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
789 void qemu_opts_loc_restore(QemuOpts
*opts
)
791 loc_restore(&opts
->loc
);
794 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
795 const char *name
, const char *value
)
798 Error
*local_err
= NULL
;
800 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
801 if (error_is_set(&local_err
)) {
802 qerror_report_err(local_err
);
803 error_free(local_err
);
806 return qemu_opt_set(opts
, name
, value
);
809 const char *qemu_opts_id(QemuOpts
*opts
)
814 void qemu_opts_del(QemuOpts
*opts
)
819 opt
= QTAILQ_FIRST(&opts
->head
);
824 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
829 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
833 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
834 opts
->id
? opts
->id
: "<noid>");
835 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
836 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
838 fprintf(stderr
, "\n");
842 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
843 const char *firstname
, bool prepend
)
845 char option
[128], value
[1024];
846 const char *p
,*pe
,*pc
;
847 Error
*local_err
= NULL
;
849 for (p
= params
; *p
!= '\0'; p
++) {
852 if (!pe
|| (pc
&& pc
< pe
)) {
853 /* found "foo,more" */
854 if (p
== params
&& firstname
) {
855 /* implicitly named first option */
856 pstrcpy(option
, sizeof(option
), firstname
);
857 p
= get_opt_value(value
, sizeof(value
), p
);
859 /* option without value, probably a flag */
860 p
= get_opt_name(option
, sizeof(option
), p
, ',');
861 if (strncmp(option
, "no", 2) == 0) {
862 memmove(option
, option
+2, strlen(option
+2)+1);
863 pstrcpy(value
, sizeof(value
), "off");
865 pstrcpy(value
, sizeof(value
), "on");
869 /* found "foo=bar,more" */
870 p
= get_opt_name(option
, sizeof(option
), p
, '=');
875 p
= get_opt_value(value
, sizeof(value
), p
);
877 if (strcmp(option
, "id") != 0) {
878 /* store and parse */
879 opt_set(opts
, option
, value
, prepend
, &local_err
);
880 if (error_is_set(&local_err
)) {
881 qerror_report_err(local_err
);
882 error_free(local_err
);
893 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
895 return opts_do_parse(opts
, params
, firstname
, false);
898 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
899 int permit_abbrev
, bool defaults
)
901 const char *firstname
;
902 char value
[1024], *id
= NULL
;
905 Error
*local_err
= NULL
;
907 assert(!permit_abbrev
|| list
->implied_opt_name
);
908 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
910 if (strncmp(params
, "id=", 3) == 0) {
911 get_opt_value(value
, sizeof(value
), params
+3);
913 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
914 get_opt_value(value
, sizeof(value
), p
+4);
917 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
919 if (error_is_set(&local_err
)) {
920 qerror_report_err(local_err
);
921 error_free(local_err
);
926 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
934 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
937 return opts_parse(list
, params
, permit_abbrev
, false);
940 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
945 opts
= opts_parse(list
, params
, permit_abbrev
, true);
949 typedef struct OptsFromQDictState
{
952 } OptsFromQDictState
;
954 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
956 OptsFromQDictState
*state
= opaque
;
961 if (!strcmp(key
, "id") || error_is_set(state
->errp
)) {
965 switch (qobject_type(obj
)) {
967 value
= qstring_get_str(qobject_to_qstring(obj
));
970 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
971 qint_get_int(qobject_to_qint(obj
)));
972 assert(n
< sizeof(buf
));
976 n
= snprintf(buf
, sizeof(buf
), "%.17g",
977 qfloat_get_double(qobject_to_qfloat(obj
)));
978 assert(n
< sizeof(buf
));
982 pstrcpy(buf
, sizeof(buf
),
983 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
990 qemu_opt_set_err(state
->opts
, key
, value
, state
->errp
);
994 * Create QemuOpts from a QDict.
995 * Use value of key "id" as ID if it exists and is a QString.
996 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
997 * other types are silently ignored.
999 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1002 OptsFromQDictState state
;
1003 Error
*local_err
= NULL
;
1006 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1008 if (error_is_set(&local_err
)) {
1009 error_propagate(errp
, local_err
);
1013 assert(opts
!= NULL
);
1015 state
.errp
= &local_err
;
1017 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1018 if (error_is_set(&local_err
)) {
1019 error_propagate(errp
, local_err
);
1020 qemu_opts_del(opts
);
1028 * Adds all QDict entries to the QemuOpts that can be added and removes them
1029 * from the QDict. When this function returns, the QDict contains only those
1030 * entries that couldn't be added to the QemuOpts.
1032 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1034 const QDictEntry
*entry
, *next
;
1036 entry
= qdict_first(qdict
);
1038 while (entry
!= NULL
) {
1039 Error
*local_err
= NULL
;
1040 OptsFromQDictState state
= {
1045 next
= qdict_next(qdict
, entry
);
1047 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1048 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1049 if (error_is_set(&local_err
)) {
1050 error_propagate(errp
, local_err
);
1053 qdict_del(qdict
, entry
->key
);
1062 * Convert from QemuOpts to QDict.
1063 * The QDict values are of type QString.
1064 * TODO We'll want to use types appropriate for opt->desc->type, but
1065 * this is enough for now.
1067 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1073 qdict
= qdict_new();
1076 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1078 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1079 val
= QOBJECT(qstring_from_str(opt
->str
));
1080 qdict_put_obj(qdict
, opt
->name
, val
);
1085 /* Validate parsed opts against descriptions where no
1086 * descriptions were provided in the QemuOptsList.
1088 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1091 Error
*local_err
= NULL
;
1093 assert(opts_accepts_any(opts
));
1095 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1096 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1098 error_set(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1102 qemu_opt_parse(opt
, &local_err
);
1103 if (error_is_set(&local_err
)) {
1104 error_propagate(errp
, local_err
);
1110 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1111 int abort_on_failure
)
1117 loc_push_none(&loc
);
1118 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1119 loc_restore(&opts
->loc
);
1120 rc
|= func(opts
, opaque
);
1121 if (abort_on_failure
&& rc
!= 0)