]> git.proxmox.com Git - qemu.git/blob - qemu-option.c
error: Convert qemu_opts_create() to QError
[qemu.git] / qemu-option.c
1 /*
2 * Commandline option parsing functions
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 */
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "qemu-common.h"
30 #include "qemu-error.h"
31 #include "qemu-objects.h"
32 #include "qemu-option.h"
33 #include "qerror.h"
34
35 /*
36 * Extracts the name of an option from the parameter string (p points at the
37 * first byte of the option name)
38 *
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.
42 *
43 * The return value is the position of the delimiter/zero byte after the option
44 * name in p.
45 */
46 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
47 {
48 char *q;
49
50 q = buf;
51 while (*p != '\0' && *p != delim) {
52 if (q && (q - buf) < buf_size - 1)
53 *q++ = *p;
54 p++;
55 }
56 if (q)
57 *q = '\0';
58
59 return p;
60 }
61
62 /*
63 * Extracts the value of an option from the parameter string p (p points at the
64 * first byte of the option value)
65 *
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.
69 */
70 const char *get_opt_value(char *buf, int buf_size, const char *p)
71 {
72 char *q;
73
74 q = buf;
75 while (*p != '\0') {
76 if (*p == ',') {
77 if (*(p + 1) != ',')
78 break;
79 p++;
80 }
81 if (q && (q - buf) < buf_size - 1)
82 *q++ = *p;
83 p++;
84 }
85 if (q)
86 *q = '\0';
87
88 return p;
89 }
90
91 int get_next_param_value(char *buf, int buf_size,
92 const char *tag, const char **pstr)
93 {
94 const char *p;
95 char option[128];
96
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)++;
107 }
108 return strlen(buf);
109 } else {
110 p = get_opt_value(NULL, 0, p);
111 }
112 if (*p != ',')
113 break;
114 p++;
115 }
116 return 0;
117 }
118
119 int get_param_value(char *buf, int buf_size,
120 const char *tag, const char *str)
121 {
122 return get_next_param_value(buf, buf_size, tag, &str);
123 }
124
125 int check_params(char *buf, int buf_size,
126 const char * const *params, const char *str)
127 {
128 const char *p;
129 int i;
130
131 p = str;
132 while (*p != '\0') {
133 p = get_opt_name(buf, buf_size, p, '=');
134 if (*p != '=') {
135 return -1;
136 }
137 p++;
138 for (i = 0; params[i] != NULL; i++) {
139 if (!strcmp(params[i], buf)) {
140 break;
141 }
142 }
143 if (params[i] == NULL) {
144 return -1;
145 }
146 p = get_opt_value(NULL, 0, p);
147 if (*p != ',') {
148 break;
149 }
150 p++;
151 }
152 return 0;
153 }
154
155 /*
156 * Searches an option list for an option with the given name
157 */
158 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
159 const char *name)
160 {
161 while (list && list->name) {
162 if (!strcmp(list->name, name)) {
163 return list;
164 }
165 list++;
166 }
167
168 return NULL;
169 }
170
171 static int parse_option_bool(const char *name, const char *value, int *ret)
172 {
173 if (value != NULL) {
174 if (!strcmp(value, "on")) {
175 *ret = 1;
176 } else if (!strcmp(value, "off")) {
177 *ret = 0;
178 } else {
179 fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
180 return -1;
181 }
182 } else {
183 *ret = 1;
184 }
185 return 0;
186 }
187
188 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
189 {
190 char *postfix;
191 uint64_t number;
192
193 if (value != NULL) {
194 number = strtoull(value, &postfix, 0);
195 if (*postfix != '\0') {
196 fprintf(stderr, "Option '%s' needs a number as parameter\n", name);
197 return -1;
198 }
199 *ret = number;
200 } else {
201 fprintf(stderr, "Option '%s' needs a parameter\n", name);
202 return -1;
203 }
204 return 0;
205 }
206
207 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
208 {
209 char *postfix;
210 double sizef;
211
212 if (value != NULL) {
213 sizef = strtod(value, &postfix);
214 switch (*postfix) {
215 case 'T':
216 sizef *= 1024;
217 case 'G':
218 sizef *= 1024;
219 case 'M':
220 sizef *= 1024;
221 case 'K':
222 case 'k':
223 sizef *= 1024;
224 case 'b':
225 case '\0':
226 *ret = (uint64_t) sizef;
227 break;
228 default:
229 fprintf(stderr, "Option '%s' needs size as parameter\n", name);
230 fprintf(stderr, "You may use k, M, G or T suffixes for "
231 "kilobytes, megabytes, gigabytes and terabytes.\n");
232 return -1;
233 }
234 } else {
235 fprintf(stderr, "Option '%s' needs a parameter\n", name);
236 return -1;
237 }
238 return 0;
239 }
240
241 /*
242 * Sets the value of a parameter in a given option list. The parsing of the
243 * value depends on the type of option:
244 *
245 * OPT_FLAG (uses value.n):
246 * If no value is given, the flag is set to 1.
247 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
248 *
249 * OPT_STRING (uses value.s):
250 * value is strdup()ed and assigned as option value
251 *
252 * OPT_SIZE (uses value.n):
253 * The value is converted to an integer. Suffixes for kilobytes etc. are
254 * allowed (powers of 1024).
255 *
256 * Returns 0 on succes, -1 in error cases
257 */
258 int set_option_parameter(QEMUOptionParameter *list, const char *name,
259 const char *value)
260 {
261 int flag;
262
263 // Find a matching parameter
264 list = get_option_parameter(list, name);
265 if (list == NULL) {
266 fprintf(stderr, "Unknown option '%s'\n", name);
267 return -1;
268 }
269
270 // Process parameter
271 switch (list->type) {
272 case OPT_FLAG:
273 if (parse_option_bool(name, value, &flag) == -1)
274 return -1;
275 list->value.n = flag;
276 break;
277
278 case OPT_STRING:
279 if (value != NULL) {
280 list->value.s = qemu_strdup(value);
281 } else {
282 fprintf(stderr, "Option '%s' needs a parameter\n", name);
283 return -1;
284 }
285 break;
286
287 case OPT_SIZE:
288 if (parse_option_size(name, value, &list->value.n) == -1)
289 return -1;
290 break;
291
292 default:
293 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
294 return -1;
295 }
296
297 return 0;
298 }
299
300 /*
301 * Sets the given parameter to an integer instead of a string.
302 * This function cannot be used to set string options.
303 *
304 * Returns 0 on success, -1 in error cases
305 */
306 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
307 uint64_t value)
308 {
309 // Find a matching parameter
310 list = get_option_parameter(list, name);
311 if (list == NULL) {
312 fprintf(stderr, "Unknown option '%s'\n", name);
313 return -1;
314 }
315
316 // Process parameter
317 switch (list->type) {
318 case OPT_FLAG:
319 case OPT_NUMBER:
320 case OPT_SIZE:
321 list->value.n = value;
322 break;
323
324 default:
325 return -1;
326 }
327
328 return 0;
329 }
330
331 /*
332 * Frees a option list. If it contains strings, the strings are freed as well.
333 */
334 void free_option_parameters(QEMUOptionParameter *list)
335 {
336 QEMUOptionParameter *cur = list;
337
338 while (cur && cur->name) {
339 if (cur->type == OPT_STRING) {
340 qemu_free(cur->value.s);
341 }
342 cur++;
343 }
344
345 qemu_free(list);
346 }
347
348 /*
349 * Parses a parameter string (param) into an option list (dest).
350 *
351 * list is the templace is. If dest is NULL, a new copy of list is created for
352 * it. If list is NULL, this function fails.
353 *
354 * A parameter string consists of one or more parameters, separated by commas.
355 * Each parameter consists of its name and possibly of a value. In the latter
356 * case, the value is delimited by an = character. To specify a value which
357 * contains commas, double each comma so it won't be recognized as the end of
358 * the parameter.
359 *
360 * For more details of the parsing see above.
361 *
362 * Returns a pointer to the first element of dest (or the newly allocated copy)
363 * or NULL in error cases
364 */
365 QEMUOptionParameter *parse_option_parameters(const char *param,
366 QEMUOptionParameter *list, QEMUOptionParameter *dest)
367 {
368 QEMUOptionParameter *cur;
369 QEMUOptionParameter *allocated = NULL;
370 char name[256];
371 char value[256];
372 char *param_delim, *value_delim;
373 char next_delim;
374 size_t num_options;
375
376 if (list == NULL) {
377 return NULL;
378 }
379
380 if (dest == NULL) {
381 // Count valid options
382 num_options = 0;
383 cur = list;
384 while (cur->name) {
385 num_options++;
386 cur++;
387 }
388
389 // Create a copy of the option list to fill in values
390 dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
391 allocated = dest;
392 memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
393 }
394
395 while (*param) {
396
397 // Find parameter name and value in the string
398 param_delim = strchr(param, ',');
399 value_delim = strchr(param, '=');
400
401 if (value_delim && (value_delim < param_delim || !param_delim)) {
402 next_delim = '=';
403 } else {
404 next_delim = ',';
405 value_delim = NULL;
406 }
407
408 param = get_opt_name(name, sizeof(name), param, next_delim);
409 if (value_delim) {
410 param = get_opt_value(value, sizeof(value), param + 1);
411 }
412 if (*param != '\0') {
413 param++;
414 }
415
416 // Set the parameter
417 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
418 goto fail;
419 }
420 }
421
422 return dest;
423
424 fail:
425 // Only free the list if it was newly allocated
426 free_option_parameters(allocated);
427 return NULL;
428 }
429
430 /*
431 * Prints all options of a list that have a value to stdout
432 */
433 void print_option_parameters(QEMUOptionParameter *list)
434 {
435 while (list && list->name) {
436 switch (list->type) {
437 case OPT_STRING:
438 if (list->value.s != NULL) {
439 printf("%s='%s' ", list->name, list->value.s);
440 }
441 break;
442 case OPT_FLAG:
443 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
444 break;
445 case OPT_SIZE:
446 case OPT_NUMBER:
447 printf("%s=%" PRId64 " ", list->name, list->value.n);
448 break;
449 default:
450 printf("%s=(unkown type) ", list->name);
451 break;
452 }
453 list++;
454 }
455 }
456
457 /*
458 * Prints an overview of all available options
459 */
460 void print_option_help(QEMUOptionParameter *list)
461 {
462 printf("Supported options:\n");
463 while (list && list->name) {
464 printf("%-16s %s\n", list->name,
465 list->help ? list->help : "No description available");
466 list++;
467 }
468 }
469
470 /* ------------------------------------------------------------------ */
471
472 struct QemuOpt {
473 const char *name;
474 const char *str;
475
476 const QemuOptDesc *desc;
477 union {
478 int boolean;
479 uint64_t uint;
480 } value;
481
482 QemuOpts *opts;
483 QTAILQ_ENTRY(QemuOpt) next;
484 };
485
486 struct QemuOpts {
487 char *id;
488 QemuOptsList *list;
489 Location loc;
490 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
491 QTAILQ_ENTRY(QemuOpts) next;
492 };
493
494 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
495 {
496 QemuOpt *opt;
497
498 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
499 if (strcmp(opt->name, name) != 0)
500 continue;
501 return opt;
502 }
503 return NULL;
504 }
505
506 const char *qemu_opt_get(QemuOpts *opts, const char *name)
507 {
508 QemuOpt *opt = qemu_opt_find(opts, name);
509 return opt ? opt->str : NULL;
510 }
511
512 int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
513 {
514 QemuOpt *opt = qemu_opt_find(opts, name);
515
516 if (opt == NULL)
517 return defval;
518 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
519 return opt->value.boolean;
520 }
521
522 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
523 {
524 QemuOpt *opt = qemu_opt_find(opts, name);
525
526 if (opt == NULL)
527 return defval;
528 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
529 return opt->value.uint;
530 }
531
532 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
533 {
534 QemuOpt *opt = qemu_opt_find(opts, name);
535
536 if (opt == NULL)
537 return defval;
538 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
539 return opt->value.uint;
540 }
541
542 static int qemu_opt_parse(QemuOpt *opt)
543 {
544 if (opt->desc == NULL)
545 return 0;
546 switch (opt->desc->type) {
547 case QEMU_OPT_STRING:
548 /* nothing */
549 return 0;
550 case QEMU_OPT_BOOL:
551 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
552 case QEMU_OPT_NUMBER:
553 return parse_option_number(opt->name, opt->str, &opt->value.uint);
554 case QEMU_OPT_SIZE:
555 return parse_option_size(opt->name, opt->str, &opt->value.uint);
556 default:
557 abort();
558 }
559 }
560
561 static void qemu_opt_del(QemuOpt *opt)
562 {
563 QTAILQ_REMOVE(&opt->opts->head, opt, next);
564 qemu_free((/* !const */ char*)opt->name);
565 qemu_free((/* !const */ char*)opt->str);
566 qemu_free(opt);
567 }
568
569 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
570 {
571 QemuOpt *opt;
572 const QemuOptDesc *desc = opts->list->desc;
573 int i;
574
575 for (i = 0; desc[i].name != NULL; i++) {
576 if (strcmp(desc[i].name, name) == 0) {
577 break;
578 }
579 }
580 if (desc[i].name == NULL) {
581 if (i == 0) {
582 /* empty list -> allow any */;
583 } else {
584 fprintf(stderr, "option \"%s\" is not valid for %s\n",
585 name, opts->list->name);
586 return -1;
587 }
588 }
589
590 opt = qemu_mallocz(sizeof(*opt));
591 opt->name = qemu_strdup(name);
592 opt->opts = opts;
593 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
594 if (desc[i].name != NULL) {
595 opt->desc = desc+i;
596 }
597 if (value) {
598 opt->str = qemu_strdup(value);
599 }
600 if (qemu_opt_parse(opt) < 0) {
601 fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str,
602 opts->list->name, opt->name);
603 qemu_opt_del(opt);
604 return -1;
605 }
606 return 0;
607 }
608
609 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
610 int abort_on_failure)
611 {
612 QemuOpt *opt;
613 int rc = 0;
614
615 QTAILQ_FOREACH(opt, &opts->head, next) {
616 rc = func(opt->name, opt->str, opaque);
617 if (abort_on_failure && rc != 0)
618 break;
619 }
620 return rc;
621 }
622
623 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
624 {
625 QemuOpts *opts;
626
627 QTAILQ_FOREACH(opts, &list->head, next) {
628 if (!opts->id) {
629 continue;
630 }
631 if (strcmp(opts->id, id) != 0) {
632 continue;
633 }
634 return opts;
635 }
636 return NULL;
637 }
638
639 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
640 {
641 QemuOpts *opts = NULL;
642
643 if (id) {
644 opts = qemu_opts_find(list, id);
645 if (opts != NULL) {
646 if (fail_if_exists) {
647 qerror_report(QERR_DUPLICATE_ID, id, list->name);
648 return NULL;
649 } else {
650 return opts;
651 }
652 }
653 }
654 opts = qemu_mallocz(sizeof(*opts));
655 if (id) {
656 opts->id = qemu_strdup(id);
657 }
658 opts->list = list;
659 loc_save(&opts->loc);
660 QTAILQ_INIT(&opts->head);
661 QTAILQ_INSERT_TAIL(&list->head, opts, next);
662 return opts;
663 }
664
665 int qemu_opts_set(QemuOptsList *list, const char *id,
666 const char *name, const char *value)
667 {
668 QemuOpts *opts;
669
670 opts = qemu_opts_create(list, id, 1);
671 if (opts == NULL) {
672 return -1;
673 }
674 return qemu_opt_set(opts, name, value);
675 }
676
677 const char *qemu_opts_id(QemuOpts *opts)
678 {
679 return opts->id;
680 }
681
682 void qemu_opts_del(QemuOpts *opts)
683 {
684 QemuOpt *opt;
685
686 for (;;) {
687 opt = QTAILQ_FIRST(&opts->head);
688 if (opt == NULL)
689 break;
690 qemu_opt_del(opt);
691 }
692 QTAILQ_REMOVE(&opts->list->head, opts, next);
693 qemu_free(opts->id);
694 qemu_free(opts);
695 }
696
697 int qemu_opts_print(QemuOpts *opts, void *dummy)
698 {
699 QemuOpt *opt;
700
701 fprintf(stderr, "%s: %s:", opts->list->name,
702 opts->id ? opts->id : "<noid>");
703 QTAILQ_FOREACH(opt, &opts->head, next) {
704 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
705 }
706 fprintf(stderr, "\n");
707 return 0;
708 }
709
710 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
711 {
712 char option[128], value[1024];
713 const char *p,*pe,*pc;
714
715 for (p = params; *p != '\0'; p++) {
716 pe = strchr(p, '=');
717 pc = strchr(p, ',');
718 if (!pe || (pc && pc < pe)) {
719 /* found "foo,more" */
720 if (p == params && firstname) {
721 /* implicitly named first option */
722 pstrcpy(option, sizeof(option), firstname);
723 p = get_opt_value(value, sizeof(value), p);
724 } else {
725 /* option without value, probably a flag */
726 p = get_opt_name(option, sizeof(option), p, ',');
727 if (strncmp(option, "no", 2) == 0) {
728 memmove(option, option+2, strlen(option+2)+1);
729 pstrcpy(value, sizeof(value), "off");
730 } else {
731 pstrcpy(value, sizeof(value), "on");
732 }
733 }
734 } else {
735 /* found "foo=bar,more" */
736 p = get_opt_name(option, sizeof(option), p, '=');
737 if (*p != '=') {
738 break;
739 }
740 p++;
741 p = get_opt_value(value, sizeof(value), p);
742 }
743 if (strcmp(option, "id") != 0) {
744 /* store and parse */
745 if (qemu_opt_set(opts, option, value) == -1) {
746 return -1;
747 }
748 }
749 if (*p != ',') {
750 break;
751 }
752 }
753 return 0;
754 }
755
756 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
757 int permit_abbrev)
758 {
759 const char *firstname;
760 char value[1024], *id = NULL;
761 const char *p;
762 QemuOpts *opts;
763
764 assert(!permit_abbrev || list->implied_opt_name);
765 firstname = permit_abbrev ? list->implied_opt_name : NULL;
766
767 if (strncmp(params, "id=", 3) == 0) {
768 get_opt_value(value, sizeof(value), params+3);
769 id = qemu_strdup(value);
770 } else if ((p = strstr(params, ",id=")) != NULL) {
771 get_opt_value(value, sizeof(value), p+4);
772 id = qemu_strdup(value);
773 }
774 opts = qemu_opts_create(list, id, 1);
775 if (opts == NULL)
776 return NULL;
777
778 if (qemu_opts_do_parse(opts, params, firstname) != 0) {
779 qemu_opts_del(opts);
780 return NULL;
781 }
782
783 return opts;
784 }
785
786 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
787 {
788 char buf[32];
789 const char *value;
790 int n;
791
792 if (!strcmp(key, "id")) {
793 return;
794 }
795
796 switch (qobject_type(obj)) {
797 case QTYPE_QSTRING:
798 value = qstring_get_str(qobject_to_qstring(obj));
799 break;
800 case QTYPE_QINT:
801 n = snprintf(buf, sizeof(buf), "%" PRId64,
802 qint_get_int(qobject_to_qint(obj)));
803 assert(n < sizeof(buf));
804 value = buf;
805 break;
806 case QTYPE_QFLOAT:
807 n = snprintf(buf, sizeof(buf), "%.17g",
808 qfloat_get_double(qobject_to_qfloat(obj)));
809 assert(n < sizeof(buf));
810 value = buf;
811 break;
812 case QTYPE_QBOOL:
813 pstrcpy(buf, sizeof(buf),
814 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
815 value = buf;
816 break;
817 default:
818 return;
819 }
820 qemu_opt_set(opaque, key, value);
821 }
822
823 /*
824 * Create QemuOpts from a QDict.
825 * Use value of key "id" as ID if it exists and is a QString.
826 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
827 * other types are silently ignored.
828 */
829 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
830 {
831 QemuOpts *opts;
832
833 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
834 if (opts == NULL)
835 return NULL;
836
837 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
838 return opts;
839 }
840
841 /*
842 * Convert from QemuOpts to QDict.
843 * The QDict values are of type QString.
844 * TODO We'll want to use types appropriate for opt->desc->type, but
845 * this is enough for now.
846 */
847 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
848 {
849 QemuOpt *opt;
850 QObject *val;
851
852 if (!qdict) {
853 qdict = qdict_new();
854 }
855 if (opts->id) {
856 qdict_put(qdict, "id", qstring_from_str(opts->id));
857 }
858 QTAILQ_FOREACH(opt, &opts->head, next) {
859 val = QOBJECT(qstring_from_str(opt->str));
860 qdict_put_obj(qdict, opt->name, val);
861 }
862 return qdict;
863 }
864
865 /* Validate parsed opts against descriptions where no
866 * descriptions were provided in the QemuOptsList.
867 */
868 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
869 {
870 QemuOpt *opt;
871
872 assert(opts->list->desc[0].name == NULL);
873
874 QTAILQ_FOREACH(opt, &opts->head, next) {
875 int i;
876
877 for (i = 0; desc[i].name != NULL; i++) {
878 if (strcmp(desc[i].name, opt->name) == 0) {
879 break;
880 }
881 }
882 if (desc[i].name == NULL) {
883 fprintf(stderr, "option \"%s\" is not valid for %s\n",
884 opt->name, opts->list->name);
885 return -1;
886 }
887
888 opt->desc = &desc[i];
889
890 if (qemu_opt_parse(opt) < 0) {
891 return -1;
892 }
893 }
894
895 return 0;
896 }
897
898 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
899 int abort_on_failure)
900 {
901 Location loc;
902 QemuOpts *opts;
903 int rc = 0;
904
905 loc_push_none(&loc);
906 QTAILQ_FOREACH(opts, &list->head, next) {
907 loc_restore(&opts->loc);
908 rc |= func(opts, opaque);
909 if (abort_on_failure && rc != 0)
910 break;
911 }
912 loc_pop(&loc);
913 return rc;
914 }