QemuOpts: move qemu_opt_del ahead for later calling
[qemu/ar7.git] / util / qemu-option.c
blob71244836c3d7015b75ba0b6c9a6e5144faf07704
1 /*
2 * Commandline option parsing functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include <stdio.h>
27 #include <string.h>
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"
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
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.
44 * The return value is the position of the delimiter/zero byte after the option
45 * name in p.
47 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
49 char *q;
51 q = buf;
52 while (*p != '\0' && *p != delim) {
53 if (q && (q - buf) < buf_size - 1)
54 *q++ = *p;
55 p++;
57 if (q)
58 *q = '\0';
60 return p;
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
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.
71 const char *get_opt_value(char *buf, int buf_size, const char *p)
73 char *q;
75 q = buf;
76 while (*p != '\0') {
77 if (*p == ',') {
78 if (*(p + 1) != ',')
79 break;
80 p++;
82 if (q && (q - buf) < buf_size - 1)
83 *q++ = *p;
84 p++;
86 if (q)
87 *q = '\0';
89 return p;
92 int get_next_param_value(char *buf, int buf_size,
93 const char *tag, const char **pstr)
95 const char *p;
96 char option[128];
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)++;
109 return strlen(buf);
110 } else {
111 p = get_opt_value(NULL, 0, p);
113 if (*p != ',')
114 break;
115 p++;
117 return 0;
120 int get_param_value(char *buf, int buf_size,
121 const char *tag, const char *str)
123 return get_next_param_value(buf, buf_size, tag, &str);
127 * Searches an option list for an option with the given name
129 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
130 const char *name)
132 while (list && list->name) {
133 if (!strcmp(list->name, name)) {
134 return list;
136 list++;
139 return NULL;
142 static void parse_option_bool(const char *name, const char *value, bool *ret,
143 Error **errp)
145 if (value != NULL) {
146 if (!strcmp(value, "on")) {
147 *ret = 1;
148 } else if (!strcmp(value, "off")) {
149 *ret = 0;
150 } else {
151 error_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
153 } else {
154 *ret = 1;
158 static void parse_option_number(const char *name, const char *value,
159 uint64_t *ret, Error **errp)
161 char *postfix;
162 uint64_t number;
164 if (value != NULL) {
165 number = strtoull(value, &postfix, 0);
166 if (*postfix != '\0') {
167 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
168 return;
170 *ret = number;
171 } else {
172 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
176 static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
177 const char *name)
179 int i;
181 for (i = 0; desc[i].name != NULL; i++) {
182 if (strcmp(desc[i].name, name) == 0) {
183 return &desc[i];
187 return NULL;
190 void parse_option_size(const char *name, const char *value,
191 uint64_t *ret, Error **errp)
193 char *postfix;
194 double sizef;
196 if (value != NULL) {
197 sizef = strtod(value, &postfix);
198 switch (*postfix) {
199 case 'T':
200 sizef *= 1024;
201 /* fall through */
202 case 'G':
203 sizef *= 1024;
204 /* fall through */
205 case 'M':
206 sizef *= 1024;
207 /* fall through */
208 case 'K':
209 case 'k':
210 sizef *= 1024;
211 /* fall through */
212 case 'b':
213 case '\0':
214 *ret = (uint64_t) sizef;
215 break;
216 default:
217 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
218 #if 0 /* conversion from qerror_report() to error_set() broke this: */
219 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
220 "kilobytes, megabytes, gigabytes and terabytes.\n");
221 #endif
222 return;
224 } else {
225 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
230 * Sets the value of a parameter in a given option list. The parsing of the
231 * value depends on the type of option:
233 * OPT_FLAG (uses value.n):
234 * If no value is given, the flag is set to 1.
235 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
237 * OPT_STRING (uses value.s):
238 * value is strdup()ed and assigned as option value
240 * OPT_SIZE (uses value.n):
241 * The value is converted to an integer. Suffixes for kilobytes etc. are
242 * allowed (powers of 1024).
244 * Returns 0 on succes, -1 in error cases
246 int set_option_parameter(QEMUOptionParameter *list, const char *name,
247 const char *value)
249 bool flag;
250 Error *local_err = NULL;
252 // Find a matching parameter
253 list = get_option_parameter(list, name);
254 if (list == NULL) {
255 fprintf(stderr, "Unknown option '%s'\n", name);
256 return -1;
259 // Process parameter
260 switch (list->type) {
261 case OPT_FLAG:
262 parse_option_bool(name, value, &flag, &local_err);
263 if (!local_err) {
264 list->value.n = flag;
266 break;
268 case OPT_STRING:
269 if (value != NULL) {
270 list->value.s = g_strdup(value);
271 } else {
272 fprintf(stderr, "Option '%s' needs a parameter\n", name);
273 return -1;
275 break;
277 case OPT_SIZE:
278 parse_option_size(name, value, &list->value.n, &local_err);
279 break;
281 default:
282 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
283 return -1;
286 if (local_err) {
287 qerror_report_err(local_err);
288 error_free(local_err);
289 return -1;
292 list->assigned = true;
294 return 0;
298 * Sets the given parameter to an integer instead of a string.
299 * This function cannot be used to set string options.
301 * Returns 0 on success, -1 in error cases
303 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
304 uint64_t value)
306 // Find a matching parameter
307 list = get_option_parameter(list, name);
308 if (list == NULL) {
309 fprintf(stderr, "Unknown option '%s'\n", name);
310 return -1;
313 // Process parameter
314 switch (list->type) {
315 case OPT_FLAG:
316 case OPT_NUMBER:
317 case OPT_SIZE:
318 list->value.n = value;
319 break;
321 default:
322 return -1;
325 list->assigned = true;
327 return 0;
331 * Frees a option list. If it contains strings, the strings are freed as well.
333 void free_option_parameters(QEMUOptionParameter *list)
335 QEMUOptionParameter *cur = list;
337 while (cur && cur->name) {
338 if (cur->type == OPT_STRING) {
339 g_free(cur->value.s);
341 cur++;
344 g_free(list);
348 * Count valid options in list
350 static size_t count_option_parameters(QEMUOptionParameter *list)
352 size_t num_options = 0;
354 while (list && list->name) {
355 num_options++;
356 list++;
359 return num_options;
363 * Append an option list (list) to an option list (dest).
365 * If dest is NULL, a new copy of list is created.
367 * Returns a pointer to the first element of dest (or the newly allocated copy)
369 QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
370 QEMUOptionParameter *list)
372 size_t num_options, num_dest_options;
374 num_options = count_option_parameters(dest);
375 num_dest_options = num_options;
377 num_options += count_option_parameters(list);
379 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
380 dest[num_dest_options].name = NULL;
382 while (list && list->name) {
383 if (get_option_parameter(dest, list->name) == NULL) {
384 dest[num_dest_options++] = *list;
385 dest[num_dest_options].name = NULL;
387 list++;
390 return dest;
394 * Parses a parameter string (param) into an option list (dest).
396 * list is the template option list. If dest is NULL, a new copy of list is
397 * created. If list is NULL, this function fails.
399 * A parameter string consists of one or more parameters, separated by commas.
400 * Each parameter consists of its name and possibly of a value. In the latter
401 * case, the value is delimited by an = character. To specify a value which
402 * contains commas, double each comma so it won't be recognized as the end of
403 * the parameter.
405 * For more details of the parsing see above.
407 * Returns a pointer to the first element of dest (or the newly allocated copy)
408 * or NULL in error cases
410 QEMUOptionParameter *parse_option_parameters(const char *param,
411 QEMUOptionParameter *list, QEMUOptionParameter *dest)
413 QEMUOptionParameter *allocated = NULL;
414 char name[256];
415 char value[256];
416 char *param_delim, *value_delim;
417 char next_delim;
418 int i;
420 if (list == NULL) {
421 return NULL;
424 if (dest == NULL) {
425 dest = allocated = append_option_parameters(NULL, list);
428 for (i = 0; dest[i].name; i++) {
429 dest[i].assigned = false;
432 while (*param) {
434 // Find parameter name and value in the string
435 param_delim = strchr(param, ',');
436 value_delim = strchr(param, '=');
438 if (value_delim && (value_delim < param_delim || !param_delim)) {
439 next_delim = '=';
440 } else {
441 next_delim = ',';
442 value_delim = NULL;
445 param = get_opt_name(name, sizeof(name), param, next_delim);
446 if (value_delim) {
447 param = get_opt_value(value, sizeof(value), param + 1);
449 if (*param != '\0') {
450 param++;
453 // Set the parameter
454 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
455 goto fail;
459 return dest;
461 fail:
462 // Only free the list if it was newly allocated
463 free_option_parameters(allocated);
464 return NULL;
467 bool has_help_option(const char *param)
469 size_t buflen = strlen(param) + 1;
470 char *buf = g_malloc0(buflen);
471 const char *p = param;
472 bool result = false;
474 while (*p) {
475 p = get_opt_value(buf, buflen, p);
476 if (*p) {
477 p++;
480 if (is_help_option(buf)) {
481 result = true;
482 goto out;
486 out:
487 free(buf);
488 return result;
491 bool is_valid_option_list(const char *param)
493 size_t buflen = strlen(param) + 1;
494 char *buf = g_malloc0(buflen);
495 const char *p = param;
496 bool result = true;
498 while (*p) {
499 p = get_opt_value(buf, buflen, p);
500 if (*p && !*++p) {
501 result = false;
502 goto out;
505 if (!*buf || *buf == ',') {
506 result = false;
507 goto out;
511 out:
512 free(buf);
513 return result;
517 * Prints all options of a list that have a value to stdout
519 void print_option_parameters(QEMUOptionParameter *list)
521 while (list && list->name) {
522 switch (list->type) {
523 case OPT_STRING:
524 if (list->value.s != NULL) {
525 printf("%s='%s' ", list->name, list->value.s);
527 break;
528 case OPT_FLAG:
529 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
530 break;
531 case OPT_SIZE:
532 case OPT_NUMBER:
533 printf("%s=%" PRId64 " ", list->name, list->value.n);
534 break;
535 default:
536 printf("%s=(unknown type) ", list->name);
537 break;
539 list++;
544 * Prints an overview of all available options
546 void print_option_help(QEMUOptionParameter *list)
548 printf("Supported options:\n");
549 while (list && list->name) {
550 printf("%-16s %s\n", list->name,
551 list->help ? list->help : "No description available");
552 list++;
556 /* ------------------------------------------------------------------ */
558 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
560 QemuOpt *opt;
562 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
563 if (strcmp(opt->name, name) != 0)
564 continue;
565 return opt;
567 return NULL;
570 static void qemu_opt_del(QemuOpt *opt)
572 QTAILQ_REMOVE(&opt->opts->head, opt, next);
573 g_free(opt->name);
574 g_free(opt->str);
575 g_free(opt);
578 const char *qemu_opt_get(QemuOpts *opts, const char *name)
580 QemuOpt *opt = qemu_opt_find(opts, name);
582 if (!opt) {
583 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
584 if (desc && desc->def_value_str) {
585 return desc->def_value_str;
588 return opt ? opt->str : NULL;
591 bool qemu_opt_has_help_opt(QemuOpts *opts)
593 QemuOpt *opt;
595 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
596 if (is_help_option(opt->name)) {
597 return true;
600 return false;
603 bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
605 QemuOpt *opt = qemu_opt_find(opts, name);
607 if (opt == NULL) {
608 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
609 if (desc && desc->def_value_str) {
610 parse_option_bool(name, desc->def_value_str, &defval, &error_abort);
612 return defval;
614 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
615 return opt->value.boolean;
618 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
620 QemuOpt *opt = qemu_opt_find(opts, name);
622 if (opt == NULL) {
623 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
624 if (desc && desc->def_value_str) {
625 parse_option_number(name, desc->def_value_str, &defval,
626 &error_abort);
628 return defval;
630 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
631 return opt->value.uint;
634 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
636 QemuOpt *opt = qemu_opt_find(opts, name);
638 if (opt == NULL) {
639 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
640 if (desc && desc->def_value_str) {
641 parse_option_size(name, desc->def_value_str, &defval, &error_abort);
643 return defval;
645 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
646 return opt->value.uint;
649 static void qemu_opt_parse(QemuOpt *opt, Error **errp)
651 if (opt->desc == NULL)
652 return;
654 switch (opt->desc->type) {
655 case QEMU_OPT_STRING:
656 /* nothing */
657 return;
658 case QEMU_OPT_BOOL:
659 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
660 break;
661 case QEMU_OPT_NUMBER:
662 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
663 break;
664 case QEMU_OPT_SIZE:
665 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
666 break;
667 default:
668 abort();
672 static bool opts_accepts_any(const QemuOpts *opts)
674 return opts->list->desc[0].name == NULL;
677 int qemu_opt_unset(QemuOpts *opts, const char *name)
679 QemuOpt *opt = qemu_opt_find(opts, name);
681 assert(opts_accepts_any(opts));
683 if (opt == NULL) {
684 return -1;
685 } else {
686 qemu_opt_del(opt);
687 return 0;
691 static void opt_set(QemuOpts *opts, const char *name, const char *value,
692 bool prepend, Error **errp)
694 QemuOpt *opt;
695 const QemuOptDesc *desc;
696 Error *local_err = NULL;
698 desc = find_desc_by_name(opts->list->desc, name);
699 if (!desc && !opts_accepts_any(opts)) {
700 error_set(errp, QERR_INVALID_PARAMETER, name);
701 return;
704 opt = g_malloc0(sizeof(*opt));
705 opt->name = g_strdup(name);
706 opt->opts = opts;
707 if (prepend) {
708 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
709 } else {
710 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
712 opt->desc = desc;
713 opt->str = g_strdup(value);
714 qemu_opt_parse(opt, &local_err);
715 if (local_err) {
716 error_propagate(errp, local_err);
717 qemu_opt_del(opt);
721 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
723 Error *local_err = NULL;
725 opt_set(opts, name, value, false, &local_err);
726 if (local_err) {
727 qerror_report_err(local_err);
728 error_free(local_err);
729 return -1;
732 return 0;
735 void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
736 Error **errp)
738 opt_set(opts, name, value, false, errp);
741 int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
743 QemuOpt *opt;
744 const QemuOptDesc *desc = opts->list->desc;
746 opt = g_malloc0(sizeof(*opt));
747 opt->desc = find_desc_by_name(desc, name);
748 if (!opt->desc && !opts_accepts_any(opts)) {
749 qerror_report(QERR_INVALID_PARAMETER, name);
750 g_free(opt);
751 return -1;
754 opt->name = g_strdup(name);
755 opt->opts = opts;
756 opt->value.boolean = !!val;
757 opt->str = g_strdup(val ? "on" : "off");
758 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
760 return 0;
763 int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
765 QemuOpt *opt;
766 const QemuOptDesc *desc = opts->list->desc;
768 opt = g_malloc0(sizeof(*opt));
769 opt->desc = find_desc_by_name(desc, name);
770 if (!opt->desc && !opts_accepts_any(opts)) {
771 qerror_report(QERR_INVALID_PARAMETER, name);
772 g_free(opt);
773 return -1;
776 opt->name = g_strdup(name);
777 opt->opts = opts;
778 opt->value.uint = val;
779 opt->str = g_strdup_printf("%" PRId64, val);
780 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
782 return 0;
785 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
786 int abort_on_failure)
788 QemuOpt *opt;
789 int rc = 0;
791 QTAILQ_FOREACH(opt, &opts->head, next) {
792 rc = func(opt->name, opt->str, opaque);
793 if (abort_on_failure && rc != 0)
794 break;
796 return rc;
799 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
801 QemuOpts *opts;
803 QTAILQ_FOREACH(opts, &list->head, next) {
804 if (!opts->id && !id) {
805 return opts;
807 if (opts->id && id && !strcmp(opts->id, id)) {
808 return opts;
811 return NULL;
814 static int id_wellformed(const char *id)
816 int i;
818 if (!qemu_isalpha(id[0])) {
819 return 0;
821 for (i = 1; id[i]; i++) {
822 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
823 return 0;
826 return 1;
829 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
830 int fail_if_exists, Error **errp)
832 QemuOpts *opts = NULL;
834 if (id) {
835 if (!id_wellformed(id)) {
836 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
837 #if 0 /* conversion from qerror_report() to error_set() broke this: */
838 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
839 #endif
840 return NULL;
842 opts = qemu_opts_find(list, id);
843 if (opts != NULL) {
844 if (fail_if_exists && !list->merge_lists) {
845 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
846 return NULL;
847 } else {
848 return opts;
851 } else if (list->merge_lists) {
852 opts = qemu_opts_find(list, NULL);
853 if (opts) {
854 return opts;
857 opts = g_malloc0(sizeof(*opts));
858 opts->id = g_strdup(id);
859 opts->list = list;
860 loc_save(&opts->loc);
861 QTAILQ_INIT(&opts->head);
862 QTAILQ_INSERT_TAIL(&list->head, opts, next);
863 return opts;
866 void qemu_opts_reset(QemuOptsList *list)
868 QemuOpts *opts, *next_opts;
870 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
871 qemu_opts_del(opts);
875 void qemu_opts_loc_restore(QemuOpts *opts)
877 loc_restore(&opts->loc);
880 int qemu_opts_set(QemuOptsList *list, const char *id,
881 const char *name, const char *value)
883 QemuOpts *opts;
884 Error *local_err = NULL;
886 opts = qemu_opts_create(list, id, 1, &local_err);
887 if (local_err) {
888 qerror_report_err(local_err);
889 error_free(local_err);
890 return -1;
892 return qemu_opt_set(opts, name, value);
895 const char *qemu_opts_id(QemuOpts *opts)
897 return opts->id;
900 /* The id string will be g_free()d by qemu_opts_del */
901 void qemu_opts_set_id(QemuOpts *opts, char *id)
903 opts->id = id;
906 void qemu_opts_del(QemuOpts *opts)
908 QemuOpt *opt;
910 for (;;) {
911 opt = QTAILQ_FIRST(&opts->head);
912 if (opt == NULL)
913 break;
914 qemu_opt_del(opt);
916 QTAILQ_REMOVE(&opts->list->head, opts, next);
917 g_free(opts->id);
918 g_free(opts);
921 void qemu_opts_print(QemuOpts *opts)
923 QemuOpt *opt;
924 QemuOptDesc *desc = opts->list->desc;
926 if (desc[0].name == NULL) {
927 QTAILQ_FOREACH(opt, &opts->head, next) {
928 printf("%s=\"%s\" ", opt->name, opt->str);
930 return;
932 for (; desc && desc->name; desc++) {
933 const char *value;
934 QemuOpt *opt = qemu_opt_find(opts, desc->name);
936 value = opt ? opt->str : desc->def_value_str;
937 if (!value) {
938 continue;
940 if (desc->type == QEMU_OPT_STRING) {
941 printf("%s='%s' ", desc->name, value);
942 } else if ((desc->type == QEMU_OPT_SIZE ||
943 desc->type == QEMU_OPT_NUMBER) && opt) {
944 printf("%s=%" PRId64 " ", desc->name, opt->value.uint);
945 } else {
946 printf("%s=%s ", desc->name, value);
951 static int opts_do_parse(QemuOpts *opts, const char *params,
952 const char *firstname, bool prepend)
954 char option[128], value[1024];
955 const char *p,*pe,*pc;
956 Error *local_err = NULL;
958 for (p = params; *p != '\0'; p++) {
959 pe = strchr(p, '=');
960 pc = strchr(p, ',');
961 if (!pe || (pc && pc < pe)) {
962 /* found "foo,more" */
963 if (p == params && firstname) {
964 /* implicitly named first option */
965 pstrcpy(option, sizeof(option), firstname);
966 p = get_opt_value(value, sizeof(value), p);
967 } else {
968 /* option without value, probably a flag */
969 p = get_opt_name(option, sizeof(option), p, ',');
970 if (strncmp(option, "no", 2) == 0) {
971 memmove(option, option+2, strlen(option+2)+1);
972 pstrcpy(value, sizeof(value), "off");
973 } else {
974 pstrcpy(value, sizeof(value), "on");
977 } else {
978 /* found "foo=bar,more" */
979 p = get_opt_name(option, sizeof(option), p, '=');
980 if (*p != '=') {
981 break;
983 p++;
984 p = get_opt_value(value, sizeof(value), p);
986 if (strcmp(option, "id") != 0) {
987 /* store and parse */
988 opt_set(opts, option, value, prepend, &local_err);
989 if (local_err) {
990 qerror_report_err(local_err);
991 error_free(local_err);
992 return -1;
995 if (*p != ',') {
996 break;
999 return 0;
1002 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
1004 return opts_do_parse(opts, params, firstname, false);
1007 static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
1008 int permit_abbrev, bool defaults)
1010 const char *firstname;
1011 char value[1024], *id = NULL;
1012 const char *p;
1013 QemuOpts *opts;
1014 Error *local_err = NULL;
1016 assert(!permit_abbrev || list->implied_opt_name);
1017 firstname = permit_abbrev ? list->implied_opt_name : NULL;
1019 if (strncmp(params, "id=", 3) == 0) {
1020 get_opt_value(value, sizeof(value), params+3);
1021 id = value;
1022 } else if ((p = strstr(params, ",id=")) != NULL) {
1023 get_opt_value(value, sizeof(value), p+4);
1024 id = value;
1028 * This code doesn't work for defaults && !list->merge_lists: when
1029 * params has no id=, and list has an element with !opts->id, it
1030 * appends a new element instead of returning the existing opts.
1031 * However, we got no use for this case. Guard against possible
1032 * (if unlikely) future misuse:
1034 assert(!defaults || list->merge_lists);
1035 opts = qemu_opts_create(list, id, !defaults, &local_err);
1036 if (opts == NULL) {
1037 if (local_err) {
1038 qerror_report_err(local_err);
1039 error_free(local_err);
1041 return NULL;
1044 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
1045 qemu_opts_del(opts);
1046 return NULL;
1049 return opts;
1052 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
1053 int permit_abbrev)
1055 return opts_parse(list, params, permit_abbrev, false);
1058 void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
1059 int permit_abbrev)
1061 QemuOpts *opts;
1063 opts = opts_parse(list, params, permit_abbrev, true);
1064 assert(opts);
1067 typedef struct OptsFromQDictState {
1068 QemuOpts *opts;
1069 Error **errp;
1070 } OptsFromQDictState;
1072 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
1074 OptsFromQDictState *state = opaque;
1075 char buf[32];
1076 const char *value;
1077 int n;
1079 if (!strcmp(key, "id") || *state->errp) {
1080 return;
1083 switch (qobject_type(obj)) {
1084 case QTYPE_QSTRING:
1085 value = qstring_get_str(qobject_to_qstring(obj));
1086 break;
1087 case QTYPE_QINT:
1088 n = snprintf(buf, sizeof(buf), "%" PRId64,
1089 qint_get_int(qobject_to_qint(obj)));
1090 assert(n < sizeof(buf));
1091 value = buf;
1092 break;
1093 case QTYPE_QFLOAT:
1094 n = snprintf(buf, sizeof(buf), "%.17g",
1095 qfloat_get_double(qobject_to_qfloat(obj)));
1096 assert(n < sizeof(buf));
1097 value = buf;
1098 break;
1099 case QTYPE_QBOOL:
1100 pstrcpy(buf, sizeof(buf),
1101 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
1102 value = buf;
1103 break;
1104 default:
1105 return;
1108 qemu_opt_set_err(state->opts, key, value, state->errp);
1112 * Create QemuOpts from a QDict.
1113 * Use value of key "id" as ID if it exists and is a QString.
1114 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1115 * other types are silently ignored.
1117 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1118 Error **errp)
1120 OptsFromQDictState state;
1121 Error *local_err = NULL;
1122 QemuOpts *opts;
1124 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1125 &local_err);
1126 if (local_err) {
1127 error_propagate(errp, local_err);
1128 return NULL;
1131 assert(opts != NULL);
1133 state.errp = &local_err;
1134 state.opts = opts;
1135 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
1136 if (local_err) {
1137 error_propagate(errp, local_err);
1138 qemu_opts_del(opts);
1139 return NULL;
1142 return opts;
1146 * Adds all QDict entries to the QemuOpts that can be added and removes them
1147 * from the QDict. When this function returns, the QDict contains only those
1148 * entries that couldn't be added to the QemuOpts.
1150 void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1152 const QDictEntry *entry, *next;
1154 entry = qdict_first(qdict);
1156 while (entry != NULL) {
1157 Error *local_err = NULL;
1158 OptsFromQDictState state = {
1159 .errp = &local_err,
1160 .opts = opts,
1163 next = qdict_next(qdict, entry);
1165 if (find_desc_by_name(opts->list->desc, entry->key)) {
1166 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
1167 if (local_err) {
1168 error_propagate(errp, local_err);
1169 return;
1170 } else {
1171 qdict_del(qdict, entry->key);
1175 entry = next;
1180 * Convert from QemuOpts to QDict.
1181 * The QDict values are of type QString.
1182 * TODO We'll want to use types appropriate for opt->desc->type, but
1183 * this is enough for now.
1185 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1187 QemuOpt *opt;
1188 QObject *val;
1190 if (!qdict) {
1191 qdict = qdict_new();
1193 if (opts->id) {
1194 qdict_put(qdict, "id", qstring_from_str(opts->id));
1196 QTAILQ_FOREACH(opt, &opts->head, next) {
1197 val = QOBJECT(qstring_from_str(opt->str));
1198 qdict_put_obj(qdict, opt->name, val);
1200 return qdict;
1203 /* Validate parsed opts against descriptions where no
1204 * descriptions were provided in the QemuOptsList.
1206 void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
1208 QemuOpt *opt;
1209 Error *local_err = NULL;
1211 assert(opts_accepts_any(opts));
1213 QTAILQ_FOREACH(opt, &opts->head, next) {
1214 opt->desc = find_desc_by_name(desc, opt->name);
1215 if (!opt->desc) {
1216 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1217 return;
1220 qemu_opt_parse(opt, &local_err);
1221 if (local_err) {
1222 error_propagate(errp, local_err);
1223 return;
1228 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1229 int abort_on_failure)
1231 Location loc;
1232 QemuOpts *opts;
1233 int rc = 0;
1235 loc_push_none(&loc);
1236 QTAILQ_FOREACH(opts, &list->head, next) {
1237 loc_restore(&opts->loc);
1238 rc |= func(opts, opaque);
1239 if (abort_on_failure && rc != 0)
1240 break;
1242 loc_pop(&loc);
1243 return rc;