Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / util / qemu-option.c
blobe266abf95f0dde876cc13d2e1a1cda9c45d5c30d
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/types.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qemu/option_int.h"
34 #include "qemu/cutils.h"
35 #include "qemu/id.h"
36 #include "qemu/help_option.h"
39 * Extracts the name of an option from the parameter string (p points at the
40 * first byte of the option name)
42 * The option name is delimited by delim (usually , or =) or the string end
43 * and is copied into buf. If the option name is longer than buf_size, it is
44 * truncated. buf is always zero terminated.
46 * The return value is the position of the delimiter/zero byte after the option
47 * name in p.
49 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
51 char *q;
53 q = buf;
54 while (*p != '\0' && *p != delim) {
55 if (q && (q - buf) < buf_size - 1)
56 *q++ = *p;
57 p++;
59 if (q)
60 *q = '\0';
62 return p;
66 * Extracts the value of an option from the parameter string p (p points at the
67 * first byte of the option value)
69 * This function is comparable to get_opt_name with the difference that the
70 * delimiter is fixed to be comma which starts a new option. To specify an
71 * option value that contains commas, double each comma.
73 const char *get_opt_value(char *buf, int buf_size, const char *p)
75 char *q;
77 q = buf;
78 while (*p != '\0') {
79 if (*p == ',') {
80 if (*(p + 1) != ',')
81 break;
82 p++;
84 if (q && (q - buf) < buf_size - 1)
85 *q++ = *p;
86 p++;
88 if (q)
89 *q = '\0';
91 return p;
94 int get_next_param_value(char *buf, int buf_size,
95 const char *tag, const char **pstr)
97 const char *p;
98 char option[128];
100 p = *pstr;
101 for(;;) {
102 p = get_opt_name(option, sizeof(option), p, '=');
103 if (*p != '=')
104 break;
105 p++;
106 if (!strcmp(tag, option)) {
107 *pstr = get_opt_value(buf, buf_size, p);
108 if (**pstr == ',') {
109 (*pstr)++;
111 return strlen(buf);
112 } else {
113 p = get_opt_value(NULL, 0, p);
115 if (*p != ',')
116 break;
117 p++;
119 return 0;
122 int get_param_value(char *buf, int buf_size,
123 const char *tag, const char *str)
125 return get_next_param_value(buf, buf_size, tag, &str);
128 static void parse_option_bool(const char *name, const char *value, bool *ret,
129 Error **errp)
131 if (!strcmp(value, "on")) {
132 *ret = 1;
133 } else if (!strcmp(value, "off")) {
134 *ret = 0;
135 } else {
136 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
137 name, "'on' or 'off'");
141 static void parse_option_number(const char *name, const char *value,
142 uint64_t *ret, Error **errp)
144 uint64_t number;
145 int err;
147 err = qemu_strtou64(value, NULL, 0, &number);
148 if (err == -ERANGE) {
149 error_setg(errp, "Value '%s' is too large for parameter '%s'",
150 value, name);
151 return;
153 if (err) {
154 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
155 return;
157 *ret = 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 uint64_t size;
178 int err;
180 err = qemu_strtosz(value, NULL, &size);
181 if (err == -ERANGE) {
182 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
183 value, name);
184 return;
186 if (err) {
187 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
188 "a non-negative number below 2^64");
189 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
190 " kilo-, mega-, giga-, tera-, peta-\n"
191 "and exabytes, respectively.\n");
192 return;
194 *ret = size;
197 bool has_help_option(const char *param)
199 size_t buflen = strlen(param) + 1;
200 char *buf = g_malloc(buflen);
201 const char *p = param;
202 bool result = false;
204 while (*p) {
205 p = get_opt_value(buf, buflen, p);
206 if (*p) {
207 p++;
210 if (is_help_option(buf)) {
211 result = true;
212 goto out;
216 out:
217 g_free(buf);
218 return result;
221 bool is_valid_option_list(const char *param)
223 size_t buflen = strlen(param) + 1;
224 char *buf = g_malloc(buflen);
225 const char *p = param;
226 bool result = true;
228 while (*p) {
229 p = get_opt_value(buf, buflen, p);
230 if (*p && !*++p) {
231 result = false;
232 goto out;
235 if (!*buf || *buf == ',') {
236 result = false;
237 goto out;
241 out:
242 g_free(buf);
243 return result;
246 void qemu_opts_print_help(QemuOptsList *list)
248 QemuOptDesc *desc;
250 assert(list);
251 desc = list->desc;
252 printf("Supported options:\n");
253 while (desc && desc->name) {
254 printf("%-16s %s\n", desc->name,
255 desc->help ? desc->help : "No description available");
256 desc++;
259 /* ------------------------------------------------------------------ */
261 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
263 QemuOpt *opt;
265 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
266 if (strcmp(opt->name, name) != 0)
267 continue;
268 return opt;
270 return NULL;
273 static void qemu_opt_del(QemuOpt *opt)
275 QTAILQ_REMOVE(&opt->opts->head, opt, next);
276 g_free(opt->name);
277 g_free(opt->str);
278 g_free(opt);
281 /* qemu_opt_set allows many settings for the same option.
282 * This function deletes all settings for an option.
284 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
286 QemuOpt *opt, *next_opt;
288 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
289 if (!strcmp(opt->name, name)) {
290 qemu_opt_del(opt);
295 const char *qemu_opt_get(QemuOpts *opts, const char *name)
297 QemuOpt *opt;
299 if (opts == NULL) {
300 return NULL;
303 opt = qemu_opt_find(opts, name);
304 if (!opt) {
305 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
306 if (desc && desc->def_value_str) {
307 return desc->def_value_str;
310 return opt ? opt->str : NULL;
313 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
315 iter->opts = opts;
316 iter->opt = QTAILQ_FIRST(&opts->head);
317 iter->name = name;
320 const char *qemu_opt_iter_next(QemuOptsIter *iter)
322 QemuOpt *ret = iter->opt;
323 if (iter->name) {
324 while (ret && !g_str_equal(iter->name, ret->name)) {
325 ret = QTAILQ_NEXT(ret, next);
328 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
329 return ret ? ret->str : NULL;
332 /* Get a known option (or its default) and remove it from the list
333 * all in one action. Return a malloced string of the option value.
334 * Result must be freed by caller with g_free().
336 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
338 QemuOpt *opt;
339 const QemuOptDesc *desc;
340 char *str = NULL;
342 if (opts == NULL) {
343 return NULL;
346 opt = qemu_opt_find(opts, name);
347 if (!opt) {
348 desc = find_desc_by_name(opts->list->desc, name);
349 if (desc && desc->def_value_str) {
350 str = g_strdup(desc->def_value_str);
352 return str;
354 str = opt->str;
355 opt->str = NULL;
356 qemu_opt_del_all(opts, name);
357 return str;
360 bool qemu_opt_has_help_opt(QemuOpts *opts)
362 QemuOpt *opt;
364 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
365 if (is_help_option(opt->name)) {
366 return true;
369 return false;
372 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
373 bool defval, bool del)
375 QemuOpt *opt;
376 bool ret = defval;
378 if (opts == NULL) {
379 return ret;
382 opt = qemu_opt_find(opts, name);
383 if (opt == NULL) {
384 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
385 if (desc && desc->def_value_str) {
386 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
388 return ret;
390 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
391 ret = opt->value.boolean;
392 if (del) {
393 qemu_opt_del_all(opts, name);
395 return ret;
398 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
400 return qemu_opt_get_bool_helper(opts, name, defval, false);
403 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
405 return qemu_opt_get_bool_helper(opts, name, defval, true);
408 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
409 uint64_t defval, bool del)
411 QemuOpt *opt;
412 uint64_t ret = defval;
414 if (opts == NULL) {
415 return ret;
418 opt = qemu_opt_find(opts, name);
419 if (opt == NULL) {
420 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
421 if (desc && desc->def_value_str) {
422 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
424 return ret;
426 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
427 ret = opt->value.uint;
428 if (del) {
429 qemu_opt_del_all(opts, name);
431 return ret;
434 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
436 return qemu_opt_get_number_helper(opts, name, defval, false);
439 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
440 uint64_t defval)
442 return qemu_opt_get_number_helper(opts, name, defval, true);
445 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
446 uint64_t defval, bool del)
448 QemuOpt *opt;
449 uint64_t ret = defval;
451 if (opts == NULL) {
452 return ret;
455 opt = qemu_opt_find(opts, name);
456 if (opt == NULL) {
457 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
458 if (desc && desc->def_value_str) {
459 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
461 return ret;
463 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
464 ret = opt->value.uint;
465 if (del) {
466 qemu_opt_del_all(opts, name);
468 return ret;
471 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
473 return qemu_opt_get_size_helper(opts, name, defval, false);
476 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
477 uint64_t defval)
479 return qemu_opt_get_size_helper(opts, name, defval, true);
482 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
484 if (opt->desc == NULL)
485 return;
487 switch (opt->desc->type) {
488 case QEMU_OPT_STRING:
489 /* nothing */
490 return;
491 case QEMU_OPT_BOOL:
492 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
493 break;
494 case QEMU_OPT_NUMBER:
495 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
496 break;
497 case QEMU_OPT_SIZE:
498 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
499 break;
500 default:
501 abort();
505 static bool opts_accepts_any(const QemuOpts *opts)
507 return opts->list->desc[0].name == NULL;
510 int qemu_opt_unset(QemuOpts *opts, const char *name)
512 QemuOpt *opt = qemu_opt_find(opts, name);
514 assert(opts_accepts_any(opts));
516 if (opt == NULL) {
517 return -1;
518 } else {
519 qemu_opt_del(opt);
520 return 0;
524 static void opt_set(QemuOpts *opts, const char *name, const char *value,
525 bool prepend, Error **errp)
527 QemuOpt *opt;
528 const QemuOptDesc *desc;
529 Error *local_err = NULL;
531 desc = find_desc_by_name(opts->list->desc, name);
532 if (!desc && !opts_accepts_any(opts)) {
533 error_setg(errp, QERR_INVALID_PARAMETER, name);
534 return;
537 opt = g_malloc0(sizeof(*opt));
538 opt->name = g_strdup(name);
539 opt->opts = opts;
540 if (prepend) {
541 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
542 } else {
543 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
545 opt->desc = desc;
546 opt->str = g_strdup(value);
547 assert(opt->str);
548 qemu_opt_parse(opt, &local_err);
549 if (local_err) {
550 error_propagate(errp, local_err);
551 qemu_opt_del(opt);
555 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
556 Error **errp)
558 opt_set(opts, name, value, false, errp);
561 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
562 Error **errp)
564 QemuOpt *opt;
565 const QemuOptDesc *desc = opts->list->desc;
567 opt = g_malloc0(sizeof(*opt));
568 opt->desc = find_desc_by_name(desc, name);
569 if (!opt->desc && !opts_accepts_any(opts)) {
570 error_setg(errp, QERR_INVALID_PARAMETER, name);
571 g_free(opt);
572 return;
575 opt->name = g_strdup(name);
576 opt->opts = opts;
577 opt->value.boolean = !!val;
578 opt->str = g_strdup(val ? "on" : "off");
579 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
582 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
583 Error **errp)
585 QemuOpt *opt;
586 const QemuOptDesc *desc = opts->list->desc;
588 opt = g_malloc0(sizeof(*opt));
589 opt->desc = find_desc_by_name(desc, name);
590 if (!opt->desc && !opts_accepts_any(opts)) {
591 error_setg(errp, QERR_INVALID_PARAMETER, name);
592 g_free(opt);
593 return;
596 opt->name = g_strdup(name);
597 opt->opts = opts;
598 opt->value.uint = val;
599 opt->str = g_strdup_printf("%" PRId64, val);
600 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
604 * For each member of @opts, call @func(@opaque, name, value, @errp).
605 * @func() may store an Error through @errp, but must return non-zero then.
606 * When @func() returns non-zero, break the loop and return that value.
607 * Return zero when the loop completes.
609 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
610 Error **errp)
612 QemuOpt *opt;
613 int rc;
615 QTAILQ_FOREACH(opt, &opts->head, next) {
616 rc = func(opaque, opt->name, opt->str, errp);
617 if (rc) {
618 return rc;
620 assert(!errp || !*errp);
622 return 0;
625 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
627 QemuOpts *opts;
629 QTAILQ_FOREACH(opts, &list->head, next) {
630 if (!opts->id && !id) {
631 return opts;
633 if (opts->id && id && !strcmp(opts->id, id)) {
634 return opts;
637 return NULL;
640 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
641 int fail_if_exists, Error **errp)
643 QemuOpts *opts = NULL;
645 if (id) {
646 if (!id_wellformed(id)) {
647 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
648 "an identifier");
649 error_append_hint(errp, "Identifiers consist of letters, digits, "
650 "'-', '.', '_', starting with a letter.\n");
651 return NULL;
653 opts = qemu_opts_find(list, id);
654 if (opts != NULL) {
655 if (fail_if_exists && !list->merge_lists) {
656 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
657 return NULL;
658 } else {
659 return opts;
662 } else if (list->merge_lists) {
663 opts = qemu_opts_find(list, NULL);
664 if (opts) {
665 return opts;
668 opts = g_malloc0(sizeof(*opts));
669 opts->id = g_strdup(id);
670 opts->list = list;
671 loc_save(&opts->loc);
672 QTAILQ_INIT(&opts->head);
673 QTAILQ_INSERT_TAIL(&list->head, opts, next);
674 return opts;
677 void qemu_opts_reset(QemuOptsList *list)
679 QemuOpts *opts, *next_opts;
681 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
682 qemu_opts_del(opts);
686 void qemu_opts_loc_restore(QemuOpts *opts)
688 loc_restore(&opts->loc);
691 void qemu_opts_set(QemuOptsList *list, const char *id,
692 const char *name, const char *value, Error **errp)
694 QemuOpts *opts;
695 Error *local_err = NULL;
697 opts = qemu_opts_create(list, id, 1, &local_err);
698 if (local_err) {
699 error_propagate(errp, local_err);
700 return;
702 qemu_opt_set(opts, name, value, errp);
705 const char *qemu_opts_id(QemuOpts *opts)
707 return opts->id;
710 /* The id string will be g_free()d by qemu_opts_del */
711 void qemu_opts_set_id(QemuOpts *opts, char *id)
713 opts->id = id;
716 void qemu_opts_del(QemuOpts *opts)
718 QemuOpt *opt;
720 if (opts == NULL) {
721 return;
724 for (;;) {
725 opt = QTAILQ_FIRST(&opts->head);
726 if (opt == NULL)
727 break;
728 qemu_opt_del(opt);
730 QTAILQ_REMOVE(&opts->list->head, opts, next);
731 g_free(opts->id);
732 g_free(opts);
735 /* print value, escaping any commas in value */
736 static void escaped_print(const char *value)
738 const char *ptr;
740 for (ptr = value; *ptr; ++ptr) {
741 if (*ptr == ',') {
742 putchar(',');
744 putchar(*ptr);
748 void qemu_opts_print(QemuOpts *opts, const char *separator)
750 QemuOpt *opt;
751 QemuOptDesc *desc = opts->list->desc;
752 const char *sep = "";
754 if (opts->id) {
755 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
756 sep = separator;
759 if (desc[0].name == NULL) {
760 QTAILQ_FOREACH(opt, &opts->head, next) {
761 printf("%s%s=", sep, opt->name);
762 escaped_print(opt->str);
763 sep = separator;
765 return;
767 for (; desc && desc->name; desc++) {
768 const char *value;
769 QemuOpt *opt = qemu_opt_find(opts, desc->name);
771 value = opt ? opt->str : desc->def_value_str;
772 if (!value) {
773 continue;
775 if (desc->type == QEMU_OPT_STRING) {
776 printf("%s%s=", sep, desc->name);
777 escaped_print(value);
778 } else if ((desc->type == QEMU_OPT_SIZE ||
779 desc->type == QEMU_OPT_NUMBER) && opt) {
780 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
781 } else {
782 printf("%s%s=%s", sep, desc->name, value);
784 sep = separator;
788 static void opts_do_parse(QemuOpts *opts, const char *params,
789 const char *firstname, bool prepend, Error **errp)
791 char option[128], value[1024];
792 const char *p,*pe,*pc;
793 Error *local_err = NULL;
795 /* Initialize data to 0 to avoid warnings from Valgrind. */
796 memset(option, 0, sizeof(option));
797 memset(value, 0, sizeof(value));
799 for (p = params; *p != '\0'; p++) {
800 pe = strchr(p, '=');
801 pc = strchr(p, ',');
802 if (!pe || (pc && pc < pe)) {
803 /* found "foo,more" */
804 if (p == params && firstname) {
805 /* implicitly named first option */
806 pstrcpy(option, sizeof(option), firstname);
807 p = get_opt_value(value, sizeof(value), p);
808 } else {
809 /* option without value, probably a flag */
810 p = get_opt_name(option, sizeof(option), p, ',');
811 if (strncmp(option, "no", 2) == 0) {
812 memmove(option, option+2, strlen(option+2)+1);
813 pstrcpy(value, sizeof(value), "off");
814 } else {
815 pstrcpy(value, sizeof(value), "on");
818 } else {
819 /* found "foo=bar,more" */
820 p = get_opt_name(option, sizeof(option), p, '=');
821 if (*p != '=') {
822 break;
824 p++;
825 p = get_opt_value(value, sizeof(value), p);
827 if (strcmp(option, "id") != 0) {
828 /* store and parse */
829 opt_set(opts, option, value, prepend, &local_err);
830 if (local_err) {
831 error_propagate(errp, local_err);
832 return;
835 if (*p != ',') {
836 break;
842 * Store options parsed from @params into @opts.
843 * If @firstname is non-null, the first key=value in @params may omit
844 * key=, and is treated as if key was @firstname.
845 * On error, store an error object through @errp if non-null.
847 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
848 const char *firstname, Error **errp)
850 opts_do_parse(opts, params, firstname, false, errp);
853 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
854 bool permit_abbrev, bool defaults, Error **errp)
856 const char *firstname;
857 char value[1024], *id = NULL;
858 const char *p;
859 QemuOpts *opts;
860 Error *local_err = NULL;
862 assert(!permit_abbrev || list->implied_opt_name);
863 firstname = permit_abbrev ? list->implied_opt_name : NULL;
865 if (strncmp(params, "id=", 3) == 0) {
866 get_opt_value(value, sizeof(value), params+3);
867 id = value;
868 } else if ((p = strstr(params, ",id=")) != NULL) {
869 get_opt_value(value, sizeof(value), p+4);
870 id = value;
874 * This code doesn't work for defaults && !list->merge_lists: when
875 * params has no id=, and list has an element with !opts->id, it
876 * appends a new element instead of returning the existing opts.
877 * However, we got no use for this case. Guard against possible
878 * (if unlikely) future misuse:
880 assert(!defaults || list->merge_lists);
881 opts = qemu_opts_create(list, id, !defaults, &local_err);
882 if (opts == NULL) {
883 error_propagate(errp, local_err);
884 return NULL;
887 opts_do_parse(opts, params, firstname, defaults, &local_err);
888 if (local_err) {
889 error_propagate(errp, local_err);
890 qemu_opts_del(opts);
891 return NULL;
894 return opts;
898 * Create a QemuOpts in @list and with options parsed from @params.
899 * If @permit_abbrev, the first key=value in @params may omit key=,
900 * and is treated as if key was @list->implied_opt_name.
901 * On error, store an error object through @errp if non-null.
902 * Return the new QemuOpts on success, null pointer on error.
904 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
905 bool permit_abbrev, Error **errp)
907 return opts_parse(list, params, permit_abbrev, false, errp);
911 * Create a QemuOpts in @list and with options parsed from @params.
912 * If @permit_abbrev, the first key=value in @params may omit key=,
913 * and is treated as if key was @list->implied_opt_name.
914 * Report errors with error_report_err(). This is inappropriate in
915 * QMP context. Do not use this function there!
916 * Return the new QemuOpts on success, null pointer on error.
918 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
919 bool permit_abbrev)
921 Error *err = NULL;
922 QemuOpts *opts;
924 opts = opts_parse(list, params, permit_abbrev, false, &err);
925 if (err) {
926 error_report_err(err);
928 return opts;
931 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
932 int permit_abbrev)
934 QemuOpts *opts;
936 opts = opts_parse(list, params, permit_abbrev, true, NULL);
937 assert(opts);
940 typedef struct OptsFromQDictState {
941 QemuOpts *opts;
942 Error **errp;
943 } OptsFromQDictState;
945 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
947 OptsFromQDictState *state = opaque;
948 char buf[32], *tmp = NULL;
949 const char *value;
951 if (!strcmp(key, "id") || *state->errp) {
952 return;
955 switch (qobject_type(obj)) {
956 case QTYPE_QSTRING:
957 value = qstring_get_str(qobject_to_qstring(obj));
958 break;
959 case QTYPE_QNUM:
960 tmp = qnum_to_string(qobject_to_qnum(obj));
961 value = tmp;
962 break;
963 case QTYPE_QBOOL:
964 pstrcpy(buf, sizeof(buf),
965 qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off");
966 value = buf;
967 break;
968 default:
969 return;
972 qemu_opt_set(state->opts, key, value, state->errp);
973 g_free(tmp);
977 * Create QemuOpts from a QDict.
978 * Use value of key "id" as ID if it exists and is a QString. Only
979 * QStrings, QNums and QBools are copied. Entries with other types
980 * are silently ignored.
982 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
983 Error **errp)
985 OptsFromQDictState state;
986 Error *local_err = NULL;
987 QemuOpts *opts;
989 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
990 &local_err);
991 if (local_err) {
992 error_propagate(errp, local_err);
993 return NULL;
996 assert(opts != NULL);
998 state.errp = &local_err;
999 state.opts = opts;
1000 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
1001 if (local_err) {
1002 error_propagate(errp, local_err);
1003 qemu_opts_del(opts);
1004 return NULL;
1007 return opts;
1011 * Adds all QDict entries to the QemuOpts that can be added and removes them
1012 * from the QDict. When this function returns, the QDict contains only those
1013 * entries that couldn't be added to the QemuOpts.
1015 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1017 const QDictEntry *entry, *next;
1019 entry = qdict_first(qdict);
1021 while (entry != NULL) {
1022 Error *local_err = NULL;
1023 OptsFromQDictState state = {
1024 .errp = &local_err,
1025 .opts = opts,
1028 next = qdict_next(qdict, entry);
1030 if (find_desc_by_name(opts->list->desc, entry->key)) {
1031 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
1032 if (local_err) {
1033 error_propagate(errp, local_err);
1034 return;
1035 } else {
1036 qdict_del(qdict, entry->key);
1040 entry = next;
1045 * Convert from QemuOpts to QDict.
1046 * The QDict values are of type QString.
1047 * TODO We'll want to use types appropriate for opt->desc->type, but
1048 * this is enough for now.
1050 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1052 QemuOpt *opt;
1054 if (!qdict) {
1055 qdict = qdict_new();
1057 if (opts->id) {
1058 qdict_put_str(qdict, "id", opts->id);
1060 QTAILQ_FOREACH(opt, &opts->head, next) {
1061 qdict_put_str(qdict, opt->name, opt->str);
1063 return qdict;
1066 /* Validate parsed opts against descriptions where no
1067 * descriptions were provided in the QemuOptsList.
1069 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1071 QemuOpt *opt;
1072 Error *local_err = NULL;
1074 assert(opts_accepts_any(opts));
1076 QTAILQ_FOREACH(opt, &opts->head, next) {
1077 opt->desc = find_desc_by_name(desc, opt->name);
1078 if (!opt->desc) {
1079 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1080 return;
1083 qemu_opt_parse(opt, &local_err);
1084 if (local_err) {
1085 error_propagate(errp, local_err);
1086 return;
1092 * For each member of @list, call @func(@opaque, member, @errp).
1093 * Call it with the current location temporarily set to the member's.
1094 * @func() may store an Error through @errp, but must return non-zero then.
1095 * When @func() returns non-zero, break the loop and return that value.
1096 * Return zero when the loop completes.
1098 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1099 void *opaque, Error **errp)
1101 Location loc;
1102 QemuOpts *opts;
1103 int rc = 0;
1105 loc_push_none(&loc);
1106 QTAILQ_FOREACH(opts, &list->head, next) {
1107 loc_restore(&opts->loc);
1108 rc = func(opaque, opts, errp);
1109 if (rc) {
1110 break;
1112 assert(!errp || !*errp);
1114 loc_pop(&loc);
1115 return rc;
1118 static size_t count_opts_list(QemuOptsList *list)
1120 QemuOptDesc *desc = NULL;
1121 size_t num_opts = 0;
1123 if (!list) {
1124 return 0;
1127 desc = list->desc;
1128 while (desc && desc->name) {
1129 num_opts++;
1130 desc++;
1133 return num_opts;
1136 void qemu_opts_free(QemuOptsList *list)
1138 g_free(list);
1141 /* Realloc dst option list and append options from an option list (list)
1142 * to it. dst could be NULL or a malloced list.
1143 * The lifetime of dst must be shorter than the input list because the
1144 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1146 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1147 QemuOptsList *list)
1149 size_t num_opts, num_dst_opts;
1150 QemuOptDesc *desc;
1151 bool need_init = false;
1152 bool need_head_update;
1154 if (!list) {
1155 return dst;
1158 /* If dst is NULL, after realloc, some area of dst should be initialized
1159 * before adding options to it.
1161 if (!dst) {
1162 need_init = true;
1163 need_head_update = true;
1164 } else {
1165 /* Moreover, even if dst is not NULL, the realloc may move it to a
1166 * different address in which case we may get a stale tail pointer
1167 * in dst->head. */
1168 need_head_update = QTAILQ_EMPTY(&dst->head);
1171 num_opts = count_opts_list(dst);
1172 num_dst_opts = num_opts;
1173 num_opts += count_opts_list(list);
1174 dst = g_realloc(dst, sizeof(QemuOptsList) +
1175 (num_opts + 1) * sizeof(QemuOptDesc));
1176 if (need_init) {
1177 dst->name = NULL;
1178 dst->implied_opt_name = NULL;
1179 dst->merge_lists = false;
1181 if (need_head_update) {
1182 QTAILQ_INIT(&dst->head);
1184 dst->desc[num_dst_opts].name = NULL;
1186 /* append list->desc to dst->desc */
1187 if (list) {
1188 desc = list->desc;
1189 while (desc && desc->name) {
1190 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1191 dst->desc[num_dst_opts++] = *desc;
1192 dst->desc[num_dst_opts].name = NULL;
1194 desc++;
1198 return dst;