qemu-option: Replace opt_set() by cleaner opt_validate()
[qemu.git] / util / 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 "qemu/osdep.h"
27
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qapi/qmp/qbool.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qapi/qmp/qerror.h"
35 #include "qemu/option_int.h"
36 #include "qemu/cutils.h"
37 #include "qemu/id.h"
38 #include "qemu/help_option.h"
39
40 /*
41 * Extracts the name of an option from the parameter string (p points at the
42 * first byte of the option name)
43 *
44 * The option name is delimited by delim (usually , or =) or the string end
45 * and is copied into option. The caller is responsible for free'ing option
46 * when no longer required.
47 *
48 * The return value is the position of the delimiter/zero byte after the option
49 * name in p.
50 */
51 static const char *get_opt_name(const char *p, char **option, char delim)
52 {
53 char *offset = strchr(p, delim);
54
55 if (offset) {
56 *option = g_strndup(p, offset - p);
57 return offset;
58 } else {
59 *option = g_strdup(p);
60 return p + strlen(p);
61 }
62 }
63
64 /*
65 * Extracts the value of an option from the parameter string p (p points at the
66 * first byte of the option value)
67 *
68 * This function is comparable to get_opt_name with the difference that the
69 * delimiter is fixed to be comma which starts a new option. To specify an
70 * option value that contains commas, double each comma.
71 */
72 const char *get_opt_value(const char *p, char **value)
73 {
74 size_t capacity = 0, length;
75 const char *offset;
76
77 *value = NULL;
78 while (1) {
79 offset = qemu_strchrnul(p, ',');
80 length = offset - p;
81 if (*offset != '\0' && *(offset + 1) == ',') {
82 length++;
83 }
84 *value = g_renew(char, *value, capacity + length + 1);
85 strncpy(*value + capacity, p, length);
86 (*value)[capacity + length] = '\0';
87 capacity += length;
88 if (*offset == '\0' ||
89 *(offset + 1) != ',') {
90 break;
91 }
92
93 p += (offset - p) + 2;
94 }
95
96 return offset;
97 }
98
99 static void parse_option_bool(const char *name, const char *value, bool *ret,
100 Error **errp)
101 {
102 if (!strcmp(value, "on")) {
103 *ret = 1;
104 } else if (!strcmp(value, "off")) {
105 *ret = 0;
106 } else {
107 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
108 name, "'on' or 'off'");
109 }
110 }
111
112 static void parse_option_number(const char *name, const char *value,
113 uint64_t *ret, Error **errp)
114 {
115 uint64_t number;
116 int err;
117
118 err = qemu_strtou64(value, NULL, 0, &number);
119 if (err == -ERANGE) {
120 error_setg(errp, "Value '%s' is too large for parameter '%s'",
121 value, name);
122 return;
123 }
124 if (err) {
125 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
126 return;
127 }
128 *ret = number;
129 }
130
131 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
132 const char *name)
133 {
134 int i;
135
136 for (i = 0; desc[i].name != NULL; i++) {
137 if (strcmp(desc[i].name, name) == 0) {
138 return &desc[i];
139 }
140 }
141
142 return NULL;
143 }
144
145 static const char *find_default_by_name(QemuOpts *opts, const char *name)
146 {
147 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
148
149 return desc ? desc->def_value_str : NULL;
150 }
151
152 void parse_option_size(const char *name, const char *value,
153 uint64_t *ret, Error **errp)
154 {
155 uint64_t size;
156 int err;
157
158 err = qemu_strtosz(value, NULL, &size);
159 if (err == -ERANGE) {
160 error_setg(errp, "Value '%s' is out of range for parameter '%s'",
161 value, name);
162 return;
163 }
164 if (err) {
165 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
166 "a non-negative number below 2^64");
167 error_append_hint(errp, "Optional suffix k, M, G, T, P or E means"
168 " kilo-, mega-, giga-, tera-, peta-\n"
169 "and exabytes, respectively.\n");
170 return;
171 }
172 *ret = size;
173 }
174
175 static const char *opt_type_to_string(enum QemuOptType type)
176 {
177 switch (type) {
178 case QEMU_OPT_STRING:
179 return "str";
180 case QEMU_OPT_BOOL:
181 return "bool (on/off)";
182 case QEMU_OPT_NUMBER:
183 return "num";
184 case QEMU_OPT_SIZE:
185 return "size";
186 }
187
188 g_assert_not_reached();
189 }
190
191 /**
192 * Print the list of options available in the given list. If
193 * @print_caption is true, a caption (including the list name, if it
194 * exists) is printed. The options itself will be indented, so
195 * @print_caption should only be set to false if the caller prints its
196 * own custom caption (so that the indentation makes sense).
197 */
198 void qemu_opts_print_help(QemuOptsList *list, bool print_caption)
199 {
200 QemuOptDesc *desc;
201 int i;
202 GPtrArray *array = g_ptr_array_new();
203
204 assert(list);
205 desc = list->desc;
206 while (desc && desc->name) {
207 GString *str = g_string_new(NULL);
208 g_string_append_printf(str, " %s=<%s>", desc->name,
209 opt_type_to_string(desc->type));
210 if (desc->help) {
211 if (str->len < 24) {
212 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
213 }
214 g_string_append_printf(str, " - %s", desc->help);
215 }
216 g_ptr_array_add(array, g_string_free(str, false));
217 desc++;
218 }
219
220 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
221 if (print_caption && array->len > 0) {
222 if (list->name) {
223 printf("%s options:\n", list->name);
224 } else {
225 printf("Options:\n");
226 }
227 } else if (array->len == 0) {
228 if (list->name) {
229 printf("There are no options for %s.\n", list->name);
230 } else {
231 printf("No options available.\n");
232 }
233 }
234 for (i = 0; i < array->len; i++) {
235 printf("%s\n", (char *)array->pdata[i]);
236 }
237 g_ptr_array_set_free_func(array, g_free);
238 g_ptr_array_free(array, true);
239
240 }
241 /* ------------------------------------------------------------------ */
242
243 QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
244 {
245 QemuOpt *opt;
246
247 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
248 if (strcmp(opt->name, name) != 0)
249 continue;
250 return opt;
251 }
252 return NULL;
253 }
254
255 static void qemu_opt_del(QemuOpt *opt)
256 {
257 QTAILQ_REMOVE(&opt->opts->head, opt, next);
258 g_free(opt->name);
259 g_free(opt->str);
260 g_free(opt);
261 }
262
263 /* qemu_opt_set allows many settings for the same option.
264 * This function deletes all settings for an option.
265 */
266 static void qemu_opt_del_all(QemuOpts *opts, const char *name)
267 {
268 QemuOpt *opt, *next_opt;
269
270 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
271 if (!strcmp(opt->name, name)) {
272 qemu_opt_del(opt);
273 }
274 }
275 }
276
277 const char *qemu_opt_get(QemuOpts *opts, const char *name)
278 {
279 QemuOpt *opt;
280
281 if (opts == NULL) {
282 return NULL;
283 }
284
285 opt = qemu_opt_find(opts, name);
286 if (!opt) {
287 return find_default_by_name(opts, name);
288 }
289
290 return opt->str;
291 }
292
293 void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name)
294 {
295 iter->opts = opts;
296 iter->opt = QTAILQ_FIRST(&opts->head);
297 iter->name = name;
298 }
299
300 const char *qemu_opt_iter_next(QemuOptsIter *iter)
301 {
302 QemuOpt *ret = iter->opt;
303 if (iter->name) {
304 while (ret && !g_str_equal(iter->name, ret->name)) {
305 ret = QTAILQ_NEXT(ret, next);
306 }
307 }
308 iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL;
309 return ret ? ret->str : NULL;
310 }
311
312 /* Get a known option (or its default) and remove it from the list
313 * all in one action. Return a malloced string of the option value.
314 * Result must be freed by caller with g_free().
315 */
316 char *qemu_opt_get_del(QemuOpts *opts, const char *name)
317 {
318 QemuOpt *opt;
319 char *str;
320
321 if (opts == NULL) {
322 return NULL;
323 }
324
325 opt = qemu_opt_find(opts, name);
326 if (!opt) {
327 return g_strdup(find_default_by_name(opts, name));
328 }
329 str = opt->str;
330 opt->str = NULL;
331 qemu_opt_del_all(opts, name);
332 return str;
333 }
334
335 bool qemu_opt_has_help_opt(QemuOpts *opts)
336 {
337 QemuOpt *opt;
338
339 QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) {
340 if (is_help_option(opt->name)) {
341 return true;
342 }
343 }
344 return false;
345 }
346
347 static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
348 bool defval, bool del)
349 {
350 QemuOpt *opt;
351 const char *def_val;
352 bool ret = defval;
353
354 if (opts == NULL) {
355 return ret;
356 }
357
358 opt = qemu_opt_find(opts, name);
359 if (opt == NULL) {
360 def_val = find_default_by_name(opts, name);
361 if (def_val) {
362 parse_option_bool(name, def_val, &ret, &error_abort);
363 }
364 return ret;
365 }
366 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
367 ret = opt->value.boolean;
368 if (del) {
369 qemu_opt_del_all(opts, name);
370 }
371 return ret;
372 }
373
374 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
375 {
376 return qemu_opt_get_bool_helper(opts, name, defval, false);
377 }
378
379 bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
380 {
381 return qemu_opt_get_bool_helper(opts, name, defval, true);
382 }
383
384 static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
385 uint64_t defval, bool del)
386 {
387 QemuOpt *opt;
388 const char *def_val;
389 uint64_t ret = defval;
390
391 if (opts == NULL) {
392 return ret;
393 }
394
395 opt = qemu_opt_find(opts, name);
396 if (opt == NULL) {
397 def_val = find_default_by_name(opts, name);
398 if (def_val) {
399 parse_option_number(name, def_val, &ret, &error_abort);
400 }
401 return ret;
402 }
403 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
404 ret = opt->value.uint;
405 if (del) {
406 qemu_opt_del_all(opts, name);
407 }
408 return ret;
409 }
410
411 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
412 {
413 return qemu_opt_get_number_helper(opts, name, defval, false);
414 }
415
416 uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
417 uint64_t defval)
418 {
419 return qemu_opt_get_number_helper(opts, name, defval, true);
420 }
421
422 static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
423 uint64_t defval, bool del)
424 {
425 QemuOpt *opt;
426 const char *def_val;
427 uint64_t ret = defval;
428
429 if (opts == NULL) {
430 return ret;
431 }
432
433 opt = qemu_opt_find(opts, name);
434 if (opt == NULL) {
435 def_val = find_default_by_name(opts, name);
436 if (def_val) {
437 parse_option_size(name, def_val, &ret, &error_abort);
438 }
439 return ret;
440 }
441 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
442 ret = opt->value.uint;
443 if (del) {
444 qemu_opt_del_all(opts, name);
445 }
446 return ret;
447 }
448
449 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
450 {
451 return qemu_opt_get_size_helper(opts, name, defval, false);
452 }
453
454 uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
455 uint64_t defval)
456 {
457 return qemu_opt_get_size_helper(opts, name, defval, true);
458 }
459
460 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
461 {
462 if (opt->desc == NULL)
463 return;
464
465 switch (opt->desc->type) {
466 case QEMU_OPT_STRING:
467 /* nothing */
468 return;
469 case QEMU_OPT_BOOL:
470 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
471 break;
472 case QEMU_OPT_NUMBER:
473 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
474 break;
475 case QEMU_OPT_SIZE:
476 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
477 break;
478 default:
479 abort();
480 }
481 }
482
483 static bool opts_accepts_any(const QemuOpts *opts)
484 {
485 return opts->list->desc[0].name == NULL;
486 }
487
488 int qemu_opt_unset(QemuOpts *opts, const char *name)
489 {
490 QemuOpt *opt = qemu_opt_find(opts, name);
491
492 assert(opts_accepts_any(opts));
493
494 if (opt == NULL) {
495 return -1;
496 } else {
497 qemu_opt_del(opt);
498 return 0;
499 }
500 }
501
502 static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value,
503 bool prepend)
504 {
505 QemuOpt *opt = g_malloc0(sizeof(*opt));
506
507 opt->name = g_strdup(name);
508 opt->str = value;
509 opt->opts = opts;
510 if (prepend) {
511 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
512 } else {
513 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
514 }
515
516 return opt;
517 }
518
519 static bool opt_validate(QemuOpt *opt, bool *help_wanted,
520 Error **errp)
521 {
522 const QemuOptDesc *desc;
523 Error *local_err = NULL;
524
525 desc = find_desc_by_name(opt->opts->list->desc, opt->name);
526 if (!desc && !opts_accepts_any(opt->opts)) {
527 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
528 if (help_wanted && is_help_option(opt->name)) {
529 *help_wanted = true;
530 }
531 return false;
532 }
533
534 opt->desc = desc;
535 qemu_opt_parse(opt, &local_err);
536 if (local_err) {
537 error_propagate(errp, local_err);
538 return false;
539 }
540
541 return true;
542 }
543
544 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
545 Error **errp)
546 {
547 QemuOpt *opt = opt_create(opts, name, g_strdup(value), false);
548
549 if (!opt_validate(opt, NULL, errp)) {
550 qemu_opt_del(opt);
551 }
552 }
553
554 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
555 Error **errp)
556 {
557 QemuOpt *opt;
558 const QemuOptDesc *desc;
559
560 desc = find_desc_by_name(opts->list->desc, name);
561 if (!desc && !opts_accepts_any(opts)) {
562 error_setg(errp, QERR_INVALID_PARAMETER, name);
563 return;
564 }
565
566 opt = g_malloc0(sizeof(*opt));
567 opt->name = g_strdup(name);
568 opt->opts = opts;
569 opt->desc = desc;
570 opt->value.boolean = !!val;
571 opt->str = g_strdup(val ? "on" : "off");
572 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
573 }
574
575 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
576 Error **errp)
577 {
578 QemuOpt *opt;
579 const QemuOptDesc *desc;
580
581 desc = find_desc_by_name(opts->list->desc, name);
582 if (!desc && !opts_accepts_any(opts)) {
583 error_setg(errp, QERR_INVALID_PARAMETER, name);
584 return;
585 }
586
587 opt = g_malloc0(sizeof(*opt));
588 opt->name = g_strdup(name);
589 opt->opts = opts;
590 opt->desc = desc;
591 opt->value.uint = val;
592 opt->str = g_strdup_printf("%" PRId64, val);
593 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
594 }
595
596 /**
597 * For each member of @opts, call @func(@opaque, name, value, @errp).
598 * @func() may store an Error through @errp, but must return non-zero then.
599 * When @func() returns non-zero, break the loop and return that value.
600 * Return zero when the loop completes.
601 */
602 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
603 Error **errp)
604 {
605 QemuOpt *opt;
606 int rc;
607
608 QTAILQ_FOREACH(opt, &opts->head, next) {
609 rc = func(opaque, opt->name, opt->str, errp);
610 if (rc) {
611 return rc;
612 }
613 assert(!errp || !*errp);
614 }
615 return 0;
616 }
617
618 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
619 {
620 QemuOpts *opts;
621
622 QTAILQ_FOREACH(opts, &list->head, next) {
623 if (!opts->id && !id) {
624 return opts;
625 }
626 if (opts->id && id && !strcmp(opts->id, id)) {
627 return opts;
628 }
629 }
630 return NULL;
631 }
632
633 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
634 int fail_if_exists, Error **errp)
635 {
636 QemuOpts *opts = NULL;
637
638 if (id) {
639 if (!id_wellformed(id)) {
640 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
641 "an identifier");
642 error_append_hint(errp, "Identifiers consist of letters, digits, "
643 "'-', '.', '_', starting with a letter.\n");
644 return NULL;
645 }
646 opts = qemu_opts_find(list, id);
647 if (opts != NULL) {
648 if (fail_if_exists && !list->merge_lists) {
649 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
650 return NULL;
651 } else {
652 return opts;
653 }
654 }
655 } else if (list->merge_lists) {
656 opts = qemu_opts_find(list, NULL);
657 if (opts) {
658 return opts;
659 }
660 }
661 opts = g_malloc0(sizeof(*opts));
662 opts->id = g_strdup(id);
663 opts->list = list;
664 loc_save(&opts->loc);
665 QTAILQ_INIT(&opts->head);
666 QTAILQ_INSERT_TAIL(&list->head, opts, next);
667 return opts;
668 }
669
670 void qemu_opts_reset(QemuOptsList *list)
671 {
672 QemuOpts *opts, *next_opts;
673
674 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
675 qemu_opts_del(opts);
676 }
677 }
678
679 void qemu_opts_loc_restore(QemuOpts *opts)
680 {
681 loc_restore(&opts->loc);
682 }
683
684 void qemu_opts_set(QemuOptsList *list, const char *id,
685 const char *name, const char *value, Error **errp)
686 {
687 QemuOpts *opts;
688
689 opts = qemu_opts_create(list, id, 1, errp);
690 if (!opts) {
691 return;
692 }
693 qemu_opt_set(opts, name, value, errp);
694 }
695
696 const char *qemu_opts_id(QemuOpts *opts)
697 {
698 return opts->id;
699 }
700
701 /* The id string will be g_free()d by qemu_opts_del */
702 void qemu_opts_set_id(QemuOpts *opts, char *id)
703 {
704 opts->id = id;
705 }
706
707 void qemu_opts_del(QemuOpts *opts)
708 {
709 QemuOpt *opt;
710
711 if (opts == NULL) {
712 return;
713 }
714
715 for (;;) {
716 opt = QTAILQ_FIRST(&opts->head);
717 if (opt == NULL)
718 break;
719 qemu_opt_del(opt);
720 }
721 QTAILQ_REMOVE(&opts->list->head, opts, next);
722 g_free(opts->id);
723 g_free(opts);
724 }
725
726 /* print value, escaping any commas in value */
727 static void escaped_print(const char *value)
728 {
729 const char *ptr;
730
731 for (ptr = value; *ptr; ++ptr) {
732 if (*ptr == ',') {
733 putchar(',');
734 }
735 putchar(*ptr);
736 }
737 }
738
739 void qemu_opts_print(QemuOpts *opts, const char *separator)
740 {
741 QemuOpt *opt;
742 QemuOptDesc *desc = opts->list->desc;
743 const char *sep = "";
744
745 if (opts->id) {
746 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
747 sep = separator;
748 }
749
750 if (desc[0].name == NULL) {
751 QTAILQ_FOREACH(opt, &opts->head, next) {
752 printf("%s%s=", sep, opt->name);
753 escaped_print(opt->str);
754 sep = separator;
755 }
756 return;
757 }
758 for (; desc && desc->name; desc++) {
759 const char *value;
760 opt = qemu_opt_find(opts, desc->name);
761
762 value = opt ? opt->str : desc->def_value_str;
763 if (!value) {
764 continue;
765 }
766 if (desc->type == QEMU_OPT_STRING) {
767 printf("%s%s=", sep, desc->name);
768 escaped_print(value);
769 } else if ((desc->type == QEMU_OPT_SIZE ||
770 desc->type == QEMU_OPT_NUMBER) && opt) {
771 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
772 } else {
773 printf("%s%s=%s", sep, desc->name, value);
774 }
775 sep = separator;
776 }
777 }
778
779 static const char *get_opt_name_value(const char *params,
780 const char *firstname,
781 char **name, char **value)
782 {
783 const char *p, *pe, *pc;
784
785 pe = strchr(params, '=');
786 pc = strchr(params, ',');
787
788 if (!pe || (pc && pc < pe)) {
789 /* found "foo,more" */
790 if (firstname) {
791 /* implicitly named first option */
792 *name = g_strdup(firstname);
793 p = get_opt_value(params, value);
794 } else {
795 /* option without value, must be a flag */
796 p = get_opt_name(params, name, ',');
797 if (strncmp(*name, "no", 2) == 0) {
798 memmove(*name, *name + 2, strlen(*name + 2) + 1);
799 *value = g_strdup("off");
800 } else {
801 *value = g_strdup("on");
802 }
803 }
804 } else {
805 /* found "foo=bar,more" */
806 p = get_opt_name(params, name, '=');
807 assert(*p == '=');
808 p++;
809 p = get_opt_value(p, value);
810 }
811
812 assert(!*p || *p == ',');
813 if (*p == ',') {
814 p++;
815 }
816 return p;
817 }
818
819 static void opts_do_parse(QemuOpts *opts, const char *params,
820 const char *firstname, bool prepend,
821 bool *help_wanted, Error **errp)
822 {
823 char *option, *value;
824 const char *p;
825 QemuOpt *opt;
826
827 for (p = params; *p;) {
828 p = get_opt_name_value(p, firstname, &option, &value);
829 firstname = NULL;
830
831 if (!strcmp(option, "id")) {
832 g_free(option);
833 g_free(value);
834 continue;
835 }
836
837 opt = opt_create(opts, option, value, prepend);
838 g_free(option);
839 if (!opt_validate(opt, help_wanted, errp)) {
840 qemu_opt_del(opt);
841 return;
842 }
843 }
844 }
845
846 static char *opts_parse_id(const char *params)
847 {
848 const char *p;
849 char *name, *value;
850
851 for (p = params; *p;) {
852 p = get_opt_name_value(p, NULL, &name, &value);
853 if (!strcmp(name, "id")) {
854 g_free(name);
855 return value;
856 }
857 g_free(name);
858 g_free(value);
859 }
860
861 return NULL;
862 }
863
864 bool has_help_option(const char *params)
865 {
866 const char *p;
867 char *name, *value;
868 bool ret;
869
870 for (p = params; *p;) {
871 p = get_opt_name_value(p, NULL, &name, &value);
872 ret = is_help_option(name);
873 g_free(name);
874 g_free(value);
875 if (ret) {
876 return true;
877 }
878 }
879
880 return false;
881 }
882
883 /**
884 * Store options parsed from @params into @opts.
885 * If @firstname is non-null, the first key=value in @params may omit
886 * key=, and is treated as if key was @firstname.
887 * On error, store an error object through @errp if non-null.
888 */
889 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
890 const char *firstname, Error **errp)
891 {
892 opts_do_parse(opts, params, firstname, false, NULL, errp);
893 }
894
895 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
896 bool permit_abbrev, bool defaults,
897 bool *help_wanted, Error **errp)
898 {
899 const char *firstname;
900 char *id = opts_parse_id(params);
901 QemuOpts *opts;
902 Error *local_err = NULL;
903
904 assert(!permit_abbrev || list->implied_opt_name);
905 firstname = permit_abbrev ? list->implied_opt_name : NULL;
906
907 /*
908 * This code doesn't work for defaults && !list->merge_lists: when
909 * params has no id=, and list has an element with !opts->id, it
910 * appends a new element instead of returning the existing opts.
911 * However, we got no use for this case. Guard against possible
912 * (if unlikely) future misuse:
913 */
914 assert(!defaults || list->merge_lists);
915 opts = qemu_opts_create(list, id, !defaults, &local_err);
916 g_free(id);
917 if (opts == NULL) {
918 error_propagate(errp, local_err);
919 return NULL;
920 }
921
922 opts_do_parse(opts, params, firstname, defaults, help_wanted, &local_err);
923 if (local_err) {
924 error_propagate(errp, local_err);
925 qemu_opts_del(opts);
926 return NULL;
927 }
928
929 return opts;
930 }
931
932 /**
933 * Create a QemuOpts in @list and with options parsed from @params.
934 * If @permit_abbrev, the first key=value in @params may omit key=,
935 * and is treated as if key was @list->implied_opt_name.
936 * On error, store an error object through @errp if non-null.
937 * Return the new QemuOpts on success, null pointer on error.
938 */
939 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
940 bool permit_abbrev, Error **errp)
941 {
942 return opts_parse(list, params, permit_abbrev, false, NULL, errp);
943 }
944
945 /**
946 * Create a QemuOpts in @list and with options parsed from @params.
947 * If @permit_abbrev, the first key=value in @params may omit key=,
948 * and is treated as if key was @list->implied_opt_name.
949 * Report errors with error_report_err(). This is inappropriate in
950 * QMP context. Do not use this function there!
951 * Return the new QemuOpts on success, null pointer on error.
952 */
953 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
954 bool permit_abbrev)
955 {
956 Error *err = NULL;
957 QemuOpts *opts;
958 bool help_wanted = false;
959
960 opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err);
961 if (err) {
962 if (help_wanted) {
963 qemu_opts_print_help(list, true);
964 error_free(err);
965 } else {
966 error_report_err(err);
967 }
968 }
969 return opts;
970 }
971
972 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
973 int permit_abbrev)
974 {
975 QemuOpts *opts;
976
977 opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL);
978 assert(opts);
979 }
980
981 static void qemu_opts_from_qdict_entry(QemuOpts *opts,
982 const QDictEntry *entry,
983 Error **errp)
984 {
985 const char *key = qdict_entry_key(entry);
986 QObject *obj = qdict_entry_value(entry);
987 char buf[32], *tmp = NULL;
988 const char *value;
989
990 if (!strcmp(key, "id")) {
991 return;
992 }
993
994 switch (qobject_type(obj)) {
995 case QTYPE_QSTRING:
996 value = qstring_get_str(qobject_to(QString, obj));
997 break;
998 case QTYPE_QNUM:
999 tmp = qnum_to_string(qobject_to(QNum, obj));
1000 value = tmp;
1001 break;
1002 case QTYPE_QBOOL:
1003 pstrcpy(buf, sizeof(buf),
1004 qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
1005 value = buf;
1006 break;
1007 default:
1008 return;
1009 }
1010
1011 qemu_opt_set(opts, key, value, errp);
1012 g_free(tmp);
1013 }
1014
1015 /*
1016 * Create QemuOpts from a QDict.
1017 * Use value of key "id" as ID if it exists and is a QString. Only
1018 * QStrings, QNums and QBools are copied. Entries with other types
1019 * are silently ignored.
1020 */
1021 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1022 Error **errp)
1023 {
1024 Error *local_err = NULL;
1025 QemuOpts *opts;
1026 const QDictEntry *entry;
1027
1028 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
1029 if (!opts) {
1030 return NULL;
1031 }
1032
1033 assert(opts != NULL);
1034
1035 for (entry = qdict_first(qdict);
1036 entry;
1037 entry = qdict_next(qdict, entry)) {
1038 qemu_opts_from_qdict_entry(opts, entry, &local_err);
1039 if (local_err) {
1040 error_propagate(errp, local_err);
1041 qemu_opts_del(opts);
1042 return NULL;
1043 }
1044 }
1045
1046 return opts;
1047 }
1048
1049 /*
1050 * Adds all QDict entries to the QemuOpts that can be added and removes them
1051 * from the QDict. When this function returns, the QDict contains only those
1052 * entries that couldn't be added to the QemuOpts.
1053 */
1054 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1055 {
1056 const QDictEntry *entry, *next;
1057
1058 entry = qdict_first(qdict);
1059
1060 while (entry != NULL) {
1061 Error *local_err = NULL;
1062
1063 next = qdict_next(qdict, entry);
1064
1065 if (find_desc_by_name(opts->list->desc, entry->key)) {
1066 qemu_opts_from_qdict_entry(opts, entry, &local_err);
1067 if (local_err) {
1068 error_propagate(errp, local_err);
1069 return;
1070 }
1071 qdict_del(qdict, entry->key);
1072 }
1073
1074 entry = next;
1075 }
1076 }
1077
1078 /*
1079 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1080 *
1081 * If @list is given, only add those options to the QDict that are contained in
1082 * the list. If @del is true, any options added to the QDict are removed from
1083 * the QemuOpts, otherwise they remain there.
1084 *
1085 * If two options in @opts have the same name, they are processed in order
1086 * so that the last one wins (consistent with the reverse iteration in
1087 * qemu_opt_find()), but all of them are deleted if @del is true.
1088 *
1089 * TODO We'll want to use types appropriate for opt->desc->type, but
1090 * this is enough for now.
1091 */
1092 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1093 QemuOptsList *list, bool del)
1094 {
1095 QemuOpt *opt, *next;
1096
1097 if (!qdict) {
1098 qdict = qdict_new();
1099 }
1100 if (opts->id) {
1101 qdict_put_str(qdict, "id", opts->id);
1102 }
1103 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1104 if (list) {
1105 QemuOptDesc *desc;
1106 bool found = false;
1107 for (desc = list->desc; desc->name; desc++) {
1108 if (!strcmp(desc->name, opt->name)) {
1109 found = true;
1110 break;
1111 }
1112 }
1113 if (!found) {
1114 continue;
1115 }
1116 }
1117 qdict_put_str(qdict, opt->name, opt->str);
1118 if (del) {
1119 qemu_opt_del(opt);
1120 }
1121 }
1122 return qdict;
1123 }
1124
1125 /* Copy all options in a QemuOpts to the given QDict. See
1126 * qemu_opts_to_qdict_filtered() for details. */
1127 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1128 {
1129 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1130 }
1131
1132 /* Validate parsed opts against descriptions where no
1133 * descriptions were provided in the QemuOptsList.
1134 */
1135 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1136 {
1137 QemuOpt *opt;
1138 Error *local_err = NULL;
1139
1140 assert(opts_accepts_any(opts));
1141
1142 QTAILQ_FOREACH(opt, &opts->head, next) {
1143 opt->desc = find_desc_by_name(desc, opt->name);
1144 if (!opt->desc) {
1145 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1146 return;
1147 }
1148
1149 qemu_opt_parse(opt, &local_err);
1150 if (local_err) {
1151 error_propagate(errp, local_err);
1152 return;
1153 }
1154 }
1155 }
1156
1157 /**
1158 * For each member of @list, call @func(@opaque, member, @errp).
1159 * Call it with the current location temporarily set to the member's.
1160 * @func() may store an Error through @errp, but must return non-zero then.
1161 * When @func() returns non-zero, break the loop and return that value.
1162 * Return zero when the loop completes.
1163 */
1164 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1165 void *opaque, Error **errp)
1166 {
1167 Location loc;
1168 QemuOpts *opts;
1169 int rc = 0;
1170
1171 loc_push_none(&loc);
1172 QTAILQ_FOREACH(opts, &list->head, next) {
1173 loc_restore(&opts->loc);
1174 rc = func(opaque, opts, errp);
1175 if (rc) {
1176 break;
1177 }
1178 assert(!errp || !*errp);
1179 }
1180 loc_pop(&loc);
1181 return rc;
1182 }
1183
1184 static size_t count_opts_list(QemuOptsList *list)
1185 {
1186 QemuOptDesc *desc = NULL;
1187 size_t num_opts = 0;
1188
1189 if (!list) {
1190 return 0;
1191 }
1192
1193 desc = list->desc;
1194 while (desc && desc->name) {
1195 num_opts++;
1196 desc++;
1197 }
1198
1199 return num_opts;
1200 }
1201
1202 void qemu_opts_free(QemuOptsList *list)
1203 {
1204 g_free(list);
1205 }
1206
1207 /* Realloc dst option list and append options from an option list (list)
1208 * to it. dst could be NULL or a malloced list.
1209 * The lifetime of dst must be shorter than the input list because the
1210 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1211 */
1212 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1213 QemuOptsList *list)
1214 {
1215 size_t num_opts, num_dst_opts;
1216 QemuOptDesc *desc;
1217 bool need_init = false;
1218 bool need_head_update;
1219
1220 if (!list) {
1221 return dst;
1222 }
1223
1224 /* If dst is NULL, after realloc, some area of dst should be initialized
1225 * before adding options to it.
1226 */
1227 if (!dst) {
1228 need_init = true;
1229 need_head_update = true;
1230 } else {
1231 /* Moreover, even if dst is not NULL, the realloc may move it to a
1232 * different address in which case we may get a stale tail pointer
1233 * in dst->head. */
1234 need_head_update = QTAILQ_EMPTY(&dst->head);
1235 }
1236
1237 num_opts = count_opts_list(dst);
1238 num_dst_opts = num_opts;
1239 num_opts += count_opts_list(list);
1240 dst = g_realloc(dst, sizeof(QemuOptsList) +
1241 (num_opts + 1) * sizeof(QemuOptDesc));
1242 if (need_init) {
1243 dst->name = NULL;
1244 dst->implied_opt_name = NULL;
1245 dst->merge_lists = false;
1246 }
1247 if (need_head_update) {
1248 QTAILQ_INIT(&dst->head);
1249 }
1250 dst->desc[num_dst_opts].name = NULL;
1251
1252 /* append list->desc to dst->desc */
1253 if (list) {
1254 desc = list->desc;
1255 while (desc && desc->name) {
1256 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1257 dst->desc[num_dst_opts++] = *desc;
1258 dst->desc[num_dst_opts].name = NULL;
1259 }
1260 desc++;
1261 }
1262 }
1263
1264 return dst;
1265 }