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
);
126 int check_params(char *buf
, int buf_size
,
127 const char * const *params
, const char *str
)
134 p
= get_opt_name(buf
, buf_size
, p
, '=');
139 for (i
= 0; params
[i
] != NULL
; i
++) {
140 if (!strcmp(params
[i
], buf
)) {
144 if (params
[i
] == NULL
) {
147 p
= get_opt_value(NULL
, 0, p
);
157 * Searches an option list for an option with the given name
159 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
162 while (list
&& list
->name
) {
163 if (!strcmp(list
->name
, name
)) {
172 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
176 if (!strcmp(value
, "on")) {
178 } else if (!strcmp(value
, "off")) {
181 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, name
, "'on' or 'off'");
188 static void parse_option_number(const char *name
, const char *value
,
189 uint64_t *ret
, Error
**errp
)
195 number
= strtoull(value
, &postfix
, 0);
196 if (*postfix
!= '\0') {
197 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
202 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
206 static void parse_option_size(const char *name
, const char *value
,
207 uint64_t *ret
, Error
**errp
)
213 sizef
= strtod(value
, &postfix
);
230 *ret
= (uint64_t) sizef
;
233 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
234 #if 0 /* conversion from qerror_report() to error_set() broke this: */
235 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
236 "kilobytes, megabytes, gigabytes and terabytes.\n");
241 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
246 * Sets the value of a parameter in a given option list. The parsing of the
247 * value depends on the type of option:
249 * OPT_FLAG (uses value.n):
250 * If no value is given, the flag is set to 1.
251 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
253 * OPT_STRING (uses value.s):
254 * value is strdup()ed and assigned as option value
256 * OPT_SIZE (uses value.n):
257 * The value is converted to an integer. Suffixes for kilobytes etc. are
258 * allowed (powers of 1024).
260 * Returns 0 on succes, -1 in error cases
262 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
266 Error
*local_err
= NULL
;
268 // Find a matching parameter
269 list
= get_option_parameter(list
, name
);
271 fprintf(stderr
, "Unknown option '%s'\n", name
);
276 switch (list
->type
) {
278 parse_option_bool(name
, value
, &flag
, &local_err
);
279 if (!error_is_set(&local_err
)) {
280 list
->value
.n
= flag
;
286 list
->value
.s
= g_strdup(value
);
288 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
294 parse_option_size(name
, value
, &list
->value
.n
, &local_err
);
298 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
302 if (error_is_set(&local_err
)) {
303 qerror_report_err(local_err
);
304 error_free(local_err
);
312 * Sets the given parameter to an integer instead of a string.
313 * This function cannot be used to set string options.
315 * Returns 0 on success, -1 in error cases
317 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
320 // Find a matching parameter
321 list
= get_option_parameter(list
, name
);
323 fprintf(stderr
, "Unknown option '%s'\n", name
);
328 switch (list
->type
) {
332 list
->value
.n
= value
;
343 * Frees a option list. If it contains strings, the strings are freed as well.
345 void free_option_parameters(QEMUOptionParameter
*list
)
347 QEMUOptionParameter
*cur
= list
;
349 while (cur
&& cur
->name
) {
350 if (cur
->type
== OPT_STRING
) {
351 g_free(cur
->value
.s
);
360 * Count valid options in list
362 static size_t count_option_parameters(QEMUOptionParameter
*list
)
364 size_t num_options
= 0;
366 while (list
&& list
->name
) {
375 * Append an option list (list) to an option list (dest).
377 * If dest is NULL, a new copy of list is created.
379 * Returns a pointer to the first element of dest (or the newly allocated copy)
381 QEMUOptionParameter
*append_option_parameters(QEMUOptionParameter
*dest
,
382 QEMUOptionParameter
*list
)
384 size_t num_options
, num_dest_options
;
386 num_options
= count_option_parameters(dest
);
387 num_dest_options
= num_options
;
389 num_options
+= count_option_parameters(list
);
391 dest
= g_realloc(dest
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
392 dest
[num_dest_options
].name
= NULL
;
394 while (list
&& list
->name
) {
395 if (get_option_parameter(dest
, list
->name
) == NULL
) {
396 dest
[num_dest_options
++] = *list
;
397 dest
[num_dest_options
].name
= NULL
;
406 * Parses a parameter string (param) into an option list (dest).
408 * list is the template option list. If dest is NULL, a new copy of list is
409 * created. If list is NULL, this function fails.
411 * A parameter string consists of one or more parameters, separated by commas.
412 * Each parameter consists of its name and possibly of a value. In the latter
413 * case, the value is delimited by an = character. To specify a value which
414 * contains commas, double each comma so it won't be recognized as the end of
417 * For more details of the parsing see above.
419 * Returns a pointer to the first element of dest (or the newly allocated copy)
420 * or NULL in error cases
422 QEMUOptionParameter
*parse_option_parameters(const char *param
,
423 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
425 QEMUOptionParameter
*allocated
= NULL
;
428 char *param_delim
, *value_delim
;
436 dest
= allocated
= append_option_parameters(NULL
, list
);
441 // Find parameter name and value in the string
442 param_delim
= strchr(param
, ',');
443 value_delim
= strchr(param
, '=');
445 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
452 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
454 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
456 if (*param
!= '\0') {
461 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
469 // Only free the list if it was newly allocated
470 free_option_parameters(allocated
);
475 * Prints all options of a list that have a value to stdout
477 void print_option_parameters(QEMUOptionParameter
*list
)
479 while (list
&& list
->name
) {
480 switch (list
->type
) {
482 if (list
->value
.s
!= NULL
) {
483 printf("%s='%s' ", list
->name
, list
->value
.s
);
487 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
491 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
494 printf("%s=(unknown type) ", list
->name
);
502 * Prints an overview of all available options
504 void print_option_help(QEMUOptionParameter
*list
)
506 printf("Supported options:\n");
507 while (list
&& list
->name
) {
508 printf("%-16s %s\n", list
->name
,
509 list
->help
? list
->help
: "No description available");
514 /* ------------------------------------------------------------------ */
516 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
520 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
521 if (strcmp(opt
->name
, name
) != 0)
528 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
530 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
531 return opt
? opt
->str
: NULL
;
534 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
538 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
539 if (is_help_option(opt
->name
)) {
546 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
548 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
552 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
553 return opt
->value
.boolean
;
556 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
558 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
562 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
563 return opt
->value
.uint
;
566 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
568 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
572 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
573 return opt
->value
.uint
;
576 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
578 if (opt
->desc
== NULL
)
581 switch (opt
->desc
->type
) {
582 case QEMU_OPT_STRING
:
586 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
588 case QEMU_OPT_NUMBER
:
589 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
592 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
599 static void qemu_opt_del(QemuOpt
*opt
)
601 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
602 g_free((/* !const */ char*)opt
->name
);
603 g_free((/* !const */ char*)opt
->str
);
607 static bool opts_accepts_any(const QemuOpts
*opts
)
609 return opts
->list
->desc
[0].name
== NULL
;
612 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
617 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
618 if (strcmp(desc
[i
].name
, name
) == 0) {
626 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
627 bool prepend
, Error
**errp
)
630 const QemuOptDesc
*desc
;
631 Error
*local_err
= NULL
;
633 desc
= find_desc_by_name(opts
->list
->desc
, name
);
634 if (!desc
&& !opts_accepts_any(opts
)) {
635 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
639 opt
= g_malloc0(sizeof(*opt
));
640 opt
->name
= g_strdup(name
);
643 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
645 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
648 opt
->str
= g_strdup(value
);
649 qemu_opt_parse(opt
, &local_err
);
650 if (error_is_set(&local_err
)) {
651 error_propagate(errp
, local_err
);
656 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
658 Error
*local_err
= NULL
;
660 opt_set(opts
, name
, value
, false, &local_err
);
661 if (error_is_set(&local_err
)) {
662 qerror_report_err(local_err
);
663 error_free(local_err
);
670 void qemu_opt_set_err(QemuOpts
*opts
, const char *name
, const char *value
,
673 opt_set(opts
, name
, value
, false, errp
);
676 int qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
)
679 const QemuOptDesc
*desc
= opts
->list
->desc
;
681 opt
= g_malloc0(sizeof(*opt
));
682 opt
->desc
= find_desc_by_name(desc
, name
);
683 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
684 qerror_report(QERR_INVALID_PARAMETER
, name
);
689 opt
->name
= g_strdup(name
);
691 opt
->value
.boolean
= !!val
;
692 opt
->str
= g_strdup(val
? "on" : "off");
693 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
698 int qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
)
701 const QemuOptDesc
*desc
= opts
->list
->desc
;
703 opt
= g_malloc0(sizeof(*opt
));
704 opt
->desc
= find_desc_by_name(desc
, name
);
705 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
706 qerror_report(QERR_INVALID_PARAMETER
, name
);
711 opt
->name
= g_strdup(name
);
713 opt
->value
.uint
= val
;
714 opt
->str
= g_strdup_printf("%" PRId64
, val
);
715 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
720 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
721 int abort_on_failure
)
726 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
727 rc
= func(opt
->name
, opt
->str
, opaque
);
728 if (abort_on_failure
&& rc
!= 0)
734 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
738 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
745 if (strcmp(opts
->id
, id
) != 0) {
753 static int id_wellformed(const char *id
)
757 if (!qemu_isalpha(id
[0])) {
760 for (i
= 1; id
[i
]; i
++) {
761 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
768 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
769 int fail_if_exists
, Error
**errp
)
771 QemuOpts
*opts
= NULL
;
774 if (!id_wellformed(id
)) {
775 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
776 #if 0 /* conversion from qerror_report() to error_set() broke this: */
777 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
781 opts
= qemu_opts_find(list
, id
);
783 if (fail_if_exists
&& !list
->merge_lists
) {
784 error_set(errp
, QERR_DUPLICATE_ID
, id
, list
->name
);
790 } else if (list
->merge_lists
) {
791 opts
= qemu_opts_find(list
, NULL
);
796 opts
= g_malloc0(sizeof(*opts
));
797 opts
->id
= g_strdup(id
);
799 loc_save(&opts
->loc
);
800 QTAILQ_INIT(&opts
->head
);
801 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
805 QemuOpts
*qemu_opts_create_nofail(QemuOptsList
*list
)
809 opts
= qemu_opts_create(list
, NULL
, 0, &errp
);
810 assert_no_error(errp
);
814 void qemu_opts_reset(QemuOptsList
*list
)
816 QemuOpts
*opts
, *next_opts
;
818 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
823 void qemu_opts_loc_restore(QemuOpts
*opts
)
825 loc_restore(&opts
->loc
);
828 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
829 const char *name
, const char *value
)
832 Error
*local_err
= NULL
;
834 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
835 if (error_is_set(&local_err
)) {
836 qerror_report_err(local_err
);
837 error_free(local_err
);
840 return qemu_opt_set(opts
, name
, value
);
843 const char *qemu_opts_id(QemuOpts
*opts
)
848 void qemu_opts_del(QemuOpts
*opts
)
853 opt
= QTAILQ_FIRST(&opts
->head
);
858 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
863 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
867 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
868 opts
->id
? opts
->id
: "<noid>");
869 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
870 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
872 fprintf(stderr
, "\n");
876 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
877 const char *firstname
, bool prepend
)
879 char option
[128], value
[1024];
880 const char *p
,*pe
,*pc
;
881 Error
*local_err
= NULL
;
883 for (p
= params
; *p
!= '\0'; p
++) {
886 if (!pe
|| (pc
&& pc
< pe
)) {
887 /* found "foo,more" */
888 if (p
== params
&& firstname
) {
889 /* implicitly named first option */
890 pstrcpy(option
, sizeof(option
), firstname
);
891 p
= get_opt_value(value
, sizeof(value
), p
);
893 /* option without value, probably a flag */
894 p
= get_opt_name(option
, sizeof(option
), p
, ',');
895 if (strncmp(option
, "no", 2) == 0) {
896 memmove(option
, option
+2, strlen(option
+2)+1);
897 pstrcpy(value
, sizeof(value
), "off");
899 pstrcpy(value
, sizeof(value
), "on");
903 /* found "foo=bar,more" */
904 p
= get_opt_name(option
, sizeof(option
), p
, '=');
909 p
= get_opt_value(value
, sizeof(value
), p
);
911 if (strcmp(option
, "id") != 0) {
912 /* store and parse */
913 opt_set(opts
, option
, value
, prepend
, &local_err
);
914 if (error_is_set(&local_err
)) {
915 qerror_report_err(local_err
);
916 error_free(local_err
);
927 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
929 return opts_do_parse(opts
, params
, firstname
, false);
932 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
933 int permit_abbrev
, bool defaults
)
935 const char *firstname
;
936 char value
[1024], *id
= NULL
;
939 Error
*local_err
= NULL
;
941 assert(!permit_abbrev
|| list
->implied_opt_name
);
942 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
944 if (strncmp(params
, "id=", 3) == 0) {
945 get_opt_value(value
, sizeof(value
), params
+3);
947 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
948 get_opt_value(value
, sizeof(value
), p
+4);
952 if (!id
&& !QTAILQ_EMPTY(&list
->head
)) {
953 opts
= qemu_opts_find(list
, NULL
);
955 opts
= qemu_opts_create(list
, id
, 0, &local_err
);
958 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
961 if (error_is_set(&local_err
)) {
962 qerror_report_err(local_err
);
963 error_free(local_err
);
968 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
976 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
979 return opts_parse(list
, params
, permit_abbrev
, false);
982 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
987 opts
= opts_parse(list
, params
, permit_abbrev
, true);
991 typedef struct OptsFromQDictState
{
994 } OptsFromQDictState
;
996 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
998 OptsFromQDictState
*state
= opaque
;
1003 if (!strcmp(key
, "id") || error_is_set(state
->errp
)) {
1007 switch (qobject_type(obj
)) {
1009 value
= qstring_get_str(qobject_to_qstring(obj
));
1012 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
1013 qint_get_int(qobject_to_qint(obj
)));
1014 assert(n
< sizeof(buf
));
1018 n
= snprintf(buf
, sizeof(buf
), "%.17g",
1019 qfloat_get_double(qobject_to_qfloat(obj
)));
1020 assert(n
< sizeof(buf
));
1024 pstrcpy(buf
, sizeof(buf
),
1025 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
1032 qemu_opt_set_err(state
->opts
, key
, value
, state
->errp
);
1036 * Create QemuOpts from a QDict.
1037 * Use value of key "id" as ID if it exists and is a QString.
1038 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1039 * other types are silently ignored.
1041 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1044 OptsFromQDictState state
;
1045 Error
*local_err
= NULL
;
1048 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1050 if (error_is_set(&local_err
)) {
1051 error_propagate(errp
, local_err
);
1055 assert(opts
!= NULL
);
1057 state
.errp
= &local_err
;
1059 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1060 if (error_is_set(&local_err
)) {
1061 error_propagate(errp
, local_err
);
1062 qemu_opts_del(opts
);
1070 * Adds all QDict entries to the QemuOpts that can be added and removes them
1071 * from the QDict. When this function returns, the QDict contains only those
1072 * entries that couldn't be added to the QemuOpts.
1074 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1076 const QDictEntry
*entry
, *next
;
1078 entry
= qdict_first(qdict
);
1080 while (entry
!= NULL
) {
1081 Error
*local_err
= NULL
;
1082 OptsFromQDictState state
= {
1087 next
= qdict_next(qdict
, entry
);
1089 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1090 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1091 if (error_is_set(&local_err
)) {
1092 error_propagate(errp
, local_err
);
1095 qdict_del(qdict
, entry
->key
);
1104 * Convert from QemuOpts to QDict.
1105 * The QDict values are of type QString.
1106 * TODO We'll want to use types appropriate for opt->desc->type, but
1107 * this is enough for now.
1109 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1115 qdict
= qdict_new();
1118 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1120 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1121 val
= QOBJECT(qstring_from_str(opt
->str
));
1122 qdict_put_obj(qdict
, opt
->name
, val
);
1127 /* Validate parsed opts against descriptions where no
1128 * descriptions were provided in the QemuOptsList.
1130 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1133 Error
*local_err
= NULL
;
1135 assert(opts_accepts_any(opts
));
1137 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1138 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1140 error_set(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1144 qemu_opt_parse(opt
, &local_err
);
1145 if (error_is_set(&local_err
)) {
1146 error_propagate(errp
, local_err
);
1152 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1153 int abort_on_failure
)
1159 loc_push_none(&loc
);
1160 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1161 loc_restore(&opts
->loc
);
1162 rc
|= func(opts
, opaque
);
1163 if (abort_on_failure
&& rc
!= 0)