2 * Commandline option parsing functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
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 buf. If the option name is longer than buf_size, it is
47 * truncated. buf is always zero terminated.
49 * The return value is the position of the delimiter/zero byte after the option
52 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
57 while (*p != '\0' && *p != delim) {
58 if (q && (q - buf) < buf_size - 1)
69 * Extracts the value of an option from the parameter string p (p points at the
70 * first byte of the option value)
72 * This function is comparable to get_opt_name with the difference that the
73 * delimiter is fixed to be comma which starts a new option. To specify an
74 * option value that contains commas, double each comma.
76 const char *get_opt_value(char *buf, int buf_size, const char *p)
87 if (q && (q - buf) < buf_size - 1)
97 static void parse_option_bool(const char *name, const char *value, bool *ret,
100 if (!strcmp(value, "on")) {
102 } else if (!strcmp(value, "off")) {
105 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
106 name, "'on' or 'off'");
110 static void parse_option_number(const char *name, const char *value,
111 uint64_t *ret, Error **errp)
116 err = qemu_strtou64(value, NULL, 0, &number);
117 if (err == -ERANGE) {
118 error_setg(errp, "Value '%s' is too large for parameter '%s'",
123 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
129 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
134 for (i = 0; desc[i].name != NULL; i++) {
135 if (strcmp(desc[i].name, name) == 0) {
143 void parse_option_size(const char *name, const char *value,
144 uint64_t *ret, Error **errp)
149 err = qemu_strtosz(value, NULL, &size);
150 if (err == -ERANGE) {
151 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
156 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
157 "a non-negative number below 2^64");
158 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
159 " kilo-, mega-, giga-, tera-, peta-\n"
160 "and exabytes, respectively.\n");
166 bool has_help_option(const char *param)
168 size_t buflen = strlen(param) + 1;
169 char *buf = g_malloc(buflen);
170 const char *p = param;
174 p = get_opt_value(buf, buflen, p);
179 if (is_help_option(buf)) {
190 bool is_valid_option_list(const char *param)
192 size_t buflen = strlen(param) + 1;
193 char *buf = g_malloc(buflen);
194 const char *p = param;
198 p = get_opt_value(buf, buflen, p);
204 if (!*buf || *buf == ',') {
215 void qemu_opts_print_help(QemuOptsList *list)
221 printf("Supported options:\n");
222 while (desc && desc->name) {
223 printf("%-16s %s\n", desc->name,
224 desc->help ? desc->help : "No description available");
228 /* ------------------------------------------------------------------ */
230 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
234 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
235 if (strcmp(opt->name, name) != 0)
242 static void qemu_opt_del(QemuOpt *opt)
244 QTAILQ_REMOVE(&opt->opts->head, opt, next);
250 /* qemu_opt_set allows many settings for the same option.
251 * This function deletes all settings for an option.
253 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
255 QemuOpt *opt, *next_opt;
257 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
258 if (!strcmp(opt->name, name)) {
264 const char *qemu_opt_get(QemuOpts *opts, const char *name)
272 opt = qemu_opt_find(opts, name);
274 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
275 if (desc && desc->def_value_str) {
276 return desc->def_value_str;
279 return opt ? opt->str : NULL;
282 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
285 iter->opt = QTAILQ_FIRST(&opts->head);
289 const char *qemu_opt_iter_next(QemuOptsIter *iter)
291 QemuOpt *ret = iter->opt;
293 while (ret && !g_str_equal(iter->name, ret->name)) {
294 ret = QTAILQ_NEXT(ret, next);
297 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
298 return ret ? ret->str : NULL;
301 /* Get a known option (or its default) and remove it from the list
302 * all in one action. Return a malloced string of the option value.
303 * Result must be freed by caller with g_free().
305 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
308 const QemuOptDesc *desc;
315 opt = qemu_opt_find(opts, name);
317 desc = find_desc_by_name(opts->list->desc, name);
318 if (desc && desc->def_value_str) {
319 str = g_strdup(desc->def_value_str);
325 qemu_opt_del_all(opts, name);
329 bool qemu_opt_has_help_opt(QemuOpts *opts)
333 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
334 if (is_help_option(opt->name)) {
341 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
342 bool defval, bool del)
351 opt = qemu_opt_find(opts, name);
353 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
354 if (desc && desc->def_value_str) {
355 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
359 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
360 ret = opt->value.boolean;
362 qemu_opt_del_all(opts, name);
367 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
369 return qemu_opt_get_bool_helper(opts, name, defval, false);
372 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
374 return qemu_opt_get_bool_helper(opts, name, defval, true);
377 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
378 uint64_t defval, bool del)
381 uint64_t ret = defval;
387 opt = qemu_opt_find(opts, name);
389 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
390 if (desc && desc->def_value_str) {
391 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
395 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
396 ret = opt->value.uint;
398 qemu_opt_del_all(opts, name);
403 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
405 return qemu_opt_get_number_helper(opts, name, defval, false);
408 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
411 return qemu_opt_get_number_helper(opts, name, defval, true);
414 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
415 uint64_t defval, bool del)
418 uint64_t ret = defval;
424 opt = qemu_opt_find(opts, name);
426 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
427 if (desc && desc->def_value_str) {
428 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
432 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
433 ret = opt->value.uint;
435 qemu_opt_del_all(opts, name);
440 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
442 return qemu_opt_get_size_helper(opts, name, defval, false);
445 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
448 return qemu_opt_get_size_helper(opts, name, defval, true);
451 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
453 if (opt->desc == NULL)
456 switch (opt->desc->type) {
457 case QEMU_OPT_STRING:
461 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
463 case QEMU_OPT_NUMBER:
464 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
467 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
474 static bool opts_accepts_any(const QemuOpts *opts)
476 return opts->list->desc[0].name == NULL;
479 int qemu_opt_unset(QemuOpts *opts, const char *name)
481 QemuOpt *opt = qemu_opt_find(opts, name);
483 assert(opts_accepts_any(opts));
493 static void opt_set(QemuOpts *opts, const char *name, const char *value,
494 bool prepend, Error **errp)
497 const QemuOptDesc *desc;
498 Error *local_err = NULL;
500 desc = find_desc_by_name(opts->list->desc, name);
501 if (!desc && !opts_accepts_any(opts)) {
502 error_setg(errp, QERR_INVALID_PARAMETER, name);
506 opt = g_malloc0(sizeof(*opt));
507 opt->name = g_strdup(name);
510 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
512 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
515 opt->str = g_strdup(value);
517 qemu_opt_parse(opt, &local_err);
519 error_propagate(errp, local_err);
524 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
527 opt_set(opts, name, value, false, errp);
530 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
534 const QemuOptDesc *desc = opts->list->desc;
536 opt = g_malloc0(sizeof(*opt));
537 opt->desc = find_desc_by_name(desc, name);
538 if (!opt->desc && !opts_accepts_any(opts)) {
539 error_setg(errp, QERR_INVALID_PARAMETER, name);
544 opt->name = g_strdup(name);
546 opt->value.boolean = !!val;
547 opt->str = g_strdup(val ? "on" : "off");
548 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
551 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
555 const QemuOptDesc *desc = opts->list->desc;
557 opt = g_malloc0(sizeof(*opt));
558 opt->desc = find_desc_by_name(desc, name);
559 if (!opt->desc && !opts_accepts_any(opts)) {
560 error_setg(errp, QERR_INVALID_PARAMETER, name);
565 opt->name = g_strdup(name);
567 opt->value.uint = val;
568 opt->str = g_strdup_printf("%" PRId64, val);
569 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
573 * For each member of @opts, call @func(@opaque, name, value, @errp).
574 * @func() may store an Error through @errp, but must return non-zero then.
575 * When @func() returns non-zero, break the loop and return that value.
576 * Return zero when the loop completes.
578 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
584 QTAILQ_FOREACH(opt, &opts->head, next) {
585 rc = func(opaque, opt->name, opt->str, errp);
589 assert(!errp || !*errp);
594 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
598 QTAILQ_FOREACH(opts, &list->head, next) {
599 if (!opts->id && !id) {
602 if (opts->id && id && !strcmp(opts->id, id)) {
609 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
610 int fail_if_exists, Error **errp)
612 QemuOpts *opts = NULL;
615 if (!id_wellformed(id)) {
616 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
618 error_append_hint(errp, "Identifiers consist of letters, digits, "
619 "'-', '.', '_', starting with a letter.\n");
622 opts = qemu_opts_find(list, id);
624 if (fail_if_exists && !list->merge_lists) {
625 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
631 } else if (list->merge_lists) {
632 opts = qemu_opts_find(list, NULL);
637 opts = g_malloc0(sizeof(*opts));
638 opts->id = g_strdup(id);
640 loc_save(&opts->loc);
641 QTAILQ_INIT(&opts->head);
642 QTAILQ_INSERT_TAIL(&list->head, opts, next);
646 void qemu_opts_reset(QemuOptsList *list)
648 QemuOpts *opts, *next_opts;
650 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
655 void qemu_opts_loc_restore(QemuOpts *opts)
657 loc_restore(&opts->loc);
660 void qemu_opts_set(QemuOptsList *list, const char *id,
661 const char *name, const char *value, Error **errp)
664 Error *local_err = NULL;
666 opts = qemu_opts_create(list, id, 1, &local_err);
668 error_propagate(errp, local_err);
671 qemu_opt_set(opts, name, value, errp);
674 const char *qemu_opts_id(QemuOpts *opts)
679 /* The id string will be g_free()d by qemu_opts_del */
680 void qemu_opts_set_id(QemuOpts *opts, char *id)
685 void qemu_opts_del(QemuOpts *opts)
694 opt = QTAILQ_FIRST(&opts->head);
699 QTAILQ_REMOVE(&opts->list->head, opts, next);
704 /* print value, escaping any commas in value */
705 static void escaped_print(const char *value)
709 for (ptr = value; *ptr; ++ptr) {
717 void qemu_opts_print(QemuOpts *opts, const char *separator)
720 QemuOptDesc *desc = opts->list->desc;
721 const char *sep = "";
724 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
728 if (desc[0].name == NULL) {
729 QTAILQ_FOREACH(opt, &opts->head, next) {
730 printf("%s%s=", sep, opt->name);
731 escaped_print(opt->str);
736 for (; desc && desc->name; desc++) {
738 opt = qemu_opt_find(opts, desc->name);
740 value = opt ? opt->str : desc->def_value_str;
744 if (desc->type == QEMU_OPT_STRING) {
745 printf("%s%s=", sep, desc->name);
746 escaped_print(value);
747 } else if ((desc->type == QEMU_OPT_SIZE ||
748 desc->type == QEMU_OPT_NUMBER) && opt) {
749 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
751 printf("%s%s=%s", sep, desc->name, value);
757 static void opts_do_parse(QemuOpts *opts, const char *params,
758 const char *firstname, bool prepend, Error **errp)
760 char option[128], value[1024];
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 pstrcpy(option, sizeof(option), firstname);
772 p = get_opt_value(value, sizeof(value), p);
774 /* option without value, probably a flag */
775 p = get_opt_name(option, sizeof(option), p, ',');
776 if (strncmp(option, "no", 2) == 0) {
777 memmove(option, option+2, strlen(option+2)+1);
778 pstrcpy(value, sizeof(value), "off");
780 pstrcpy(value, sizeof(value), "on");
784 /* found "foo=bar,more" */
785 p = get_opt_name(option, sizeof(option), p, '=');
790 p = get_opt_value(value, sizeof(value), p);
792 if (strcmp(option, "id") != 0) {
793 /* store and parse */
794 opt_set(opts, option, value, prepend, &local_err);
796 error_propagate(errp, local_err);
807 * Store options parsed from @params into @opts.
808 * If @firstname is non-null, the first key=value in @params may omit
809 * key=, and is treated as if key was @firstname.
810 * On error, store an error object through @errp if non-null.
812 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
813 const char *firstname, Error **errp)
815 opts_do_parse(opts, params, firstname, false, errp);
818 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
819 bool permit_abbrev, bool defaults, Error **errp)
821 const char *firstname;
822 char value[1024], *id = NULL;
825 Error *local_err = NULL;
827 assert(!permit_abbrev || list->implied_opt_name);
828 firstname = permit_abbrev ? list->implied_opt_name : NULL;
830 if (strncmp(params, "id=", 3) == 0) {
831 get_opt_value(value, sizeof(value), params+3);
833 } else if ((p = strstr(params, ",id=")) != NULL) {
834 get_opt_value(value, sizeof(value), p+4);
839 * This code doesn't work for defaults && !list->merge_lists: when
840 * params has no id=, and list has an element with !opts->id, it
841 * appends a new element instead of returning the existing opts.
842 * However, we got no use for this case. Guard against possible
843 * (if unlikely) future misuse:
845 assert(!defaults || list->merge_lists);
846 opts = qemu_opts_create(list, id, !defaults, &local_err);
848 error_propagate(errp, local_err);
852 opts_do_parse(opts, params, firstname, defaults, &local_err);
854 error_propagate(errp, local_err);
863 * Create a QemuOpts in @list and with options parsed from @params.
864 * If @permit_abbrev, the first key=value in @params may omit key=,
865 * and is treated as if key was @list->implied_opt_name.
866 * On error, store an error object through @errp if non-null.
867 * Return the new QemuOpts on success, null pointer on error.
869 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
870 bool permit_abbrev, Error **errp)
872 return opts_parse(list, params, permit_abbrev, false, errp);
876 * Create a QemuOpts in @list and with options parsed from @params.
877 * If @permit_abbrev, the first key=value in @params may omit key=,
878 * and is treated as if key was @list->implied_opt_name.
879 * Report errors with error_report_err(). This is inappropriate in
880 * QMP context. Do not use this function there!
881 * Return the new QemuOpts on success, null pointer on error.
883 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
889 opts = opts_parse(list, params, permit_abbrev, false, &err);
891 error_report_err(err);
896 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
901 opts = opts_parse(list, params, permit_abbrev, true, NULL);
905 typedef struct OptsFromQDictState {
908 } OptsFromQDictState;
910 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
912 OptsFromQDictState *state = opaque;
913 char buf[32], *tmp = NULL;
916 if (!strcmp(key, "id") || *state->errp) {
920 switch (qobject_type(obj)) {
922 value = qstring_get_str(qobject_to_qstring(obj));
925 tmp = qnum_to_string(qobject_to_qnum(obj));
929 pstrcpy(buf, sizeof(buf),
930 qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off");
937 qemu_opt_set(state->opts, key, value, state->errp);
942 * Create QemuOpts from a QDict.
943 * Use value of key "id" as ID if it exists and is a QString. Only
944 * QStrings, QNums and QBools are copied. Entries with other types
945 * are silently ignored.
947 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
950 OptsFromQDictState state;
951 Error *local_err = NULL;
954 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
957 error_propagate(errp, local_err);
961 assert(opts != NULL);
963 state.errp = &local_err;
965 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
967 error_propagate(errp, local_err);
976 * Adds all QDict entries to the QemuOpts that can be added and removes them
977 * from the QDict. When this function returns, the QDict contains only those
978 * entries that couldn't be added to the QemuOpts.
980 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
982 const QDictEntry *entry, *next;
984 entry = qdict_first(qdict);
986 while (entry != NULL) {
987 Error *local_err = NULL;
988 OptsFromQDictState state = {
993 next = qdict_next(qdict, entry);
995 if (find_desc_by_name(opts->list->desc, entry->key)) {
996 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
998 error_propagate(errp, local_err);
1001 qdict_del(qdict, entry->key);
1010 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1012 * If @list is given, only add those options to the QDict that are contained in
1013 * the list. If @del is true, any options added to the QDict are removed from
1014 * the QemuOpts, otherwise they remain there.
1016 * If two options in @opts have the same name, they are processed in order
1017 * so that the last one wins (consistent with the reverse iteration in
1018 * qemu_opt_find()), but all of them are deleted if @del is true.
1020 * TODO We'll want to use types appropriate for opt->desc->type, but
1021 * this is enough for now.
1023 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1024 QemuOptsList *list, bool del)
1026 QemuOpt *opt, *next;
1029 qdict = qdict_new();
1032 qdict_put_str(qdict, "id", opts->id);
1034 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1038 for (desc = list->desc; desc->name; desc++) {
1039 if (!strcmp(desc->name, opt->name)) {
1048 qdict_put_str(qdict, opt->name, opt->str);
1056 /* Copy all options in a QemuOpts to the given QDict. See
1057 * qemu_opts_to_qdict_filtered() for details. */
1058 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1060 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1063 /* Validate parsed opts against descriptions where no
1064 * descriptions were provided in the QemuOptsList.
1066 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1069 Error *local_err = NULL;
1071 assert(opts_accepts_any(opts));
1073 QTAILQ_FOREACH(opt, &opts->head, next) {
1074 opt->desc = find_desc_by_name(desc, opt->name);
1076 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1080 qemu_opt_parse(opt, &local_err);
1082 error_propagate(errp, local_err);
1089 * For each member of @list, call @func(@opaque, member, @errp).
1090 * Call it with the current location temporarily set to the member's.
1091 * @func() may store an Error through @errp, but must return non-zero then.
1092 * When @func() returns non-zero, break the loop and return that value.
1093 * Return zero when the loop completes.
1095 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1096 void *opaque, Error **errp)
1102 loc_push_none(&loc);
1103 QTAILQ_FOREACH(opts, &list->head, next) {
1104 loc_restore(&opts->loc);
1105 rc = func(opaque, opts, errp);
1109 assert(!errp || !*errp);
1115 static size_t count_opts_list(QemuOptsList *list)
1117 QemuOptDesc *desc = NULL;
1118 size_t num_opts = 0;
1125 while (desc && desc->name) {
1133 void qemu_opts_free(QemuOptsList *list)
1138 /* Realloc dst option list and append options from an option list (list)
1139 * to it. dst could be NULL or a malloced list.
1140 * The lifetime of dst must be shorter than the input list because the
1141 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1143 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1146 size_t num_opts, num_dst_opts;
1148 bool need_init = false;
1149 bool need_head_update;
1155 /* If dst is NULL, after realloc, some area of dst should be initialized
1156 * before adding options to it.
1160 need_head_update = true;
1162 /* Moreover, even if dst is not NULL, the realloc may move it to a
1163 * different address in which case we may get a stale tail pointer
1165 need_head_update = QTAILQ_EMPTY(&dst->head);
1168 num_opts = count_opts_list(dst);
1169 num_dst_opts = num_opts;
1170 num_opts += count_opts_list(list);
1171 dst = g_realloc(dst, sizeof(QemuOptsList) +
1172 (num_opts + 1) * sizeof(QemuOptDesc));
1175 dst->implied_opt_name = NULL;
1176 dst->merge_lists = false;
1178 if (need_head_update) {
1179 QTAILQ_INIT(&dst->head);
1181 dst->desc[num_dst_opts].name = NULL;
1183 /* append list->desc to dst->desc */
1186 while (desc && desc->name) {
1187 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1188 dst->desc[num_dst_opts++] = *desc;
1189 dst->desc[num_dst_opts].name = NULL;