qdev: Add new qdev_init_gpio_in_named_with_opaque()
[qemu.git] / util / qemu-option.c
bloba401e936daf65f198a74530aa2ff030559406e56
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-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"
38 #include "qemu/id.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
50 * name in p.
52 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
54 char *q;
56 q = buf;
57 while (*p != '\0' && *p != delim) {
58 if (q && (q - buf) < buf_size - 1)
59 *q++ = *p;
60 p++;
62 if (q)
63 *q = '\0';
65 return p;
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)
78 char *q;
80 q = buf;
81 while (*p != '\0') {
82 if (*p == ',') {
83 if (*(p + 1) != ',')
84 break;
85 p++;
87 if (q && (q - buf) < buf_size - 1)
88 *q++ = *p;
89 p++;
91 if (q)
92 *q = '\0';
94 return p;
97 static void parse_option_bool(const char *name, const char *value, bool *ret,
98 Error **errp)
100 if (!strcmp(value, "on")) {
101 *ret = 1;
102 } else if (!strcmp(value, "off")) {
103 *ret = 0;
104 } else {
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)
113 uint64_t number;
114 int err;
116 err = qemu_strtou64(value, NULL, 0, &number);
117 if (err == -ERANGE) {
118 error_setg(errp, "Value '%s' is too large for parameter '%s'",
119 value, name);
120 return;
122 if (err) {
123 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
124 return;
126 *ret = number;
129 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
130 const char *name)
132 int i;
134 for (i = 0; desc[i].name != NULL; i++) {
135 if (strcmp(desc[i].name, name) == 0) {
136 return &desc[i];
140 return NULL;
143 void parse_option_size(const char *name, const char *value,
144 uint64_t *ret, Error **errp)
146 uint64_t size;
147 int err;
149 err = qemu_strtosz(value, NULL, &size);
150 if (err == -ERANGE) {
151 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
152 value, name);
153 return;
155 if (err) {
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");
161 return;
163 *ret = size;
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;
171 bool result = false;
173 while (*p) {
174 p = get_opt_value(buf, buflen, p);
175 if (*p) {
176 p++;
179 if (is_help_option(buf)) {
180 result = true;
181 goto out;
185 out:
186 g_free(buf);
187 return result;
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;
195 bool result = true;
197 while (*p) {
198 p = get_opt_value(buf, buflen, p);
199 if (*p && !*++p) {
200 result = false;
201 goto out;
204 if (!*buf || *buf == ',') {
205 result = false;
206 goto out;
210 out:
211 g_free(buf);
212 return result;
215 void qemu_opts_print_help(QemuOptsList *list)
217 QemuOptDesc *desc;
219 assert(list);
220 desc = list->desc;
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");
225 desc++;
228 /* ------------------------------------------------------------------ */
230 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
232 QemuOpt *opt;
234 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
235 if (strcmp(opt->name, name) != 0)
236 continue;
237 return opt;
239 return NULL;
242 static void qemu_opt_del(QemuOpt *opt)
244 QTAILQ_REMOVE(&opt->opts->head, opt, next);
245 g_free(opt->name);
246 g_free(opt->str);
247 g_free(opt);
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)) {
259 qemu_opt_del(opt);
264 const char *qemu_opt_get(QemuOpts *opts, const char *name)
266 QemuOpt *opt;
268 if (opts == NULL) {
269 return NULL;
272 opt = qemu_opt_find(opts, name);
273 if (!opt) {
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)
284 iter->opts = opts;
285 iter->opt = QTAILQ_FIRST(&opts->head);
286 iter->name = name;
289 const char *qemu_opt_iter_next(QemuOptsIter *iter)
291 QemuOpt *ret = iter->opt;
292 if (iter->name) {
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)
307 QemuOpt *opt;
308 const QemuOptDesc *desc;
309 char *str = NULL;
311 if (opts == NULL) {
312 return NULL;
315 opt = qemu_opt_find(opts, name);
316 if (!opt) {
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);
321 return str;
323 str = opt->str;
324 opt->str = NULL;
325 qemu_opt_del_all(opts, name);
326 return str;
329 bool qemu_opt_has_help_opt(QemuOpts *opts)
331 QemuOpt *opt;
333 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
334 if (is_help_option(opt->name)) {
335 return true;
338 return false;
341 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
342 bool defval, bool del)
344 QemuOpt *opt;
345 bool ret = defval;
347 if (opts == NULL) {
348 return ret;
351 opt = qemu_opt_find(opts, name);
352 if (opt == NULL) {
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);
357 return ret;
359 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
360 ret = opt->value.boolean;
361 if (del) {
362 qemu_opt_del_all(opts, name);
364 return ret;
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)
380 QemuOpt *opt;
381 uint64_t ret = defval;
383 if (opts == NULL) {
384 return ret;
387 opt = qemu_opt_find(opts, name);
388 if (opt == NULL) {
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);
393 return ret;
395 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
396 ret = opt->value.uint;
397 if (del) {
398 qemu_opt_del_all(opts, name);
400 return ret;
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,
409 uint64_t defval)
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)
417 QemuOpt *opt;
418 uint64_t ret = defval;
420 if (opts == NULL) {
421 return ret;
424 opt = qemu_opt_find(opts, name);
425 if (opt == NULL) {
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);
430 return ret;
432 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
433 ret = opt->value.uint;
434 if (del) {
435 qemu_opt_del_all(opts, name);
437 return ret;
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,
446 uint64_t defval)
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)
454 return;
456 switch (opt->desc->type) {
457 case QEMU_OPT_STRING:
458 /* nothing */
459 return;
460 case QEMU_OPT_BOOL:
461 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
462 break;
463 case QEMU_OPT_NUMBER:
464 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
465 break;
466 case QEMU_OPT_SIZE:
467 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
468 break;
469 default:
470 abort();
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));
485 if (opt == NULL) {
486 return -1;
487 } else {
488 qemu_opt_del(opt);
489 return 0;
493 static void opt_set(QemuOpts *opts, const char *name, const char *value,
494 bool prepend, Error **errp)
496 QemuOpt *opt;
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);
503 return;
506 opt = g_malloc0(sizeof(*opt));
507 opt->name = g_strdup(name);
508 opt->opts = opts;
509 if (prepend) {
510 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
511 } else {
512 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
514 opt->desc = desc;
515 opt->str = g_strdup(value);
516 assert(opt->str);
517 qemu_opt_parse(opt, &local_err);
518 if (local_err) {
519 error_propagate(errp, local_err);
520 qemu_opt_del(opt);
524 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
525 Error **errp)
527 opt_set(opts, name, value, false, errp);
530 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
531 Error **errp)
533 QemuOpt *opt;
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);
540 g_free(opt);
541 return;
544 opt->name = g_strdup(name);
545 opt->opts = opts;
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,
552 Error **errp)
554 QemuOpt *opt;
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);
561 g_free(opt);
562 return;
565 opt->name = g_strdup(name);
566 opt->opts = opts;
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,
579 Error **errp)
581 QemuOpt *opt;
582 int rc;
584 QTAILQ_FOREACH(opt, &opts->head, next) {
585 rc = func(opaque, opt->name, opt->str, errp);
586 if (rc) {
587 return rc;
589 assert(!errp || !*errp);
591 return 0;
594 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
596 QemuOpts *opts;
598 QTAILQ_FOREACH(opts, &list->head, next) {
599 if (!opts->id && !id) {
600 return opts;
602 if (opts->id && id && !strcmp(opts->id, id)) {
603 return opts;
606 return NULL;
609 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
610 int fail_if_exists, Error **errp)
612 QemuOpts *opts = NULL;
614 if (id) {
615 if (!id_wellformed(id)) {
616 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
617 "an identifier");
618 error_append_hint(errp, "Identifiers consist of letters, digits, "
619 "'-', '.', '_', starting with a letter.\n");
620 return NULL;
622 opts = qemu_opts_find(list, id);
623 if (opts != NULL) {
624 if (fail_if_exists && !list->merge_lists) {
625 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
626 return NULL;
627 } else {
628 return opts;
631 } else if (list->merge_lists) {
632 opts = qemu_opts_find(list, NULL);
633 if (opts) {
634 return opts;
637 opts = g_malloc0(sizeof(*opts));
638 opts->id = g_strdup(id);
639 opts->list = list;
640 loc_save(&opts->loc);
641 QTAILQ_INIT(&opts->head);
642 QTAILQ_INSERT_TAIL(&list->head, opts, next);
643 return opts;
646 void qemu_opts_reset(QemuOptsList *list)
648 QemuOpts *opts, *next_opts;
650 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
651 qemu_opts_del(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)
663 QemuOpts *opts;
664 Error *local_err = NULL;
666 opts = qemu_opts_create(list, id, 1, &local_err);
667 if (local_err) {
668 error_propagate(errp, local_err);
669 return;
671 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 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++) {
765 pe = strchr(p, '=');
766 pc = strchr(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);
773 } else {
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");
779 } else {
780 pstrcpy(value, sizeof(value), "on");
783 } else {
784 /* found "foo=bar,more" */
785 p = get_opt_name(option, sizeof(option), p, '=');
786 if (*p != '=') {
787 break;
789 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);
795 if (local_err) {
796 error_propagate(errp, local_err);
797 return;
800 if (*p != ',') {
801 break;
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;
823 const char *p;
824 QemuOpts *opts;
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);
832 id = value;
833 } else if ((p = strstr(params, ",id=")) != NULL) {
834 get_opt_value(value, sizeof(value), p+4);
835 id = value;
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);
847 if (opts == NULL) {
848 error_propagate(errp, local_err);
849 return NULL;
852 opts_do_parse(opts, params, firstname, defaults, &local_err);
853 if (local_err) {
854 error_propagate(errp, local_err);
855 qemu_opts_del(opts);
856 return NULL;
859 return opts;
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,
884 bool permit_abbrev)
886 Error *err = NULL;
887 QemuOpts *opts;
889 opts = opts_parse(list, params, permit_abbrev, false, &err);
890 if (err) {
891 error_report_err(err);
893 return opts;
896 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
897 int permit_abbrev)
899 QemuOpts *opts;
901 opts = opts_parse(list, params, permit_abbrev, true, NULL);
902 assert(opts);
905 typedef struct OptsFromQDictState {
906 QemuOpts *opts;
907 Error **errp;
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;
914 const char *value;
916 if (!strcmp(key, "id") || *state->errp) {
917 return;
920 switch (qobject_type(obj)) {
921 case QTYPE_QSTRING:
922 value = qstring_get_str(qobject_to_qstring(obj));
923 break;
924 case QTYPE_QNUM:
925 tmp = qnum_to_string(qobject_to_qnum(obj));
926 value = tmp;
927 break;
928 case QTYPE_QBOOL:
929 pstrcpy(buf, sizeof(buf),
930 qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off");
931 value = buf;
932 break;
933 default:
934 return;
937 qemu_opt_set(state->opts, key, value, state->errp);
938 g_free(tmp);
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,
948 Error **errp)
950 OptsFromQDictState state;
951 Error *local_err = NULL;
952 QemuOpts *opts;
954 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
955 &local_err);
956 if (local_err) {
957 error_propagate(errp, local_err);
958 return NULL;
961 assert(opts != NULL);
963 state.errp = &local_err;
964 state.opts = opts;
965 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
966 if (local_err) {
967 error_propagate(errp, local_err);
968 qemu_opts_del(opts);
969 return NULL;
972 return opts;
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 = {
989 .errp = &local_err,
990 .opts = opts,
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);
997 if (local_err) {
998 error_propagate(errp, local_err);
999 return;
1000 } else {
1001 qdict_del(qdict, entry->key);
1005 entry = next;
1010 * Convert from QemuOpts to QDict.
1011 * The QDict values are of type QString.
1012 * TODO We'll want to use types appropriate for opt->desc->type, but
1013 * this is enough for now.
1015 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1017 QemuOpt *opt;
1019 if (!qdict) {
1020 qdict = qdict_new();
1022 if (opts->id) {
1023 qdict_put_str(qdict, "id", opts->id);
1025 QTAILQ_FOREACH(opt, &opts->head, next) {
1026 qdict_put_str(qdict, opt->name, opt->str);
1028 return qdict;
1031 /* Validate parsed opts against descriptions where no
1032 * descriptions were provided in the QemuOptsList.
1034 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1036 QemuOpt *opt;
1037 Error *local_err = NULL;
1039 assert(opts_accepts_any(opts));
1041 QTAILQ_FOREACH(opt, &opts->head, next) {
1042 opt->desc = find_desc_by_name(desc, opt->name);
1043 if (!opt->desc) {
1044 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1045 return;
1048 qemu_opt_parse(opt, &local_err);
1049 if (local_err) {
1050 error_propagate(errp, local_err);
1051 return;
1057 * For each member of @list, call @func(@opaque, member, @errp).
1058 * Call it with the current location temporarily set to the member's.
1059 * @func() may store an Error through @errp, but must return non-zero then.
1060 * When @func() returns non-zero, break the loop and return that value.
1061 * Return zero when the loop completes.
1063 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1064 void *opaque, Error **errp)
1066 Location loc;
1067 QemuOpts *opts;
1068 int rc = 0;
1070 loc_push_none(&loc);
1071 QTAILQ_FOREACH(opts, &list->head, next) {
1072 loc_restore(&opts->loc);
1073 rc = func(opaque, opts, errp);
1074 if (rc) {
1075 break;
1077 assert(!errp || !*errp);
1079 loc_pop(&loc);
1080 return rc;
1083 static size_t count_opts_list(QemuOptsList *list)
1085 QemuOptDesc *desc = NULL;
1086 size_t num_opts = 0;
1088 if (!list) {
1089 return 0;
1092 desc = list->desc;
1093 while (desc && desc->name) {
1094 num_opts++;
1095 desc++;
1098 return num_opts;
1101 void qemu_opts_free(QemuOptsList *list)
1103 g_free(list);
1106 /* Realloc dst option list and append options from an option list (list)
1107 * to it. dst could be NULL or a malloced list.
1108 * The lifetime of dst must be shorter than the input list because the
1109 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1111 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1112 QemuOptsList *list)
1114 size_t num_opts, num_dst_opts;
1115 QemuOptDesc *desc;
1116 bool need_init = false;
1117 bool need_head_update;
1119 if (!list) {
1120 return dst;
1123 /* If dst is NULL, after realloc, some area of dst should be initialized
1124 * before adding options to it.
1126 if (!dst) {
1127 need_init = true;
1128 need_head_update = true;
1129 } else {
1130 /* Moreover, even if dst is not NULL, the realloc may move it to a
1131 * different address in which case we may get a stale tail pointer
1132 * in dst->head. */
1133 need_head_update = QTAILQ_EMPTY(&dst->head);
1136 num_opts = count_opts_list(dst);
1137 num_dst_opts = num_opts;
1138 num_opts += count_opts_list(list);
1139 dst = g_realloc(dst, sizeof(QemuOptsList) +
1140 (num_opts + 1) * sizeof(QemuOptDesc));
1141 if (need_init) {
1142 dst->name = NULL;
1143 dst->implied_opt_name = NULL;
1144 dst->merge_lists = false;
1146 if (need_head_update) {
1147 QTAILQ_INIT(&dst->head);
1149 dst->desc[num_dst_opts].name = NULL;
1151 /* append list->desc to dst->desc */
1152 if (list) {
1153 desc = list->desc;
1154 while (desc && desc->name) {
1155 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1156 dst->desc[num_dst_opts++] = *desc;
1157 dst->desc[num_dst_opts].name = NULL;
1159 desc++;
1163 return dst;