]> git.proxmox.com Git - qemu.git/blob - qemu-option.c
qemu-option: parse_option_number(): use error_set()
[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 "error.h"
34 #include "qerror.h"
35
36 /*
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
39 *
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.
43 *
44 * The return value is the position of the delimiter/zero byte after the option
45 * name in p.
46 */
47 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
48 {
49 char *q;
50
51 q = buf;
52 while (*p != '\0' && *p != delim) {
53 if (q && (q - buf) < buf_size - 1)
54 *q++ = *p;
55 p++;
56 }
57 if (q)
58 *q = '\0';
59
60 return p;
61 }
62
63 /*
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
66 *
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.
70 */
71 const char *get_opt_value(char *buf, int buf_size, const char *p)
72 {
73 char *q;
74
75 q = buf;
76 while (*p != '\0') {
77 if (*p == ',') {
78 if (*(p + 1) != ',')
79 break;
80 p++;
81 }
82 if (q && (q - buf) < buf_size - 1)
83 *q++ = *p;
84 p++;
85 }
86 if (q)
87 *q = '\0';
88
89 return p;
90 }
91
92 int get_next_param_value(char *buf, int buf_size,
93 const char *tag, const char **pstr)
94 {
95 const char *p;
96 char option[128];
97
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)++;
108 }
109 return strlen(buf);
110 } else {
111 p = get_opt_value(NULL, 0, p);
112 }
113 if (*p != ',')
114 break;
115 p++;
116 }
117 return 0;
118 }
119
120 int get_param_value(char *buf, int buf_size,
121 const char *tag, const char *str)
122 {
123 return get_next_param_value(buf, buf_size, tag, &str);
124 }
125
126 int check_params(char *buf, int buf_size,
127 const char * const *params, const char *str)
128 {
129 const char *p;
130 int i;
131
132 p = str;
133 while (*p != '\0') {
134 p = get_opt_name(buf, buf_size, p, '=');
135 if (*p != '=') {
136 return -1;
137 }
138 p++;
139 for (i = 0; params[i] != NULL; i++) {
140 if (!strcmp(params[i], buf)) {
141 break;
142 }
143 }
144 if (params[i] == NULL) {
145 return -1;
146 }
147 p = get_opt_value(NULL, 0, p);
148 if (*p != ',') {
149 break;
150 }
151 p++;
152 }
153 return 0;
154 }
155
156 /*
157 * Searches an option list for an option with the given name
158 */
159 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
160 const char *name)
161 {
162 while (list && list->name) {
163 if (!strcmp(list->name, name)) {
164 return list;
165 }
166 list++;
167 }
168
169 return NULL;
170 }
171
172 static int parse_option_bool(const char *name, const char *value, bool *ret)
173 {
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;
182 }
183 } else {
184 *ret = 1;
185 }
186 return 0;
187 }
188
189 static void parse_option_number(const char *name, const char *value,
190 uint64_t *ret, Error **errp)
191 {
192 char *postfix;
193 uint64_t number;
194
195 if (value != NULL) {
196 number = strtoull(value, &postfix, 0);
197 if (*postfix != '\0') {
198 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
199 return;
200 }
201 *ret = number;
202 } else {
203 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
204 }
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 /* 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;
237 }
238 } else {
239 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
240 return -1;
241 }
242 return 0;
243 }
244
245 /*
246 * Sets the value of a parameter in a given option list. The parsing of the
247 * value depends on the type of option:
248 *
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)
252 *
253 * OPT_STRING (uses value.s):
254 * value is strdup()ed and assigned as option value
255 *
256 * OPT_SIZE (uses value.n):
257 * The value is converted to an integer. Suffixes for kilobytes etc. are
258 * allowed (powers of 1024).
259 *
260 * Returns 0 on succes, -1 in error cases
261 */
262 int set_option_parameter(QEMUOptionParameter *list, const char *name,
263 const char *value)
264 {
265 bool flag;
266
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;
272 }
273
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;
281
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;
288 }
289 break;
290
291 case OPT_SIZE:
292 if (parse_option_size(name, value, &list->value.n) == -1)
293 return -1;
294 break;
295
296 default:
297 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
298 return -1;
299 }
300
301 return 0;
302 }
303
304 /*
305 * Sets the given parameter to an integer instead of a string.
306 * This function cannot be used to set string options.
307 *
308 * Returns 0 on success, -1 in error cases
309 */
310 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
311 uint64_t value)
312 {
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;
318 }
319
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;
327
328 default:
329 return -1;
330 }
331
332 return 0;
333 }
334
335 /*
336 * Frees a option list. If it contains strings, the strings are freed as well.
337 */
338 void free_option_parameters(QEMUOptionParameter *list)
339 {
340 QEMUOptionParameter *cur = list;
341
342 while (cur && cur->name) {
343 if (cur->type == OPT_STRING) {
344 g_free(cur->value.s);
345 }
346 cur++;
347 }
348
349 g_free(list);
350 }
351
352 /*
353 * Count valid options in list
354 */
355 static size_t count_option_parameters(QEMUOptionParameter *list)
356 {
357 size_t num_options = 0;
358
359 while (list && list->name) {
360 num_options++;
361 list++;
362 }
363
364 return num_options;
365 }
366
367 /*
368 * Append an option list (list) to an option list (dest).
369 *
370 * If dest is NULL, a new copy of list is created.
371 *
372 * Returns a pointer to the first element of dest (or the newly allocated copy)
373 */
374 QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
375 QEMUOptionParameter *list)
376 {
377 size_t num_options, num_dest_options;
378
379 num_options = count_option_parameters(dest);
380 num_dest_options = num_options;
381
382 num_options += count_option_parameters(list);
383
384 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
385 dest[num_dest_options].name = NULL;
386
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;
391 }
392 list++;
393 }
394
395 return dest;
396 }
397
398 /*
399 * Parses a parameter string (param) into an option list (dest).
400 *
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.
403 *
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.
409 *
410 * For more details of the parsing see above.
411 *
412 * Returns a pointer to the first element of dest (or the newly allocated copy)
413 * or NULL in error cases
414 */
415 QEMUOptionParameter *parse_option_parameters(const char *param,
416 QEMUOptionParameter *list, QEMUOptionParameter *dest)
417 {
418 QEMUOptionParameter *allocated = NULL;
419 char name[256];
420 char value[256];
421 char *param_delim, *value_delim;
422 char next_delim;
423
424 if (list == NULL) {
425 return NULL;
426 }
427
428 if (dest == NULL) {
429 dest = allocated = append_option_parameters(NULL, list);
430 }
431
432 while (*param) {
433
434 // Find parameter name and value in the string
435 param_delim = strchr(param, ',');
436 value_delim = strchr(param, '=');
437
438 if (value_delim && (value_delim < param_delim || !param_delim)) {
439 next_delim = '=';
440 } else {
441 next_delim = ',';
442 value_delim = NULL;
443 }
444
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);
448 }
449 if (*param != '\0') {
450 param++;
451 }
452
453 // Set the parameter
454 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
455 goto fail;
456 }
457 }
458
459 return dest;
460
461 fail:
462 // Only free the list if it was newly allocated
463 free_option_parameters(allocated);
464 return NULL;
465 }
466
467 /*
468 * Prints all options of a list that have a value to stdout
469 */
470 void print_option_parameters(QEMUOptionParameter *list)
471 {
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);
477 }
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;
489 }
490 list++;
491 }
492 }
493
494 /*
495 * Prints an overview of all available options
496 */
497 void print_option_help(QEMUOptionParameter *list)
498 {
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++;
504 }
505 }
506
507 /* ------------------------------------------------------------------ */
508
509 struct QemuOpt {
510 const char *name;
511 const char *str;
512
513 const QemuOptDesc *desc;
514 union {
515 bool boolean;
516 uint64_t uint;
517 } value;
518
519 QemuOpts *opts;
520 QTAILQ_ENTRY(QemuOpt) next;
521 };
522
523 struct QemuOpts {
524 char *id;
525 QemuOptsList *list;
526 Location loc;
527 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
528 QTAILQ_ENTRY(QemuOpts) next;
529 };
530
531 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
532 {
533 QemuOpt *opt;
534
535 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
536 if (strcmp(opt->name, name) != 0)
537 continue;
538 return opt;
539 }
540 return NULL;
541 }
542
543 const char *qemu_opt_get(QemuOpts *opts, const char *name)
544 {
545 QemuOpt *opt = qemu_opt_find(opts, name);
546 return opt ? opt->str : NULL;
547 }
548
549 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
550 {
551 QemuOpt *opt = qemu_opt_find(opts, name);
552
553 if (opt == NULL)
554 return defval;
555 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
556 return opt->value.boolean;
557 }
558
559 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
560 {
561 QemuOpt *opt = qemu_opt_find(opts, name);
562
563 if (opt == NULL)
564 return defval;
565 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
566 return opt->value.uint;
567 }
568
569 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
570 {
571 QemuOpt *opt = qemu_opt_find(opts, name);
572
573 if (opt == NULL)
574 return defval;
575 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
576 return opt->value.uint;
577 }
578
579 static int qemu_opt_parse(QemuOpt *opt)
580 {
581 Error *local_err = NULL;
582
583 if (opt->desc == NULL)
584 return 0;
585
586 switch (opt->desc->type) {
587 case QEMU_OPT_STRING:
588 /* nothing */
589 return 0;
590 case QEMU_OPT_BOOL:
591 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
592 case QEMU_OPT_NUMBER:
593 parse_option_number(opt->name, opt->str, &opt->value.uint,
594 &local_err);
595 break;
596 case QEMU_OPT_SIZE:
597 return parse_option_size(opt->name, opt->str, &opt->value.uint);
598 default:
599 abort();
600 }
601
602 if (error_is_set(&local_err)) {
603 qerror_report_err(local_err);
604 error_free(local_err);
605 return -1;
606 }
607
608 return 0;
609 }
610
611 static void qemu_opt_del(QemuOpt *opt)
612 {
613 QTAILQ_REMOVE(&opt->opts->head, opt, next);
614 g_free((/* !const */ char*)opt->name);
615 g_free((/* !const */ char*)opt->str);
616 g_free(opt);
617 }
618
619 static int opt_set(QemuOpts *opts, const char *name, const char *value,
620 bool prepend)
621 {
622 QemuOpt *opt;
623 const QemuOptDesc *desc = opts->list->desc;
624 int i;
625
626 for (i = 0; desc[i].name != NULL; i++) {
627 if (strcmp(desc[i].name, name) == 0) {
628 break;
629 }
630 }
631 if (desc[i].name == NULL) {
632 if (i == 0) {
633 /* empty list -> allow any */;
634 } else {
635 qerror_report(QERR_INVALID_PARAMETER, name);
636 return -1;
637 }
638 }
639
640 opt = g_malloc0(sizeof(*opt));
641 opt->name = g_strdup(name);
642 opt->opts = opts;
643 if (prepend) {
644 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
645 } else {
646 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
647 }
648 if (desc[i].name != NULL) {
649 opt->desc = desc+i;
650 }
651 if (value) {
652 opt->str = g_strdup(value);
653 }
654 if (qemu_opt_parse(opt) < 0) {
655 qemu_opt_del(opt);
656 return -1;
657 }
658 return 0;
659 }
660
661 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
662 {
663 return opt_set(opts, name, value, false);
664 }
665
666 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
667 {
668 QemuOpt *opt;
669 const QemuOptDesc *desc = opts->list->desc;
670 int i;
671
672 for (i = 0; desc[i].name != NULL; i++) {
673 if (strcmp(desc[i].name, name) == 0) {
674 break;
675 }
676 }
677 if (desc[i].name == NULL) {
678 if (i == 0) {
679 /* empty list -> allow any */;
680 } else {
681 qerror_report(QERR_INVALID_PARAMETER, name);
682 return -1;
683 }
684 }
685
686 opt = g_malloc0(sizeof(*opt));
687 opt->name = g_strdup(name);
688 opt->opts = opts;
689 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
690 if (desc[i].name != NULL) {
691 opt->desc = desc+i;
692 }
693 opt->value.boolean = !!val;
694 return 0;
695 }
696
697 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
698 int abort_on_failure)
699 {
700 QemuOpt *opt;
701 int rc = 0;
702
703 QTAILQ_FOREACH(opt, &opts->head, next) {
704 rc = func(opt->name, opt->str, opaque);
705 if (abort_on_failure && rc != 0)
706 break;
707 }
708 return rc;
709 }
710
711 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
712 {
713 QemuOpts *opts;
714
715 QTAILQ_FOREACH(opts, &list->head, next) {
716 if (!opts->id) {
717 if (!id) {
718 return opts;
719 }
720 continue;
721 }
722 if (strcmp(opts->id, id) != 0) {
723 continue;
724 }
725 return opts;
726 }
727 return NULL;
728 }
729
730 static int id_wellformed(const char *id)
731 {
732 int i;
733
734 if (!qemu_isalpha(id[0])) {
735 return 0;
736 }
737 for (i = 1; id[i]; i++) {
738 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
739 return 0;
740 }
741 }
742 return 1;
743 }
744
745 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
746 int fail_if_exists, Error **errp)
747 {
748 QemuOpts *opts = NULL;
749
750 if (id) {
751 if (!id_wellformed(id)) {
752 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
753 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
754 return NULL;
755 }
756 opts = qemu_opts_find(list, id);
757 if (opts != NULL) {
758 if (fail_if_exists && !list->merge_lists) {
759 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
760 return NULL;
761 } else {
762 return opts;
763 }
764 }
765 } else if (list->merge_lists) {
766 opts = qemu_opts_find(list, NULL);
767 if (opts) {
768 return opts;
769 }
770 }
771 opts = g_malloc0(sizeof(*opts));
772 if (id) {
773 opts->id = g_strdup(id);
774 }
775 opts->list = list;
776 loc_save(&opts->loc);
777 QTAILQ_INIT(&opts->head);
778 QTAILQ_INSERT_TAIL(&list->head, opts, next);
779 return opts;
780 }
781
782 void qemu_opts_reset(QemuOptsList *list)
783 {
784 QemuOpts *opts, *next_opts;
785
786 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
787 qemu_opts_del(opts);
788 }
789 }
790
791 void qemu_opts_loc_restore(QemuOpts *opts)
792 {
793 loc_restore(&opts->loc);
794 }
795
796 int qemu_opts_set(QemuOptsList *list, const char *id,
797 const char *name, const char *value)
798 {
799 QemuOpts *opts;
800 Error *local_err = NULL;
801
802 opts = qemu_opts_create(list, id, 1, &local_err);
803 if (error_is_set(&local_err)) {
804 qerror_report_err(local_err);
805 error_free(local_err);
806 return -1;
807 }
808 return qemu_opt_set(opts, name, value);
809 }
810
811 const char *qemu_opts_id(QemuOpts *opts)
812 {
813 return opts->id;
814 }
815
816 void qemu_opts_del(QemuOpts *opts)
817 {
818 QemuOpt *opt;
819
820 for (;;) {
821 opt = QTAILQ_FIRST(&opts->head);
822 if (opt == NULL)
823 break;
824 qemu_opt_del(opt);
825 }
826 QTAILQ_REMOVE(&opts->list->head, opts, next);
827 g_free(opts->id);
828 g_free(opts);
829 }
830
831 int qemu_opts_print(QemuOpts *opts, void *dummy)
832 {
833 QemuOpt *opt;
834
835 fprintf(stderr, "%s: %s:", opts->list->name,
836 opts->id ? opts->id : "<noid>");
837 QTAILQ_FOREACH(opt, &opts->head, next) {
838 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
839 }
840 fprintf(stderr, "\n");
841 return 0;
842 }
843
844 static int opts_do_parse(QemuOpts *opts, const char *params,
845 const char *firstname, bool prepend)
846 {
847 char option[128], value[1024];
848 const char *p,*pe,*pc;
849
850 for (p = params; *p != '\0'; p++) {
851 pe = strchr(p, '=');
852 pc = strchr(p, ',');
853 if (!pe || (pc && pc < pe)) {
854 /* found "foo,more" */
855 if (p == params && firstname) {
856 /* implicitly named first option */
857 pstrcpy(option, sizeof(option), firstname);
858 p = get_opt_value(value, sizeof(value), p);
859 } else {
860 /* option without value, probably a flag */
861 p = get_opt_name(option, sizeof(option), p, ',');
862 if (strncmp(option, "no", 2) == 0) {
863 memmove(option, option+2, strlen(option+2)+1);
864 pstrcpy(value, sizeof(value), "off");
865 } else {
866 pstrcpy(value, sizeof(value), "on");
867 }
868 }
869 } else {
870 /* found "foo=bar,more" */
871 p = get_opt_name(option, sizeof(option), p, '=');
872 if (*p != '=') {
873 break;
874 }
875 p++;
876 p = get_opt_value(value, sizeof(value), p);
877 }
878 if (strcmp(option, "id") != 0) {
879 /* store and parse */
880 if (opt_set(opts, option, value, prepend) == -1) {
881 return -1;
882 }
883 }
884 if (*p != ',') {
885 break;
886 }
887 }
888 return 0;
889 }
890
891 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
892 {
893 return opts_do_parse(opts, params, firstname, false);
894 }
895
896 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
897 int permit_abbrev, bool defaults)
898 {
899 const char *firstname;
900 char value[1024], *id = NULL;
901 const char *p;
902 QemuOpts *opts;
903 Error *local_err = NULL;
904
905 assert(!permit_abbrev || list->implied_opt_name);
906 firstname = permit_abbrev ? list->implied_opt_name : NULL;
907
908 if (strncmp(params, "id=", 3) == 0) {
909 get_opt_value(value, sizeof(value), params+3);
910 id = value;
911 } else if ((p = strstr(params, ",id=")) != NULL) {
912 get_opt_value(value, sizeof(value), p+4);
913 id = value;
914 }
915 if (defaults) {
916 if (!id && !QTAILQ_EMPTY(&list->head)) {
917 opts = qemu_opts_find(list, NULL);
918 } else {
919 opts = qemu_opts_create(list, id, 0, &local_err);
920 }
921 } else {
922 opts = qemu_opts_create(list, id, 1, &local_err);
923 }
924 if (opts == NULL) {
925 if (error_is_set(&local_err)) {
926 qerror_report_err(local_err);
927 error_free(local_err);
928 }
929 return NULL;
930 }
931
932 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
933 qemu_opts_del(opts);
934 return NULL;
935 }
936
937 return opts;
938 }
939
940 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
941 int permit_abbrev)
942 {
943 return opts_parse(list, params, permit_abbrev, false);
944 }
945
946 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
947 int permit_abbrev)
948 {
949 QemuOpts *opts;
950
951 opts = opts_parse(list, params, permit_abbrev, true);
952 assert(opts);
953 }
954
955 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
956 {
957 char buf[32];
958 const char *value;
959 int n;
960
961 if (!strcmp(key, "id")) {
962 return;
963 }
964
965 switch (qobject_type(obj)) {
966 case QTYPE_QSTRING:
967 value = qstring_get_str(qobject_to_qstring(obj));
968 break;
969 case QTYPE_QINT:
970 n = snprintf(buf, sizeof(buf), "%" PRId64,
971 qint_get_int(qobject_to_qint(obj)));
972 assert(n < sizeof(buf));
973 value = buf;
974 break;
975 case QTYPE_QFLOAT:
976 n = snprintf(buf, sizeof(buf), "%.17g",
977 qfloat_get_double(qobject_to_qfloat(obj)));
978 assert(n < sizeof(buf));
979 value = buf;
980 break;
981 case QTYPE_QBOOL:
982 pstrcpy(buf, sizeof(buf),
983 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
984 value = buf;
985 break;
986 default:
987 return;
988 }
989 qemu_opt_set(opaque, key, value);
990 }
991
992 /*
993 * Create QemuOpts from a QDict.
994 * Use value of key "id" as ID if it exists and is a QString.
995 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
996 * other types are silently ignored.
997 */
998 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
999 {
1000 QemuOpts *opts;
1001 Error *local_err = NULL;
1002
1003 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1004 &local_err);
1005 if (error_is_set(&local_err)) {
1006 qerror_report_err(local_err);
1007 error_free(local_err);
1008 return NULL;
1009 }
1010
1011 assert(opts != NULL);
1012 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
1013 return opts;
1014 }
1015
1016 /*
1017 * Convert from QemuOpts to QDict.
1018 * The QDict values are of type QString.
1019 * TODO We'll want to use types appropriate for opt->desc->type, but
1020 * this is enough for now.
1021 */
1022 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1023 {
1024 QemuOpt *opt;
1025 QObject *val;
1026
1027 if (!qdict) {
1028 qdict = qdict_new();
1029 }
1030 if (opts->id) {
1031 qdict_put(qdict, "id", qstring_from_str(opts->id));
1032 }
1033 QTAILQ_FOREACH(opt, &opts->head, next) {
1034 val = QOBJECT(qstring_from_str(opt->str));
1035 qdict_put_obj(qdict, opt->name, val);
1036 }
1037 return qdict;
1038 }
1039
1040 /* Validate parsed opts against descriptions where no
1041 * descriptions were provided in the QemuOptsList.
1042 */
1043 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
1044 {
1045 QemuOpt *opt;
1046
1047 assert(opts->list->desc[0].name == NULL);
1048
1049 QTAILQ_FOREACH(opt, &opts->head, next) {
1050 int i;
1051
1052 for (i = 0; desc[i].name != NULL; i++) {
1053 if (strcmp(desc[i].name, opt->name) == 0) {
1054 break;
1055 }
1056 }
1057 if (desc[i].name == NULL) {
1058 qerror_report(QERR_INVALID_PARAMETER, opt->name);
1059 return -1;
1060 }
1061
1062 opt->desc = &desc[i];
1063
1064 if (qemu_opt_parse(opt) < 0) {
1065 return -1;
1066 }
1067 }
1068
1069 return 0;
1070 }
1071
1072 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1073 int abort_on_failure)
1074 {
1075 Location loc;
1076 QemuOpts *opts;
1077 int rc = 0;
1078
1079 loc_push_none(&loc);
1080 QTAILQ_FOREACH(opts, &list->head, next) {
1081 loc_restore(&opts->loc);
1082 rc |= func(opts, opaque);
1083 if (abort_on_failure && rc != 0)
1084 break;
1085 }
1086 loc_pop(&loc);
1087 return rc;
1088 }