trace: document how to specify multiple --trace patterns
[qemu/kevin.git] / util / qemu-option.c
blob40564a12eb0263d8af5f9294bf545e9016e3558e
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 @len characters long and is copied into @option. The
45 * caller is responsible for free'ing @option when no longer required.
47 * The return value is the position of the delimiter/zero byte after the option
48 * name in @p.
50 static const char *get_opt_name(const char *p, char **option, size_t len)
52 *option = g_strndup(p, len);
53 return p + len;
57 * Extracts the value of an option from the parameter string p (p points at the
58 * first byte of the option value)
60 * This function is comparable to get_opt_name with the difference that the
61 * delimiter is fixed to be comma which starts a new option. To specify an
62 * option value that contains commas, double each comma.
64 const char *get_opt_value(const char *p, char **value)
66 size_t capacity = 0, length;
67 const char *offset;
69 *value = NULL;
70 while (1) {
71 offset = qemu_strchrnul(p, ',');
72 length = offset - p;
73 if (*offset != '\0' && *(offset + 1) == ',') {
74 length++;
76 *value = g_renew(char, *value, capacity + length + 1);
77 strncpy(*value + capacity, p, length);
78 (*value)[capacity + length] = '\0';
79 capacity += length;
80 if (*offset == '\0' ||
81 *(offset + 1) != ',') {
82 break;
85 p += (offset - p) + 2;
88 return offset;
91 static bool parse_option_number(const char *name, const char *value,
92 uint64_t *ret, Error **errp)
94 uint64_t number;
95 int err;
97 err = qemu_strtou64(value, NULL, 0, &number);
98 if (err == -ERANGE) {
99 error_setg(errp, "Value '%s' is too large for parameter '%s'",
100 value, name);
101 return false;
103 if (err) {
104 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
105 return false;
107 *ret = number;
108 return true;
111 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
112 const char *name)
114 int i;
116 for (i = 0; desc[i].name != NULL; i++) {
117 if (strcmp(desc[i].name, name) == 0) {
118 return &desc[i];
122 return NULL;
125 static const char *find_default_by_name(QemuOpts *opts, const char *name)
127 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
129 return desc ? desc->def_value_str : NULL;
132 bool parse_option_size(const char *name, const char *value,
133 uint64_t *ret, Error **errp)
135 uint64_t size;
136 int err;
138 err = qemu_strtosz(value, NULL, &size);
139 if (err == -ERANGE) {
140 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
141 value, name);
142 return false;
144 if (err) {
145 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
146 "a non-negative number below 2^64");
147 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
148 " kilo-, mega-, giga-, tera-, peta-\n"
149 "and exabytes, respectively.\n");
150 return false;
152 *ret = size;
153 return true;
156 static const char *opt_type_to_string(enum QemuOptType type)
158 switch (type) {
159 case QEMU_OPT_STRING:
160 return "str";
161 case QEMU_OPT_BOOL:
162 return "bool (on/off)";
163 case QEMU_OPT_NUMBER:
164 return "num";
165 case QEMU_OPT_SIZE:
166 return "size";
169 g_assert_not_reached();
173 * Print the list of options available in the given list. If
174 * @print_caption is true, a caption (including the list name, if it
175 * exists) is printed. The options itself will be indented, so
176 * @print_caption should only be set to false if the caller prints its
177 * own custom caption (so that the indentation makes sense).
179 void qemu_opts_print_help(QemuOptsList *list, bool print_caption)
181 QemuOptDesc *desc;
182 int i;
183 GPtrArray *array = g_ptr_array_new();
185 assert(list);
186 desc = list->desc;
187 while (desc && desc->name) {
188 GString *str = g_string_new(NULL);
189 g_string_append_printf(str, " %s=<%s>", desc->name,
190 opt_type_to_string(desc->type));
191 if (desc->help) {
192 if (str->len < 24) {
193 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
195 g_string_append_printf(str, " - %s", desc->help);
197 g_ptr_array_add(array, g_string_free(str, false));
198 desc++;
201 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
202 if (print_caption && array->len > 0) {
203 if (list->name) {
204 printf("%s options:\n", list->name);
205 } else {
206 printf("Options:\n");
208 } else if (array->len == 0) {
209 if (list->name) {
210 printf("There are no options for %s.\n", list->name);
211 } else {
212 printf("No options available.\n");
215 for (i = 0; i < array->len; i++) {
216 printf("%s\n", (char *)array->pdata[i]);
218 g_ptr_array_set_free_func(array, g_free);
219 g_ptr_array_free(array, true);
222 /* ------------------------------------------------------------------ */
224 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
226 QemuOpt *opt;
228 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
229 if (strcmp(opt->name, name) != 0)
230 continue;
231 return opt;
233 return NULL;
236 static void qemu_opt_del(QemuOpt *opt)
238 QTAILQ_REMOVE(&opt->opts->head, opt, next);
239 g_free(opt->name);
240 g_free(opt->str);
241 g_free(opt);
244 /* qemu_opt_set allows many settings for the same option.
245 * This function deletes all settings for an option.
247 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
249 QemuOpt *opt, *next_opt;
251 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
252 if (!strcmp(opt->name, name)) {
253 qemu_opt_del(opt);
258 const char *qemu_opt_get(QemuOpts *opts, const char *name)
260 QemuOpt *opt;
262 if (opts == NULL) {
263 return NULL;
266 opt = qemu_opt_find(opts, name);
267 if (!opt) {
268 return find_default_by_name(opts, name);
271 return opt->str;
274 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
276 iter->opts = opts;
277 iter->opt = QTAILQ_FIRST(&opts->head);
278 iter->name = name;
281 const char *qemu_opt_iter_next(QemuOptsIter *iter)
283 QemuOpt *ret = iter->opt;
284 if (iter->name) {
285 while (ret && !g_str_equal(iter->name, ret->name)) {
286 ret = QTAILQ_NEXT(ret, next);
289 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
290 return ret ? ret->str : NULL;
293 /* Get a known option (or its default) and remove it from the list
294 * all in one action. Return a malloced string of the option value.
295 * Result must be freed by caller with g_free().
297 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
299 QemuOpt *opt;
300 char *str;
302 if (opts == NULL) {
303 return NULL;
306 opt = qemu_opt_find(opts, name);
307 if (!opt) {
308 return g_strdup(find_default_by_name(opts, name));
310 str = opt->str;
311 opt->str = NULL;
312 qemu_opt_del_all(opts, name);
313 return str;
316 bool qemu_opt_has_help_opt(QemuOpts *opts)
318 QemuOpt *opt;
320 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
321 if (is_help_option(opt->name)) {
322 return true;
325 return false;
328 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
329 bool defval, bool del)
331 QemuOpt *opt;
332 const char *def_val;
333 bool ret = defval;
335 if (opts == NULL) {
336 return ret;
339 opt = qemu_opt_find(opts, name);
340 if (opt == NULL) {
341 def_val = find_default_by_name(opts, name);
342 if (def_val) {
343 qapi_bool_parse(name, def_val, &ret, &error_abort);
345 return ret;
347 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
348 ret = opt->value.boolean;
349 if (del) {
350 qemu_opt_del_all(opts, name);
352 return ret;
355 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
357 return qemu_opt_get_bool_helper(opts, name, defval, false);
360 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
362 return qemu_opt_get_bool_helper(opts, name, defval, true);
365 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
366 uint64_t defval, bool del)
368 QemuOpt *opt;
369 const char *def_val;
370 uint64_t ret = defval;
372 if (opts == NULL) {
373 return ret;
376 opt = qemu_opt_find(opts, name);
377 if (opt == NULL) {
378 def_val = find_default_by_name(opts, name);
379 if (def_val) {
380 parse_option_number(name, def_val, &ret, &error_abort);
382 return ret;
384 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
385 ret = opt->value.uint;
386 if (del) {
387 qemu_opt_del_all(opts, name);
389 return ret;
392 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
394 return qemu_opt_get_number_helper(opts, name, defval, false);
397 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
398 uint64_t defval)
400 return qemu_opt_get_number_helper(opts, name, defval, true);
403 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
404 uint64_t defval, bool del)
406 QemuOpt *opt;
407 const char *def_val;
408 uint64_t ret = defval;
410 if (opts == NULL) {
411 return ret;
414 opt = qemu_opt_find(opts, name);
415 if (opt == NULL) {
416 def_val = find_default_by_name(opts, name);
417 if (def_val) {
418 parse_option_size(name, def_val, &ret, &error_abort);
420 return ret;
422 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
423 ret = opt->value.uint;
424 if (del) {
425 qemu_opt_del_all(opts, name);
427 return ret;
430 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
432 return qemu_opt_get_size_helper(opts, name, defval, false);
435 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
436 uint64_t defval)
438 return qemu_opt_get_size_helper(opts, name, defval, true);
441 static bool qemu_opt_parse(QemuOpt *opt, Error **errp)
443 if (opt->desc == NULL)
444 return true;
446 switch (opt->desc->type) {
447 case QEMU_OPT_STRING:
448 /* nothing */
449 return true;
450 case QEMU_OPT_BOOL:
451 return qapi_bool_parse(opt->name, opt->str, &opt->value.boolean, errp);
452 case QEMU_OPT_NUMBER:
453 return parse_option_number(opt->name, opt->str, &opt->value.uint,
454 errp);
455 case QEMU_OPT_SIZE:
456 return parse_option_size(opt->name, opt->str, &opt->value.uint,
457 errp);
458 default:
459 abort();
463 static bool opts_accepts_any(const QemuOptsList *list)
465 return list->desc[0].name == NULL;
468 int qemu_opt_unset(QemuOpts *opts, const char *name)
470 QemuOpt *opt = qemu_opt_find(opts, name);
472 assert(opts_accepts_any(opts->list));
474 if (opt == NULL) {
475 return -1;
476 } else {
477 qemu_opt_del(opt);
478 return 0;
482 static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
483 bool prepend)
485 QemuOpt *opt = g_malloc0(sizeof(*opt));
487 opt->name = g_strdup(name);
488 opt->str = value;
489 opt->opts = opts;
490 if (prepend) {
491 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
492 } else {
493 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
496 return opt;
499 static bool opt_validate(QemuOpt *opt, Error **errp)
501 const QemuOptDesc *desc;
502 const QemuOptsList *list = opt->opts->list;
504 desc = find_desc_by_name(list->desc, opt->name);
505 if (!desc && !opts_accepts_any(list)) {
506 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
507 return false;
510 opt->desc = desc;
511 if (!qemu_opt_parse(opt, errp)) {
512 return false;
515 return true;
518 bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
519 Error **errp)
521 QemuOpt *opt = opt_create(opts, name, g_strdup(value), false);
523 if (!opt_validate(opt, errp)) {
524 qemu_opt_del(opt);
525 return false;
527 return true;
530 bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
531 Error **errp)
533 QemuOpt *opt;
534 const QemuOptDesc *desc;
535 const QemuOptsList *list = opts->list;
537 desc = find_desc_by_name(list->desc, name);
538 if (!desc && !opts_accepts_any(list)) {
539 error_setg(errp, QERR_INVALID_PARAMETER, name);
540 return false;
543 opt = g_malloc0(sizeof(*opt));
544 opt->name = g_strdup(name);
545 opt->opts = opts;
546 opt->desc = desc;
547 opt->value.boolean = !!val;
548 opt->str = g_strdup(val ? "on" : "off");
549 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
550 return true;
553 bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
554 Error **errp)
556 QemuOpt *opt;
557 const QemuOptDesc *desc;
558 const QemuOptsList *list = opts->list;
560 desc = find_desc_by_name(list->desc, name);
561 if (!desc && !opts_accepts_any(list)) {
562 error_setg(errp, QERR_INVALID_PARAMETER, name);
563 return false;
566 opt = g_malloc0(sizeof(*opt));
567 opt->name = g_strdup(name);
568 opt->opts = opts;
569 opt->desc = desc;
570 opt->value.uint = val;
571 opt->str = g_strdup_printf("%" PRId64, val);
572 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
573 return true;
577 * For each member of @opts, call @func(@opaque, name, value, @errp).
578 * @func() may store an Error through @errp, but must return non-zero then.
579 * When @func() returns non-zero, break the loop and return that value.
580 * Return zero when the loop completes.
582 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
583 Error **errp)
585 QemuOpt *opt;
586 int rc;
588 QTAILQ_FOREACH(opt, &opts->head, next) {
589 rc = func(opaque, opt->name, opt->str, errp);
590 if (rc) {
591 return rc;
593 assert(!errp || !*errp);
595 return 0;
598 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
600 QemuOpts *opts;
602 QTAILQ_FOREACH(opts, &list->head, next) {
603 if (!opts->id && !id) {
604 return opts;
606 if (opts->id && id && !strcmp(opts->id, id)) {
607 return opts;
610 return NULL;
613 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
614 int fail_if_exists, Error **errp)
616 QemuOpts *opts = NULL;
618 if (list->merge_lists) {
619 if (id) {
620 error_setg(errp, QERR_INVALID_PARAMETER, "id");
621 return NULL;
623 opts = qemu_opts_find(list, NULL);
624 if (opts) {
625 return opts;
627 } else if (id) {
628 assert(fail_if_exists);
629 if (!id_wellformed(id)) {
630 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
631 "an identifier");
632 error_append_hint(errp, "Identifiers consist of letters, digits, "
633 "'-', '.', '_', starting with a letter.\n");
634 return NULL;
636 opts = qemu_opts_find(list, id);
637 if (opts != NULL) {
638 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
639 return NULL;
642 opts = g_malloc0(sizeof(*opts));
643 opts->id = g_strdup(id);
644 opts->list = list;
645 loc_save(&opts->loc);
646 QTAILQ_INIT(&opts->head);
647 QTAILQ_INSERT_TAIL(&list->head, opts, next);
648 return opts;
651 void qemu_opts_reset(QemuOptsList *list)
653 QemuOpts *opts, *next_opts;
655 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
656 qemu_opts_del(opts);
660 void qemu_opts_loc_restore(QemuOpts *opts)
662 loc_restore(&opts->loc);
665 bool qemu_opts_set(QemuOptsList *list, const char *name, const char *value, Error **errp)
667 QemuOpts *opts;
669 assert(list->merge_lists);
670 opts = qemu_opts_create(list, NULL, 0, &error_abort);
671 return qemu_opt_set(opts, name, value, errp);
674 const char *qemu_opts_id(QemuOpts *opts)
676 return opts->id;
679 /* The id string will be g_free()d by qemu_opts_del */
680 void qemu_opts_set_id(QemuOpts *opts, char *id)
682 opts->id = id;
685 void qemu_opts_del(QemuOpts *opts)
687 QemuOpt *opt;
689 if (opts == NULL) {
690 return;
693 for (;;) {
694 opt = QTAILQ_FIRST(&opts->head);
695 if (opt == NULL)
696 break;
697 qemu_opt_del(opt);
699 QTAILQ_REMOVE(&opts->list->head, opts, next);
700 g_free(opts->id);
701 g_free(opts);
704 /* print value, escaping any commas in value */
705 static void escaped_print(const char *value)
707 const char *ptr;
709 for (ptr = value; *ptr; ++ptr) {
710 if (*ptr == ',') {
711 putchar(',');
713 putchar(*ptr);
717 void qemu_opts_print(QemuOpts *opts, const char *separator)
719 QemuOpt *opt;
720 QemuOptDesc *desc = opts->list->desc;
721 const char *sep = "";
723 if (opts->id) {
724 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
725 sep = separator;
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);
732 sep = separator;
734 return;
736 for (; desc && desc->name; desc++) {
737 const char *value;
738 opt = qemu_opt_find(opts, desc->name);
740 value = opt ? opt->str : desc->def_value_str;
741 if (!value) {
742 continue;
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);
750 } else {
751 printf("%s%s=%s", sep, desc->name, value);
753 sep = separator;
757 static const char *get_opt_name_value(const char *params,
758 const char *firstname,
759 bool warn_on_flag,
760 bool *help_wanted,
761 char **name, char **value)
763 const char *p;
764 const char *prefix = "";
765 size_t len;
766 bool is_help = false;
768 len = strcspn(params, "=,");
769 if (params[len] != '=') {
770 /* found "foo,more" */
771 if (firstname) {
772 /* implicitly named first option */
773 *name = g_strdup(firstname);
774 p = get_opt_value(params, value);
775 } else {
776 /* option without value, must be a flag */
777 p = get_opt_name(params, name, len);
778 if (strncmp(*name, "no", 2) == 0) {
779 memmove(*name, *name + 2, strlen(*name + 2) + 1);
780 *value = g_strdup("off");
781 prefix = "no";
782 } else {
783 *value = g_strdup("on");
784 is_help = is_help_option(*name);
786 if (!is_help && warn_on_flag) {
787 warn_report("short-form boolean option '%s%s' deprecated", prefix, *name);
788 error_printf("Please use %s=%s instead\n", *name, *value);
791 } else {
792 /* found "foo=bar,more" */
793 p = get_opt_name(params, name, len);
794 assert(*p == '=');
795 p++;
796 p = get_opt_value(p, value);
799 assert(!*p || *p == ',');
800 if (help_wanted && is_help) {
801 *help_wanted = true;
803 if (*p == ',') {
804 p++;
806 return p;
809 static bool opts_do_parse(QemuOpts *opts, const char *params,
810 const char *firstname, bool prepend,
811 bool warn_on_flag, bool *help_wanted, Error **errp)
813 char *option, *value;
814 const char *p;
815 QemuOpt *opt;
817 for (p = params; *p;) {
818 p = get_opt_name_value(p, firstname, warn_on_flag, help_wanted, &option, &value);
819 if (help_wanted && *help_wanted) {
820 g_free(option);
821 g_free(value);
822 return false;
824 firstname = NULL;
826 if (!strcmp(option, "id")) {
827 g_free(option);
828 g_free(value);
829 continue;
832 opt = opt_create(opts, option, value, prepend);
833 g_free(option);
834 if (!opt_validate(opt, errp)) {
835 qemu_opt_del(opt);
836 return false;
840 return true;
843 static char *opts_parse_id(const char *params)
845 const char *p;
846 char *name, *value;
848 for (p = params; *p;) {
849 p = get_opt_name_value(p, NULL, false, NULL, &name, &value);
850 if (!strcmp(name, "id")) {
851 g_free(name);
852 return value;
854 g_free(name);
855 g_free(value);
858 return NULL;
861 bool has_help_option(const char *params)
863 const char *p;
864 char *name, *value;
865 bool ret = false;
867 for (p = params; *p;) {
868 p = get_opt_name_value(p, NULL, false, &ret, &name, &value);
869 g_free(name);
870 g_free(value);
871 if (ret) {
872 return true;
876 return false;
880 * Store options parsed from @params into @opts.
881 * If @firstname is non-null, the first key=value in @params may omit
882 * key=, and is treated as if key was @firstname.
883 * On error, store an error object through @errp if non-null.
885 bool qemu_opts_do_parse(QemuOpts *opts, const char *params,
886 const char *firstname, Error **errp)
888 return opts_do_parse(opts, params, firstname, false, false, NULL, errp);
891 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
892 bool permit_abbrev, bool defaults,
893 bool warn_on_flag, bool *help_wanted, Error **errp)
895 const char *firstname;
896 char *id = opts_parse_id(params);
897 QemuOpts *opts;
899 assert(!permit_abbrev || list->implied_opt_name);
900 firstname = permit_abbrev ? list->implied_opt_name : NULL;
903 * This code doesn't work for defaults && !list->merge_lists: when
904 * params has no id=, and list has an element with !opts->id, it
905 * appends a new element instead of returning the existing opts.
906 * However, we got no use for this case. Guard against possible
907 * (if unlikely) future misuse:
909 assert(!defaults || list->merge_lists);
910 opts = qemu_opts_create(list, id, !list->merge_lists, errp);
911 g_free(id);
912 if (opts == NULL) {
913 return NULL;
916 if (!opts_do_parse(opts, params, firstname, defaults,
917 warn_on_flag, help_wanted, errp)) {
918 qemu_opts_del(opts);
919 return NULL;
922 return opts;
926 * Create a QemuOpts in @list and with options parsed from @params.
927 * If @permit_abbrev, the first key=value in @params may omit key=,
928 * and is treated as if key was @list->implied_opt_name.
929 * On error, store an error object through @errp if non-null.
930 * Return the new QemuOpts on success, null pointer on error.
932 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
933 bool permit_abbrev, Error **errp)
935 return opts_parse(list, params, permit_abbrev, false, false, NULL, errp);
939 * Create a QemuOpts in @list and with options parsed from @params.
940 * If @permit_abbrev, the first key=value in @params may omit key=,
941 * and is treated as if key was @list->implied_opt_name.
942 * Report errors with error_report_err(). This is inappropriate in
943 * QMP context. Do not use this function there!
944 * Return the new QemuOpts on success, null pointer on error.
946 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
947 bool permit_abbrev)
949 Error *err = NULL;
950 QemuOpts *opts;
951 bool help_wanted = false;
953 opts = opts_parse(list, params, permit_abbrev, false, true,
954 opts_accepts_any(list) ? NULL : &help_wanted,
955 &err);
956 if (!opts) {
957 assert(!!err + !!help_wanted == 1);
958 if (help_wanted) {
959 qemu_opts_print_help(list, true);
960 } else {
961 error_report_err(err);
964 return opts;
967 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
968 int permit_abbrev)
970 QemuOpts *opts;
972 opts = opts_parse(list, params, permit_abbrev, true, false, NULL, NULL);
973 assert(opts);
976 static bool qemu_opts_from_qdict_entry(QemuOpts *opts,
977 const QDictEntry *entry,
978 Error **errp)
980 const char *key = qdict_entry_key(entry);
981 QObject *obj = qdict_entry_value(entry);
982 char buf[32];
983 g_autofree char *tmp = NULL;
984 const char *value;
986 if (!strcmp(key, "id")) {
987 return true;
990 switch (qobject_type(obj)) {
991 case QTYPE_QSTRING:
992 value = qstring_get_str(qobject_to(QString, obj));
993 break;
994 case QTYPE_QNUM:
995 tmp = qnum_to_string(qobject_to(QNum, obj));
996 value = tmp;
997 break;
998 case QTYPE_QBOOL:
999 pstrcpy(buf, sizeof(buf),
1000 qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
1001 value = buf;
1002 break;
1003 default:
1004 return true;
1007 return qemu_opt_set(opts, key, value, errp);
1011 * Create QemuOpts from a QDict.
1012 * Use value of key "id" as ID if it exists and is a QString. Only
1013 * QStrings, QNums and QBools are copied. Entries with other types
1014 * are silently ignored.
1016 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1017 Error **errp)
1019 QemuOpts *opts;
1020 const QDictEntry *entry;
1022 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
1023 if (!opts) {
1024 return NULL;
1027 assert(opts != NULL);
1029 for (entry = qdict_first(qdict);
1030 entry;
1031 entry = qdict_next(qdict, entry)) {
1032 if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
1033 qemu_opts_del(opts);
1034 return NULL;
1038 return opts;
1042 * Adds all QDict entries to the QemuOpts that can be added and removes them
1043 * from the QDict. When this function returns, the QDict contains only those
1044 * entries that couldn't be added to the QemuOpts.
1046 bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1048 const QDictEntry *entry, *next;
1050 entry = qdict_first(qdict);
1052 while (entry != NULL) {
1053 next = qdict_next(qdict, entry);
1055 if (find_desc_by_name(opts->list->desc, entry->key)) {
1056 if (!qemu_opts_from_qdict_entry(opts, entry, errp)) {
1057 return false;
1059 qdict_del(qdict, entry->key);
1062 entry = next;
1065 return true;
1069 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1071 * If @list is given, only add those options to the QDict that are contained in
1072 * the list. If @del is true, any options added to the QDict are removed from
1073 * the QemuOpts, otherwise they remain there.
1075 * If two options in @opts have the same name, they are processed in order
1076 * so that the last one wins (consistent with the reverse iteration in
1077 * qemu_opt_find()), but all of them are deleted if @del is true.
1079 * TODO We'll want to use types appropriate for opt->desc->type, but
1080 * this is enough for now.
1082 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1083 QemuOptsList *list, bool del)
1085 QemuOpt *opt, *next;
1087 if (!qdict) {
1088 qdict = qdict_new();
1090 if (opts->id) {
1091 qdict_put_str(qdict, "id", opts->id);
1093 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1094 if (list) {
1095 QemuOptDesc *desc;
1096 bool found = false;
1097 for (desc = list->desc; desc->name; desc++) {
1098 if (!strcmp(desc->name, opt->name)) {
1099 found = true;
1100 break;
1103 if (!found) {
1104 continue;
1107 qdict_put_str(qdict, opt->name, opt->str);
1108 if (del) {
1109 qemu_opt_del(opt);
1112 return qdict;
1115 /* Copy all options in a QemuOpts to the given QDict. See
1116 * qemu_opts_to_qdict_filtered() for details. */
1117 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1119 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1122 /* Validate parsed opts against descriptions where no
1123 * descriptions were provided in the QemuOptsList.
1125 bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1127 QemuOpt *opt;
1129 assert(opts_accepts_any(opts->list));
1131 QTAILQ_FOREACH(opt, &opts->head, next) {
1132 opt->desc = find_desc_by_name(desc, opt->name);
1133 if (!opt->desc) {
1134 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1135 return false;
1138 if (!qemu_opt_parse(opt, errp)) {
1139 return false;
1143 return true;
1147 * For each member of @list, call @func(@opaque, member, @errp).
1148 * Call it with the current location temporarily set to the member's.
1149 * @func() may store an Error through @errp, but must return non-zero then.
1150 * When @func() returns non-zero, break the loop and return that value.
1151 * Return zero when the loop completes.
1153 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1154 void *opaque, Error **errp)
1156 Location loc;
1157 QemuOpts *opts;
1158 int rc = 0;
1160 loc_push_none(&loc);
1161 QTAILQ_FOREACH(opts, &list->head, next) {
1162 loc_restore(&opts->loc);
1163 rc = func(opaque, opts, errp);
1164 if (rc) {
1165 break;
1167 assert(!errp || !*errp);
1169 loc_pop(&loc);
1170 return rc;
1173 static size_t count_opts_list(QemuOptsList *list)
1175 QemuOptDesc *desc = NULL;
1176 size_t num_opts = 0;
1178 if (!list) {
1179 return 0;
1182 desc = list->desc;
1183 while (desc && desc->name) {
1184 num_opts++;
1185 desc++;
1188 return num_opts;
1191 void qemu_opts_free(QemuOptsList *list)
1193 g_free(list);
1196 /* Realloc dst option list and append options from an option list (list)
1197 * to it. dst could be NULL or a malloced list.
1198 * The lifetime of dst must be shorter than the input list because the
1199 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1201 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1202 QemuOptsList *list)
1204 size_t num_opts, num_dst_opts;
1205 QemuOptDesc *desc;
1206 bool need_init = false;
1207 bool need_head_update;
1209 if (!list) {
1210 return dst;
1213 /* If dst is NULL, after realloc, some area of dst should be initialized
1214 * before adding options to it.
1216 if (!dst) {
1217 need_init = true;
1218 need_head_update = true;
1219 } else {
1220 /* Moreover, even if dst is not NULL, the realloc may move it to a
1221 * different address in which case we may get a stale tail pointer
1222 * in dst->head. */
1223 need_head_update = QTAILQ_EMPTY(&dst->head);
1226 num_opts = count_opts_list(dst);
1227 num_dst_opts = num_opts;
1228 num_opts += count_opts_list(list);
1229 dst = g_realloc(dst, sizeof(QemuOptsList) +
1230 (num_opts + 1) * sizeof(QemuOptDesc));
1231 if (need_init) {
1232 dst->name = NULL;
1233 dst->implied_opt_name = NULL;
1234 dst->merge_lists = false;
1236 if (need_head_update) {
1237 QTAILQ_INIT(&dst->head);
1239 dst->desc[num_dst_opts].name = NULL;
1241 /* append list->desc to dst->desc */
1242 if (list) {
1243 desc = list->desc;
1244 while (desc && desc->name) {
1245 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1246 dst->desc[num_dst_opts++] = *desc;
1247 dst->desc[num_dst_opts].name = NULL;
1249 desc++;
1253 return dst;