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