Merge remote-tracking branch 'stefanha/trivial-patches' into staging
[qemu.git] / qemu-option.c
blobbb3886c6b9d73103c32d33ccd1c7601329a38c45
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.h"
31 #include "qemu-objects.h"
32 #include "qemu-option.h"
33 #include "error.h"
34 #include "qerror.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 int check_params(char *buf, int buf_size,
127 const char * const *params, const char *str)
129 const char *p;
130 int i;
132 p = str;
133 while (*p != '\0') {
134 p = get_opt_name(buf, buf_size, p, '=');
135 if (*p != '=') {
136 return -1;
138 p++;
139 for (i = 0; params[i] != NULL; i++) {
140 if (!strcmp(params[i], buf)) {
141 break;
144 if (params[i] == NULL) {
145 return -1;
147 p = get_opt_value(NULL, 0, p);
148 if (*p != ',') {
149 break;
151 p++;
153 return 0;
157 * Searches an option list for an option with the given name
159 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
160 const char *name)
162 while (list && list->name) {
163 if (!strcmp(list->name, name)) {
164 return list;
166 list++;
169 return NULL;
172 static void parse_option_bool(const char *name, const char *value, bool *ret,
173 Error **errp)
175 if (value != NULL) {
176 if (!strcmp(value, "on")) {
177 *ret = 1;
178 } else if (!strcmp(value, "off")) {
179 *ret = 0;
180 } else {
181 error_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
183 } else {
184 *ret = 1;
188 static void parse_option_number(const char *name, const char *value,
189 uint64_t *ret, Error **errp)
191 char *postfix;
192 uint64_t number;
194 if (value != NULL) {
195 number = strtoull(value, &postfix, 0);
196 if (*postfix != '\0') {
197 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
198 return;
200 *ret = number;
201 } else {
202 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
206 static void parse_option_size(const char *name, const char *value,
207 uint64_t *ret, Error **errp)
209 char *postfix;
210 double sizef;
212 if (value != NULL) {
213 sizef = strtod(value, &postfix);
214 switch (*postfix) {
215 case 'T':
216 sizef *= 1024;
217 /* fall through */
218 case 'G':
219 sizef *= 1024;
220 /* fall through */
221 case 'M':
222 sizef *= 1024;
223 /* fall through */
224 case 'K':
225 case 'k':
226 sizef *= 1024;
227 /* fall through */
228 case 'b':
229 case '\0':
230 *ret = (uint64_t) sizef;
231 break;
232 default:
233 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
234 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
235 "kilobytes, megabytes, gigabytes and terabytes.\n");
236 return;
238 } else {
239 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
244 * Sets the value of a parameter in a given option list. The parsing of the
245 * value depends on the type of option:
247 * OPT_FLAG (uses value.n):
248 * If no value is given, the flag is set to 1.
249 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
251 * OPT_STRING (uses value.s):
252 * value is strdup()ed and assigned as option value
254 * OPT_SIZE (uses value.n):
255 * The value is converted to an integer. Suffixes for kilobytes etc. are
256 * allowed (powers of 1024).
258 * Returns 0 on succes, -1 in error cases
260 int set_option_parameter(QEMUOptionParameter *list, const char *name,
261 const char *value)
263 bool flag;
264 Error *local_err = NULL;
266 // Find a matching parameter
267 list = get_option_parameter(list, name);
268 if (list == NULL) {
269 fprintf(stderr, "Unknown option '%s'\n", name);
270 return -1;
273 // Process parameter
274 switch (list->type) {
275 case OPT_FLAG:
276 parse_option_bool(name, value, &flag, &local_err);
277 if (!error_is_set(&local_err)) {
278 list->value.n = flag;
280 break;
282 case OPT_STRING:
283 if (value != NULL) {
284 list->value.s = g_strdup(value);
285 } else {
286 fprintf(stderr, "Option '%s' needs a parameter\n", name);
287 return -1;
289 break;
291 case OPT_SIZE:
292 parse_option_size(name, value, &list->value.n, &local_err);
293 break;
295 default:
296 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
297 return -1;
300 if (error_is_set(&local_err)) {
301 qerror_report_err(local_err);
302 error_free(local_err);
303 return -1;
306 return 0;
310 * Sets the given parameter to an integer instead of a string.
311 * This function cannot be used to set string options.
313 * Returns 0 on success, -1 in error cases
315 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
316 uint64_t value)
318 // Find a matching parameter
319 list = get_option_parameter(list, name);
320 if (list == NULL) {
321 fprintf(stderr, "Unknown option '%s'\n", name);
322 return -1;
325 // Process parameter
326 switch (list->type) {
327 case OPT_FLAG:
328 case OPT_NUMBER:
329 case OPT_SIZE:
330 list->value.n = value;
331 break;
333 default:
334 return -1;
337 return 0;
341 * Frees a option list. If it contains strings, the strings are freed as well.
343 void free_option_parameters(QEMUOptionParameter *list)
345 QEMUOptionParameter *cur = list;
347 while (cur && cur->name) {
348 if (cur->type == OPT_STRING) {
349 g_free(cur->value.s);
351 cur++;
354 g_free(list);
358 * Count valid options in list
360 static size_t count_option_parameters(QEMUOptionParameter *list)
362 size_t num_options = 0;
364 while (list && list->name) {
365 num_options++;
366 list++;
369 return num_options;
373 * Append an option list (list) to an option list (dest).
375 * If dest is NULL, a new copy of list is created.
377 * Returns a pointer to the first element of dest (or the newly allocated copy)
379 QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
380 QEMUOptionParameter *list)
382 size_t num_options, num_dest_options;
384 num_options = count_option_parameters(dest);
385 num_dest_options = num_options;
387 num_options += count_option_parameters(list);
389 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
390 dest[num_dest_options].name = NULL;
392 while (list && list->name) {
393 if (get_option_parameter(dest, list->name) == NULL) {
394 dest[num_dest_options++] = *list;
395 dest[num_dest_options].name = NULL;
397 list++;
400 return dest;
404 * Parses a parameter string (param) into an option list (dest).
406 * list is the template option list. If dest is NULL, a new copy of list is
407 * created. If list is NULL, this function fails.
409 * A parameter string consists of one or more parameters, separated by commas.
410 * Each parameter consists of its name and possibly of a value. In the latter
411 * case, the value is delimited by an = character. To specify a value which
412 * contains commas, double each comma so it won't be recognized as the end of
413 * the parameter.
415 * For more details of the parsing see above.
417 * Returns a pointer to the first element of dest (or the newly allocated copy)
418 * or NULL in error cases
420 QEMUOptionParameter *parse_option_parameters(const char *param,
421 QEMUOptionParameter *list, QEMUOptionParameter *dest)
423 QEMUOptionParameter *allocated = NULL;
424 char name[256];
425 char value[256];
426 char *param_delim, *value_delim;
427 char next_delim;
429 if (list == NULL) {
430 return NULL;
433 if (dest == NULL) {
434 dest = allocated = append_option_parameters(NULL, list);
437 while (*param) {
439 // Find parameter name and value in the string
440 param_delim = strchr(param, ',');
441 value_delim = strchr(param, '=');
443 if (value_delim && (value_delim < param_delim || !param_delim)) {
444 next_delim = '=';
445 } else {
446 next_delim = ',';
447 value_delim = NULL;
450 param = get_opt_name(name, sizeof(name), param, next_delim);
451 if (value_delim) {
452 param = get_opt_value(value, sizeof(value), param + 1);
454 if (*param != '\0') {
455 param++;
458 // Set the parameter
459 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
460 goto fail;
464 return dest;
466 fail:
467 // Only free the list if it was newly allocated
468 free_option_parameters(allocated);
469 return NULL;
473 * Prints all options of a list that have a value to stdout
475 void print_option_parameters(QEMUOptionParameter *list)
477 while (list && list->name) {
478 switch (list->type) {
479 case OPT_STRING:
480 if (list->value.s != NULL) {
481 printf("%s='%s' ", list->name, list->value.s);
483 break;
484 case OPT_FLAG:
485 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
486 break;
487 case OPT_SIZE:
488 case OPT_NUMBER:
489 printf("%s=%" PRId64 " ", list->name, list->value.n);
490 break;
491 default:
492 printf("%s=(unknown type) ", list->name);
493 break;
495 list++;
500 * Prints an overview of all available options
502 void print_option_help(QEMUOptionParameter *list)
504 printf("Supported options:\n");
505 while (list && list->name) {
506 printf("%-16s %s\n", list->name,
507 list->help ? list->help : "No description available");
508 list++;
512 /* ------------------------------------------------------------------ */
514 struct QemuOpt {
515 const char *name;
516 const char *str;
518 const QemuOptDesc *desc;
519 union {
520 bool boolean;
521 uint64_t uint;
522 } value;
524 QemuOpts *opts;
525 QTAILQ_ENTRY(QemuOpt) next;
528 struct QemuOpts {
529 char *id;
530 QemuOptsList *list;
531 Location loc;
532 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
533 QTAILQ_ENTRY(QemuOpts) next;
536 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
538 QemuOpt *opt;
540 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
541 if (strcmp(opt->name, name) != 0)
542 continue;
543 return opt;
545 return NULL;
548 const char *qemu_opt_get(QemuOpts *opts, const char *name)
550 QemuOpt *opt = qemu_opt_find(opts, name);
551 return opt ? opt->str : NULL;
554 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
556 QemuOpt *opt = qemu_opt_find(opts, name);
558 if (opt == NULL)
559 return defval;
560 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
561 return opt->value.boolean;
564 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
566 QemuOpt *opt = qemu_opt_find(opts, name);
568 if (opt == NULL)
569 return defval;
570 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
571 return opt->value.uint;
574 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
576 QemuOpt *opt = qemu_opt_find(opts, name);
578 if (opt == NULL)
579 return defval;
580 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
581 return opt->value.uint;
584 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
586 if (opt->desc == NULL)
587 return;
589 switch (opt->desc->type) {
590 case QEMU_OPT_STRING:
591 /* nothing */
592 return;
593 case QEMU_OPT_BOOL:
594 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
595 break;
596 case QEMU_OPT_NUMBER:
597 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
598 break;
599 case QEMU_OPT_SIZE:
600 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
601 break;
602 default:
603 abort();
607 static void qemu_opt_del(QemuOpt *opt)
609 QTAILQ_REMOVE(&opt->opts->head, opt, next);
610 g_free((/* !const */ char*)opt->name);
611 g_free((/* !const */ char*)opt->str);
612 g_free(opt);
615 static void opt_set(QemuOpts *opts, const char *name, const char *value,
616 bool prepend, Error **errp)
618 QemuOpt *opt;
619 const QemuOptDesc *desc = opts->list->desc;
620 Error *local_err = NULL;
621 int i;
623 for (i = 0; desc[i].name != NULL; i++) {
624 if (strcmp(desc[i].name, name) == 0) {
625 break;
628 if (desc[i].name == NULL) {
629 if (i == 0) {
630 /* empty list -> allow any */;
631 } else {
632 error_set(errp, QERR_INVALID_PARAMETER, name);
633 return;
637 opt = g_malloc0(sizeof(*opt));
638 opt->name = g_strdup(name);
639 opt->opts = opts;
640 if (prepend) {
641 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
642 } else {
643 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
645 if (desc[i].name != NULL) {
646 opt->desc = desc+i;
648 if (value) {
649 opt->str = g_strdup(value);
651 qemu_opt_parse(opt, &local_err);
652 if (error_is_set(&local_err)) {
653 error_propagate(errp, local_err);
654 qemu_opt_del(opt);
658 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
660 Error *local_err = NULL;
662 opt_set(opts, name, value, false, &local_err);
663 if (error_is_set(&local_err)) {
664 qerror_report_err(local_err);
665 error_free(local_err);
666 return -1;
669 return 0;
672 void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
673 Error **errp)
675 opt_set(opts, name, value, false, errp);
678 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
680 QemuOpt *opt;
681 const QemuOptDesc *desc = opts->list->desc;
682 int i;
684 for (i = 0; desc[i].name != NULL; i++) {
685 if (strcmp(desc[i].name, name) == 0) {
686 break;
689 if (desc[i].name == NULL) {
690 if (i == 0) {
691 /* empty list -> allow any */;
692 } else {
693 qerror_report(QERR_INVALID_PARAMETER, name);
694 return -1;
698 opt = g_malloc0(sizeof(*opt));
699 opt->name = g_strdup(name);
700 opt->opts = opts;
701 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
702 if (desc[i].name != NULL) {
703 opt->desc = desc+i;
705 opt->value.boolean = !!val;
706 return 0;
709 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
710 int abort_on_failure)
712 QemuOpt *opt;
713 int rc = 0;
715 QTAILQ_FOREACH(opt, &opts->head, next) {
716 rc = func(opt->name, opt->str, opaque);
717 if (abort_on_failure && rc != 0)
718 break;
720 return rc;
723 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
725 QemuOpts *opts;
727 QTAILQ_FOREACH(opts, &list->head, next) {
728 if (!opts->id) {
729 if (!id) {
730 return opts;
732 continue;
734 if (strcmp(opts->id, id) != 0) {
735 continue;
737 return opts;
739 return NULL;
742 static int id_wellformed(const char *id)
744 int i;
746 if (!qemu_isalpha(id[0])) {
747 return 0;
749 for (i = 1; id[i]; i++) {
750 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
751 return 0;
754 return 1;
757 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
758 int fail_if_exists, Error **errp)
760 QemuOpts *opts = NULL;
762 if (id) {
763 if (!id_wellformed(id)) {
764 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
765 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
766 return NULL;
768 opts = qemu_opts_find(list, id);
769 if (opts != NULL) {
770 if (fail_if_exists && !list->merge_lists) {
771 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
772 return NULL;
773 } else {
774 return opts;
777 } else if (list->merge_lists) {
778 opts = qemu_opts_find(list, NULL);
779 if (opts) {
780 return opts;
783 opts = g_malloc0(sizeof(*opts));
784 if (id) {
785 opts->id = g_strdup(id);
787 opts->list = list;
788 loc_save(&opts->loc);
789 QTAILQ_INIT(&opts->head);
790 QTAILQ_INSERT_TAIL(&list->head, opts, next);
791 return opts;
794 void qemu_opts_reset(QemuOptsList *list)
796 QemuOpts *opts, *next_opts;
798 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
799 qemu_opts_del(opts);
803 void qemu_opts_loc_restore(QemuOpts *opts)
805 loc_restore(&opts->loc);
808 int qemu_opts_set(QemuOptsList *list, const char *id,
809 const char *name, const char *value)
811 QemuOpts *opts;
812 Error *local_err = NULL;
814 opts = qemu_opts_create(list, id, 1, &local_err);
815 if (error_is_set(&local_err)) {
816 qerror_report_err(local_err);
817 error_free(local_err);
818 return -1;
820 return qemu_opt_set(opts, name, value);
823 const char *qemu_opts_id(QemuOpts *opts)
825 return opts->id;
828 void qemu_opts_del(QemuOpts *opts)
830 QemuOpt *opt;
832 for (;;) {
833 opt = QTAILQ_FIRST(&opts->head);
834 if (opt == NULL)
835 break;
836 qemu_opt_del(opt);
838 QTAILQ_REMOVE(&opts->list->head, opts, next);
839 g_free(opts->id);
840 g_free(opts);
843 int qemu_opts_print(QemuOpts *opts, void *dummy)
845 QemuOpt *opt;
847 fprintf(stderr, "%s: %s:", opts->list->name,
848 opts->id ? opts->id : "<noid>");
849 QTAILQ_FOREACH(opt, &opts->head, next) {
850 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
852 fprintf(stderr, "\n");
853 return 0;
856 static int opts_do_parse(QemuOpts *opts, const char *params,
857 const char *firstname, bool prepend)
859 char option[128], value[1024];
860 const char *p,*pe,*pc;
861 Error *local_err = NULL;
863 for (p = params; *p != '\0'; p++) {
864 pe = strchr(p, '=');
865 pc = strchr(p, ',');
866 if (!pe || (pc && pc < pe)) {
867 /* found "foo,more" */
868 if (p == params && firstname) {
869 /* implicitly named first option */
870 pstrcpy(option, sizeof(option), firstname);
871 p = get_opt_value(value, sizeof(value), p);
872 } else {
873 /* option without value, probably a flag */
874 p = get_opt_name(option, sizeof(option), p, ',');
875 if (strncmp(option, "no", 2) == 0) {
876 memmove(option, option+2, strlen(option+2)+1);
877 pstrcpy(value, sizeof(value), "off");
878 } else {
879 pstrcpy(value, sizeof(value), "on");
882 } else {
883 /* found "foo=bar,more" */
884 p = get_opt_name(option, sizeof(option), p, '=');
885 if (*p != '=') {
886 break;
888 p++;
889 p = get_opt_value(value, sizeof(value), p);
891 if (strcmp(option, "id") != 0) {
892 /* store and parse */
893 opt_set(opts, option, value, prepend, &local_err);
894 if (error_is_set(&local_err)) {
895 qerror_report_err(local_err);
896 error_free(local_err);
897 return -1;
900 if (*p != ',') {
901 break;
904 return 0;
907 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
909 return opts_do_parse(opts, params, firstname, false);
912 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
913 int permit_abbrev, bool defaults)
915 const char *firstname;
916 char value[1024], *id = NULL;
917 const char *p;
918 QemuOpts *opts;
919 Error *local_err = NULL;
921 assert(!permit_abbrev || list->implied_opt_name);
922 firstname = permit_abbrev ? list->implied_opt_name : NULL;
924 if (strncmp(params, "id=", 3) == 0) {
925 get_opt_value(value, sizeof(value), params+3);
926 id = value;
927 } else if ((p = strstr(params, ",id=")) != NULL) {
928 get_opt_value(value, sizeof(value), p+4);
929 id = value;
931 if (defaults) {
932 if (!id && !QTAILQ_EMPTY(&list->head)) {
933 opts = qemu_opts_find(list, NULL);
934 } else {
935 opts = qemu_opts_create(list, id, 0, &local_err);
937 } else {
938 opts = qemu_opts_create(list, id, 1, &local_err);
940 if (opts == NULL) {
941 if (error_is_set(&local_err)) {
942 qerror_report_err(local_err);
943 error_free(local_err);
945 return NULL;
948 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
949 qemu_opts_del(opts);
950 return NULL;
953 return opts;
956 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
957 int permit_abbrev)
959 return opts_parse(list, params, permit_abbrev, false);
962 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
963 int permit_abbrev)
965 QemuOpts *opts;
967 opts = opts_parse(list, params, permit_abbrev, true);
968 assert(opts);
971 typedef struct OptsFromQDictState {
972 QemuOpts *opts;
973 Error **errp;
974 } OptsFromQDictState;
976 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
978 OptsFromQDictState *state = opaque;
979 char buf[32];
980 const char *value;
981 int n;
983 if (!strcmp(key, "id") || error_is_set(state->errp)) {
984 return;
987 switch (qobject_type(obj)) {
988 case QTYPE_QSTRING:
989 value = qstring_get_str(qobject_to_qstring(obj));
990 break;
991 case QTYPE_QINT:
992 n = snprintf(buf, sizeof(buf), "%" PRId64,
993 qint_get_int(qobject_to_qint(obj)));
994 assert(n < sizeof(buf));
995 value = buf;
996 break;
997 case QTYPE_QFLOAT:
998 n = snprintf(buf, sizeof(buf), "%.17g",
999 qfloat_get_double(qobject_to_qfloat(obj)));
1000 assert(n < sizeof(buf));
1001 value = buf;
1002 break;
1003 case QTYPE_QBOOL:
1004 pstrcpy(buf, sizeof(buf),
1005 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
1006 value = buf;
1007 break;
1008 default:
1009 return;
1012 qemu_opt_set_err(state->opts, key, value, state->errp);
1016 * Create QemuOpts from a QDict.
1017 * Use value of key "id" as ID if it exists and is a QString.
1018 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1019 * other types are silently ignored.
1021 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1022 Error **errp)
1024 OptsFromQDictState state;
1025 Error *local_err = NULL;
1026 QemuOpts *opts;
1028 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1029 &local_err);
1030 if (error_is_set(&local_err)) {
1031 error_propagate(errp, local_err);
1032 return NULL;
1035 assert(opts != NULL);
1037 state.errp = &local_err;
1038 state.opts = opts;
1039 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
1040 if (error_is_set(&local_err)) {
1041 error_propagate(errp, local_err);
1042 qemu_opts_del(opts);
1043 return NULL;
1046 return opts;
1050 * Convert from QemuOpts to QDict.
1051 * The QDict values are of type QString.
1052 * TODO We'll want to use types appropriate for opt->desc->type, but
1053 * this is enough for now.
1055 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1057 QemuOpt *opt;
1058 QObject *val;
1060 if (!qdict) {
1061 qdict = qdict_new();
1063 if (opts->id) {
1064 qdict_put(qdict, "id", qstring_from_str(opts->id));
1066 QTAILQ_FOREACH(opt, &opts->head, next) {
1067 val = QOBJECT(qstring_from_str(opt->str));
1068 qdict_put_obj(qdict, opt->name, val);
1070 return qdict;
1073 /* Validate parsed opts against descriptions where no
1074 * descriptions were provided in the QemuOptsList.
1076 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1078 QemuOpt *opt;
1079 Error *local_err = NULL;
1081 assert(opts->list->desc[0].name == NULL);
1083 QTAILQ_FOREACH(opt, &opts->head, next) {
1084 int i;
1086 for (i = 0; desc[i].name != NULL; i++) {
1087 if (strcmp(desc[i].name, opt->name) == 0) {
1088 break;
1091 if (desc[i].name == NULL) {
1092 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1093 return;
1096 opt->desc = &desc[i];
1098 qemu_opt_parse(opt, &local_err);
1099 if (error_is_set(&local_err)) {
1100 error_propagate(errp, local_err);
1101 return;
1106 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1107 int abort_on_failure)
1109 Location loc;
1110 QemuOpts *opts;
1111 int rc = 0;
1113 loc_push_none(&loc);
1114 QTAILQ_FOREACH(opts, &list->head, next) {
1115 loc_restore(&opts->loc);
1116 rc |= func(opts, opaque);
1117 if (abort_on_failure && rc != 0)
1118 break;
1120 loc_pop(&loc);
1121 return rc;