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
);
197 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
203 static int parse_option_size(const char *name
, const char *value
, uint64_t *ret
)
209 sizef
= strtod(value
, &postfix
);
222 *ret
= (uint64_t) sizef
;
225 fprintf(stderr
, "Option '%s' needs size as parameter\n", name
);
226 fprintf(stderr
, "You may use k, M, G or T suffixes for "
227 "kilobytes, megabytes, gigabytes and terabytes.\n");
231 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
238 * Sets the value of a parameter in a given option list. The parsing of the
239 * value depends on the type of option:
241 * OPT_FLAG (uses value.n):
242 * If no value is given, the flag is set to 1.
243 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
245 * OPT_STRING (uses value.s):
246 * value is strdup()ed and assigned as option value
248 * OPT_SIZE (uses value.n):
249 * The value is converted to an integer. Suffixes for kilobytes etc. are
250 * allowed (powers of 1024).
252 * Returns 0 on succes, -1 in error cases
254 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
259 // Find a matching parameter
260 list
= get_option_parameter(list
, name
);
262 fprintf(stderr
, "Unknown option '%s'\n", name
);
267 switch (list
->type
) {
269 if (-1 == parse_option_bool(name
, value
, &flag
))
271 list
->value
.n
= flag
;
276 list
->value
.s
= strdup(value
);
278 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
284 if (-1 == parse_option_size(name
, value
, &list
->value
.n
))
289 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
297 * Sets the given parameter to an integer instead of a string.
298 * This function cannot be used to set string options.
300 * Returns 0 on success, -1 in error cases
302 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
305 // Find a matching parameter
306 list
= get_option_parameter(list
, name
);
308 fprintf(stderr
, "Unknown option '%s'\n", name
);
313 switch (list
->type
) {
317 list
->value
.n
= value
;
328 * Frees a option list. If it contains strings, the strings are freed as well.
330 void free_option_parameters(QEMUOptionParameter
*list
)
332 QEMUOptionParameter
*cur
= list
;
334 while (cur
&& cur
->name
) {
335 if (cur
->type
== OPT_STRING
) {
345 * Parses a parameter string (param) into an option list (dest).
347 * list is the templace is. If dest is NULL, a new copy of list is created for
348 * it. If list is NULL, this function fails.
350 * A parameter string consists of one or more parameters, separated by commas.
351 * Each parameter consists of its name and possibly of a value. In the latter
352 * case, the value is delimited by an = character. To specify a value which
353 * contains commas, double each comma so it won't be recognized as the end of
356 * For more details of the parsing see above.
358 * Returns a pointer to the first element of dest (or the newly allocated copy)
359 * or NULL in error cases
361 QEMUOptionParameter
*parse_option_parameters(const char *param
,
362 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
364 QEMUOptionParameter
*cur
;
365 QEMUOptionParameter
*allocated
= NULL
;
368 char *param_delim
, *value_delim
;
377 // Count valid options
385 // Create a copy of the option list to fill in values
386 dest
= qemu_mallocz((num_options
+ 1) * sizeof(QEMUOptionParameter
));
388 memcpy(dest
, list
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
393 // Find parameter name and value in the string
394 param_delim
= strchr(param
, ',');
395 value_delim
= strchr(param
, '=');
397 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
404 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
406 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
408 if (*param
!= '\0') {
413 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
421 // Only free the list if it was newly allocated
422 free_option_parameters(allocated
);
427 * Prints all options of a list that have a value to stdout
429 void print_option_parameters(QEMUOptionParameter
*list
)
431 while (list
&& list
->name
) {
432 switch (list
->type
) {
434 if (list
->value
.s
!= NULL
) {
435 printf("%s='%s' ", list
->name
, list
->value
.s
);
439 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
443 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
446 printf("%s=(unkown type) ", list
->name
);
454 * Prints an overview of all available options
456 void print_option_help(QEMUOptionParameter
*list
)
458 printf("Supported options:\n");
459 while (list
&& list
->name
) {
460 printf("%-16s %s\n", list
->name
,
461 list
->help
? list
->help
: "No description available");
466 /* ------------------------------------------------------------------ */
479 TAILQ_ENTRY(QemuOpt
) next
;
485 TAILQ_HEAD(, QemuOpt
) head
;
486 TAILQ_ENTRY(QemuOpts
) next
;
489 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
493 TAILQ_FOREACH(opt
, &opts
->head
, next
) {
494 if (strcmp(opt
->name
, name
) != 0)
501 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
503 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
504 return opt
? opt
->str
: NULL
;
507 int qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, int defval
)
509 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
513 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
514 return opt
->value
.bool;
517 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
519 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
523 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
524 return opt
->value
.uint
;
527 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
529 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
533 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
534 return opt
->value
.uint
;
537 static int qemu_opt_parse(QemuOpt
*opt
)
539 if (opt
->desc
== NULL
)
541 switch (opt
->desc
->type
) {
542 case QEMU_OPT_STRING
:
546 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.bool);
547 case QEMU_OPT_NUMBER
:
548 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
);
550 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
);
556 static void qemu_opt_del(QemuOpt
*opt
)
558 TAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
559 qemu_free((/* !const */ char*)opt
->name
);
560 qemu_free((/* !const */ char*)opt
->str
);
564 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
568 opt
= qemu_opt_find(opts
, name
);
570 QemuOptDesc
*desc
= opts
->list
->desc
;
573 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
574 if (strcmp(desc
[i
].name
, name
) == 0) {
578 if (desc
[i
].name
== NULL
) {
580 /* empty list -> allow any */;
582 fprintf(stderr
, "option \"%s\" is not valid for %s\n",
583 name
, opts
->list
->name
);
587 opt
= qemu_mallocz(sizeof(*opt
));
588 opt
->name
= qemu_strdup(name
);
590 TAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
591 if (desc
[i
].name
!= NULL
) {
595 qemu_free((/* !const */ char*)opt
->str
);
598 opt
->str
= qemu_strdup(value
);
600 if (qemu_opt_parse(opt
) < 0) {
601 fprintf(stderr
, "Failed to parse \"%s\" for \"%s.%s\"\n", opt
->str
,
602 opts
->list
->name
, opt
->name
);
609 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
613 TAILQ_FOREACH(opts
, &list
->head
, next
) {
617 if (strcmp(opts
->id
, id
) != 0) {
625 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
, int fail_if_exists
)
627 QemuOpts
*opts
= NULL
;
630 opts
= qemu_opts_find(list
, id
);
632 if (fail_if_exists
) {
633 fprintf(stderr
, "tried to create id \"%s\" twice for \"%s\"\n",
641 opts
= qemu_mallocz(sizeof(*opts
));
643 opts
->id
= qemu_strdup(id
);
646 TAILQ_INIT(&opts
->head
);
647 TAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
651 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
652 const char *name
, const char *value
)
656 opts
= qemu_opts_create(list
, id
, 1);
658 fprintf(stderr
, "id \"%s\" not found for \"%s\"\n",
662 return qemu_opt_set(opts
, name
, value
);
665 void qemu_opts_del(QemuOpts
*opts
)
670 opt
= TAILQ_FIRST(&opts
->head
);
675 TAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
679 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
683 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
684 opts
->id
? opts
->id
: "<noid>");
685 TAILQ_FOREACH(opt
, &opts
->head
, next
) {
686 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
688 fprintf(stderr
, "\n");
692 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
, const char *firstname
)
694 char option
[128], value
[128], *id
= NULL
;
696 const char *p
,*pe
,*pc
;
698 if (get_param_value(value
, sizeof(value
), "id", params
))
699 id
= qemu_strdup(value
);
700 opts
= qemu_opts_create(list
, id
, 1);
708 if (!pe
|| (pc
&& pc
< pe
)) {
709 /* found "foo,more" */
710 if (p
== params
&& firstname
) {
711 /* implicitly named first option */
712 pstrcpy(option
, sizeof(option
), firstname
);
713 p
= get_opt_value(value
, sizeof(value
), p
);
715 /* option without value, probably a flag */
716 p
= get_opt_name(option
, sizeof(option
), p
, ',');
717 if (strncmp(p
, "no", 2) == 0) {
718 memmove(option
, option
+2, strlen(option
+2)+1);
719 pstrcpy(value
, sizeof(value
), "off");
721 pstrcpy(value
, sizeof(value
), "on");
725 /* found "foo=bar,more" */
726 p
= get_opt_name(option
, sizeof(option
), p
, '=');
731 p
= get_opt_value(value
, sizeof(value
), p
);
733 if (strcmp(option
, "id") != 0) {
734 /* store and parse */
735 if (-1 == qemu_opt_set(opts
, option
, value
)) {
748 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
749 int abort_on_failure
)
754 TAILQ_FOREACH(opts
, &list
->head
, next
) {
755 rc
= func(opts
, opaque
);
756 if (abort_on_failure
&& rc
!= 0)