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
);
186 * Sets the value of a parameter in a given option list. The parsing of the
187 * value depends on the type of option:
189 * OPT_FLAG (uses value.n):
190 * If no value is given, the flag is set to 1.
191 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
193 * OPT_STRING (uses value.s):
194 * value is strdup()ed and assigned as option value
196 * OPT_SIZE (uses value.n):
197 * The value is converted to an integer. Suffixes for kilobytes etc. are
198 * allowed (powers of 1024).
200 * Returns 0 on succes, -1 in error cases
202 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
207 // Find a matching parameter
208 list
= get_option_parameter(list
, name
);
210 fprintf(stderr
, "Unknown option '%s'\n", name
);
215 switch (list
->type
) {
217 if (-1 == parse_option_bool(name
, value
, &flag
))
219 list
->value
.n
= flag
;
224 list
->value
.s
= strdup(value
);
226 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
233 double sizef
= strtod(value
, (char**) &value
);
247 list
->value
.n
= (uint64_t) sizef
;
250 fprintf(stderr
, "Option '%s' needs size as parameter\n", name
);
251 fprintf(stderr
, "You may use k, M, G or T suffixes for "
252 "kilobytes, megabytes, gigabytes and terabytes.\n");
256 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
261 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
269 * Sets the given parameter to an integer instead of a string.
270 * This function cannot be used to set string options.
272 * Returns 0 on success, -1 in error cases
274 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
277 // Find a matching parameter
278 list
= get_option_parameter(list
, name
);
280 fprintf(stderr
, "Unknown option '%s'\n", name
);
285 switch (list
->type
) {
289 list
->value
.n
= value
;
300 * Frees a option list. If it contains strings, the strings are freed as well.
302 void free_option_parameters(QEMUOptionParameter
*list
)
304 QEMUOptionParameter
*cur
= list
;
306 while (cur
&& cur
->name
) {
307 if (cur
->type
== OPT_STRING
) {
317 * Parses a parameter string (param) into an option list (dest).
319 * list is the templace is. If dest is NULL, a new copy of list is created for
320 * it. If list is NULL, this function fails.
322 * A parameter string consists of one or more parameters, separated by commas.
323 * Each parameter consists of its name and possibly of a value. In the latter
324 * case, the value is delimited by an = character. To specify a value which
325 * contains commas, double each comma so it won't be recognized as the end of
328 * For more details of the parsing see above.
330 * Returns a pointer to the first element of dest (or the newly allocated copy)
331 * or NULL in error cases
333 QEMUOptionParameter
*parse_option_parameters(const char *param
,
334 QEMUOptionParameter
*list
, QEMUOptionParameter
*dest
)
336 QEMUOptionParameter
*cur
;
337 QEMUOptionParameter
*allocated
= NULL
;
340 char *param_delim
, *value_delim
;
349 // Count valid options
357 // Create a copy of the option list to fill in values
358 dest
= qemu_mallocz((num_options
+ 1) * sizeof(QEMUOptionParameter
));
360 memcpy(dest
, list
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
365 // Find parameter name and value in the string
366 param_delim
= strchr(param
, ',');
367 value_delim
= strchr(param
, '=');
369 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
376 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
378 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
380 if (*param
!= '\0') {
385 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
393 // Only free the list if it was newly allocated
394 free_option_parameters(allocated
);
399 * Prints all options of a list that have a value to stdout
401 void print_option_parameters(QEMUOptionParameter
*list
)
403 while (list
&& list
->name
) {
404 switch (list
->type
) {
406 if (list
->value
.s
!= NULL
) {
407 printf("%s='%s' ", list
->name
, list
->value
.s
);
411 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
415 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
418 printf("%s=(unkown type) ", list
->name
);
426 * Prints an overview of all available options
428 void print_option_help(QEMUOptionParameter
*list
)
430 printf("Supported options:\n");
431 while (list
&& list
->name
) {
432 printf("%-16s %s\n", list
->name
,
433 list
->help
? list
->help
: "No description available");