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"
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
40 * The option name is delimited by delim (usually , or =) or the string end
41 * and is copied into buf. If the option name is longer than buf_size, it is
42 * truncated. buf is always zero terminated.
44 * The return value is the position of the delimiter/zero byte after the option
47 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
52 while (*p
!= '\0' && *p
!= delim
) {
53 if (q
&& (q
- buf
) < buf_size
- 1)
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
67 * This function is comparable to get_opt_name with the difference that the
68 * delimiter is fixed to be comma which starts a new option. To specify an
69 * option value that contains commas, double each comma.
71 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
82 if (q
&& (q
- buf
) < buf_size
- 1)
92 int get_next_param_value(char *buf
, int buf_size
,
93 const char *tag
, const char **pstr
)
100 p
= get_opt_name(option
, sizeof(option
), p
, '=');
104 if (!strcmp(tag
, option
)) {
105 *pstr
= get_opt_value(buf
, buf_size
, p
);
111 p
= get_opt_value(NULL
, 0, p
);
120 int get_param_value(char *buf
, int buf_size
,
121 const char *tag
, const char *str
)
123 return get_next_param_value(buf
, buf_size
, tag
, &str
);
126 int check_params(char *buf
, int buf_size
,
127 const char * const *params
, const char *str
)
134 p
= get_opt_name(buf
, buf_size
, p
, '=');
139 for (i
= 0; params
[i
] != NULL
; i
++) {
140 if (!strcmp(params
[i
], buf
)) {
144 if (params
[i
] == NULL
) {
147 p
= get_opt_value(NULL
, 0, p
);
157 * Searches an option list for an option with the given name
159 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
162 while (list
&& list
->name
) {
163 if (!strcmp(list
->name
, name
)) {
172 static int parse_option_bool(const char *name
, const char *value
, bool *ret
)
175 if (!strcmp(value
, "on")) {
177 } else if (!strcmp(value
, "off")) {
180 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "'on' or 'off'");
189 static int parse_option_number(const char *name
, const char *value
, uint64_t *ret
)
195 number
= strtoull(value
, &postfix
, 0);
196 if (*postfix
!= '\0') {
197 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
202 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
208 static int parse_option_size(const char *name
, const char *value
, uint64_t *ret
)
214 sizef
= strtod(value
, &postfix
);
231 *ret
= (uint64_t) sizef
;
234 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
235 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
236 "kilobytes, megabytes, gigabytes and terabytes.\n");
240 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
247 * Sets the value of a parameter in a given option list. The parsing of the
248 * value depends on the type of option:
250 * OPT_FLAG (uses value.n):
251 * If no value is given, the flag is set to 1.
252 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
254 * OPT_STRING (uses value.s):
255 * value is strdup()ed and assigned as option value
257 * OPT_SIZE (uses value.n):
258 * The value is converted to an integer. Suffixes for kilobytes etc. are
259 * allowed (powers of 1024).
261 * Returns 0 on succes, -1 in error cases
263 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
268 // Find a matching parameter
269 list
= get_option_parameter(list
, name
);
271 fprintf(stderr
, "Unknown option '%s'\n", name
);
276 switch (list
->type
) {
278 if (parse_option_bool(name
, value
, &flag
) == -1)
280 list
->value
.n
= flag
;
285 list
->value
.s
= g_strdup(value
);
287 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
293 if (parse_option_size(name
, value
, &list
->value
.n
) == -1)
298 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
306 * Sets the given parameter to an integer instead of a string.
307 * This function cannot be used to set string options.
309 * Returns 0 on success, -1 in error cases
311 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
314 // Find a matching parameter
315 list
= get_option_parameter(list
, name
);
317 fprintf(stderr
, "Unknown option '%s'\n", name
);
322 switch (list
->type
) {
326 list
->value
.n
= value
;
337 * Frees a option list. If it contains strings, the strings are freed as well.
339 void free_option_parameters(QEMUOptionParameter
*list
)
341 QEMUOptionParameter
*cur
= list
;
343 while (cur
&& cur
->name
) {
344 if (cur
->type
== OPT_STRING
) {
345 g_free(cur
->value
.s
);
354 * Count valid options in list
356 static size_t count_option_parameters(QEMUOptionParameter
*list
)
358 size_t num_options
= 0;
360 while (list
&& list
->name
) {
369 * Append an option list (list) to an option list (dest).
371 * If dest is NULL, a new copy of list is created.
373 * Returns a pointer to the first element of dest (or the newly allocated copy)
375 QEMUOptionParameter
*append_option_parameters(QEMUOptionParameter
*dest
,
376 QEMUOptionParameter
*list
)
378 size_t num_options
, num_dest_options
;
380 num_options
= count_option_parameters(dest
);
381 num_dest_options
= num_options
;
383 num_options
+= count_option_parameters(list
);
385 dest
= g_realloc(dest
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
386 dest
[num_dest_options
].name
= NULL
;
388 while (list
&& list
->name
) {
389 if (get_option_parameter(dest
, list
->name
) == NULL
) {
390 dest
[num_dest_options
++] = *list
;
391 dest
[num_dest_options
].name
= NULL
;
400 * Parses a parameter string (param) into an option list (dest).
402 * list is the template option list. If dest is NULL, a new copy of list is
403 * created. If list is NULL, this function fails.
405 * A parameter string consists of one or more parameters, separated by commas.
406 * Each parameter consists of its name and possibly of a value. In the latter
407 * case, the value is delimited by an = character. To specify a value which
408 * contains commas, double each comma so it won't be recognized as the end of
411 * For more details of the parsing see above.
413 * Returns a pointer to the first element of dest (or the newly allocated copy)
414 * or NULL in error cases
416 QEMUOptionParameter
*parse_option_parameters(const char *param
,
417 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
419 QEMUOptionParameter
*allocated
= NULL
;
422 char *param_delim
, *value_delim
;
430 dest
= allocated
= append_option_parameters(NULL
, list
);
435 // Find parameter name and value in the string
436 param_delim
= strchr(param
, ',');
437 value_delim
= strchr(param
, '=');
439 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
446 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
448 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
450 if (*param
!= '\0') {
455 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
463 // Only free the list if it was newly allocated
464 free_option_parameters(allocated
);
469 * Prints all options of a list that have a value to stdout
471 void print_option_parameters(QEMUOptionParameter
*list
)
473 while (list
&& list
->name
) {
474 switch (list
->type
) {
476 if (list
->value
.s
!= NULL
) {
477 printf("%s='%s' ", list
->name
, list
->value
.s
);
481 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
485 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
488 printf("%s=(unknown type) ", list
->name
);
496 * Prints an overview of all available options
498 void print_option_help(QEMUOptionParameter
*list
)
500 printf("Supported options:\n");
501 while (list
&& list
->name
) {
502 printf("%-16s %s\n", list
->name
,
503 list
->help
? list
->help
: "No description available");
508 /* ------------------------------------------------------------------ */
514 const QemuOptDesc
*desc
;
521 QTAILQ_ENTRY(QemuOpt
) next
;
528 QTAILQ_HEAD(QemuOptHead
, QemuOpt
) head
;
529 QTAILQ_ENTRY(QemuOpts
) next
;
532 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
536 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
537 if (strcmp(opt
->name
, name
) != 0)
544 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
546 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
547 return opt
? opt
->str
: NULL
;
550 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
552 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
556 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
557 return opt
->value
.boolean
;
560 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
562 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
566 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
567 return opt
->value
.uint
;
570 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
572 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
576 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
577 return opt
->value
.uint
;
580 static int qemu_opt_parse(QemuOpt
*opt
)
582 if (opt
->desc
== NULL
)
584 switch (opt
->desc
->type
) {
585 case QEMU_OPT_STRING
:
589 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
);
590 case QEMU_OPT_NUMBER
:
591 return parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
);
593 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
);
599 static void qemu_opt_del(QemuOpt
*opt
)
601 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
602 g_free((/* !const */ char*)opt
->name
);
603 g_free((/* !const */ char*)opt
->str
);
607 static int opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
611 const QemuOptDesc
*desc
= opts
->list
->desc
;
614 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
615 if (strcmp(desc
[i
].name
, name
) == 0) {
619 if (desc
[i
].name
== NULL
) {
621 /* empty list -> allow any */;
623 qerror_report(QERR_INVALID_PARAMETER
, name
);
628 opt
= g_malloc0(sizeof(*opt
));
629 opt
->name
= g_strdup(name
);
632 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
634 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
636 if (desc
[i
].name
!= NULL
) {
640 opt
->str
= g_strdup(value
);
642 if (qemu_opt_parse(opt
) < 0) {
649 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
651 return opt_set(opts
, name
, value
, false);
654 int qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
)
657 const QemuOptDesc
*desc
= opts
->list
->desc
;
660 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
661 if (strcmp(desc
[i
].name
, name
) == 0) {
665 if (desc
[i
].name
== NULL
) {
667 /* empty list -> allow any */;
669 qerror_report(QERR_INVALID_PARAMETER
, name
);
674 opt
= g_malloc0(sizeof(*opt
));
675 opt
->name
= g_strdup(name
);
677 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
678 if (desc
[i
].name
!= NULL
) {
681 opt
->value
.boolean
= !!val
;
685 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
686 int abort_on_failure
)
691 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
692 rc
= func(opt
->name
, opt
->str
, opaque
);
693 if (abort_on_failure
&& rc
!= 0)
699 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
703 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
710 if (strcmp(opts
->id
, id
) != 0) {
718 static int id_wellformed(const char *id
)
722 if (!qemu_isalpha(id
[0])) {
725 for (i
= 1; id
[i
]; i
++) {
726 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
733 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
734 int fail_if_exists
, Error
**errp
)
736 QemuOpts
*opts
= NULL
;
739 if (!id_wellformed(id
)) {
740 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
741 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
744 opts
= qemu_opts_find(list
, id
);
746 if (fail_if_exists
&& !list
->merge_lists
) {
747 error_set(errp
, QERR_DUPLICATE_ID
, id
, list
->name
);
753 } else if (list
->merge_lists
) {
754 opts
= qemu_opts_find(list
, NULL
);
759 opts
= g_malloc0(sizeof(*opts
));
761 opts
->id
= g_strdup(id
);
764 loc_save(&opts
->loc
);
765 QTAILQ_INIT(&opts
->head
);
766 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
770 void qemu_opts_reset(QemuOptsList
*list
)
772 QemuOpts
*opts
, *next_opts
;
774 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
779 void qemu_opts_loc_restore(QemuOpts
*opts
)
781 loc_restore(&opts
->loc
);
784 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
785 const char *name
, const char *value
)
788 Error
*local_err
= NULL
;
790 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
791 if (error_is_set(&local_err
)) {
792 qerror_report_err(local_err
);
793 error_free(local_err
);
796 return qemu_opt_set(opts
, name
, value
);
799 const char *qemu_opts_id(QemuOpts
*opts
)
804 void qemu_opts_del(QemuOpts
*opts
)
809 opt
= QTAILQ_FIRST(&opts
->head
);
814 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
819 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
823 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
824 opts
->id
? opts
->id
: "<noid>");
825 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
826 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
828 fprintf(stderr
, "\n");
832 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
833 const char *firstname
, bool prepend
)
835 char option
[128], value
[1024];
836 const char *p
,*pe
,*pc
;
838 for (p
= params
; *p
!= '\0'; p
++) {
841 if (!pe
|| (pc
&& pc
< pe
)) {
842 /* found "foo,more" */
843 if (p
== params
&& firstname
) {
844 /* implicitly named first option */
845 pstrcpy(option
, sizeof(option
), firstname
);
846 p
= get_opt_value(value
, sizeof(value
), p
);
848 /* option without value, probably a flag */
849 p
= get_opt_name(option
, sizeof(option
), p
, ',');
850 if (strncmp(option
, "no", 2) == 0) {
851 memmove(option
, option
+2, strlen(option
+2)+1);
852 pstrcpy(value
, sizeof(value
), "off");
854 pstrcpy(value
, sizeof(value
), "on");
858 /* found "foo=bar,more" */
859 p
= get_opt_name(option
, sizeof(option
), p
, '=');
864 p
= get_opt_value(value
, sizeof(value
), p
);
866 if (strcmp(option
, "id") != 0) {
867 /* store and parse */
868 if (opt_set(opts
, option
, value
, prepend
) == -1) {
879 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
881 return opts_do_parse(opts
, params
, firstname
, false);
884 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
885 int permit_abbrev
, bool defaults
)
887 const char *firstname
;
888 char value
[1024], *id
= NULL
;
891 Error
*local_err
= NULL
;
893 assert(!permit_abbrev
|| list
->implied_opt_name
);
894 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
896 if (strncmp(params
, "id=", 3) == 0) {
897 get_opt_value(value
, sizeof(value
), params
+3);
899 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
900 get_opt_value(value
, sizeof(value
), p
+4);
904 if (!id
&& !QTAILQ_EMPTY(&list
->head
)) {
905 opts
= qemu_opts_find(list
, NULL
);
907 opts
= qemu_opts_create(list
, id
, 0, &local_err
);
910 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
913 if (error_is_set(&local_err
)) {
914 qerror_report_err(local_err
);
915 error_free(local_err
);
920 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
928 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
931 return opts_parse(list
, params
, permit_abbrev
, false);
934 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
939 opts
= opts_parse(list
, params
, permit_abbrev
, true);
943 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
949 if (!strcmp(key
, "id")) {
953 switch (qobject_type(obj
)) {
955 value
= qstring_get_str(qobject_to_qstring(obj
));
958 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
959 qint_get_int(qobject_to_qint(obj
)));
960 assert(n
< sizeof(buf
));
964 n
= snprintf(buf
, sizeof(buf
), "%.17g",
965 qfloat_get_double(qobject_to_qfloat(obj
)));
966 assert(n
< sizeof(buf
));
970 pstrcpy(buf
, sizeof(buf
),
971 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
977 qemu_opt_set(opaque
, key
, value
);
981 * Create QemuOpts from a QDict.
982 * Use value of key "id" as ID if it exists and is a QString.
983 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
984 * other types are silently ignored.
986 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
)
989 Error
*local_err
= NULL
;
991 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
993 if (error_is_set(&local_err
)) {
994 qerror_report_err(local_err
);
995 error_free(local_err
);
999 assert(opts
!= NULL
);
1000 qdict_iter(qdict
, qemu_opts_from_qdict_1
, opts
);
1005 * Convert from QemuOpts to QDict.
1006 * The QDict values are of type QString.
1007 * TODO We'll want to use types appropriate for opt->desc->type, but
1008 * this is enough for now.
1010 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1016 qdict
= qdict_new();
1019 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1021 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1022 val
= QOBJECT(qstring_from_str(opt
->str
));
1023 qdict_put_obj(qdict
, opt
->name
, val
);
1028 /* Validate parsed opts against descriptions where no
1029 * descriptions were provided in the QemuOptsList.
1031 int qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
)
1035 assert(opts
->list
->desc
[0].name
== NULL
);
1037 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1040 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
1041 if (strcmp(desc
[i
].name
, opt
->name
) == 0) {
1045 if (desc
[i
].name
== NULL
) {
1046 qerror_report(QERR_INVALID_PARAMETER
, opt
->name
);
1050 opt
->desc
= &desc
[i
];
1052 if (qemu_opt_parse(opt
) < 0) {
1060 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1061 int abort_on_failure
)
1067 loc_push_none(&loc
);
1068 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1069 loc_restore(&opts
->loc
);
1070 rc
|= func(opts
, opaque
);
1071 if (abort_on_failure
&& rc
!= 0)