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-option.h"
34 * Extracts the name of an option from the parameter string (p points at the
35 * first byte of the option name)
37 * The option name is delimited by delim (usually , or =) or the string end
38 * and is copied into buf. If the option name is longer than buf_size, it is
39 * truncated. buf is always zero terminated.
41 * The return value is the position of the delimiter/zero byte after the option
44 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
49 while (*p
!= '\0' && *p
!= delim
) {
50 if (q
&& (q
- buf
) < buf_size
- 1)
61 * Extracts the value of an option from the parameter string p (p points at the
62 * first byte of the option value)
64 * This function is comparable to get_opt_name with the difference that the
65 * delimiter is fixed to be comma which starts a new option. To specify an
66 * option value that contains commas, double each comma.
68 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
79 if (q
&& (q
- buf
) < buf_size
- 1)
89 int get_next_param_value(char *buf
, int buf_size
,
90 const char *tag
, const char **pstr
)
97 p
= get_opt_name(option
, sizeof(option
), p
, '=');
101 if (!strcmp(tag
, option
)) {
102 *pstr
= get_opt_value(buf
, buf_size
, p
);
108 p
= get_opt_value(NULL
, 0, p
);
117 int get_param_value(char *buf
, int buf_size
,
118 const char *tag
, const char *str
)
120 return get_next_param_value(buf
, buf_size
, tag
, &str
);
123 int check_params(char *buf
, int buf_size
,
124 const char * const *params
, const char *str
)
131 p
= get_opt_name(buf
, buf_size
, p
, '=');
136 for (i
= 0; params
[i
] != NULL
; i
++) {
137 if (!strcmp(params
[i
], buf
)) {
141 if (params
[i
] == NULL
) {
144 p
= get_opt_value(NULL
, 0, p
);
154 * Searches an option list for an option with the given name
156 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
159 while (list
&& list
->name
) {
160 if (!strcmp(list
->name
, name
)) {
169 static int parse_option_bool(const char *name
, const char *value
, int *ret
)
172 if (!strcmp(value
, "on")) {
174 } else if (!strcmp(value
, "off")) {
177 fprintf(stderr
, "Option '%s': Use 'on' or 'off'\n", name
);
186 static int parse_option_number(const char *name
, const char *value
, uint64_t *ret
)
192 number
= strtoull(value
, &postfix
, 0);
193 if (*postfix
!= '\0') {
194 fprintf(stderr
, "Option '%s' needs a number as parameter\n", name
);
199 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
205 static int parse_option_size(const char *name
, const char *value
, uint64_t *ret
)
211 sizef
= strtod(value
, &postfix
);
224 *ret
= (uint64_t) sizef
;
227 fprintf(stderr
, "Option '%s' needs size as parameter\n", name
);
228 fprintf(stderr
, "You may use k, M, G or T suffixes for "
229 "kilobytes, megabytes, gigabytes and terabytes.\n");
233 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
240 * Sets the value of a parameter in a given option list. The parsing of the
241 * value depends on the type of option:
243 * OPT_FLAG (uses value.n):
244 * If no value is given, the flag is set to 1.
245 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
247 * OPT_STRING (uses value.s):
248 * value is strdup()ed and assigned as option value
250 * OPT_SIZE (uses value.n):
251 * The value is converted to an integer. Suffixes for kilobytes etc. are
252 * allowed (powers of 1024).
254 * Returns 0 on succes, -1 in error cases
256 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
261 // Find a matching parameter
262 list
= get_option_parameter(list
, name
);
264 fprintf(stderr
, "Unknown option '%s'\n", name
);
269 switch (list
->type
) {
271 if (parse_option_bool(name
, value
, &flag
) == -1)
273 list
->value
.n
= flag
;
278 list
->value
.s
= qemu_strdup(value
);
280 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
286 if (parse_option_size(name
, value
, &list
->value
.n
) == -1)
291 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
299 * Sets the given parameter to an integer instead of a string.
300 * This function cannot be used to set string options.
302 * Returns 0 on success, -1 in error cases
304 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
307 // Find a matching parameter
308 list
= get_option_parameter(list
, name
);
310 fprintf(stderr
, "Unknown option '%s'\n", name
);
315 switch (list
->type
) {
319 list
->value
.n
= value
;
330 * Frees a option list. If it contains strings, the strings are freed as well.
332 void free_option_parameters(QEMUOptionParameter
*list
)
334 QEMUOptionParameter
*cur
= list
;
336 while (cur
&& cur
->name
) {
337 if (cur
->type
== OPT_STRING
) {
338 qemu_free(cur
->value
.s
);
347 * Parses a parameter string (param) into an option list (dest).
349 * list is the templace is. If dest is NULL, a new copy of list is created for
350 * it. If list is NULL, this function fails.
352 * A parameter string consists of one or more parameters, separated by commas.
353 * Each parameter consists of its name and possibly of a value. In the latter
354 * case, the value is delimited by an = character. To specify a value which
355 * contains commas, double each comma so it won't be recognized as the end of
358 * For more details of the parsing see above.
360 * Returns a pointer to the first element of dest (or the newly allocated copy)
361 * or NULL in error cases
363 QEMUOptionParameter
*parse_option_parameters(const char *param
,
364 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
366 QEMUOptionParameter
*cur
;
367 QEMUOptionParameter
*allocated
= NULL
;
370 char *param_delim
, *value_delim
;
379 // Count valid options
387 // Create a copy of the option list to fill in values
388 dest
= qemu_mallocz((num_options
+ 1) * sizeof(QEMUOptionParameter
));
390 memcpy(dest
, list
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
395 // Find parameter name and value in the string
396 param_delim
= strchr(param
, ',');
397 value_delim
= strchr(param
, '=');
399 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
406 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
408 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
410 if (*param
!= '\0') {
415 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
423 // Only free the list if it was newly allocated
424 free_option_parameters(allocated
);
429 * Prints all options of a list that have a value to stdout
431 void print_option_parameters(QEMUOptionParameter
*list
)
433 while (list
&& list
->name
) {
434 switch (list
->type
) {
436 if (list
->value
.s
!= NULL
) {
437 printf("%s='%s' ", list
->name
, list
->value
.s
);
441 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
445 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
448 printf("%s=(unkown type) ", list
->name
);
456 * Prints an overview of all available options
458 void print_option_help(QEMUOptionParameter
*list
)
460 printf("Supported options:\n");
461 while (list
&& list
->name
) {
462 printf("%-16s %s\n", list
->name
,
463 list
->help
? list
->help
: "No description available");
468 /* ------------------------------------------------------------------ */
474 const QemuOptDesc
*desc
;
481 QTAILQ_ENTRY(QemuOpt
) next
;
488 QTAILQ_HEAD(QemuOptHead
, QemuOpt
) head
;
489 QTAILQ_ENTRY(QemuOpts
) next
;
492 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
496 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
497 if (strcmp(opt
->name
, name
) != 0)
504 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
506 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
507 return opt
? opt
->str
: NULL
;
510 int qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, int defval
)
512 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
516 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
517 return opt
->value
.boolean
;
520 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
522 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
526 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
527 return opt
->value
.uint
;
530 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
532 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
536 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
537 return opt
->value
.uint
;
540 static int qemu_opt_parse(QemuOpt
*opt
)
542 if (opt
->desc
== NULL
)
544 switch (opt
->desc
->type
) {
545 case QEMU_OPT_STRING
:
549 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
);
550 case QEMU_OPT_NUMBER
:
551 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
);
553 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
);
559 static void qemu_opt_del(QemuOpt
*opt
)
561 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
562 qemu_free((/* !const */ char*)opt
->name
);
563 qemu_free((/* !const */ char*)opt
->str
);
567 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
570 const 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
);
588 opt
= qemu_mallocz(sizeof(*opt
));
589 opt
->name
= qemu_strdup(name
);
591 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
592 if (desc
[i
].name
!= NULL
) {
596 opt
->str
= qemu_strdup(value
);
598 if (qemu_opt_parse(opt
) < 0) {
599 fprintf(stderr
, "Failed to parse \"%s\" for \"%s.%s\"\n", opt
->str
,
600 opts
->list
->name
, opt
->name
);
607 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
608 int abort_on_failure
)
613 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
614 rc
= func(opt
->name
, opt
->str
, opaque
);
615 if (abort_on_failure
&& rc
!= 0)
621 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
625 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
629 if (strcmp(opts
->id
, id
) != 0) {
637 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
, int fail_if_exists
)
639 QemuOpts
*opts
= NULL
;
642 opts
= qemu_opts_find(list
, id
);
644 if (fail_if_exists
) {
645 fprintf(stderr
, "tried to create id \"%s\" twice for \"%s\"\n",
653 opts
= qemu_mallocz(sizeof(*opts
));
655 opts
->id
= qemu_strdup(id
);
658 loc_save(&opts
->loc
);
659 QTAILQ_INIT(&opts
->head
);
660 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
664 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
665 const char *name
, const char *value
)
669 opts
= qemu_opts_create(list
, id
, 1);
673 return qemu_opt_set(opts
, name
, value
);
676 const char *qemu_opts_id(QemuOpts
*opts
)
681 void qemu_opts_del(QemuOpts
*opts
)
686 opt
= QTAILQ_FIRST(&opts
->head
);
691 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
696 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
700 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
701 opts
->id
? opts
->id
: "<noid>");
702 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
703 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
705 fprintf(stderr
, "\n");
709 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
711 char option
[128], value
[1024];
712 const char *p
,*pe
,*pc
;
714 for (p
= params
; *p
!= '\0'; p
++) {
717 if (!pe
|| (pc
&& pc
< pe
)) {
718 /* found "foo,more" */
719 if (p
== params
&& firstname
) {
720 /* implicitly named first option */
721 pstrcpy(option
, sizeof(option
), firstname
);
722 p
= get_opt_value(value
, sizeof(value
), p
);
724 /* option without value, probably a flag */
725 p
= get_opt_name(option
, sizeof(option
), p
, ',');
726 if (strncmp(option
, "no", 2) == 0) {
727 memmove(option
, option
+2, strlen(option
+2)+1);
728 pstrcpy(value
, sizeof(value
), "off");
730 pstrcpy(value
, sizeof(value
), "on");
734 /* found "foo=bar,more" */
735 p
= get_opt_name(option
, sizeof(option
), p
, '=');
740 p
= get_opt_value(value
, sizeof(value
), p
);
742 if (strcmp(option
, "id") != 0) {
743 /* store and parse */
744 if (qemu_opt_set(opts
, option
, value
) == -1) {
755 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
, const char *firstname
)
757 char value
[1024], *id
= NULL
;
761 if (strncmp(params
, "id=", 3) == 0) {
762 get_opt_value(value
, sizeof(value
), params
+3);
763 id
= qemu_strdup(value
);
764 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
765 get_opt_value(value
, sizeof(value
), p
+4);
766 id
= qemu_strdup(value
);
768 opts
= qemu_opts_create(list
, id
, 1);
772 if (qemu_opts_do_parse(opts
, params
, firstname
) != 0) {
780 /* Validate parsed opts against descriptions where no
781 * descriptions were provided in the QemuOptsList.
783 int qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
)
787 assert(opts
->list
->desc
[0].name
== NULL
);
789 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
792 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
793 if (strcmp(desc
[i
].name
, opt
->name
) == 0) {
797 if (desc
[i
].name
== NULL
) {
798 fprintf(stderr
, "option \"%s\" is not valid for %s\n",
799 opt
->name
, opts
->list
->name
);
803 opt
->desc
= &desc
[i
];
805 if (qemu_opt_parse(opt
) < 0) {
813 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
814 int abort_on_failure
)
821 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
822 loc_restore(&opts
->loc
);
823 rc
|= func(opts
, opaque
);
824 if (abort_on_failure
&& rc
!= 0)