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
);
199 *ret
= (uint64_t) sizef
;
202 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
203 #if 0 /* conversion from qerror_report() to error_set() broke this: */
204 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
205 "kilobytes, megabytes, gigabytes and terabytes.\n");
210 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
214 bool has_help_option(const char *param
)
216 size_t buflen
= strlen(param
) + 1;
217 char *buf
= g_malloc(buflen
);
218 const char *p
= param
;
222 p
= get_opt_value(buf
, buflen
, p
);
227 if (is_help_option(buf
)) {
238 bool is_valid_option_list(const char *param
)
240 size_t buflen
= strlen(param
) + 1;
241 char *buf
= g_malloc(buflen
);
242 const char *p
= param
;
246 p
= get_opt_value(buf
, buflen
, p
);
252 if (!*buf
|| *buf
== ',') {
263 void qemu_opts_print_help(QemuOptsList
*list
)
269 printf("Supported options:\n");
270 while (desc
&& desc
->name
) {
271 printf("%-16s %s\n", desc
->name
,
272 desc
->help
? desc
->help
: "No description available");
276 /* ------------------------------------------------------------------ */
278 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
282 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
283 if (strcmp(opt
->name
, name
) != 0)
290 static void qemu_opt_del(QemuOpt
*opt
)
292 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
298 /* qemu_opt_set allows many settings for the same option.
299 * This function deletes all settings for an option.
301 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
303 QemuOpt
*opt
, *next_opt
;
305 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
306 if (!strcmp(opt
->name
, name
)) {
312 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
320 opt
= qemu_opt_find(opts
, name
);
322 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
323 if (desc
&& desc
->def_value_str
) {
324 return desc
->def_value_str
;
327 return opt
? opt
->str
: NULL
;
330 /* Get a known option (or its default) and remove it from the list
331 * all in one action. Return a malloced string of the option value.
332 * Result must be freed by caller with g_free().
334 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
337 const QemuOptDesc
*desc
;
344 opt
= qemu_opt_find(opts
, name
);
346 desc
= find_desc_by_name(opts
->list
->desc
, name
);
347 if (desc
&& desc
->def_value_str
) {
348 str
= g_strdup(desc
->def_value_str
);
354 qemu_opt_del_all(opts
, name
);
358 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
362 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
363 if (is_help_option(opt
->name
)) {
370 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
371 bool defval
, bool del
)
380 opt
= qemu_opt_find(opts
, name
);
382 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
383 if (desc
&& desc
->def_value_str
) {
384 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
388 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
389 ret
= opt
->value
.boolean
;
391 qemu_opt_del_all(opts
, name
);
396 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
398 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
401 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
403 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
406 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
407 uint64_t defval
, bool del
)
410 uint64_t ret
= defval
;
416 opt
= qemu_opt_find(opts
, name
);
418 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
419 if (desc
&& desc
->def_value_str
) {
420 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
424 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
425 ret
= opt
->value
.uint
;
427 qemu_opt_del_all(opts
, name
);
432 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
434 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
437 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
440 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
443 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
444 uint64_t defval
, bool del
)
447 uint64_t ret
= defval
;
453 opt
= qemu_opt_find(opts
, name
);
455 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
456 if (desc
&& desc
->def_value_str
) {
457 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
461 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
462 ret
= opt
->value
.uint
;
464 qemu_opt_del_all(opts
, name
);
469 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
471 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
474 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
477 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
480 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
482 if (opt
->desc
== NULL
)
485 switch (opt
->desc
->type
) {
486 case QEMU_OPT_STRING
:
490 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
492 case QEMU_OPT_NUMBER
:
493 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
496 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
503 static bool opts_accepts_any(const QemuOpts
*opts
)
505 return opts
->list
->desc
[0].name
== NULL
;
508 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
510 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
512 assert(opts_accepts_any(opts
));
522 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
523 bool prepend
, Error
**errp
)
526 const QemuOptDesc
*desc
;
527 Error
*local_err
= NULL
;
529 desc
= find_desc_by_name(opts
->list
->desc
, name
);
530 if (!desc
&& !opts_accepts_any(opts
)) {
531 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
535 opt
= g_malloc0(sizeof(*opt
));
536 opt
->name
= g_strdup(name
);
539 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
541 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
544 opt
->str
= g_strdup(value
);
545 qemu_opt_parse(opt
, &local_err
);
547 error_propagate(errp
, local_err
);
552 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
555 opt_set(opts
, name
, value
, false, errp
);
558 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
562 const QemuOptDesc
*desc
= opts
->list
->desc
;
564 opt
= g_malloc0(sizeof(*opt
));
565 opt
->desc
= find_desc_by_name(desc
, name
);
566 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
567 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
572 opt
->name
= g_strdup(name
);
574 opt
->value
.boolean
= !!val
;
575 opt
->str
= g_strdup(val
? "on" : "off");
576 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
579 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
583 const QemuOptDesc
*desc
= opts
->list
->desc
;
585 opt
= g_malloc0(sizeof(*opt
));
586 opt
->desc
= find_desc_by_name(desc
, name
);
587 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
588 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
593 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
);
601 * For each member of @opts, call @func(@opaque, name, value, @errp).
602 * @func() may store an Error through @errp, but must return non-zero then.
603 * When @func() returns non-zero, break the loop and return that value.
604 * Return zero when the loop completes.
606 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
612 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
613 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
617 assert(!errp
|| !*errp
);
622 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
626 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
627 if (!opts
->id
&& !id
) {
630 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
637 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
638 int fail_if_exists
, Error
**errp
)
640 QemuOpts
*opts
= NULL
;
643 if (!id_wellformed(id
)) {
644 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
646 #if 0 /* conversion from qerror_report() to error_set() broke this: */
647 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', 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 void qemu_opts_print(QemuOpts
*opts
, const char *sep
)
736 QemuOptDesc
*desc
= opts
->list
->desc
;
738 if (desc
[0].name
== NULL
) {
739 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
740 printf("%s%s=\"%s\"", sep
, opt
->name
, opt
->str
);
744 for (; desc
&& desc
->name
; desc
++) {
746 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
748 value
= opt
? opt
->str
: desc
->def_value_str
;
752 if (desc
->type
== QEMU_OPT_STRING
) {
753 printf("%s%s='%s'", sep
, desc
->name
, value
);
754 } else if ((desc
->type
== QEMU_OPT_SIZE
||
755 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
756 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
758 printf("%s%s=%s", sep
, desc
->name
, value
);
763 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
764 const char *firstname
, bool prepend
, Error
**errp
)
766 char option
[128], value
[1024];
767 const char *p
,*pe
,*pc
;
768 Error
*local_err
= NULL
;
770 for (p
= params
; *p
!= '\0'; p
++) {
773 if (!pe
|| (pc
&& pc
< pe
)) {
774 /* found "foo,more" */
775 if (p
== params
&& firstname
) {
776 /* implicitly named first option */
777 pstrcpy(option
, sizeof(option
), firstname
);
778 p
= get_opt_value(value
, sizeof(value
), p
);
780 /* option without value, probably a flag */
781 p
= get_opt_name(option
, sizeof(option
), p
, ',');
782 if (strncmp(option
, "no", 2) == 0) {
783 memmove(option
, option
+2, strlen(option
+2)+1);
784 pstrcpy(value
, sizeof(value
), "off");
786 pstrcpy(value
, sizeof(value
), "on");
790 /* found "foo=bar,more" */
791 p
= get_opt_name(option
, sizeof(option
), p
, '=');
796 p
= get_opt_value(value
, sizeof(value
), p
);
798 if (strcmp(option
, "id") != 0) {
799 /* store and parse */
800 opt_set(opts
, option
, value
, prepend
, &local_err
);
802 error_propagate(errp
, local_err
);
813 * Store options parsed from @params into @opts.
814 * If @firstname is non-null, the first key=value in @params may omit
815 * key=, and is treated as if key was @firstname.
816 * On error, store an error object through @errp if non-null.
818 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
819 const char *firstname
, Error
**errp
)
821 opts_do_parse(opts
, params
, firstname
, false, errp
);
824 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
825 bool permit_abbrev
, bool defaults
, Error
**errp
)
827 const char *firstname
;
828 char value
[1024], *id
= NULL
;
831 Error
*local_err
= NULL
;
833 assert(!permit_abbrev
|| list
->implied_opt_name
);
834 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
836 if (strncmp(params
, "id=", 3) == 0) {
837 get_opt_value(value
, sizeof(value
), params
+3);
839 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
840 get_opt_value(value
, sizeof(value
), p
+4);
845 * This code doesn't work for defaults && !list->merge_lists: when
846 * params has no id=, and list has an element with !opts->id, it
847 * appends a new element instead of returning the existing opts.
848 * However, we got no use for this case. Guard against possible
849 * (if unlikely) future misuse:
851 assert(!defaults
|| list
->merge_lists
);
852 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
854 error_propagate(errp
, local_err
);
858 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
860 error_propagate(errp
, local_err
);
869 * Create a QemuOpts in @list and with options parsed from @params.
870 * If @permit_abbrev, the first key=value in @params may omit key=,
871 * and is treated as if key was @list->implied_opt_name.
872 * On error, store an error object through @errp if non-null.
873 * Return the new QemuOpts on success, null pointer on error.
875 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
876 bool permit_abbrev
, Error
**errp
)
878 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
882 * Create a QemuOpts in @list and with options parsed from @params.
883 * If @permit_abbrev, the first key=value in @params may omit key=,
884 * and is treated as if key was @list->implied_opt_name.
885 * Report errors with error_report_err(). This is inappropriate in
886 * QMP context. Do not use this function there!
887 * Return the new QemuOpts on success, null pointer on error.
889 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
895 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
897 error_report_err(err
);
902 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
907 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
911 typedef struct OptsFromQDictState
{
914 } OptsFromQDictState
;
916 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
918 OptsFromQDictState
*state
= opaque
;
923 if (!strcmp(key
, "id") || *state
->errp
) {
927 switch (qobject_type(obj
)) {
929 value
= qstring_get_str(qobject_to_qstring(obj
));
932 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
933 qint_get_int(qobject_to_qint(obj
)));
934 assert(n
< sizeof(buf
));
938 n
= snprintf(buf
, sizeof(buf
), "%.17g",
939 qfloat_get_double(qobject_to_qfloat(obj
)));
940 assert(n
< sizeof(buf
));
944 pstrcpy(buf
, sizeof(buf
),
945 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
952 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
956 * Create QemuOpts from a QDict.
957 * Use value of key "id" as ID if it exists and is a QString.
958 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
959 * other types are silently ignored.
961 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
964 OptsFromQDictState state
;
965 Error
*local_err
= NULL
;
968 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
971 error_propagate(errp
, local_err
);
975 assert(opts
!= NULL
);
977 state
.errp
= &local_err
;
979 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
981 error_propagate(errp
, local_err
);
990 * Adds all QDict entries to the QemuOpts that can be added and removes them
991 * from the QDict. When this function returns, the QDict contains only those
992 * entries that couldn't be added to the QemuOpts.
994 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
996 const QDictEntry
*entry
, *next
;
998 entry
= qdict_first(qdict
);
1000 while (entry
!= NULL
) {
1001 Error
*local_err
= NULL
;
1002 OptsFromQDictState state
= {
1007 next
= qdict_next(qdict
, entry
);
1009 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1010 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1012 error_propagate(errp
, local_err
);
1015 qdict_del(qdict
, entry
->key
);
1024 * Convert from QemuOpts to QDict.
1025 * The QDict values are of type QString.
1026 * TODO We'll want to use types appropriate for opt->desc->type, but
1027 * this is enough for now.
1029 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1035 qdict
= qdict_new();
1038 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1040 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1041 val
= QOBJECT(qstring_from_str(opt
->str
));
1042 qdict_put_obj(qdict
, opt
->name
, val
);
1047 /* Validate parsed opts against descriptions where no
1048 * descriptions were provided in the QemuOptsList.
1050 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1053 Error
*local_err
= NULL
;
1055 assert(opts_accepts_any(opts
));
1057 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1058 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1060 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1064 qemu_opt_parse(opt
, &local_err
);
1066 error_propagate(errp
, local_err
);
1073 * For each member of @list, call @func(@opaque, member, @errp).
1074 * Call it with the current location temporarily set to the member's.
1075 * @func() may store an Error through @errp, but must return non-zero then.
1076 * When @func() returns non-zero, break the loop and return that value.
1077 * Return zero when the loop completes.
1079 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1080 void *opaque
, Error
**errp
)
1086 loc_push_none(&loc
);
1087 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1088 loc_restore(&opts
->loc
);
1089 rc
= func(opaque
, opts
, errp
);
1093 assert(!errp
|| !*errp
);
1099 static size_t count_opts_list(QemuOptsList
*list
)
1101 QemuOptDesc
*desc
= NULL
;
1102 size_t num_opts
= 0;
1109 while (desc
&& desc
->name
) {
1117 void qemu_opts_free(QemuOptsList
*list
)
1122 /* Realloc dst option list and append options from an option list (list)
1123 * to it. dst could be NULL or a malloced list.
1124 * The lifetime of dst must be shorter than the input list because the
1125 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1127 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1130 size_t num_opts
, num_dst_opts
;
1132 bool need_init
= false;
1133 bool need_head_update
;
1139 /* If dst is NULL, after realloc, some area of dst should be initialized
1140 * before adding options to it.
1144 need_head_update
= true;
1146 /* Moreover, even if dst is not NULL, the realloc may move it to a
1147 * different address in which case we may get a stale tail pointer
1149 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1152 num_opts
= count_opts_list(dst
);
1153 num_dst_opts
= num_opts
;
1154 num_opts
+= count_opts_list(list
);
1155 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1156 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1159 dst
->implied_opt_name
= NULL
;
1160 dst
->merge_lists
= false;
1162 if (need_head_update
) {
1163 QTAILQ_INIT(&dst
->head
);
1165 dst
->desc
[num_dst_opts
].name
= NULL
;
1167 /* append list->desc to dst->desc */
1170 while (desc
&& desc
->name
) {
1171 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1172 dst
->desc
[num_dst_opts
++] = *desc
;
1173 dst
->desc
[num_dst_opts
].name
= NULL
;