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 "qapi/error.h"
29 #include "qemu-common.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/types.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qemu/option_int.h"
34 #include "qemu/cutils.h"
36 #include "qemu/help_option.h"
39 * Extracts the name of an option from the parameter string (p points at the
40 * first byte of the option name)
42 * The option name is delimited by delim (usually , or =) or the string end
43 * and is copied into buf. If the option name is longer than buf_size, it is
44 * truncated. buf is always zero terminated.
46 * The return value is the position of the delimiter/zero byte after the option
49 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
54 while (*p
!= '\0' && *p
!= delim
) {
55 if (q
&& (q
- buf
) < buf_size
- 1)
66 * Extracts the value of an option from the parameter string p (p points at the
67 * first byte of the option value)
69 * This function is comparable to get_opt_name with the difference that the
70 * delimiter is fixed to be comma which starts a new option. To specify an
71 * option value that contains commas, double each comma.
73 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
84 if (q
&& (q
- buf
) < buf_size
- 1)
94 int get_next_param_value(char *buf
, int buf_size
,
95 const char *tag
, const char **pstr
)
102 p
= get_opt_name(option
, sizeof(option
), p
, '=');
106 if (!strcmp(tag
, option
)) {
107 *pstr
= get_opt_value(buf
, buf_size
, p
);
113 p
= get_opt_value(NULL
, 0, p
);
122 int get_param_value(char *buf
, int buf_size
,
123 const char *tag
, const char *str
)
125 return get_next_param_value(buf
, buf_size
, tag
, &str
);
128 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
132 if (!strcmp(value
, "on")) {
134 } else if (!strcmp(value
, "off")) {
137 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
138 name
, "'on' or 'off'");
145 static void parse_option_number(const char *name
, const char *value
,
146 uint64_t *ret
, Error
**errp
)
152 number
= strtoull(value
, &postfix
, 0);
153 if (*postfix
!= '\0') {
154 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
159 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
163 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
168 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
169 if (strcmp(desc
[i
].name
, name
) == 0) {
177 void parse_option_size(const char *name
, const char *value
,
178 uint64_t *ret
, Error
**errp
)
184 sizef
= strtod(value
, &postfix
);
185 if (sizef
< 0 || sizef
> UINT64_MAX
) {
186 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
187 "a non-negative number below 2^64");
206 *ret
= (uint64_t) sizef
;
209 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
210 error_append_hint(errp
, "You may use k, M, G or T suffixes for "
211 "kilobytes, megabytes, gigabytes and terabytes.\n");
215 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
219 bool has_help_option(const char *param
)
221 size_t buflen
= strlen(param
) + 1;
222 char *buf
= g_malloc(buflen
);
223 const char *p
= param
;
227 p
= get_opt_value(buf
, buflen
, p
);
232 if (is_help_option(buf
)) {
243 bool is_valid_option_list(const char *param
)
245 size_t buflen
= strlen(param
) + 1;
246 char *buf
= g_malloc(buflen
);
247 const char *p
= param
;
251 p
= get_opt_value(buf
, buflen
, p
);
257 if (!*buf
|| *buf
== ',') {
268 void qemu_opts_print_help(QemuOptsList
*list
)
274 printf("Supported options:\n");
275 while (desc
&& desc
->name
) {
276 printf("%-16s %s\n", desc
->name
,
277 desc
->help
? desc
->help
: "No description available");
281 /* ------------------------------------------------------------------ */
283 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
287 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
288 if (strcmp(opt
->name
, name
) != 0)
295 static void qemu_opt_del(QemuOpt
*opt
)
297 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
303 /* qemu_opt_set allows many settings for the same option.
304 * This function deletes all settings for an option.
306 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
308 QemuOpt
*opt
, *next_opt
;
310 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
311 if (!strcmp(opt
->name
, name
)) {
317 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
325 opt
= qemu_opt_find(opts
, name
);
327 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
328 if (desc
&& desc
->def_value_str
) {
329 return desc
->def_value_str
;
332 return opt
? opt
->str
: NULL
;
335 /* Get a known option (or its default) and remove it from the list
336 * all in one action. Return a malloced string of the option value.
337 * Result must be freed by caller with g_free().
339 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
342 const QemuOptDesc
*desc
;
349 opt
= qemu_opt_find(opts
, name
);
351 desc
= find_desc_by_name(opts
->list
->desc
, name
);
352 if (desc
&& desc
->def_value_str
) {
353 str
= g_strdup(desc
->def_value_str
);
359 qemu_opt_del_all(opts
, name
);
363 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
367 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
368 if (is_help_option(opt
->name
)) {
375 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
376 bool defval
, bool del
)
385 opt
= qemu_opt_find(opts
, name
);
387 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
388 if (desc
&& desc
->def_value_str
) {
389 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
393 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
394 ret
= opt
->value
.boolean
;
396 qemu_opt_del_all(opts
, name
);
401 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
403 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
406 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
408 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
411 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
412 uint64_t defval
, bool del
)
415 uint64_t ret
= defval
;
421 opt
= qemu_opt_find(opts
, name
);
423 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
424 if (desc
&& desc
->def_value_str
) {
425 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
429 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
430 ret
= opt
->value
.uint
;
432 qemu_opt_del_all(opts
, name
);
437 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
439 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
442 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
445 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
448 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
449 uint64_t defval
, bool del
)
452 uint64_t ret
= defval
;
458 opt
= qemu_opt_find(opts
, name
);
460 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
461 if (desc
&& desc
->def_value_str
) {
462 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
466 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
467 ret
= opt
->value
.uint
;
469 qemu_opt_del_all(opts
, name
);
474 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
476 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
479 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
482 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
485 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
487 if (opt
->desc
== NULL
)
490 switch (opt
->desc
->type
) {
491 case QEMU_OPT_STRING
:
495 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
497 case QEMU_OPT_NUMBER
:
498 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
501 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
508 static bool opts_accepts_any(const QemuOpts
*opts
)
510 return opts
->list
->desc
[0].name
== NULL
;
513 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
515 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
517 assert(opts_accepts_any(opts
));
527 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
528 bool prepend
, Error
**errp
)
531 const QemuOptDesc
*desc
;
532 Error
*local_err
= NULL
;
534 desc
= find_desc_by_name(opts
->list
->desc
, name
);
535 if (!desc
&& !opts_accepts_any(opts
)) {
536 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
540 opt
= g_malloc0(sizeof(*opt
));
541 opt
->name
= g_strdup(name
);
544 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
546 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
549 opt
->str
= g_strdup(value
);
550 qemu_opt_parse(opt
, &local_err
);
552 error_propagate(errp
, local_err
);
557 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
560 opt_set(opts
, name
, value
, false, errp
);
563 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
567 const QemuOptDesc
*desc
= opts
->list
->desc
;
569 opt
= g_malloc0(sizeof(*opt
));
570 opt
->desc
= find_desc_by_name(desc
, name
);
571 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
572 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
577 opt
->name
= g_strdup(name
);
579 opt
->value
.boolean
= !!val
;
580 opt
->str
= g_strdup(val
? "on" : "off");
581 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
584 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
588 const QemuOptDesc
*desc
= opts
->list
->desc
;
590 opt
= g_malloc0(sizeof(*opt
));
591 opt
->desc
= find_desc_by_name(desc
, name
);
592 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
593 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
598 opt
->name
= g_strdup(name
);
600 opt
->value
.uint
= val
;
601 opt
->str
= g_strdup_printf("%" PRId64
, val
);
602 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
606 * For each member of @opts, call @func(@opaque, name, value, @errp).
607 * @func() may store an Error through @errp, but must return non-zero then.
608 * When @func() returns non-zero, break the loop and return that value.
609 * Return zero when the loop completes.
611 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
617 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
618 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
622 assert(!errp
|| !*errp
);
627 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
631 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
632 if (!opts
->id
&& !id
) {
635 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
642 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
643 int fail_if_exists
, Error
**errp
)
645 QemuOpts
*opts
= NULL
;
648 if (!id_wellformed(id
)) {
649 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
651 error_append_hint(errp
, "Identifiers consist of letters, digits, "
652 "'-', '.', '_', starting with a letter.\n");
655 opts
= qemu_opts_find(list
, id
);
657 if (fail_if_exists
&& !list
->merge_lists
) {
658 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
664 } else if (list
->merge_lists
) {
665 opts
= qemu_opts_find(list
, NULL
);
670 opts
= g_malloc0(sizeof(*opts
));
671 opts
->id
= g_strdup(id
);
673 loc_save(&opts
->loc
);
674 QTAILQ_INIT(&opts
->head
);
675 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
679 void qemu_opts_reset(QemuOptsList
*list
)
681 QemuOpts
*opts
, *next_opts
;
683 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
688 void qemu_opts_loc_restore(QemuOpts
*opts
)
690 loc_restore(&opts
->loc
);
693 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
694 const char *name
, const char *value
, Error
**errp
)
697 Error
*local_err
= NULL
;
699 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
701 error_propagate(errp
, local_err
);
704 qemu_opt_set(opts
, name
, value
, errp
);
707 const char *qemu_opts_id(QemuOpts
*opts
)
712 /* The id string will be g_free()d by qemu_opts_del */
713 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
718 void qemu_opts_del(QemuOpts
*opts
)
727 opt
= QTAILQ_FIRST(&opts
->head
);
732 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
737 /* print value, escaping any commas in value */
738 static void escaped_print(const char *value
)
742 for (ptr
= value
; *ptr
; ++ptr
) {
750 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
753 QemuOptDesc
*desc
= opts
->list
->desc
;
754 const char *sep
= "";
757 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
761 if (desc
[0].name
== NULL
) {
762 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
763 printf("%s%s=", sep
, opt
->name
);
764 escaped_print(opt
->str
);
769 for (; desc
&& desc
->name
; desc
++) {
771 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
773 value
= opt
? opt
->str
: desc
->def_value_str
;
777 if (desc
->type
== QEMU_OPT_STRING
) {
778 printf("%s%s=", sep
, desc
->name
);
779 escaped_print(value
);
780 } else if ((desc
->type
== QEMU_OPT_SIZE
||
781 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
782 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
784 printf("%s%s=%s", sep
, desc
->name
, value
);
790 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
791 const char *firstname
, bool prepend
, Error
**errp
)
793 char option
[128], value
[1024];
794 const char *p
,*pe
,*pc
;
795 Error
*local_err
= NULL
;
797 /* Initialize data to 0 to avoid warnings from Valgrind. */
798 memset(option
, 0, sizeof(option
));
799 memset(value
, 0, sizeof(value
));
801 for (p
= params
; *p
!= '\0'; p
++) {
804 if (!pe
|| (pc
&& pc
< pe
)) {
805 /* found "foo,more" */
806 if (p
== params
&& firstname
) {
807 /* implicitly named first option */
808 pstrcpy(option
, sizeof(option
), firstname
);
809 p
= get_opt_value(value
, sizeof(value
), p
);
811 /* option without value, probably a flag */
812 p
= get_opt_name(option
, sizeof(option
), p
, ',');
813 if (strncmp(option
, "no", 2) == 0) {
814 memmove(option
, option
+2, strlen(option
+2)+1);
815 pstrcpy(value
, sizeof(value
), "off");
817 pstrcpy(value
, sizeof(value
), "on");
821 /* found "foo=bar,more" */
822 p
= get_opt_name(option
, sizeof(option
), p
, '=');
827 p
= get_opt_value(value
, sizeof(value
), p
);
829 if (strcmp(option
, "id") != 0) {
830 /* store and parse */
831 opt_set(opts
, option
, value
, prepend
, &local_err
);
833 error_propagate(errp
, local_err
);
844 * Store options parsed from @params into @opts.
845 * If @firstname is non-null, the first key=value in @params may omit
846 * key=, and is treated as if key was @firstname.
847 * On error, store an error object through @errp if non-null.
849 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
850 const char *firstname
, Error
**errp
)
852 opts_do_parse(opts
, params
, firstname
, false, errp
);
855 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
856 bool permit_abbrev
, bool defaults
, Error
**errp
)
858 const char *firstname
;
859 char value
[1024], *id
= NULL
;
862 Error
*local_err
= NULL
;
864 assert(!permit_abbrev
|| list
->implied_opt_name
);
865 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
867 if (strncmp(params
, "id=", 3) == 0) {
868 get_opt_value(value
, sizeof(value
), params
+3);
870 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
871 get_opt_value(value
, sizeof(value
), p
+4);
876 * This code doesn't work for defaults && !list->merge_lists: when
877 * params has no id=, and list has an element with !opts->id, it
878 * appends a new element instead of returning the existing opts.
879 * However, we got no use for this case. Guard against possible
880 * (if unlikely) future misuse:
882 assert(!defaults
|| list
->merge_lists
);
883 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
885 error_propagate(errp
, local_err
);
889 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
891 error_propagate(errp
, local_err
);
900 * Create a QemuOpts in @list and with options parsed from @params.
901 * If @permit_abbrev, the first key=value in @params may omit key=,
902 * and is treated as if key was @list->implied_opt_name.
903 * On error, store an error object through @errp if non-null.
904 * Return the new QemuOpts on success, null pointer on error.
906 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
907 bool permit_abbrev
, Error
**errp
)
909 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
913 * Create a QemuOpts in @list and with options parsed from @params.
914 * If @permit_abbrev, the first key=value in @params may omit key=,
915 * and is treated as if key was @list->implied_opt_name.
916 * Report errors with error_report_err(). This is inappropriate in
917 * QMP context. Do not use this function there!
918 * Return the new QemuOpts on success, null pointer on error.
920 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
926 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
928 error_report_err(err
);
933 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
938 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
942 typedef struct OptsFromQDictState
{
945 } OptsFromQDictState
;
947 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
949 OptsFromQDictState
*state
= opaque
;
954 if (!strcmp(key
, "id") || *state
->errp
) {
958 switch (qobject_type(obj
)) {
960 value
= qstring_get_str(qobject_to_qstring(obj
));
963 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
964 qint_get_int(qobject_to_qint(obj
)));
965 assert(n
< sizeof(buf
));
969 n
= snprintf(buf
, sizeof(buf
), "%.17g",
970 qfloat_get_double(qobject_to_qfloat(obj
)));
971 assert(n
< sizeof(buf
));
975 pstrcpy(buf
, sizeof(buf
),
976 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
983 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
987 * Create QemuOpts from a QDict.
988 * Use value of key "id" as ID if it exists and is a QString.
989 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
990 * other types are silently ignored.
992 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
995 OptsFromQDictState state
;
996 Error
*local_err
= NULL
;
999 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1002 error_propagate(errp
, local_err
);
1006 assert(opts
!= NULL
);
1008 state
.errp
= &local_err
;
1010 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1012 error_propagate(errp
, local_err
);
1013 qemu_opts_del(opts
);
1021 * Adds all QDict entries to the QemuOpts that can be added and removes them
1022 * from the QDict. When this function returns, the QDict contains only those
1023 * entries that couldn't be added to the QemuOpts.
1025 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1027 const QDictEntry
*entry
, *next
;
1029 entry
= qdict_first(qdict
);
1031 while (entry
!= NULL
) {
1032 Error
*local_err
= NULL
;
1033 OptsFromQDictState state
= {
1038 next
= qdict_next(qdict
, entry
);
1040 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1041 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1043 error_propagate(errp
, local_err
);
1046 qdict_del(qdict
, entry
->key
);
1055 * Convert from QemuOpts to QDict.
1056 * The QDict values are of type QString.
1057 * TODO We'll want to use types appropriate for opt->desc->type, but
1058 * this is enough for now.
1060 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1066 qdict
= qdict_new();
1069 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1071 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1072 val
= QOBJECT(qstring_from_str(opt
->str
));
1073 qdict_put_obj(qdict
, opt
->name
, val
);
1078 /* Validate parsed opts against descriptions where no
1079 * descriptions were provided in the QemuOptsList.
1081 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1084 Error
*local_err
= NULL
;
1086 assert(opts_accepts_any(opts
));
1088 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1089 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1091 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1095 qemu_opt_parse(opt
, &local_err
);
1097 error_propagate(errp
, local_err
);
1104 * For each member of @list, call @func(@opaque, member, @errp).
1105 * Call it with the current location temporarily set to the member's.
1106 * @func() may store an Error through @errp, but must return non-zero then.
1107 * When @func() returns non-zero, break the loop and return that value.
1108 * Return zero when the loop completes.
1110 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1111 void *opaque
, Error
**errp
)
1117 loc_push_none(&loc
);
1118 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1119 loc_restore(&opts
->loc
);
1120 rc
= func(opaque
, opts
, errp
);
1124 assert(!errp
|| !*errp
);
1130 static size_t count_opts_list(QemuOptsList
*list
)
1132 QemuOptDesc
*desc
= NULL
;
1133 size_t num_opts
= 0;
1140 while (desc
&& desc
->name
) {
1148 void qemu_opts_free(QemuOptsList
*list
)
1153 /* Realloc dst option list and append options from an option list (list)
1154 * to it. dst could be NULL or a malloced list.
1155 * The lifetime of dst must be shorter than the input list because the
1156 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1158 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1161 size_t num_opts
, num_dst_opts
;
1163 bool need_init
= false;
1164 bool need_head_update
;
1170 /* If dst is NULL, after realloc, some area of dst should be initialized
1171 * before adding options to it.
1175 need_head_update
= true;
1177 /* Moreover, even if dst is not NULL, the realloc may move it to a
1178 * different address in which case we may get a stale tail pointer
1180 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1183 num_opts
= count_opts_list(dst
);
1184 num_dst_opts
= num_opts
;
1185 num_opts
+= count_opts_list(list
);
1186 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1187 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1190 dst
->implied_opt_name
= NULL
;
1191 dst
->merge_lists
= false;
1193 if (need_head_update
) {
1194 QTAILQ_INIT(&dst
->head
);
1196 dst
->desc
[num_dst_opts
].name
= NULL
;
1198 /* append list->desc to dst->desc */
1201 while (desc
&& desc
->name
) {
1202 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1203 dst
->desc
[num_dst_opts
++] = *desc
;
1204 dst
->desc
[num_dst_opts
].name
= NULL
;