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