qemu-option: qemu_opts_create(): use error_set()
[qemu.git] / qemu-option.c
blob9f531c8d01cc11db6aedffa980933827288a3b30
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 int parse_option_bool(const char *name, const char *value, bool *ret)
174 if (value != NULL) {
175 if (!strcmp(value, "on")) {
176 *ret = 1;
177 } else if (!strcmp(value, "off")) {
178 *ret = 0;
179 } else {
180 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
181 return -1;
183 } else {
184 *ret = 1;
186 return 0;
189 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
191 char *postfix;
192 uint64_t number;
194 if (value != NULL) {
195 number = strtoull(value, &postfix, 0);
196 if (*postfix != '\0') {
197 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
198 return -1;
200 *ret = number;
201 } else {
202 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
203 return -1;
205 return 0;
208 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
210 char *postfix;
211 double sizef;
213 if (value != NULL) {
214 sizef = strtod(value, &postfix);
215 switch (*postfix) {
216 case 'T':
217 sizef *= 1024;
218 /* fall through */
219 case 'G':
220 sizef *= 1024;
221 /* fall through */
222 case 'M':
223 sizef *= 1024;
224 /* fall through */
225 case 'K':
226 case 'k':
227 sizef *= 1024;
228 /* fall through */
229 case 'b':
230 case '\0':
231 *ret = (uint64_t) sizef;
232 break;
233 default:
234 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
235 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
236 "kilobytes, megabytes, gigabytes and terabytes.\n");
237 return -1;
239 } else {
240 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
241 return -1;
243 return 0;
247 * Sets the value of a parameter in a given option list. The parsing of the
248 * value depends on the type of option:
250 * OPT_FLAG (uses value.n):
251 * If no value is given, the flag is set to 1.
252 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
254 * OPT_STRING (uses value.s):
255 * value is strdup()ed and assigned as option value
257 * OPT_SIZE (uses value.n):
258 * The value is converted to an integer. Suffixes for kilobytes etc. are
259 * allowed (powers of 1024).
261 * Returns 0 on succes, -1 in error cases
263 int set_option_parameter(QEMUOptionParameter *list, const char *name,
264 const char *value)
266 bool flag;
268 // Find a matching parameter
269 list = get_option_parameter(list, name);
270 if (list == NULL) {
271 fprintf(stderr, "Unknown option '%s'\n", name);
272 return -1;
275 // Process parameter
276 switch (list->type) {
277 case OPT_FLAG:
278 if (parse_option_bool(name, value, &flag) == -1)
279 return -1;
280 list->value.n = flag;
281 break;
283 case OPT_STRING:
284 if (value != NULL) {
285 list->value.s = g_strdup(value);
286 } else {
287 fprintf(stderr, "Option '%s' needs a parameter\n", name);
288 return -1;
290 break;
292 case OPT_SIZE:
293 if (parse_option_size(name, value, &list->value.n) == -1)
294 return -1;
295 break;
297 default:
298 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
299 return -1;
302 return 0;
306 * Sets the given parameter to an integer instead of a string.
307 * This function cannot be used to set string options.
309 * Returns 0 on success, -1 in error cases
311 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
312 uint64_t value)
314 // Find a matching parameter
315 list = get_option_parameter(list, name);
316 if (list == NULL) {
317 fprintf(stderr, "Unknown option '%s'\n", name);
318 return -1;
321 // Process parameter
322 switch (list->type) {
323 case OPT_FLAG:
324 case OPT_NUMBER:
325 case OPT_SIZE:
326 list->value.n = value;
327 break;
329 default:
330 return -1;
333 return 0;
337 * Frees a option list. If it contains strings, the strings are freed as well.
339 void free_option_parameters(QEMUOptionParameter *list)
341 QEMUOptionParameter *cur = list;
343 while (cur && cur->name) {
344 if (cur->type == OPT_STRING) {
345 g_free(cur->value.s);
347 cur++;
350 g_free(list);
354 * Count valid options in list
356 static size_t count_option_parameters(QEMUOptionParameter *list)
358 size_t num_options = 0;
360 while (list && list->name) {
361 num_options++;
362 list++;
365 return num_options;
369 * Append an option list (list) to an option list (dest).
371 * If dest is NULL, a new copy of list is created.
373 * Returns a pointer to the first element of dest (or the newly allocated copy)
375 QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
376 QEMUOptionParameter *list)
378 size_t num_options, num_dest_options;
380 num_options = count_option_parameters(dest);
381 num_dest_options = num_options;
383 num_options += count_option_parameters(list);
385 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
386 dest[num_dest_options].name = NULL;
388 while (list && list->name) {
389 if (get_option_parameter(dest, list->name) == NULL) {
390 dest[num_dest_options++] = *list;
391 dest[num_dest_options].name = NULL;
393 list++;
396 return dest;
400 * Parses a parameter string (param) into an option list (dest).
402 * list is the template option list. If dest is NULL, a new copy of list is
403 * created. If list is NULL, this function fails.
405 * A parameter string consists of one or more parameters, separated by commas.
406 * Each parameter consists of its name and possibly of a value. In the latter
407 * case, the value is delimited by an = character. To specify a value which
408 * contains commas, double each comma so it won't be recognized as the end of
409 * the parameter.
411 * For more details of the parsing see above.
413 * Returns a pointer to the first element of dest (or the newly allocated copy)
414 * or NULL in error cases
416 QEMUOptionParameter *parse_option_parameters(const char *param,
417 QEMUOptionParameter *list, QEMUOptionParameter *dest)
419 QEMUOptionParameter *allocated = NULL;
420 char name[256];
421 char value[256];
422 char *param_delim, *value_delim;
423 char next_delim;
425 if (list == NULL) {
426 return NULL;
429 if (dest == NULL) {
430 dest = allocated = append_option_parameters(NULL, list);
433 while (*param) {
435 // Find parameter name and value in the string
436 param_delim = strchr(param, ',');
437 value_delim = strchr(param, '=');
439 if (value_delim && (value_delim < param_delim || !param_delim)) {
440 next_delim = '=';
441 } else {
442 next_delim = ',';
443 value_delim = NULL;
446 param = get_opt_name(name, sizeof(name), param, next_delim);
447 if (value_delim) {
448 param = get_opt_value(value, sizeof(value), param + 1);
450 if (*param != '\0') {
451 param++;
454 // Set the parameter
455 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
456 goto fail;
460 return dest;
462 fail:
463 // Only free the list if it was newly allocated
464 free_option_parameters(allocated);
465 return NULL;
469 * Prints all options of a list that have a value to stdout
471 void print_option_parameters(QEMUOptionParameter *list)
473 while (list && list->name) {
474 switch (list->type) {
475 case OPT_STRING:
476 if (list->value.s != NULL) {
477 printf("%s='%s' ", list->name, list->value.s);
479 break;
480 case OPT_FLAG:
481 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
482 break;
483 case OPT_SIZE:
484 case OPT_NUMBER:
485 printf("%s=%" PRId64 " ", list->name, list->value.n);
486 break;
487 default:
488 printf("%s=(unknown type) ", list->name);
489 break;
491 list++;
496 * Prints an overview of all available options
498 void print_option_help(QEMUOptionParameter *list)
500 printf("Supported options:\n");
501 while (list && list->name) {
502 printf("%-16s %s\n", list->name,
503 list->help ? list->help : "No description available");
504 list++;
508 /* ------------------------------------------------------------------ */
510 struct QemuOpt {
511 const char *name;
512 const char *str;
514 const QemuOptDesc *desc;
515 union {
516 bool boolean;
517 uint64_t uint;
518 } value;
520 QemuOpts *opts;
521 QTAILQ_ENTRY(QemuOpt) next;
524 struct QemuOpts {
525 char *id;
526 QemuOptsList *list;
527 Location loc;
528 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
529 QTAILQ_ENTRY(QemuOpts) next;
532 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
534 QemuOpt *opt;
536 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
537 if (strcmp(opt->name, name) != 0)
538 continue;
539 return opt;
541 return NULL;
544 const char *qemu_opt_get(QemuOpts *opts, const char *name)
546 QemuOpt *opt = qemu_opt_find(opts, name);
547 return opt ? opt->str : NULL;
550 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
552 QemuOpt *opt = qemu_opt_find(opts, name);
554 if (opt == NULL)
555 return defval;
556 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
557 return opt->value.boolean;
560 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
562 QemuOpt *opt = qemu_opt_find(opts, name);
564 if (opt == NULL)
565 return defval;
566 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
567 return opt->value.uint;
570 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
572 QemuOpt *opt = qemu_opt_find(opts, name);
574 if (opt == NULL)
575 return defval;
576 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
577 return opt->value.uint;
580 static int qemu_opt_parse(QemuOpt *opt)
582 if (opt->desc == NULL)
583 return 0;
584 switch (opt->desc->type) {
585 case QEMU_OPT_STRING:
586 /* nothing */
587 return 0;
588 case QEMU_OPT_BOOL:
589 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
590 case QEMU_OPT_NUMBER:
591 return parse_option_number(opt->name, opt->str, &opt->value.uint);
592 case QEMU_OPT_SIZE:
593 return parse_option_size(opt->name, opt->str, &opt->value.uint);
594 default:
595 abort();
599 static void qemu_opt_del(QemuOpt *opt)
601 QTAILQ_REMOVE(&opt->opts->head, opt, next);
602 g_free((/* !const */ char*)opt->name);
603 g_free((/* !const */ char*)opt->str);
604 g_free(opt);
607 static int opt_set(QemuOpts *opts, const char *name, const char *value,
608 bool prepend)
610 QemuOpt *opt;
611 const QemuOptDesc *desc = opts->list->desc;
612 int i;
614 for (i = 0; desc[i].name != NULL; i++) {
615 if (strcmp(desc[i].name, name) == 0) {
616 break;
619 if (desc[i].name == NULL) {
620 if (i == 0) {
621 /* empty list -> allow any */;
622 } else {
623 qerror_report(QERR_INVALID_PARAMETER, name);
624 return -1;
628 opt = g_malloc0(sizeof(*opt));
629 opt->name = g_strdup(name);
630 opt->opts = opts;
631 if (prepend) {
632 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
633 } else {
634 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
636 if (desc[i].name != NULL) {
637 opt->desc = desc+i;
639 if (value) {
640 opt->str = g_strdup(value);
642 if (qemu_opt_parse(opt) < 0) {
643 qemu_opt_del(opt);
644 return -1;
646 return 0;
649 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
651 return opt_set(opts, name, value, false);
654 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
656 QemuOpt *opt;
657 const QemuOptDesc *desc = opts->list->desc;
658 int i;
660 for (i = 0; desc[i].name != NULL; i++) {
661 if (strcmp(desc[i].name, name) == 0) {
662 break;
665 if (desc[i].name == NULL) {
666 if (i == 0) {
667 /* empty list -> allow any */;
668 } else {
669 qerror_report(QERR_INVALID_PARAMETER, name);
670 return -1;
674 opt = g_malloc0(sizeof(*opt));
675 opt->name = g_strdup(name);
676 opt->opts = opts;
677 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
678 if (desc[i].name != NULL) {
679 opt->desc = desc+i;
681 opt->value.boolean = !!val;
682 return 0;
685 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
686 int abort_on_failure)
688 QemuOpt *opt;
689 int rc = 0;
691 QTAILQ_FOREACH(opt, &opts->head, next) {
692 rc = func(opt->name, opt->str, opaque);
693 if (abort_on_failure && rc != 0)
694 break;
696 return rc;
699 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
701 QemuOpts *opts;
703 QTAILQ_FOREACH(opts, &list->head, next) {
704 if (!opts->id) {
705 if (!id) {
706 return opts;
708 continue;
710 if (strcmp(opts->id, id) != 0) {
711 continue;
713 return opts;
715 return NULL;
718 static int id_wellformed(const char *id)
720 int i;
722 if (!qemu_isalpha(id[0])) {
723 return 0;
725 for (i = 1; id[i]; i++) {
726 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
727 return 0;
730 return 1;
733 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
734 int fail_if_exists, Error **errp)
736 QemuOpts *opts = NULL;
738 if (id) {
739 if (!id_wellformed(id)) {
740 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
741 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
742 return NULL;
744 opts = qemu_opts_find(list, id);
745 if (opts != NULL) {
746 if (fail_if_exists && !list->merge_lists) {
747 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
748 return NULL;
749 } else {
750 return opts;
753 } else if (list->merge_lists) {
754 opts = qemu_opts_find(list, NULL);
755 if (opts) {
756 return opts;
759 opts = g_malloc0(sizeof(*opts));
760 if (id) {
761 opts->id = g_strdup(id);
763 opts->list = list;
764 loc_save(&opts->loc);
765 QTAILQ_INIT(&opts->head);
766 QTAILQ_INSERT_TAIL(&list->head, opts, next);
767 return opts;
770 void qemu_opts_reset(QemuOptsList *list)
772 QemuOpts *opts, *next_opts;
774 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
775 qemu_opts_del(opts);
779 void qemu_opts_loc_restore(QemuOpts *opts)
781 loc_restore(&opts->loc);
784 int qemu_opts_set(QemuOptsList *list, const char *id,
785 const char *name, const char *value)
787 QemuOpts *opts;
788 Error *local_err = NULL;
790 opts = qemu_opts_create(list, id, 1, &local_err);
791 if (error_is_set(&local_err)) {
792 qerror_report_err(local_err);
793 error_free(local_err);
794 return -1;
796 return qemu_opt_set(opts, name, value);
799 const char *qemu_opts_id(QemuOpts *opts)
801 return opts->id;
804 void qemu_opts_del(QemuOpts *opts)
806 QemuOpt *opt;
808 for (;;) {
809 opt = QTAILQ_FIRST(&opts->head);
810 if (opt == NULL)
811 break;
812 qemu_opt_del(opt);
814 QTAILQ_REMOVE(&opts->list->head, opts, next);
815 g_free(opts->id);
816 g_free(opts);
819 int qemu_opts_print(QemuOpts *opts, void *dummy)
821 QemuOpt *opt;
823 fprintf(stderr, "%s: %s:", opts->list->name,
824 opts->id ? opts->id : "<noid>");
825 QTAILQ_FOREACH(opt, &opts->head, next) {
826 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
828 fprintf(stderr, "\n");
829 return 0;
832 static int opts_do_parse(QemuOpts *opts, const char *params,
833 const char *firstname, bool prepend)
835 char option[128], value[1024];
836 const char *p,*pe,*pc;
838 for (p = params; *p != '\0'; p++) {
839 pe = strchr(p, '=');
840 pc = strchr(p, ',');
841 if (!pe || (pc && pc < pe)) {
842 /* found "foo,more" */
843 if (p == params && firstname) {
844 /* implicitly named first option */
845 pstrcpy(option, sizeof(option), firstname);
846 p = get_opt_value(value, sizeof(value), p);
847 } else {
848 /* option without value, probably a flag */
849 p = get_opt_name(option, sizeof(option), p, ',');
850 if (strncmp(option, "no", 2) == 0) {
851 memmove(option, option+2, strlen(option+2)+1);
852 pstrcpy(value, sizeof(value), "off");
853 } else {
854 pstrcpy(value, sizeof(value), "on");
857 } else {
858 /* found "foo=bar,more" */
859 p = get_opt_name(option, sizeof(option), p, '=');
860 if (*p != '=') {
861 break;
863 p++;
864 p = get_opt_value(value, sizeof(value), p);
866 if (strcmp(option, "id") != 0) {
867 /* store and parse */
868 if (opt_set(opts, option, value, prepend) == -1) {
869 return -1;
872 if (*p != ',') {
873 break;
876 return 0;
879 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
881 return opts_do_parse(opts, params, firstname, false);
884 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
885 int permit_abbrev, bool defaults)
887 const char *firstname;
888 char value[1024], *id = NULL;
889 const char *p;
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;
896 if (strncmp(params, "id=", 3) == 0) {
897 get_opt_value(value, sizeof(value), params+3);
898 id = value;
899 } else if ((p = strstr(params, ",id=")) != NULL) {
900 get_opt_value(value, sizeof(value), p+4);
901 id = value;
903 if (defaults) {
904 if (!id && !QTAILQ_EMPTY(&list->head)) {
905 opts = qemu_opts_find(list, NULL);
906 } else {
907 opts = qemu_opts_create(list, id, 0, &local_err);
909 } else {
910 opts = qemu_opts_create(list, id, 1, &local_err);
912 if (opts == NULL) {
913 if (error_is_set(&local_err)) {
914 qerror_report_err(local_err);
915 error_free(local_err);
917 return NULL;
920 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
921 qemu_opts_del(opts);
922 return NULL;
925 return opts;
928 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
929 int permit_abbrev)
931 return opts_parse(list, params, permit_abbrev, false);
934 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
935 int permit_abbrev)
937 QemuOpts *opts;
939 opts = opts_parse(list, params, permit_abbrev, true);
940 assert(opts);
943 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
945 char buf[32];
946 const char *value;
947 int n;
949 if (!strcmp(key, "id")) {
950 return;
953 switch (qobject_type(obj)) {
954 case QTYPE_QSTRING:
955 value = qstring_get_str(qobject_to_qstring(obj));
956 break;
957 case QTYPE_QINT:
958 n = snprintf(buf, sizeof(buf), "%" PRId64,
959 qint_get_int(qobject_to_qint(obj)));
960 assert(n < sizeof(buf));
961 value = buf;
962 break;
963 case QTYPE_QFLOAT:
964 n = snprintf(buf, sizeof(buf), "%.17g",
965 qfloat_get_double(qobject_to_qfloat(obj)));
966 assert(n < sizeof(buf));
967 value = buf;
968 break;
969 case QTYPE_QBOOL:
970 pstrcpy(buf, sizeof(buf),
971 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
972 value = buf;
973 break;
974 default:
975 return;
977 qemu_opt_set(opaque, key, value);
981 * Create QemuOpts from a QDict.
982 * Use value of key "id" as ID if it exists and is a QString.
983 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
984 * other types are silently ignored.
986 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
988 QemuOpts *opts;
989 Error *local_err = NULL;
991 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
992 &local_err);
993 if (error_is_set(&local_err)) {
994 qerror_report_err(local_err);
995 error_free(local_err);
996 return NULL;
999 assert(opts != NULL);
1000 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
1001 return opts;
1005 * Convert from QemuOpts to QDict.
1006 * The QDict values are of type QString.
1007 * TODO We'll want to use types appropriate for opt->desc->type, but
1008 * this is enough for now.
1010 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1012 QemuOpt *opt;
1013 QObject *val;
1015 if (!qdict) {
1016 qdict = qdict_new();
1018 if (opts->id) {
1019 qdict_put(qdict, "id", qstring_from_str(opts->id));
1021 QTAILQ_FOREACH(opt, &opts->head, next) {
1022 val = QOBJECT(qstring_from_str(opt->str));
1023 qdict_put_obj(qdict, opt->name, val);
1025 return qdict;
1028 /* Validate parsed opts against descriptions where no
1029 * descriptions were provided in the QemuOptsList.
1031 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
1033 QemuOpt *opt;
1035 assert(opts->list->desc[0].name == NULL);
1037 QTAILQ_FOREACH(opt, &opts->head, next) {
1038 int i;
1040 for (i = 0; desc[i].name != NULL; i++) {
1041 if (strcmp(desc[i].name, opt->name) == 0) {
1042 break;
1045 if (desc[i].name == NULL) {
1046 qerror_report(QERR_INVALID_PARAMETER, opt->name);
1047 return -1;
1050 opt->desc = &desc[i];
1052 if (qemu_opt_parse(opt) < 0) {
1053 return -1;
1057 return 0;
1060 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1061 int abort_on_failure)
1063 Location loc;
1064 QemuOpts *opts;
1065 int rc = 0;
1067 loc_push_none(&loc);
1068 QTAILQ_FOREACH(opts, &list->head, next) {
1069 loc_restore(&opts->loc);
1070 rc |= func(opts, opaque);
1071 if (abort_on_failure && rc != 0)
1072 break;
1074 loc_pop(&loc);
1075 return rc;