]> git.proxmox.com Git - mirror_qemu.git/blob - util/qemu-option.c
qemu-option: Factor out helper opt_create()
[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 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 void opt_set(QemuOpts *opts, const char *name, char *value,
520 bool prepend, bool *help_wanted, Error **errp)
521 {
522 QemuOpt *opt;
523 const QemuOptDesc *desc;
524 Error *local_err = NULL;
525
526 desc = find_desc_by_name(opts->list->desc, name);
527 if (!desc && !opts_accepts_any(opts)) {
528 g_free(value);
529 error_setg(errp, QERR_INVALID_PARAMETER, name);
530 if (help_wanted && is_help_option(name)) {
531 *help_wanted = true;
532 }
533 return;
534 }
535
536 opt = opt_create(opts, name, value, prepend);
537 opt->desc = desc;
538 qemu_opt_parse(opt, &local_err);
539 if (local_err) {
540 error_propagate(errp, local_err);
541 qemu_opt_del(opt);
542 }
543 }
544
545 void qemu_opt_set(QemuOpts *opts, const char *name, const char *value,
546 Error **errp)
547 {
548 opt_set(opts, name, g_strdup(value), false, NULL, errp);
549 }
550
551 void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val,
552 Error **errp)
553 {
554 QemuOpt *opt;
555 const QemuOptDesc *desc;
556
557 desc = find_desc_by_name(opts->list->desc, name);
558 if (!desc && !opts_accepts_any(opts)) {
559 error_setg(errp, QERR_INVALID_PARAMETER, name);
560 return;
561 }
562
563 opt = g_malloc0(sizeof(*opt));
564 opt->name = g_strdup(name);
565 opt->opts = opts;
566 opt->desc = desc;
567 opt->value.boolean = !!val;
568 opt->str = g_strdup(val ? "on" : "off");
569 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
570 }
571
572 void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val,
573 Error **errp)
574 {
575 QemuOpt *opt;
576 const QemuOptDesc *desc;
577
578 desc = find_desc_by_name(opts->list->desc, name);
579 if (!desc && !opts_accepts_any(opts)) {
580 error_setg(errp, QERR_INVALID_PARAMETER, name);
581 return;
582 }
583
584 opt = g_malloc0(sizeof(*opt));
585 opt->name = g_strdup(name);
586 opt->opts = opts;
587 opt->desc = desc;
588 opt->value.uint = val;
589 opt->str = g_strdup_printf("%" PRId64, val);
590 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
591 }
592
593 /**
594 * For each member of @opts, call @func(@opaque, name, value, @errp).
595 * @func() may store an Error through @errp, but must return non-zero then.
596 * When @func() returns non-zero, break the loop and return that value.
597 * Return zero when the loop completes.
598 */
599 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
600 Error **errp)
601 {
602 QemuOpt *opt;
603 int rc;
604
605 QTAILQ_FOREACH(opt, &opts->head, next) {
606 rc = func(opaque, opt->name, opt->str, errp);
607 if (rc) {
608 return rc;
609 }
610 assert(!errp || !*errp);
611 }
612 return 0;
613 }
614
615 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
616 {
617 QemuOpts *opts;
618
619 QTAILQ_FOREACH(opts, &list->head, next) {
620 if (!opts->id && !id) {
621 return opts;
622 }
623 if (opts->id && id && !strcmp(opts->id, id)) {
624 return opts;
625 }
626 }
627 return NULL;
628 }
629
630 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
631 int fail_if_exists, Error **errp)
632 {
633 QemuOpts *opts = NULL;
634
635 if (id) {
636 if (!id_wellformed(id)) {
637 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id",
638 "an identifier");
639 error_append_hint(errp, "Identifiers consist of letters, digits, "
640 "'-', '.', '_', starting with a letter.\n");
641 return NULL;
642 }
643 opts = qemu_opts_find(list, id);
644 if (opts != NULL) {
645 if (fail_if_exists && !list->merge_lists) {
646 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
647 return NULL;
648 } else {
649 return opts;
650 }
651 }
652 } else if (list->merge_lists) {
653 opts = qemu_opts_find(list, NULL);
654 if (opts) {
655 return opts;
656 }
657 }
658 opts = g_malloc0(sizeof(*opts));
659 opts->id = g_strdup(id);
660 opts->list = list;
661 loc_save(&opts->loc);
662 QTAILQ_INIT(&opts->head);
663 QTAILQ_INSERT_TAIL(&list->head, opts, next);
664 return opts;
665 }
666
667 void qemu_opts_reset(QemuOptsList *list)
668 {
669 QemuOpts *opts, *next_opts;
670
671 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
672 qemu_opts_del(opts);
673 }
674 }
675
676 void qemu_opts_loc_restore(QemuOpts *opts)
677 {
678 loc_restore(&opts->loc);
679 }
680
681 void qemu_opts_set(QemuOptsList *list, const char *id,
682 const char *name, const char *value, Error **errp)
683 {
684 QemuOpts *opts;
685
686 opts = qemu_opts_create(list, id, 1, errp);
687 if (!opts) {
688 return;
689 }
690 qemu_opt_set(opts, name, value, errp);
691 }
692
693 const char *qemu_opts_id(QemuOpts *opts)
694 {
695 return opts->id;
696 }
697
698 /* The id string will be g_free()d by qemu_opts_del */
699 void qemu_opts_set_id(QemuOpts *opts, char *id)
700 {
701 opts->id = id;
702 }
703
704 void qemu_opts_del(QemuOpts *opts)
705 {
706 QemuOpt *opt;
707
708 if (opts == NULL) {
709 return;
710 }
711
712 for (;;) {
713 opt = QTAILQ_FIRST(&opts->head);
714 if (opt == NULL)
715 break;
716 qemu_opt_del(opt);
717 }
718 QTAILQ_REMOVE(&opts->list->head, opts, next);
719 g_free(opts->id);
720 g_free(opts);
721 }
722
723 /* print value, escaping any commas in value */
724 static void escaped_print(const char *value)
725 {
726 const char *ptr;
727
728 for (ptr = value; *ptr; ++ptr) {
729 if (*ptr == ',') {
730 putchar(',');
731 }
732 putchar(*ptr);
733 }
734 }
735
736 void qemu_opts_print(QemuOpts *opts, const char *separator)
737 {
738 QemuOpt *opt;
739 QemuOptDesc *desc = opts->list->desc;
740 const char *sep = "";
741
742 if (opts->id) {
743 printf("id=%s", opts->id); /* passed id_wellformed -> no commas */
744 sep = separator;
745 }
746
747 if (desc[0].name == NULL) {
748 QTAILQ_FOREACH(opt, &opts->head, next) {
749 printf("%s%s=", sep, opt->name);
750 escaped_print(opt->str);
751 sep = separator;
752 }
753 return;
754 }
755 for (; desc && desc->name; desc++) {
756 const char *value;
757 opt = qemu_opt_find(opts, desc->name);
758
759 value = opt ? opt->str : desc->def_value_str;
760 if (!value) {
761 continue;
762 }
763 if (desc->type == QEMU_OPT_STRING) {
764 printf("%s%s=", sep, desc->name);
765 escaped_print(value);
766 } else if ((desc->type == QEMU_OPT_SIZE ||
767 desc->type == QEMU_OPT_NUMBER) && opt) {
768 printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint);
769 } else {
770 printf("%s%s=%s", sep, desc->name, value);
771 }
772 sep = separator;
773 }
774 }
775
776 static const char *get_opt_name_value(const char *params,
777 const char *firstname,
778 char **name, char **value)
779 {
780 const char *p, *pe, *pc;
781
782 pe = strchr(params, '=');
783 pc = strchr(params, ',');
784
785 if (!pe || (pc && pc < pe)) {
786 /* found "foo,more" */
787 if (firstname) {
788 /* implicitly named first option */
789 *name = g_strdup(firstname);
790 p = get_opt_value(params, value);
791 } else {
792 /* option without value, must be a flag */
793 p = get_opt_name(params, name, ',');
794 if (strncmp(*name, "no", 2) == 0) {
795 memmove(*name, *name + 2, strlen(*name + 2) + 1);
796 *value = g_strdup("off");
797 } else {
798 *value = g_strdup("on");
799 }
800 }
801 } else {
802 /* found "foo=bar,more" */
803 p = get_opt_name(params, name, '=');
804 assert(*p == '=');
805 p++;
806 p = get_opt_value(p, value);
807 }
808
809 assert(!*p || *p == ',');
810 if (*p == ',') {
811 p++;
812 }
813 return p;
814 }
815
816 static void opts_do_parse(QemuOpts *opts, const char *params,
817 const char *firstname, bool prepend,
818 bool *help_wanted, Error **errp)
819 {
820 Error *local_err = NULL;
821 char *option, *value;
822 const char *p;
823
824 for (p = params; *p;) {
825 p = get_opt_name_value(p, firstname, &option, &value);
826 firstname = NULL;
827
828 if (!strcmp(option, "id")) {
829 g_free(option);
830 g_free(value);
831 continue;
832 }
833
834 opt_set(opts, option, value, prepend, help_wanted, &local_err);
835 g_free(option);
836 if (local_err) {
837 error_propagate(errp, local_err);
838 return;
839 }
840 }
841 }
842
843 static char *opts_parse_id(const char *params)
844 {
845 const char *p;
846 char *name, *value;
847
848 for (p = params; *p;) {
849 p = get_opt_name_value(p, NULL, &name, &value);
850 if (!strcmp(name, "id")) {
851 g_free(name);
852 return value;
853 }
854 g_free(name);
855 g_free(value);
856 }
857
858 return NULL;
859 }
860
861 bool has_help_option(const char *params)
862 {
863 const char *p;
864 char *name, *value;
865 bool ret;
866
867 for (p = params; *p;) {
868 p = get_opt_name_value(p, NULL, &name, &value);
869 ret = is_help_option(name);
870 g_free(name);
871 g_free(value);
872 if (ret) {
873 return true;
874 }
875 }
876
877 return false;
878 }
879
880 /**
881 * Store options parsed from @params into @opts.
882 * If @firstname is non-null, the first key=value in @params may omit
883 * key=, and is treated as if key was @firstname.
884 * On error, store an error object through @errp if non-null.
885 */
886 void qemu_opts_do_parse(QemuOpts *opts, const char *params,
887 const char *firstname, Error **errp)
888 {
889 opts_do_parse(opts, params, firstname, false, NULL, errp);
890 }
891
892 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
893 bool permit_abbrev, bool defaults,
894 bool *help_wanted, Error **errp)
895 {
896 const char *firstname;
897 char *id = opts_parse_id(params);
898 QemuOpts *opts;
899 Error *local_err = NULL;
900
901 assert(!permit_abbrev || list->implied_opt_name);
902 firstname = permit_abbrev ? list->implied_opt_name : NULL;
903
904 /*
905 * This code doesn't work for defaults && !list->merge_lists: when
906 * params has no id=, and list has an element with !opts->id, it
907 * appends a new element instead of returning the existing opts.
908 * However, we got no use for this case. Guard against possible
909 * (if unlikely) future misuse:
910 */
911 assert(!defaults || list->merge_lists);
912 opts = qemu_opts_create(list, id, !defaults, &local_err);
913 g_free(id);
914 if (opts == NULL) {
915 error_propagate(errp, local_err);
916 return NULL;
917 }
918
919 opts_do_parse(opts, params, firstname, defaults, help_wanted, &local_err);
920 if (local_err) {
921 error_propagate(errp, local_err);
922 qemu_opts_del(opts);
923 return NULL;
924 }
925
926 return opts;
927 }
928
929 /**
930 * Create a QemuOpts in @list and with options parsed from @params.
931 * If @permit_abbrev, the first key=value in @params may omit key=,
932 * and is treated as if key was @list->implied_opt_name.
933 * On error, store an error object through @errp if non-null.
934 * Return the new QemuOpts on success, null pointer on error.
935 */
936 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
937 bool permit_abbrev, Error **errp)
938 {
939 return opts_parse(list, params, permit_abbrev, false, NULL, errp);
940 }
941
942 /**
943 * Create a QemuOpts in @list and with options parsed from @params.
944 * If @permit_abbrev, the first key=value in @params may omit key=,
945 * and is treated as if key was @list->implied_opt_name.
946 * Report errors with error_report_err(). This is inappropriate in
947 * QMP context. Do not use this function there!
948 * Return the new QemuOpts on success, null pointer on error.
949 */
950 QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params,
951 bool permit_abbrev)
952 {
953 Error *err = NULL;
954 QemuOpts *opts;
955 bool help_wanted = false;
956
957 opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err);
958 if (err) {
959 if (help_wanted) {
960 qemu_opts_print_help(list, true);
961 error_free(err);
962 } else {
963 error_report_err(err);
964 }
965 }
966 return opts;
967 }
968
969 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
970 int permit_abbrev)
971 {
972 QemuOpts *opts;
973
974 opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL);
975 assert(opts);
976 }
977
978 static void qemu_opts_from_qdict_entry(QemuOpts *opts,
979 const QDictEntry *entry,
980 Error **errp)
981 {
982 const char *key = qdict_entry_key(entry);
983 QObject *obj = qdict_entry_value(entry);
984 char buf[32], *tmp = NULL;
985 const char *value;
986
987 if (!strcmp(key, "id")) {
988 return;
989 }
990
991 switch (qobject_type(obj)) {
992 case QTYPE_QSTRING:
993 value = qstring_get_str(qobject_to(QString, obj));
994 break;
995 case QTYPE_QNUM:
996 tmp = qnum_to_string(qobject_to(QNum, obj));
997 value = tmp;
998 break;
999 case QTYPE_QBOOL:
1000 pstrcpy(buf, sizeof(buf),
1001 qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off");
1002 value = buf;
1003 break;
1004 default:
1005 return;
1006 }
1007
1008 qemu_opt_set(opts, key, value, errp);
1009 g_free(tmp);
1010 }
1011
1012 /*
1013 * Create QemuOpts from a QDict.
1014 * Use value of key "id" as ID if it exists and is a QString. Only
1015 * QStrings, QNums and QBools are copied. Entries with other types
1016 * are silently ignored.
1017 */
1018 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1019 Error **errp)
1020 {
1021 Error *local_err = NULL;
1022 QemuOpts *opts;
1023 const QDictEntry *entry;
1024
1025 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp);
1026 if (!opts) {
1027 return NULL;
1028 }
1029
1030 assert(opts != NULL);
1031
1032 for (entry = qdict_first(qdict);
1033 entry;
1034 entry = qdict_next(qdict, entry)) {
1035 qemu_opts_from_qdict_entry(opts, entry, &local_err);
1036 if (local_err) {
1037 error_propagate(errp, local_err);
1038 qemu_opts_del(opts);
1039 return NULL;
1040 }
1041 }
1042
1043 return opts;
1044 }
1045
1046 /*
1047 * Adds all QDict entries to the QemuOpts that can be added and removes them
1048 * from the QDict. When this function returns, the QDict contains only those
1049 * entries that couldn't be added to the QemuOpts.
1050 */
1051 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1052 {
1053 const QDictEntry *entry, *next;
1054
1055 entry = qdict_first(qdict);
1056
1057 while (entry != NULL) {
1058 Error *local_err = NULL;
1059
1060 next = qdict_next(qdict, entry);
1061
1062 if (find_desc_by_name(opts->list->desc, entry->key)) {
1063 qemu_opts_from_qdict_entry(opts, entry, &local_err);
1064 if (local_err) {
1065 error_propagate(errp, local_err);
1066 return;
1067 }
1068 qdict_del(qdict, entry->key);
1069 }
1070
1071 entry = next;
1072 }
1073 }
1074
1075 /*
1076 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1077 *
1078 * If @list is given, only add those options to the QDict that are contained in
1079 * the list. If @del is true, any options added to the QDict are removed from
1080 * the QemuOpts, otherwise they remain there.
1081 *
1082 * If two options in @opts have the same name, they are processed in order
1083 * so that the last one wins (consistent with the reverse iteration in
1084 * qemu_opt_find()), but all of them are deleted if @del is true.
1085 *
1086 * TODO We'll want to use types appropriate for opt->desc->type, but
1087 * this is enough for now.
1088 */
1089 QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict,
1090 QemuOptsList *list, bool del)
1091 {
1092 QemuOpt *opt, *next;
1093
1094 if (!qdict) {
1095 qdict = qdict_new();
1096 }
1097 if (opts->id) {
1098 qdict_put_str(qdict, "id", opts->id);
1099 }
1100 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) {
1101 if (list) {
1102 QemuOptDesc *desc;
1103 bool found = false;
1104 for (desc = list->desc; desc->name; desc++) {
1105 if (!strcmp(desc->name, opt->name)) {
1106 found = true;
1107 break;
1108 }
1109 }
1110 if (!found) {
1111 continue;
1112 }
1113 }
1114 qdict_put_str(qdict, opt->name, opt->str);
1115 if (del) {
1116 qemu_opt_del(opt);
1117 }
1118 }
1119 return qdict;
1120 }
1121
1122 /* Copy all options in a QemuOpts to the given QDict. See
1123 * qemu_opts_to_qdict_filtered() for details. */
1124 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1125 {
1126 return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false);
1127 }
1128
1129 /* Validate parsed opts against descriptions where no
1130 * descriptions were provided in the QemuOptsList.
1131 */
1132 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1133 {
1134 QemuOpt *opt;
1135 Error *local_err = NULL;
1136
1137 assert(opts_accepts_any(opts));
1138
1139 QTAILQ_FOREACH(opt, &opts->head, next) {
1140 opt->desc = find_desc_by_name(desc, opt->name);
1141 if (!opt->desc) {
1142 error_setg(errp, QERR_INVALID_PARAMETER, opt->name);
1143 return;
1144 }
1145
1146 qemu_opt_parse(opt, &local_err);
1147 if (local_err) {
1148 error_propagate(errp, local_err);
1149 return;
1150 }
1151 }
1152 }
1153
1154 /**
1155 * For each member of @list, call @func(@opaque, member, @errp).
1156 * Call it with the current location temporarily set to the member's.
1157 * @func() may store an Error through @errp, but must return non-zero then.
1158 * When @func() returns non-zero, break the loop and return that value.
1159 * Return zero when the loop completes.
1160 */
1161 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func,
1162 void *opaque, Error **errp)
1163 {
1164 Location loc;
1165 QemuOpts *opts;
1166 int rc = 0;
1167
1168 loc_push_none(&loc);
1169 QTAILQ_FOREACH(opts, &list->head, next) {
1170 loc_restore(&opts->loc);
1171 rc = func(opaque, opts, errp);
1172 if (rc) {
1173 break;
1174 }
1175 assert(!errp || !*errp);
1176 }
1177 loc_pop(&loc);
1178 return rc;
1179 }
1180
1181 static size_t count_opts_list(QemuOptsList *list)
1182 {
1183 QemuOptDesc *desc = NULL;
1184 size_t num_opts = 0;
1185
1186 if (!list) {
1187 return 0;
1188 }
1189
1190 desc = list->desc;
1191 while (desc && desc->name) {
1192 num_opts++;
1193 desc++;
1194 }
1195
1196 return num_opts;
1197 }
1198
1199 void qemu_opts_free(QemuOptsList *list)
1200 {
1201 g_free(list);
1202 }
1203
1204 /* Realloc dst option list and append options from an option list (list)
1205 * to it. dst could be NULL or a malloced list.
1206 * The lifetime of dst must be shorter than the input list because the
1207 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1208 */
1209 QemuOptsList *qemu_opts_append(QemuOptsList *dst,
1210 QemuOptsList *list)
1211 {
1212 size_t num_opts, num_dst_opts;
1213 QemuOptDesc *desc;
1214 bool need_init = false;
1215 bool need_head_update;
1216
1217 if (!list) {
1218 return dst;
1219 }
1220
1221 /* If dst is NULL, after realloc, some area of dst should be initialized
1222 * before adding options to it.
1223 */
1224 if (!dst) {
1225 need_init = true;
1226 need_head_update = true;
1227 } else {
1228 /* Moreover, even if dst is not NULL, the realloc may move it to a
1229 * different address in which case we may get a stale tail pointer
1230 * in dst->head. */
1231 need_head_update = QTAILQ_EMPTY(&dst->head);
1232 }
1233
1234 num_opts = count_opts_list(dst);
1235 num_dst_opts = num_opts;
1236 num_opts += count_opts_list(list);
1237 dst = g_realloc(dst, sizeof(QemuOptsList) +
1238 (num_opts + 1) * sizeof(QemuOptDesc));
1239 if (need_init) {
1240 dst->name = NULL;
1241 dst->implied_opt_name = NULL;
1242 dst->merge_lists = false;
1243 }
1244 if (need_head_update) {
1245 QTAILQ_INIT(&dst->head);
1246 }
1247 dst->desc[num_dst_opts].name = NULL;
1248
1249 /* append list->desc to dst->desc */
1250 if (list) {
1251 desc = list->desc;
1252 while (desc && desc->name) {
1253 if (find_desc_by_name(dst->desc, desc->name) == NULL) {
1254 dst->desc[num_dst_opts++] = *desc;
1255 dst->desc[num_dst_opts].name = NULL;
1256 }
1257 desc++;
1258 }
1259 }
1260
1261 return dst;
1262 }