]>
Commit | Line | Data |
---|---|---|
d3f24367 KW |
1 | /* |
2 | * Commandline option parsing functions | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com> | |
6 | * | |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
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 | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <string.h> | |
28 | ||
29 | #include "qemu-common.h" | |
1de7afc9 | 30 | #include "qemu/error-report.h" |
7b1b5d19 PB |
31 | #include "qapi/qmp/types.h" |
32 | #include "qapi/error.h" | |
33 | #include "qapi/qmp/qerror.h" | |
1de7afc9 | 34 | #include "qemu/option_int.h" |
d3f24367 KW |
35 | |
36 | /* | |
37 | * Extracts the name of an option from the parameter string (p points at the | |
38 | * first byte of the option name) | |
39 | * | |
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. | |
43 | * | |
44 | * The return value is the position of the delimiter/zero byte after the option | |
45 | * name in p. | |
46 | */ | |
47 | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) | |
48 | { | |
49 | char *q; | |
50 | ||
51 | q = buf; | |
52 | while (*p != '\0' && *p != delim) { | |
53 | if (q && (q - buf) < buf_size - 1) | |
54 | *q++ = *p; | |
55 | p++; | |
56 | } | |
57 | if (q) | |
58 | *q = '\0'; | |
59 | ||
60 | return p; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Extracts the value of an option from the parameter string p (p points at the | |
65 | * first byte of the option value) | |
66 | * | |
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. | |
70 | */ | |
71 | const char *get_opt_value(char *buf, int buf_size, const char *p) | |
72 | { | |
73 | char *q; | |
74 | ||
75 | q = buf; | |
76 | while (*p != '\0') { | |
77 | if (*p == ',') { | |
78 | if (*(p + 1) != ',') | |
79 | break; | |
80 | p++; | |
81 | } | |
82 | if (q && (q - buf) < buf_size - 1) | |
83 | *q++ = *p; | |
84 | p++; | |
85 | } | |
86 | if (q) | |
87 | *q = '\0'; | |
88 | ||
89 | return p; | |
90 | } | |
91 | ||
62c5802e GH |
92 | int get_next_param_value(char *buf, int buf_size, |
93 | const char *tag, const char **pstr) | |
94 | { | |
95 | const char *p; | |
96 | char option[128]; | |
97 | ||
98 | p = *pstr; | |
99 | for(;;) { | |
100 | p = get_opt_name(option, sizeof(option), p, '='); | |
101 | if (*p != '=') | |
102 | break; | |
103 | p++; | |
104 | if (!strcmp(tag, option)) { | |
105 | *pstr = get_opt_value(buf, buf_size, p); | |
106 | if (**pstr == ',') { | |
107 | (*pstr)++; | |
108 | } | |
109 | return strlen(buf); | |
110 | } else { | |
111 | p = get_opt_value(NULL, 0, p); | |
112 | } | |
113 | if (*p != ',') | |
114 | break; | |
115 | p++; | |
116 | } | |
117 | return 0; | |
118 | } | |
119 | ||
120 | int get_param_value(char *buf, int buf_size, | |
121 | const char *tag, const char *str) | |
122 | { | |
123 | return get_next_param_value(buf, buf_size, tag, &str); | |
124 | } | |
125 | ||
d3f24367 KW |
126 | /* |
127 | * Searches an option list for an option with the given name | |
128 | */ | |
129 | QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, | |
130 | const char *name) | |
131 | { | |
132 | while (list && list->name) { | |
133 | if (!strcmp(list->name, name)) { | |
134 | return list; | |
135 | } | |
136 | list++; | |
137 | } | |
138 | ||
139 | return NULL; | |
140 | } | |
141 | ||
cf62adfa LC |
142 | static void parse_option_bool(const char *name, const char *value, bool *ret, |
143 | Error **errp) | |
67b1355b GH |
144 | { |
145 | if (value != NULL) { | |
146 | if (!strcmp(value, "on")) { | |
147 | *ret = 1; | |
148 | } else if (!strcmp(value, "off")) { | |
149 | *ret = 0; | |
150 | } else { | |
cf62adfa | 151 | error_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'"); |
67b1355b GH |
152 | } |
153 | } else { | |
154 | *ret = 1; | |
155 | } | |
67b1355b GH |
156 | } |
157 | ||
2f39df5b LC |
158 | static void parse_option_number(const char *name, const char *value, |
159 | uint64_t *ret, Error **errp) | |
e27c88fe GH |
160 | { |
161 | char *postfix; | |
162 | uint64_t number; | |
163 | ||
164 | if (value != NULL) { | |
165 | number = strtoull(value, &postfix, 0); | |
166 | if (*postfix != '\0') { | |
2f39df5b LC |
167 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
168 | return; | |
e27c88fe | 169 | } |
21c9f4cd | 170 | *ret = number; |
e27c88fe | 171 | } else { |
2f39df5b | 172 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
e27c88fe | 173 | } |
e27c88fe GH |
174 | } |
175 | ||
e8cd45c7 VL |
176 | void parse_option_size(const char *name, const char *value, |
177 | uint64_t *ret, Error **errp) | |
7695019b GH |
178 | { |
179 | char *postfix; | |
180 | double sizef; | |
181 | ||
182 | if (value != NULL) { | |
183 | sizef = strtod(value, &postfix); | |
184 | switch (*postfix) { | |
185 | case 'T': | |
186 | sizef *= 1024; | |
0b0404bf | 187 | /* fall through */ |
7695019b GH |
188 | case 'G': |
189 | sizef *= 1024; | |
0b0404bf | 190 | /* fall through */ |
7695019b GH |
191 | case 'M': |
192 | sizef *= 1024; | |
0b0404bf | 193 | /* fall through */ |
7695019b GH |
194 | case 'K': |
195 | case 'k': | |
196 | sizef *= 1024; | |
0b0404bf | 197 | /* fall through */ |
7695019b GH |
198 | case 'b': |
199 | case '\0': | |
200 | *ret = (uint64_t) sizef; | |
201 | break; | |
202 | default: | |
ec7b2ccb | 203 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); |
7216ae3d | 204 | #if 0 /* conversion from qerror_report() to error_set() broke this: */ |
c64f27d4 | 205 | error_printf_unless_qmp("You may use k, M, G or T suffixes for " |
7695019b | 206 | "kilobytes, megabytes, gigabytes and terabytes.\n"); |
7216ae3d | 207 | #endif |
ec7b2ccb | 208 | return; |
7695019b GH |
209 | } |
210 | } else { | |
ec7b2ccb | 211 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); |
7695019b | 212 | } |
7695019b GH |
213 | } |
214 | ||
d3f24367 KW |
215 | /* |
216 | * Sets the value of a parameter in a given option list. The parsing of the | |
217 | * value depends on the type of option: | |
218 | * | |
219 | * OPT_FLAG (uses value.n): | |
220 | * If no value is given, the flag is set to 1. | |
221 | * Otherwise the value must be "on" (set to 1) or "off" (set to 0) | |
222 | * | |
223 | * OPT_STRING (uses value.s): | |
224 | * value is strdup()ed and assigned as option value | |
225 | * | |
226 | * OPT_SIZE (uses value.n): | |
227 | * The value is converted to an integer. Suffixes for kilobytes etc. are | |
228 | * allowed (powers of 1024). | |
229 | * | |
230 | * Returns 0 on succes, -1 in error cases | |
231 | */ | |
232 | int set_option_parameter(QEMUOptionParameter *list, const char *name, | |
233 | const char *value) | |
234 | { | |
f02b77c9 | 235 | bool flag; |
cf62adfa | 236 | Error *local_err = NULL; |
67b1355b | 237 | |
d3f24367 KW |
238 | // Find a matching parameter |
239 | list = get_option_parameter(list, name); | |
240 | if (list == NULL) { | |
241 | fprintf(stderr, "Unknown option '%s'\n", name); | |
242 | return -1; | |
243 | } | |
244 | ||
245 | // Process parameter | |
246 | switch (list->type) { | |
247 | case OPT_FLAG: | |
cf62adfa | 248 | parse_option_bool(name, value, &flag, &local_err); |
84d18f06 | 249 | if (!local_err) { |
cf62adfa LC |
250 | list->value.n = flag; |
251 | } | |
d3f24367 KW |
252 | break; |
253 | ||
254 | case OPT_STRING: | |
255 | if (value != NULL) { | |
7267c094 | 256 | list->value.s = g_strdup(value); |
d3f24367 KW |
257 | } else { |
258 | fprintf(stderr, "Option '%s' needs a parameter\n", name); | |
259 | return -1; | |
260 | } | |
261 | break; | |
262 | ||
263 | case OPT_SIZE: | |
ec7b2ccb | 264 | parse_option_size(name, value, &list->value.n, &local_err); |
d3f24367 | 265 | break; |
7695019b | 266 | |
d3f24367 KW |
267 | default: |
268 | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name); | |
269 | return -1; | |
270 | } | |
271 | ||
84d18f06 | 272 | if (local_err) { |
cf62adfa LC |
273 | qerror_report_err(local_err); |
274 | error_free(local_err); | |
275 | return -1; | |
276 | } | |
277 | ||
d4ca092a HR |
278 | list->assigned = true; |
279 | ||
d3f24367 KW |
280 | return 0; |
281 | } | |
282 | ||
283 | /* | |
284 | * Sets the given parameter to an integer instead of a string. | |
285 | * This function cannot be used to set string options. | |
286 | * | |
287 | * Returns 0 on success, -1 in error cases | |
288 | */ | |
289 | int set_option_parameter_int(QEMUOptionParameter *list, const char *name, | |
290 | uint64_t value) | |
291 | { | |
292 | // Find a matching parameter | |
293 | list = get_option_parameter(list, name); | |
294 | if (list == NULL) { | |
295 | fprintf(stderr, "Unknown option '%s'\n", name); | |
296 | return -1; | |
297 | } | |
298 | ||
299 | // Process parameter | |
300 | switch (list->type) { | |
301 | case OPT_FLAG: | |
302 | case OPT_NUMBER: | |
303 | case OPT_SIZE: | |
304 | list->value.n = value; | |
305 | break; | |
306 | ||
307 | default: | |
308 | return -1; | |
309 | } | |
310 | ||
d4ca092a HR |
311 | list->assigned = true; |
312 | ||
d3f24367 KW |
313 | return 0; |
314 | } | |
315 | ||
316 | /* | |
317 | * Frees a option list. If it contains strings, the strings are freed as well. | |
318 | */ | |
319 | void free_option_parameters(QEMUOptionParameter *list) | |
320 | { | |
321 | QEMUOptionParameter *cur = list; | |
322 | ||
323 | while (cur && cur->name) { | |
324 | if (cur->type == OPT_STRING) { | |
7267c094 | 325 | g_free(cur->value.s); |
d3f24367 KW |
326 | } |
327 | cur++; | |
328 | } | |
329 | ||
7267c094 | 330 | g_free(list); |
d3f24367 KW |
331 | } |
332 | ||
b50cbabc MK |
333 | /* |
334 | * Count valid options in list | |
335 | */ | |
336 | static size_t count_option_parameters(QEMUOptionParameter *list) | |
337 | { | |
338 | size_t num_options = 0; | |
339 | ||
340 | while (list && list->name) { | |
341 | num_options++; | |
342 | list++; | |
343 | } | |
344 | ||
345 | return num_options; | |
346 | } | |
347 | ||
348 | /* | |
349 | * Append an option list (list) to an option list (dest). | |
350 | * | |
351 | * If dest is NULL, a new copy of list is created. | |
352 | * | |
353 | * Returns a pointer to the first element of dest (or the newly allocated copy) | |
354 | */ | |
355 | QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, | |
356 | QEMUOptionParameter *list) | |
357 | { | |
358 | size_t num_options, num_dest_options; | |
359 | ||
360 | num_options = count_option_parameters(dest); | |
361 | num_dest_options = num_options; | |
362 | ||
363 | num_options += count_option_parameters(list); | |
364 | ||
7267c094 | 365 | dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter)); |
bd69fe84 | 366 | dest[num_dest_options].name = NULL; |
b50cbabc MK |
367 | |
368 | while (list && list->name) { | |
369 | if (get_option_parameter(dest, list->name) == NULL) { | |
370 | dest[num_dest_options++] = *list; | |
371 | dest[num_dest_options].name = NULL; | |
372 | } | |
373 | list++; | |
374 | } | |
375 | ||
376 | return dest; | |
377 | } | |
378 | ||
d3f24367 KW |
379 | /* |
380 | * Parses a parameter string (param) into an option list (dest). | |
381 | * | |
0e72e753 SH |
382 | * list is the template option list. If dest is NULL, a new copy of list is |
383 | * created. If list is NULL, this function fails. | |
d3f24367 KW |
384 | * |
385 | * A parameter string consists of one or more parameters, separated by commas. | |
386 | * Each parameter consists of its name and possibly of a value. In the latter | |
387 | * case, the value is delimited by an = character. To specify a value which | |
388 | * contains commas, double each comma so it won't be recognized as the end of | |
389 | * the parameter. | |
390 | * | |
391 | * For more details of the parsing see above. | |
392 | * | |
393 | * Returns a pointer to the first element of dest (or the newly allocated copy) | |
394 | * or NULL in error cases | |
395 | */ | |
396 | QEMUOptionParameter *parse_option_parameters(const char *param, | |
397 | QEMUOptionParameter *list, QEMUOptionParameter *dest) | |
398 | { | |
d3f24367 KW |
399 | QEMUOptionParameter *allocated = NULL; |
400 | char name[256]; | |
401 | char value[256]; | |
402 | char *param_delim, *value_delim; | |
403 | char next_delim; | |
d4ca092a | 404 | int i; |
d3f24367 KW |
405 | |
406 | if (list == NULL) { | |
407 | return NULL; | |
408 | } | |
409 | ||
410 | if (dest == NULL) { | |
898c257b | 411 | dest = allocated = append_option_parameters(NULL, list); |
d3f24367 KW |
412 | } |
413 | ||
d4ca092a HR |
414 | for (i = 0; dest[i].name; i++) { |
415 | dest[i].assigned = false; | |
416 | } | |
417 | ||
d3f24367 KW |
418 | while (*param) { |
419 | ||
420 | // Find parameter name and value in the string | |
421 | param_delim = strchr(param, ','); | |
422 | value_delim = strchr(param, '='); | |
423 | ||
424 | if (value_delim && (value_delim < param_delim || !param_delim)) { | |
425 | next_delim = '='; | |
426 | } else { | |
427 | next_delim = ','; | |
428 | value_delim = NULL; | |
429 | } | |
430 | ||
431 | param = get_opt_name(name, sizeof(name), param, next_delim); | |
432 | if (value_delim) { | |
433 | param = get_opt_value(value, sizeof(value), param + 1); | |
434 | } | |
435 | if (*param != '\0') { | |
436 | param++; | |
437 | } | |
438 | ||
439 | // Set the parameter | |
440 | if (set_option_parameter(dest, name, value_delim ? value : NULL)) { | |
441 | goto fail; | |
442 | } | |
443 | } | |
444 | ||
445 | return dest; | |
446 | ||
447 | fail: | |
448 | // Only free the list if it was newly allocated | |
449 | free_option_parameters(allocated); | |
450 | return NULL; | |
451 | } | |
452 | ||
453 | /* | |
454 | * Prints all options of a list that have a value to stdout | |
455 | */ | |
456 | void print_option_parameters(QEMUOptionParameter *list) | |
457 | { | |
458 | while (list && list->name) { | |
459 | switch (list->type) { | |
460 | case OPT_STRING: | |
461 | if (list->value.s != NULL) { | |
462 | printf("%s='%s' ", list->name, list->value.s); | |
463 | } | |
464 | break; | |
465 | case OPT_FLAG: | |
466 | printf("%s=%s ", list->name, list->value.n ? "on" : "off"); | |
467 | break; | |
468 | case OPT_SIZE: | |
469 | case OPT_NUMBER: | |
470 | printf("%s=%" PRId64 " ", list->name, list->value.n); | |
471 | break; | |
472 | default: | |
07f35073 | 473 | printf("%s=(unknown type) ", list->name); |
d3f24367 KW |
474 | break; |
475 | } | |
476 | list++; | |
477 | } | |
478 | } | |
db08adf5 KW |
479 | |
480 | /* | |
481 | * Prints an overview of all available options | |
482 | */ | |
483 | void print_option_help(QEMUOptionParameter *list) | |
484 | { | |
485 | printf("Supported options:\n"); | |
486 | while (list && list->name) { | |
487 | printf("%-16s %s\n", list->name, | |
488 | list->help ? list->help : "No description available"); | |
489 | list++; | |
490 | } | |
491 | } | |
e27c88fe GH |
492 | |
493 | /* ------------------------------------------------------------------ */ | |
494 | ||
e27c88fe GH |
495 | static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
496 | { | |
497 | QemuOpt *opt; | |
498 | ||
dc9ca4ba | 499 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { |
e27c88fe GH |
500 | if (strcmp(opt->name, name) != 0) |
501 | continue; | |
502 | return opt; | |
503 | } | |
504 | return NULL; | |
505 | } | |
506 | ||
507 | const char *qemu_opt_get(QemuOpts *opts, const char *name) | |
508 | { | |
509 | QemuOpt *opt = qemu_opt_find(opts, name); | |
510 | return opt ? opt->str : NULL; | |
511 | } | |
512 | ||
c8057f95 PM |
513 | bool qemu_opt_has_help_opt(QemuOpts *opts) |
514 | { | |
515 | QemuOpt *opt; | |
516 | ||
517 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { | |
518 | if (is_help_option(opt->name)) { | |
519 | return true; | |
520 | } | |
521 | } | |
522 | return false; | |
523 | } | |
524 | ||
f02b77c9 | 525 | bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
e27c88fe GH |
526 | { |
527 | QemuOpt *opt = qemu_opt_find(opts, name); | |
528 | ||
529 | if (opt == NULL) | |
530 | return defval; | |
531 | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); | |
1f5c1775 | 532 | return opt->value.boolean; |
e27c88fe GH |
533 | } |
534 | ||
535 | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) | |
536 | { | |
537 | QemuOpt *opt = qemu_opt_find(opts, name); | |
538 | ||
539 | if (opt == NULL) | |
540 | return defval; | |
541 | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); | |
542 | return opt->value.uint; | |
543 | } | |
544 | ||
545 | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) | |
546 | { | |
547 | QemuOpt *opt = qemu_opt_find(opts, name); | |
548 | ||
549 | if (opt == NULL) | |
550 | return defval; | |
551 | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); | |
552 | return opt->value.uint; | |
553 | } | |
554 | ||
6c519404 | 555 | static void qemu_opt_parse(QemuOpt *opt, Error **errp) |
e27c88fe GH |
556 | { |
557 | if (opt->desc == NULL) | |
6c519404 | 558 | return; |
2f39df5b | 559 | |
e27c88fe GH |
560 | switch (opt->desc->type) { |
561 | case QEMU_OPT_STRING: | |
562 | /* nothing */ | |
6c519404 | 563 | return; |
e27c88fe | 564 | case QEMU_OPT_BOOL: |
6c519404 | 565 | parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp); |
cf62adfa | 566 | break; |
e27c88fe | 567 | case QEMU_OPT_NUMBER: |
6c519404 | 568 | parse_option_number(opt->name, opt->str, &opt->value.uint, errp); |
2f39df5b | 569 | break; |
e27c88fe | 570 | case QEMU_OPT_SIZE: |
6c519404 | 571 | parse_option_size(opt->name, opt->str, &opt->value.uint, errp); |
ec7b2ccb | 572 | break; |
e27c88fe GH |
573 | default: |
574 | abort(); | |
575 | } | |
576 | } | |
577 | ||
578 | static void qemu_opt_del(QemuOpt *opt) | |
579 | { | |
72cf2d4f | 580 | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
7267c094 AL |
581 | g_free((/* !const */ char*)opt->name); |
582 | g_free((/* !const */ char*)opt->str); | |
583 | g_free(opt); | |
e27c88fe GH |
584 | } |
585 | ||
c474ced8 DXW |
586 | static bool opts_accepts_any(const QemuOpts *opts) |
587 | { | |
588 | return opts->list->desc[0].name == NULL; | |
589 | } | |
590 | ||
591 | static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc, | |
592 | const char *name) | |
e27c88fe | 593 | { |
dc9ca4ba | 594 | int i; |
e27c88fe | 595 | |
dc9ca4ba MM |
596 | for (i = 0; desc[i].name != NULL; i++) { |
597 | if (strcmp(desc[i].name, name) == 0) { | |
c474ced8 | 598 | return &desc[i]; |
e27c88fe | 599 | } |
dc9ca4ba | 600 | } |
c474ced8 DXW |
601 | |
602 | return NULL; | |
603 | } | |
604 | ||
0dd6c526 KW |
605 | int qemu_opt_unset(QemuOpts *opts, const char *name) |
606 | { | |
607 | QemuOpt *opt = qemu_opt_find(opts, name); | |
608 | ||
609 | assert(opts_accepts_any(opts)); | |
610 | ||
611 | if (opt == NULL) { | |
612 | return -1; | |
613 | } else { | |
614 | qemu_opt_del(opt); | |
615 | return 0; | |
616 | } | |
617 | } | |
618 | ||
c474ced8 DXW |
619 | static void opt_set(QemuOpts *opts, const char *name, const char *value, |
620 | bool prepend, Error **errp) | |
621 | { | |
622 | QemuOpt *opt; | |
623 | const QemuOptDesc *desc; | |
624 | Error *local_err = NULL; | |
625 | ||
626 | desc = find_desc_by_name(opts->list->desc, name); | |
627 | if (!desc && !opts_accepts_any(opts)) { | |
628 | error_set(errp, QERR_INVALID_PARAMETER, name); | |
629 | return; | |
e27c88fe | 630 | } |
dc9ca4ba | 631 | |
7267c094 AL |
632 | opt = g_malloc0(sizeof(*opt)); |
633 | opt->name = g_strdup(name); | |
dc9ca4ba | 634 | opt->opts = opts; |
4f6dd9af JK |
635 | if (prepend) { |
636 | QTAILQ_INSERT_HEAD(&opts->head, opt, next); | |
637 | } else { | |
638 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
639 | } | |
c474ced8 | 640 | opt->desc = desc; |
c64f50d1 | 641 | opt->str = g_strdup(value); |
6c519404 | 642 | qemu_opt_parse(opt, &local_err); |
84d18f06 | 643 | if (local_err) { |
584d4064 | 644 | error_propagate(errp, local_err); |
e27c88fe | 645 | qemu_opt_del(opt); |
e27c88fe | 646 | } |
e27c88fe GH |
647 | } |
648 | ||
4f6dd9af JK |
649 | int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
650 | { | |
584d4064 LC |
651 | Error *local_err = NULL; |
652 | ||
653 | opt_set(opts, name, value, false, &local_err); | |
84d18f06 | 654 | if (local_err) { |
584d4064 LC |
655 | qerror_report_err(local_err); |
656 | error_free(local_err); | |
657 | return -1; | |
658 | } | |
659 | ||
660 | return 0; | |
4f6dd9af JK |
661 | } |
662 | ||
384f2139 LC |
663 | void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value, |
664 | Error **errp) | |
665 | { | |
666 | opt_set(opts, name, value, false, errp); | |
667 | } | |
668 | ||
f02b77c9 MK |
669 | int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) |
670 | { | |
671 | QemuOpt *opt; | |
672 | const QemuOptDesc *desc = opts->list->desc; | |
f02b77c9 | 673 | |
ad718d01 DXW |
674 | opt = g_malloc0(sizeof(*opt)); |
675 | opt->desc = find_desc_by_name(desc, name); | |
676 | if (!opt->desc && !opts_accepts_any(opts)) { | |
677 | qerror_report(QERR_INVALID_PARAMETER, name); | |
678 | g_free(opt); | |
679 | return -1; | |
f02b77c9 MK |
680 | } |
681 | ||
f02b77c9 MK |
682 | opt->name = g_strdup(name); |
683 | opt->opts = opts; | |
f02b77c9 | 684 | opt->value.boolean = !!val; |
ad718d01 DXW |
685 | opt->str = g_strdup(val ? "on" : "off"); |
686 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
687 | ||
f02b77c9 MK |
688 | return 0; |
689 | } | |
690 | ||
b83c18e2 DXW |
691 | int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val) |
692 | { | |
693 | QemuOpt *opt; | |
694 | const QemuOptDesc *desc = opts->list->desc; | |
695 | ||
696 | opt = g_malloc0(sizeof(*opt)); | |
697 | opt->desc = find_desc_by_name(desc, name); | |
698 | if (!opt->desc && !opts_accepts_any(opts)) { | |
699 | qerror_report(QERR_INVALID_PARAMETER, name); | |
700 | g_free(opt); | |
701 | return -1; | |
702 | } | |
703 | ||
704 | opt->name = g_strdup(name); | |
705 | opt->opts = opts; | |
706 | opt->value.uint = val; | |
707 | opt->str = g_strdup_printf("%" PRId64, val); | |
708 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
48026075 GH |
713 | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
714 | int abort_on_failure) | |
715 | { | |
716 | QemuOpt *opt; | |
717 | int rc = 0; | |
718 | ||
72cf2d4f | 719 | QTAILQ_FOREACH(opt, &opts->head, next) { |
48026075 GH |
720 | rc = func(opt->name, opt->str, opaque); |
721 | if (abort_on_failure && rc != 0) | |
722 | break; | |
723 | } | |
724 | return rc; | |
725 | } | |
726 | ||
e27c88fe GH |
727 | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
728 | { | |
729 | QemuOpts *opts; | |
730 | ||
72cf2d4f | 731 | QTAILQ_FOREACH(opts, &list->head, next) { |
96bc97eb MA |
732 | if (!opts->id && !id) { |
733 | return opts; | |
e27c88fe | 734 | } |
96bc97eb MA |
735 | if (opts->id && id && !strcmp(opts->id, id)) { |
736 | return opts; | |
e27c88fe | 737 | } |
e27c88fe GH |
738 | } |
739 | return NULL; | |
740 | } | |
741 | ||
b560a9ab MA |
742 | static int id_wellformed(const char *id) |
743 | { | |
744 | int i; | |
745 | ||
746 | if (!qemu_isalpha(id[0])) { | |
747 | return 0; | |
748 | } | |
749 | for (i = 1; id[i]; i++) { | |
750 | if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { | |
751 | return 0; | |
752 | } | |
753 | } | |
754 | return 1; | |
755 | } | |
756 | ||
8be7e7e4 LC |
757 | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, |
758 | int fail_if_exists, Error **errp) | |
e27c88fe GH |
759 | { |
760 | QemuOpts *opts = NULL; | |
761 | ||
762 | if (id) { | |
b560a9ab | 763 | if (!id_wellformed(id)) { |
8be7e7e4 | 764 | error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); |
7216ae3d | 765 | #if 0 /* conversion from qerror_report() to error_set() broke this: */ |
b560a9ab | 766 | error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n"); |
7216ae3d | 767 | #endif |
b560a9ab MA |
768 | return NULL; |
769 | } | |
e27c88fe GH |
770 | opts = qemu_opts_find(list, id); |
771 | if (opts != NULL) { | |
da93318a | 772 | if (fail_if_exists && !list->merge_lists) { |
8be7e7e4 | 773 | error_set(errp, QERR_DUPLICATE_ID, id, list->name); |
e27c88fe GH |
774 | return NULL; |
775 | } else { | |
776 | return opts; | |
777 | } | |
778 | } | |
da93318a PM |
779 | } else if (list->merge_lists) { |
780 | opts = qemu_opts_find(list, NULL); | |
781 | if (opts) { | |
782 | return opts; | |
783 | } | |
e27c88fe | 784 | } |
7267c094 | 785 | opts = g_malloc0(sizeof(*opts)); |
c64f50d1 | 786 | opts->id = g_strdup(id); |
e27c88fe | 787 | opts->list = list; |
827b0813 | 788 | loc_save(&opts->loc); |
72cf2d4f BS |
789 | QTAILQ_INIT(&opts->head); |
790 | QTAILQ_INSERT_TAIL(&list->head, opts, next); | |
e27c88fe GH |
791 | return opts; |
792 | } | |
793 | ||
bb67ab02 MA |
794 | void qemu_opts_reset(QemuOptsList *list) |
795 | { | |
796 | QemuOpts *opts, *next_opts; | |
797 | ||
798 | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { | |
799 | qemu_opts_del(opts); | |
800 | } | |
801 | } | |
802 | ||
94ac7268 MA |
803 | void qemu_opts_loc_restore(QemuOpts *opts) |
804 | { | |
805 | loc_restore(&opts->loc); | |
806 | } | |
807 | ||
e27c88fe GH |
808 | int qemu_opts_set(QemuOptsList *list, const char *id, |
809 | const char *name, const char *value) | |
810 | { | |
811 | QemuOpts *opts; | |
8be7e7e4 | 812 | Error *local_err = NULL; |
e27c88fe | 813 | |
8be7e7e4 | 814 | opts = qemu_opts_create(list, id, 1, &local_err); |
84d18f06 | 815 | if (local_err) { |
8be7e7e4 LC |
816 | qerror_report_err(local_err); |
817 | error_free(local_err); | |
e27c88fe GH |
818 | return -1; |
819 | } | |
820 | return qemu_opt_set(opts, name, value); | |
821 | } | |
822 | ||
48026075 GH |
823 | const char *qemu_opts_id(QemuOpts *opts) |
824 | { | |
825 | return opts->id; | |
826 | } | |
827 | ||
326642bc KW |
828 | /* The id string will be g_free()d by qemu_opts_del */ |
829 | void qemu_opts_set_id(QemuOpts *opts, char *id) | |
830 | { | |
831 | opts->id = id; | |
832 | } | |
833 | ||
e27c88fe GH |
834 | void qemu_opts_del(QemuOpts *opts) |
835 | { | |
836 | QemuOpt *opt; | |
837 | ||
838 | for (;;) { | |
72cf2d4f | 839 | opt = QTAILQ_FIRST(&opts->head); |
e27c88fe GH |
840 | if (opt == NULL) |
841 | break; | |
842 | qemu_opt_del(opt); | |
843 | } | |
72cf2d4f | 844 | QTAILQ_REMOVE(&opts->list->head, opts, next); |
7267c094 AL |
845 | g_free(opts->id); |
846 | g_free(opts); | |
e27c88fe GH |
847 | } |
848 | ||
849 | int qemu_opts_print(QemuOpts *opts, void *dummy) | |
850 | { | |
851 | QemuOpt *opt; | |
852 | ||
853 | fprintf(stderr, "%s: %s:", opts->list->name, | |
854 | opts->id ? opts->id : "<noid>"); | |
72cf2d4f | 855 | QTAILQ_FOREACH(opt, &opts->head, next) { |
e27c88fe GH |
856 | fprintf(stderr, " %s=\"%s\"", opt->name, opt->str); |
857 | } | |
858 | fprintf(stderr, "\n"); | |
859 | return 0; | |
860 | } | |
861 | ||
4f6dd9af JK |
862 | static int opts_do_parse(QemuOpts *opts, const char *params, |
863 | const char *firstname, bool prepend) | |
e27c88fe | 864 | { |
d318ff99 | 865 | char option[128], value[1024]; |
e27c88fe | 866 | const char *p,*pe,*pc; |
584d4064 | 867 | Error *local_err = NULL; |
e27c88fe | 868 | |
2cfa571f | 869 | for (p = params; *p != '\0'; p++) { |
e27c88fe GH |
870 | pe = strchr(p, '='); |
871 | pc = strchr(p, ','); | |
872 | if (!pe || (pc && pc < pe)) { | |
873 | /* found "foo,more" */ | |
874 | if (p == params && firstname) { | |
875 | /* implicitly named first option */ | |
876 | pstrcpy(option, sizeof(option), firstname); | |
877 | p = get_opt_value(value, sizeof(value), p); | |
878 | } else { | |
879 | /* option without value, probably a flag */ | |
880 | p = get_opt_name(option, sizeof(option), p, ','); | |
96729cbd | 881 | if (strncmp(option, "no", 2) == 0) { |
e27c88fe GH |
882 | memmove(option, option+2, strlen(option+2)+1); |
883 | pstrcpy(value, sizeof(value), "off"); | |
884 | } else { | |
885 | pstrcpy(value, sizeof(value), "on"); | |
886 | } | |
887 | } | |
888 | } else { | |
889 | /* found "foo=bar,more" */ | |
890 | p = get_opt_name(option, sizeof(option), p, '='); | |
891 | if (*p != '=') { | |
892 | break; | |
893 | } | |
894 | p++; | |
895 | p = get_opt_value(value, sizeof(value), p); | |
896 | } | |
897 | if (strcmp(option, "id") != 0) { | |
898 | /* store and parse */ | |
584d4064 | 899 | opt_set(opts, option, value, prepend, &local_err); |
84d18f06 | 900 | if (local_err) { |
584d4064 LC |
901 | qerror_report_err(local_err); |
902 | error_free(local_err); | |
96729cbd | 903 | return -1; |
e27c88fe GH |
904 | } |
905 | } | |
906 | if (*p != ',') { | |
907 | break; | |
908 | } | |
e27c88fe | 909 | } |
96729cbd GH |
910 | return 0; |
911 | } | |
912 | ||
4f6dd9af JK |
913 | int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
914 | { | |
915 | return opts_do_parse(opts, params, firstname, false); | |
916 | } | |
917 | ||
918 | static QemuOpts *opts_parse(QemuOptsList *list, const char *params, | |
919 | int permit_abbrev, bool defaults) | |
96729cbd | 920 | { |
8212c64f | 921 | const char *firstname; |
d318ff99 | 922 | char value[1024], *id = NULL; |
96729cbd GH |
923 | const char *p; |
924 | QemuOpts *opts; | |
8be7e7e4 | 925 | Error *local_err = NULL; |
96729cbd | 926 | |
8212c64f MA |
927 | assert(!permit_abbrev || list->implied_opt_name); |
928 | firstname = permit_abbrev ? list->implied_opt_name : NULL; | |
929 | ||
96729cbd GH |
930 | if (strncmp(params, "id=", 3) == 0) { |
931 | get_opt_value(value, sizeof(value), params+3); | |
d510c5cf | 932 | id = value; |
96729cbd GH |
933 | } else if ((p = strstr(params, ",id=")) != NULL) { |
934 | get_opt_value(value, sizeof(value), p+4); | |
d510c5cf | 935 | id = value; |
96729cbd | 936 | } |
cb77d192 MA |
937 | |
938 | /* | |
939 | * This code doesn't work for defaults && !list->merge_lists: when | |
940 | * params has no id=, and list has an element with !opts->id, it | |
941 | * appends a new element instead of returning the existing opts. | |
942 | * However, we got no use for this case. Guard against possible | |
943 | * (if unlikely) future misuse: | |
944 | */ | |
945 | assert(!defaults || list->merge_lists); | |
6d4cd408 | 946 | opts = qemu_opts_create(list, id, !defaults, &local_err); |
8be7e7e4 | 947 | if (opts == NULL) { |
84d18f06 | 948 | if (local_err) { |
8be7e7e4 LC |
949 | qerror_report_err(local_err); |
950 | error_free(local_err); | |
951 | } | |
96729cbd | 952 | return NULL; |
8be7e7e4 | 953 | } |
96729cbd | 954 | |
4f6dd9af | 955 | if (opts_do_parse(opts, params, firstname, defaults) != 0) { |
96729cbd GH |
956 | qemu_opts_del(opts); |
957 | return NULL; | |
958 | } | |
959 | ||
e27c88fe GH |
960 | return opts; |
961 | } | |
962 | ||
4f6dd9af JK |
963 | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
964 | int permit_abbrev) | |
965 | { | |
966 | return opts_parse(list, params, permit_abbrev, false); | |
967 | } | |
968 | ||
969 | void qemu_opts_set_defaults(QemuOptsList *list, const char *params, | |
970 | int permit_abbrev) | |
971 | { | |
972 | QemuOpts *opts; | |
973 | ||
974 | opts = opts_parse(list, params, permit_abbrev, true); | |
975 | assert(opts); | |
976 | } | |
977 | ||
4e89978e LC |
978 | typedef struct OptsFromQDictState { |
979 | QemuOpts *opts; | |
980 | Error **errp; | |
981 | } OptsFromQDictState; | |
982 | ||
01e7f188 MA |
983 | static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) |
984 | { | |
4e89978e | 985 | OptsFromQDictState *state = opaque; |
01e7f188 MA |
986 | char buf[32]; |
987 | const char *value; | |
988 | int n; | |
989 | ||
4e89978e | 990 | if (!strcmp(key, "id") || error_is_set(state->errp)) { |
01e7f188 MA |
991 | return; |
992 | } | |
993 | ||
994 | switch (qobject_type(obj)) { | |
995 | case QTYPE_QSTRING: | |
996 | value = qstring_get_str(qobject_to_qstring(obj)); | |
997 | break; | |
998 | case QTYPE_QINT: | |
999 | n = snprintf(buf, sizeof(buf), "%" PRId64, | |
1000 | qint_get_int(qobject_to_qint(obj))); | |
1001 | assert(n < sizeof(buf)); | |
1002 | value = buf; | |
1003 | break; | |
1004 | case QTYPE_QFLOAT: | |
1005 | n = snprintf(buf, sizeof(buf), "%.17g", | |
1006 | qfloat_get_double(qobject_to_qfloat(obj))); | |
1007 | assert(n < sizeof(buf)); | |
1008 | value = buf; | |
1009 | break; | |
1010 | case QTYPE_QBOOL: | |
d35215f8 BS |
1011 | pstrcpy(buf, sizeof(buf), |
1012 | qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off"); | |
01e7f188 MA |
1013 | value = buf; |
1014 | break; | |
1015 | default: | |
1016 | return; | |
1017 | } | |
4e89978e LC |
1018 | |
1019 | qemu_opt_set_err(state->opts, key, value, state->errp); | |
01e7f188 MA |
1020 | } |
1021 | ||
1022 | /* | |
1023 | * Create QemuOpts from a QDict. | |
1024 | * Use value of key "id" as ID if it exists and is a QString. | |
1025 | * Only QStrings, QInts, QFloats and QBools are copied. Entries with | |
1026 | * other types are silently ignored. | |
1027 | */ | |
4e89978e LC |
1028 | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, |
1029 | Error **errp) | |
01e7f188 | 1030 | { |
4e89978e | 1031 | OptsFromQDictState state; |
8be7e7e4 | 1032 | Error *local_err = NULL; |
4e89978e | 1033 | QemuOpts *opts; |
01e7f188 | 1034 | |
8be7e7e4 LC |
1035 | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, |
1036 | &local_err); | |
84d18f06 | 1037 | if (local_err) { |
4e89978e | 1038 | error_propagate(errp, local_err); |
01e7f188 | 1039 | return NULL; |
8be7e7e4 | 1040 | } |
01e7f188 | 1041 | |
8be7e7e4 | 1042 | assert(opts != NULL); |
4e89978e LC |
1043 | |
1044 | state.errp = &local_err; | |
1045 | state.opts = opts; | |
1046 | qdict_iter(qdict, qemu_opts_from_qdict_1, &state); | |
84d18f06 | 1047 | if (local_err) { |
4e89978e LC |
1048 | error_propagate(errp, local_err); |
1049 | qemu_opts_del(opts); | |
1050 | return NULL; | |
1051 | } | |
1052 | ||
01e7f188 MA |
1053 | return opts; |
1054 | } | |
1055 | ||
376609cc KW |
1056 | /* |
1057 | * Adds all QDict entries to the QemuOpts that can be added and removes them | |
1058 | * from the QDict. When this function returns, the QDict contains only those | |
1059 | * entries that couldn't be added to the QemuOpts. | |
1060 | */ | |
1061 | void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp) | |
1062 | { | |
1063 | const QDictEntry *entry, *next; | |
1064 | ||
1065 | entry = qdict_first(qdict); | |
1066 | ||
1067 | while (entry != NULL) { | |
1068 | Error *local_err = NULL; | |
1069 | OptsFromQDictState state = { | |
1070 | .errp = &local_err, | |
1071 | .opts = opts, | |
1072 | }; | |
1073 | ||
1074 | next = qdict_next(qdict, entry); | |
1075 | ||
1076 | if (find_desc_by_name(opts->list->desc, entry->key)) { | |
1077 | qemu_opts_from_qdict_1(entry->key, entry->value, &state); | |
84d18f06 | 1078 | if (local_err) { |
376609cc KW |
1079 | error_propagate(errp, local_err); |
1080 | return; | |
1081 | } else { | |
1082 | qdict_del(qdict, entry->key); | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | entry = next; | |
1087 | } | |
1088 | } | |
1089 | ||
01e7f188 MA |
1090 | /* |
1091 | * Convert from QemuOpts to QDict. | |
1092 | * The QDict values are of type QString. | |
1093 | * TODO We'll want to use types appropriate for opt->desc->type, but | |
1094 | * this is enough for now. | |
1095 | */ | |
1096 | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) | |
1097 | { | |
1098 | QemuOpt *opt; | |
1099 | QObject *val; | |
1100 | ||
1101 | if (!qdict) { | |
1102 | qdict = qdict_new(); | |
1103 | } | |
1104 | if (opts->id) { | |
1105 | qdict_put(qdict, "id", qstring_from_str(opts->id)); | |
1106 | } | |
1107 | QTAILQ_FOREACH(opt, &opts->head, next) { | |
1108 | val = QOBJECT(qstring_from_str(opt->str)); | |
1109 | qdict_put_obj(qdict, opt->name, val); | |
1110 | } | |
1111 | return qdict; | |
1112 | } | |
1113 | ||
5dc519ef MM |
1114 | /* Validate parsed opts against descriptions where no |
1115 | * descriptions were provided in the QemuOptsList. | |
1116 | */ | |
29952866 | 1117 | void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp) |
5dc519ef MM |
1118 | { |
1119 | QemuOpt *opt; | |
6c519404 | 1120 | Error *local_err = NULL; |
5dc519ef | 1121 | |
db97ceba | 1122 | assert(opts_accepts_any(opts)); |
5dc519ef MM |
1123 | |
1124 | QTAILQ_FOREACH(opt, &opts->head, next) { | |
db97ceba DXW |
1125 | opt->desc = find_desc_by_name(desc, opt->name); |
1126 | if (!opt->desc) { | |
29952866 LC |
1127 | error_set(errp, QERR_INVALID_PARAMETER, opt->name); |
1128 | return; | |
5dc519ef MM |
1129 | } |
1130 | ||
6c519404 | 1131 | qemu_opt_parse(opt, &local_err); |
84d18f06 | 1132 | if (local_err) { |
29952866 LC |
1133 | error_propagate(errp, local_err); |
1134 | return; | |
5dc519ef MM |
1135 | } |
1136 | } | |
5dc519ef MM |
1137 | } |
1138 | ||
e27c88fe GH |
1139 | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
1140 | int abort_on_failure) | |
1141 | { | |
827b0813 | 1142 | Location loc; |
e27c88fe GH |
1143 | QemuOpts *opts; |
1144 | int rc = 0; | |
1145 | ||
827b0813 | 1146 | loc_push_none(&loc); |
72cf2d4f | 1147 | QTAILQ_FOREACH(opts, &list->head, next) { |
827b0813 | 1148 | loc_restore(&opts->loc); |
4a2594dd | 1149 | rc |= func(opts, opaque); |
e27c88fe GH |
1150 | if (abort_on_failure && rc != 0) |
1151 | break; | |
1152 | } | |
827b0813 | 1153 | loc_pop(&loc); |
e27c88fe GH |
1154 | return rc; |
1155 | } |