cleanup QEMUOptionParameter
[qemu/ar7.git] / util / qemu-option.c
blob8ac8111ea892f9f0541bebb449be5f4789767909
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_malloc0(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 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_malloc0(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 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 = qemu_opt_find(opts, name);
315 if (!opt) {
316 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
317 if (desc && desc->def_value_str) {
318 return desc->def_value_str;
321 return opt ? opt->str : NULL;
324 /* Get a known option (or its default) and remove it from the list
325 * all in one action. Return a malloced string of the option value.
326 * Result must be freed by caller with g_free().
328 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
330 QemuOpt *opt;
331 const QemuOptDesc *desc;
332 char *str = NULL;
334 if (opts == NULL) {
335 return NULL;
338 opt = qemu_opt_find(opts, name);
339 if (!opt) {
340 desc = find_desc_by_name(opts->list->desc, name);
341 if (desc && desc->def_value_str) {
342 str = g_strdup(desc->def_value_str);
344 return str;
346 str = opt->str;
347 opt->str = NULL;
348 qemu_opt_del_all(opts, name);
349 return str;
352 bool qemu_opt_has_help_opt(QemuOpts *opts)
354 QemuOpt *opt;
356 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
357 if (is_help_option(opt->name)) {
358 return true;
361 return false;
364 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
365 bool defval, bool del)
367 QemuOpt *opt = qemu_opt_find(opts, name);
368 bool ret = defval;
370 if (opt == NULL) {
371 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
372 if (desc && desc->def_value_str) {
373 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
375 return ret;
377 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
378 ret = opt->value.boolean;
379 if (del) {
380 qemu_opt_del_all(opts, name);
382 return ret;
385 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
387 return qemu_opt_get_bool_helper(opts, name, defval, false);
390 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
392 return qemu_opt_get_bool_helper(opts, name, defval, true);
395 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
396 uint64_t defval, bool del)
398 QemuOpt *opt = qemu_opt_find(opts, name);
399 uint64_t ret = defval;
401 if (opt == NULL) {
402 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
403 if (desc && desc->def_value_str) {
404 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
406 return ret;
408 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
409 ret = opt->value.uint;
410 if (del) {
411 qemu_opt_del_all(opts, name);
413 return ret;
416 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
418 return qemu_opt_get_number_helper(opts, name, defval, false);
421 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
422 uint64_t defval)
424 return qemu_opt_get_number_helper(opts, name, defval, true);
427 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
428 uint64_t defval, bool del)
430 QemuOpt *opt = qemu_opt_find(opts, name);
431 uint64_t ret = defval;
433 if (opt == NULL) {
434 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
435 if (desc && desc->def_value_str) {
436 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
438 return ret;
440 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
441 ret = opt->value.uint;
442 if (del) {
443 qemu_opt_del_all(opts, name);
445 return ret;
448 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
450 return qemu_opt_get_size_helper(opts, name, defval, false);
453 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
454 uint64_t defval)
456 return qemu_opt_get_size_helper(opts, name, defval, true);
459 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
461 if (opt->desc == NULL)
462 return;
464 switch (opt->desc->type) {
465 case QEMU_OPT_STRING:
466 /* nothing */
467 return;
468 case QEMU_OPT_BOOL:
469 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
470 break;
471 case QEMU_OPT_NUMBER:
472 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
473 break;
474 case QEMU_OPT_SIZE:
475 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
476 break;
477 default:
478 abort();
482 static bool opts_accepts_any(const QemuOpts *opts)
484 return opts->list->desc[0].name == NULL;
487 int qemu_opt_unset(QemuOpts *opts, const char *name)
489 QemuOpt *opt = qemu_opt_find(opts, name);
491 assert(opts_accepts_any(opts));
493 if (opt == NULL) {
494 return -1;
495 } else {
496 qemu_opt_del(opt);
497 return 0;
501 static void opt_set(QemuOpts *opts, const char *name, const char *value,
502 bool prepend, Error **errp)
504 QemuOpt *opt;
505 const QemuOptDesc *desc;
506 Error *local_err = NULL;
508 desc = find_desc_by_name(opts->list->desc, name);
509 if (!desc && !opts_accepts_any(opts)) {
510 error_set(errp, QERR_INVALID_PARAMETER, name);
511 return;
514 opt = g_malloc0(sizeof(*opt));
515 opt->name = g_strdup(name);
516 opt->opts = opts;
517 if (prepend) {
518 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
519 } else {
520 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
522 opt->desc = desc;
523 opt->str = g_strdup(value);
524 qemu_opt_parse(opt, &local_err);
525 if (local_err) {
526 error_propagate(errp, local_err);
527 qemu_opt_del(opt);
531 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
533 Error *local_err = NULL;
535 opt_set(opts, name, value, false, &local_err);
536 if (local_err) {
537 qerror_report_err(local_err);
538 error_free(local_err);
539 return -1;
542 return 0;
545 void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
546 Error **errp)
548 opt_set(opts, name, value, false, errp);
551 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
553 QemuOpt *opt;
554 const QemuOptDesc *desc = opts->list->desc;
556 opt = g_malloc0(sizeof(*opt));
557 opt->desc = find_desc_by_name(desc, name);
558 if (!opt->desc && !opts_accepts_any(opts)) {
559 qerror_report(QERR_INVALID_PARAMETER, name);
560 g_free(opt);
561 return -1;
564 opt->name = g_strdup(name);
565 opt->opts = opts;
566 opt->value.boolean = !!val;
567 opt->str = g_strdup(val ? "on" : "off");
568 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
570 return 0;
573 int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
575 QemuOpt *opt;
576 const QemuOptDesc *desc = opts->list->desc;
578 opt = g_malloc0(sizeof(*opt));
579 opt->desc = find_desc_by_name(desc, name);
580 if (!opt->desc && !opts_accepts_any(opts)) {
581 qerror_report(QERR_INVALID_PARAMETER, name);
582 g_free(opt);
583 return -1;
586 opt->name = g_strdup(name);
587 opt->opts = opts;
588 opt->value.uint = val;
589 opt->str = g_strdup_printf("%" PRId64, val);
590 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
592 return 0;
595 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
596 int abort_on_failure)
598 QemuOpt *opt;
599 int rc = 0;
601 QTAILQ_FOREACH(opt, &opts->head, next) {
602 rc = func(opt->name, opt->str, opaque);
603 if (abort_on_failure && rc != 0)
604 break;
606 return rc;
609 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
611 QemuOpts *opts;
613 QTAILQ_FOREACH(opts, &list->head, next) {
614 if (!opts->id && !id) {
615 return opts;
617 if (opts->id && id && !strcmp(opts->id, id)) {
618 return opts;
621 return NULL;
624 static int id_wellformed(const char *id)
626 int i;
628 if (!qemu_isalpha(id[0])) {
629 return 0;
631 for (i = 1; id[i]; i++) {
632 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
633 return 0;
636 return 1;
639 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
640 int fail_if_exists, Error **errp)
642 QemuOpts *opts = NULL;
644 if (id) {
645 if (!id_wellformed(id)) {
646 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
647 #if 0 /* conversion from qerror_report() to error_set() broke this: */
648 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
649 #endif
650 return NULL;
652 opts = qemu_opts_find(list, id);
653 if (opts != NULL) {
654 if (fail_if_exists && !list->merge_lists) {
655 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
656 return NULL;
657 } else {
658 return opts;
661 } else if (list->merge_lists) {
662 opts = qemu_opts_find(list, NULL);
663 if (opts) {
664 return opts;
667 opts = g_malloc0(sizeof(*opts));
668 opts->id = g_strdup(id);
669 opts->list = list;
670 loc_save(&opts->loc);
671 QTAILQ_INIT(&opts->head);
672 QTAILQ_INSERT_TAIL(&list->head, opts, next);
673 return opts;
676 void qemu_opts_reset(QemuOptsList *list)
678 QemuOpts *opts, *next_opts;
680 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
681 qemu_opts_del(opts);
685 void qemu_opts_loc_restore(QemuOpts *opts)
687 loc_restore(&opts->loc);
690 int qemu_opts_set(QemuOptsList *list, const char *id,
691 const char *name, const char *value)
693 QemuOpts *opts;
694 Error *local_err = NULL;
696 opts = qemu_opts_create(list, id, 1, &local_err);
697 if (local_err) {
698 qerror_report_err(local_err);
699 error_free(local_err);
700 return -1;
702 return qemu_opt_set(opts, name, value);
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 void qemu_opts_print(QemuOpts *opts)
737 QemuOpt *opt;
738 QemuOptDesc *desc = opts->list->desc;
740 if (desc[0].name == NULL) {
741 QTAILQ_FOREACH(opt, &opts->head, next) {
742 printf("%s=\"%s\" ", opt->name, opt->str);
744 return;
746 for (; desc && desc->name; desc++) {
747 const char *value;
748 QemuOpt *opt = qemu_opt_find(opts, desc->name);
750 value = opt ? opt->str : desc->def_value_str;
751 if (!value) {
752 continue;
754 if (desc->type == QEMU_OPT_STRING) {
755 printf("%s='%s' ", desc->name, value);
756 } else if ((desc->type == QEMU_OPT_SIZE ||
757 desc->type == QEMU_OPT_NUMBER) && opt) {
758 printf("%s=%" PRId64 " ", desc->name, opt->value.uint);
759 } else {
760 printf("%s=%s ", desc->name, value);
765 static int opts_do_parse(QemuOpts *opts, const char *params,
766 const char *firstname, bool prepend)
768 char option[128], value[1024];
769 const char *p,*pe,*pc;
770 Error *local_err = NULL;
772 for (p = params; *p != '\0'; p++) {
773 pe = strchr(p, '=');
774 pc = strchr(p, ',');
775 if (!pe || (pc && pc < pe)) {
776 /* found "foo,more" */
777 if (p == params && firstname) {
778 /* implicitly named first option */
779 pstrcpy(option, sizeof(option), firstname);
780 p = get_opt_value(value, sizeof(value), p);
781 } else {
782 /* option without value, probably a flag */
783 p = get_opt_name(option, sizeof(option), p, ',');
784 if (strncmp(option, "no", 2) == 0) {
785 memmove(option, option+2, strlen(option+2)+1);
786 pstrcpy(value, sizeof(value), "off");
787 } else {
788 pstrcpy(value, sizeof(value), "on");
791 } else {
792 /* found "foo=bar,more" */
793 p = get_opt_name(option, sizeof(option), p, '=');
794 if (*p != '=') {
795 break;
797 p++;
798 p = get_opt_value(value, sizeof(value), p);
800 if (strcmp(option, "id") != 0) {
801 /* store and parse */
802 opt_set(opts, option, value, prepend, &local_err);
803 if (local_err) {
804 qerror_report_err(local_err);
805 error_free(local_err);
806 return -1;
809 if (*p != ',') {
810 break;
813 return 0;
816 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
818 return opts_do_parse(opts, params, firstname, false);
821 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
822 int permit_abbrev, bool defaults)
824 const char *firstname;
825 char value[1024], *id = NULL;
826 const char *p;
827 QemuOpts *opts;
828 Error *local_err = NULL;
830 assert(!permit_abbrev || list->implied_opt_name);
831 firstname = permit_abbrev ? list->implied_opt_name : NULL;
833 if (strncmp(params, "id=", 3) == 0) {
834 get_opt_value(value, sizeof(value), params+3);
835 id = value;
836 } else if ((p = strstr(params, ",id=")) != NULL) {
837 get_opt_value(value, sizeof(value), p+4);
838 id = value;
842 * This code doesn't work for defaults && !list->merge_lists: when
843 * params has no id=, and list has an element with !opts->id, it
844 * appends a new element instead of returning the existing opts.
845 * However, we got no use for this case. Guard against possible
846 * (if unlikely) future misuse:
848 assert(!defaults || list->merge_lists);
849 opts = qemu_opts_create(list, id, !defaults, &local_err);
850 if (opts == NULL) {
851 if (local_err) {
852 qerror_report_err(local_err);
853 error_free(local_err);
855 return NULL;
858 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
859 qemu_opts_del(opts);
860 return NULL;
863 return opts;
866 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
867 int permit_abbrev)
869 return opts_parse(list, params, permit_abbrev, false);
872 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
873 int permit_abbrev)
875 QemuOpts *opts;
877 opts = opts_parse(list, params, permit_abbrev, true);
878 assert(opts);
881 typedef struct OptsFromQDictState {
882 QemuOpts *opts;
883 Error **errp;
884 } OptsFromQDictState;
886 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
888 OptsFromQDictState *state = opaque;
889 char buf[32];
890 const char *value;
891 int n;
893 if (!strcmp(key, "id") || *state->errp) {
894 return;
897 switch (qobject_type(obj)) {
898 case QTYPE_QSTRING:
899 value = qstring_get_str(qobject_to_qstring(obj));
900 break;
901 case QTYPE_QINT:
902 n = snprintf(buf, sizeof(buf), "%" PRId64,
903 qint_get_int(qobject_to_qint(obj)));
904 assert(n < sizeof(buf));
905 value = buf;
906 break;
907 case QTYPE_QFLOAT:
908 n = snprintf(buf, sizeof(buf), "%.17g",
909 qfloat_get_double(qobject_to_qfloat(obj)));
910 assert(n < sizeof(buf));
911 value = buf;
912 break;
913 case QTYPE_QBOOL:
914 pstrcpy(buf, sizeof(buf),
915 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
916 value = buf;
917 break;
918 default:
919 return;
922 qemu_opt_set_err(state->opts, key, value, state->errp);
926 * Create QemuOpts from a QDict.
927 * Use value of key "id" as ID if it exists and is a QString.
928 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
929 * other types are silently ignored.
931 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
932 Error **errp)
934 OptsFromQDictState state;
935 Error *local_err = NULL;
936 QemuOpts *opts;
938 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
939 &local_err);
940 if (local_err) {
941 error_propagate(errp, local_err);
942 return NULL;
945 assert(opts != NULL);
947 state.errp = &local_err;
948 state.opts = opts;
949 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
950 if (local_err) {
951 error_propagate(errp, local_err);
952 qemu_opts_del(opts);
953 return NULL;
956 return opts;
960 * Adds all QDict entries to the QemuOpts that can be added and removes them
961 * from the QDict. When this function returns, the QDict contains only those
962 * entries that couldn't be added to the QemuOpts.
964 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
966 const QDictEntry *entry, *next;
968 entry = qdict_first(qdict);
970 while (entry != NULL) {
971 Error *local_err = NULL;
972 OptsFromQDictState state = {
973 .errp = &local_err,
974 .opts = opts,
977 next = qdict_next(qdict, entry);
979 if (find_desc_by_name(opts->list->desc, entry->key)) {
980 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
981 if (local_err) {
982 error_propagate(errp, local_err);
983 return;
984 } else {
985 qdict_del(qdict, entry->key);
989 entry = next;
994 * Convert from QemuOpts to QDict.
995 * The QDict values are of type QString.
996 * TODO We'll want to use types appropriate for opt->desc->type, but
997 * this is enough for now.
999 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1001 QemuOpt *opt;
1002 QObject *val;
1004 if (!qdict) {
1005 qdict = qdict_new();
1007 if (opts->id) {
1008 qdict_put(qdict, "id", qstring_from_str(opts->id));
1010 QTAILQ_FOREACH(opt, &opts->head, next) {
1011 val = QOBJECT(qstring_from_str(opt->str));
1012 qdict_put_obj(qdict, opt->name, val);
1014 return qdict;
1017 /* Validate parsed opts against descriptions where no
1018 * descriptions were provided in the QemuOptsList.
1020 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1022 QemuOpt *opt;
1023 Error *local_err = NULL;
1025 assert(opts_accepts_any(opts));
1027 QTAILQ_FOREACH(opt, &opts->head, next) {
1028 opt->desc = find_desc_by_name(desc, opt->name);
1029 if (!opt->desc) {
1030 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1031 return;
1034 qemu_opt_parse(opt, &local_err);
1035 if (local_err) {
1036 error_propagate(errp, local_err);
1037 return;
1042 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1043 int abort_on_failure)
1045 Location loc;
1046 QemuOpts *opts;
1047 int rc = 0;
1049 loc_push_none(&loc);
1050 QTAILQ_FOREACH(opts, &list->head, next) {
1051 loc_restore(&opts->loc);
1052 rc |= func(opts, opaque);
1053 if (abort_on_failure && rc != 0)
1054 break;
1056 loc_pop(&loc);
1057 return rc;
1060 static size_t count_opts_list(QemuOptsList *list)
1062 QemuOptDesc *desc = NULL;
1063 size_t num_opts = 0;
1065 if (!list) {
1066 return 0;
1069 desc = list->desc;
1070 while (desc && desc->name) {
1071 num_opts++;
1072 desc++;
1075 return num_opts;
1078 void qemu_opts_free(QemuOptsList *list)
1080 /* List members point to new malloced space and need to be freed.
1081 * FIXME:
1082 * Introduced for QEMUOptionParamter->QemuOpts conversion.
1083 * Will remove after all drivers switch to QemuOpts.
1085 if (list && list->allocated) {
1086 QemuOptDesc *desc = list->desc;
1087 while (desc && desc->name) {
1088 g_free((char *)desc->name);
1089 g_free((char *)desc->help);
1090 g_free((char *)desc->def_value_str);
1091 desc++;
1095 g_free(list);
1098 /* Realloc dst option list and append options from an option list (list)
1099 * to it. dst could be NULL or a malloced list.
1101 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1102 QemuOptsList *list)
1104 size_t num_opts, num_dst_opts;
1105 QemuOptDesc *desc;
1106 bool need_init = false;
1108 if (!list) {
1109 return dst;
1112 /* If dst is NULL, after realloc, some area of dst should be initialized
1113 * before adding options to it.
1115 if (!dst) {
1116 need_init = true;
1119 num_opts = count_opts_list(dst);
1120 num_dst_opts = num_opts;
1121 num_opts += count_opts_list(list);
1122 dst = g_realloc(dst, sizeof(QemuOptsList) +
1123 (num_opts + 1) * sizeof(QemuOptDesc));
1124 if (need_init) {
1125 dst->name = NULL;
1126 dst->implied_opt_name = NULL;
1127 QTAILQ_INIT(&dst->head);
1128 dst->allocated = true;
1129 dst->merge_lists = false;
1131 dst->desc[num_dst_opts].name = NULL;
1133 /* (const char *) members of result dst are malloced, need free. */
1134 assert(dst->allocated);
1135 /* append list->desc to dst->desc */
1136 if (list) {
1137 desc = list->desc;
1138 while (desc && desc->name) {
1139 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1140 dst->desc[num_dst_opts].name = g_strdup(desc->name);
1141 dst->desc[num_dst_opts].type = desc->type;
1142 dst->desc[num_dst_opts].help = g_strdup(desc->help);
1143 dst->desc[num_dst_opts].def_value_str =
1144 g_strdup(desc->def_value_str);
1145 num_dst_opts++;
1146 dst->desc[num_dst_opts].name = NULL;
1148 desc++;
1152 return dst;