]> git.proxmox.com Git - qemu.git/blame - qemu-option.c
qdev: Revert the hack to let -net nic and pci_add set qdev ID
[qemu.git] / qemu-option.c
CommitLineData
d3f24367
KW
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"
827b0813 30#include "qemu-error.h"
01e7f188 31#include "qemu-objects.h"
d3f24367 32#include "qemu-option.h"
975b63a4 33#include "qerror.h"
d3f24367
KW
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 */
46const 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 */
70const 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
62c5802e
GH
91int 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
119int 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
125int 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
d3f24367
KW
155/*
156 * Searches an option list for an option with the given name
157 */
158QEMUOptionParameter *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
67b1355b
GH
171static 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 {
c64f27d4 179 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
67b1355b
GH
180 return -1;
181 }
182 } else {
183 *ret = 1;
184 }
185 return 0;
186}
187
e27c88fe
GH
188static 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') {
c64f27d4 196 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
e27c88fe
GH
197 return -1;
198 }
21c9f4cd 199 *ret = number;
e27c88fe 200 } else {
c64f27d4 201 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
e27c88fe
GH
202 return -1;
203 }
204 return 0;
205}
206
7695019b
GH
207static 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:
c64f27d4
MA
229 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
230 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
7695019b
GH
231 "kilobytes, megabytes, gigabytes and terabytes.\n");
232 return -1;
233 }
234 } else {
c64f27d4 235 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
7695019b
GH
236 return -1;
237 }
238 return 0;
239}
240
d3f24367
KW
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 */
258int set_option_parameter(QEMUOptionParameter *list, const char *name,
259 const char *value)
260{
67b1355b
GH
261 int flag;
262
d3f24367
KW
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:
3df04ac3 273 if (parse_option_bool(name, value, &flag) == -1)
67b1355b
GH
274 return -1;
275 list->value.n = flag;
d3f24367
KW
276 break;
277
278 case OPT_STRING:
279 if (value != NULL) {
d4c3fddd 280 list->value.s = qemu_strdup(value);
d3f24367
KW
281 } else {
282 fprintf(stderr, "Option '%s' needs a parameter\n", name);
283 return -1;
284 }
285 break;
286
287 case OPT_SIZE:
3df04ac3 288 if (parse_option_size(name, value, &list->value.n) == -1)
d3f24367 289 return -1;
d3f24367 290 break;
7695019b 291
d3f24367
KW
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 */
306int 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 */
334void free_option_parameters(QEMUOptionParameter *list)
335{
336 QEMUOptionParameter *cur = list;
337
338 while (cur && cur->name) {
339 if (cur->type == OPT_STRING) {
d4c3fddd 340 qemu_free(cur->value.s);
d3f24367
KW
341 }
342 cur++;
343 }
344
d4c3fddd 345 qemu_free(list);
d3f24367
KW
346}
347
b50cbabc
MK
348/*
349 * Count valid options in list
350 */
351static size_t count_option_parameters(QEMUOptionParameter *list)
352{
353 size_t num_options = 0;
354
355 while (list && list->name) {
356 num_options++;
357 list++;
358 }
359
360 return num_options;
361}
362
363/*
364 * Append an option list (list) to an option list (dest).
365 *
366 * If dest is NULL, a new copy of list is created.
367 *
368 * Returns a pointer to the first element of dest (or the newly allocated copy)
369 */
370QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
371 QEMUOptionParameter *list)
372{
373 size_t num_options, num_dest_options;
374
375 num_options = count_option_parameters(dest);
376 num_dest_options = num_options;
377
378 num_options += count_option_parameters(list);
379
380 dest = qemu_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
bd69fe84 381 dest[num_dest_options].name = NULL;
b50cbabc
MK
382
383 while (list && list->name) {
384 if (get_option_parameter(dest, list->name) == NULL) {
385 dest[num_dest_options++] = *list;
386 dest[num_dest_options].name = NULL;
387 }
388 list++;
389 }
390
391 return dest;
392}
393
d3f24367
KW
394/*
395 * Parses a parameter string (param) into an option list (dest).
396 *
397 * list is the templace is. If dest is NULL, a new copy of list is created for
398 * it. If list is NULL, this function fails.
399 *
400 * A parameter string consists of one or more parameters, separated by commas.
401 * Each parameter consists of its name and possibly of a value. In the latter
402 * case, the value is delimited by an = character. To specify a value which
403 * contains commas, double each comma so it won't be recognized as the end of
404 * the parameter.
405 *
406 * For more details of the parsing see above.
407 *
408 * Returns a pointer to the first element of dest (or the newly allocated copy)
409 * or NULL in error cases
410 */
411QEMUOptionParameter *parse_option_parameters(const char *param,
412 QEMUOptionParameter *list, QEMUOptionParameter *dest)
413{
d3f24367
KW
414 QEMUOptionParameter *allocated = NULL;
415 char name[256];
416 char value[256];
417 char *param_delim, *value_delim;
418 char next_delim;
419 size_t num_options;
420
421 if (list == NULL) {
422 return NULL;
423 }
424
425 if (dest == NULL) {
426 // Count valid options
b50cbabc 427 num_options = count_option_parameters(list);
d3f24367
KW
428
429 // Create a copy of the option list to fill in values
430 dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
431 allocated = dest;
432 memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
433 }
434
435 while (*param) {
436
437 // Find parameter name and value in the string
438 param_delim = strchr(param, ',');
439 value_delim = strchr(param, '=');
440
441 if (value_delim && (value_delim < param_delim || !param_delim)) {
442 next_delim = '=';
443 } else {
444 next_delim = ',';
445 value_delim = NULL;
446 }
447
448 param = get_opt_name(name, sizeof(name), param, next_delim);
449 if (value_delim) {
450 param = get_opt_value(value, sizeof(value), param + 1);
451 }
452 if (*param != '\0') {
453 param++;
454 }
455
456 // Set the parameter
457 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
458 goto fail;
459 }
460 }
461
462 return dest;
463
464fail:
465 // Only free the list if it was newly allocated
466 free_option_parameters(allocated);
467 return NULL;
468}
469
470/*
471 * Prints all options of a list that have a value to stdout
472 */
473void print_option_parameters(QEMUOptionParameter *list)
474{
475 while (list && list->name) {
476 switch (list->type) {
477 case OPT_STRING:
478 if (list->value.s != NULL) {
479 printf("%s='%s' ", list->name, list->value.s);
480 }
481 break;
482 case OPT_FLAG:
483 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
484 break;
485 case OPT_SIZE:
486 case OPT_NUMBER:
487 printf("%s=%" PRId64 " ", list->name, list->value.n);
488 break;
489 default:
490 printf("%s=(unkown type) ", list->name);
491 break;
492 }
493 list++;
494 }
495}
db08adf5
KW
496
497/*
498 * Prints an overview of all available options
499 */
500void print_option_help(QEMUOptionParameter *list)
501{
502 printf("Supported options:\n");
503 while (list && list->name) {
504 printf("%-16s %s\n", list->name,
505 list->help ? list->help : "No description available");
506 list++;
507 }
508}
e27c88fe
GH
509
510/* ------------------------------------------------------------------ */
511
512struct QemuOpt {
513 const char *name;
514 const char *str;
515
238431a9 516 const QemuOptDesc *desc;
e27c88fe 517 union {
1f5c1775 518 int boolean;
e27c88fe
GH
519 uint64_t uint;
520 } value;
521
522 QemuOpts *opts;
72cf2d4f 523 QTAILQ_ENTRY(QemuOpt) next;
e27c88fe
GH
524};
525
526struct QemuOpts {
b09417b9 527 char *id;
e27c88fe 528 QemuOptsList *list;
827b0813 529 Location loc;
dc9ca4ba 530 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
72cf2d4f 531 QTAILQ_ENTRY(QemuOpts) next;
e27c88fe
GH
532};
533
534static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
535{
536 QemuOpt *opt;
537
dc9ca4ba 538 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
e27c88fe
GH
539 if (strcmp(opt->name, name) != 0)
540 continue;
541 return opt;
542 }
543 return NULL;
544}
545
546const char *qemu_opt_get(QemuOpts *opts, const char *name)
547{
548 QemuOpt *opt = qemu_opt_find(opts, name);
549 return opt ? opt->str : NULL;
550}
551
552int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
553{
554 QemuOpt *opt = qemu_opt_find(opts, name);
555
556 if (opt == NULL)
557 return defval;
558 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
1f5c1775 559 return opt->value.boolean;
e27c88fe
GH
560}
561
562uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
563{
564 QemuOpt *opt = qemu_opt_find(opts, name);
565
566 if (opt == NULL)
567 return defval;
568 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
569 return opt->value.uint;
570}
571
572uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
573{
574 QemuOpt *opt = qemu_opt_find(opts, name);
575
576 if (opt == NULL)
577 return defval;
578 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
579 return opt->value.uint;
580}
581
582static int qemu_opt_parse(QemuOpt *opt)
583{
584 if (opt->desc == NULL)
585 return 0;
586 switch (opt->desc->type) {
587 case QEMU_OPT_STRING:
588 /* nothing */
589 return 0;
590 case QEMU_OPT_BOOL:
1f5c1775 591 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
e27c88fe
GH
592 case QEMU_OPT_NUMBER:
593 return parse_option_number(opt->name, opt->str, &opt->value.uint);
594 case QEMU_OPT_SIZE:
595 return parse_option_size(opt->name, opt->str, &opt->value.uint);
596 default:
597 abort();
598 }
599}
600
601static void qemu_opt_del(QemuOpt *opt)
602{
72cf2d4f 603 QTAILQ_REMOVE(&opt->opts->head, opt, next);
e27c88fe
GH
604 qemu_free((/* !const */ char*)opt->name);
605 qemu_free((/* !const */ char*)opt->str);
606 qemu_free(opt);
607}
608
609int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
610{
611 QemuOpt *opt;
238431a9 612 const QemuOptDesc *desc = opts->list->desc;
dc9ca4ba 613 int i;
e27c88fe 614
dc9ca4ba
MM
615 for (i = 0; desc[i].name != NULL; i++) {
616 if (strcmp(desc[i].name, name) == 0) {
617 break;
e27c88fe 618 }
dc9ca4ba
MM
619 }
620 if (desc[i].name == NULL) {
621 if (i == 0) {
622 /* empty list -> allow any */;
623 } else {
c64f27d4 624 qerror_report(QERR_INVALID_PARAMETER, name);
dc9ca4ba 625 return -1;
e27c88fe
GH
626 }
627 }
dc9ca4ba
MM
628
629 opt = qemu_mallocz(sizeof(*opt));
630 opt->name = qemu_strdup(name);
631 opt->opts = opts;
632 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
633 if (desc[i].name != NULL) {
634 opt->desc = desc+i;
635 }
e27c88fe
GH
636 if (value) {
637 opt->str = qemu_strdup(value);
638 }
639 if (qemu_opt_parse(opt) < 0) {
e27c88fe
GH
640 qemu_opt_del(opt);
641 return -1;
642 }
643 return 0;
644}
645
48026075
GH
646int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
647 int abort_on_failure)
648{
649 QemuOpt *opt;
650 int rc = 0;
651
72cf2d4f 652 QTAILQ_FOREACH(opt, &opts->head, next) {
48026075
GH
653 rc = func(opt->name, opt->str, opaque);
654 if (abort_on_failure && rc != 0)
655 break;
656 }
657 return rc;
658}
659
e27c88fe
GH
660QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
661{
662 QemuOpts *opts;
663
72cf2d4f 664 QTAILQ_FOREACH(opts, &list->head, next) {
e27c88fe
GH
665 if (!opts->id) {
666 continue;
667 }
668 if (strcmp(opts->id, id) != 0) {
669 continue;
670 }
671 return opts;
672 }
673 return NULL;
674}
675
676QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
677{
678 QemuOpts *opts = NULL;
679
680 if (id) {
681 opts = qemu_opts_find(list, id);
682 if (opts != NULL) {
683 if (fail_if_exists) {
975b63a4 684 qerror_report(QERR_DUPLICATE_ID, id, list->name);
e27c88fe
GH
685 return NULL;
686 } else {
687 return opts;
688 }
689 }
690 }
691 opts = qemu_mallocz(sizeof(*opts));
692 if (id) {
693 opts->id = qemu_strdup(id);
694 }
695 opts->list = list;
827b0813 696 loc_save(&opts->loc);
72cf2d4f
BS
697 QTAILQ_INIT(&opts->head);
698 QTAILQ_INSERT_TAIL(&list->head, opts, next);
e27c88fe
GH
699 return opts;
700}
701
702int qemu_opts_set(QemuOptsList *list, const char *id,
703 const char *name, const char *value)
704{
705 QemuOpts *opts;
706
707 opts = qemu_opts_create(list, id, 1);
708 if (opts == NULL) {
e27c88fe
GH
709 return -1;
710 }
711 return qemu_opt_set(opts, name, value);
712}
713
48026075
GH
714const char *qemu_opts_id(QemuOpts *opts)
715{
716 return opts->id;
717}
718
e27c88fe
GH
719void qemu_opts_del(QemuOpts *opts)
720{
721 QemuOpt *opt;
722
723 for (;;) {
72cf2d4f 724 opt = QTAILQ_FIRST(&opts->head);
e27c88fe
GH
725 if (opt == NULL)
726 break;
727 qemu_opt_del(opt);
728 }
72cf2d4f 729 QTAILQ_REMOVE(&opts->list->head, opts, next);
b09417b9 730 qemu_free(opts->id);
e27c88fe
GH
731 qemu_free(opts);
732}
733
734int qemu_opts_print(QemuOpts *opts, void *dummy)
735{
736 QemuOpt *opt;
737
738 fprintf(stderr, "%s: %s:", opts->list->name,
739 opts->id ? opts->id : "<noid>");
72cf2d4f 740 QTAILQ_FOREACH(opt, &opts->head, next) {
e27c88fe
GH
741 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
742 }
743 fprintf(stderr, "\n");
744 return 0;
745}
746
96729cbd 747int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
e27c88fe 748{
d318ff99 749 char option[128], value[1024];
e27c88fe
GH
750 const char *p,*pe,*pc;
751
2cfa571f 752 for (p = params; *p != '\0'; p++) {
e27c88fe
GH
753 pe = strchr(p, '=');
754 pc = strchr(p, ',');
755 if (!pe || (pc && pc < pe)) {
756 /* found "foo,more" */
757 if (p == params && firstname) {
758 /* implicitly named first option */
759 pstrcpy(option, sizeof(option), firstname);
760 p = get_opt_value(value, sizeof(value), p);
761 } else {
762 /* option without value, probably a flag */
763 p = get_opt_name(option, sizeof(option), p, ',');
96729cbd 764 if (strncmp(option, "no", 2) == 0) {
e27c88fe
GH
765 memmove(option, option+2, strlen(option+2)+1);
766 pstrcpy(value, sizeof(value), "off");
767 } else {
768 pstrcpy(value, sizeof(value), "on");
769 }
770 }
771 } else {
772 /* found "foo=bar,more" */
773 p = get_opt_name(option, sizeof(option), p, '=');
774 if (*p != '=') {
775 break;
776 }
777 p++;
778 p = get_opt_value(value, sizeof(value), p);
779 }
780 if (strcmp(option, "id") != 0) {
781 /* store and parse */
3df04ac3 782 if (qemu_opt_set(opts, option, value) == -1) {
96729cbd 783 return -1;
e27c88fe
GH
784 }
785 }
786 if (*p != ',') {
787 break;
788 }
e27c88fe 789 }
96729cbd
GH
790 return 0;
791}
792
8212c64f
MA
793QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
794 int permit_abbrev)
96729cbd 795{
8212c64f 796 const char *firstname;
d318ff99 797 char value[1024], *id = NULL;
96729cbd
GH
798 const char *p;
799 QemuOpts *opts;
800
8212c64f
MA
801 assert(!permit_abbrev || list->implied_opt_name);
802 firstname = permit_abbrev ? list->implied_opt_name : NULL;
803
96729cbd
GH
804 if (strncmp(params, "id=", 3) == 0) {
805 get_opt_value(value, sizeof(value), params+3);
d510c5cf 806 id = value;
96729cbd
GH
807 } else if ((p = strstr(params, ",id=")) != NULL) {
808 get_opt_value(value, sizeof(value), p+4);
d510c5cf 809 id = value;
96729cbd
GH
810 }
811 opts = qemu_opts_create(list, id, 1);
812 if (opts == NULL)
813 return NULL;
814
815 if (qemu_opts_do_parse(opts, params, firstname) != 0) {
816 qemu_opts_del(opts);
817 return NULL;
818 }
819
e27c88fe
GH
820 return opts;
821}
822
01e7f188
MA
823static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
824{
825 char buf[32];
826 const char *value;
827 int n;
828
829 if (!strcmp(key, "id")) {
830 return;
831 }
832
833 switch (qobject_type(obj)) {
834 case QTYPE_QSTRING:
835 value = qstring_get_str(qobject_to_qstring(obj));
836 break;
837 case QTYPE_QINT:
838 n = snprintf(buf, sizeof(buf), "%" PRId64,
839 qint_get_int(qobject_to_qint(obj)));
840 assert(n < sizeof(buf));
841 value = buf;
842 break;
843 case QTYPE_QFLOAT:
844 n = snprintf(buf, sizeof(buf), "%.17g",
845 qfloat_get_double(qobject_to_qfloat(obj)));
846 assert(n < sizeof(buf));
847 value = buf;
848 break;
849 case QTYPE_QBOOL:
d35215f8
BS
850 pstrcpy(buf, sizeof(buf),
851 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
01e7f188
MA
852 value = buf;
853 break;
854 default:
855 return;
856 }
857 qemu_opt_set(opaque, key, value);
858}
859
860/*
861 * Create QemuOpts from a QDict.
862 * Use value of key "id" as ID if it exists and is a QString.
863 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
864 * other types are silently ignored.
865 */
866QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
867{
868 QemuOpts *opts;
869
870 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
871 if (opts == NULL)
872 return NULL;
873
874 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
875 return opts;
876}
877
878/*
879 * Convert from QemuOpts to QDict.
880 * The QDict values are of type QString.
881 * TODO We'll want to use types appropriate for opt->desc->type, but
882 * this is enough for now.
883 */
884QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
885{
886 QemuOpt *opt;
887 QObject *val;
888
889 if (!qdict) {
890 qdict = qdict_new();
891 }
892 if (opts->id) {
893 qdict_put(qdict, "id", qstring_from_str(opts->id));
894 }
895 QTAILQ_FOREACH(opt, &opts->head, next) {
896 val = QOBJECT(qstring_from_str(opt->str));
897 qdict_put_obj(qdict, opt->name, val);
898 }
899 return qdict;
900}
901
5dc519ef
MM
902/* Validate parsed opts against descriptions where no
903 * descriptions were provided in the QemuOptsList.
904 */
238431a9 905int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
5dc519ef
MM
906{
907 QemuOpt *opt;
908
909 assert(opts->list->desc[0].name == NULL);
910
911 QTAILQ_FOREACH(opt, &opts->head, next) {
912 int i;
913
914 for (i = 0; desc[i].name != NULL; i++) {
915 if (strcmp(desc[i].name, opt->name) == 0) {
916 break;
917 }
918 }
919 if (desc[i].name == NULL) {
db716e9c 920 qerror_report(QERR_INVALID_PARAMETER, opt->name);
5dc519ef
MM
921 return -1;
922 }
923
924 opt->desc = &desc[i];
925
926 if (qemu_opt_parse(opt) < 0) {
927 return -1;
928 }
929 }
930
931 return 0;
932}
933
e27c88fe
GH
934int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
935 int abort_on_failure)
936{
827b0813 937 Location loc;
e27c88fe
GH
938 QemuOpts *opts;
939 int rc = 0;
940
827b0813 941 loc_push_none(&loc);
72cf2d4f 942 QTAILQ_FOREACH(opts, &list->head, next) {
827b0813 943 loc_restore(&opts->loc);
4a2594dd 944 rc |= func(opts, opaque);
e27c88fe
GH
945 if (abort_on_failure && rc != 0)
946 break;
947 }
827b0813 948 loc_pop(&loc);
e27c88fe
GH
949 return rc;
950}