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