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"
36 * Extracts the name of an option from the parameter string (p points at the
37 * first byte of the option name)
39 * The option name is delimited by delim (usually , or =) or the string end
40 * and is copied into buf. If the option name is longer than buf_size, it is
41 * truncated. buf is always zero terminated.
43 * The return value is the position of the delimiter/zero byte after the option
46 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
51 while (*p
!= '\0' && *p
!= delim
) {
52 if (q
&& (q
- buf
) < buf_size
- 1)
63 * Extracts the value of an option from the parameter string p (p points at the
64 * first byte of the option value)
66 * This function is comparable to get_opt_name with the difference that the
67 * delimiter is fixed to be comma which starts a new option. To specify an
68 * option value that contains commas, double each comma.
70 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
81 if (q
&& (q
- buf
) < buf_size
- 1)
91 int get_next_param_value(char *buf
, int buf_size
,
92 const char *tag
, const char **pstr
)
99 p
= get_opt_name(option
, sizeof(option
), p
, '=');
103 if (!strcmp(tag
, option
)) {
104 *pstr
= get_opt_value(buf
, buf_size
, p
);
110 p
= get_opt_value(NULL
, 0, p
);
119 int get_param_value(char *buf
, int buf_size
,
120 const char *tag
, const char *str
)
122 return get_next_param_value(buf
, buf_size
, tag
, &str
);
125 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
129 if (!strcmp(value
, "on")) {
131 } else if (!strcmp(value
, "off")) {
134 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
135 name
, "'on' or 'off'");
142 static void parse_option_number(const char *name
, const char *value
,
143 uint64_t *ret
, Error
**errp
)
149 number
= strtoull(value
, &postfix
, 0);
150 if (*postfix
!= '\0') {
151 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
156 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
160 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
165 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
166 if (strcmp(desc
[i
].name
, name
) == 0) {
174 void parse_option_size(const char *name
, const char *value
,
175 uint64_t *ret
, Error
**errp
)
181 sizef
= strtod(value
, &postfix
);
182 if (sizef
< 0 || sizef
> UINT64_MAX
) {
183 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
184 "a non-negative number below 2^64");
203 *ret
= (uint64_t) sizef
;
206 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
207 error_append_hint(errp
, "You may use k, M, G or T suffixes for "
208 "kilobytes, megabytes, gigabytes and terabytes.\n");
212 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
216 bool has_help_option(const char *param
)
218 size_t buflen
= strlen(param
) + 1;
219 char *buf
= g_malloc(buflen
);
220 const char *p
= param
;
224 p
= get_opt_value(buf
, buflen
, p
);
229 if (is_help_option(buf
)) {
240 bool is_valid_option_list(const char *param
)
242 size_t buflen
= strlen(param
) + 1;
243 char *buf
= g_malloc(buflen
);
244 const char *p
= param
;
248 p
= get_opt_value(buf
, buflen
, p
);
254 if (!*buf
|| *buf
== ',') {
265 void qemu_opts_print_help(QemuOptsList
*list
)
271 printf("Supported options:\n");
272 while (desc
&& desc
->name
) {
273 printf("%-16s %s\n", desc
->name
,
274 desc
->help
? desc
->help
: "No description available");
278 /* ------------------------------------------------------------------ */
280 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
284 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
285 if (strcmp(opt
->name
, name
) != 0)
292 static void qemu_opt_del(QemuOpt
*opt
)
294 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
300 /* qemu_opt_set allows many settings for the same option.
301 * This function deletes all settings for an option.
303 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
305 QemuOpt
*opt
, *next_opt
;
307 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
308 if (!strcmp(opt
->name
, name
)) {
314 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
322 opt
= qemu_opt_find(opts
, name
);
324 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
325 if (desc
&& desc
->def_value_str
) {
326 return desc
->def_value_str
;
329 return opt
? opt
->str
: NULL
;
332 /* Get a known option (or its default) and remove it from the list
333 * all in one action. Return a malloced string of the option value.
334 * Result must be freed by caller with g_free().
336 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
339 const QemuOptDesc
*desc
;
346 opt
= qemu_opt_find(opts
, name
);
348 desc
= find_desc_by_name(opts
->list
->desc
, name
);
349 if (desc
&& desc
->def_value_str
) {
350 str
= g_strdup(desc
->def_value_str
);
356 qemu_opt_del_all(opts
, name
);
360 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
364 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
365 if (is_help_option(opt
->name
)) {
372 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
373 bool defval
, bool del
)
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_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
390 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
391 ret
= opt
->value
.boolean
;
393 qemu_opt_del_all(opts
, name
);
398 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
400 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
403 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
405 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
408 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
409 uint64_t defval
, bool del
)
412 uint64_t ret
= defval
;
418 opt
= qemu_opt_find(opts
, name
);
420 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
421 if (desc
&& desc
->def_value_str
) {
422 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
426 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
427 ret
= opt
->value
.uint
;
429 qemu_opt_del_all(opts
, name
);
434 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
436 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
439 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
442 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
445 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
446 uint64_t defval
, bool del
)
449 uint64_t ret
= defval
;
455 opt
= qemu_opt_find(opts
, name
);
457 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
458 if (desc
&& desc
->def_value_str
) {
459 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
463 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
464 ret
= opt
->value
.uint
;
466 qemu_opt_del_all(opts
, name
);
471 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
473 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
476 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
479 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
482 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
484 if (opt
->desc
== NULL
)
487 switch (opt
->desc
->type
) {
488 case QEMU_OPT_STRING
:
492 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
494 case QEMU_OPT_NUMBER
:
495 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
498 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
505 static bool opts_accepts_any(const QemuOpts
*opts
)
507 return opts
->list
->desc
[0].name
== NULL
;
510 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
512 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
514 assert(opts_accepts_any(opts
));
524 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
525 bool prepend
, Error
**errp
)
528 const QemuOptDesc
*desc
;
529 Error
*local_err
= NULL
;
531 desc
= find_desc_by_name(opts
->list
->desc
, name
);
532 if (!desc
&& !opts_accepts_any(opts
)) {
533 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
537 opt
= g_malloc0(sizeof(*opt
));
538 opt
->name
= g_strdup(name
);
541 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
543 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
546 opt
->str
= g_strdup(value
);
547 qemu_opt_parse(opt
, &local_err
);
549 error_propagate(errp
, local_err
);
554 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
557 opt_set(opts
, name
, value
, false, errp
);
560 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
564 const QemuOptDesc
*desc
= opts
->list
->desc
;
566 opt
= g_malloc0(sizeof(*opt
));
567 opt
->desc
= find_desc_by_name(desc
, name
);
568 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
569 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
574 opt
->name
= g_strdup(name
);
576 opt
->value
.boolean
= !!val
;
577 opt
->str
= g_strdup(val
? "on" : "off");
578 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
581 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
585 const QemuOptDesc
*desc
= opts
->list
->desc
;
587 opt
= g_malloc0(sizeof(*opt
));
588 opt
->desc
= find_desc_by_name(desc
, name
);
589 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
590 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
595 opt
->name
= g_strdup(name
);
597 opt
->value
.uint
= val
;
598 opt
->str
= g_strdup_printf("%" PRId64
, val
);
599 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
603 * For each member of @opts, call @func(@opaque, name, value, @errp).
604 * @func() may store an Error through @errp, but must return non-zero then.
605 * When @func() returns non-zero, break the loop and return that value.
606 * Return zero when the loop completes.
608 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
614 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
615 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
619 assert(!errp
|| !*errp
);
624 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
628 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
629 if (!opts
->id
&& !id
) {
632 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
639 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
640 int fail_if_exists
, Error
**errp
)
642 QemuOpts
*opts
= NULL
;
645 if (!id_wellformed(id
)) {
646 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
648 error_append_hint(errp
, "Identifiers consist of letters, digits, "
649 "'-', '.', '_', starting with a letter.\n");
652 opts
= qemu_opts_find(list
, id
);
654 if (fail_if_exists
&& !list
->merge_lists
) {
655 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
661 } else if (list
->merge_lists
) {
662 opts
= qemu_opts_find(list
, NULL
);
667 opts
= g_malloc0(sizeof(*opts
));
668 opts
->id
= g_strdup(id
);
670 loc_save(&opts
->loc
);
671 QTAILQ_INIT(&opts
->head
);
672 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
676 void qemu_opts_reset(QemuOptsList
*list
)
678 QemuOpts
*opts
, *next_opts
;
680 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
685 void qemu_opts_loc_restore(QemuOpts
*opts
)
687 loc_restore(&opts
->loc
);
690 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
691 const char *name
, const char *value
, Error
**errp
)
694 Error
*local_err
= NULL
;
696 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
698 error_propagate(errp
, local_err
);
701 qemu_opt_set(opts
, name
, value
, errp
);
704 const char *qemu_opts_id(QemuOpts
*opts
)
709 /* The id string will be g_free()d by qemu_opts_del */
710 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
715 void qemu_opts_del(QemuOpts
*opts
)
724 opt
= QTAILQ_FIRST(&opts
->head
);
729 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
734 /* print value, escaping any commas in value */
735 static void escaped_print(const char *value
)
739 for (ptr
= value
; *ptr
; ++ptr
) {
747 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
750 QemuOptDesc
*desc
= opts
->list
->desc
;
751 const char *sep
= "";
754 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
758 if (desc
[0].name
== NULL
) {
759 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
760 printf("%s%s=", sep
, opt
->name
);
761 escaped_print(opt
->str
);
766 for (; desc
&& desc
->name
; desc
++) {
768 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
770 value
= opt
? opt
->str
: desc
->def_value_str
;
774 if (desc
->type
== QEMU_OPT_STRING
) {
775 printf("%s%s=", sep
, desc
->name
);
776 escaped_print(value
);
777 } else if ((desc
->type
== QEMU_OPT_SIZE
||
778 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
779 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
781 printf("%s%s=%s", sep
, desc
->name
, value
);
787 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
788 const char *firstname
, bool prepend
, Error
**errp
)
790 char option
[128], value
[1024];
791 const char *p
,*pe
,*pc
;
792 Error
*local_err
= NULL
;
794 for (p
= params
; *p
!= '\0'; p
++) {
797 if (!pe
|| (pc
&& pc
< pe
)) {
798 /* found "foo,more" */
799 if (p
== params
&& firstname
) {
800 /* implicitly named first option */
801 pstrcpy(option
, sizeof(option
), firstname
);
802 p
= get_opt_value(value
, sizeof(value
), p
);
804 /* option without value, probably a flag */
805 p
= get_opt_name(option
, sizeof(option
), p
, ',');
806 if (strncmp(option
, "no", 2) == 0) {
807 memmove(option
, option
+2, strlen(option
+2)+1);
808 pstrcpy(value
, sizeof(value
), "off");
810 pstrcpy(value
, sizeof(value
), "on");
814 /* found "foo=bar,more" */
815 p
= get_opt_name(option
, sizeof(option
), p
, '=');
820 p
= get_opt_value(value
, sizeof(value
), p
);
822 if (strcmp(option
, "id") != 0) {
823 /* store and parse */
824 opt_set(opts
, option
, value
, prepend
, &local_err
);
826 error_propagate(errp
, local_err
);
837 * Store options parsed from @params into @opts.
838 * If @firstname is non-null, the first key=value in @params may omit
839 * key=, and is treated as if key was @firstname.
840 * On error, store an error object through @errp if non-null.
842 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
843 const char *firstname
, Error
**errp
)
845 opts_do_parse(opts
, params
, firstname
, false, errp
);
848 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
849 bool permit_abbrev
, bool defaults
, Error
**errp
)
851 const char *firstname
;
852 char value
[1024], *id
= NULL
;
855 Error
*local_err
= NULL
;
857 assert(!permit_abbrev
|| list
->implied_opt_name
);
858 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
860 if (strncmp(params
, "id=", 3) == 0) {
861 get_opt_value(value
, sizeof(value
), params
+3);
863 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
864 get_opt_value(value
, sizeof(value
), p
+4);
869 * This code doesn't work for defaults && !list->merge_lists: when
870 * params has no id=, and list has an element with !opts->id, it
871 * appends a new element instead of returning the existing opts.
872 * However, we got no use for this case. Guard against possible
873 * (if unlikely) future misuse:
875 assert(!defaults
|| list
->merge_lists
);
876 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
878 error_propagate(errp
, local_err
);
882 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
884 error_propagate(errp
, local_err
);
893 * Create a QemuOpts in @list and with options parsed from @params.
894 * If @permit_abbrev, the first key=value in @params may omit key=,
895 * and is treated as if key was @list->implied_opt_name.
896 * On error, store an error object through @errp if non-null.
897 * Return the new QemuOpts on success, null pointer on error.
899 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
900 bool permit_abbrev
, Error
**errp
)
902 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
906 * Create a QemuOpts in @list and with options parsed from @params.
907 * If @permit_abbrev, the first key=value in @params may omit key=,
908 * and is treated as if key was @list->implied_opt_name.
909 * Report errors with error_report_err(). This is inappropriate in
910 * QMP context. Do not use this function there!
911 * Return the new QemuOpts on success, null pointer on error.
913 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
919 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
921 error_report_err(err
);
926 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
931 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
935 typedef struct OptsFromQDictState
{
938 } OptsFromQDictState
;
940 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
942 OptsFromQDictState
*state
= opaque
;
947 if (!strcmp(key
, "id") || *state
->errp
) {
951 switch (qobject_type(obj
)) {
953 value
= qstring_get_str(qobject_to_qstring(obj
));
956 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
957 qint_get_int(qobject_to_qint(obj
)));
958 assert(n
< sizeof(buf
));
962 n
= snprintf(buf
, sizeof(buf
), "%.17g",
963 qfloat_get_double(qobject_to_qfloat(obj
)));
964 assert(n
< sizeof(buf
));
968 pstrcpy(buf
, sizeof(buf
),
969 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
976 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
980 * Create QemuOpts from a QDict.
981 * Use value of key "id" as ID if it exists and is a QString.
982 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
983 * other types are silently ignored.
985 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
988 OptsFromQDictState state
;
989 Error
*local_err
= NULL
;
992 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
995 error_propagate(errp
, local_err
);
999 assert(opts
!= NULL
);
1001 state
.errp
= &local_err
;
1003 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1005 error_propagate(errp
, local_err
);
1006 qemu_opts_del(opts
);
1014 * Adds all QDict entries to the QemuOpts that can be added and removes them
1015 * from the QDict. When this function returns, the QDict contains only those
1016 * entries that couldn't be added to the QemuOpts.
1018 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1020 const QDictEntry
*entry
, *next
;
1022 entry
= qdict_first(qdict
);
1024 while (entry
!= NULL
) {
1025 Error
*local_err
= NULL
;
1026 OptsFromQDictState state
= {
1031 next
= qdict_next(qdict
, entry
);
1033 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1034 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1036 error_propagate(errp
, local_err
);
1039 qdict_del(qdict
, entry
->key
);
1048 * Convert from QemuOpts to QDict.
1049 * The QDict values are of type QString.
1050 * TODO We'll want to use types appropriate for opt->desc->type, but
1051 * this is enough for now.
1053 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1059 qdict
= qdict_new();
1062 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1064 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1065 val
= QOBJECT(qstring_from_str(opt
->str
));
1066 qdict_put_obj(qdict
, opt
->name
, val
);
1071 /* Validate parsed opts against descriptions where no
1072 * descriptions were provided in the QemuOptsList.
1074 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1077 Error
*local_err
= NULL
;
1079 assert(opts_accepts_any(opts
));
1081 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1082 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1084 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1088 qemu_opt_parse(opt
, &local_err
);
1090 error_propagate(errp
, local_err
);
1097 * For each member of @list, call @func(@opaque, member, @errp).
1098 * Call it with the current location temporarily set to the member's.
1099 * @func() may store an Error through @errp, but must return non-zero then.
1100 * When @func() returns non-zero, break the loop and return that value.
1101 * Return zero when the loop completes.
1103 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1104 void *opaque
, Error
**errp
)
1110 loc_push_none(&loc
);
1111 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1112 loc_restore(&opts
->loc
);
1113 rc
= func(opaque
, opts
, errp
);
1117 assert(!errp
|| !*errp
);
1123 static size_t count_opts_list(QemuOptsList
*list
)
1125 QemuOptDesc
*desc
= NULL
;
1126 size_t num_opts
= 0;
1133 while (desc
&& desc
->name
) {
1141 void qemu_opts_free(QemuOptsList
*list
)
1146 /* Realloc dst option list and append options from an option list (list)
1147 * to it. dst could be NULL or a malloced list.
1148 * The lifetime of dst must be shorter than the input list because the
1149 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1151 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1154 size_t num_opts
, num_dst_opts
;
1156 bool need_init
= false;
1157 bool need_head_update
;
1163 /* If dst is NULL, after realloc, some area of dst should be initialized
1164 * before adding options to it.
1168 need_head_update
= true;
1170 /* Moreover, even if dst is not NULL, the realloc may move it to a
1171 * different address in which case we may get a stale tail pointer
1173 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1176 num_opts
= count_opts_list(dst
);
1177 num_dst_opts
= num_opts
;
1178 num_opts
+= count_opts_list(list
);
1179 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1180 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1183 dst
->implied_opt_name
= NULL
;
1184 dst
->merge_lists
= false;
1186 if (need_head_update
) {
1187 QTAILQ_INIT(&dst
->head
);
1189 dst
->desc
[num_dst_opts
].name
= NULL
;
1191 /* append list->desc to dst->desc */
1194 while (desc
&& desc
->name
) {
1195 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1196 dst
->desc
[num_dst_opts
++] = *desc
;
1197 dst
->desc
[num_dst_opts
].name
= NULL
;