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
29 #include "qemu-common.h"
30 #include "qemu-error.h"
31 #include "qemu-objects.h"
32 #include "qemu-option.h"
35 * Extracts the name of an option from the parameter string (p points at the
36 * first byte of the option name)
38 * The option name is delimited by delim (usually , or =) or the string end
39 * and is copied into buf. If the option name is longer than buf_size, it is
40 * truncated. buf is always zero terminated.
42 * The return value is the position of the delimiter/zero byte after the option
45 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
50 while (*p != '\0' && *p != delim) {
51 if (q && (q - buf) < buf_size - 1)
62 * Extracts the value of an option from the parameter string p (p points at the
63 * first byte of the option value)
65 * This function is comparable to get_opt_name with the difference that the
66 * delimiter is fixed to be comma which starts a new option. To specify an
67 * option value that contains commas, double each comma.
69 const char *get_opt_value(char *buf, int buf_size, const char *p)
80 if (q && (q - buf) < buf_size - 1)
90 int get_next_param_value(char *buf, int buf_size,
91 const char *tag, const char **pstr)
98 p = get_opt_name(option, sizeof(option), p, '=');
102 if (!strcmp(tag, option)) {
103 *pstr = get_opt_value(buf, buf_size, p);
109 p = get_opt_value(NULL, 0, p);
118 int get_param_value(char *buf, int buf_size,
119 const char *tag, const char *str)
121 return get_next_param_value(buf, buf_size, tag, &str);
124 int check_params(char *buf, int buf_size,
125 const char * const *params, const char *str)
132 p = get_opt_name(buf, buf_size, p, '=');
137 for (i = 0; params[i] != NULL; i++) {
138 if (!strcmp(params[i], buf)) {
142 if (params[i] == NULL) {
145 p = get_opt_value(NULL, 0, p);
155 * Searches an option list for an option with the given name
157 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
160 while (list && list->name) {
161 if (!strcmp(list->name, name)) {
170 static int parse_option_bool(const char *name, const char *value, int *ret)
173 if (!strcmp(value, "on")) {
175 } else if (!strcmp(value, "off")) {
178 fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
187 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
193 number = strtoull(value, &postfix, 0);
194 if (*postfix != '\0') {
195 fprintf(stderr, "Option '%s' needs a number as parameter\n", name);
200 fprintf(stderr, "Option '%s' needs a parameter\n", name);
206 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
212 sizef = strtod(value, &postfix);
225 *ret = (uint64_t) sizef;
228 fprintf(stderr, "Option '%s' needs size as parameter\n", name);
229 fprintf(stderr, "You may use k, M, G or T suffixes for "
230 "kilobytes, megabytes, gigabytes and terabytes.\n");
234 fprintf(stderr, "Option '%s' needs a parameter\n", name);
241 * Sets the value of a parameter in a given option list. The parsing of the
242 * value depends on the type of option:
244 * OPT_FLAG (uses value.n):
245 * If no value is given, the flag is set to 1.
246 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
248 * OPT_STRING (uses value.s):
249 * value is strdup()ed and assigned as option value
251 * OPT_SIZE (uses value.n):
252 * The value is converted to an integer. Suffixes for kilobytes etc. are
253 * allowed (powers of 1024).
255 * Returns 0 on succes, -1 in error cases
257 int set_option_parameter(QEMUOptionParameter *list, const char *name,
262 // Find a matching parameter
263 list = get_option_parameter(list, name);
265 fprintf(stderr, "Unknown option '%s'\n", name);
270 switch (list->type) {
272 if (parse_option_bool(name, value, &flag) == -1)
274 list->value.n = flag;
279 list->value.s = qemu_strdup(value);
281 fprintf(stderr, "Option '%s' needs a parameter\n", name);
287 if (parse_option_size(name, value, &list->value.n) == -1)
292 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
300 * Sets the given parameter to an integer instead of a string.
301 * This function cannot be used to set string options.
303 * Returns 0 on success, -1 in error cases
305 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
308 // Find a matching parameter
309 list = get_option_parameter(list, name);
311 fprintf(stderr, "Unknown option '%s'\n", name);
316 switch (list->type) {
320 list->value.n = value;
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 qemu_free(cur->value.s);
348 * Parses a parameter string (param) into an option list (dest).
350 * list is the templace is. If dest is NULL, a new copy of list is created for
351 * it. If list is NULL, this function fails.
353 * A parameter string consists of one or more parameters, separated by commas.
354 * Each parameter consists of its name and possibly of a value. In the latter
355 * case, the value is delimited by an = character. To specify a value which
356 * contains commas, double each comma so it won't be recognized as the end of
359 * For more details of the parsing see above.
361 * Returns a pointer to the first element of dest (or the newly allocated copy)
362 * or NULL in error cases
364 QEMUOptionParameter *parse_option_parameters(const char *param,
365 QEMUOptionParameter *list, QEMUOptionParameter *dest)
367 QEMUOptionParameter *cur;
368 QEMUOptionParameter *allocated = NULL;
371 char *param_delim, *value_delim;
380 // Count valid options
388 // Create a copy of the option list to fill in values
389 dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
391 memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
396 // Find parameter name and value in the string
397 param_delim = strchr(param, ',');
398 value_delim = strchr(param, '=');
400 if (value_delim && (value_delim < param_delim || !param_delim)) {
407 param = get_opt_name(name, sizeof(name), param, next_delim);
409 param = get_opt_value(value, sizeof(value), param + 1);
411 if (*param != '\0') {
416 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
424 // Only free the list if it was newly allocated
425 free_option_parameters(allocated);
430 * Prints all options of a list that have a value to stdout
432 void print_option_parameters(QEMUOptionParameter *list)
434 while (list && list->name) {
435 switch (list->type) {
437 if (list->value.s != NULL) {
438 printf("%s='%s' ", list->name, list->value.s);
442 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
446 printf("%s=%" PRId64 " ", list->name, list->value.n);
449 printf("%s=(unkown type) ", list->name);
457 * Prints an overview of all available options
459 void print_option_help(QEMUOptionParameter *list)
461 printf("Supported options:\n");
462 while (list && list->name) {
463 printf("%-16s %s\n", list->name,
464 list->help ? list->help : "No description available");
469 /* ------------------------------------------------------------------ */
475 const QemuOptDesc *desc;
482 QTAILQ_ENTRY(QemuOpt) next;
489 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
490 QTAILQ_ENTRY(QemuOpts) next;
493 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
497 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
498 if (strcmp(opt->name, name) != 0)
505 const char *qemu_opt_get(QemuOpts *opts, const char *name)
507 QemuOpt *opt = qemu_opt_find(opts, name);
508 return opt ? opt->str : NULL;
511 int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
513 QemuOpt *opt = qemu_opt_find(opts, name);
517 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
518 return opt->value.boolean;
521 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
523 QemuOpt *opt = qemu_opt_find(opts, name);
527 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
528 return opt->value.uint;
531 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
533 QemuOpt *opt = qemu_opt_find(opts, name);
537 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
538 return opt->value.uint;
541 static int qemu_opt_parse(QemuOpt *opt)
543 if (opt->desc == NULL)
545 switch (opt->desc->type) {
546 case QEMU_OPT_STRING:
550 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
551 case QEMU_OPT_NUMBER:
552 return parse_option_number(opt->name, opt->str, &opt->value.uint);
554 return parse_option_size(opt->name, opt->str, &opt->value.uint);
560 static void qemu_opt_del(QemuOpt *opt)
562 QTAILQ_REMOVE(&opt->opts->head, opt, next);
563 qemu_free((/* !const */ char*)opt->name);
564 qemu_free((/* !const */ char*)opt->str);
568 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
571 const QemuOptDesc *desc = opts->list->desc;
574 for (i = 0; desc[i].name != NULL; i++) {
575 if (strcmp(desc[i].name, name) == 0) {
579 if (desc[i].name == NULL) {
581 /* empty list -> allow any */;
583 fprintf(stderr, "option \"%s\" is not valid for %s\n",
584 name, opts->list->name);
589 opt = qemu_mallocz(sizeof(*opt));
590 opt->name = qemu_strdup(name);
592 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
593 if (desc[i].name != NULL) {
597 opt->str = qemu_strdup(value);
599 if (qemu_opt_parse(opt) < 0) {
600 fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str,
601 opts->list->name, opt->name);
608 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
609 int abort_on_failure)
614 QTAILQ_FOREACH(opt, &opts->head, next) {
615 rc = func(opt->name, opt->str, opaque);
616 if (abort_on_failure && rc != 0)
622 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
626 QTAILQ_FOREACH(opts, &list->head, next) {
630 if (strcmp(opts->id, id) != 0) {
638 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
640 QemuOpts *opts = NULL;
643 opts = qemu_opts_find(list, id);
645 if (fail_if_exists) {
646 fprintf(stderr, "tried to create id \"%s\" twice for \"%s\"\n",
654 opts = qemu_mallocz(sizeof(*opts));
656 opts->id = qemu_strdup(id);
659 loc_save(&opts->loc);
660 QTAILQ_INIT(&opts->head);
661 QTAILQ_INSERT_TAIL(&list->head, opts, next);
665 int qemu_opts_set(QemuOptsList *list, const char *id,
666 const char *name, const char *value)
670 opts = qemu_opts_create(list, id, 1);
674 return qemu_opt_set(opts, name, value);
677 const char *qemu_opts_id(QemuOpts *opts)
682 void qemu_opts_del(QemuOpts *opts)
687 opt = QTAILQ_FIRST(&opts->head);
692 QTAILQ_REMOVE(&opts->list->head, opts, next);
697 int qemu_opts_print(QemuOpts *opts, void *dummy)
701 fprintf(stderr, "%s: %s:", opts->list->name,
702 opts->id ? opts->id : "<noid>");
703 QTAILQ_FOREACH(opt, &opts->head, next) {
704 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
706 fprintf(stderr, "\n");
710 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
712 char option[128], value[1024];
713 const char *p,*pe,*pc;
715 for (p = params; *p != '\0'; p++) {
718 if (!pe || (pc && pc < pe)) {
719 /* found "foo,more" */
720 if (p == params && firstname) {
721 /* implicitly named first option */
722 pstrcpy(option, sizeof(option), firstname);
723 p = get_opt_value(value, sizeof(value), p);
725 /* option without value, probably a flag */
726 p = get_opt_name(option, sizeof(option), p, ',');
727 if (strncmp(option, "no", 2) == 0) {
728 memmove(option, option+2, strlen(option+2)+1);
729 pstrcpy(value, sizeof(value), "off");
731 pstrcpy(value, sizeof(value), "on");
735 /* found "foo=bar,more" */
736 p = get_opt_name(option, sizeof(option), p, '=');
741 p = get_opt_value(value, sizeof(value), p);
743 if (strcmp(option, "id") != 0) {
744 /* store and parse */
745 if (qemu_opt_set(opts, option, value) == -1) {
756 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname)
758 char value[1024], *id = NULL;
762 if (strncmp(params, "id=", 3) == 0) {
763 get_opt_value(value, sizeof(value), params+3);
764 id = qemu_strdup(value);
765 } else if ((p = strstr(params, ",id=")) != NULL) {
766 get_opt_value(value, sizeof(value), p+4);
767 id = qemu_strdup(value);
769 opts = qemu_opts_create(list, id, 1);
773 if (qemu_opts_do_parse(opts, params, firstname) != 0) {
781 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
787 if (!strcmp(key, "id")) {
791 switch (qobject_type(obj)) {
793 value = qstring_get_str(qobject_to_qstring(obj));
796 n = snprintf(buf, sizeof(buf), "%" PRId64,
797 qint_get_int(qobject_to_qint(obj)));
798 assert(n < sizeof(buf));
802 n = snprintf(buf, sizeof(buf), "%.17g",
803 qfloat_get_double(qobject_to_qfloat(obj)));
804 assert(n < sizeof(buf));
808 strcpy(buf, qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
814 qemu_opt_set(opaque, key, value);
818 * Create QemuOpts from a QDict.
819 * Use value of key "id" as ID if it exists and is a QString.
820 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
821 * other types are silently ignored.
823 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
827 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
831 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
836 * Convert from QemuOpts to QDict.
837 * The QDict values are of type QString.
838 * TODO We'll want to use types appropriate for opt->desc->type, but
839 * this is enough for now.
841 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
850 qdict_put(qdict, "id", qstring_from_str(opts->id));
852 QTAILQ_FOREACH(opt, &opts->head, next) {
853 val = QOBJECT(qstring_from_str(opt->str));
854 qdict_put_obj(qdict, opt->name, val);
859 /* Validate parsed opts against descriptions where no
860 * descriptions were provided in the QemuOptsList.
862 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
866 assert(opts->list->desc[0].name == NULL);
868 QTAILQ_FOREACH(opt, &opts->head, next) {
871 for (i = 0; desc[i].name != NULL; i++) {
872 if (strcmp(desc[i].name, opt->name) == 0) {
876 if (desc[i].name == NULL) {
877 fprintf(stderr, "option \"%s\" is not valid for %s\n",
878 opt->name, opts->list->name);
882 opt->desc = &desc[i];
884 if (qemu_opt_parse(opt) < 0) {
892 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
893 int abort_on_failure)
900 QTAILQ_FOREACH(opts, &list->head, next) {
901 loc_restore(&opts->loc);
902 rc |= func(opts, opaque);
903 if (abort_on_failure && rc != 0)