2 * Commandline option parsing functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu-common.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/types.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qemu/option_int.h"
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
40 * The option name is delimited by delim (usually , or =) or the string end
41 * and is copied into buf. If the option name is longer than buf_size, it is
42 * truncated. buf is always zero terminated.
44 * The return value is the position of the delimiter/zero byte after the option
47 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
52 while (*p
!= '\0' && *p
!= delim
) {
53 if (q
&& (q
- buf
) < buf_size
- 1)
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
67 * This function is comparable to get_opt_name with the difference that the
68 * delimiter is fixed to be comma which starts a new option. To specify an
69 * option value that contains commas, double each comma.
71 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
82 if (q
&& (q
- buf
) < buf_size
- 1)
92 int get_next_param_value(char *buf
, int buf_size
,
93 const char *tag
, const char **pstr
)
100 p
= get_opt_name(option
, sizeof(option
), p
, '=');
104 if (!strcmp(tag
, option
)) {
105 *pstr
= get_opt_value(buf
, buf_size
, p
);
111 p
= get_opt_value(NULL
, 0, p
);
120 int get_param_value(char *buf
, int buf_size
,
121 const char *tag
, const char *str
)
123 return get_next_param_value(buf
, buf_size
, tag
, &str
);
126 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
130 if (!strcmp(value
, "on")) {
132 } else if (!strcmp(value
, "off")) {
135 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, 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_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
156 error_set(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
);
198 *ret
= (uint64_t) sizef
;
201 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
202 #if 0 /* conversion from qerror_report() to error_set() broke this: */
203 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
204 "kilobytes, megabytes, gigabytes and terabytes.\n");
209 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
213 bool has_help_option(const char *param
)
215 size_t buflen
= strlen(param
) + 1;
216 char *buf
= g_malloc(buflen
);
217 const char *p
= param
;
221 p
= get_opt_value(buf
, buflen
, p
);
226 if (is_help_option(buf
)) {
237 bool is_valid_option_list(const char *param
)
239 size_t buflen
= strlen(param
) + 1;
240 char *buf
= g_malloc(buflen
);
241 const char *p
= param
;
245 p
= get_opt_value(buf
, buflen
, p
);
251 if (!*buf
|| *buf
== ',') {
262 void qemu_opts_print_help(QemuOptsList
*list
)
268 printf("Supported options:\n");
269 while (desc
&& desc
->name
) {
270 printf("%-16s %s\n", desc
->name
,
271 desc
->help
? desc
->help
: "No description available");
275 /* ------------------------------------------------------------------ */
277 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
281 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
282 if (strcmp(opt
->name
, name
) != 0)
289 static void qemu_opt_del(QemuOpt
*opt
)
291 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
297 /* qemu_opt_set allows many settings for the same option.
298 * This function deletes all settings for an option.
300 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
302 QemuOpt
*opt
, *next_opt
;
304 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
305 if (!strcmp(opt
->name
, name
)) {
311 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
319 opt
= qemu_opt_find(opts
, name
);
321 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
322 if (desc
&& desc
->def_value_str
) {
323 return desc
->def_value_str
;
326 return opt
? opt
->str
: NULL
;
329 /* Get a known option (or its default) and remove it from the list
330 * all in one action. Return a malloced string of the option value.
331 * Result must be freed by caller with g_free().
333 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
336 const QemuOptDesc
*desc
;
343 opt
= qemu_opt_find(opts
, name
);
345 desc
= find_desc_by_name(opts
->list
->desc
, name
);
346 if (desc
&& desc
->def_value_str
) {
347 str
= g_strdup(desc
->def_value_str
);
353 qemu_opt_del_all(opts
, name
);
357 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
361 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
362 if (is_help_option(opt
->name
)) {
369 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
370 bool defval
, bool del
)
379 opt
= qemu_opt_find(opts
, name
);
381 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
382 if (desc
&& desc
->def_value_str
) {
383 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
387 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
388 ret
= opt
->value
.boolean
;
390 qemu_opt_del_all(opts
, name
);
395 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
397 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
400 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
402 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
405 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
406 uint64_t defval
, bool del
)
409 uint64_t ret
= defval
;
415 opt
= qemu_opt_find(opts
, name
);
417 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
418 if (desc
&& desc
->def_value_str
) {
419 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
423 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
424 ret
= opt
->value
.uint
;
426 qemu_opt_del_all(opts
, name
);
431 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
433 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
436 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
439 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
442 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
443 uint64_t defval
, bool del
)
446 uint64_t ret
= defval
;
452 opt
= qemu_opt_find(opts
, name
);
454 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
455 if (desc
&& desc
->def_value_str
) {
456 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
460 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
461 ret
= opt
->value
.uint
;
463 qemu_opt_del_all(opts
, name
);
468 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
470 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
473 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
476 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
479 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
481 if (opt
->desc
== NULL
)
484 switch (opt
->desc
->type
) {
485 case QEMU_OPT_STRING
:
489 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
491 case QEMU_OPT_NUMBER
:
492 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
495 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
502 static bool opts_accepts_any(const QemuOpts
*opts
)
504 return opts
->list
->desc
[0].name
== NULL
;
507 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
509 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
511 assert(opts_accepts_any(opts
));
521 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
522 bool prepend
, Error
**errp
)
525 const QemuOptDesc
*desc
;
526 Error
*local_err
= NULL
;
528 desc
= find_desc_by_name(opts
->list
->desc
, name
);
529 if (!desc
&& !opts_accepts_any(opts
)) {
530 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
534 opt
= g_malloc0(sizeof(*opt
));
535 opt
->name
= g_strdup(name
);
538 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
540 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
543 opt
->str
= g_strdup(value
);
544 qemu_opt_parse(opt
, &local_err
);
546 error_propagate(errp
, local_err
);
551 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
553 Error
*local_err
= NULL
;
555 opt_set(opts
, name
, value
, false, &local_err
);
557 qerror_report_err(local_err
);
558 error_free(local_err
);
565 void qemu_opt_set_err(QemuOpts
*opts
, const char *name
, const char *value
,
568 opt_set(opts
, name
, value
, false, errp
);
571 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
575 const QemuOptDesc
*desc
= opts
->list
->desc
;
577 opt
= g_malloc0(sizeof(*opt
));
578 opt
->desc
= find_desc_by_name(desc
, name
);
579 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
580 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
585 opt
->name
= g_strdup(name
);
587 opt
->value
.boolean
= !!val
;
588 opt
->str
= g_strdup(val
? "on" : "off");
589 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
592 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
596 const QemuOptDesc
*desc
= opts
->list
->desc
;
598 opt
= g_malloc0(sizeof(*opt
));
599 opt
->desc
= find_desc_by_name(desc
, name
);
600 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
601 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
606 opt
->name
= g_strdup(name
);
608 opt
->value
.uint
= val
;
609 opt
->str
= g_strdup_printf("%" PRId64
, val
);
610 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
613 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
614 int abort_on_failure
)
619 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
620 rc
= func(opt
->name
, opt
->str
, opaque
);
621 if (abort_on_failure
&& rc
!= 0)
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_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
650 #if 0 /* conversion from qerror_report() to error_set() broke this: */
651 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', 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_err(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 void qemu_opts_print(QemuOpts
*opts
, const char *sep
)
740 QemuOptDesc
*desc
= opts
->list
->desc
;
742 if (desc
[0].name
== NULL
) {
743 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
744 printf("%s%s=\"%s\"", sep
, opt
->name
, opt
->str
);
748 for (; desc
&& desc
->name
; desc
++) {
750 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
752 value
= opt
? opt
->str
: desc
->def_value_str
;
756 if (desc
->type
== QEMU_OPT_STRING
) {
757 printf("%s%s='%s'", sep
, desc
->name
, value
);
758 } else if ((desc
->type
== QEMU_OPT_SIZE
||
759 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
760 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
762 printf("%s%s=%s", sep
, desc
->name
, value
);
767 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
768 const char *firstname
, bool prepend
)
770 char option
[128], value
[1024];
771 const char *p
,*pe
,*pc
;
772 Error
*local_err
= NULL
;
774 for (p
= params
; *p
!= '\0'; p
++) {
777 if (!pe
|| (pc
&& pc
< pe
)) {
778 /* found "foo,more" */
779 if (p
== params
&& firstname
) {
780 /* implicitly named first option */
781 pstrcpy(option
, sizeof(option
), firstname
);
782 p
= get_opt_value(value
, sizeof(value
), p
);
784 /* option without value, probably a flag */
785 p
= get_opt_name(option
, sizeof(option
), p
, ',');
786 if (strncmp(option
, "no", 2) == 0) {
787 memmove(option
, option
+2, strlen(option
+2)+1);
788 pstrcpy(value
, sizeof(value
), "off");
790 pstrcpy(value
, sizeof(value
), "on");
794 /* found "foo=bar,more" */
795 p
= get_opt_name(option
, sizeof(option
), p
, '=');
800 p
= get_opt_value(value
, sizeof(value
), p
);
802 if (strcmp(option
, "id") != 0) {
803 /* store and parse */
804 opt_set(opts
, option
, value
, prepend
, &local_err
);
806 qerror_report_err(local_err
);
807 error_free(local_err
);
818 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
820 return opts_do_parse(opts
, params
, firstname
, false);
823 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
824 int permit_abbrev
, bool defaults
)
826 const char *firstname
;
827 char value
[1024], *id
= NULL
;
830 Error
*local_err
= NULL
;
832 assert(!permit_abbrev
|| list
->implied_opt_name
);
833 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
835 if (strncmp(params
, "id=", 3) == 0) {
836 get_opt_value(value
, sizeof(value
), params
+3);
838 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
839 get_opt_value(value
, sizeof(value
), p
+4);
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 qerror_report_err(local_err
);
855 error_free(local_err
);
860 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
868 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
871 return opts_parse(list
, params
, permit_abbrev
, false);
874 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
879 opts
= opts_parse(list
, params
, permit_abbrev
, true);
883 typedef struct OptsFromQDictState
{
886 } OptsFromQDictState
;
888 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
890 OptsFromQDictState
*state
= opaque
;
895 if (!strcmp(key
, "id") || *state
->errp
) {
899 switch (qobject_type(obj
)) {
901 value
= qstring_get_str(qobject_to_qstring(obj
));
904 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
905 qint_get_int(qobject_to_qint(obj
)));
906 assert(n
< sizeof(buf
));
910 n
= snprintf(buf
, sizeof(buf
), "%.17g",
911 qfloat_get_double(qobject_to_qfloat(obj
)));
912 assert(n
< sizeof(buf
));
916 pstrcpy(buf
, sizeof(buf
),
917 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
924 qemu_opt_set_err(state
->opts
, key
, value
, state
->errp
);
928 * Create QemuOpts from a QDict.
929 * Use value of key "id" as ID if it exists and is a QString.
930 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
931 * other types are silently ignored.
933 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
936 OptsFromQDictState state
;
937 Error
*local_err
= NULL
;
940 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
943 error_propagate(errp
, local_err
);
947 assert(opts
!= NULL
);
949 state
.errp
= &local_err
;
951 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
953 error_propagate(errp
, local_err
);
962 * Adds all QDict entries to the QemuOpts that can be added and removes them
963 * from the QDict. When this function returns, the QDict contains only those
964 * entries that couldn't be added to the QemuOpts.
966 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
968 const QDictEntry
*entry
, *next
;
970 entry
= qdict_first(qdict
);
972 while (entry
!= NULL
) {
973 Error
*local_err
= NULL
;
974 OptsFromQDictState state
= {
979 next
= qdict_next(qdict
, entry
);
981 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
982 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
984 error_propagate(errp
, local_err
);
987 qdict_del(qdict
, entry
->key
);
996 * Convert from QemuOpts to QDict.
997 * The QDict values are of type QString.
998 * TODO We'll want to use types appropriate for opt->desc->type, but
999 * this is enough for now.
1001 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1007 qdict
= qdict_new();
1010 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1012 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1013 val
= QOBJECT(qstring_from_str(opt
->str
));
1014 qdict_put_obj(qdict
, opt
->name
, val
);
1019 /* Validate parsed opts against descriptions where no
1020 * descriptions were provided in the QemuOptsList.
1022 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1025 Error
*local_err
= NULL
;
1027 assert(opts_accepts_any(opts
));
1029 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1030 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1032 error_set(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1036 qemu_opt_parse(opt
, &local_err
);
1038 error_propagate(errp
, local_err
);
1044 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1045 int abort_on_failure
)
1051 loc_push_none(&loc
);
1052 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1053 loc_restore(&opts
->loc
);
1054 rc
|= func(opts
, opaque
);
1055 if (abort_on_failure
&& rc
!= 0)
1062 static size_t count_opts_list(QemuOptsList
*list
)
1064 QemuOptDesc
*desc
= NULL
;
1065 size_t num_opts
= 0;
1072 while (desc
&& desc
->name
) {
1080 void qemu_opts_free(QemuOptsList
*list
)
1085 /* Realloc dst option list and append options from an option list (list)
1086 * to it. dst could be NULL or a malloced list.
1087 * The lifetime of dst must be shorter than the input list because the
1088 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1090 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1093 size_t num_opts
, num_dst_opts
;
1095 bool need_init
= false;
1096 bool need_head_update
;
1102 /* If dst is NULL, after realloc, some area of dst should be initialized
1103 * before adding options to it.
1107 need_head_update
= true;
1109 /* Moreover, even if dst is not NULL, the realloc may move it to a
1110 * different address in which case we may get a stale tail pointer
1112 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1115 num_opts
= count_opts_list(dst
);
1116 num_dst_opts
= num_opts
;
1117 num_opts
+= count_opts_list(list
);
1118 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1119 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1122 dst
->implied_opt_name
= NULL
;
1123 dst
->merge_lists
= false;
1125 if (need_head_update
) {
1126 QTAILQ_INIT(&dst
->head
);
1128 dst
->desc
[num_dst_opts
].name
= NULL
;
1130 /* append list->desc to dst->desc */
1133 while (desc
&& desc
->name
) {
1134 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1135 dst
->desc
[num_dst_opts
++] = *desc
;
1136 dst
->desc
[num_dst_opts
].name
= NULL
;