scsi/scsi_bus: switch search direction in scsi_device_find
[qemu/ar7.git] / util / qemu-option.c
blobb9f93a7f8b8b17cf5c69da392b36d5a52d270e76
1 /*
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
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/qbool.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qapi/qmp/qerror.h"
35 #include "qemu/option_int.h"
36 #include "qemu/cutils.h"
37 #include "qemu/id.h"
38 #include "qemu/help_option.h"
41 * Extracts the name of an option from the parameter string (p points at the
42 * first byte of the option name)
44 * The option name is delimited by delim (usually , or =) or the string end
45 * and is copied into option. The caller is responsible for free'ing option
46 * when no longer required.
48 * The return value is the position of the delimiter/zero byte after the option
49 * name in p.
51 static const char *get_opt_name(const char *p, char **option, char delim)
53 char *offset = strchr(p, delim);
55 if (offset) {
56 *option = g_strndup(p, offset - p);
57 return offset;
58 } else {
59 *option = g_strdup(p);
60 return p + strlen(p);
65 * Extracts the value of an option from the parameter string p (p points at the
66 * first byte of the option value)
68 * This function is comparable to get_opt_name with the difference that the
69 * delimiter is fixed to be comma which starts a new option. To specify an
70 * option value that contains commas, double each comma.
72 const char *get_opt_value(const char *p, char **value)
74 size_t capacity = 0, length;
75 const char *offset;
77 *value = NULL;
78 while (1) {
79 offset = qemu_strchrnul(p, ',');
80 length = offset - p;
81 if (*offset != '\0' && *(offset + 1) == ',') {
82 length++;
84 *value = g_renew(char, *value, capacity + length + 1);
85 strncpy(*value + capacity, p, length);
86 (*value)[capacity + length] = '\0';
87 capacity += length;
88 if (*offset == '\0' ||
89 *(offset + 1) != ',') {
90 break;
93 p += (offset - p) + 2;
96 return offset;
99 static bool parse_option_bool(const char *name, const char *value, bool *ret,
100 Error **errp)
102 if (!strcmp(value, "on")) {
103 *ret = 1;
104 } else if (!strcmp(value, "off")) {
105 *ret = 0;
106 } else {
107 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
108 name, "'on' or 'off'");
109 return false;
111 return true;
114 static bool parse_option_number(const char *name, const char *value,
115 uint64_t *ret, Error **errp)
117 uint64_t number;
118 int err;
120 err = qemu_strtou64(value, NULL, 0, &number);
121 if (err == -ERANGE) {
122 error_setg(errp, "Value '%s' is too large for parameter '%s'",
123 value, name);
124 return false;
126 if (err) {
127 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
128 return false;
130 *ret = number;
131 return true;
134 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
135 const char *name)
137 int i;
139 for (i = 0; desc[i].name != NULL; i++) {
140 if (strcmp(desc[i].name, name) == 0) {
141 return &desc[i];
145 return NULL;
148 static const char *find_default_by_name(QemuOpts *opts, const char *name)
150 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
152 return desc ? desc->def_value_str : NULL;
155 bool parse_option_size(const char *name, const char *value,
156 uint64_t *ret, Error **errp)
158 uint64_t size;
159 int err;
161 err = qemu_strtosz(value, NULL, &size);
162 if (err == -ERANGE) {
163 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
164 value, name);
165 return false;
167 if (err) {
168 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
169 "a non-negative number below 2^64");
170 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
171 " kilo-, mega-, giga-, tera-, peta-\n"
172 "and exabytes, respectively.\n");
173 return false;
175 *ret = size;
176 return true;
179 static const char *opt_type_to_string(enum QemuOptType type)
181 switch (type) {
182 case QEMU_OPT_STRING:
183 return "str";
184 case QEMU_OPT_BOOL:
185 return "bool (on/off)";
186 case QEMU_OPT_NUMBER:
187 return "num";
188 case QEMU_OPT_SIZE:
189 return "size";
192 g_assert_not_reached();
196 * Print the list of options available in the given list. If
197 * @print_caption is true, a caption (including the list name, if it
198 * exists) is printed. The options itself will be indented, so
199 * @print_caption should only be set to false if the caller prints its
200 * own custom caption (so that the indentation makes sense).
202 void qemu_opts_print_help(QemuOptsList *list, bool print_caption)
204 QemuOptDesc *desc;
205 int i;
206 GPtrArray *array = g_ptr_array_new();
208 assert(list);
209 desc = list->desc;
210 while (desc && desc->name) {
211 GString *str = g_string_new(NULL);
212 g_string_append_printf(str, " %s=<%s>", desc->name,
213 opt_type_to_string(desc->type));
214 if (desc->help) {
215 if (str->len < 24) {
216 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
218 g_string_append_printf(str, " - %s", desc->help);
220 g_ptr_array_add(array, g_string_free(str, false));
221 desc++;
224 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
225 if (print_caption && array->len > 0) {
226 if (list->name) {
227 printf("%s options:\n", list->name);
228 } else {
229 printf("Options:\n");
231 } else if (array->len == 0) {
232 if (list->name) {
233 printf("There are no options for %s.\n", list->name);
234 } else {
235 printf("No options available.\n");
238 for (i = 0; i < array->len; i++) {
239 printf("%s\n", (char *)array->pdata[i]);
241 g_ptr_array_set_free_func(array, g_free);
242 g_ptr_array_free(array, true);
245 /* ------------------------------------------------------------------ */
247 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
249 QemuOpt *opt;
251 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
252 if (strcmp(opt->name, name) != 0)
253 continue;
254 return opt;
256 return NULL;
259 static void qemu_opt_del(QemuOpt *opt)
261 QTAILQ_REMOVE(&opt->opts->head, opt, next);
262 g_free(opt->name);
263 g_free(opt->str);
264 g_free(opt);
267 /* qemu_opt_set allows many settings for the same option.
268 * This function deletes all settings for an option.
270 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
272 QemuOpt *opt, *next_opt;
274 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
275 if (!strcmp(opt->name, name)) {
276 qemu_opt_del(opt);
281 const char *qemu_opt_get(QemuOpts *opts, const char *name)
283 QemuOpt *opt;
285 if (opts == NULL) {
286 return NULL;
289 opt = qemu_opt_find(opts, name);
290 if (!opt) {
291 return find_default_by_name(opts, name);
294 return opt->str;
297 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
299 iter->opts = opts;
300 iter->opt = QTAILQ_FIRST(&opts->head);
301 iter->name = name;
304 const char *qemu_opt_iter_next(QemuOptsIter *iter)
306 QemuOpt *ret = iter->opt;
307 if (iter->name) {
308 while (ret && !g_str_equal(iter->name, ret->name)) {
309 ret = QTAILQ_NEXT(ret, next);
312 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
313 return ret ? ret->str : NULL;
316 /* Get a known option (or its default) and remove it from the list
317 * all in one action. Return a malloced string of the option value.
318 * Result must be freed by caller with g_free().
320 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
322 QemuOpt *opt;
323 char *str;
325 if (opts == NULL) {
326 return NULL;
329 opt = qemu_opt_find(opts, name);
330 if (!opt) {
331 return g_strdup(find_default_by_name(opts, name));
333 str = opt->str;
334 opt->str = NULL;
335 qemu_opt_del_all(opts, name);
336 return str;
339 bool qemu_opt_has_help_opt(QemuOpts *opts)
341 QemuOpt *opt;
343 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
344 if (is_help_option(opt->name)) {
345 return true;
348 return false;
351 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
352 bool defval, bool del)
354 QemuOpt *opt;
355 const char *def_val;
356 bool ret = defval;
358 if (opts == NULL) {
359 return ret;
362 opt = qemu_opt_find(opts, name);
363 if (opt == NULL) {
364 def_val = find_default_by_name(opts, name);
365 if (def_val) {
366 parse_option_bool(name, def_val, &ret, &error_abort);
368 return ret;
370 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
371 ret = opt->value.boolean;
372 if (del) {
373 qemu_opt_del_all(opts, name);
375 return ret;
378 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
380 return qemu_opt_get_bool_helper(opts, name, defval, false);
383 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
385 return qemu_opt_get_bool_helper(opts, name, defval, true);
388 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
389 uint64_t defval, bool del)
391 QemuOpt *opt;
392 const char *def_val;
393 uint64_t ret = defval;
395 if (opts == NULL) {
396 return ret;
399 opt = qemu_opt_find(opts, name);
400 if (opt == NULL) {
401 def_val = find_default_by_name(opts, name);
402 if (def_val) {
403 parse_option_number(name, def_val, &ret, &error_abort);
405 return ret;
407 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
408 ret = opt->value.uint;
409 if (del) {
410 qemu_opt_del_all(opts, name);
412 return ret;
415 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
417 return qemu_opt_get_number_helper(opts, name, defval, false);
420 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
421 uint64_t defval)
423 return qemu_opt_get_number_helper(opts, name, defval, true);
426 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
427 uint64_t defval, bool del)
429 QemuOpt *opt;
430 const char *def_val;
431 uint64_t ret = defval;
433 if (opts == NULL) {
434 return ret;
437 opt = qemu_opt_find(opts, name);
438 if (opt == NULL) {
439 def_val = find_default_by_name(opts, name);
440 if (def_val) {
441 parse_option_size(name, def_val, &ret, &error_abort);
443 return ret;
445 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
446 ret = opt->value.uint;
447 if (del) {
448 qemu_opt_del_all(opts, name);
450 return ret;
453 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
455 return qemu_opt_get_size_helper(opts, name, defval, false);
458 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
459 uint64_t defval)
461 return qemu_opt_get_size_helper(opts, name, defval, true);
464 static bool qemu_opt_parse(QemuOpt *opt, Error **errp)
466 if (opt->desc == NULL)
467 return true;
469 switch (opt->desc->type) {
470 case QEMU_OPT_STRING:
471 /* nothing */
472 return true;
473 case QEMU_OPT_BOOL:
474 return parse_option_bool(opt->name, opt->str, &opt->value.boolean,
475 errp);
476 case QEMU_OPT_NUMBER:
477 return parse_option_number(opt->name, opt->str, &opt->value.uint,
478 errp);
479 case QEMU_OPT_SIZE:
480 return parse_option_size(opt->name, opt->str, &opt->value.uint,
481 errp);
482 default:
483 abort();
487 static bool opts_accepts_any(const QemuOpts *opts)
489 return opts->list->desc[0].name == NULL;
492 int qemu_opt_unset(QemuOpts *opts, const char *name)
494 QemuOpt *opt = qemu_opt_find(opts, name);
496 assert(opts_accepts_any(opts));
498 if (opt == NULL) {
499 return -1;
500 } else {
501 qemu_opt_del(opt);
502 return 0;
506 static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
507 bool prepend)
509 QemuOpt *opt = g_malloc0(sizeof(*opt));
511 opt->name = g_strdup(name);
512 opt->str = value;
513 opt->opts = opts;
514 if (prepend) {
515 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
516 } else {
517 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
520 return opt;
523 static bool opt_validate(QemuOpt *opt, bool *help_wanted,
524 Error **errp)
526 const QemuOptDesc *desc;
528 desc = find_desc_by_name(opt->opts->list->desc, opt->name);
529 if (!desc && !opts_accepts_any(opt->opts)) {
530 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
531 if (help_wanted && is_help_option(opt->name)) {
532 *help_wanted = true;
534 return false;
537 opt->desc = desc;
538 if (!qemu_opt_parse(opt, errp)) {
539 return false;
542 return true;
545 bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
546 Error **errp)
548 QemuOpt *opt = opt_create(opts, name, g_strdup(value), false);
550 if (!opt_validate(opt, NULL, errp)) {
551 qemu_opt_del(opt);
552 return false;
554 return true;
557 bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
558 Error **errp)
560 QemuOpt *opt;
561 const QemuOptDesc *desc;
563 desc = find_desc_by_name(opts->list->desc, name);
564 if (!desc && !opts_accepts_any(opts)) {
565 error_setg(errp, QERR_INVALID_PARAMETER, name);
566 return false;
569 opt = g_malloc0(sizeof(*opt));
570 opt->name = g_strdup(name);
571 opt->opts = opts;
572 opt->desc = desc;
573 opt->value.boolean = !!val;
574 opt->str = g_strdup(val ? "on" : "off");
575 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
576 return true;
579 bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
580 Error **errp)
582 QemuOpt *opt;
583 const QemuOptDesc *desc;
585 desc = find_desc_by_name(opts->list->desc, name);
586 if (!desc && !opts_accepts_any(opts)) {
587 error_setg(errp, QERR_INVALID_PARAMETER, name);
588 return false;
591 opt = g_malloc0(sizeof(*opt));
592 opt->name = g_strdup(name);
593 opt->opts = opts;
594 opt->desc = desc;
595 opt->value.uint = val;
596 opt->str = g_strdup_printf("%" PRId64, val);
597 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
598 return true;
602 * For each member of @opts, call @func(@opaque, name, value, @errp).
603 * @func() may store an Error through @errp, but must return non-zero then.
604 * When @func() returns non-zero, break the loop and return that value.
605 * Return zero when the loop completes.
607 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
608 Error **errp)
610 QemuOpt *opt;
611 int rc;
613 QTAILQ_FOREACH(opt, &opts->head, next) {
614 rc = func(opaque, opt->name, opt->str, errp);
615 if (rc) {
616 return rc;
618 assert(!errp || !*errp);
620 return 0;
623 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
625 QemuOpts *opts;
627 QTAILQ_FOREACH(opts, &list->head, next) {
628 if (!opts->id && !id) {
629 return opts;
631 if (opts->id && id && !strcmp(opts->id, id)) {
632 return opts;
635 return NULL;
638 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
639 int fail_if_exists, Error **errp)
641 QemuOpts *opts = NULL;
643 if (id) {
644 if (!id_wellformed(id)) {
645 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
646 "an identifier");
647 error_append_hint(errp, "Identifiers consist of letters, digits, "
648 "'-', '.', '_', starting with a letter.\n");
649 return NULL;
651 opts = qemu_opts_find(list, id);
652 if (opts != NULL) {
653 if (fail_if_exists && !list->merge_lists) {
654 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
655 return NULL;
656 } else {
657 return opts;
660 } else if (list->merge_lists) {
661 opts = qemu_opts_find(list, NULL);
662 if (opts) {
663 return opts;
666 opts = g_malloc0(sizeof(*opts));
667 opts->id = g_strdup(id);
668 opts->list = list;
669 loc_save(&opts->loc);
670 QTAILQ_INIT(&opts->head);
671 QTAILQ_INSERT_TAIL(&list->head, opts, next);
672 return opts;
675 void qemu_opts_reset(QemuOptsList *list)
677 QemuOpts *opts, *next_opts;
679 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
680 qemu_opts_del(opts);
684 void qemu_opts_loc_restore(QemuOpts *opts)
686 loc_restore(&opts->loc);
689 bool qemu_opts_set(QemuOptsList *list, const char *id,
690 const char *name, const char *value, Error **errp)
692 QemuOpts *opts;
694 opts = qemu_opts_create(list, id, 1, errp);
695 if (!opts) {
696 return false;
698 return qemu_opt_set(opts, name, value, errp);
701 const char *qemu_opts_id(QemuOpts *opts)
703 return opts->id;
706 /* The id string will be g_free()d by qemu_opts_del */
707 void qemu_opts_set_id(QemuOpts *opts, char *id)
709 opts->id = id;
712 void qemu_opts_del(QemuOpts *opts)
714 QemuOpt *opt;
716 if (opts == NULL) {
717 return;
720 for (;;) {
721 opt = QTAILQ_FIRST(&opts->head);
722 if (opt == NULL)
723 break;
724 qemu_opt_del(opt);
726 QTAILQ_REMOVE(&opts->list->head, opts, next);
727 g_free(opts->id);
728 g_free(opts);
731 /* print value, escaping any commas in value */
732 static void escaped_print(const char *value)
734 const char *ptr;
736 for (ptr = value; *ptr; ++ptr) {
737 if (*ptr == ',') {
738 putchar(',');
740 putchar(*ptr);
744 void qemu_opts_print(QemuOpts *opts, const char *separator)
746 QemuOpt *opt;
747 QemuOptDesc *desc = opts->list->desc;
748 const char *sep = "";
750 if (opts->id) {
751 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
752 sep = separator;
755 if (desc[0].name == NULL) {
756 QTAILQ_FOREACH(opt, &opts->head, next) {
757 printf("%s%s=", sep, opt->name);
758 escaped_print(opt->str);
759 sep = separator;
761 return;
763 for (; desc && desc->name; desc++) {
764 const char *value;
765 opt = qemu_opt_find(opts, desc->name);
767 value = opt ? opt->str : desc->def_value_str;
768 if (!value) {
769 continue;
771 if (desc->type == QEMU_OPT_STRING) {
772 printf("%s%s=", sep, desc->name);
773 escaped_print(value);
774 } else if ((desc->type == QEMU_OPT_SIZE ||
775 desc->type == QEMU_OPT_NUMBER) && opt) {
776 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
777 } else {
778 printf("%s%s=%s", sep, desc->name, value);
780 sep = separator;
784 static const char *get_opt_name_value(const char *params,
785 const char *firstname,
786 char **name, char **value)
788 const char *p, *pe, *pc;
790 pe = strchr(params, '=');
791 pc = strchr(params, ',');
793 if (!pe || (pc && pc < pe)) {
794 /* found "foo,more" */
795 if (firstname) {
796 /* implicitly named first option */
797 *name = g_strdup(firstname);
798 p = get_opt_value(params, value);
799 } else {
800 /* option without value, must be a flag */
801 p = get_opt_name(params, name, ',');
802 if (strncmp(*name, "no", 2) == 0) {
803 memmove(*name, *name + 2, strlen(*name + 2) + 1);
804 *value = g_strdup("off");
805 } else {
806 *value = g_strdup("on");
809 } else {
810 /* found "foo=bar,more" */
811 p = get_opt_name(params, name, '=');
812 assert(*p == '=');
813 p++;
814 p = get_opt_value(p, value);
817 assert(!*p || *p == ',');
818 if (*p == ',') {
819 p++;
821 return p;
824 static bool opts_do_parse(QemuOpts *opts, const char *params,
825 const char *firstname, bool prepend,
826 bool *help_wanted, Error **errp)
828 char *option, *value;
829 const char *p;
830 QemuOpt *opt;
832 for (p = params; *p;) {
833 p = get_opt_name_value(p, firstname, &option, &value);
834 firstname = NULL;
836 if (!strcmp(option, "id")) {
837 g_free(option);
838 g_free(value);
839 continue;
842 opt = opt_create(opts, option, value, prepend);
843 g_free(option);
844 if (!opt_validate(opt, help_wanted, errp)) {
845 qemu_opt_del(opt);
846 return false;
850 return true;
853 static char *opts_parse_id(const char *params)
855 const char *p;
856 char *name, *value;
858 for (p = params; *p;) {
859 p = get_opt_name_value(p, NULL, &name, &value);
860 if (!strcmp(name, "id")) {
861 g_free(name);
862 return value;
864 g_free(name);
865 g_free(value);
868 return NULL;
871 bool has_help_option(const char *params)
873 const char *p;
874 char *name, *value;
875 bool ret;
877 for (p = params; *p;) {
878 p = get_opt_name_value(p, NULL, &name, &value);
879 ret = is_help_option(name);
880 g_free(name);
881 g_free(value);
882 if (ret) {
883 return true;
887 return false;
891 * Store options parsed from @params into @opts.
892 * If @firstname is non-null, the first key=value in @params may omit
893 * key=, and is treated as if key was @firstname.
894 * On error, store an error object through @errp if non-null.
896 bool qemu_opts_do_parse(QemuOpts *opts, const char *params,
897 const char *firstname, Error **errp)
899 return opts_do_parse(opts, params, firstname, false, NULL, errp);
902 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
903 bool permit_abbrev, bool defaults,
904 bool *help_wanted, Error **errp)
906 const char *firstname;
907 char *id = opts_parse_id(params);
908 QemuOpts *opts;
910 assert(!permit_abbrev || list->implied_opt_name);
911 firstname = permit_abbrev ? list->implied_opt_name : NULL;
914 * This code doesn't work for defaults && !list->merge_lists: when
915 * params has no id=, and list has an element with !opts->id, it
916 * appends a new element instead of returning the existing opts.
917 * However, we got no use for this case. Guard against possible
918 * (if unlikely) future misuse:
920 assert(!defaults || list->merge_lists);
921 opts = qemu_opts_create(list, id, !defaults, errp);
922 g_free(id);
923 if (opts == NULL) {
924 return NULL;
927 if (!opts_do_parse(opts, params, firstname, defaults, help_wanted,
928 errp)) {
929 qemu_opts_del(opts);
930 return NULL;
933 return opts;
937 * Create a QemuOpts in @list and with options parsed from @params.
938 * If @permit_abbrev, the first key=value in @params may omit key=,
939 * and is treated as if key was @list->implied_opt_name.
940 * On error, store an error object through @errp if non-null.
941 * Return the new QemuOpts on success, null pointer on error.
943 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
944 bool permit_abbrev, Error **errp)
946 return opts_parse(list, params, permit_abbrev, false, NULL, errp);
950 * Create a QemuOpts in @list and with options parsed from @params.
951 * If @permit_abbrev, the first key=value in @params may omit key=,
952 * and is treated as if key was @list->implied_opt_name.
953 * Report errors with error_report_err(). This is inappropriate in
954 * QMP context. Do not use this function there!
955 * Return the new QemuOpts on success, null pointer on error.
957 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
958 bool permit_abbrev)
960 Error *err = NULL;
961 QemuOpts *opts;
962 bool help_wanted = false;
964 opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err);
965 if (err) {
966 if (help_wanted) {
967 qemu_opts_print_help(list, true);
968 error_free(err);
969 } else {
970 error_report_err(err);
973 return opts;
976 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
977 int permit_abbrev)
979 QemuOpts *opts;
981 opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL);
982 assert(opts);
985 static bool qemu_opts_from_qdict_entry(QemuOpts *opts,
986 const QDictEntry *entry,
987 Error **errp)
989 const char *key = qdict_entry_key(entry);
990 QObject *obj = qdict_entry_value(entry);
991 char buf[32];
992 g_autofree char *tmp = NULL;
993 const char *value;
995 if (!strcmp(key, "id")) {
996 return true;
999 switch (qobject_type(obj)) {
1000 case QTYPE_QSTRING:
1001 value = qstring_get_str(qobject_to(QString, obj));
1002 break;
1003 case QTYPE_QNUM:
1004 tmp = qnum_to_string(qobject_to(QNum, obj));
1005 value = tmp;
1006 break;
1007 case QTYPE_QBOOL:
1008 pstrcpy(buf, sizeof(buf),
1009 qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
1010 value = buf;
1011 break;
1012 default:
1013 return true;
1016 return qemu_opt_set(opts, key, value, errp);
1020 * Create QemuOpts from a QDict.
1021 * Use value of key "id" as ID if it exists and is a QString. Only
1022 * QStrings, QNums and QBools are copied. Entries with other types
1023 * are silently ignored.
1025 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1026 Error **errp)
1028 QemuOpts *opts;
1029 const QDictEntry *entry;
1031 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
1032 if (!opts) {
1033 return NULL;
1036 assert(opts != NULL);
1038 for (entry = qdict_first(qdict);
1039 entry;
1040 entry = qdict_next(qdict, entry)) {
1041 if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
1042 qemu_opts_del(opts);
1043 return NULL;
1047 return opts;
1051 * Adds all QDict entries to the QemuOpts that can be added and removes them
1052 * from the QDict. When this function returns, the QDict contains only those
1053 * entries that couldn't be added to the QemuOpts.
1055 bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1057 const QDictEntry *entry, *next;
1059 entry = qdict_first(qdict);
1061 while (entry != NULL) {
1062 next = qdict_next(qdict, entry);
1064 if (find_desc_by_name(opts->list->desc, entry->key)) {
1065 if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
1066 return false;
1068 qdict_del(qdict, entry->key);
1071 entry = next;
1074 return true;
1078 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1080 * If @list is given, only add those options to the QDict that are contained in
1081 * the list. If @del is true, any options added to the QDict are removed from
1082 * the QemuOpts, otherwise they remain there.
1084 * If two options in @opts have the same name, they are processed in order
1085 * so that the last one wins (consistent with the reverse iteration in
1086 * qemu_opt_find()), but all of them are deleted if @del is true.
1088 * TODO We'll want to use types appropriate for opt->desc->type, but
1089 * this is enough for now.
1091 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1092 QemuOptsList *list, bool del)
1094 QemuOpt *opt, *next;
1096 if (!qdict) {
1097 qdict = qdict_new();
1099 if (opts->id) {
1100 qdict_put_str(qdict, "id", opts->id);
1102 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1103 if (list) {
1104 QemuOptDesc *desc;
1105 bool found = false;
1106 for (desc = list->desc; desc->name; desc++) {
1107 if (!strcmp(desc->name, opt->name)) {
1108 found = true;
1109 break;
1112 if (!found) {
1113 continue;
1116 qdict_put_str(qdict, opt->name, opt->str);
1117 if (del) {
1118 qemu_opt_del(opt);
1121 return qdict;
1124 /* Copy all options in a QemuOpts to the given QDict. See
1125 * qemu_opts_to_qdict_filtered() for details. */
1126 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1128 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1131 /* Validate parsed opts against descriptions where no
1132 * descriptions were provided in the QemuOptsList.
1134 bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1136 QemuOpt *opt;
1138 assert(opts_accepts_any(opts));
1140 QTAILQ_FOREACH(opt, &opts->head, next) {
1141 opt->desc = find_desc_by_name(desc, opt->name);
1142 if (!opt->desc) {
1143 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1144 return false;
1147 if (!qemu_opt_parse(opt, errp)) {
1148 return false;
1152 return true;
1156 * For each member of @list, call @func(@opaque, member, @errp).
1157 * Call it with the current location temporarily set to the member's.
1158 * @func() may store an Error through @errp, but must return non-zero then.
1159 * When @func() returns non-zero, break the loop and return that value.
1160 * Return zero when the loop completes.
1162 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1163 void *opaque, Error **errp)
1165 Location loc;
1166 QemuOpts *opts;
1167 int rc = 0;
1169 loc_push_none(&loc);
1170 QTAILQ_FOREACH(opts, &list->head, next) {
1171 loc_restore(&opts->loc);
1172 rc = func(opaque, opts, errp);
1173 if (rc) {
1174 break;
1176 assert(!errp || !*errp);
1178 loc_pop(&loc);
1179 return rc;
1182 static size_t count_opts_list(QemuOptsList *list)
1184 QemuOptDesc *desc = NULL;
1185 size_t num_opts = 0;
1187 if (!list) {
1188 return 0;
1191 desc = list->desc;
1192 while (desc && desc->name) {
1193 num_opts++;
1194 desc++;
1197 return num_opts;
1200 void qemu_opts_free(QemuOptsList *list)
1202 g_free(list);
1205 /* Realloc dst option list and append options from an option list (list)
1206 * to it. dst could be NULL or a malloced list.
1207 * The lifetime of dst must be shorter than the input list because the
1208 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1210 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1211 QemuOptsList *list)
1213 size_t num_opts, num_dst_opts;
1214 QemuOptDesc *desc;
1215 bool need_init = false;
1216 bool need_head_update;
1218 if (!list) {
1219 return dst;
1222 /* If dst is NULL, after realloc, some area of dst should be initialized
1223 * before adding options to it.
1225 if (!dst) {
1226 need_init = true;
1227 need_head_update = true;
1228 } else {
1229 /* Moreover, even if dst is not NULL, the realloc may move it to a
1230 * different address in which case we may get a stale tail pointer
1231 * in dst->head. */
1232 need_head_update = QTAILQ_EMPTY(&dst->head);
1235 num_opts = count_opts_list(dst);
1236 num_dst_opts = num_opts;
1237 num_opts += count_opts_list(list);
1238 dst = g_realloc(dst, sizeof(QemuOptsList) +
1239 (num_opts + 1) * sizeof(QemuOptDesc));
1240 if (need_init) {
1241 dst->name = NULL;
1242 dst->implied_opt_name = NULL;
1243 dst->merge_lists = false;
1245 if (need_head_update) {
1246 QTAILQ_INIT(&dst->head);
1248 dst->desc[num_dst_opts].name = NULL;
1250 /* append list->desc to dst->desc */
1251 if (list) {
1252 desc = list->desc;
1253 while (desc && desc->name) {
1254 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1255 dst->desc[num_dst_opts++] = *desc;
1256 dst->desc[num_dst_opts].name = NULL;
1258 desc++;
1262 return dst;