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 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
130 if (!strcmp(value
, "on")) {
132 } else if (!strcmp(value
, "off")) {
135 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
136 name
, "'on' or 'off'");
143 static void parse_option_number(const char *name
, const char *value
,
144 uint64_t *ret
, Error
**errp
)
150 number
= strtoull(value
, &postfix
, 0);
151 if (*postfix
!= '\0') {
152 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
157 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
161 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
166 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
167 if (strcmp(desc
[i
].name
, name
) == 0) {
175 void parse_option_size(const char *name
, const char *value
,
176 uint64_t *ret
, Error
**errp
)
182 sizef
= strtod(value
, &postfix
);
183 if (sizef
< 0 || sizef
> UINT64_MAX
) {
184 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
185 "a non-negative number below 2^64");
204 *ret
= (uint64_t) sizef
;
207 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
208 error_append_hint(errp
, "You may use k, M, G or T suffixes for "
209 "kilobytes, megabytes, gigabytes and terabytes.");
213 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
217 bool has_help_option(const char *param
)
219 size_t buflen
= strlen(param
) + 1;
220 char *buf
= g_malloc(buflen
);
221 const char *p
= param
;
225 p
= get_opt_value(buf
, buflen
, p
);
230 if (is_help_option(buf
)) {
241 bool is_valid_option_list(const char *param
)
243 size_t buflen
= strlen(param
) + 1;
244 char *buf
= g_malloc(buflen
);
245 const char *p
= param
;
249 p
= get_opt_value(buf
, buflen
, p
);
255 if (!*buf
|| *buf
== ',') {
266 void qemu_opts_print_help(QemuOptsList
*list
)
272 printf("Supported options:\n");
273 while (desc
&& desc
->name
) {
274 printf("%-16s %s\n", desc
->name
,
275 desc
->help
? desc
->help
: "No description available");
279 /* ------------------------------------------------------------------ */
281 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
285 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
286 if (strcmp(opt
->name
, name
) != 0)
293 static void qemu_opt_del(QemuOpt
*opt
)
295 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
301 /* qemu_opt_set allows many settings for the same option.
302 * This function deletes all settings for an option.
304 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
306 QemuOpt
*opt
, *next_opt
;
308 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
309 if (!strcmp(opt
->name
, name
)) {
315 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
323 opt
= qemu_opt_find(opts
, name
);
325 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
326 if (desc
&& desc
->def_value_str
) {
327 return desc
->def_value_str
;
330 return opt
? opt
->str
: NULL
;
333 /* Get a known option (or its default) and remove it from the list
334 * all in one action. Return a malloced string of the option value.
335 * Result must be freed by caller with g_free().
337 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
340 const QemuOptDesc
*desc
;
347 opt
= qemu_opt_find(opts
, name
);
349 desc
= find_desc_by_name(opts
->list
->desc
, name
);
350 if (desc
&& desc
->def_value_str
) {
351 str
= g_strdup(desc
->def_value_str
);
357 qemu_opt_del_all(opts
, name
);
361 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
365 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
366 if (is_help_option(opt
->name
)) {
373 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
374 bool defval
, bool del
)
383 opt
= qemu_opt_find(opts
, name
);
385 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
386 if (desc
&& desc
->def_value_str
) {
387 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
391 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
392 ret
= opt
->value
.boolean
;
394 qemu_opt_del_all(opts
, name
);
399 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
401 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
404 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
406 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
409 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
410 uint64_t defval
, bool del
)
413 uint64_t ret
= defval
;
419 opt
= qemu_opt_find(opts
, name
);
421 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
422 if (desc
&& desc
->def_value_str
) {
423 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
427 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
428 ret
= opt
->value
.uint
;
430 qemu_opt_del_all(opts
, name
);
435 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
437 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
440 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
443 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
446 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
447 uint64_t defval
, bool del
)
450 uint64_t ret
= defval
;
456 opt
= qemu_opt_find(opts
, name
);
458 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
459 if (desc
&& desc
->def_value_str
) {
460 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
464 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
465 ret
= opt
->value
.uint
;
467 qemu_opt_del_all(opts
, name
);
472 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
474 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
477 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
480 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
483 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
485 if (opt
->desc
== NULL
)
488 switch (opt
->desc
->type
) {
489 case QEMU_OPT_STRING
:
493 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
495 case QEMU_OPT_NUMBER
:
496 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
499 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
506 static bool opts_accepts_any(const QemuOpts
*opts
)
508 return opts
->list
->desc
[0].name
== NULL
;
511 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
513 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
515 assert(opts_accepts_any(opts
));
525 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
526 bool prepend
, Error
**errp
)
529 const QemuOptDesc
*desc
;
530 Error
*local_err
= NULL
;
532 desc
= find_desc_by_name(opts
->list
->desc
, name
);
533 if (!desc
&& !opts_accepts_any(opts
)) {
534 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
538 opt
= g_malloc0(sizeof(*opt
));
539 opt
->name
= g_strdup(name
);
542 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
544 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
547 opt
->str
= g_strdup(value
);
548 qemu_opt_parse(opt
, &local_err
);
550 error_propagate(errp
, local_err
);
555 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
558 opt_set(opts
, name
, value
, false, errp
);
561 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
565 const QemuOptDesc
*desc
= opts
->list
->desc
;
567 opt
= g_malloc0(sizeof(*opt
));
568 opt
->desc
= find_desc_by_name(desc
, name
);
569 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
570 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
575 opt
->name
= g_strdup(name
);
577 opt
->value
.boolean
= !!val
;
578 opt
->str
= g_strdup(val
? "on" : "off");
579 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
582 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
586 const QemuOptDesc
*desc
= opts
->list
->desc
;
588 opt
= g_malloc0(sizeof(*opt
));
589 opt
->desc
= find_desc_by_name(desc
, name
);
590 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
591 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
596 opt
->name
= g_strdup(name
);
598 opt
->value
.uint
= val
;
599 opt
->str
= g_strdup_printf("%" PRId64
, val
);
600 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
604 * For each member of @opts, call @func(@opaque, name, value, @errp).
605 * @func() may store an Error through @errp, but must return non-zero then.
606 * When @func() returns non-zero, break the loop and return that value.
607 * Return zero when the loop completes.
609 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
615 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
616 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
620 assert(!errp
|| !*errp
);
625 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
629 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
630 if (!opts
->id
&& !id
) {
633 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
640 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
641 int fail_if_exists
, Error
**errp
)
643 QemuOpts
*opts
= NULL
;
646 if (!id_wellformed(id
)) {
647 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
649 error_append_hint(errp
, "Identifiers consist of letters, digits, "
650 "'-', '.', '_', starting with a letter.");
653 opts
= qemu_opts_find(list
, id
);
655 if (fail_if_exists
&& !list
->merge_lists
) {
656 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
662 } else if (list
->merge_lists
) {
663 opts
= qemu_opts_find(list
, NULL
);
668 opts
= g_malloc0(sizeof(*opts
));
669 opts
->id
= g_strdup(id
);
671 loc_save(&opts
->loc
);
672 QTAILQ_INIT(&opts
->head
);
673 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
677 void qemu_opts_reset(QemuOptsList
*list
)
679 QemuOpts
*opts
, *next_opts
;
681 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
686 void qemu_opts_loc_restore(QemuOpts
*opts
)
688 loc_restore(&opts
->loc
);
691 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
692 const char *name
, const char *value
, Error
**errp
)
695 Error
*local_err
= NULL
;
697 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
699 error_propagate(errp
, local_err
);
702 qemu_opt_set(opts
, name
, value
, errp
);
705 const char *qemu_opts_id(QemuOpts
*opts
)
710 /* The id string will be g_free()d by qemu_opts_del */
711 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
716 void qemu_opts_del(QemuOpts
*opts
)
725 opt
= QTAILQ_FIRST(&opts
->head
);
730 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
735 /* print value, escaping any commas in value */
736 static void escaped_print(const char *value
)
740 for (ptr
= value
; *ptr
; ++ptr
) {
748 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
751 QemuOptDesc
*desc
= opts
->list
->desc
;
752 const char *sep
= "";
755 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
759 if (desc
[0].name
== NULL
) {
760 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
761 printf("%s%s=", sep
, opt
->name
);
762 escaped_print(opt
->str
);
767 for (; desc
&& desc
->name
; desc
++) {
769 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
771 value
= opt
? opt
->str
: desc
->def_value_str
;
775 if (desc
->type
== QEMU_OPT_STRING
) {
776 printf("%s%s=", sep
, desc
->name
);
777 escaped_print(value
);
778 } else if ((desc
->type
== QEMU_OPT_SIZE
||
779 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
780 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
782 printf("%s%s=%s", sep
, desc
->name
, value
);
788 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
789 const char *firstname
, bool prepend
, Error
**errp
)
791 char option
[128], value
[1024];
792 const char *p
,*pe
,*pc
;
793 Error
*local_err
= NULL
;
795 /* Initialize data to 0 to avoid warnings from Valgrind. */
796 memset(option
, 0, sizeof(option
));
797 memset(value
, 0, sizeof(value
));
799 for (p
= params
; *p
!= '\0'; p
++) {
802 if (!pe
|| (pc
&& pc
< pe
)) {
803 /* found "foo,more" */
804 if (p
== params
&& firstname
) {
805 /* implicitly named first option */
806 pstrcpy(option
, sizeof(option
), firstname
);
807 p
= get_opt_value(value
, sizeof(value
), p
);
809 /* option without value, probably a flag */
810 p
= get_opt_name(option
, sizeof(option
), p
, ',');
811 if (strncmp(option
, "no", 2) == 0) {
812 memmove(option
, option
+2, strlen(option
+2)+1);
813 pstrcpy(value
, sizeof(value
), "off");
815 pstrcpy(value
, sizeof(value
), "on");
819 /* found "foo=bar,more" */
820 p
= get_opt_name(option
, sizeof(option
), p
, '=');
825 p
= get_opt_value(value
, sizeof(value
), p
);
827 if (strcmp(option
, "id") != 0) {
828 /* store and parse */
829 opt_set(opts
, option
, value
, prepend
, &local_err
);
831 error_propagate(errp
, local_err
);
842 * Store options parsed from @params into @opts.
843 * If @firstname is non-null, the first key=value in @params may omit
844 * key=, and is treated as if key was @firstname.
845 * On error, store an error object through @errp if non-null.
847 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
848 const char *firstname
, Error
**errp
)
850 opts_do_parse(opts
, params
, firstname
, false, errp
);
853 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
854 bool permit_abbrev
, bool defaults
, Error
**errp
)
856 const char *firstname
;
857 char value
[1024], *id
= NULL
;
860 Error
*local_err
= NULL
;
862 assert(!permit_abbrev
|| list
->implied_opt_name
);
863 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
865 if (strncmp(params
, "id=", 3) == 0) {
866 get_opt_value(value
, sizeof(value
), params
+3);
868 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
869 get_opt_value(value
, sizeof(value
), p
+4);
874 * This code doesn't work for defaults && !list->merge_lists: when
875 * params has no id=, and list has an element with !opts->id, it
876 * appends a new element instead of returning the existing opts.
877 * However, we got no use for this case. Guard against possible
878 * (if unlikely) future misuse:
880 assert(!defaults
|| list
->merge_lists
);
881 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
883 error_propagate(errp
, local_err
);
887 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
889 error_propagate(errp
, local_err
);
898 * Create a QemuOpts in @list and with options parsed from @params.
899 * If @permit_abbrev, the first key=value in @params may omit key=,
900 * and is treated as if key was @list->implied_opt_name.
901 * On error, store an error object through @errp if non-null.
902 * Return the new QemuOpts on success, null pointer on error.
904 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
905 bool permit_abbrev
, Error
**errp
)
907 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
911 * Create a QemuOpts in @list and with options parsed from @params.
912 * If @permit_abbrev, the first key=value in @params may omit key=,
913 * and is treated as if key was @list->implied_opt_name.
914 * Report errors with error_report_err(). This is inappropriate in
915 * QMP context. Do not use this function there!
916 * Return the new QemuOpts on success, null pointer on error.
918 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
924 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
926 error_report_err(err
);
931 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
936 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
940 typedef struct OptsFromQDictState
{
943 } OptsFromQDictState
;
945 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
947 OptsFromQDictState
*state
= opaque
;
952 if (!strcmp(key
, "id") || *state
->errp
) {
956 switch (qobject_type(obj
)) {
958 value
= qstring_get_str(qobject_to_qstring(obj
));
961 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
962 qint_get_int(qobject_to_qint(obj
)));
963 assert(n
< sizeof(buf
));
967 n
= snprintf(buf
, sizeof(buf
), "%.17g",
968 qfloat_get_double(qobject_to_qfloat(obj
)));
969 assert(n
< sizeof(buf
));
973 pstrcpy(buf
, sizeof(buf
),
974 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
981 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
985 * Create QemuOpts from a QDict.
986 * Use value of key "id" as ID if it exists and is a QString.
987 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
988 * other types are silently ignored.
990 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
993 OptsFromQDictState state
;
994 Error
*local_err
= NULL
;
997 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1000 error_propagate(errp
, local_err
);
1004 assert(opts
!= NULL
);
1006 state
.errp
= &local_err
;
1008 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1010 error_propagate(errp
, local_err
);
1011 qemu_opts_del(opts
);
1019 * Adds all QDict entries to the QemuOpts that can be added and removes them
1020 * from the QDict. When this function returns, the QDict contains only those
1021 * entries that couldn't be added to the QemuOpts.
1023 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1025 const QDictEntry
*entry
, *next
;
1027 entry
= qdict_first(qdict
);
1029 while (entry
!= NULL
) {
1030 Error
*local_err
= NULL
;
1031 OptsFromQDictState state
= {
1036 next
= qdict_next(qdict
, entry
);
1038 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1039 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1041 error_propagate(errp
, local_err
);
1044 qdict_del(qdict
, entry
->key
);
1053 * Convert from QemuOpts to QDict.
1054 * The QDict values are of type QString.
1055 * TODO We'll want to use types appropriate for opt->desc->type, but
1056 * this is enough for now.
1058 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1064 qdict
= qdict_new();
1067 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1069 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1070 val
= QOBJECT(qstring_from_str(opt
->str
));
1071 qdict_put_obj(qdict
, opt
->name
, val
);
1076 /* Validate parsed opts against descriptions where no
1077 * descriptions were provided in the QemuOptsList.
1079 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1082 Error
*local_err
= NULL
;
1084 assert(opts_accepts_any(opts
));
1086 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1087 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1089 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1093 qemu_opt_parse(opt
, &local_err
);
1095 error_propagate(errp
, local_err
);
1102 * For each member of @list, call @func(@opaque, member, @errp).
1103 * Call it with the current location temporarily set to the member's.
1104 * @func() may store an Error through @errp, but must return non-zero then.
1105 * When @func() returns non-zero, break the loop and return that value.
1106 * Return zero when the loop completes.
1108 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1109 void *opaque
, Error
**errp
)
1115 loc_push_none(&loc
);
1116 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1117 loc_restore(&opts
->loc
);
1118 rc
= func(opaque
, opts
, errp
);
1122 assert(!errp
|| !*errp
);
1128 static size_t count_opts_list(QemuOptsList
*list
)
1130 QemuOptDesc
*desc
= NULL
;
1131 size_t num_opts
= 0;
1138 while (desc
&& desc
->name
) {
1146 void qemu_opts_free(QemuOptsList
*list
)
1151 /* Realloc dst option list and append options from an option list (list)
1152 * to it. dst could be NULL or a malloced list.
1153 * The lifetime of dst must be shorter than the input list because the
1154 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1156 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1159 size_t num_opts
, num_dst_opts
;
1161 bool need_init
= false;
1162 bool need_head_update
;
1168 /* If dst is NULL, after realloc, some area of dst should be initialized
1169 * before adding options to it.
1173 need_head_update
= true;
1175 /* Moreover, even if dst is not NULL, the realloc may move it to a
1176 * different address in which case we may get a stale tail pointer
1178 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1181 num_opts
= count_opts_list(dst
);
1182 num_dst_opts
= num_opts
;
1183 num_opts
+= count_opts_list(list
);
1184 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1185 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1188 dst
->implied_opt_name
= NULL
;
1189 dst
->merge_lists
= false;
1191 if (need_head_update
) {
1192 QTAILQ_INIT(&dst
->head
);
1194 dst
->desc
[num_dst_opts
].name
= NULL
;
1196 /* append list->desc to dst->desc */
1199 while (desc
&& desc
->name
) {
1200 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1201 dst
->desc
[num_dst_opts
++] = *desc
;
1202 dst
->desc
[num_dst_opts
].name
= NULL
;