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_malloc0(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_malloc0(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
)
313 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
316 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
317 if (desc
&& desc
->def_value_str
) {
318 return desc
->def_value_str
;
321 return opt
? opt
->str
: NULL
;
324 /* Get a known option (or its default) and remove it from the list
325 * all in one action. Return a malloced string of the option value.
326 * Result must be freed by caller with g_free().
328 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
331 const QemuOptDesc
*desc
;
338 opt
= qemu_opt_find(opts
, name
);
340 desc
= find_desc_by_name(opts
->list
->desc
, name
);
341 if (desc
&& desc
->def_value_str
) {
342 str
= g_strdup(desc
->def_value_str
);
348 qemu_opt_del_all(opts
, name
);
352 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
356 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
357 if (is_help_option(opt
->name
)) {
364 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
365 bool defval
, bool del
)
367 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
371 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
372 if (desc
&& desc
->def_value_str
) {
373 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
377 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
378 ret
= opt
->value
.boolean
;
380 qemu_opt_del_all(opts
, name
);
385 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
387 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
390 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
392 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
395 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
396 uint64_t defval
, bool del
)
398 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
399 uint64_t ret
= defval
;
402 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
403 if (desc
&& desc
->def_value_str
) {
404 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
408 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
409 ret
= opt
->value
.uint
;
411 qemu_opt_del_all(opts
, name
);
416 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
418 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
421 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
424 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
427 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
428 uint64_t defval
, bool del
)
430 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
431 uint64_t ret
= defval
;
434 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
435 if (desc
&& desc
->def_value_str
) {
436 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
440 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
441 ret
= opt
->value
.uint
;
443 qemu_opt_del_all(opts
, name
);
448 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
450 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
453 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
456 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
459 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
461 if (opt
->desc
== NULL
)
464 switch (opt
->desc
->type
) {
465 case QEMU_OPT_STRING
:
469 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
471 case QEMU_OPT_NUMBER
:
472 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
475 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
482 static bool opts_accepts_any(const QemuOpts
*opts
)
484 return opts
->list
->desc
[0].name
== NULL
;
487 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
489 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
491 assert(opts_accepts_any(opts
));
501 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
502 bool prepend
, Error
**errp
)
505 const QemuOptDesc
*desc
;
506 Error
*local_err
= NULL
;
508 desc
= find_desc_by_name(opts
->list
->desc
, name
);
509 if (!desc
&& !opts_accepts_any(opts
)) {
510 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
514 opt
= g_malloc0(sizeof(*opt
));
515 opt
->name
= g_strdup(name
);
518 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
520 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
523 opt
->str
= g_strdup(value
);
524 qemu_opt_parse(opt
, &local_err
);
526 error_propagate(errp
, local_err
);
531 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
533 Error
*local_err
= NULL
;
535 opt_set(opts
, name
, value
, false, &local_err
);
537 qerror_report_err(local_err
);
538 error_free(local_err
);
545 void qemu_opt_set_err(QemuOpts
*opts
, const char *name
, const char *value
,
548 opt_set(opts
, name
, value
, false, errp
);
551 int qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
)
554 const QemuOptDesc
*desc
= opts
->list
->desc
;
556 opt
= g_malloc0(sizeof(*opt
));
557 opt
->desc
= find_desc_by_name(desc
, name
);
558 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
559 qerror_report(QERR_INVALID_PARAMETER
, name
);
564 opt
->name
= g_strdup(name
);
566 opt
->value
.boolean
= !!val
;
567 opt
->str
= g_strdup(val
? "on" : "off");
568 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
573 int qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
)
576 const QemuOptDesc
*desc
= opts
->list
->desc
;
578 opt
= g_malloc0(sizeof(*opt
));
579 opt
->desc
= find_desc_by_name(desc
, name
);
580 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
581 qerror_report(QERR_INVALID_PARAMETER
, name
);
586 opt
->name
= g_strdup(name
);
588 opt
->value
.uint
= val
;
589 opt
->str
= g_strdup_printf("%" PRId64
, val
);
590 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
595 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
596 int abort_on_failure
)
601 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
602 rc
= func(opt
->name
, opt
->str
, opaque
);
603 if (abort_on_failure
&& rc
!= 0)
609 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
613 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
614 if (!opts
->id
&& !id
) {
617 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
624 static int id_wellformed(const char *id
)
628 if (!qemu_isalpha(id
[0])) {
631 for (i
= 1; id
[i
]; i
++) {
632 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
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_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
647 #if 0 /* conversion from qerror_report() to error_set() broke this: */
648 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', 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 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
691 const char *name
, const char *value
)
694 Error
*local_err
= NULL
;
696 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
698 qerror_report_err(local_err
);
699 error_free(local_err
);
702 return qemu_opt_set(opts
, name
, value
);
705 const char *qemu_opts_id(QemuOpts
*opts
)
710 /* The id string will be g_free()d by qemu_opts_del */
711 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
716 void qemu_opts_del(QemuOpts
*opts
)
725 opt
= QTAILQ_FIRST(&opts
->head
);
730 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
735 void qemu_opts_print(QemuOpts
*opts
)
738 QemuOptDesc
*desc
= opts
->list
->desc
;
740 if (desc
[0].name
== NULL
) {
741 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
742 printf("%s=\"%s\" ", opt
->name
, opt
->str
);
746 for (; desc
&& desc
->name
; desc
++) {
748 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
750 value
= opt
? opt
->str
: desc
->def_value_str
;
754 if (desc
->type
== QEMU_OPT_STRING
) {
755 printf("%s='%s' ", desc
->name
, value
);
756 } else if ((desc
->type
== QEMU_OPT_SIZE
||
757 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
758 printf("%s=%" PRId64
" ", desc
->name
, opt
->value
.uint
);
760 printf("%s=%s ", desc
->name
, value
);
765 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
766 const char *firstname
, bool prepend
)
768 char option
[128], value
[1024];
769 const char *p
,*pe
,*pc
;
770 Error
*local_err
= NULL
;
772 for (p
= params
; *p
!= '\0'; p
++) {
775 if (!pe
|| (pc
&& pc
< pe
)) {
776 /* found "foo,more" */
777 if (p
== params
&& firstname
) {
778 /* implicitly named first option */
779 pstrcpy(option
, sizeof(option
), firstname
);
780 p
= get_opt_value(value
, sizeof(value
), p
);
782 /* option without value, probably a flag */
783 p
= get_opt_name(option
, sizeof(option
), p
, ',');
784 if (strncmp(option
, "no", 2) == 0) {
785 memmove(option
, option
+2, strlen(option
+2)+1);
786 pstrcpy(value
, sizeof(value
), "off");
788 pstrcpy(value
, sizeof(value
), "on");
792 /* found "foo=bar,more" */
793 p
= get_opt_name(option
, sizeof(option
), p
, '=');
798 p
= get_opt_value(value
, sizeof(value
), p
);
800 if (strcmp(option
, "id") != 0) {
801 /* store and parse */
802 opt_set(opts
, option
, value
, prepend
, &local_err
);
804 qerror_report_err(local_err
);
805 error_free(local_err
);
816 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
818 return opts_do_parse(opts
, params
, firstname
, false);
821 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
822 int permit_abbrev
, bool defaults
)
824 const char *firstname
;
825 char value
[1024], *id
= NULL
;
828 Error
*local_err
= NULL
;
830 assert(!permit_abbrev
|| list
->implied_opt_name
);
831 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
833 if (strncmp(params
, "id=", 3) == 0) {
834 get_opt_value(value
, sizeof(value
), params
+3);
836 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
837 get_opt_value(value
, sizeof(value
), p
+4);
842 * This code doesn't work for defaults && !list->merge_lists: when
843 * params has no id=, and list has an element with !opts->id, it
844 * appends a new element instead of returning the existing opts.
845 * However, we got no use for this case. Guard against possible
846 * (if unlikely) future misuse:
848 assert(!defaults
|| list
->merge_lists
);
849 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
852 qerror_report_err(local_err
);
853 error_free(local_err
);
858 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
866 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
869 return opts_parse(list
, params
, permit_abbrev
, false);
872 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
877 opts
= opts_parse(list
, params
, permit_abbrev
, true);
881 typedef struct OptsFromQDictState
{
884 } OptsFromQDictState
;
886 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
888 OptsFromQDictState
*state
= opaque
;
893 if (!strcmp(key
, "id") || *state
->errp
) {
897 switch (qobject_type(obj
)) {
899 value
= qstring_get_str(qobject_to_qstring(obj
));
902 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
903 qint_get_int(qobject_to_qint(obj
)));
904 assert(n
< sizeof(buf
));
908 n
= snprintf(buf
, sizeof(buf
), "%.17g",
909 qfloat_get_double(qobject_to_qfloat(obj
)));
910 assert(n
< sizeof(buf
));
914 pstrcpy(buf
, sizeof(buf
),
915 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
922 qemu_opt_set_err(state
->opts
, key
, value
, state
->errp
);
926 * Create QemuOpts from a QDict.
927 * Use value of key "id" as ID if it exists and is a QString.
928 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
929 * other types are silently ignored.
931 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
934 OptsFromQDictState state
;
935 Error
*local_err
= NULL
;
938 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
941 error_propagate(errp
, local_err
);
945 assert(opts
!= NULL
);
947 state
.errp
= &local_err
;
949 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
951 error_propagate(errp
, local_err
);
960 * Adds all QDict entries to the QemuOpts that can be added and removes them
961 * from the QDict. When this function returns, the QDict contains only those
962 * entries that couldn't be added to the QemuOpts.
964 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
966 const QDictEntry
*entry
, *next
;
968 entry
= qdict_first(qdict
);
970 while (entry
!= NULL
) {
971 Error
*local_err
= NULL
;
972 OptsFromQDictState state
= {
977 next
= qdict_next(qdict
, entry
);
979 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
980 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
982 error_propagate(errp
, local_err
);
985 qdict_del(qdict
, entry
->key
);
994 * Convert from QemuOpts to QDict.
995 * The QDict values are of type QString.
996 * TODO We'll want to use types appropriate for opt->desc->type, but
997 * this is enough for now.
999 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1005 qdict
= qdict_new();
1008 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1010 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1011 val
= QOBJECT(qstring_from_str(opt
->str
));
1012 qdict_put_obj(qdict
, opt
->name
, val
);
1017 /* Validate parsed opts against descriptions where no
1018 * descriptions were provided in the QemuOptsList.
1020 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1023 Error
*local_err
= NULL
;
1025 assert(opts_accepts_any(opts
));
1027 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1028 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1030 error_set(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1034 qemu_opt_parse(opt
, &local_err
);
1036 error_propagate(errp
, local_err
);
1042 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1043 int abort_on_failure
)
1049 loc_push_none(&loc
);
1050 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1051 loc_restore(&opts
->loc
);
1052 rc
|= func(opts
, opaque
);
1053 if (abort_on_failure
&& rc
!= 0)
1060 static size_t count_opts_list(QemuOptsList
*list
)
1062 QemuOptDesc
*desc
= NULL
;
1063 size_t num_opts
= 0;
1070 while (desc
&& desc
->name
) {
1078 void qemu_opts_free(QemuOptsList
*list
)
1083 /* Realloc dst option list and append options from an option list (list)
1084 * to it. dst could be NULL or a malloced list.
1085 * The lifetime of dst must be shorter than the input list because the
1086 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1088 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1091 size_t num_opts
, num_dst_opts
;
1093 bool need_init
= false;
1099 /* If dst is NULL, after realloc, some area of dst should be initialized
1100 * before adding options to it.
1106 num_opts
= count_opts_list(dst
);
1107 num_dst_opts
= num_opts
;
1108 num_opts
+= count_opts_list(list
);
1109 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1110 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1113 dst
->implied_opt_name
= NULL
;
1114 QTAILQ_INIT(&dst
->head
);
1115 dst
->merge_lists
= false;
1117 dst
->desc
[num_dst_opts
].name
= NULL
;
1119 /* append list->desc to dst->desc */
1122 while (desc
&& desc
->name
) {
1123 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1124 dst
->desc
[num_dst_opts
++] = *desc
;
1125 dst
->desc
[num_dst_opts
].name
= NULL
;