./configure: request pkg-config to provide private libs when static linking
[qemu/ar7.git] / qemu-option.c
bloba303f87e1cc8ef481dd8a9f01b92a8e15d0e51af
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 "qerror.h"
36 * Extracts the name of an option from the parameter string (p points at the
37 * first byte of the option name)
39 * The option name is delimited by delim (usually , or =) or the string end
40 * and is copied into buf. If the option name is longer than buf_size, it is
41 * truncated. buf is always zero terminated.
43 * The return value is the position of the delimiter/zero byte after the option
44 * name in p.
46 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
48 char *q;
50 q = buf;
51 while (*p != '\0' && *p != delim) {
52 if (q && (q - buf) < buf_size - 1)
53 *q++ = *p;
54 p++;
56 if (q)
57 *q = '\0';
59 return p;
63 * Extracts the value of an option from the parameter string p (p points at the
64 * first byte of the option value)
66 * This function is comparable to get_opt_name with the difference that the
67 * delimiter is fixed to be comma which starts a new option. To specify an
68 * option value that contains commas, double each comma.
70 const char *get_opt_value(char *buf, int buf_size, const char *p)
72 char *q;
74 q = buf;
75 while (*p != '\0') {
76 if (*p == ',') {
77 if (*(p + 1) != ',')
78 break;
79 p++;
81 if (q && (q - buf) < buf_size - 1)
82 *q++ = *p;
83 p++;
85 if (q)
86 *q = '\0';
88 return p;
91 int get_next_param_value(char *buf, int buf_size,
92 const char *tag, const char **pstr)
94 const char *p;
95 char option[128];
97 p = *pstr;
98 for(;;) {
99 p = get_opt_name(option, sizeof(option), p, '=');
100 if (*p != '=')
101 break;
102 p++;
103 if (!strcmp(tag, option)) {
104 *pstr = get_opt_value(buf, buf_size, p);
105 if (**pstr == ',') {
106 (*pstr)++;
108 return strlen(buf);
109 } else {
110 p = get_opt_value(NULL, 0, p);
112 if (*p != ',')
113 break;
114 p++;
116 return 0;
119 int get_param_value(char *buf, int buf_size,
120 const char *tag, const char *str)
122 return get_next_param_value(buf, buf_size, tag, &str);
125 int check_params(char *buf, int buf_size,
126 const char * const *params, const char *str)
128 const char *p;
129 int i;
131 p = str;
132 while (*p != '\0') {
133 p = get_opt_name(buf, buf_size, p, '=');
134 if (*p != '=') {
135 return -1;
137 p++;
138 for (i = 0; params[i] != NULL; i++) {
139 if (!strcmp(params[i], buf)) {
140 break;
143 if (params[i] == NULL) {
144 return -1;
146 p = get_opt_value(NULL, 0, p);
147 if (*p != ',') {
148 break;
150 p++;
152 return 0;
156 * Searches an option list for an option with the given name
158 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
159 const char *name)
161 while (list && list->name) {
162 if (!strcmp(list->name, name)) {
163 return list;
165 list++;
168 return NULL;
171 static int parse_option_bool(const char *name, const char *value, bool *ret)
173 if (value != NULL) {
174 if (!strcmp(value, "on")) {
175 *ret = 1;
176 } else if (!strcmp(value, "off")) {
177 *ret = 0;
178 } else {
179 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
180 return -1;
182 } else {
183 *ret = 1;
185 return 0;
188 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
190 char *postfix;
191 uint64_t number;
193 if (value != NULL) {
194 number = strtoull(value, &postfix, 0);
195 if (*postfix != '\0') {
196 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
197 return -1;
199 *ret = number;
200 } else {
201 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
202 return -1;
204 return 0;
207 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
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 qerror_report(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 -1;
238 } else {
239 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
240 return -1;
242 return 0;
246 * Sets the value of a parameter in a given option list. The parsing of the
247 * value depends on the type of option:
249 * OPT_FLAG (uses value.n):
250 * If no value is given, the flag is set to 1.
251 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
253 * OPT_STRING (uses value.s):
254 * value is strdup()ed and assigned as option value
256 * OPT_SIZE (uses value.n):
257 * The value is converted to an integer. Suffixes for kilobytes etc. are
258 * allowed (powers of 1024).
260 * Returns 0 on succes, -1 in error cases
262 int set_option_parameter(QEMUOptionParameter *list, const char *name,
263 const char *value)
265 bool flag;
267 // Find a matching parameter
268 list = get_option_parameter(list, name);
269 if (list == NULL) {
270 fprintf(stderr, "Unknown option '%s'\n", name);
271 return -1;
274 // Process parameter
275 switch (list->type) {
276 case OPT_FLAG:
277 if (parse_option_bool(name, value, &flag) == -1)
278 return -1;
279 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 if (parse_option_size(name, value, &list->value.n) == -1)
293 return -1;
294 break;
296 default:
297 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
298 return -1;
301 return 0;
305 * Sets the given parameter to an integer instead of a string.
306 * This function cannot be used to set string options.
308 * Returns 0 on success, -1 in error cases
310 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
311 uint64_t value)
313 // Find a matching parameter
314 list = get_option_parameter(list, name);
315 if (list == NULL) {
316 fprintf(stderr, "Unknown option '%s'\n", name);
317 return -1;
320 // Process parameter
321 switch (list->type) {
322 case OPT_FLAG:
323 case OPT_NUMBER:
324 case OPT_SIZE:
325 list->value.n = value;
326 break;
328 default:
329 return -1;
332 return 0;
336 * Frees a option list. If it contains strings, the strings are freed as well.
338 void free_option_parameters(QEMUOptionParameter *list)
340 QEMUOptionParameter *cur = list;
342 while (cur && cur->name) {
343 if (cur->type == OPT_STRING) {
344 g_free(cur->value.s);
346 cur++;
349 g_free(list);
353 * Count valid options in list
355 static size_t count_option_parameters(QEMUOptionParameter *list)
357 size_t num_options = 0;
359 while (list && list->name) {
360 num_options++;
361 list++;
364 return num_options;
368 * Append an option list (list) to an option list (dest).
370 * If dest is NULL, a new copy of list is created.
372 * Returns a pointer to the first element of dest (or the newly allocated copy)
374 QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
375 QEMUOptionParameter *list)
377 size_t num_options, num_dest_options;
379 num_options = count_option_parameters(dest);
380 num_dest_options = num_options;
382 num_options += count_option_parameters(list);
384 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
385 dest[num_dest_options].name = NULL;
387 while (list && list->name) {
388 if (get_option_parameter(dest, list->name) == NULL) {
389 dest[num_dest_options++] = *list;
390 dest[num_dest_options].name = NULL;
392 list++;
395 return dest;
399 * Parses a parameter string (param) into an option list (dest).
401 * list is the template option list. If dest is NULL, a new copy of list is
402 * created. If list is NULL, this function fails.
404 * A parameter string consists of one or more parameters, separated by commas.
405 * Each parameter consists of its name and possibly of a value. In the latter
406 * case, the value is delimited by an = character. To specify a value which
407 * contains commas, double each comma so it won't be recognized as the end of
408 * the parameter.
410 * For more details of the parsing see above.
412 * Returns a pointer to the first element of dest (or the newly allocated copy)
413 * or NULL in error cases
415 QEMUOptionParameter *parse_option_parameters(const char *param,
416 QEMUOptionParameter *list, QEMUOptionParameter *dest)
418 QEMUOptionParameter *allocated = NULL;
419 char name[256];
420 char value[256];
421 char *param_delim, *value_delim;
422 char next_delim;
424 if (list == NULL) {
425 return NULL;
428 if (dest == NULL) {
429 dest = allocated = append_option_parameters(NULL, list);
432 while (*param) {
434 // Find parameter name and value in the string
435 param_delim = strchr(param, ',');
436 value_delim = strchr(param, '=');
438 if (value_delim && (value_delim < param_delim || !param_delim)) {
439 next_delim = '=';
440 } else {
441 next_delim = ',';
442 value_delim = NULL;
445 param = get_opt_name(name, sizeof(name), param, next_delim);
446 if (value_delim) {
447 param = get_opt_value(value, sizeof(value), param + 1);
449 if (*param != '\0') {
450 param++;
453 // Set the parameter
454 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
455 goto fail;
459 return dest;
461 fail:
462 // Only free the list if it was newly allocated
463 free_option_parameters(allocated);
464 return NULL;
468 * Prints all options of a list that have a value to stdout
470 void print_option_parameters(QEMUOptionParameter *list)
472 while (list && list->name) {
473 switch (list->type) {
474 case OPT_STRING:
475 if (list->value.s != NULL) {
476 printf("%s='%s' ", list->name, list->value.s);
478 break;
479 case OPT_FLAG:
480 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
481 break;
482 case OPT_SIZE:
483 case OPT_NUMBER:
484 printf("%s=%" PRId64 " ", list->name, list->value.n);
485 break;
486 default:
487 printf("%s=(unknown type) ", list->name);
488 break;
490 list++;
495 * Prints an overview of all available options
497 void print_option_help(QEMUOptionParameter *list)
499 printf("Supported options:\n");
500 while (list && list->name) {
501 printf("%-16s %s\n", list->name,
502 list->help ? list->help : "No description available");
503 list++;
507 /* ------------------------------------------------------------------ */
509 struct QemuOpt {
510 const char *name;
511 const char *str;
513 const QemuOptDesc *desc;
514 union {
515 bool boolean;
516 uint64_t uint;
517 } value;
519 QemuOpts *opts;
520 QTAILQ_ENTRY(QemuOpt) next;
523 struct QemuOpts {
524 char *id;
525 QemuOptsList *list;
526 Location loc;
527 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
528 QTAILQ_ENTRY(QemuOpts) next;
531 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
533 QemuOpt *opt;
535 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
536 if (strcmp(opt->name, name) != 0)
537 continue;
538 return opt;
540 return NULL;
543 const char *qemu_opt_get(QemuOpts *opts, const char *name)
545 QemuOpt *opt = qemu_opt_find(opts, name);
546 return opt ? opt->str : NULL;
549 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
551 QemuOpt *opt = qemu_opt_find(opts, name);
553 if (opt == NULL)
554 return defval;
555 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
556 return opt->value.boolean;
559 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
561 QemuOpt *opt = qemu_opt_find(opts, name);
563 if (opt == NULL)
564 return defval;
565 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
566 return opt->value.uint;
569 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
571 QemuOpt *opt = qemu_opt_find(opts, name);
573 if (opt == NULL)
574 return defval;
575 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
576 return opt->value.uint;
579 static int qemu_opt_parse(QemuOpt *opt)
581 if (opt->desc == NULL)
582 return 0;
583 switch (opt->desc->type) {
584 case QEMU_OPT_STRING:
585 /* nothing */
586 return 0;
587 case QEMU_OPT_BOOL:
588 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
589 case QEMU_OPT_NUMBER:
590 return parse_option_number(opt->name, opt->str, &opt->value.uint);
591 case QEMU_OPT_SIZE:
592 return parse_option_size(opt->name, opt->str, &opt->value.uint);
593 default:
594 abort();
598 static void qemu_opt_del(QemuOpt *opt)
600 QTAILQ_REMOVE(&opt->opts->head, opt, next);
601 g_free((/* !const */ char*)opt->name);
602 g_free((/* !const */ char*)opt->str);
603 g_free(opt);
606 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
608 QemuOpt *opt;
609 const QemuOptDesc *desc = opts->list->desc;
610 int i;
612 for (i = 0; desc[i].name != NULL; i++) {
613 if (strcmp(desc[i].name, name) == 0) {
614 break;
617 if (desc[i].name == NULL) {
618 if (i == 0) {
619 /* empty list -> allow any */;
620 } else {
621 qerror_report(QERR_INVALID_PARAMETER, name);
622 return -1;
626 opt = g_malloc0(sizeof(*opt));
627 opt->name = g_strdup(name);
628 opt->opts = opts;
629 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
630 if (desc[i].name != NULL) {
631 opt->desc = desc+i;
633 if (value) {
634 opt->str = g_strdup(value);
636 if (qemu_opt_parse(opt) < 0) {
637 qemu_opt_del(opt);
638 return -1;
640 return 0;
643 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
645 QemuOpt *opt;
646 const QemuOptDesc *desc = opts->list->desc;
647 int i;
649 for (i = 0; desc[i].name != NULL; i++) {
650 if (strcmp(desc[i].name, name) == 0) {
651 break;
654 if (desc[i].name == NULL) {
655 if (i == 0) {
656 /* empty list -> allow any */;
657 } else {
658 qerror_report(QERR_INVALID_PARAMETER, name);
659 return -1;
663 opt = g_malloc0(sizeof(*opt));
664 opt->name = g_strdup(name);
665 opt->opts = opts;
666 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
667 if (desc[i].name != NULL) {
668 opt->desc = desc+i;
670 opt->value.boolean = !!val;
671 return 0;
674 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
675 int abort_on_failure)
677 QemuOpt *opt;
678 int rc = 0;
680 QTAILQ_FOREACH(opt, &opts->head, next) {
681 rc = func(opt->name, opt->str, opaque);
682 if (abort_on_failure && rc != 0)
683 break;
685 return rc;
688 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
690 QemuOpts *opts;
692 QTAILQ_FOREACH(opts, &list->head, next) {
693 if (!opts->id) {
694 continue;
696 if (strcmp(opts->id, id) != 0) {
697 continue;
699 return opts;
701 return NULL;
704 static int id_wellformed(const char *id)
706 int i;
708 if (!qemu_isalpha(id[0])) {
709 return 0;
711 for (i = 1; id[i]; i++) {
712 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
713 return 0;
716 return 1;
719 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
721 QemuOpts *opts = NULL;
723 if (id) {
724 if (!id_wellformed(id)) {
725 qerror_report(QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
726 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
727 return NULL;
729 opts = qemu_opts_find(list, id);
730 if (opts != NULL) {
731 if (fail_if_exists) {
732 qerror_report(QERR_DUPLICATE_ID, id, list->name);
733 return NULL;
734 } else {
735 return opts;
739 opts = g_malloc0(sizeof(*opts));
740 if (id) {
741 opts->id = g_strdup(id);
743 opts->list = list;
744 loc_save(&opts->loc);
745 QTAILQ_INIT(&opts->head);
746 QTAILQ_INSERT_TAIL(&list->head, opts, next);
747 return opts;
750 void qemu_opts_reset(QemuOptsList *list)
752 QemuOpts *opts, *next_opts;
754 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
755 qemu_opts_del(opts);
759 void qemu_opts_loc_restore(QemuOpts *opts)
761 loc_restore(&opts->loc);
764 int qemu_opts_set(QemuOptsList *list, const char *id,
765 const char *name, const char *value)
767 QemuOpts *opts;
769 opts = qemu_opts_create(list, id, 1);
770 if (opts == NULL) {
771 return -1;
773 return qemu_opt_set(opts, name, value);
776 const char *qemu_opts_id(QemuOpts *opts)
778 return opts->id;
781 void qemu_opts_del(QemuOpts *opts)
783 QemuOpt *opt;
785 for (;;) {
786 opt = QTAILQ_FIRST(&opts->head);
787 if (opt == NULL)
788 break;
789 qemu_opt_del(opt);
791 QTAILQ_REMOVE(&opts->list->head, opts, next);
792 g_free(opts->id);
793 g_free(opts);
796 int qemu_opts_print(QemuOpts *opts, void *dummy)
798 QemuOpt *opt;
800 fprintf(stderr, "%s: %s:", opts->list->name,
801 opts->id ? opts->id : "<noid>");
802 QTAILQ_FOREACH(opt, &opts->head, next) {
803 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
805 fprintf(stderr, "\n");
806 return 0;
809 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
811 char option[128], value[1024];
812 const char *p,*pe,*pc;
814 for (p = params; *p != '\0'; p++) {
815 pe = strchr(p, '=');
816 pc = strchr(p, ',');
817 if (!pe || (pc && pc < pe)) {
818 /* found "foo,more" */
819 if (p == params && firstname) {
820 /* implicitly named first option */
821 pstrcpy(option, sizeof(option), firstname);
822 p = get_opt_value(value, sizeof(value), p);
823 } else {
824 /* option without value, probably a flag */
825 p = get_opt_name(option, sizeof(option), p, ',');
826 if (strncmp(option, "no", 2) == 0) {
827 memmove(option, option+2, strlen(option+2)+1);
828 pstrcpy(value, sizeof(value), "off");
829 } else {
830 pstrcpy(value, sizeof(value), "on");
833 } else {
834 /* found "foo=bar,more" */
835 p = get_opt_name(option, sizeof(option), p, '=');
836 if (*p != '=') {
837 break;
839 p++;
840 p = get_opt_value(value, sizeof(value), p);
842 if (strcmp(option, "id") != 0) {
843 /* store and parse */
844 if (qemu_opt_set(opts, option, value) == -1) {
845 return -1;
848 if (*p != ',') {
849 break;
852 return 0;
855 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
856 int permit_abbrev)
858 const char *firstname;
859 char value[1024], *id = NULL;
860 const char *p;
861 QemuOpts *opts;
863 assert(!permit_abbrev || list->implied_opt_name);
864 firstname = permit_abbrev ? list->implied_opt_name : NULL;
866 if (strncmp(params, "id=", 3) == 0) {
867 get_opt_value(value, sizeof(value), params+3);
868 id = value;
869 } else if ((p = strstr(params, ",id=")) != NULL) {
870 get_opt_value(value, sizeof(value), p+4);
871 id = value;
873 opts = qemu_opts_create(list, id, 1);
874 if (opts == NULL)
875 return NULL;
877 if (qemu_opts_do_parse(opts, params, firstname) != 0) {
878 qemu_opts_del(opts);
879 return NULL;
882 return opts;
885 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
887 char buf[32];
888 const char *value;
889 int n;
891 if (!strcmp(key, "id")) {
892 return;
895 switch (qobject_type(obj)) {
896 case QTYPE_QSTRING:
897 value = qstring_get_str(qobject_to_qstring(obj));
898 break;
899 case QTYPE_QINT:
900 n = snprintf(buf, sizeof(buf), "%" PRId64,
901 qint_get_int(qobject_to_qint(obj)));
902 assert(n < sizeof(buf));
903 value = buf;
904 break;
905 case QTYPE_QFLOAT:
906 n = snprintf(buf, sizeof(buf), "%.17g",
907 qfloat_get_double(qobject_to_qfloat(obj)));
908 assert(n < sizeof(buf));
909 value = buf;
910 break;
911 case QTYPE_QBOOL:
912 pstrcpy(buf, sizeof(buf),
913 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
914 value = buf;
915 break;
916 default:
917 return;
919 qemu_opt_set(opaque, key, value);
923 * Create QemuOpts from a QDict.
924 * Use value of key "id" as ID if it exists and is a QString.
925 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
926 * other types are silently ignored.
928 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
930 QemuOpts *opts;
932 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
933 if (opts == NULL)
934 return NULL;
936 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
937 return opts;
941 * Convert from QemuOpts to QDict.
942 * The QDict values are of type QString.
943 * TODO We'll want to use types appropriate for opt->desc->type, but
944 * this is enough for now.
946 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
948 QemuOpt *opt;
949 QObject *val;
951 if (!qdict) {
952 qdict = qdict_new();
954 if (opts->id) {
955 qdict_put(qdict, "id", qstring_from_str(opts->id));
957 QTAILQ_FOREACH(opt, &opts->head, next) {
958 val = QOBJECT(qstring_from_str(opt->str));
959 qdict_put_obj(qdict, opt->name, val);
961 return qdict;
964 /* Validate parsed opts against descriptions where no
965 * descriptions were provided in the QemuOptsList.
967 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
969 QemuOpt *opt;
971 assert(opts->list->desc[0].name == NULL);
973 QTAILQ_FOREACH(opt, &opts->head, next) {
974 int i;
976 for (i = 0; desc[i].name != NULL; i++) {
977 if (strcmp(desc[i].name, opt->name) == 0) {
978 break;
981 if (desc[i].name == NULL) {
982 qerror_report(QERR_INVALID_PARAMETER, opt->name);
983 return -1;
986 opt->desc = &desc[i];
988 if (qemu_opt_parse(opt) < 0) {
989 return -1;
993 return 0;
996 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
997 int abort_on_failure)
999 Location loc;
1000 QemuOpts *opts;
1001 int rc = 0;
1003 loc_push_none(&loc);
1004 QTAILQ_FOREACH(opts, &list->head, next) {
1005 loc_restore(&opts->loc);
1006 rc |= func(opts, opaque);
1007 if (abort_on_failure && rc != 0)
1008 break;
1010 loc_pop(&loc);
1011 return rc;