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"
36 * Extracts the name of an option from the parameter string (p points at the
37 * first byte of the option name)
39 * The option name is delimited by delim (usually , or =) or the string end
40 * and is copied into buf. If the option name is longer than buf_size, it is
41 * truncated. buf is always zero terminated.
43 * The return value is the position of the delimiter/zero byte after the option
46 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
51 while (*p
!= '\0' && *p
!= delim
) {
52 if (q
&& (q
- buf
) < buf_size
- 1)
63 * Extracts the value of an option from the parameter string p (p points at the
64 * first byte of the option value)
66 * This function is comparable to get_opt_name with the difference that the
67 * delimiter is fixed to be comma which starts a new option. To specify an
68 * option value that contains commas, double each comma.
70 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
81 if (q
&& (q
- buf
) < buf_size
- 1)
91 int get_next_param_value(char *buf
, int buf_size
,
92 const char *tag
, const char **pstr
)
99 p
= get_opt_name(option
, sizeof(option
), p
, '=');
103 if (!strcmp(tag
, option
)) {
104 *pstr
= get_opt_value(buf
, buf_size
, p
);
110 p
= get_opt_value(NULL
, 0, p
);
119 int get_param_value(char *buf
, int buf_size
,
120 const char *tag
, const char *str
)
122 return get_next_param_value(buf
, buf_size
, tag
, &str
);
125 int check_params(char *buf
, int buf_size
,
126 const char * const *params
, const char *str
)
133 p
= get_opt_name(buf
, buf_size
, p
, '=');
138 for (i
= 0; params
[i
] != NULL
; i
++) {
139 if (!strcmp(params
[i
], buf
)) {
143 if (params
[i
] == NULL
) {
146 p
= get_opt_value(NULL
, 0, p
);
156 * Searches an option list for an option with the given name
158 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
161 while (list
&& list
->name
) {
162 if (!strcmp(list
->name
, name
)) {
171 static int parse_option_bool(const char *name
, const char *value
, int *ret
)
174 if (!strcmp(value
, "on")) {
176 } else if (!strcmp(value
, "off")) {
179 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "'on' or 'off'");
188 static int parse_option_number(const char *name
, const char *value
, uint64_t *ret
)
194 number
= strtoull(value
, &postfix
, 0);
195 if (*postfix
!= '\0') {
196 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
201 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
207 static int parse_option_size(const char *name
, const char *value
, uint64_t *ret
)
213 sizef
= strtod(value
, &postfix
);
226 *ret
= (uint64_t) sizef
;
229 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
230 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
231 "kilobytes, megabytes, gigabytes and terabytes.\n");
235 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
242 * Sets the value of a parameter in a given option list. The parsing of the
243 * value depends on the type of option:
245 * OPT_FLAG (uses value.n):
246 * If no value is given, the flag is set to 1.
247 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
249 * OPT_STRING (uses value.s):
250 * value is strdup()ed and assigned as option value
252 * OPT_SIZE (uses value.n):
253 * The value is converted to an integer. Suffixes for kilobytes etc. are
254 * allowed (powers of 1024).
256 * Returns 0 on succes, -1 in error cases
258 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
263 // Find a matching parameter
264 list
= get_option_parameter(list
, name
);
266 fprintf(stderr
, "Unknown option '%s'\n", name
);
271 switch (list
->type
) {
273 if (parse_option_bool(name
, value
, &flag
) == -1)
275 list
->value
.n
= flag
;
280 list
->value
.s
= qemu_strdup(value
);
282 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
288 if (parse_option_size(name
, value
, &list
->value
.n
) == -1)
293 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
301 * Sets the given parameter to an integer instead of a string.
302 * This function cannot be used to set string options.
304 * Returns 0 on success, -1 in error cases
306 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
309 // Find a matching parameter
310 list
= get_option_parameter(list
, name
);
312 fprintf(stderr
, "Unknown option '%s'\n", name
);
317 switch (list
->type
) {
321 list
->value
.n
= value
;
332 * Frees a option list. If it contains strings, the strings are freed as well.
334 void free_option_parameters(QEMUOptionParameter
*list
)
336 QEMUOptionParameter
*cur
= list
;
338 while (cur
&& cur
->name
) {
339 if (cur
->type
== OPT_STRING
) {
340 qemu_free(cur
->value
.s
);
349 * Parses a parameter string (param) into an option list (dest).
351 * list is the templace is. If dest is NULL, a new copy of list is created for
352 * it. If list is NULL, this function fails.
354 * A parameter string consists of one or more parameters, separated by commas.
355 * Each parameter consists of its name and possibly of a value. In the latter
356 * case, the value is delimited by an = character. To specify a value which
357 * contains commas, double each comma so it won't be recognized as the end of
360 * For more details of the parsing see above.
362 * Returns a pointer to the first element of dest (or the newly allocated copy)
363 * or NULL in error cases
365 QEMUOptionParameter
*parse_option_parameters(const char *param
,
366 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
368 QEMUOptionParameter
*cur
;
369 QEMUOptionParameter
*allocated
= NULL
;
372 char *param_delim
, *value_delim
;
381 // Count valid options
389 // Create a copy of the option list to fill in values
390 dest
= qemu_mallocz((num_options
+ 1) * sizeof(QEMUOptionParameter
));
392 memcpy(dest
, list
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
397 // Find parameter name and value in the string
398 param_delim
= strchr(param
, ',');
399 value_delim
= strchr(param
, '=');
401 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
408 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
410 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
412 if (*param
!= '\0') {
417 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
425 // Only free the list if it was newly allocated
426 free_option_parameters(allocated
);
431 * Prints all options of a list that have a value to stdout
433 void print_option_parameters(QEMUOptionParameter
*list
)
435 while (list
&& list
->name
) {
436 switch (list
->type
) {
438 if (list
->value
.s
!= NULL
) {
439 printf("%s='%s' ", list
->name
, list
->value
.s
);
443 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
447 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
450 printf("%s=(unkown type) ", list
->name
);
458 * Prints an overview of all available options
460 void print_option_help(QEMUOptionParameter
*list
)
462 printf("Supported options:\n");
463 while (list
&& list
->name
) {
464 printf("%-16s %s\n", list
->name
,
465 list
->help
? list
->help
: "No description available");
470 /* ------------------------------------------------------------------ */
476 const QemuOptDesc
*desc
;
483 QTAILQ_ENTRY(QemuOpt
) next
;
490 QTAILQ_HEAD(QemuOptHead
, QemuOpt
) head
;
491 QTAILQ_ENTRY(QemuOpts
) next
;
494 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
498 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
499 if (strcmp(opt
->name
, name
) != 0)
506 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
508 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
509 return opt
? opt
->str
: NULL
;
512 int qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, int defval
)
514 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
518 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
519 return opt
->value
.boolean
;
522 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
524 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
528 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
529 return opt
->value
.uint
;
532 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
534 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
538 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
539 return opt
->value
.uint
;
542 static int qemu_opt_parse(QemuOpt
*opt
)
544 if (opt
->desc
== NULL
)
546 switch (opt
->desc
->type
) {
547 case QEMU_OPT_STRING
:
551 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
);
552 case QEMU_OPT_NUMBER
:
553 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
);
555 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
);
561 static void qemu_opt_del(QemuOpt
*opt
)
563 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
564 qemu_free((/* !const */ char*)opt
->name
);
565 qemu_free((/* !const */ char*)opt
->str
);
569 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
572 const QemuOptDesc
*desc
= opts
->list
->desc
;
575 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
576 if (strcmp(desc
[i
].name
, name
) == 0) {
580 if (desc
[i
].name
== NULL
) {
582 /* empty list -> allow any */;
584 qerror_report(QERR_INVALID_PARAMETER
, 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) {
606 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
607 int abort_on_failure
)
612 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
613 rc
= func(opt
->name
, opt
->str
, opaque
);
614 if (abort_on_failure
&& rc
!= 0)
620 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
624 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
628 if (strcmp(opts
->id
, id
) != 0) {
636 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
, int fail_if_exists
)
638 QemuOpts
*opts
= NULL
;
641 opts
= qemu_opts_find(list
, id
);
643 if (fail_if_exists
) {
644 qerror_report(QERR_DUPLICATE_ID
, id
, list
->name
);
651 opts
= qemu_mallocz(sizeof(*opts
));
653 opts
->id
= qemu_strdup(id
);
656 loc_save(&opts
->loc
);
657 QTAILQ_INIT(&opts
->head
);
658 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
662 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
663 const char *name
, const char *value
)
667 opts
= qemu_opts_create(list
, id
, 1);
671 return qemu_opt_set(opts
, name
, value
);
674 const char *qemu_opts_id(QemuOpts
*opts
)
679 void qemu_opts_del(QemuOpts
*opts
)
684 opt
= QTAILQ_FIRST(&opts
->head
);
689 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
694 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
698 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
699 opts
->id
? opts
->id
: "<noid>");
700 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
701 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
703 fprintf(stderr
, "\n");
707 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
709 char option
[128], value
[1024];
710 const char *p
,*pe
,*pc
;
712 for (p
= params
; *p
!= '\0'; p
++) {
715 if (!pe
|| (pc
&& pc
< pe
)) {
716 /* found "foo,more" */
717 if (p
== params
&& firstname
) {
718 /* implicitly named first option */
719 pstrcpy(option
, sizeof(option
), firstname
);
720 p
= get_opt_value(value
, sizeof(value
), p
);
722 /* option without value, probably a flag */
723 p
= get_opt_name(option
, sizeof(option
), p
, ',');
724 if (strncmp(option
, "no", 2) == 0) {
725 memmove(option
, option
+2, strlen(option
+2)+1);
726 pstrcpy(value
, sizeof(value
), "off");
728 pstrcpy(value
, sizeof(value
), "on");
732 /* found "foo=bar,more" */
733 p
= get_opt_name(option
, sizeof(option
), p
, '=');
738 p
= get_opt_value(value
, sizeof(value
), p
);
740 if (strcmp(option
, "id") != 0) {
741 /* store and parse */
742 if (qemu_opt_set(opts
, option
, value
) == -1) {
753 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
756 const char *firstname
;
757 char value
[1024], *id
= NULL
;
761 assert(!permit_abbrev
|| list
->implied_opt_name
);
762 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
764 if (strncmp(params
, "id=", 3) == 0) {
765 get_opt_value(value
, sizeof(value
), params
+3);
767 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
768 get_opt_value(value
, sizeof(value
), p
+4);
771 opts
= qemu_opts_create(list
, id
, 1);
775 if (qemu_opts_do_parse(opts
, params
, firstname
) != 0) {
783 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
789 if (!strcmp(key
, "id")) {
793 switch (qobject_type(obj
)) {
795 value
= qstring_get_str(qobject_to_qstring(obj
));
798 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
799 qint_get_int(qobject_to_qint(obj
)));
800 assert(n
< sizeof(buf
));
804 n
= snprintf(buf
, sizeof(buf
), "%.17g",
805 qfloat_get_double(qobject_to_qfloat(obj
)));
806 assert(n
< sizeof(buf
));
810 pstrcpy(buf
, sizeof(buf
),
811 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
817 qemu_opt_set(opaque
, key
, value
);
821 * Create QemuOpts from a QDict.
822 * Use value of key "id" as ID if it exists and is a QString.
823 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
824 * other types are silently ignored.
826 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
)
830 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1);
834 qdict_iter(qdict
, qemu_opts_from_qdict_1
, opts
);
839 * Convert from QemuOpts to QDict.
840 * The QDict values are of type QString.
841 * TODO We'll want to use types appropriate for opt->desc->type, but
842 * this is enough for now.
844 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
853 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
855 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
856 val
= QOBJECT(qstring_from_str(opt
->str
));
857 qdict_put_obj(qdict
, opt
->name
, val
);
862 /* Validate parsed opts against descriptions where no
863 * descriptions were provided in the QemuOptsList.
865 int qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
)
869 assert(opts
->list
->desc
[0].name
== NULL
);
871 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
874 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
875 if (strcmp(desc
[i
].name
, opt
->name
) == 0) {
879 if (desc
[i
].name
== NULL
) {
880 qerror_report(QERR_INVALID_PARAMETER
, opt
->name
);
884 opt
->desc
= &desc
[i
];
886 if (qemu_opt_parse(opt
) < 0) {
894 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
895 int abort_on_failure
)
902 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
903 loc_restore(&opts
->loc
);
904 rc
|= func(opts
, opaque
);
905 if (abort_on_failure
&& rc
!= 0)