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/qbool.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qapi/qmp/qnum.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qapi/qmp/qerror.h"
36 #include "qemu/option_int.h"
37 #include "qemu/cutils.h"
39 #include "qemu/help_option.h"
42 * Extracts the name of an option from the parameter string (p points at the
43 * first byte of the option name)
45 * The option name is delimited by delim (usually , or =) or the string end
46 * and is copied into option. The caller is responsible for free'ing option
47 * when no longer required.
49 * The return value is the position of the delimiter/zero byte after the option
52 static const char *get_opt_name(const char *p
, char **option
, char delim
)
54 char *offset
= strchr(p
, delim
);
57 *option
= g_strndup(p
, offset
- p
);
60 *option
= g_strdup(p
);
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(const char *p
, char **value
)
75 size_t capacity
= 0, length
;
80 offset
= qemu_strchrnul(p
, ',');
82 if (*offset
!= '\0' && *(offset
+ 1) == ',') {
85 *value
= g_renew(char, *value
, capacity
+ length
+ 1);
86 strncpy(*value
+ capacity
, p
, length
);
87 (*value
)[capacity
+ length
] = '\0';
89 if (*offset
== '\0' ||
90 *(offset
+ 1) != ',') {
94 p
+= (offset
- p
) + 2;
100 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
103 if (!strcmp(value
, "on")) {
105 } else if (!strcmp(value
, "off")) {
108 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
109 name
, "'on' or 'off'");
113 static void parse_option_number(const char *name
, const char *value
,
114 uint64_t *ret
, Error
**errp
)
119 err
= qemu_strtou64(value
, NULL
, 0, &number
);
120 if (err
== -ERANGE
) {
121 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
126 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
132 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
137 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
138 if (strcmp(desc
[i
].name
, name
) == 0) {
146 void parse_option_size(const char *name
, const char *value
,
147 uint64_t *ret
, Error
**errp
)
152 err
= qemu_strtosz(value
, NULL
, &size
);
153 if (err
== -ERANGE
) {
154 error_setg(errp
, "Value '%s' is out of range for parameter '%s'",
159 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
160 "a non-negative number below 2^64");
161 error_append_hint(errp
, "Optional suffix k, M, G, T, P or E means"
162 " kilo-, mega-, giga-, tera-, peta-\n"
163 "and exabytes, respectively.\n");
169 bool has_help_option(const char *param
)
171 const char *p
= param
;
174 while (*p
&& !result
) {
177 p
= get_opt_value(p
, &value
);
182 result
= is_help_option(value
);
189 bool is_valid_option_list(const char *p
)
195 p
= get_opt_value(p
, &value
);
197 (!*value
|| *value
== ',')) {
211 void qemu_opts_print_help(QemuOptsList
*list
)
217 while (desc
&& desc
->name
) {
218 printf("%-16s %s\n", desc
->name
,
219 desc
->help
? desc
->help
: "No description available");
223 /* ------------------------------------------------------------------ */
225 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
229 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
230 if (strcmp(opt
->name
, name
) != 0)
237 static void qemu_opt_del(QemuOpt
*opt
)
239 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
245 /* qemu_opt_set allows many settings for the same option.
246 * This function deletes all settings for an option.
248 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
250 QemuOpt
*opt
, *next_opt
;
252 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
253 if (!strcmp(opt
->name
, name
)) {
259 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
267 opt
= qemu_opt_find(opts
, name
);
269 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
270 if (desc
&& desc
->def_value_str
) {
271 return desc
->def_value_str
;
274 return opt
? opt
->str
: NULL
;
277 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
280 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
284 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
286 QemuOpt
*ret
= iter
->opt
;
288 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
289 ret
= QTAILQ_NEXT(ret
, next
);
292 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
293 return ret
? ret
->str
: NULL
;
296 /* Get a known option (or its default) and remove it from the list
297 * all in one action. Return a malloced string of the option value.
298 * Result must be freed by caller with g_free().
300 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
303 const QemuOptDesc
*desc
;
310 opt
= qemu_opt_find(opts
, name
);
312 desc
= find_desc_by_name(opts
->list
->desc
, name
);
313 if (desc
&& desc
->def_value_str
) {
314 str
= g_strdup(desc
->def_value_str
);
320 qemu_opt_del_all(opts
, name
);
324 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
328 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
329 if (is_help_option(opt
->name
)) {
336 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
337 bool defval
, bool del
)
346 opt
= qemu_opt_find(opts
, name
);
348 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
349 if (desc
&& desc
->def_value_str
) {
350 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
354 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
355 ret
= opt
->value
.boolean
;
357 qemu_opt_del_all(opts
, name
);
362 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
364 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
367 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
369 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
372 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
373 uint64_t defval
, bool del
)
376 uint64_t ret
= defval
;
382 opt
= qemu_opt_find(opts
, name
);
384 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
385 if (desc
&& desc
->def_value_str
) {
386 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
390 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
391 ret
= opt
->value
.uint
;
393 qemu_opt_del_all(opts
, name
);
398 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
400 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
403 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
406 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
409 static uint64_t qemu_opt_get_size_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_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
427 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
428 ret
= opt
->value
.uint
;
430 qemu_opt_del_all(opts
, name
);
435 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
437 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
440 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
443 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
446 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
448 if (opt
->desc
== NULL
)
451 switch (opt
->desc
->type
) {
452 case QEMU_OPT_STRING
:
456 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
458 case QEMU_OPT_NUMBER
:
459 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
462 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
469 static bool opts_accepts_any(const QemuOpts
*opts
)
471 return opts
->list
->desc
[0].name
== NULL
;
474 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
476 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
478 assert(opts_accepts_any(opts
));
488 static void opt_set(QemuOpts
*opts
, const char *name
, char *value
,
489 bool prepend
, bool *invalidp
, Error
**errp
)
492 const QemuOptDesc
*desc
;
493 Error
*local_err
= NULL
;
495 desc
= find_desc_by_name(opts
->list
->desc
, name
);
496 if (!desc
&& !opts_accepts_any(opts
)) {
498 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
505 opt
= g_malloc0(sizeof(*opt
));
506 opt
->name
= g_strdup(name
);
509 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
511 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
515 qemu_opt_parse(opt
, &local_err
);
517 error_propagate(errp
, local_err
);
522 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
525 opt_set(opts
, name
, g_strdup(value
), false, NULL
, errp
);
528 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
532 const QemuOptDesc
*desc
= opts
->list
->desc
;
534 opt
= g_malloc0(sizeof(*opt
));
535 opt
->desc
= find_desc_by_name(desc
, name
);
536 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
537 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
542 opt
->name
= g_strdup(name
);
544 opt
->value
.boolean
= !!val
;
545 opt
->str
= g_strdup(val
? "on" : "off");
546 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
549 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
553 const QemuOptDesc
*desc
= opts
->list
->desc
;
555 opt
= g_malloc0(sizeof(*opt
));
556 opt
->desc
= find_desc_by_name(desc
, name
);
557 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
558 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
563 opt
->name
= g_strdup(name
);
565 opt
->value
.uint
= val
;
566 opt
->str
= g_strdup_printf("%" PRId64
, val
);
567 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
571 * For each member of @opts, call @func(@opaque, name, value, @errp).
572 * @func() may store an Error through @errp, but must return non-zero then.
573 * When @func() returns non-zero, break the loop and return that value.
574 * Return zero when the loop completes.
576 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
582 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
583 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
587 assert(!errp
|| !*errp
);
592 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
596 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
597 if (!opts
->id
&& !id
) {
600 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
607 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
608 int fail_if_exists
, Error
**errp
)
610 QemuOpts
*opts
= NULL
;
613 if (!id_wellformed(id
)) {
614 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
616 error_append_hint(errp
, "Identifiers consist of letters, digits, "
617 "'-', '.', '_', starting with a letter.\n");
620 opts
= qemu_opts_find(list
, id
);
622 if (fail_if_exists
&& !list
->merge_lists
) {
623 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
629 } else if (list
->merge_lists
) {
630 opts
= qemu_opts_find(list
, NULL
);
635 opts
= g_malloc0(sizeof(*opts
));
636 opts
->id
= g_strdup(id
);
638 loc_save(&opts
->loc
);
639 QTAILQ_INIT(&opts
->head
);
640 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
644 void qemu_opts_reset(QemuOptsList
*list
)
646 QemuOpts
*opts
, *next_opts
;
648 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
653 void qemu_opts_loc_restore(QemuOpts
*opts
)
655 loc_restore(&opts
->loc
);
658 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
659 const char *name
, const char *value
, Error
**errp
)
662 Error
*local_err
= NULL
;
664 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
666 error_propagate(errp
, local_err
);
669 qemu_opt_set(opts
, name
, value
, errp
);
672 const char *qemu_opts_id(QemuOpts
*opts
)
677 /* The id string will be g_free()d by qemu_opts_del */
678 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
683 void qemu_opts_del(QemuOpts
*opts
)
692 opt
= QTAILQ_FIRST(&opts
->head
);
697 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
702 /* print value, escaping any commas in value */
703 static void escaped_print(const char *value
)
707 for (ptr
= value
; *ptr
; ++ptr
) {
715 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
718 QemuOptDesc
*desc
= opts
->list
->desc
;
719 const char *sep
= "";
722 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
726 if (desc
[0].name
== NULL
) {
727 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
728 printf("%s%s=", sep
, opt
->name
);
729 escaped_print(opt
->str
);
734 for (; desc
&& desc
->name
; desc
++) {
736 opt
= qemu_opt_find(opts
, desc
->name
);
738 value
= opt
? opt
->str
: desc
->def_value_str
;
742 if (desc
->type
== QEMU_OPT_STRING
) {
743 printf("%s%s=", sep
, desc
->name
);
744 escaped_print(value
);
745 } else if ((desc
->type
== QEMU_OPT_SIZE
||
746 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
747 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
749 printf("%s%s=%s", sep
, desc
->name
, value
);
755 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
756 const char *firstname
, bool prepend
,
757 bool *invalidp
, Error
**errp
)
761 const char *p
,*pe
,*pc
;
762 Error
*local_err
= NULL
;
764 for (p
= params
; *p
!= '\0'; p
++) {
767 if (!pe
|| (pc
&& pc
< pe
)) {
768 /* found "foo,more" */
769 if (p
== params
&& firstname
) {
770 /* implicitly named first option */
771 option
= g_strdup(firstname
);
772 p
= get_opt_value(p
, &value
);
774 /* option without value, probably a flag */
775 p
= get_opt_name(p
, &option
, ',');
776 if (strncmp(option
, "no", 2) == 0) {
777 memmove(option
, option
+2, strlen(option
+2)+1);
778 value
= g_strdup("off");
780 value
= g_strdup("on");
784 /* found "foo=bar,more" */
785 p
= get_opt_name(p
, &option
, '=');
788 p
= get_opt_value(p
, &value
);
790 if (strcmp(option
, "id") != 0) {
791 /* store and parse */
792 opt_set(opts
, option
, value
, prepend
, invalidp
, &local_err
);
795 error_propagate(errp
, local_err
);
804 option
= value
= NULL
;
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, NULL
, errp
);
824 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
825 bool permit_abbrev
, bool defaults
,
826 bool *invalidp
, Error
**errp
)
828 const char *firstname
;
832 Error
*local_err
= NULL
;
834 assert(!permit_abbrev
|| list
->implied_opt_name
);
835 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
837 if (strncmp(params
, "id=", 3) == 0) {
838 get_opt_value(params
+ 3, &id
);
839 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
840 get_opt_value(p
+ 4, &id
);
844 * This code doesn't work for defaults && !list->merge_lists: when
845 * params has no id=, and list has an element with !opts->id, it
846 * appends a new element instead of returning the existing opts.
847 * However, we got no use for this case. Guard against possible
848 * (if unlikely) future misuse:
850 assert(!defaults
|| list
->merge_lists
);
851 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
854 error_propagate(errp
, local_err
);
858 opts_do_parse(opts
, params
, firstname
, defaults
, invalidp
, &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, NULL
, 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
,
894 bool invalidp
= false;
896 opts
= opts_parse(list
, params
, permit_abbrev
, false, &invalidp
, &err
);
898 if (invalidp
&& has_help_option(params
)) {
899 qemu_opts_print_help(list
);
902 error_report_err(err
);
908 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
913 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
, NULL
);
917 typedef struct OptsFromQDictState
{
920 } OptsFromQDictState
;
922 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
924 OptsFromQDictState
*state
= opaque
;
925 char buf
[32], *tmp
= NULL
;
928 if (!strcmp(key
, "id") || *state
->errp
) {
932 switch (qobject_type(obj
)) {
934 value
= qstring_get_str(qobject_to(QString
, obj
));
937 tmp
= qnum_to_string(qobject_to(QNum
, obj
));
941 pstrcpy(buf
, sizeof(buf
),
942 qbool_get_bool(qobject_to(QBool
, obj
)) ? "on" : "off");
949 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
954 * Create QemuOpts from a QDict.
955 * Use value of key "id" as ID if it exists and is a QString. Only
956 * QStrings, QNums and QBools are copied. Entries with other types
957 * are silently ignored.
959 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
962 OptsFromQDictState state
;
963 Error
*local_err
= NULL
;
966 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
969 error_propagate(errp
, local_err
);
973 assert(opts
!= NULL
);
975 state
.errp
= &local_err
;
977 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
979 error_propagate(errp
, local_err
);
988 * Adds all QDict entries to the QemuOpts that can be added and removes them
989 * from the QDict. When this function returns, the QDict contains only those
990 * entries that couldn't be added to the QemuOpts.
992 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
994 const QDictEntry
*entry
, *next
;
996 entry
= qdict_first(qdict
);
998 while (entry
!= NULL
) {
999 Error
*local_err
= NULL
;
1000 OptsFromQDictState state
= {
1005 next
= qdict_next(qdict
, entry
);
1007 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1008 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1010 error_propagate(errp
, local_err
);
1013 qdict_del(qdict
, entry
->key
);
1022 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1024 * If @list is given, only add those options to the QDict that are contained in
1025 * the list. If @del is true, any options added to the QDict are removed from
1026 * the QemuOpts, otherwise they remain there.
1028 * If two options in @opts have the same name, they are processed in order
1029 * so that the last one wins (consistent with the reverse iteration in
1030 * qemu_opt_find()), but all of them are deleted if @del is true.
1032 * TODO We'll want to use types appropriate for opt->desc->type, but
1033 * this is enough for now.
1035 QDict
*qemu_opts_to_qdict_filtered(QemuOpts
*opts
, QDict
*qdict
,
1036 QemuOptsList
*list
, bool del
)
1038 QemuOpt
*opt
, *next
;
1041 qdict
= qdict_new();
1044 qdict_put_str(qdict
, "id", opts
->id
);
1046 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next
) {
1050 for (desc
= list
->desc
; desc
->name
; desc
++) {
1051 if (!strcmp(desc
->name
, opt
->name
)) {
1060 qdict_put_str(qdict
, opt
->name
, opt
->str
);
1068 /* Copy all options in a QemuOpts to the given QDict. See
1069 * qemu_opts_to_qdict_filtered() for details. */
1070 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1072 return qemu_opts_to_qdict_filtered(opts
, qdict
, NULL
, false);
1075 /* Validate parsed opts against descriptions where no
1076 * descriptions were provided in the QemuOptsList.
1078 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1081 Error
*local_err
= NULL
;
1083 assert(opts_accepts_any(opts
));
1085 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1086 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1088 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1092 qemu_opt_parse(opt
, &local_err
);
1094 error_propagate(errp
, local_err
);
1101 * For each member of @list, call @func(@opaque, member, @errp).
1102 * Call it with the current location temporarily set to the member's.
1103 * @func() may store an Error through @errp, but must return non-zero then.
1104 * When @func() returns non-zero, break the loop and return that value.
1105 * Return zero when the loop completes.
1107 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1108 void *opaque
, Error
**errp
)
1114 loc_push_none(&loc
);
1115 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1116 loc_restore(&opts
->loc
);
1117 rc
= func(opaque
, opts
, errp
);
1121 assert(!errp
|| !*errp
);
1127 static size_t count_opts_list(QemuOptsList
*list
)
1129 QemuOptDesc
*desc
= NULL
;
1130 size_t num_opts
= 0;
1137 while (desc
&& desc
->name
) {
1145 void qemu_opts_free(QemuOptsList
*list
)
1150 /* Realloc dst option list and append options from an option list (list)
1151 * to it. dst could be NULL or a malloced list.
1152 * The lifetime of dst must be shorter than the input list because the
1153 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1155 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1158 size_t num_opts
, num_dst_opts
;
1160 bool need_init
= false;
1161 bool need_head_update
;
1167 /* If dst is NULL, after realloc, some area of dst should be initialized
1168 * before adding options to it.
1172 need_head_update
= true;
1174 /* Moreover, even if dst is not NULL, the realloc may move it to a
1175 * different address in which case we may get a stale tail pointer
1177 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1180 num_opts
= count_opts_list(dst
);
1181 num_dst_opts
= num_opts
;
1182 num_opts
+= count_opts_list(list
);
1183 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1184 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1187 dst
->implied_opt_name
= NULL
;
1188 dst
->merge_lists
= false;
1190 if (need_head_update
) {
1191 QTAILQ_INIT(&dst
->head
);
1193 dst
->desc
[num_dst_opts
].name
= NULL
;
1195 /* append list->desc to dst->desc */
1198 while (desc
&& desc
->name
) {
1199 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1200 dst
->desc
[num_dst_opts
++] = *desc
;
1201 dst
->desc
[num_dst_opts
].name
= NULL
;