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-option.h"
33 * Extracts the name of an option from the parameter string (p points at the
34 * first byte of the option name)
36 * The option name is delimited by delim (usually , or =) or the string end
37 * and is copied into buf. If the option name is longer than buf_size, it is
38 * truncated. buf is always zero terminated.
40 * The return value is the position of the delimiter/zero byte after the option
43 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
48 while (*p
!= '\0' && *p
!= delim
) {
49 if (q
&& (q
- buf
) < buf_size
- 1)
60 * Extracts the value of an option from the parameter string p (p points at the
61 * first byte of the option value)
63 * This function is comparable to get_opt_name with the difference that the
64 * delimiter is fixed to be comma which starts a new option. To specify an
65 * option value that contains commas, double each comma.
67 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
78 if (q
&& (q
- buf
) < buf_size
- 1)
88 int get_next_param_value(char *buf
, int buf_size
,
89 const char *tag
, const char **pstr
)
96 p
= get_opt_name(option
, sizeof(option
), p
, '=');
100 if (!strcmp(tag
, option
)) {
101 *pstr
= get_opt_value(buf
, buf_size
, p
);
107 p
= get_opt_value(NULL
, 0, p
);
116 int get_param_value(char *buf
, int buf_size
,
117 const char *tag
, const char *str
)
119 return get_next_param_value(buf
, buf_size
, tag
, &str
);
122 int check_params(char *buf
, int buf_size
,
123 const char * const *params
, const char *str
)
130 p
= get_opt_name(buf
, buf_size
, p
, '=');
135 for (i
= 0; params
[i
] != NULL
; i
++) {
136 if (!strcmp(params
[i
], buf
)) {
140 if (params
[i
] == NULL
) {
143 p
= get_opt_value(NULL
, 0, p
);
153 * Searches an option list for an option with the given name
155 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
158 while (list
&& list
->name
) {
159 if (!strcmp(list
->name
, name
)) {
168 static int parse_option_bool(const char *name
, const char *value
, int *ret
)
171 if (!strcmp(value
, "on")) {
173 } else if (!strcmp(value
, "off")) {
176 fprintf(stderr
, "Option '%s': Use 'on' or 'off'\n", name
);
185 static int parse_option_number(const char *name
, const char *value
, uint64_t *ret
)
191 number
= strtoull(value
, &postfix
, 0);
192 if (*postfix
!= '\0') {
193 fprintf(stderr
, "Option '%s' needs a number as parameter\n", name
);
198 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
204 static int parse_option_size(const char *name
, const char *value
, uint64_t *ret
)
210 sizef
= strtod(value
, &postfix
);
223 *ret
= (uint64_t) sizef
;
226 fprintf(stderr
, "Option '%s' needs size as parameter\n", name
);
227 fprintf(stderr
, "You may use k, M, G or T suffixes for "
228 "kilobytes, megabytes, gigabytes and terabytes.\n");
232 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
239 * Sets the value of a parameter in a given option list. The parsing of the
240 * value depends on the type of option:
242 * OPT_FLAG (uses value.n):
243 * If no value is given, the flag is set to 1.
244 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
246 * OPT_STRING (uses value.s):
247 * value is strdup()ed and assigned as option value
249 * OPT_SIZE (uses value.n):
250 * The value is converted to an integer. Suffixes for kilobytes etc. are
251 * allowed (powers of 1024).
253 * Returns 0 on succes, -1 in error cases
255 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
260 // Find a matching parameter
261 list
= get_option_parameter(list
, name
);
263 fprintf(stderr
, "Unknown option '%s'\n", name
);
268 switch (list
->type
) {
270 if (parse_option_bool(name
, value
, &flag
) == -1)
272 list
->value
.n
= flag
;
277 list
->value
.s
= qemu_strdup(value
);
279 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
285 if (parse_option_size(name
, value
, &list
->value
.n
) == -1)
290 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
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
,
306 // Find a matching parameter
307 list
= get_option_parameter(list
, name
);
309 fprintf(stderr
, "Unknown option '%s'\n", name
);
314 switch (list
->type
) {
318 list
->value
.n
= value
;
329 * Frees a option list. If it contains strings, the strings are freed as well.
331 void free_option_parameters(QEMUOptionParameter
*list
)
333 QEMUOptionParameter
*cur
= list
;
335 while (cur
&& cur
->name
) {
336 if (cur
->type
== OPT_STRING
) {
337 qemu_free(cur
->value
.s
);
346 * Parses a parameter string (param) into an option list (dest).
348 * list is the templace is. If dest is NULL, a new copy of list is created for
349 * it. If list is NULL, this function fails.
351 * A parameter string consists of one or more parameters, separated by commas.
352 * Each parameter consists of its name and possibly of a value. In the latter
353 * case, the value is delimited by an = character. To specify a value which
354 * contains commas, double each comma so it won't be recognized as the end of
357 * For more details of the parsing see above.
359 * Returns a pointer to the first element of dest (or the newly allocated copy)
360 * or NULL in error cases
362 QEMUOptionParameter
*parse_option_parameters(const char *param
,
363 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
365 QEMUOptionParameter
*cur
;
366 QEMUOptionParameter
*allocated
= NULL
;
369 char *param_delim
, *value_delim
;
378 // Count valid options
386 // Create a copy of the option list to fill in values
387 dest
= qemu_mallocz((num_options
+ 1) * sizeof(QEMUOptionParameter
));
389 memcpy(dest
, list
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
394 // Find parameter name and value in the string
395 param_delim
= strchr(param
, ',');
396 value_delim
= strchr(param
, '=');
398 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
405 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
407 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
409 if (*param
!= '\0') {
414 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
422 // Only free the list if it was newly allocated
423 free_option_parameters(allocated
);
428 * Prints all options of a list that have a value to stdout
430 void print_option_parameters(QEMUOptionParameter
*list
)
432 while (list
&& list
->name
) {
433 switch (list
->type
) {
435 if (list
->value
.s
!= NULL
) {
436 printf("%s='%s' ", list
->name
, list
->value
.s
);
440 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
444 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
447 printf("%s=(unkown type) ", list
->name
);
455 * Prints an overview of all available options
457 void print_option_help(QEMUOptionParameter
*list
)
459 printf("Supported options:\n");
460 while (list
&& list
->name
) {
461 printf("%-16s %s\n", list
->name
,
462 list
->help
? list
->help
: "No description available");
467 /* ------------------------------------------------------------------ */
480 QTAILQ_ENTRY(QemuOpt
) next
;
486 QTAILQ_HEAD(QemuOptHead
, QemuOpt
) head
;
487 QTAILQ_ENTRY(QemuOpts
) next
;
490 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
494 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
495 if (strcmp(opt
->name
, name
) != 0)
502 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
504 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
505 return opt
? opt
->str
: NULL
;
508 int qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, int defval
)
510 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
514 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
515 return opt
->value
.boolean
;
518 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
520 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
524 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
525 return opt
->value
.uint
;
528 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
530 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
534 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
535 return opt
->value
.uint
;
538 static int qemu_opt_parse(QemuOpt
*opt
)
540 if (opt
->desc
== NULL
)
542 switch (opt
->desc
->type
) {
543 case QEMU_OPT_STRING
:
547 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
);
548 case QEMU_OPT_NUMBER
:
549 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
);
551 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
);
557 static void qemu_opt_del(QemuOpt
*opt
)
559 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
560 qemu_free((/* !const */ char*)opt
->name
);
561 qemu_free((/* !const */ char*)opt
->str
);
565 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
568 QemuOptDesc
*desc
= opts
->list
->desc
;
571 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
572 if (strcmp(desc
[i
].name
, name
) == 0) {
576 if (desc
[i
].name
== NULL
) {
578 /* empty list -> allow any */;
580 fprintf(stderr
, "option \"%s\" is not valid for %s\n",
581 name
, opts
->list
->name
);
586 opt
= qemu_mallocz(sizeof(*opt
));
587 opt
->name
= qemu_strdup(name
);
589 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
590 if (desc
[i
].name
!= NULL
) {
594 opt
->str
= qemu_strdup(value
);
596 if (qemu_opt_parse(opt
) < 0) {
597 fprintf(stderr
, "Failed to parse \"%s\" for \"%s.%s\"\n", opt
->str
,
598 opts
->list
->name
, opt
->name
);
605 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
606 int abort_on_failure
)
611 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
612 rc
= func(opt
->name
, opt
->str
, opaque
);
613 if (abort_on_failure
&& rc
!= 0)
619 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
623 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
627 if (strcmp(opts
->id
, id
) != 0) {
635 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
, int fail_if_exists
)
637 QemuOpts
*opts
= NULL
;
640 opts
= qemu_opts_find(list
, id
);
642 if (fail_if_exists
) {
643 fprintf(stderr
, "tried to create id \"%s\" twice for \"%s\"\n",
651 opts
= qemu_mallocz(sizeof(*opts
));
653 opts
->id
= qemu_strdup(id
);
656 QTAILQ_INIT(&opts
->head
);
657 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
661 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
662 const char *name
, const char *value
)
666 opts
= qemu_opts_create(list
, id
, 1);
670 return qemu_opt_set(opts
, name
, value
);
673 const char *qemu_opts_id(QemuOpts
*opts
)
678 void qemu_opts_del(QemuOpts
*opts
)
683 opt
= QTAILQ_FIRST(&opts
->head
);
688 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
693 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
697 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
698 opts
->id
? opts
->id
: "<noid>");
699 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
700 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
702 fprintf(stderr
, "\n");
706 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
708 char option
[128], value
[1024];
709 const char *p
,*pe
,*pc
;
711 for (p
= params
; *p
!= '\0'; p
++) {
714 if (!pe
|| (pc
&& pc
< pe
)) {
715 /* found "foo,more" */
716 if (p
== params
&& firstname
) {
717 /* implicitly named first option */
718 pstrcpy(option
, sizeof(option
), firstname
);
719 p
= get_opt_value(value
, sizeof(value
), p
);
721 /* option without value, probably a flag */
722 p
= get_opt_name(option
, sizeof(option
), p
, ',');
723 if (strncmp(option
, "no", 2) == 0) {
724 memmove(option
, option
+2, strlen(option
+2)+1);
725 pstrcpy(value
, sizeof(value
), "off");
727 pstrcpy(value
, sizeof(value
), "on");
731 /* found "foo=bar,more" */
732 p
= get_opt_name(option
, sizeof(option
), p
, '=');
737 p
= get_opt_value(value
, sizeof(value
), p
);
739 if (strcmp(option
, "id") != 0) {
740 /* store and parse */
741 if (qemu_opt_set(opts
, option
, value
) == -1) {
752 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
, const char *firstname
)
754 char value
[1024], *id
= NULL
;
758 if (strncmp(params
, "id=", 3) == 0) {
759 get_opt_value(value
, sizeof(value
), params
+3);
760 id
= qemu_strdup(value
);
761 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
762 get_opt_value(value
, sizeof(value
), p
+4);
763 id
= qemu_strdup(value
);
765 opts
= qemu_opts_create(list
, id
, 1);
769 if (qemu_opts_do_parse(opts
, params
, firstname
) != 0) {
777 /* Validate parsed opts against descriptions where no
778 * descriptions were provided in the QemuOptsList.
780 int qemu_opts_validate(QemuOpts
*opts
, QemuOptDesc
*desc
)
784 assert(opts
->list
->desc
[0].name
== NULL
);
786 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
789 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
790 if (strcmp(desc
[i
].name
, opt
->name
) == 0) {
794 if (desc
[i
].name
== NULL
) {
795 fprintf(stderr
, "option \"%s\" is not valid for %s\n",
796 opt
->name
, opts
->list
->name
);
800 opt
->desc
= &desc
[i
];
802 if (qemu_opt_parse(opt
) < 0) {
810 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
811 int abort_on_failure
)
816 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
817 rc
= func(opts
, opaque
);
818 if (abort_on_failure
&& rc
!= 0)