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 void parse_option_number(const char *name
, const char *value
,
190 uint64_t *ret
, Error
**errp
)
196 number
= strtoull(value
, &postfix
, 0);
197 if (*postfix
!= '\0') {
198 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
203 error_set(errp
, 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
);
230 *ret
= (uint64_t) sizef
;
233 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
234 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
235 "kilobytes, megabytes, gigabytes and terabytes.\n");
239 qerror_report(QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
246 * Sets the value of a parameter in a given option list. The parsing of the
247 * value depends on the type of option:
249 * OPT_FLAG (uses value.n):
250 * If no value is given, the flag is set to 1.
251 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
253 * OPT_STRING (uses value.s):
254 * value is strdup()ed and assigned as option value
256 * OPT_SIZE (uses value.n):
257 * The value is converted to an integer. Suffixes for kilobytes etc. are
258 * allowed (powers of 1024).
260 * Returns 0 on succes, -1 in error cases
262 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
267 // Find a matching parameter
268 list
= get_option_parameter(list
, name
);
270 fprintf(stderr
, "Unknown option '%s'\n", name
);
275 switch (list
->type
) {
277 if (parse_option_bool(name
, value
, &flag
) == -1)
279 list
->value
.n
= flag
;
284 list
->value
.s
= g_strdup(value
);
286 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
292 if (parse_option_size(name
, value
, &list
->value
.n
) == -1)
297 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
305 * Sets the given parameter to an integer instead of a string.
306 * This function cannot be used to set string options.
308 * Returns 0 on success, -1 in error cases
310 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
313 // Find a matching parameter
314 list
= get_option_parameter(list
, name
);
316 fprintf(stderr
, "Unknown option '%s'\n", name
);
321 switch (list
->type
) {
325 list
->value
.n
= value
;
336 * Frees a option list. If it contains strings, the strings are freed as well.
338 void free_option_parameters(QEMUOptionParameter
*list
)
340 QEMUOptionParameter
*cur
= list
;
342 while (cur
&& cur
->name
) {
343 if (cur
->type
== OPT_STRING
) {
344 g_free(cur
->value
.s
);
353 * Count valid options in list
355 static size_t count_option_parameters(QEMUOptionParameter
*list
)
357 size_t num_options
= 0;
359 while (list
&& list
->name
) {
368 * Append an option list (list) to an option list (dest).
370 * If dest is NULL, a new copy of list is created.
372 * Returns a pointer to the first element of dest (or the newly allocated copy)
374 QEMUOptionParameter
*append_option_parameters(QEMUOptionParameter
*dest
,
375 QEMUOptionParameter
*list
)
377 size_t num_options
, num_dest_options
;
379 num_options
= count_option_parameters(dest
);
380 num_dest_options
= num_options
;
382 num_options
+= count_option_parameters(list
);
384 dest
= g_realloc(dest
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
385 dest
[num_dest_options
].name
= NULL
;
387 while (list
&& list
->name
) {
388 if (get_option_parameter(dest
, list
->name
) == NULL
) {
389 dest
[num_dest_options
++] = *list
;
390 dest
[num_dest_options
].name
= NULL
;
399 * Parses a parameter string (param) into an option list (dest).
401 * list is the template option list. If dest is NULL, a new copy of list is
402 * created. If list is NULL, this function fails.
404 * A parameter string consists of one or more parameters, separated by commas.
405 * Each parameter consists of its name and possibly of a value. In the latter
406 * case, the value is delimited by an = character. To specify a value which
407 * contains commas, double each comma so it won't be recognized as the end of
410 * For more details of the parsing see above.
412 * Returns a pointer to the first element of dest (or the newly allocated copy)
413 * or NULL in error cases
415 QEMUOptionParameter
*parse_option_parameters(const char *param
,
416 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
418 QEMUOptionParameter
*allocated
= NULL
;
421 char *param_delim
, *value_delim
;
429 dest
= allocated
= append_option_parameters(NULL
, list
);
434 // Find parameter name and value in the string
435 param_delim
= strchr(param
, ',');
436 value_delim
= strchr(param
, '=');
438 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
445 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
447 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
449 if (*param
!= '\0') {
454 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
462 // Only free the list if it was newly allocated
463 free_option_parameters(allocated
);
468 * Prints all options of a list that have a value to stdout
470 void print_option_parameters(QEMUOptionParameter
*list
)
472 while (list
&& list
->name
) {
473 switch (list
->type
) {
475 if (list
->value
.s
!= NULL
) {
476 printf("%s='%s' ", list
->name
, list
->value
.s
);
480 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
484 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
487 printf("%s=(unknown type) ", list
->name
);
495 * Prints an overview of all available options
497 void print_option_help(QEMUOptionParameter
*list
)
499 printf("Supported options:\n");
500 while (list
&& list
->name
) {
501 printf("%-16s %s\n", list
->name
,
502 list
->help
? list
->help
: "No description available");
507 /* ------------------------------------------------------------------ */
513 const QemuOptDesc
*desc
;
520 QTAILQ_ENTRY(QemuOpt
) next
;
527 QTAILQ_HEAD(QemuOptHead
, QemuOpt
) head
;
528 QTAILQ_ENTRY(QemuOpts
) next
;
531 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
535 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
536 if (strcmp(opt
->name
, name
) != 0)
543 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
545 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
546 return opt
? opt
->str
: NULL
;
549 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
551 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
555 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
556 return opt
->value
.boolean
;
559 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
561 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
565 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
566 return opt
->value
.uint
;
569 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
571 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
575 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
576 return opt
->value
.uint
;
579 static int qemu_opt_parse(QemuOpt
*opt
)
581 Error
*local_err
= NULL
;
583 if (opt
->desc
== NULL
)
586 switch (opt
->desc
->type
) {
587 case QEMU_OPT_STRING
:
591 return parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
);
592 case QEMU_OPT_NUMBER
:
593 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
,
597 return parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
);
602 if (error_is_set(&local_err
)) {
603 qerror_report_err(local_err
);
604 error_free(local_err
);
611 static void qemu_opt_del(QemuOpt
*opt
)
613 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
614 g_free((/* !const */ char*)opt
->name
);
615 g_free((/* !const */ char*)opt
->str
);
619 static int opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
623 const QemuOptDesc
*desc
= opts
->list
->desc
;
626 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
627 if (strcmp(desc
[i
].name
, name
) == 0) {
631 if (desc
[i
].name
== NULL
) {
633 /* empty list -> allow any */;
635 qerror_report(QERR_INVALID_PARAMETER
, name
);
640 opt
= g_malloc0(sizeof(*opt
));
641 opt
->name
= g_strdup(name
);
644 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
646 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
648 if (desc
[i
].name
!= NULL
) {
652 opt
->str
= g_strdup(value
);
654 if (qemu_opt_parse(opt
) < 0) {
661 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
663 return opt_set(opts
, name
, value
, false);
666 int qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
)
669 const QemuOptDesc
*desc
= opts
->list
->desc
;
672 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
673 if (strcmp(desc
[i
].name
, name
) == 0) {
677 if (desc
[i
].name
== NULL
) {
679 /* empty list -> allow any */;
681 qerror_report(QERR_INVALID_PARAMETER
, name
);
686 opt
= g_malloc0(sizeof(*opt
));
687 opt
->name
= g_strdup(name
);
689 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
690 if (desc
[i
].name
!= NULL
) {
693 opt
->value
.boolean
= !!val
;
697 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
698 int abort_on_failure
)
703 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
704 rc
= func(opt
->name
, opt
->str
, opaque
);
705 if (abort_on_failure
&& rc
!= 0)
711 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
715 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
722 if (strcmp(opts
->id
, id
) != 0) {
730 static int id_wellformed(const char *id
)
734 if (!qemu_isalpha(id
[0])) {
737 for (i
= 1; id
[i
]; i
++) {
738 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
745 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
746 int fail_if_exists
, Error
**errp
)
748 QemuOpts
*opts
= NULL
;
751 if (!id_wellformed(id
)) {
752 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
753 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
756 opts
= qemu_opts_find(list
, id
);
758 if (fail_if_exists
&& !list
->merge_lists
) {
759 error_set(errp
, QERR_DUPLICATE_ID
, id
, list
->name
);
765 } else if (list
->merge_lists
) {
766 opts
= qemu_opts_find(list
, NULL
);
771 opts
= g_malloc0(sizeof(*opts
));
773 opts
->id
= g_strdup(id
);
776 loc_save(&opts
->loc
);
777 QTAILQ_INIT(&opts
->head
);
778 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
782 void qemu_opts_reset(QemuOptsList
*list
)
784 QemuOpts
*opts
, *next_opts
;
786 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
791 void qemu_opts_loc_restore(QemuOpts
*opts
)
793 loc_restore(&opts
->loc
);
796 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
797 const char *name
, const char *value
)
800 Error
*local_err
= NULL
;
802 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
803 if (error_is_set(&local_err
)) {
804 qerror_report_err(local_err
);
805 error_free(local_err
);
808 return qemu_opt_set(opts
, name
, value
);
811 const char *qemu_opts_id(QemuOpts
*opts
)
816 void qemu_opts_del(QemuOpts
*opts
)
821 opt
= QTAILQ_FIRST(&opts
->head
);
826 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
831 int qemu_opts_print(QemuOpts
*opts
, void *dummy
)
835 fprintf(stderr
, "%s: %s:", opts
->list
->name
,
836 opts
->id
? opts
->id
: "<noid>");
837 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
838 fprintf(stderr
, " %s=\"%s\"", opt
->name
, opt
->str
);
840 fprintf(stderr
, "\n");
844 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
845 const char *firstname
, bool prepend
)
847 char option
[128], value
[1024];
848 const char *p
,*pe
,*pc
;
850 for (p
= params
; *p
!= '\0'; p
++) {
853 if (!pe
|| (pc
&& pc
< pe
)) {
854 /* found "foo,more" */
855 if (p
== params
&& firstname
) {
856 /* implicitly named first option */
857 pstrcpy(option
, sizeof(option
), firstname
);
858 p
= get_opt_value(value
, sizeof(value
), p
);
860 /* option without value, probably a flag */
861 p
= get_opt_name(option
, sizeof(option
), p
, ',');
862 if (strncmp(option
, "no", 2) == 0) {
863 memmove(option
, option
+2, strlen(option
+2)+1);
864 pstrcpy(value
, sizeof(value
), "off");
866 pstrcpy(value
, sizeof(value
), "on");
870 /* found "foo=bar,more" */
871 p
= get_opt_name(option
, sizeof(option
), p
, '=');
876 p
= get_opt_value(value
, sizeof(value
), p
);
878 if (strcmp(option
, "id") != 0) {
879 /* store and parse */
880 if (opt_set(opts
, option
, value
, prepend
) == -1) {
891 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
893 return opts_do_parse(opts
, params
, firstname
, false);
896 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
897 int permit_abbrev
, bool defaults
)
899 const char *firstname
;
900 char value
[1024], *id
= NULL
;
903 Error
*local_err
= NULL
;
905 assert(!permit_abbrev
|| list
->implied_opt_name
);
906 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
908 if (strncmp(params
, "id=", 3) == 0) {
909 get_opt_value(value
, sizeof(value
), params
+3);
911 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
912 get_opt_value(value
, sizeof(value
), p
+4);
916 if (!id
&& !QTAILQ_EMPTY(&list
->head
)) {
917 opts
= qemu_opts_find(list
, NULL
);
919 opts
= qemu_opts_create(list
, id
, 0, &local_err
);
922 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
925 if (error_is_set(&local_err
)) {
926 qerror_report_err(local_err
);
927 error_free(local_err
);
932 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
940 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
943 return opts_parse(list
, params
, permit_abbrev
, false);
946 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
951 opts
= opts_parse(list
, params
, permit_abbrev
, true);
955 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
961 if (!strcmp(key
, "id")) {
965 switch (qobject_type(obj
)) {
967 value
= qstring_get_str(qobject_to_qstring(obj
));
970 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
971 qint_get_int(qobject_to_qint(obj
)));
972 assert(n
< sizeof(buf
));
976 n
= snprintf(buf
, sizeof(buf
), "%.17g",
977 qfloat_get_double(qobject_to_qfloat(obj
)));
978 assert(n
< sizeof(buf
));
982 pstrcpy(buf
, sizeof(buf
),
983 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
989 qemu_opt_set(opaque
, key
, value
);
993 * Create QemuOpts from a QDict.
994 * Use value of key "id" as ID if it exists and is a QString.
995 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
996 * other types are silently ignored.
998 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
)
1001 Error
*local_err
= NULL
;
1003 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1005 if (error_is_set(&local_err
)) {
1006 qerror_report_err(local_err
);
1007 error_free(local_err
);
1011 assert(opts
!= NULL
);
1012 qdict_iter(qdict
, qemu_opts_from_qdict_1
, opts
);
1017 * Convert from QemuOpts to QDict.
1018 * The QDict values are of type QString.
1019 * TODO We'll want to use types appropriate for opt->desc->type, but
1020 * this is enough for now.
1022 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1028 qdict
= qdict_new();
1031 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1033 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1034 val
= QOBJECT(qstring_from_str(opt
->str
));
1035 qdict_put_obj(qdict
, opt
->name
, val
);
1040 /* Validate parsed opts against descriptions where no
1041 * descriptions were provided in the QemuOptsList.
1043 int qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
)
1047 assert(opts
->list
->desc
[0].name
== NULL
);
1049 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1052 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
1053 if (strcmp(desc
[i
].name
, opt
->name
) == 0) {
1057 if (desc
[i
].name
== NULL
) {
1058 qerror_report(QERR_INVALID_PARAMETER
, opt
->name
);
1062 opt
->desc
= &desc
[i
];
1064 if (qemu_opt_parse(opt
) < 0) {
1072 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1073 int abort_on_failure
)
1079 loc_push_none(&loc
);
1080 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1081 loc_restore(&opts
->loc
);
1082 rc
|= func(opts
, opaque
);
1083 if (abort_on_failure
&& rc
!= 0)