target-arm: Restrict AArch64 TLB flushes to the MMU indexes they must touch
[qemu.git] / util / qemu-option.c
blobefe9d279c423e15c16069b6c091ce5bc8748aa6c
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 <stdio.h>
27 #include <string.h>
29 #include "qemu-common.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/types.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qemu/option_int.h"
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
40 * The option name is delimited by delim (usually , or =) or the string end
41 * and is copied into buf. If the option name is longer than buf_size, it is
42 * truncated. buf is always zero terminated.
44 * The return value is the position of the delimiter/zero byte after the option
45 * name in p.
47 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
49 char *q;
51 q = buf;
52 while (*p != '\0' && *p != delim) {
53 if (q && (q - buf) < buf_size - 1)
54 *q++ = *p;
55 p++;
57 if (q)
58 *q = '\0';
60 return p;
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
67 * This function is comparable to get_opt_name with the difference that the
68 * delimiter is fixed to be comma which starts a new option. To specify an
69 * option value that contains commas, double each comma.
71 const char *get_opt_value(char *buf, int buf_size, const char *p)
73 char *q;
75 q = buf;
76 while (*p != '\0') {
77 if (*p == ',') {
78 if (*(p + 1) != ',')
79 break;
80 p++;
82 if (q && (q - buf) < buf_size - 1)
83 *q++ = *p;
84 p++;
86 if (q)
87 *q = '\0';
89 return p;
92 int get_next_param_value(char *buf, int buf_size,
93 const char *tag, const char **pstr)
95 const char *p;
96 char option[128];
98 p = *pstr;
99 for(;;) {
100 p = get_opt_name(option, sizeof(option), p, '=');
101 if (*p != '=')
102 break;
103 p++;
104 if (!strcmp(tag, option)) {
105 *pstr = get_opt_value(buf, buf_size, p);
106 if (**pstr == ',') {
107 (*pstr)++;
109 return strlen(buf);
110 } else {
111 p = get_opt_value(NULL, 0, p);
113 if (*p != ',')
114 break;
115 p++;
117 return 0;
120 int get_param_value(char *buf, int buf_size,
121 const char *tag, const char *str)
123 return get_next_param_value(buf, buf_size, tag, &str);
126 static void parse_option_bool(const char *name, const char *value, bool *ret,
127 Error **errp)
129 if (value != NULL) {
130 if (!strcmp(value, "on")) {
131 *ret = 1;
132 } else if (!strcmp(value, "off")) {
133 *ret = 0;
134 } else {
135 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
136 name, "'on' or 'off'");
138 } else {
139 *ret = 1;
143 static void parse_option_number(const char *name, const char *value,
144 uint64_t *ret, Error **errp)
146 char *postfix;
147 uint64_t number;
149 if (value != NULL) {
150 number = strtoull(value, &postfix, 0);
151 if (*postfix != '\0') {
152 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
153 return;
155 *ret = number;
156 } else {
157 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
161 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
162 const char *name)
164 int i;
166 for (i = 0; desc[i].name != NULL; i++) {
167 if (strcmp(desc[i].name, name) == 0) {
168 return &desc[i];
172 return NULL;
175 void parse_option_size(const char *name, const char *value,
176 uint64_t *ret, Error **errp)
178 char *postfix;
179 double sizef;
181 if (value != NULL) {
182 sizef = strtod(value, &postfix);
183 switch (*postfix) {
184 case 'T':
185 sizef *= 1024;
186 /* fall through */
187 case 'G':
188 sizef *= 1024;
189 /* fall through */
190 case 'M':
191 sizef *= 1024;
192 /* fall through */
193 case 'K':
194 case 'k':
195 sizef *= 1024;
196 /* fall through */
197 case 'b':
198 case '\0':
199 *ret = (uint64_t) sizef;
200 break;
201 default:
202 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
203 #if 0 /* conversion from qerror_report() to error_set() broke this: */
204 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
205 "kilobytes, megabytes, gigabytes and terabytes.\n");
206 #endif
207 return;
209 } else {
210 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
214 bool has_help_option(const char *param)
216 size_t buflen = strlen(param) + 1;
217 char *buf = g_malloc(buflen);
218 const char *p = param;
219 bool result = false;
221 while (*p) {
222 p = get_opt_value(buf, buflen, p);
223 if (*p) {
224 p++;
227 if (is_help_option(buf)) {
228 result = true;
229 goto out;
233 out:
234 g_free(buf);
235 return result;
238 bool is_valid_option_list(const char *param)
240 size_t buflen = strlen(param) + 1;
241 char *buf = g_malloc(buflen);
242 const char *p = param;
243 bool result = true;
245 while (*p) {
246 p = get_opt_value(buf, buflen, p);
247 if (*p && !*++p) {
248 result = false;
249 goto out;
252 if (!*buf || *buf == ',') {
253 result = false;
254 goto out;
258 out:
259 g_free(buf);
260 return result;
263 void qemu_opts_print_help(QemuOptsList *list)
265 QemuOptDesc *desc;
267 assert(list);
268 desc = list->desc;
269 printf("Supported options:\n");
270 while (desc && desc->name) {
271 printf("%-16s %s\n", desc->name,
272 desc->help ? desc->help : "No description available");
273 desc++;
276 /* ------------------------------------------------------------------ */
278 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
280 QemuOpt *opt;
282 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
283 if (strcmp(opt->name, name) != 0)
284 continue;
285 return opt;
287 return NULL;
290 static void qemu_opt_del(QemuOpt *opt)
292 QTAILQ_REMOVE(&opt->opts->head, opt, next);
293 g_free(opt->name);
294 g_free(opt->str);
295 g_free(opt);
298 /* qemu_opt_set allows many settings for the same option.
299 * This function deletes all settings for an option.
301 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
303 QemuOpt *opt, *next_opt;
305 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
306 if (!strcmp(opt->name, name)) {
307 qemu_opt_del(opt);
312 const char *qemu_opt_get(QemuOpts *opts, const char *name)
314 QemuOpt *opt;
316 if (opts == NULL) {
317 return NULL;
320 opt = qemu_opt_find(opts, name);
321 if (!opt) {
322 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
323 if (desc && desc->def_value_str) {
324 return desc->def_value_str;
327 return opt ? opt->str : NULL;
330 /* Get a known option (or its default) and remove it from the list
331 * all in one action. Return a malloced string of the option value.
332 * Result must be freed by caller with g_free().
334 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
336 QemuOpt *opt;
337 const QemuOptDesc *desc;
338 char *str = NULL;
340 if (opts == NULL) {
341 return NULL;
344 opt = qemu_opt_find(opts, name);
345 if (!opt) {
346 desc = find_desc_by_name(opts->list->desc, name);
347 if (desc && desc->def_value_str) {
348 str = g_strdup(desc->def_value_str);
350 return str;
352 str = opt->str;
353 opt->str = NULL;
354 qemu_opt_del_all(opts, name);
355 return str;
358 bool qemu_opt_has_help_opt(QemuOpts *opts)
360 QemuOpt *opt;
362 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
363 if (is_help_option(opt->name)) {
364 return true;
367 return false;
370 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
371 bool defval, bool del)
373 QemuOpt *opt;
374 bool ret = defval;
376 if (opts == NULL) {
377 return ret;
380 opt = qemu_opt_find(opts, name);
381 if (opt == NULL) {
382 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
383 if (desc && desc->def_value_str) {
384 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
386 return ret;
388 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
389 ret = opt->value.boolean;
390 if (del) {
391 qemu_opt_del_all(opts, name);
393 return ret;
396 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
398 return qemu_opt_get_bool_helper(opts, name, defval, false);
401 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
403 return qemu_opt_get_bool_helper(opts, name, defval, true);
406 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
407 uint64_t defval, bool del)
409 QemuOpt *opt;
410 uint64_t ret = defval;
412 if (opts == NULL) {
413 return ret;
416 opt = qemu_opt_find(opts, name);
417 if (opt == NULL) {
418 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
419 if (desc && desc->def_value_str) {
420 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
422 return ret;
424 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
425 ret = opt->value.uint;
426 if (del) {
427 qemu_opt_del_all(opts, name);
429 return ret;
432 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
434 return qemu_opt_get_number_helper(opts, name, defval, false);
437 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
438 uint64_t defval)
440 return qemu_opt_get_number_helper(opts, name, defval, true);
443 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
444 uint64_t defval, bool del)
446 QemuOpt *opt;
447 uint64_t ret = defval;
449 if (opts == NULL) {
450 return ret;
453 opt = qemu_opt_find(opts, name);
454 if (opt == NULL) {
455 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
456 if (desc && desc->def_value_str) {
457 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
459 return ret;
461 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
462 ret = opt->value.uint;
463 if (del) {
464 qemu_opt_del_all(opts, name);
466 return ret;
469 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
471 return qemu_opt_get_size_helper(opts, name, defval, false);
474 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
475 uint64_t defval)
477 return qemu_opt_get_size_helper(opts, name, defval, true);
480 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
482 if (opt->desc == NULL)
483 return;
485 switch (opt->desc->type) {
486 case QEMU_OPT_STRING:
487 /* nothing */
488 return;
489 case QEMU_OPT_BOOL:
490 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
491 break;
492 case QEMU_OPT_NUMBER:
493 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
494 break;
495 case QEMU_OPT_SIZE:
496 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
497 break;
498 default:
499 abort();
503 static bool opts_accepts_any(const QemuOpts *opts)
505 return opts->list->desc[0].name == NULL;
508 int qemu_opt_unset(QemuOpts *opts, const char *name)
510 QemuOpt *opt = qemu_opt_find(opts, name);
512 assert(opts_accepts_any(opts));
514 if (opt == NULL) {
515 return -1;
516 } else {
517 qemu_opt_del(opt);
518 return 0;
522 static void opt_set(QemuOpts *opts, const char *name, const char *value,
523 bool prepend, Error **errp)
525 QemuOpt *opt;
526 const QemuOptDesc *desc;
527 Error *local_err = NULL;
529 desc = find_desc_by_name(opts->list->desc, name);
530 if (!desc && !opts_accepts_any(opts)) {
531 error_setg(errp, QERR_INVALID_PARAMETER, name);
532 return;
535 opt = g_malloc0(sizeof(*opt));
536 opt->name = g_strdup(name);
537 opt->opts = opts;
538 if (prepend) {
539 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
540 } else {
541 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
543 opt->desc = desc;
544 opt->str = g_strdup(value);
545 qemu_opt_parse(opt, &local_err);
546 if (local_err) {
547 error_propagate(errp, local_err);
548 qemu_opt_del(opt);
552 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
553 Error **errp)
555 opt_set(opts, name, value, false, errp);
558 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
559 Error **errp)
561 QemuOpt *opt;
562 const QemuOptDesc *desc = opts->list->desc;
564 opt = g_malloc0(sizeof(*opt));
565 opt->desc = find_desc_by_name(desc, name);
566 if (!opt->desc && !opts_accepts_any(opts)) {
567 error_setg(errp, QERR_INVALID_PARAMETER, name);
568 g_free(opt);
569 return;
572 opt->name = g_strdup(name);
573 opt->opts = opts;
574 opt->value.boolean = !!val;
575 opt->str = g_strdup(val ? "on" : "off");
576 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
579 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
580 Error **errp)
582 QemuOpt *opt;
583 const QemuOptDesc *desc = opts->list->desc;
585 opt = g_malloc0(sizeof(*opt));
586 opt->desc = find_desc_by_name(desc, name);
587 if (!opt->desc && !opts_accepts_any(opts)) {
588 error_setg(errp, QERR_INVALID_PARAMETER, name);
589 g_free(opt);
590 return;
593 opt->name = g_strdup(name);
594 opt->opts = opts;
595 opt->value.uint = val;
596 opt->str = g_strdup_printf("%" PRId64, val);
597 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
601 * For each member of @opts, call @func(@opaque, name, value, @errp).
602 * @func() may store an Error through @errp, but must return non-zero then.
603 * When @func() returns non-zero, break the loop and return that value.
604 * Return zero when the loop completes.
606 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
607 Error **errp)
609 QemuOpt *opt;
610 int rc;
612 QTAILQ_FOREACH(opt, &opts->head, next) {
613 rc = func(opaque, opt->name, opt->str, errp);
614 if (rc) {
615 return rc;
617 assert(!errp || !*errp);
619 return 0;
622 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
624 QemuOpts *opts;
626 QTAILQ_FOREACH(opts, &list->head, next) {
627 if (!opts->id && !id) {
628 return opts;
630 if (opts->id && id && !strcmp(opts->id, id)) {
631 return opts;
634 return NULL;
637 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
638 int fail_if_exists, Error **errp)
640 QemuOpts *opts = NULL;
642 if (id) {
643 if (!id_wellformed(id)) {
644 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
645 "an identifier");
646 #if 0 /* conversion from qerror_report() to error_set() broke this: */
647 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
648 #endif
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 void qemu_opts_set(QemuOptsList *list, const char *id,
690 const char *name, const char *value, Error **errp)
692 QemuOpts *opts;
693 Error *local_err = NULL;
695 opts = qemu_opts_create(list, id, 1, &local_err);
696 if (local_err) {
697 error_propagate(errp, local_err);
698 return;
700 qemu_opt_set(opts, name, value, errp);
703 const char *qemu_opts_id(QemuOpts *opts)
705 return opts->id;
708 /* The id string will be g_free()d by qemu_opts_del */
709 void qemu_opts_set_id(QemuOpts *opts, char *id)
711 opts->id = id;
714 void qemu_opts_del(QemuOpts *opts)
716 QemuOpt *opt;
718 if (opts == NULL) {
719 return;
722 for (;;) {
723 opt = QTAILQ_FIRST(&opts->head);
724 if (opt == NULL)
725 break;
726 qemu_opt_del(opt);
728 QTAILQ_REMOVE(&opts->list->head, opts, next);
729 g_free(opts->id);
730 g_free(opts);
733 void qemu_opts_print(QemuOpts *opts, const char *sep)
735 QemuOpt *opt;
736 QemuOptDesc *desc = opts->list->desc;
738 if (desc[0].name == NULL) {
739 QTAILQ_FOREACH(opt, &opts->head, next) {
740 printf("%s%s=\"%s\"", sep, opt->name, opt->str);
742 return;
744 for (; desc && desc->name; desc++) {
745 const char *value;
746 QemuOpt *opt = qemu_opt_find(opts, desc->name);
748 value = opt ? opt->str : desc->def_value_str;
749 if (!value) {
750 continue;
752 if (desc->type == QEMU_OPT_STRING) {
753 printf("%s%s='%s'", sep, desc->name, value);
754 } else if ((desc->type == QEMU_OPT_SIZE ||
755 desc->type == QEMU_OPT_NUMBER) && opt) {
756 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
757 } else {
758 printf("%s%s=%s", sep, desc->name, value);
763 static void opts_do_parse(QemuOpts *opts, const char *params,
764 const char *firstname, bool prepend, Error **errp)
766 char option[128], value[1024];
767 const char *p,*pe,*pc;
768 Error *local_err = NULL;
770 for (p = params; *p != '\0'; p++) {
771 pe = strchr(p, '=');
772 pc = strchr(p, ',');
773 if (!pe || (pc && pc < pe)) {
774 /* found "foo,more" */
775 if (p == params && firstname) {
776 /* implicitly named first option */
777 pstrcpy(option, sizeof(option), firstname);
778 p = get_opt_value(value, sizeof(value), p);
779 } else {
780 /* option without value, probably a flag */
781 p = get_opt_name(option, sizeof(option), p, ',');
782 if (strncmp(option, "no", 2) == 0) {
783 memmove(option, option+2, strlen(option+2)+1);
784 pstrcpy(value, sizeof(value), "off");
785 } else {
786 pstrcpy(value, sizeof(value), "on");
789 } else {
790 /* found "foo=bar,more" */
791 p = get_opt_name(option, sizeof(option), p, '=');
792 if (*p != '=') {
793 break;
795 p++;
796 p = get_opt_value(value, sizeof(value), p);
798 if (strcmp(option, "id") != 0) {
799 /* store and parse */
800 opt_set(opts, option, value, prepend, &local_err);
801 if (local_err) {
802 error_propagate(errp, local_err);
803 return;
806 if (*p != ',') {
807 break;
813 * Store options parsed from @params into @opts.
814 * If @firstname is non-null, the first key=value in @params may omit
815 * key=, and is treated as if key was @firstname.
816 * On error, store an error object through @errp if non-null.
818 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
819 const char *firstname, Error **errp)
821 opts_do_parse(opts, params, firstname, false, errp);
824 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
825 bool permit_abbrev, bool defaults, Error **errp)
827 const char *firstname;
828 char value[1024], *id = NULL;
829 const char *p;
830 QemuOpts *opts;
831 Error *local_err = NULL;
833 assert(!permit_abbrev || list->implied_opt_name);
834 firstname = permit_abbrev ? list->implied_opt_name : NULL;
836 if (strncmp(params, "id=", 3) == 0) {
837 get_opt_value(value, sizeof(value), params+3);
838 id = value;
839 } else if ((p = strstr(params, ",id=")) != NULL) {
840 get_opt_value(value, sizeof(value), p+4);
841 id = value;
845 * This code doesn't work for defaults && !list->merge_lists: when
846 * params has no id=, and list has an element with !opts->id, it
847 * appends a new element instead of returning the existing opts.
848 * However, we got no use for this case. Guard against possible
849 * (if unlikely) future misuse:
851 assert(!defaults || list->merge_lists);
852 opts = qemu_opts_create(list, id, !defaults, &local_err);
853 if (opts == NULL) {
854 error_propagate(errp, local_err);
855 return NULL;
858 opts_do_parse(opts, params, firstname, defaults, &local_err);
859 if (local_err) {
860 error_propagate(errp, local_err);
861 qemu_opts_del(opts);
862 return NULL;
865 return opts;
869 * Create a QemuOpts in @list and with options parsed from @params.
870 * If @permit_abbrev, the first key=value in @params may omit key=,
871 * and is treated as if key was @list->implied_opt_name.
872 * On error, store an error object through @errp if non-null.
873 * Return the new QemuOpts on success, null pointer on error.
875 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
876 bool permit_abbrev, Error **errp)
878 return opts_parse(list, params, permit_abbrev, false, errp);
882 * Create a QemuOpts in @list and with options parsed from @params.
883 * If @permit_abbrev, the first key=value in @params may omit key=,
884 * and is treated as if key was @list->implied_opt_name.
885 * Report errors with error_report_err(). This is inappropriate in
886 * QMP context. Do not use this function there!
887 * Return the new QemuOpts on success, null pointer on error.
889 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
890 bool permit_abbrev)
892 Error *err = NULL;
893 QemuOpts *opts;
895 opts = opts_parse(list, params, permit_abbrev, false, &err);
896 if (err) {
897 error_report_err(err);
899 return opts;
902 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
903 int permit_abbrev)
905 QemuOpts *opts;
907 opts = opts_parse(list, params, permit_abbrev, true, NULL);
908 assert(opts);
911 typedef struct OptsFromQDictState {
912 QemuOpts *opts;
913 Error **errp;
914 } OptsFromQDictState;
916 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
918 OptsFromQDictState *state = opaque;
919 char buf[32];
920 const char *value;
921 int n;
923 if (!strcmp(key, "id") || *state->errp) {
924 return;
927 switch (qobject_type(obj)) {
928 case QTYPE_QSTRING:
929 value = qstring_get_str(qobject_to_qstring(obj));
930 break;
931 case QTYPE_QINT:
932 n = snprintf(buf, sizeof(buf), "%" PRId64,
933 qint_get_int(qobject_to_qint(obj)));
934 assert(n < sizeof(buf));
935 value = buf;
936 break;
937 case QTYPE_QFLOAT:
938 n = snprintf(buf, sizeof(buf), "%.17g",
939 qfloat_get_double(qobject_to_qfloat(obj)));
940 assert(n < sizeof(buf));
941 value = buf;
942 break;
943 case QTYPE_QBOOL:
944 pstrcpy(buf, sizeof(buf),
945 qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off");
946 value = buf;
947 break;
948 default:
949 return;
952 qemu_opt_set(state->opts, key, value, state->errp);
956 * Create QemuOpts from a QDict.
957 * Use value of key "id" as ID if it exists and is a QString.
958 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
959 * other types are silently ignored.
961 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
962 Error **errp)
964 OptsFromQDictState state;
965 Error *local_err = NULL;
966 QemuOpts *opts;
968 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
969 &local_err);
970 if (local_err) {
971 error_propagate(errp, local_err);
972 return NULL;
975 assert(opts != NULL);
977 state.errp = &local_err;
978 state.opts = opts;
979 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
980 if (local_err) {
981 error_propagate(errp, local_err);
982 qemu_opts_del(opts);
983 return NULL;
986 return opts;
990 * Adds all QDict entries to the QemuOpts that can be added and removes them
991 * from the QDict. When this function returns, the QDict contains only those
992 * entries that couldn't be added to the QemuOpts.
994 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
996 const QDictEntry *entry, *next;
998 entry = qdict_first(qdict);
1000 while (entry != NULL) {
1001 Error *local_err = NULL;
1002 OptsFromQDictState state = {
1003 .errp = &local_err,
1004 .opts = opts,
1007 next = qdict_next(qdict, entry);
1009 if (find_desc_by_name(opts->list->desc, entry->key)) {
1010 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
1011 if (local_err) {
1012 error_propagate(errp, local_err);
1013 return;
1014 } else {
1015 qdict_del(qdict, entry->key);
1019 entry = next;
1024 * Convert from QemuOpts to QDict.
1025 * The QDict values are of type QString.
1026 * TODO We'll want to use types appropriate for opt->desc->type, but
1027 * this is enough for now.
1029 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1031 QemuOpt *opt;
1032 QObject *val;
1034 if (!qdict) {
1035 qdict = qdict_new();
1037 if (opts->id) {
1038 qdict_put(qdict, "id", qstring_from_str(opts->id));
1040 QTAILQ_FOREACH(opt, &opts->head, next) {
1041 val = QOBJECT(qstring_from_str(opt->str));
1042 qdict_put_obj(qdict, opt->name, val);
1044 return qdict;
1047 /* Validate parsed opts against descriptions where no
1048 * descriptions were provided in the QemuOptsList.
1050 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1052 QemuOpt *opt;
1053 Error *local_err = NULL;
1055 assert(opts_accepts_any(opts));
1057 QTAILQ_FOREACH(opt, &opts->head, next) {
1058 opt->desc = find_desc_by_name(desc, opt->name);
1059 if (!opt->desc) {
1060 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1061 return;
1064 qemu_opt_parse(opt, &local_err);
1065 if (local_err) {
1066 error_propagate(errp, local_err);
1067 return;
1073 * For each member of @list, call @func(@opaque, member, @errp).
1074 * Call it with the current location temporarily set to the member's.
1075 * @func() may store an Error through @errp, but must return non-zero then.
1076 * When @func() returns non-zero, break the loop and return that value.
1077 * Return zero when the loop completes.
1079 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1080 void *opaque, Error **errp)
1082 Location loc;
1083 QemuOpts *opts;
1084 int rc;
1086 loc_push_none(&loc);
1087 QTAILQ_FOREACH(opts, &list->head, next) {
1088 loc_restore(&opts->loc);
1089 rc = func(opaque, opts, errp);
1090 if (rc) {
1091 return rc;
1093 assert(!errp || !*errp);
1095 loc_pop(&loc);
1096 return 0;
1099 static size_t count_opts_list(QemuOptsList *list)
1101 QemuOptDesc *desc = NULL;
1102 size_t num_opts = 0;
1104 if (!list) {
1105 return 0;
1108 desc = list->desc;
1109 while (desc && desc->name) {
1110 num_opts++;
1111 desc++;
1114 return num_opts;
1117 void qemu_opts_free(QemuOptsList *list)
1119 g_free(list);
1122 /* Realloc dst option list and append options from an option list (list)
1123 * to it. dst could be NULL or a malloced list.
1124 * The lifetime of dst must be shorter than the input list because the
1125 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1127 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1128 QemuOptsList *list)
1130 size_t num_opts, num_dst_opts;
1131 QemuOptDesc *desc;
1132 bool need_init = false;
1133 bool need_head_update;
1135 if (!list) {
1136 return dst;
1139 /* If dst is NULL, after realloc, some area of dst should be initialized
1140 * before adding options to it.
1142 if (!dst) {
1143 need_init = true;
1144 need_head_update = true;
1145 } else {
1146 /* Moreover, even if dst is not NULL, the realloc may move it to a
1147 * different address in which case we may get a stale tail pointer
1148 * in dst->head. */
1149 need_head_update = QTAILQ_EMPTY(&dst->head);
1152 num_opts = count_opts_list(dst);
1153 num_dst_opts = num_opts;
1154 num_opts += count_opts_list(list);
1155 dst = g_realloc(dst, sizeof(QemuOptsList) +
1156 (num_opts + 1) * sizeof(QemuOptDesc));
1157 if (need_init) {
1158 dst->name = NULL;
1159 dst->implied_opt_name = NULL;
1160 dst->merge_lists = false;
1162 if (need_head_update) {
1163 QTAILQ_INIT(&dst->head);
1165 dst->desc[num_dst_opts].name = NULL;
1167 /* append list->desc to dst->desc */
1168 if (list) {
1169 desc = list->desc;
1170 while (desc && desc->name) {
1171 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1172 dst->desc[num_dst_opts++] = *desc;
1173 dst->desc[num_dst_opts].name = NULL;
1175 desc++;
1179 return dst;