qemu-option: Make uses of find_desc_by_name() more similar
[qemu/ar7.git] / util / qemu-option.c
blob1df55bc8819264461e4943aa8de8fc04dafb050c
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/error-report.h"
30 #include "qapi/qmp/qbool.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qapi/qmp/qerror.h"
35 #include "qemu/option_int.h"
36 #include "qemu/cutils.h"
37 #include "qemu/id.h"
38 #include "qemu/help_option.h"
41 * Extracts the name of an option from the parameter string (p points at the
42 * first byte of the option name)
44 * The option name is delimited by delim (usually , or =) or the string end
45 * and is copied into option. The caller is responsible for free'ing option
46 * when no longer required.
48 * The return value is the position of the delimiter/zero byte after the option
49 * name in p.
51 static const char *get_opt_name(const char *p, char **option, char delim)
53 char *offset = strchr(p, delim);
55 if (offset) {
56 *option = g_strndup(p, offset - p);
57 return offset;
58 } else {
59 *option = g_strdup(p);
60 return p + strlen(p);
65 * Extracts the value of an option from the parameter string p (p points at the
66 * first byte of the option value)
68 * This function is comparable to get_opt_name with the difference that the
69 * delimiter is fixed to be comma which starts a new option. To specify an
70 * option value that contains commas, double each comma.
72 const char *get_opt_value(const char *p, char **value)
74 size_t capacity = 0, length;
75 const char *offset;
77 *value = NULL;
78 while (1) {
79 offset = qemu_strchrnul(p, ',');
80 length = offset - p;
81 if (*offset != '\0' && *(offset + 1) == ',') {
82 length++;
84 *value = g_renew(char, *value, capacity + length + 1);
85 strncpy(*value + capacity, p, length);
86 (*value)[capacity + length] = '\0';
87 capacity += length;
88 if (*offset == '\0' ||
89 *(offset + 1) != ',') {
90 break;
93 p += (offset - p) + 2;
96 return offset;
99 static void parse_option_bool(const char *name, const char *value, bool *ret,
100 Error **errp)
102 if (!strcmp(value, "on")) {
103 *ret = 1;
104 } else if (!strcmp(value, "off")) {
105 *ret = 0;
106 } else {
107 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
108 name, "'on' or 'off'");
112 static void parse_option_number(const char *name, const char *value,
113 uint64_t *ret, Error **errp)
115 uint64_t number;
116 int err;
118 err = qemu_strtou64(value, NULL, 0, &number);
119 if (err == -ERANGE) {
120 error_setg(errp, "Value '%s' is too large for parameter '%s'",
121 value, name);
122 return;
124 if (err) {
125 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
126 return;
128 *ret = number;
131 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
132 const char *name)
134 int i;
136 for (i = 0; desc[i].name != NULL; i++) {
137 if (strcmp(desc[i].name, name) == 0) {
138 return &desc[i];
142 return NULL;
145 void parse_option_size(const char *name, const char *value,
146 uint64_t *ret, Error **errp)
148 uint64_t size;
149 int err;
151 err = qemu_strtosz(value, NULL, &size);
152 if (err == -ERANGE) {
153 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
154 value, name);
155 return;
157 if (err) {
158 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
159 "a non-negative number below 2^64");
160 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
161 " kilo-, mega-, giga-, tera-, peta-\n"
162 "and exabytes, respectively.\n");
163 return;
165 *ret = size;
168 static const char *opt_type_to_string(enum QemuOptType type)
170 switch (type) {
171 case QEMU_OPT_STRING:
172 return "str";
173 case QEMU_OPT_BOOL:
174 return "bool (on/off)";
175 case QEMU_OPT_NUMBER:
176 return "num";
177 case QEMU_OPT_SIZE:
178 return "size";
181 g_assert_not_reached();
185 * Print the list of options available in the given list. If
186 * @print_caption is true, a caption (including the list name, if it
187 * exists) is printed. The options itself will be indented, so
188 * @print_caption should only be set to false if the caller prints its
189 * own custom caption (so that the indentation makes sense).
191 void qemu_opts_print_help(QemuOptsList *list, bool print_caption)
193 QemuOptDesc *desc;
194 int i;
195 GPtrArray *array = g_ptr_array_new();
197 assert(list);
198 desc = list->desc;
199 while (desc && desc->name) {
200 GString *str = g_string_new(NULL);
201 g_string_append_printf(str, " %s=<%s>", desc->name,
202 opt_type_to_string(desc->type));
203 if (desc->help) {
204 if (str->len < 24) {
205 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
207 g_string_append_printf(str, " - %s", desc->help);
209 g_ptr_array_add(array, g_string_free(str, false));
210 desc++;
213 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
214 if (print_caption && array->len > 0) {
215 if (list->name) {
216 printf("%s options:\n", list->name);
217 } else {
218 printf("Options:\n");
220 } else if (array->len == 0) {
221 if (list->name) {
222 printf("There are no options for %s.\n", list->name);
223 } else {
224 printf("No options available.\n");
227 for (i = 0; i < array->len; i++) {
228 printf("%s\n", (char *)array->pdata[i]);
230 g_ptr_array_set_free_func(array, g_free);
231 g_ptr_array_free(array, true);
234 /* ------------------------------------------------------------------ */
236 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
238 QemuOpt *opt;
240 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
241 if (strcmp(opt->name, name) != 0)
242 continue;
243 return opt;
245 return NULL;
248 static void qemu_opt_del(QemuOpt *opt)
250 QTAILQ_REMOVE(&opt->opts->head, opt, next);
251 g_free(opt->name);
252 g_free(opt->str);
253 g_free(opt);
256 /* qemu_opt_set allows many settings for the same option.
257 * This function deletes all settings for an option.
259 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
261 QemuOpt *opt, *next_opt;
263 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
264 if (!strcmp(opt->name, name)) {
265 qemu_opt_del(opt);
270 const char *qemu_opt_get(QemuOpts *opts, const char *name)
272 QemuOpt *opt;
273 const QemuOptDesc *desc;
275 if (opts == NULL) {
276 return NULL;
279 opt = qemu_opt_find(opts, name);
280 if (!opt) {
281 desc = find_desc_by_name(opts->list->desc, name);
282 if (desc && desc->def_value_str) {
283 return desc->def_value_str;
286 return opt ? opt->str : NULL;
289 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
291 iter->opts = opts;
292 iter->opt = QTAILQ_FIRST(&opts->head);
293 iter->name = name;
296 const char *qemu_opt_iter_next(QemuOptsIter *iter)
298 QemuOpt *ret = iter->opt;
299 if (iter->name) {
300 while (ret && !g_str_equal(iter->name, ret->name)) {
301 ret = QTAILQ_NEXT(ret, next);
304 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
305 return ret ? ret->str : NULL;
308 /* Get a known option (or its default) and remove it from the list
309 * all in one action. Return a malloced string of the option value.
310 * Result must be freed by caller with g_free().
312 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
314 QemuOpt *opt;
315 const QemuOptDesc *desc;
316 char *str = NULL;
318 if (opts == NULL) {
319 return NULL;
322 opt = qemu_opt_find(opts, name);
323 if (!opt) {
324 desc = find_desc_by_name(opts->list->desc, name);
325 if (desc && desc->def_value_str) {
326 str = g_strdup(desc->def_value_str);
328 return str;
330 str = opt->str;
331 opt->str = NULL;
332 qemu_opt_del_all(opts, name);
333 return str;
336 bool qemu_opt_has_help_opt(QemuOpts *opts)
338 QemuOpt *opt;
340 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
341 if (is_help_option(opt->name)) {
342 return true;
345 return false;
348 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
349 bool defval, bool del)
351 QemuOpt *opt;
352 const QemuOptDesc *desc;
353 bool ret = defval;
355 if (opts == NULL) {
356 return ret;
359 opt = qemu_opt_find(opts, name);
360 if (opt == NULL) {
361 desc = find_desc_by_name(opts->list->desc, name);
362 if (desc && desc->def_value_str) {
363 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
365 return ret;
367 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
368 ret = opt->value.boolean;
369 if (del) {
370 qemu_opt_del_all(opts, name);
372 return ret;
375 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
377 return qemu_opt_get_bool_helper(opts, name, defval, false);
380 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
382 return qemu_opt_get_bool_helper(opts, name, defval, true);
385 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
386 uint64_t defval, bool del)
388 QemuOpt *opt;
389 const QemuOptDesc *desc;
390 uint64_t ret = defval;
392 if (opts == NULL) {
393 return ret;
396 opt = qemu_opt_find(opts, name);
397 if (opt == NULL) {
398 desc = find_desc_by_name(opts->list->desc, name);
399 if (desc && desc->def_value_str) {
400 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
402 return ret;
404 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
405 ret = opt->value.uint;
406 if (del) {
407 qemu_opt_del_all(opts, name);
409 return ret;
412 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
414 return qemu_opt_get_number_helper(opts, name, defval, false);
417 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
418 uint64_t defval)
420 return qemu_opt_get_number_helper(opts, name, defval, true);
423 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
424 uint64_t defval, bool del)
426 QemuOpt *opt;
427 const QemuOptDesc *desc;
428 uint64_t ret = defval;
430 if (opts == NULL) {
431 return ret;
434 opt = qemu_opt_find(opts, name);
435 if (opt == NULL) {
436 desc = find_desc_by_name(opts->list->desc, name);
437 if (desc && desc->def_value_str) {
438 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
440 return ret;
442 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
443 ret = opt->value.uint;
444 if (del) {
445 qemu_opt_del_all(opts, name);
447 return ret;
450 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
452 return qemu_opt_get_size_helper(opts, name, defval, false);
455 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
456 uint64_t defval)
458 return qemu_opt_get_size_helper(opts, name, defval, true);
461 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
463 if (opt->desc == NULL)
464 return;
466 switch (opt->desc->type) {
467 case QEMU_OPT_STRING:
468 /* nothing */
469 return;
470 case QEMU_OPT_BOOL:
471 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
472 break;
473 case QEMU_OPT_NUMBER:
474 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
475 break;
476 case QEMU_OPT_SIZE:
477 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
478 break;
479 default:
480 abort();
484 static bool opts_accepts_any(const QemuOpts *opts)
486 return opts->list->desc[0].name == NULL;
489 int qemu_opt_unset(QemuOpts *opts, const char *name)
491 QemuOpt *opt = qemu_opt_find(opts, name);
493 assert(opts_accepts_any(opts));
495 if (opt == NULL) {
496 return -1;
497 } else {
498 qemu_opt_del(opt);
499 return 0;
503 static void opt_set(QemuOpts *opts, const char *name, char *value,
504 bool prepend, bool *help_wanted, Error **errp)
506 QemuOpt *opt;
507 const QemuOptDesc *desc;
508 Error *local_err = NULL;
510 desc = find_desc_by_name(opts->list->desc, name);
511 if (!desc && !opts_accepts_any(opts)) {
512 g_free(value);
513 error_setg(errp, QERR_INVALID_PARAMETER, name);
514 if (help_wanted && is_help_option(name)) {
515 *help_wanted = true;
517 return;
520 opt = g_malloc0(sizeof(*opt));
521 opt->name = g_strdup(name);
522 opt->opts = opts;
523 if (prepend) {
524 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
525 } else {
526 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
528 opt->desc = desc;
529 opt->str = value;
530 qemu_opt_parse(opt, &local_err);
531 if (local_err) {
532 error_propagate(errp, local_err);
533 qemu_opt_del(opt);
537 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
538 Error **errp)
540 opt_set(opts, name, g_strdup(value), false, NULL, errp);
543 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
544 Error **errp)
546 QemuOpt *opt;
547 const QemuOptDesc *desc;
549 desc = find_desc_by_name(opts->list->desc, name);
550 if (!desc && !opts_accepts_any(opts)) {
551 error_setg(errp, QERR_INVALID_PARAMETER, name);
552 return;
555 opt = g_malloc0(sizeof(*opt));
556 opt->name = g_strdup(name);
557 opt->opts = opts;
558 opt->desc = desc;
559 opt->value.boolean = !!val;
560 opt->str = g_strdup(val ? "on" : "off");
561 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
564 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
565 Error **errp)
567 QemuOpt *opt;
568 const QemuOptDesc *desc;
570 desc = find_desc_by_name(opts->list->desc, name);
571 if (!desc && !opts_accepts_any(opts)) {
572 error_setg(errp, QERR_INVALID_PARAMETER, name);
573 return;
576 opt = g_malloc0(sizeof(*opt));
577 opt->name = g_strdup(name);
578 opt->opts = opts;
579 opt->desc = desc;
580 opt->value.uint = val;
581 opt->str = g_strdup_printf("%" PRId64, val);
582 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
586 * For each member of @opts, call @func(@opaque, name, value, @errp).
587 * @func() may store an Error through @errp, but must return non-zero then.
588 * When @func() returns non-zero, break the loop and return that value.
589 * Return zero when the loop completes.
591 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
592 Error **errp)
594 QemuOpt *opt;
595 int rc;
597 QTAILQ_FOREACH(opt, &opts->head, next) {
598 rc = func(opaque, opt->name, opt->str, errp);
599 if (rc) {
600 return rc;
602 assert(!errp || !*errp);
604 return 0;
607 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
609 QemuOpts *opts;
611 QTAILQ_FOREACH(opts, &list->head, next) {
612 if (!opts->id && !id) {
613 return opts;
615 if (opts->id && id && !strcmp(opts->id, id)) {
616 return opts;
619 return NULL;
622 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
623 int fail_if_exists, Error **errp)
625 QemuOpts *opts = NULL;
627 if (id) {
628 if (!id_wellformed(id)) {
629 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
630 "an identifier");
631 error_append_hint(errp, "Identifiers consist of letters, digits, "
632 "'-', '.', '_', starting with a letter.\n");
633 return NULL;
635 opts = qemu_opts_find(list, id);
636 if (opts != NULL) {
637 if (fail_if_exists && !list->merge_lists) {
638 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
639 return NULL;
640 } else {
641 return opts;
644 } else if (list->merge_lists) {
645 opts = qemu_opts_find(list, NULL);
646 if (opts) {
647 return opts;
650 opts = g_malloc0(sizeof(*opts));
651 opts->id = g_strdup(id);
652 opts->list = list;
653 loc_save(&opts->loc);
654 QTAILQ_INIT(&opts->head);
655 QTAILQ_INSERT_TAIL(&list->head, opts, next);
656 return opts;
659 void qemu_opts_reset(QemuOptsList *list)
661 QemuOpts *opts, *next_opts;
663 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
664 qemu_opts_del(opts);
668 void qemu_opts_loc_restore(QemuOpts *opts)
670 loc_restore(&opts->loc);
673 void qemu_opts_set(QemuOptsList *list, const char *id,
674 const char *name, const char *value, Error **errp)
676 QemuOpts *opts;
678 opts = qemu_opts_create(list, id, 1, errp);
679 if (!opts) {
680 return;
682 qemu_opt_set(opts, name, value, errp);
685 const char *qemu_opts_id(QemuOpts *opts)
687 return opts->id;
690 /* The id string will be g_free()d by qemu_opts_del */
691 void qemu_opts_set_id(QemuOpts *opts, char *id)
693 opts->id = id;
696 void qemu_opts_del(QemuOpts *opts)
698 QemuOpt *opt;
700 if (opts == NULL) {
701 return;
704 for (;;) {
705 opt = QTAILQ_FIRST(&opts->head);
706 if (opt == NULL)
707 break;
708 qemu_opt_del(opt);
710 QTAILQ_REMOVE(&opts->list->head, opts, next);
711 g_free(opts->id);
712 g_free(opts);
715 /* print value, escaping any commas in value */
716 static void escaped_print(const char *value)
718 const char *ptr;
720 for (ptr = value; *ptr; ++ptr) {
721 if (*ptr == ',') {
722 putchar(',');
724 putchar(*ptr);
728 void qemu_opts_print(QemuOpts *opts, const char *separator)
730 QemuOpt *opt;
731 QemuOptDesc *desc = opts->list->desc;
732 const char *sep = "";
734 if (opts->id) {
735 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
736 sep = separator;
739 if (desc[0].name == NULL) {
740 QTAILQ_FOREACH(opt, &opts->head, next) {
741 printf("%s%s=", sep, opt->name);
742 escaped_print(opt->str);
743 sep = separator;
745 return;
747 for (; desc && desc->name; desc++) {
748 const char *value;
749 opt = qemu_opt_find(opts, desc->name);
751 value = opt ? opt->str : desc->def_value_str;
752 if (!value) {
753 continue;
755 if (desc->type == QEMU_OPT_STRING) {
756 printf("%s%s=", sep, desc->name);
757 escaped_print(value);
758 } else if ((desc->type == QEMU_OPT_SIZE ||
759 desc->type == QEMU_OPT_NUMBER) && opt) {
760 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
761 } else {
762 printf("%s%s=%s", sep, desc->name, value);
764 sep = separator;
768 static const char *get_opt_name_value(const char *params,
769 const char *firstname,
770 char **name, char **value)
772 const char *p, *pe, *pc;
774 pe = strchr(params, '=');
775 pc = strchr(params, ',');
777 if (!pe || (pc && pc < pe)) {
778 /* found "foo,more" */
779 if (firstname) {
780 /* implicitly named first option */
781 *name = g_strdup(firstname);
782 p = get_opt_value(params, value);
783 } else {
784 /* option without value, must be a flag */
785 p = get_opt_name(params, name, ',');
786 if (strncmp(*name, "no", 2) == 0) {
787 memmove(*name, *name + 2, strlen(*name + 2) + 1);
788 *value = g_strdup("off");
789 } else {
790 *value = g_strdup("on");
793 } else {
794 /* found "foo=bar,more" */
795 p = get_opt_name(params, name, '=');
796 assert(*p == '=');
797 p++;
798 p = get_opt_value(p, value);
801 assert(!*p || *p == ',');
802 if (*p == ',') {
803 p++;
805 return p;
808 static void opts_do_parse(QemuOpts *opts, const char *params,
809 const char *firstname, bool prepend,
810 bool *help_wanted, Error **errp)
812 Error *local_err = NULL;
813 char *option, *value;
814 const char *p;
816 for (p = params; *p;) {
817 p = get_opt_name_value(p, firstname, &option, &value);
818 firstname = NULL;
820 if (!strcmp(option, "id")) {
821 g_free(option);
822 g_free(value);
823 continue;
826 opt_set(opts, option, value, prepend, help_wanted, &local_err);
827 g_free(option);
828 if (local_err) {
829 error_propagate(errp, local_err);
830 return;
835 static char *opts_parse_id(const char *params)
837 const char *p;
838 char *name, *value;
840 for (p = params; *p;) {
841 p = get_opt_name_value(p, NULL, &name, &value);
842 if (!strcmp(name, "id")) {
843 g_free(name);
844 return value;
846 g_free(name);
847 g_free(value);
850 return NULL;
853 bool has_help_option(const char *params)
855 const char *p;
856 char *name, *value;
857 bool ret;
859 for (p = params; *p;) {
860 p = get_opt_name_value(p, NULL, &name, &value);
861 ret = is_help_option(name);
862 g_free(name);
863 g_free(value);
864 if (ret) {
865 return true;
869 return false;
873 * Store options parsed from @params into @opts.
874 * If @firstname is non-null, the first key=value in @params may omit
875 * key=, and is treated as if key was @firstname.
876 * On error, store an error object through @errp if non-null.
878 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
879 const char *firstname, Error **errp)
881 opts_do_parse(opts, params, firstname, false, NULL, errp);
884 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
885 bool permit_abbrev, bool defaults,
886 bool *help_wanted, Error **errp)
888 const char *firstname;
889 char *id = opts_parse_id(params);
890 QemuOpts *opts;
891 Error *local_err = NULL;
893 assert(!permit_abbrev || list->implied_opt_name);
894 firstname = permit_abbrev ? list->implied_opt_name : NULL;
897 * This code doesn't work for defaults && !list->merge_lists: when
898 * params has no id=, and list has an element with !opts->id, it
899 * appends a new element instead of returning the existing opts.
900 * However, we got no use for this case. Guard against possible
901 * (if unlikely) future misuse:
903 assert(!defaults || list->merge_lists);
904 opts = qemu_opts_create(list, id, !defaults, &local_err);
905 g_free(id);
906 if (opts == NULL) {
907 error_propagate(errp, local_err);
908 return NULL;
911 opts_do_parse(opts, params, firstname, defaults, help_wanted, &local_err);
912 if (local_err) {
913 error_propagate(errp, local_err);
914 qemu_opts_del(opts);
915 return NULL;
918 return opts;
922 * Create a QemuOpts in @list and with options parsed from @params.
923 * If @permit_abbrev, the first key=value in @params may omit key=,
924 * and is treated as if key was @list->implied_opt_name.
925 * On error, store an error object through @errp if non-null.
926 * Return the new QemuOpts on success, null pointer on error.
928 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
929 bool permit_abbrev, Error **errp)
931 return opts_parse(list, params, permit_abbrev, false, NULL, errp);
935 * Create a QemuOpts in @list and with options parsed from @params.
936 * If @permit_abbrev, the first key=value in @params may omit key=,
937 * and is treated as if key was @list->implied_opt_name.
938 * Report errors with error_report_err(). This is inappropriate in
939 * QMP context. Do not use this function there!
940 * Return the new QemuOpts on success, null pointer on error.
942 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
943 bool permit_abbrev)
945 Error *err = NULL;
946 QemuOpts *opts;
947 bool help_wanted = false;
949 opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err);
950 if (err) {
951 if (help_wanted) {
952 qemu_opts_print_help(list, true);
953 error_free(err);
954 } else {
955 error_report_err(err);
958 return opts;
961 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
962 int permit_abbrev)
964 QemuOpts *opts;
966 opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL);
967 assert(opts);
970 static void qemu_opts_from_qdict_entry(QemuOpts *opts,
971 const QDictEntry *entry,
972 Error **errp)
974 const char *key = qdict_entry_key(entry);
975 QObject *obj = qdict_entry_value(entry);
976 char buf[32], *tmp = NULL;
977 const char *value;
979 if (!strcmp(key, "id")) {
980 return;
983 switch (qobject_type(obj)) {
984 case QTYPE_QSTRING:
985 value = qstring_get_str(qobject_to(QString, obj));
986 break;
987 case QTYPE_QNUM:
988 tmp = qnum_to_string(qobject_to(QNum, obj));
989 value = tmp;
990 break;
991 case QTYPE_QBOOL:
992 pstrcpy(buf, sizeof(buf),
993 qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
994 value = buf;
995 break;
996 default:
997 return;
1000 qemu_opt_set(opts, key, value, errp);
1001 g_free(tmp);
1005 * Create QemuOpts from a QDict.
1006 * Use value of key "id" as ID if it exists and is a QString. Only
1007 * QStrings, QNums and QBools are copied. Entries with other types
1008 * are silently ignored.
1010 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1011 Error **errp)
1013 Error *local_err = NULL;
1014 QemuOpts *opts;
1015 const QDictEntry *entry;
1017 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
1018 if (!opts) {
1019 return NULL;
1022 assert(opts != NULL);
1024 for (entry = qdict_first(qdict);
1025 entry;
1026 entry = qdict_next(qdict, entry)) {
1027 qemu_opts_from_qdict_entry(opts, entry, &local_err);
1028 if (local_err) {
1029 error_propagate(errp, local_err);
1030 qemu_opts_del(opts);
1031 return NULL;
1035 return opts;
1039 * Adds all QDict entries to the QemuOpts that can be added and removes them
1040 * from the QDict. When this function returns, the QDict contains only those
1041 * entries that couldn't be added to the QemuOpts.
1043 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1045 const QDictEntry *entry, *next;
1047 entry = qdict_first(qdict);
1049 while (entry != NULL) {
1050 Error *local_err = NULL;
1052 next = qdict_next(qdict, entry);
1054 if (find_desc_by_name(opts->list->desc, entry->key)) {
1055 qemu_opts_from_qdict_entry(opts, entry, &local_err);
1056 if (local_err) {
1057 error_propagate(errp, local_err);
1058 return;
1060 qdict_del(qdict, entry->key);
1063 entry = next;
1068 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1070 * If @list is given, only add those options to the QDict that are contained in
1071 * the list. If @del is true, any options added to the QDict are removed from
1072 * the QemuOpts, otherwise they remain there.
1074 * If two options in @opts have the same name, they are processed in order
1075 * so that the last one wins (consistent with the reverse iteration in
1076 * qemu_opt_find()), but all of them are deleted if @del is true.
1078 * TODO We'll want to use types appropriate for opt->desc->type, but
1079 * this is enough for now.
1081 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1082 QemuOptsList *list, bool del)
1084 QemuOpt *opt, *next;
1086 if (!qdict) {
1087 qdict = qdict_new();
1089 if (opts->id) {
1090 qdict_put_str(qdict, "id", opts->id);
1092 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1093 if (list) {
1094 QemuOptDesc *desc;
1095 bool found = false;
1096 for (desc = list->desc; desc->name; desc++) {
1097 if (!strcmp(desc->name, opt->name)) {
1098 found = true;
1099 break;
1102 if (!found) {
1103 continue;
1106 qdict_put_str(qdict, opt->name, opt->str);
1107 if (del) {
1108 qemu_opt_del(opt);
1111 return qdict;
1114 /* Copy all options in a QemuOpts to the given QDict. See
1115 * qemu_opts_to_qdict_filtered() for details. */
1116 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1118 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1121 /* Validate parsed opts against descriptions where no
1122 * descriptions were provided in the QemuOptsList.
1124 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1126 QemuOpt *opt;
1127 Error *local_err = NULL;
1129 assert(opts_accepts_any(opts));
1131 QTAILQ_FOREACH(opt, &opts->head, next) {
1132 opt->desc = find_desc_by_name(desc, opt->name);
1133 if (!opt->desc) {
1134 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1135 return;
1138 qemu_opt_parse(opt, &local_err);
1139 if (local_err) {
1140 error_propagate(errp, local_err);
1141 return;
1147 * For each member of @list, call @func(@opaque, member, @errp).
1148 * Call it with the current location temporarily set to the member's.
1149 * @func() may store an Error through @errp, but must return non-zero then.
1150 * When @func() returns non-zero, break the loop and return that value.
1151 * Return zero when the loop completes.
1153 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1154 void *opaque, Error **errp)
1156 Location loc;
1157 QemuOpts *opts;
1158 int rc = 0;
1160 loc_push_none(&loc);
1161 QTAILQ_FOREACH(opts, &list->head, next) {
1162 loc_restore(&opts->loc);
1163 rc = func(opaque, opts, errp);
1164 if (rc) {
1165 break;
1167 assert(!errp || !*errp);
1169 loc_pop(&loc);
1170 return rc;
1173 static size_t count_opts_list(QemuOptsList *list)
1175 QemuOptDesc *desc = NULL;
1176 size_t num_opts = 0;
1178 if (!list) {
1179 return 0;
1182 desc = list->desc;
1183 while (desc && desc->name) {
1184 num_opts++;
1185 desc++;
1188 return num_opts;
1191 void qemu_opts_free(QemuOptsList *list)
1193 g_free(list);
1196 /* Realloc dst option list and append options from an option list (list)
1197 * to it. dst could be NULL or a malloced list.
1198 * The lifetime of dst must be shorter than the input list because the
1199 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1201 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1202 QemuOptsList *list)
1204 size_t num_opts, num_dst_opts;
1205 QemuOptDesc *desc;
1206 bool need_init = false;
1207 bool need_head_update;
1209 if (!list) {
1210 return dst;
1213 /* If dst is NULL, after realloc, some area of dst should be initialized
1214 * before adding options to it.
1216 if (!dst) {
1217 need_init = true;
1218 need_head_update = true;
1219 } else {
1220 /* Moreover, even if dst is not NULL, the realloc may move it to a
1221 * different address in which case we may get a stale tail pointer
1222 * in dst->head. */
1223 need_head_update = QTAILQ_EMPTY(&dst->head);
1226 num_opts = count_opts_list(dst);
1227 num_dst_opts = num_opts;
1228 num_opts += count_opts_list(list);
1229 dst = g_realloc(dst, sizeof(QemuOptsList) +
1230 (num_opts + 1) * sizeof(QemuOptDesc));
1231 if (need_init) {
1232 dst->name = NULL;
1233 dst->implied_opt_name = NULL;
1234 dst->merge_lists = false;
1236 if (need_head_update) {
1237 QTAILQ_INIT(&dst->head);
1239 dst->desc[num_dst_opts].name = NULL;
1241 /* append list->desc to dst->desc */
1242 if (list) {
1243 desc = list->desc;
1244 while (desc && desc->name) {
1245 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1246 dst->desc[num_dst_opts++] = *desc;
1247 dst->desc[num_dst_opts].name = NULL;
1249 desc++;
1253 return dst;