qapi: Fix file name in error messages for included files
[qemu.git] / util / qemu-option.c
blob840f5f7a5b92f465684b5796a6e346f9f10634ab
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_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
137 } else {
138 *ret = 1;
142 static void parse_option_number(const char *name, const char *value,
143 uint64_t *ret, Error **errp)
145 char *postfix;
146 uint64_t number;
148 if (value != NULL) {
149 number = strtoull(value, &postfix, 0);
150 if (*postfix != '\0') {
151 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
152 return;
154 *ret = number;
155 } else {
156 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
160 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
161 const char *name)
163 int i;
165 for (i = 0; desc[i].name != NULL; i++) {
166 if (strcmp(desc[i].name, name) == 0) {
167 return &desc[i];
171 return NULL;
174 void parse_option_size(const char *name, const char *value,
175 uint64_t *ret, Error **errp)
177 char *postfix;
178 double sizef;
180 if (value != NULL) {
181 sizef = strtod(value, &postfix);
182 switch (*postfix) {
183 case 'T':
184 sizef *= 1024;
185 /* fall through */
186 case 'G':
187 sizef *= 1024;
188 /* fall through */
189 case 'M':
190 sizef *= 1024;
191 /* fall through */
192 case 'K':
193 case 'k':
194 sizef *= 1024;
195 /* fall through */
196 case 'b':
197 case '\0':
198 *ret = (uint64_t) sizef;
199 break;
200 default:
201 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
202 #if 0 /* conversion from qerror_report() to error_set() broke this: */
203 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
204 "kilobytes, megabytes, gigabytes and terabytes.\n");
205 #endif
206 return;
208 } else {
209 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
213 bool has_help_option(const char *param)
215 size_t buflen = strlen(param) + 1;
216 char *buf = g_malloc(buflen);
217 const char *p = param;
218 bool result = false;
220 while (*p) {
221 p = get_opt_value(buf, buflen, p);
222 if (*p) {
223 p++;
226 if (is_help_option(buf)) {
227 result = true;
228 goto out;
232 out:
233 g_free(buf);
234 return result;
237 bool is_valid_option_list(const char *param)
239 size_t buflen = strlen(param) + 1;
240 char *buf = g_malloc(buflen);
241 const char *p = param;
242 bool result = true;
244 while (*p) {
245 p = get_opt_value(buf, buflen, p);
246 if (*p && !*++p) {
247 result = false;
248 goto out;
251 if (!*buf || *buf == ',') {
252 result = false;
253 goto out;
257 out:
258 g_free(buf);
259 return result;
262 void qemu_opts_print_help(QemuOptsList *list)
264 QemuOptDesc *desc;
266 assert(list);
267 desc = list->desc;
268 printf("Supported options:\n");
269 while (desc && desc->name) {
270 printf("%-16s %s\n", desc->name,
271 desc->help ? desc->help : "No description available");
272 desc++;
275 /* ------------------------------------------------------------------ */
277 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
279 QemuOpt *opt;
281 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
282 if (strcmp(opt->name, name) != 0)
283 continue;
284 return opt;
286 return NULL;
289 static void qemu_opt_del(QemuOpt *opt)
291 QTAILQ_REMOVE(&opt->opts->head, opt, next);
292 g_free(opt->name);
293 g_free(opt->str);
294 g_free(opt);
297 /* qemu_opt_set allows many settings for the same option.
298 * This function deletes all settings for an option.
300 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
302 QemuOpt *opt, *next_opt;
304 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
305 if (!strcmp(opt->name, name)) {
306 qemu_opt_del(opt);
311 const char *qemu_opt_get(QemuOpts *opts, const char *name)
313 QemuOpt *opt;
315 if (opts == NULL) {
316 return NULL;
319 opt = qemu_opt_find(opts, name);
320 if (!opt) {
321 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
322 if (desc && desc->def_value_str) {
323 return desc->def_value_str;
326 return opt ? opt->str : NULL;
329 /* Get a known option (or its default) and remove it from the list
330 * all in one action. Return a malloced string of the option value.
331 * Result must be freed by caller with g_free().
333 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
335 QemuOpt *opt;
336 const QemuOptDesc *desc;
337 char *str = NULL;
339 if (opts == NULL) {
340 return NULL;
343 opt = qemu_opt_find(opts, name);
344 if (!opt) {
345 desc = find_desc_by_name(opts->list->desc, name);
346 if (desc && desc->def_value_str) {
347 str = g_strdup(desc->def_value_str);
349 return str;
351 str = opt->str;
352 opt->str = NULL;
353 qemu_opt_del_all(opts, name);
354 return str;
357 bool qemu_opt_has_help_opt(QemuOpts *opts)
359 QemuOpt *opt;
361 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
362 if (is_help_option(opt->name)) {
363 return true;
366 return false;
369 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
370 bool defval, bool del)
372 QemuOpt *opt;
373 bool ret = defval;
375 if (opts == NULL) {
376 return ret;
379 opt = qemu_opt_find(opts, name);
380 if (opt == NULL) {
381 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
382 if (desc && desc->def_value_str) {
383 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
385 return ret;
387 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
388 ret = opt->value.boolean;
389 if (del) {
390 qemu_opt_del_all(opts, name);
392 return ret;
395 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
397 return qemu_opt_get_bool_helper(opts, name, defval, false);
400 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
402 return qemu_opt_get_bool_helper(opts, name, defval, true);
405 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
406 uint64_t defval, bool del)
408 QemuOpt *opt;
409 uint64_t ret = defval;
411 if (opts == NULL) {
412 return ret;
415 opt = qemu_opt_find(opts, name);
416 if (opt == NULL) {
417 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
418 if (desc && desc->def_value_str) {
419 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
421 return ret;
423 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
424 ret = opt->value.uint;
425 if (del) {
426 qemu_opt_del_all(opts, name);
428 return ret;
431 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
433 return qemu_opt_get_number_helper(opts, name, defval, false);
436 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
437 uint64_t defval)
439 return qemu_opt_get_number_helper(opts, name, defval, true);
442 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
443 uint64_t defval, bool del)
445 QemuOpt *opt;
446 uint64_t ret = defval;
448 if (opts == NULL) {
449 return ret;
452 opt = qemu_opt_find(opts, name);
453 if (opt == NULL) {
454 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
455 if (desc && desc->def_value_str) {
456 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
458 return ret;
460 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
461 ret = opt->value.uint;
462 if (del) {
463 qemu_opt_del_all(opts, name);
465 return ret;
468 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
470 return qemu_opt_get_size_helper(opts, name, defval, false);
473 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
474 uint64_t defval)
476 return qemu_opt_get_size_helper(opts, name, defval, true);
479 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
481 if (opt->desc == NULL)
482 return;
484 switch (opt->desc->type) {
485 case QEMU_OPT_STRING:
486 /* nothing */
487 return;
488 case QEMU_OPT_BOOL:
489 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
490 break;
491 case QEMU_OPT_NUMBER:
492 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
493 break;
494 case QEMU_OPT_SIZE:
495 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
496 break;
497 default:
498 abort();
502 static bool opts_accepts_any(const QemuOpts *opts)
504 return opts->list->desc[0].name == NULL;
507 int qemu_opt_unset(QemuOpts *opts, const char *name)
509 QemuOpt *opt = qemu_opt_find(opts, name);
511 assert(opts_accepts_any(opts));
513 if (opt == NULL) {
514 return -1;
515 } else {
516 qemu_opt_del(opt);
517 return 0;
521 static void opt_set(QemuOpts *opts, const char *name, const char *value,
522 bool prepend, Error **errp)
524 QemuOpt *opt;
525 const QemuOptDesc *desc;
526 Error *local_err = NULL;
528 desc = find_desc_by_name(opts->list->desc, name);
529 if (!desc && !opts_accepts_any(opts)) {
530 error_set(errp, QERR_INVALID_PARAMETER, name);
531 return;
534 opt = g_malloc0(sizeof(*opt));
535 opt->name = g_strdup(name);
536 opt->opts = opts;
537 if (prepend) {
538 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
539 } else {
540 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
542 opt->desc = desc;
543 opt->str = g_strdup(value);
544 qemu_opt_parse(opt, &local_err);
545 if (local_err) {
546 error_propagate(errp, local_err);
547 qemu_opt_del(opt);
551 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
552 Error **errp)
554 opt_set(opts, name, value, false, errp);
557 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
558 Error **errp)
560 QemuOpt *opt;
561 const QemuOptDesc *desc = opts->list->desc;
563 opt = g_malloc0(sizeof(*opt));
564 opt->desc = find_desc_by_name(desc, name);
565 if (!opt->desc && !opts_accepts_any(opts)) {
566 error_set(errp, QERR_INVALID_PARAMETER, name);
567 g_free(opt);
568 return;
571 opt->name = g_strdup(name);
572 opt->opts = opts;
573 opt->value.boolean = !!val;
574 opt->str = g_strdup(val ? "on" : "off");
575 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
578 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
579 Error **errp)
581 QemuOpt *opt;
582 const QemuOptDesc *desc = opts->list->desc;
584 opt = g_malloc0(sizeof(*opt));
585 opt->desc = find_desc_by_name(desc, name);
586 if (!opt->desc && !opts_accepts_any(opts)) {
587 error_set(errp, QERR_INVALID_PARAMETER, name);
588 g_free(opt);
589 return;
592 opt->name = g_strdup(name);
593 opt->opts = opts;
594 opt->value.uint = val;
595 opt->str = g_strdup_printf("%" PRId64, val);
596 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
600 * For each member of @opts, call @func(@opaque, name, value, @errp).
601 * @func() may store an Error through @errp, but must return non-zero then.
602 * When @func() returns non-zero, break the loop and return that value.
603 * Return zero when the loop completes.
605 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
606 Error **errp)
608 QemuOpt *opt;
609 int rc;
611 QTAILQ_FOREACH(opt, &opts->head, next) {
612 rc = func(opaque, opt->name, opt->str, errp);
613 if (rc) {
614 return rc;
616 assert(!errp || !*errp);
618 return 0;
621 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
623 QemuOpts *opts;
625 QTAILQ_FOREACH(opts, &list->head, next) {
626 if (!opts->id && !id) {
627 return opts;
629 if (opts->id && id && !strcmp(opts->id, id)) {
630 return opts;
633 return NULL;
636 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
637 int fail_if_exists, Error **errp)
639 QemuOpts *opts = NULL;
641 if (id) {
642 if (!id_wellformed(id)) {
643 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
644 #if 0 /* conversion from qerror_report() to error_set() broke this: */
645 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
646 #endif
647 return NULL;
649 opts = qemu_opts_find(list, id);
650 if (opts != NULL) {
651 if (fail_if_exists && !list->merge_lists) {
652 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
653 return NULL;
654 } else {
655 return opts;
658 } else if (list->merge_lists) {
659 opts = qemu_opts_find(list, NULL);
660 if (opts) {
661 return opts;
664 opts = g_malloc0(sizeof(*opts));
665 opts->id = g_strdup(id);
666 opts->list = list;
667 loc_save(&opts->loc);
668 QTAILQ_INIT(&opts->head);
669 QTAILQ_INSERT_TAIL(&list->head, opts, next);
670 return opts;
673 void qemu_opts_reset(QemuOptsList *list)
675 QemuOpts *opts, *next_opts;
677 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
678 qemu_opts_del(opts);
682 void qemu_opts_loc_restore(QemuOpts *opts)
684 loc_restore(&opts->loc);
687 void qemu_opts_set(QemuOptsList *list, const char *id,
688 const char *name, const char *value, Error **errp)
690 QemuOpts *opts;
691 Error *local_err = NULL;
693 opts = qemu_opts_create(list, id, 1, &local_err);
694 if (local_err) {
695 error_propagate(errp, local_err);
696 return;
698 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 void qemu_opts_print(QemuOpts *opts, const char *sep)
733 QemuOpt *opt;
734 QemuOptDesc *desc = opts->list->desc;
736 if (desc[0].name == NULL) {
737 QTAILQ_FOREACH(opt, &opts->head, next) {
738 printf("%s%s=\"%s\"", sep, opt->name, opt->str);
740 return;
742 for (; desc && desc->name; desc++) {
743 const char *value;
744 QemuOpt *opt = qemu_opt_find(opts, desc->name);
746 value = opt ? opt->str : desc->def_value_str;
747 if (!value) {
748 continue;
750 if (desc->type == QEMU_OPT_STRING) {
751 printf("%s%s='%s'", sep, desc->name, value);
752 } else if ((desc->type == QEMU_OPT_SIZE ||
753 desc->type == QEMU_OPT_NUMBER) && opt) {
754 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
755 } else {
756 printf("%s%s=%s", sep, desc->name, value);
761 static void opts_do_parse(QemuOpts *opts, const char *params,
762 const char *firstname, bool prepend, Error **errp)
764 char option[128], value[1024];
765 const char *p,*pe,*pc;
766 Error *local_err = NULL;
768 for (p = params; *p != '\0'; p++) {
769 pe = strchr(p, '=');
770 pc = strchr(p, ',');
771 if (!pe || (pc && pc < pe)) {
772 /* found "foo,more" */
773 if (p == params && firstname) {
774 /* implicitly named first option */
775 pstrcpy(option, sizeof(option), firstname);
776 p = get_opt_value(value, sizeof(value), p);
777 } else {
778 /* option without value, probably a flag */
779 p = get_opt_name(option, sizeof(option), p, ',');
780 if (strncmp(option, "no", 2) == 0) {
781 memmove(option, option+2, strlen(option+2)+1);
782 pstrcpy(value, sizeof(value), "off");
783 } else {
784 pstrcpy(value, sizeof(value), "on");
787 } else {
788 /* found "foo=bar,more" */
789 p = get_opt_name(option, sizeof(option), p, '=');
790 if (*p != '=') {
791 break;
793 p++;
794 p = get_opt_value(value, sizeof(value), p);
796 if (strcmp(option, "id") != 0) {
797 /* store and parse */
798 opt_set(opts, option, value, prepend, &local_err);
799 if (local_err) {
800 error_propagate(errp, local_err);
801 return;
804 if (*p != ',') {
805 break;
811 * Store options parsed from @params into @opts.
812 * If @firstname is non-null, the first key=value in @params may omit
813 * key=, and is treated as if key was @firstname.
814 * On error, store an error object through @errp if non-null.
816 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
817 const char *firstname, Error **errp)
819 opts_do_parse(opts, params, firstname, false, errp);
822 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
823 int permit_abbrev, bool defaults, Error **errp)
825 const char *firstname;
826 char value[1024], *id = NULL;
827 const char *p;
828 QemuOpts *opts;
829 Error *local_err = NULL;
831 assert(!permit_abbrev || list->implied_opt_name);
832 firstname = permit_abbrev ? list->implied_opt_name : NULL;
834 if (strncmp(params, "id=", 3) == 0) {
835 get_opt_value(value, sizeof(value), params+3);
836 id = value;
837 } else if ((p = strstr(params, ",id=")) != NULL) {
838 get_opt_value(value, sizeof(value), p+4);
839 id = value;
843 * This code doesn't work for defaults && !list->merge_lists: when
844 * params has no id=, and list has an element with !opts->id, it
845 * appends a new element instead of returning the existing opts.
846 * However, we got no use for this case. Guard against possible
847 * (if unlikely) future misuse:
849 assert(!defaults || list->merge_lists);
850 opts = qemu_opts_create(list, id, !defaults, &local_err);
851 if (opts == NULL) {
852 error_propagate(errp, local_err);
853 return NULL;
856 opts_do_parse(opts, params, firstname, defaults, &local_err);
857 if (local_err) {
858 error_propagate(errp, local_err);
859 qemu_opts_del(opts);
860 return NULL;
863 return opts;
867 * Create a QemuOpts in @list and with options parsed from @params.
868 * If @permit_abbrev, the first key=value in @params may omit key=,
869 * and is treated as if key was @list->implied_opt_name.
870 * Report errors with qerror_report_err().
871 * Return the new QemuOpts on success, null pointer on error.
873 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
874 int permit_abbrev)
876 Error *err = NULL;
877 QemuOpts *opts;
879 opts = opts_parse(list, params, permit_abbrev, false, &err);
880 if (!opts) {
881 qerror_report_err(err);
882 error_free(err);
884 return opts;
887 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
888 int permit_abbrev)
890 QemuOpts *opts;
892 opts = opts_parse(list, params, permit_abbrev, true, NULL);
893 assert(opts);
896 typedef struct OptsFromQDictState {
897 QemuOpts *opts;
898 Error **errp;
899 } OptsFromQDictState;
901 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
903 OptsFromQDictState *state = opaque;
904 char buf[32];
905 const char *value;
906 int n;
908 if (!strcmp(key, "id") || *state->errp) {
909 return;
912 switch (qobject_type(obj)) {
913 case QTYPE_QSTRING:
914 value = qstring_get_str(qobject_to_qstring(obj));
915 break;
916 case QTYPE_QINT:
917 n = snprintf(buf, sizeof(buf), "%" PRId64,
918 qint_get_int(qobject_to_qint(obj)));
919 assert(n < sizeof(buf));
920 value = buf;
921 break;
922 case QTYPE_QFLOAT:
923 n = snprintf(buf, sizeof(buf), "%.17g",
924 qfloat_get_double(qobject_to_qfloat(obj)));
925 assert(n < sizeof(buf));
926 value = buf;
927 break;
928 case QTYPE_QBOOL:
929 pstrcpy(buf, sizeof(buf),
930 qbool_get_int(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);
941 * Create QemuOpts from a QDict.
942 * Use value of key "id" as ID if it exists and is a QString.
943 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
944 * other types are silently ignored.
946 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
947 Error **errp)
949 OptsFromQDictState state;
950 Error *local_err = NULL;
951 QemuOpts *opts;
953 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
954 &local_err);
955 if (local_err) {
956 error_propagate(errp, local_err);
957 return NULL;
960 assert(opts != NULL);
962 state.errp = &local_err;
963 state.opts = opts;
964 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
965 if (local_err) {
966 error_propagate(errp, local_err);
967 qemu_opts_del(opts);
968 return NULL;
971 return opts;
975 * Adds all QDict entries to the QemuOpts that can be added and removes them
976 * from the QDict. When this function returns, the QDict contains only those
977 * entries that couldn't be added to the QemuOpts.
979 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
981 const QDictEntry *entry, *next;
983 entry = qdict_first(qdict);
985 while (entry != NULL) {
986 Error *local_err = NULL;
987 OptsFromQDictState state = {
988 .errp = &local_err,
989 .opts = opts,
992 next = qdict_next(qdict, entry);
994 if (find_desc_by_name(opts->list->desc, entry->key)) {
995 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
996 if (local_err) {
997 error_propagate(errp, local_err);
998 return;
999 } else {
1000 qdict_del(qdict, entry->key);
1004 entry = next;
1009 * Convert from QemuOpts to QDict.
1010 * The QDict values are of type QString.
1011 * TODO We'll want to use types appropriate for opt->desc->type, but
1012 * this is enough for now.
1014 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1016 QemuOpt *opt;
1017 QObject *val;
1019 if (!qdict) {
1020 qdict = qdict_new();
1022 if (opts->id) {
1023 qdict_put(qdict, "id", qstring_from_str(opts->id));
1025 QTAILQ_FOREACH(opt, &opts->head, next) {
1026 val = QOBJECT(qstring_from_str(opt->str));
1027 qdict_put_obj(qdict, opt->name, val);
1029 return qdict;
1032 /* Validate parsed opts against descriptions where no
1033 * descriptions were provided in the QemuOptsList.
1035 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1037 QemuOpt *opt;
1038 Error *local_err = NULL;
1040 assert(opts_accepts_any(opts));
1042 QTAILQ_FOREACH(opt, &opts->head, next) {
1043 opt->desc = find_desc_by_name(desc, opt->name);
1044 if (!opt->desc) {
1045 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1046 return;
1049 qemu_opt_parse(opt, &local_err);
1050 if (local_err) {
1051 error_propagate(errp, local_err);
1052 return;
1058 * For each member of @list, call @func(@opaque, member, @errp).
1059 * Call it with the current location temporarily set to the member's.
1060 * @func() may store an Error through @errp, but must return non-zero then.
1061 * When @func() returns non-zero, break the loop and return that value.
1062 * Return zero when the loop completes.
1064 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1065 void *opaque, Error **errp)
1067 Location loc;
1068 QemuOpts *opts;
1069 int rc;
1071 loc_push_none(&loc);
1072 QTAILQ_FOREACH(opts, &list->head, next) {
1073 loc_restore(&opts->loc);
1074 rc = func(opaque, opts, errp);
1075 if (rc) {
1076 return rc;
1078 assert(!errp || !*errp);
1080 loc_pop(&loc);
1081 return 0;
1084 static size_t count_opts_list(QemuOptsList *list)
1086 QemuOptDesc *desc = NULL;
1087 size_t num_opts = 0;
1089 if (!list) {
1090 return 0;
1093 desc = list->desc;
1094 while (desc && desc->name) {
1095 num_opts++;
1096 desc++;
1099 return num_opts;
1102 void qemu_opts_free(QemuOptsList *list)
1104 g_free(list);
1107 /* Realloc dst option list and append options from an option list (list)
1108 * to it. dst could be NULL or a malloced list.
1109 * The lifetime of dst must be shorter than the input list because the
1110 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1112 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1113 QemuOptsList *list)
1115 size_t num_opts, num_dst_opts;
1116 QemuOptDesc *desc;
1117 bool need_init = false;
1118 bool need_head_update;
1120 if (!list) {
1121 return dst;
1124 /* If dst is NULL, after realloc, some area of dst should be initialized
1125 * before adding options to it.
1127 if (!dst) {
1128 need_init = true;
1129 need_head_update = true;
1130 } else {
1131 /* Moreover, even if dst is not NULL, the realloc may move it to a
1132 * different address in which case we may get a stale tail pointer
1133 * in dst->head. */
1134 need_head_update = QTAILQ_EMPTY(&dst->head);
1137 num_opts = count_opts_list(dst);
1138 num_dst_opts = num_opts;
1139 num_opts += count_opts_list(list);
1140 dst = g_realloc(dst, sizeof(QemuOptsList) +
1141 (num_opts + 1) * sizeof(QemuOptDesc));
1142 if (need_init) {
1143 dst->name = NULL;
1144 dst->implied_opt_name = NULL;
1145 dst->merge_lists = false;
1147 if (need_head_update) {
1148 QTAILQ_INIT(&dst->head);
1150 dst->desc[num_dst_opts].name = NULL;
1152 /* append list->desc to dst->desc */
1153 if (list) {
1154 desc = list->desc;
1155 while (desc && desc->name) {
1156 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1157 dst->desc[num_dst_opts++] = *desc;
1158 dst->desc[num_dst_opts].name = NULL;
1160 desc++;
1164 return dst;