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 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
338 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
342 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
344 QemuOpt
*ret
= iter
->opt
;
346 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
347 ret
= QTAILQ_NEXT(ret
, next
);
350 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
351 return ret
? ret
->str
: NULL
;
354 /* Get a known option (or its default) and remove it from the list
355 * all in one action. Return a malloced string of the option value.
356 * Result must be freed by caller with g_free().
358 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
361 const QemuOptDesc
*desc
;
368 opt
= qemu_opt_find(opts
, name
);
370 desc
= find_desc_by_name(opts
->list
->desc
, name
);
371 if (desc
&& desc
->def_value_str
) {
372 str
= g_strdup(desc
->def_value_str
);
378 qemu_opt_del_all(opts
, name
);
382 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
386 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
387 if (is_help_option(opt
->name
)) {
394 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
395 bool defval
, bool del
)
404 opt
= qemu_opt_find(opts
, name
);
406 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
407 if (desc
&& desc
->def_value_str
) {
408 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
412 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
413 ret
= opt
->value
.boolean
;
415 qemu_opt_del_all(opts
, name
);
420 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
422 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
425 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
427 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
430 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
431 uint64_t defval
, bool del
)
434 uint64_t ret
= defval
;
440 opt
= qemu_opt_find(opts
, name
);
442 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
443 if (desc
&& desc
->def_value_str
) {
444 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
448 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
449 ret
= opt
->value
.uint
;
451 qemu_opt_del_all(opts
, name
);
456 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
458 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
461 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
464 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
467 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
468 uint64_t defval
, bool del
)
471 uint64_t ret
= defval
;
477 opt
= qemu_opt_find(opts
, name
);
479 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
480 if (desc
&& desc
->def_value_str
) {
481 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
485 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
486 ret
= opt
->value
.uint
;
488 qemu_opt_del_all(opts
, name
);
493 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
495 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
498 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
501 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
504 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
506 if (opt
->desc
== NULL
)
509 switch (opt
->desc
->type
) {
510 case QEMU_OPT_STRING
:
514 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
516 case QEMU_OPT_NUMBER
:
517 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
520 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
527 static bool opts_accepts_any(const QemuOpts
*opts
)
529 return opts
->list
->desc
[0].name
== NULL
;
532 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
534 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
536 assert(opts_accepts_any(opts
));
546 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
547 bool prepend
, Error
**errp
)
550 const QemuOptDesc
*desc
;
551 Error
*local_err
= NULL
;
553 desc
= find_desc_by_name(opts
->list
->desc
, name
);
554 if (!desc
&& !opts_accepts_any(opts
)) {
555 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
559 opt
= g_malloc0(sizeof(*opt
));
560 opt
->name
= g_strdup(name
);
563 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
565 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
568 opt
->str
= g_strdup(value
);
569 qemu_opt_parse(opt
, &local_err
);
571 error_propagate(errp
, local_err
);
576 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
579 opt_set(opts
, name
, value
, false, errp
);
582 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool 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
.boolean
= !!val
;
599 opt
->str
= g_strdup(val
? "on" : "off");
600 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
603 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
607 const QemuOptDesc
*desc
= opts
->list
->desc
;
609 opt
= g_malloc0(sizeof(*opt
));
610 opt
->desc
= find_desc_by_name(desc
, name
);
611 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
612 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
617 opt
->name
= g_strdup(name
);
619 opt
->value
.uint
= val
;
620 opt
->str
= g_strdup_printf("%" PRId64
, val
);
621 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
625 * For each member of @opts, call @func(@opaque, name, value, @errp).
626 * @func() may store an Error through @errp, but must return non-zero then.
627 * When @func() returns non-zero, break the loop and return that value.
628 * Return zero when the loop completes.
630 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
636 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
637 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
641 assert(!errp
|| !*errp
);
646 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
650 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
651 if (!opts
->id
&& !id
) {
654 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
661 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
662 int fail_if_exists
, Error
**errp
)
664 QemuOpts
*opts
= NULL
;
667 if (!id_wellformed(id
)) {
668 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
670 error_append_hint(errp
, "Identifiers consist of letters, digits, "
671 "'-', '.', '_', starting with a letter.\n");
674 opts
= qemu_opts_find(list
, id
);
676 if (fail_if_exists
&& !list
->merge_lists
) {
677 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
683 } else if (list
->merge_lists
) {
684 opts
= qemu_opts_find(list
, NULL
);
689 opts
= g_malloc0(sizeof(*opts
));
690 opts
->id
= g_strdup(id
);
692 loc_save(&opts
->loc
);
693 QTAILQ_INIT(&opts
->head
);
694 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
698 void qemu_opts_reset(QemuOptsList
*list
)
700 QemuOpts
*opts
, *next_opts
;
702 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
707 void qemu_opts_loc_restore(QemuOpts
*opts
)
709 loc_restore(&opts
->loc
);
712 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
713 const char *name
, const char *value
, Error
**errp
)
716 Error
*local_err
= NULL
;
718 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
720 error_propagate(errp
, local_err
);
723 qemu_opt_set(opts
, name
, value
, errp
);
726 const char *qemu_opts_id(QemuOpts
*opts
)
731 /* The id string will be g_free()d by qemu_opts_del */
732 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
737 void qemu_opts_del(QemuOpts
*opts
)
746 opt
= QTAILQ_FIRST(&opts
->head
);
751 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
756 /* print value, escaping any commas in value */
757 static void escaped_print(const char *value
)
761 for (ptr
= value
; *ptr
; ++ptr
) {
769 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
772 QemuOptDesc
*desc
= opts
->list
->desc
;
773 const char *sep
= "";
776 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
780 if (desc
[0].name
== NULL
) {
781 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
782 printf("%s%s=", sep
, opt
->name
);
783 escaped_print(opt
->str
);
788 for (; desc
&& desc
->name
; desc
++) {
790 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
792 value
= opt
? opt
->str
: desc
->def_value_str
;
796 if (desc
->type
== QEMU_OPT_STRING
) {
797 printf("%s%s=", sep
, desc
->name
);
798 escaped_print(value
);
799 } else if ((desc
->type
== QEMU_OPT_SIZE
||
800 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
801 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
803 printf("%s%s=%s", sep
, desc
->name
, value
);
809 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
810 const char *firstname
, bool prepend
, Error
**errp
)
812 char option
[128], value
[1024];
813 const char *p
,*pe
,*pc
;
814 Error
*local_err
= NULL
;
816 for (p
= params
; *p
!= '\0'; p
++) {
819 if (!pe
|| (pc
&& pc
< pe
)) {
820 /* found "foo,more" */
821 if (p
== params
&& firstname
) {
822 /* implicitly named first option */
823 pstrcpy(option
, sizeof(option
), firstname
);
824 p
= get_opt_value(value
, sizeof(value
), p
);
826 /* option without value, probably a flag */
827 p
= get_opt_name(option
, sizeof(option
), p
, ',');
828 if (strncmp(option
, "no", 2) == 0) {
829 memmove(option
, option
+2, strlen(option
+2)+1);
830 pstrcpy(value
, sizeof(value
), "off");
832 pstrcpy(value
, sizeof(value
), "on");
836 /* found "foo=bar,more" */
837 p
= get_opt_name(option
, sizeof(option
), p
, '=');
842 p
= get_opt_value(value
, sizeof(value
), p
);
844 if (strcmp(option
, "id") != 0) {
845 /* store and parse */
846 opt_set(opts
, option
, value
, prepend
, &local_err
);
848 error_propagate(errp
, local_err
);
859 * Store options parsed from @params into @opts.
860 * If @firstname is non-null, the first key=value in @params may omit
861 * key=, and is treated as if key was @firstname.
862 * On error, store an error object through @errp if non-null.
864 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
865 const char *firstname
, Error
**errp
)
867 opts_do_parse(opts
, params
, firstname
, false, errp
);
870 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
871 bool permit_abbrev
, bool defaults
, Error
**errp
)
873 const char *firstname
;
874 char value
[1024], *id
= NULL
;
877 Error
*local_err
= NULL
;
879 assert(!permit_abbrev
|| list
->implied_opt_name
);
880 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
882 if (strncmp(params
, "id=", 3) == 0) {
883 get_opt_value(value
, sizeof(value
), params
+3);
885 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
886 get_opt_value(value
, sizeof(value
), p
+4);
891 * This code doesn't work for defaults && !list->merge_lists: when
892 * params has no id=, and list has an element with !opts->id, it
893 * appends a new element instead of returning the existing opts.
894 * However, we got no use for this case. Guard against possible
895 * (if unlikely) future misuse:
897 assert(!defaults
|| list
->merge_lists
);
898 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
900 error_propagate(errp
, local_err
);
904 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
906 error_propagate(errp
, local_err
);
915 * Create a QemuOpts in @list and with options parsed from @params.
916 * If @permit_abbrev, the first key=value in @params may omit key=,
917 * and is treated as if key was @list->implied_opt_name.
918 * On error, store an error object through @errp if non-null.
919 * Return the new QemuOpts on success, null pointer on error.
921 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
922 bool permit_abbrev
, Error
**errp
)
924 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
928 * Create a QemuOpts in @list and with options parsed from @params.
929 * If @permit_abbrev, the first key=value in @params may omit key=,
930 * and is treated as if key was @list->implied_opt_name.
931 * Report errors with error_report_err(). This is inappropriate in
932 * QMP context. Do not use this function there!
933 * Return the new QemuOpts on success, null pointer on error.
935 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
941 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
943 error_report_err(err
);
948 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
953 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
957 typedef struct OptsFromQDictState
{
960 } OptsFromQDictState
;
962 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
964 OptsFromQDictState
*state
= opaque
;
969 if (!strcmp(key
, "id") || *state
->errp
) {
973 switch (qobject_type(obj
)) {
975 value
= qstring_get_str(qobject_to_qstring(obj
));
978 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
979 qint_get_int(qobject_to_qint(obj
)));
980 assert(n
< sizeof(buf
));
984 n
= snprintf(buf
, sizeof(buf
), "%.17g",
985 qfloat_get_double(qobject_to_qfloat(obj
)));
986 assert(n
< sizeof(buf
));
990 pstrcpy(buf
, sizeof(buf
),
991 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
998 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
1002 * Create QemuOpts from a QDict.
1003 * Use value of key "id" as ID if it exists and is a QString.
1004 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1005 * other types are silently ignored.
1007 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1010 OptsFromQDictState state
;
1011 Error
*local_err
= NULL
;
1014 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1017 error_propagate(errp
, local_err
);
1021 assert(opts
!= NULL
);
1023 state
.errp
= &local_err
;
1025 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1027 error_propagate(errp
, local_err
);
1028 qemu_opts_del(opts
);
1036 * Adds all QDict entries to the QemuOpts that can be added and removes them
1037 * from the QDict. When this function returns, the QDict contains only those
1038 * entries that couldn't be added to the QemuOpts.
1040 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1042 const QDictEntry
*entry
, *next
;
1044 entry
= qdict_first(qdict
);
1046 while (entry
!= NULL
) {
1047 Error
*local_err
= NULL
;
1048 OptsFromQDictState state
= {
1053 next
= qdict_next(qdict
, entry
);
1055 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1056 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1058 error_propagate(errp
, local_err
);
1061 qdict_del(qdict
, entry
->key
);
1070 * Convert from QemuOpts to QDict.
1071 * The QDict values are of type QString.
1072 * TODO We'll want to use types appropriate for opt->desc->type, but
1073 * this is enough for now.
1075 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1081 qdict
= qdict_new();
1084 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1086 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1087 val
= QOBJECT(qstring_from_str(opt
->str
));
1088 qdict_put_obj(qdict
, opt
->name
, val
);
1093 /* Validate parsed opts against descriptions where no
1094 * descriptions were provided in the QemuOptsList.
1096 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1099 Error
*local_err
= NULL
;
1101 assert(opts_accepts_any(opts
));
1103 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1104 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1106 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1110 qemu_opt_parse(opt
, &local_err
);
1112 error_propagate(errp
, local_err
);
1119 * For each member of @list, call @func(@opaque, member, @errp).
1120 * Call it with the current location temporarily set to the member's.
1121 * @func() may store an Error through @errp, but must return non-zero then.
1122 * When @func() returns non-zero, break the loop and return that value.
1123 * Return zero when the loop completes.
1125 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1126 void *opaque
, Error
**errp
)
1132 loc_push_none(&loc
);
1133 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1134 loc_restore(&opts
->loc
);
1135 rc
= func(opaque
, opts
, errp
);
1139 assert(!errp
|| !*errp
);
1145 static size_t count_opts_list(QemuOptsList
*list
)
1147 QemuOptDesc
*desc
= NULL
;
1148 size_t num_opts
= 0;
1155 while (desc
&& desc
->name
) {
1163 void qemu_opts_free(QemuOptsList
*list
)
1168 /* Realloc dst option list and append options from an option list (list)
1169 * to it. dst could be NULL or a malloced list.
1170 * The lifetime of dst must be shorter than the input list because the
1171 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1173 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1176 size_t num_opts
, num_dst_opts
;
1178 bool need_init
= false;
1179 bool need_head_update
;
1185 /* If dst is NULL, after realloc, some area of dst should be initialized
1186 * before adding options to it.
1190 need_head_update
= true;
1192 /* Moreover, even if dst is not NULL, the realloc may move it to a
1193 * different address in which case we may get a stale tail pointer
1195 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1198 num_opts
= count_opts_list(dst
);
1199 num_dst_opts
= num_opts
;
1200 num_opts
+= count_opts_list(list
);
1201 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1202 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1205 dst
->implied_opt_name
= NULL
;
1206 dst
->merge_lists
= false;
1208 if (need_head_update
) {
1209 QTAILQ_INIT(&dst
->head
);
1211 dst
->desc
[num_dst_opts
].name
= NULL
;
1213 /* append list->desc to dst->desc */
1216 while (desc
&& desc
->name
) {
1217 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1218 dst
->desc
[num_dst_opts
++] = *desc
;
1219 dst
->desc
[num_dst_opts
].name
= NULL
;