Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / util / qemu-option.c
blob50ff99a58c121e54c3bee0ef37d2e93c3a025305
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 static void parse_option_bool(const char *name, const char *value, bool *ret,
95 Error **errp)
97 if (!strcmp(value, "on")) {
98 *ret = 1;
99 } else if (!strcmp(value, "off")) {
100 *ret = 0;
101 } else {
102 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
103 name, "'on' or 'off'");
107 static void parse_option_number(const char *name, const char *value,
108 uint64_t *ret, Error **errp)
110 uint64_t number;
111 int err;
113 err = qemu_strtou64(value, NULL, 0, &number);
114 if (err == -ERANGE) {
115 error_setg(errp, "Value '%s' is too large for parameter '%s'",
116 value, name);
117 return;
119 if (err) {
120 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
121 return;
123 *ret = number;
126 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
127 const char *name)
129 int i;
131 for (i = 0; desc[i].name != NULL; i++) {
132 if (strcmp(desc[i].name, name) == 0) {
133 return &desc[i];
137 return NULL;
140 void parse_option_size(const char *name, const char *value,
141 uint64_t *ret, Error **errp)
143 uint64_t size;
144 int err;
146 err = qemu_strtosz(value, NULL, &size);
147 if (err == -ERANGE) {
148 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
149 value, name);
150 return;
152 if (err) {
153 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
154 "a non-negative number below 2^64");
155 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
156 " kilo-, mega-, giga-, tera-, peta-\n"
157 "and exabytes, respectively.\n");
158 return;
160 *ret = size;
163 bool has_help_option(const char *param)
165 size_t buflen = strlen(param) + 1;
166 char *buf = g_malloc(buflen);
167 const char *p = param;
168 bool result = false;
170 while (*p) {
171 p = get_opt_value(buf, buflen, p);
172 if (*p) {
173 p++;
176 if (is_help_option(buf)) {
177 result = true;
178 goto out;
182 out:
183 g_free(buf);
184 return result;
187 bool is_valid_option_list(const char *param)
189 size_t buflen = strlen(param) + 1;
190 char *buf = g_malloc(buflen);
191 const char *p = param;
192 bool result = true;
194 while (*p) {
195 p = get_opt_value(buf, buflen, p);
196 if (*p && !*++p) {
197 result = false;
198 goto out;
201 if (!*buf || *buf == ',') {
202 result = false;
203 goto out;
207 out:
208 g_free(buf);
209 return result;
212 void qemu_opts_print_help(QemuOptsList *list)
214 QemuOptDesc *desc;
216 assert(list);
217 desc = list->desc;
218 printf("Supported options:\n");
219 while (desc && desc->name) {
220 printf("%-16s %s\n", desc->name,
221 desc->help ? desc->help : "No description available");
222 desc++;
225 /* ------------------------------------------------------------------ */
227 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
229 QemuOpt *opt;
231 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
232 if (strcmp(opt->name, name) != 0)
233 continue;
234 return opt;
236 return NULL;
239 static void qemu_opt_del(QemuOpt *opt)
241 QTAILQ_REMOVE(&opt->opts->head, opt, next);
242 g_free(opt->name);
243 g_free(opt->str);
244 g_free(opt);
247 /* qemu_opt_set allows many settings for the same option.
248 * This function deletes all settings for an option.
250 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
252 QemuOpt *opt, *next_opt;
254 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
255 if (!strcmp(opt->name, name)) {
256 qemu_opt_del(opt);
261 const char *qemu_opt_get(QemuOpts *opts, const char *name)
263 QemuOpt *opt;
265 if (opts == NULL) {
266 return NULL;
269 opt = qemu_opt_find(opts, name);
270 if (!opt) {
271 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
272 if (desc && desc->def_value_str) {
273 return desc->def_value_str;
276 return opt ? opt->str : NULL;
279 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
281 iter->opts = opts;
282 iter->opt = QTAILQ_FIRST(&opts->head);
283 iter->name = name;
286 const char *qemu_opt_iter_next(QemuOptsIter *iter)
288 QemuOpt *ret = iter->opt;
289 if (iter->name) {
290 while (ret && !g_str_equal(iter->name, ret->name)) {
291 ret = QTAILQ_NEXT(ret, next);
294 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
295 return ret ? ret->str : NULL;
298 /* Get a known option (or its default) and remove it from the list
299 * all in one action. Return a malloced string of the option value.
300 * Result must be freed by caller with g_free().
302 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
304 QemuOpt *opt;
305 const QemuOptDesc *desc;
306 char *str = NULL;
308 if (opts == NULL) {
309 return NULL;
312 opt = qemu_opt_find(opts, name);
313 if (!opt) {
314 desc = find_desc_by_name(opts->list->desc, name);
315 if (desc && desc->def_value_str) {
316 str = g_strdup(desc->def_value_str);
318 return str;
320 str = opt->str;
321 opt->str = NULL;
322 qemu_opt_del_all(opts, name);
323 return str;
326 bool qemu_opt_has_help_opt(QemuOpts *opts)
328 QemuOpt *opt;
330 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
331 if (is_help_option(opt->name)) {
332 return true;
335 return false;
338 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
339 bool defval, bool del)
341 QemuOpt *opt;
342 bool ret = defval;
344 if (opts == NULL) {
345 return ret;
348 opt = qemu_opt_find(opts, name);
349 if (opt == NULL) {
350 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
351 if (desc && desc->def_value_str) {
352 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
354 return ret;
356 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
357 ret = opt->value.boolean;
358 if (del) {
359 qemu_opt_del_all(opts, name);
361 return ret;
364 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
366 return qemu_opt_get_bool_helper(opts, name, defval, false);
369 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
371 return qemu_opt_get_bool_helper(opts, name, defval, true);
374 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
375 uint64_t defval, bool del)
377 QemuOpt *opt;
378 uint64_t ret = defval;
380 if (opts == NULL) {
381 return ret;
384 opt = qemu_opt_find(opts, name);
385 if (opt == NULL) {
386 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
387 if (desc && desc->def_value_str) {
388 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
390 return ret;
392 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
393 ret = opt->value.uint;
394 if (del) {
395 qemu_opt_del_all(opts, name);
397 return ret;
400 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
402 return qemu_opt_get_number_helper(opts, name, defval, false);
405 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
406 uint64_t defval)
408 return qemu_opt_get_number_helper(opts, name, defval, true);
411 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
412 uint64_t defval, bool del)
414 QemuOpt *opt;
415 uint64_t ret = defval;
417 if (opts == NULL) {
418 return ret;
421 opt = qemu_opt_find(opts, name);
422 if (opt == NULL) {
423 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
424 if (desc && desc->def_value_str) {
425 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
427 return ret;
429 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
430 ret = opt->value.uint;
431 if (del) {
432 qemu_opt_del_all(opts, name);
434 return ret;
437 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
439 return qemu_opt_get_size_helper(opts, name, defval, false);
442 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
443 uint64_t defval)
445 return qemu_opt_get_size_helper(opts, name, defval, true);
448 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
450 if (opt->desc == NULL)
451 return;
453 switch (opt->desc->type) {
454 case QEMU_OPT_STRING:
455 /* nothing */
456 return;
457 case QEMU_OPT_BOOL:
458 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
459 break;
460 case QEMU_OPT_NUMBER:
461 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
462 break;
463 case QEMU_OPT_SIZE:
464 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
465 break;
466 default:
467 abort();
471 static bool opts_accepts_any(const QemuOpts *opts)
473 return opts->list->desc[0].name == NULL;
476 int qemu_opt_unset(QemuOpts *opts, const char *name)
478 QemuOpt *opt = qemu_opt_find(opts, name);
480 assert(opts_accepts_any(opts));
482 if (opt == NULL) {
483 return -1;
484 } else {
485 qemu_opt_del(opt);
486 return 0;
490 static void opt_set(QemuOpts *opts, const char *name, const char *value,
491 bool prepend, Error **errp)
493 QemuOpt *opt;
494 const QemuOptDesc *desc;
495 Error *local_err = NULL;
497 desc = find_desc_by_name(opts->list->desc, name);
498 if (!desc && !opts_accepts_any(opts)) {
499 error_setg(errp, QERR_INVALID_PARAMETER, name);
500 return;
503 opt = g_malloc0(sizeof(*opt));
504 opt->name = g_strdup(name);
505 opt->opts = opts;
506 if (prepend) {
507 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
508 } else {
509 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
511 opt->desc = desc;
512 opt->str = g_strdup(value);
513 assert(opt->str);
514 qemu_opt_parse(opt, &local_err);
515 if (local_err) {
516 error_propagate(errp, local_err);
517 qemu_opt_del(opt);
521 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
522 Error **errp)
524 opt_set(opts, name, value, false, errp);
527 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
528 Error **errp)
530 QemuOpt *opt;
531 const QemuOptDesc *desc = opts->list->desc;
533 opt = g_malloc0(sizeof(*opt));
534 opt->desc = find_desc_by_name(desc, name);
535 if (!opt->desc && !opts_accepts_any(opts)) {
536 error_setg(errp, QERR_INVALID_PARAMETER, name);
537 g_free(opt);
538 return;
541 opt->name = g_strdup(name);
542 opt->opts = opts;
543 opt->value.boolean = !!val;
544 opt->str = g_strdup(val ? "on" : "off");
545 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
548 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
549 Error **errp)
551 QemuOpt *opt;
552 const QemuOptDesc *desc = opts->list->desc;
554 opt = g_malloc0(sizeof(*opt));
555 opt->desc = find_desc_by_name(desc, name);
556 if (!opt->desc && !opts_accepts_any(opts)) {
557 error_setg(errp, QERR_INVALID_PARAMETER, name);
558 g_free(opt);
559 return;
562 opt->name = g_strdup(name);
563 opt->opts = opts;
564 opt->value.uint = val;
565 opt->str = g_strdup_printf("%" PRId64, val);
566 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
570 * For each member of @opts, call @func(@opaque, name, value, @errp).
571 * @func() may store an Error through @errp, but must return non-zero then.
572 * When @func() returns non-zero, break the loop and return that value.
573 * Return zero when the loop completes.
575 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
576 Error **errp)
578 QemuOpt *opt;
579 int rc;
581 QTAILQ_FOREACH(opt, &opts->head, next) {
582 rc = func(opaque, opt->name, opt->str, errp);
583 if (rc) {
584 return rc;
586 assert(!errp || !*errp);
588 return 0;
591 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
593 QemuOpts *opts;
595 QTAILQ_FOREACH(opts, &list->head, next) {
596 if (!opts->id && !id) {
597 return opts;
599 if (opts->id && id && !strcmp(opts->id, id)) {
600 return opts;
603 return NULL;
606 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
607 int fail_if_exists, Error **errp)
609 QemuOpts *opts = NULL;
611 if (id) {
612 if (!id_wellformed(id)) {
613 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
614 "an identifier");
615 error_append_hint(errp, "Identifiers consist of letters, digits, "
616 "'-', '.', '_', starting with a letter.\n");
617 return NULL;
619 opts = qemu_opts_find(list, id);
620 if (opts != NULL) {
621 if (fail_if_exists && !list->merge_lists) {
622 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
623 return NULL;
624 } else {
625 return opts;
628 } else if (list->merge_lists) {
629 opts = qemu_opts_find(list, NULL);
630 if (opts) {
631 return opts;
634 opts = g_malloc0(sizeof(*opts));
635 opts->id = g_strdup(id);
636 opts->list = list;
637 loc_save(&opts->loc);
638 QTAILQ_INIT(&opts->head);
639 QTAILQ_INSERT_TAIL(&list->head, opts, next);
640 return opts;
643 void qemu_opts_reset(QemuOptsList *list)
645 QemuOpts *opts, *next_opts;
647 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
648 qemu_opts_del(opts);
652 void qemu_opts_loc_restore(QemuOpts *opts)
654 loc_restore(&opts->loc);
657 void qemu_opts_set(QemuOptsList *list, const char *id,
658 const char *name, const char *value, Error **errp)
660 QemuOpts *opts;
661 Error *local_err = NULL;
663 opts = qemu_opts_create(list, id, 1, &local_err);
664 if (local_err) {
665 error_propagate(errp, local_err);
666 return;
668 qemu_opt_set(opts, name, value, errp);
671 const char *qemu_opts_id(QemuOpts *opts)
673 return opts->id;
676 /* The id string will be g_free()d by qemu_opts_del */
677 void qemu_opts_set_id(QemuOpts *opts, char *id)
679 opts->id = id;
682 void qemu_opts_del(QemuOpts *opts)
684 QemuOpt *opt;
686 if (opts == NULL) {
687 return;
690 for (;;) {
691 opt = QTAILQ_FIRST(&opts->head);
692 if (opt == NULL)
693 break;
694 qemu_opt_del(opt);
696 QTAILQ_REMOVE(&opts->list->head, opts, next);
697 g_free(opts->id);
698 g_free(opts);
701 /* print value, escaping any commas in value */
702 static void escaped_print(const char *value)
704 const char *ptr;
706 for (ptr = value; *ptr; ++ptr) {
707 if (*ptr == ',') {
708 putchar(',');
710 putchar(*ptr);
714 void qemu_opts_print(QemuOpts *opts, const char *separator)
716 QemuOpt *opt;
717 QemuOptDesc *desc = opts->list->desc;
718 const char *sep = "";
720 if (opts->id) {
721 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
722 sep = separator;
725 if (desc[0].name == NULL) {
726 QTAILQ_FOREACH(opt, &opts->head, next) {
727 printf("%s%s=", sep, opt->name);
728 escaped_print(opt->str);
729 sep = separator;
731 return;
733 for (; desc && desc->name; desc++) {
734 const char *value;
735 opt = qemu_opt_find(opts, desc->name);
737 value = opt ? opt->str : desc->def_value_str;
738 if (!value) {
739 continue;
741 if (desc->type == QEMU_OPT_STRING) {
742 printf("%s%s=", sep, desc->name);
743 escaped_print(value);
744 } else if ((desc->type == QEMU_OPT_SIZE ||
745 desc->type == QEMU_OPT_NUMBER) && opt) {
746 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
747 } else {
748 printf("%s%s=%s", sep, desc->name, value);
750 sep = separator;
754 static void opts_do_parse(QemuOpts *opts, const char *params,
755 const char *firstname, bool prepend, Error **errp)
757 char option[128], value[1024];
758 const char *p,*pe,*pc;
759 Error *local_err = NULL;
761 /* Initialize data to 0 to avoid warnings from Valgrind. */
762 memset(option, 0, sizeof(option));
763 memset(value, 0, sizeof(value));
765 for (p = params; *p != '\0'; p++) {
766 pe = strchr(p, '=');
767 pc = strchr(p, ',');
768 if (!pe || (pc && pc < pe)) {
769 /* found "foo,more" */
770 if (p == params && firstname) {
771 /* implicitly named first option */
772 pstrcpy(option, sizeof(option), firstname);
773 p = get_opt_value(value, sizeof(value), p);
774 } else {
775 /* option without value, probably a flag */
776 p = get_opt_name(option, sizeof(option), p, ',');
777 if (strncmp(option, "no", 2) == 0) {
778 memmove(option, option+2, strlen(option+2)+1);
779 pstrcpy(value, sizeof(value), "off");
780 } else {
781 pstrcpy(value, sizeof(value), "on");
784 } else {
785 /* found "foo=bar,more" */
786 p = get_opt_name(option, sizeof(option), p, '=');
787 if (*p != '=') {
788 break;
790 p++;
791 p = get_opt_value(value, sizeof(value), p);
793 if (strcmp(option, "id") != 0) {
794 /* store and parse */
795 opt_set(opts, option, value, prepend, &local_err);
796 if (local_err) {
797 error_propagate(errp, local_err);
798 return;
801 if (*p != ',') {
802 break;
808 * Store options parsed from @params into @opts.
809 * If @firstname is non-null, the first key=value in @params may omit
810 * key=, and is treated as if key was @firstname.
811 * On error, store an error object through @errp if non-null.
813 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
814 const char *firstname, Error **errp)
816 opts_do_parse(opts, params, firstname, false, errp);
819 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
820 bool permit_abbrev, bool defaults, Error **errp)
822 const char *firstname;
823 char value[1024], *id = NULL;
824 const char *p;
825 QemuOpts *opts;
826 Error *local_err = NULL;
828 assert(!permit_abbrev || list->implied_opt_name);
829 firstname = permit_abbrev ? list->implied_opt_name : NULL;
831 if (strncmp(params, "id=", 3) == 0) {
832 get_opt_value(value, sizeof(value), params+3);
833 id = value;
834 } else if ((p = strstr(params, ",id=")) != NULL) {
835 get_opt_value(value, sizeof(value), p+4);
836 id = value;
840 * This code doesn't work for defaults && !list->merge_lists: when
841 * params has no id=, and list has an element with !opts->id, it
842 * appends a new element instead of returning the existing opts.
843 * However, we got no use for this case. Guard against possible
844 * (if unlikely) future misuse:
846 assert(!defaults || list->merge_lists);
847 opts = qemu_opts_create(list, id, !defaults, &local_err);
848 if (opts == NULL) {
849 error_propagate(errp, local_err);
850 return NULL;
853 opts_do_parse(opts, params, firstname, defaults, &local_err);
854 if (local_err) {
855 error_propagate(errp, local_err);
856 qemu_opts_del(opts);
857 return NULL;
860 return opts;
864 * Create a QemuOpts in @list and with options parsed from @params.
865 * If @permit_abbrev, the first key=value in @params may omit key=,
866 * and is treated as if key was @list->implied_opt_name.
867 * On error, store an error object through @errp if non-null.
868 * Return the new QemuOpts on success, null pointer on error.
870 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
871 bool permit_abbrev, Error **errp)
873 return opts_parse(list, params, permit_abbrev, false, errp);
877 * Create a QemuOpts in @list and with options parsed from @params.
878 * If @permit_abbrev, the first key=value in @params may omit key=,
879 * and is treated as if key was @list->implied_opt_name.
880 * Report errors with error_report_err(). This is inappropriate in
881 * QMP context. Do not use this function there!
882 * Return the new QemuOpts on success, null pointer on error.
884 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
885 bool permit_abbrev)
887 Error *err = NULL;
888 QemuOpts *opts;
890 opts = opts_parse(list, params, permit_abbrev, false, &err);
891 if (err) {
892 error_report_err(err);
894 return opts;
897 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
898 int permit_abbrev)
900 QemuOpts *opts;
902 opts = opts_parse(list, params, permit_abbrev, true, NULL);
903 assert(opts);
906 typedef struct OptsFromQDictState {
907 QemuOpts *opts;
908 Error **errp;
909 } OptsFromQDictState;
911 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
913 OptsFromQDictState *state = opaque;
914 char buf[32], *tmp = NULL;
915 const char *value;
917 if (!strcmp(key, "id") || *state->errp) {
918 return;
921 switch (qobject_type(obj)) {
922 case QTYPE_QSTRING:
923 value = qstring_get_str(qobject_to_qstring(obj));
924 break;
925 case QTYPE_QNUM:
926 tmp = qnum_to_string(qobject_to_qnum(obj));
927 value = tmp;
928 break;
929 case QTYPE_QBOOL:
930 pstrcpy(buf, sizeof(buf),
931 qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off");
932 value = buf;
933 break;
934 default:
935 return;
938 qemu_opt_set(state->opts, key, value, state->errp);
939 g_free(tmp);
943 * Create QemuOpts from a QDict.
944 * Use value of key "id" as ID if it exists and is a QString. Only
945 * QStrings, QNums and QBools are copied. Entries with other types
946 * are silently ignored.
948 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
949 Error **errp)
951 OptsFromQDictState state;
952 Error *local_err = NULL;
953 QemuOpts *opts;
955 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
956 &local_err);
957 if (local_err) {
958 error_propagate(errp, local_err);
959 return NULL;
962 assert(opts != NULL);
964 state.errp = &local_err;
965 state.opts = opts;
966 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
967 if (local_err) {
968 error_propagate(errp, local_err);
969 qemu_opts_del(opts);
970 return NULL;
973 return opts;
977 * Adds all QDict entries to the QemuOpts that can be added and removes them
978 * from the QDict. When this function returns, the QDict contains only those
979 * entries that couldn't be added to the QemuOpts.
981 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
983 const QDictEntry *entry, *next;
985 entry = qdict_first(qdict);
987 while (entry != NULL) {
988 Error *local_err = NULL;
989 OptsFromQDictState state = {
990 .errp = &local_err,
991 .opts = opts,
994 next = qdict_next(qdict, entry);
996 if (find_desc_by_name(opts->list->desc, entry->key)) {
997 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
998 if (local_err) {
999 error_propagate(errp, local_err);
1000 return;
1001 } else {
1002 qdict_del(qdict, entry->key);
1006 entry = next;
1011 * Convert from QemuOpts to QDict.
1012 * The QDict values are of type QString.
1013 * TODO We'll want to use types appropriate for opt->desc->type, but
1014 * this is enough for now.
1016 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1018 QemuOpt *opt;
1020 if (!qdict) {
1021 qdict = qdict_new();
1023 if (opts->id) {
1024 qdict_put_str(qdict, "id", opts->id);
1026 QTAILQ_FOREACH(opt, &opts->head, next) {
1027 qdict_put_str(qdict, opt->name, opt->str);
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_setg(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 = 0;
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 break;
1078 assert(!errp || !*errp);
1080 loc_pop(&loc);
1081 return rc;
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;