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