]>
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 | ||
aafd7584 | 26 | #include "qemu/osdep.h" |
d3f24367 | 27 | |
da34e65c | 28 | #include "qapi/error.h" |
1de7afc9 | 29 | #include "qemu/error-report.h" |
6b673957 MA |
30 | #include "qapi/qmp/qbool.h" |
31 | #include "qapi/qmp/qdict.h" | |
15280c36 | 32 | #include "qapi/qmp/qnum.h" |
6b673957 | 33 | #include "qapi/qmp/qstring.h" |
7b1b5d19 | 34 | #include "qapi/qmp/qerror.h" |
1de7afc9 | 35 | #include "qemu/option_int.h" |
f348b6d1 VB |
36 | #include "qemu/cutils.h" |
37 | #include "qemu/id.h" | |
38 | #include "qemu/help_option.h" | |
d3f24367 KW |
39 | |
40 | /* | |
41 | * Extracts the name of an option from the parameter string (p points at the | |
42 | * first byte of the option name) | |
43 | * | |
44 | * The option name is delimited by delim (usually , or =) or the string end | |
e652714f DB |
45 | * and is copied into option. The caller is responsible for free'ing option |
46 | * when no longer required. | |
d3f24367 KW |
47 | * |
48 | * The return value is the position of the delimiter/zero byte after the option | |
49 | * name in p. | |
50 | */ | |
e652714f | 51 | static const char *get_opt_name(const char *p, char **option, char delim) |
d3f24367 | 52 | { |
e652714f | 53 | char *offset = strchr(p, delim); |
d3f24367 | 54 | |
e652714f DB |
55 | if (offset) { |
56 | *option = g_strndup(p, offset - p); | |
57 | return offset; | |
58 | } else { | |
59 | *option = g_strdup(p); | |
60 | return p + strlen(p); | |
d3f24367 | 61 | } |
d3f24367 KW |
62 | } |
63 | ||
64 | /* | |
65 | * Extracts the value of an option from the parameter string p (p points at the | |
66 | * first byte of the option value) | |
67 | * | |
68 | * This function is comparable to get_opt_name with the difference that the | |
69 | * delimiter is fixed to be comma which starts a new option. To specify an | |
70 | * option value that contains commas, double each comma. | |
71 | */ | |
950c4e6c | 72 | const char *get_opt_value(const char *p, char **value) |
d3f24367 | 73 | { |
950c4e6c DB |
74 | size_t capacity = 0, length; |
75 | const char *offset; | |
d3f24367 | 76 | |
0c2f6e7e | 77 | *value = NULL; |
950c4e6c | 78 | while (1) { |
5c99fa37 | 79 | offset = qemu_strchrnul(p, ','); |
950c4e6c DB |
80 | length = offset - p; |
81 | if (*offset != '\0' && *(offset + 1) == ',') { | |
82 | length++; | |
83 | } | |
0c2f6e7e DB |
84 | *value = g_renew(char, *value, capacity + length + 1); |
85 | strncpy(*value + capacity, p, length); | |
86 | (*value)[capacity + length] = '\0'; | |
950c4e6c DB |
87 | capacity += length; |
88 | if (*offset == '\0' || | |
89 | *(offset + 1) != ',') { | |
90 | break; | |
91 | } | |
92 | ||
93 | p += (offset - p) + 2; | |
d3f24367 | 94 | } |
d3f24367 | 95 | |
950c4e6c | 96 | return offset; |
d3f24367 KW |
97 | } |
98 | ||
c75d7f71 | 99 | static bool parse_option_bool(const char *name, const char *value, bool *ret, |
cf62adfa | 100 | Error **errp) |
67b1355b | 101 | { |
8ee8409e | 102 | if (!strcmp(value, "on")) { |
67b1355b | 103 | *ret = 1; |
8ee8409e MA |
104 | } else if (!strcmp(value, "off")) { |
105 | *ret = 0; | |
106 | } else { | |
107 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, | |
108 | name, "'on' or 'off'"); | |
c75d7f71 | 109 | return false; |
67b1355b | 110 | } |
c75d7f71 | 111 | return true; |
67b1355b GH |
112 | } |
113 | ||
c75d7f71 | 114 | static bool parse_option_number(const char *name, const char *value, |
2f39df5b | 115 | uint64_t *ret, Error **errp) |
e27c88fe | 116 | { |
e27c88fe | 117 | uint64_t number; |
3403e5eb | 118 | int err; |
e27c88fe | 119 | |
3403e5eb MA |
120 | err = qemu_strtou64(value, NULL, 0, &number); |
121 | if (err == -ERANGE) { | |
122 | error_setg(errp, "Value '%s' is too large for parameter '%s'", | |
123 | value, name); | |
c75d7f71 | 124 | return false; |
3403e5eb MA |
125 | } |
126 | if (err) { | |
c6bd8c70 | 127 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
c75d7f71 | 128 | return false; |
e27c88fe | 129 | } |
8ee8409e | 130 | *ret = number; |
c75d7f71 | 131 | return true; |
e27c88fe GH |
132 | } |
133 | ||
5e89db76 CL |
134 | static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc, |
135 | const char *name) | |
136 | { | |
137 | int i; | |
138 | ||
139 | for (i = 0; desc[i].name != NULL; i++) { | |
140 | if (strcmp(desc[i].name, name) == 0) { | |
141 | return &desc[i]; | |
142 | } | |
143 | } | |
144 | ||
145 | return NULL; | |
146 | } | |
147 | ||
f23db652 MA |
148 | static const char *find_default_by_name(QemuOpts *opts, const char *name) |
149 | { | |
150 | const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); | |
151 | ||
152 | return desc ? desc->def_value_str : NULL; | |
153 | } | |
154 | ||
c75d7f71 | 155 | bool parse_option_size(const char *name, const char *value, |
e8cd45c7 | 156 | uint64_t *ret, Error **errp) |
7695019b | 157 | { |
75cdcd15 MA |
158 | uint64_t size; |
159 | int err; | |
7695019b | 160 | |
75cdcd15 MA |
161 | err = qemu_strtosz(value, NULL, &size); |
162 | if (err == -ERANGE) { | |
9e19ad4e | 163 | error_setg(errp, "Value '%s' is out of range for parameter '%s'", |
75cdcd15 | 164 | value, name); |
c75d7f71 | 165 | return false; |
8ee8409e | 166 | } |
75cdcd15 MA |
167 | if (err) { |
168 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, | |
169 | "a non-negative number below 2^64"); | |
170 | error_append_hint(errp, "Optional suffix k, M, G, T, P or E means" | |
171 | " kilo-, mega-, giga-, tera-, peta-\n" | |
172 | "and exabytes, respectively.\n"); | |
c75d7f71 | 173 | return false; |
7695019b | 174 | } |
75cdcd15 | 175 | *ret = size; |
c75d7f71 | 176 | return true; |
7695019b GH |
177 | } |
178 | ||
9cbef9d6 MAL |
179 | static const char *opt_type_to_string(enum QemuOptType type) |
180 | { | |
181 | switch (type) { | |
182 | case QEMU_OPT_STRING: | |
183 | return "str"; | |
184 | case QEMU_OPT_BOOL: | |
185 | return "bool (on/off)"; | |
186 | case QEMU_OPT_NUMBER: | |
187 | return "num"; | |
188 | case QEMU_OPT_SIZE: | |
189 | return "size"; | |
190 | } | |
191 | ||
192 | g_assert_not_reached(); | |
193 | } | |
194 | ||
63898712 HR |
195 | /** |
196 | * Print the list of options available in the given list. If | |
197 | * @print_caption is true, a caption (including the list name, if it | |
198 | * exists) is printed. The options itself will be indented, so | |
199 | * @print_caption should only be set to false if the caller prints its | |
200 | * own custom caption (so that the indentation makes sense). | |
201 | */ | |
202 | void qemu_opts_print_help(QemuOptsList *list, bool print_caption) | |
504189a9 CL |
203 | { |
204 | QemuOptDesc *desc; | |
9cbef9d6 MAL |
205 | int i; |
206 | GPtrArray *array = g_ptr_array_new(); | |
504189a9 CL |
207 | |
208 | assert(list); | |
209 | desc = list->desc; | |
504189a9 | 210 | while (desc && desc->name) { |
9cbef9d6 | 211 | GString *str = g_string_new(NULL); |
63898712 | 212 | g_string_append_printf(str, " %s=<%s>", desc->name, |
9cbef9d6 MAL |
213 | opt_type_to_string(desc->type)); |
214 | if (desc->help) { | |
63898712 HR |
215 | if (str->len < 24) { |
216 | g_string_append_printf(str, "%*s", 24 - (int)str->len, ""); | |
217 | } | |
9cbef9d6 MAL |
218 | g_string_append_printf(str, " - %s", desc->help); |
219 | } | |
220 | g_ptr_array_add(array, g_string_free(str, false)); | |
504189a9 CL |
221 | desc++; |
222 | } | |
9cbef9d6 MAL |
223 | |
224 | g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0); | |
63898712 HR |
225 | if (print_caption && array->len > 0) { |
226 | if (list->name) { | |
227 | printf("%s options:\n", list->name); | |
228 | } else { | |
229 | printf("Options:\n"); | |
230 | } | |
231 | } else if (array->len == 0) { | |
232 | if (list->name) { | |
233 | printf("There are no options for %s.\n", list->name); | |
234 | } else { | |
235 | printf("No options available.\n"); | |
236 | } | |
237 | } | |
9cbef9d6 MAL |
238 | for (i = 0; i < array->len; i++) { |
239 | printf("%s\n", (char *)array->pdata[i]); | |
240 | } | |
241 | g_ptr_array_set_free_func(array, g_free); | |
242 | g_ptr_array_free(array, true); | |
243 | ||
504189a9 | 244 | } |
e27c88fe GH |
245 | /* ------------------------------------------------------------------ */ |
246 | ||
74c3c197 | 247 | QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
e27c88fe GH |
248 | { |
249 | QemuOpt *opt; | |
250 | ||
eae3eb3e | 251 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) { |
e27c88fe GH |
252 | if (strcmp(opt->name, name) != 0) |
253 | continue; | |
254 | return opt; | |
255 | } | |
256 | return NULL; | |
257 | } | |
258 | ||
fc345512 CL |
259 | static void qemu_opt_del(QemuOpt *opt) |
260 | { | |
261 | QTAILQ_REMOVE(&opt->opts->head, opt, next); | |
262 | g_free(opt->name); | |
263 | g_free(opt->str); | |
264 | g_free(opt); | |
265 | } | |
266 | ||
782730b0 CL |
267 | /* qemu_opt_set allows many settings for the same option. |
268 | * This function deletes all settings for an option. | |
269 | */ | |
270 | static void qemu_opt_del_all(QemuOpts *opts, const char *name) | |
271 | { | |
272 | QemuOpt *opt, *next_opt; | |
273 | ||
274 | QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) { | |
275 | if (!strcmp(opt->name, name)) { | |
276 | qemu_opt_del(opt); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
e27c88fe GH |
281 | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
282 | { | |
435db4cf CL |
283 | QemuOpt *opt; |
284 | ||
285 | if (opts == NULL) { | |
286 | return NULL; | |
287 | } | |
09722032 | 288 | |
435db4cf | 289 | opt = qemu_opt_find(opts, name); |
09722032 | 290 | if (!opt) { |
44b0b7d1 | 291 | return find_default_by_name(opts, name); |
09722032 | 292 | } |
44b0b7d1 MA |
293 | |
294 | return opt->str; | |
e27c88fe GH |
295 | } |
296 | ||
e998e209 DB |
297 | void qemu_opt_iter_init(QemuOptsIter *iter, QemuOpts *opts, const char *name) |
298 | { | |
299 | iter->opts = opts; | |
300 | iter->opt = QTAILQ_FIRST(&opts->head); | |
301 | iter->name = name; | |
302 | } | |
303 | ||
304 | const char *qemu_opt_iter_next(QemuOptsIter *iter) | |
305 | { | |
306 | QemuOpt *ret = iter->opt; | |
307 | if (iter->name) { | |
308 | while (ret && !g_str_equal(iter->name, ret->name)) { | |
309 | ret = QTAILQ_NEXT(ret, next); | |
310 | } | |
311 | } | |
312 | iter->opt = ret ? QTAILQ_NEXT(ret, next) : NULL; | |
313 | return ret ? ret->str : NULL; | |
314 | } | |
315 | ||
782730b0 CL |
316 | /* Get a known option (or its default) and remove it from the list |
317 | * all in one action. Return a malloced string of the option value. | |
318 | * Result must be freed by caller with g_free(). | |
319 | */ | |
320 | char *qemu_opt_get_del(QemuOpts *opts, const char *name) | |
321 | { | |
322 | QemuOpt *opt; | |
44b0b7d1 | 323 | char *str; |
782730b0 CL |
324 | |
325 | if (opts == NULL) { | |
326 | return NULL; | |
327 | } | |
328 | ||
329 | opt = qemu_opt_find(opts, name); | |
330 | if (!opt) { | |
44b0b7d1 | 331 | return g_strdup(find_default_by_name(opts, name)); |
782730b0 CL |
332 | } |
333 | str = opt->str; | |
334 | opt->str = NULL; | |
335 | qemu_opt_del_all(opts, name); | |
336 | return str; | |
337 | } | |
338 | ||
c8057f95 PM |
339 | bool qemu_opt_has_help_opt(QemuOpts *opts) |
340 | { | |
341 | QemuOpt *opt; | |
342 | ||
eae3eb3e | 343 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, next) { |
c8057f95 PM |
344 | if (is_help_option(opt->name)) { |
345 | return true; | |
346 | } | |
347 | } | |
348 | return false; | |
349 | } | |
350 | ||
782730b0 CL |
351 | static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name, |
352 | bool defval, bool del) | |
e27c88fe | 353 | { |
435db4cf | 354 | QemuOpt *opt; |
f23db652 | 355 | const char *def_val; |
782730b0 | 356 | bool ret = defval; |
e27c88fe | 357 | |
435db4cf CL |
358 | if (opts == NULL) { |
359 | return ret; | |
360 | } | |
361 | ||
362 | opt = qemu_opt_find(opts, name); | |
09722032 | 363 | if (opt == NULL) { |
f23db652 MA |
364 | def_val = find_default_by_name(opts, name); |
365 | if (def_val) { | |
366 | parse_option_bool(name, def_val, &ret, &error_abort); | |
09722032 | 367 | } |
782730b0 | 368 | return ret; |
09722032 | 369 | } |
e27c88fe | 370 | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
782730b0 CL |
371 | ret = opt->value.boolean; |
372 | if (del) { | |
373 | qemu_opt_del_all(opts, name); | |
374 | } | |
375 | return ret; | |
e27c88fe GH |
376 | } |
377 | ||
782730b0 CL |
378 | bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
379 | { | |
380 | return qemu_opt_get_bool_helper(opts, name, defval, false); | |
381 | } | |
382 | ||
383 | bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval) | |
384 | { | |
385 | return qemu_opt_get_bool_helper(opts, name, defval, true); | |
386 | } | |
387 | ||
388 | static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name, | |
389 | uint64_t defval, bool del) | |
e27c88fe | 390 | { |
435db4cf | 391 | QemuOpt *opt; |
f23db652 | 392 | const char *def_val; |
782730b0 | 393 | uint64_t ret = defval; |
e27c88fe | 394 | |
435db4cf CL |
395 | if (opts == NULL) { |
396 | return ret; | |
397 | } | |
398 | ||
399 | opt = qemu_opt_find(opts, name); | |
09722032 | 400 | if (opt == NULL) { |
f23db652 MA |
401 | def_val = find_default_by_name(opts, name); |
402 | if (def_val) { | |
403 | parse_option_number(name, def_val, &ret, &error_abort); | |
09722032 | 404 | } |
782730b0 | 405 | return ret; |
09722032 | 406 | } |
e27c88fe | 407 | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
782730b0 CL |
408 | ret = opt->value.uint; |
409 | if (del) { | |
410 | qemu_opt_del_all(opts, name); | |
411 | } | |
412 | return ret; | |
e27c88fe GH |
413 | } |
414 | ||
782730b0 CL |
415 | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
416 | { | |
417 | return qemu_opt_get_number_helper(opts, name, defval, false); | |
418 | } | |
419 | ||
420 | uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name, | |
421 | uint64_t defval) | |
422 | { | |
423 | return qemu_opt_get_number_helper(opts, name, defval, true); | |
424 | } | |
425 | ||
426 | static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name, | |
427 | uint64_t defval, bool del) | |
e27c88fe | 428 | { |
435db4cf | 429 | QemuOpt *opt; |
f23db652 | 430 | const char *def_val; |
782730b0 | 431 | uint64_t ret = defval; |
e27c88fe | 432 | |
435db4cf CL |
433 | if (opts == NULL) { |
434 | return ret; | |
435 | } | |
436 | ||
437 | opt = qemu_opt_find(opts, name); | |
09722032 | 438 | if (opt == NULL) { |
f23db652 MA |
439 | def_val = find_default_by_name(opts, name); |
440 | if (def_val) { | |
441 | parse_option_size(name, def_val, &ret, &error_abort); | |
09722032 | 442 | } |
782730b0 | 443 | return ret; |
09722032 | 444 | } |
e27c88fe | 445 | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
782730b0 CL |
446 | ret = opt->value.uint; |
447 | if (del) { | |
448 | qemu_opt_del_all(opts, name); | |
449 | } | |
450 | return ret; | |
451 | } | |
452 | ||
453 | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) | |
454 | { | |
455 | return qemu_opt_get_size_helper(opts, name, defval, false); | |
456 | } | |
457 | ||
458 | uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, | |
459 | uint64_t defval) | |
460 | { | |
461 | return qemu_opt_get_size_helper(opts, name, defval, true); | |
e27c88fe GH |
462 | } |
463 | ||
c75d7f71 | 464 | static bool qemu_opt_parse(QemuOpt *opt, Error **errp) |
e27c88fe GH |
465 | { |
466 | if (opt->desc == NULL) | |
c75d7f71 | 467 | return true; |
2f39df5b | 468 | |
e27c88fe GH |
469 | switch (opt->desc->type) { |
470 | case QEMU_OPT_STRING: | |
471 | /* nothing */ | |
c75d7f71 | 472 | return true; |
e27c88fe | 473 | case QEMU_OPT_BOOL: |
c75d7f71 MA |
474 | return parse_option_bool(opt->name, opt->str, &opt->value.boolean, |
475 | errp); | |
e27c88fe | 476 | case QEMU_OPT_NUMBER: |
c75d7f71 MA |
477 | return parse_option_number(opt->name, opt->str, &opt->value.uint, |
478 | errp); | |
e27c88fe | 479 | case QEMU_OPT_SIZE: |
c75d7f71 MA |
480 | return parse_option_size(opt->name, opt->str, &opt->value.uint, |
481 | errp); | |
e27c88fe GH |
482 | default: |
483 | abort(); | |
484 | } | |
485 | } | |
486 | ||
c474ced8 DXW |
487 | static bool opts_accepts_any(const QemuOpts *opts) |
488 | { | |
489 | return opts->list->desc[0].name == NULL; | |
490 | } | |
491 | ||
0dd6c526 KW |
492 | int qemu_opt_unset(QemuOpts *opts, const char *name) |
493 | { | |
494 | QemuOpt *opt = qemu_opt_find(opts, name); | |
495 | ||
496 | assert(opts_accepts_any(opts)); | |
497 | ||
498 | if (opt == NULL) { | |
499 | return -1; | |
500 | } else { | |
501 | qemu_opt_del(opt); | |
502 | return 0; | |
503 | } | |
504 | } | |
505 | ||
81a8a072 MA |
506 | static QemuOpt *opt_create(QemuOpts *opts, const char *name, char *value, |
507 | bool prepend) | |
508 | { | |
509 | QemuOpt *opt = g_malloc0(sizeof(*opt)); | |
510 | ||
511 | opt->name = g_strdup(name); | |
512 | opt->str = value; | |
513 | opt->opts = opts; | |
514 | if (prepend) { | |
515 | QTAILQ_INSERT_HEAD(&opts->head, opt, next); | |
516 | } else { | |
517 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
518 | } | |
519 | ||
520 | return opt; | |
521 | } | |
522 | ||
64af7a8b MA |
523 | static bool opt_validate(QemuOpt *opt, bool *help_wanted, |
524 | Error **errp) | |
c474ced8 | 525 | { |
c474ced8 DXW |
526 | const QemuOptDesc *desc; |
527 | Error *local_err = NULL; | |
528 | ||
64af7a8b MA |
529 | desc = find_desc_by_name(opt->opts->list->desc, opt->name); |
530 | if (!desc && !opts_accepts_any(opt->opts)) { | |
531 | error_setg(errp, QERR_INVALID_PARAMETER, opt->name); | |
532 | if (help_wanted && is_help_option(opt->name)) { | |
56a9efa1 | 533 | *help_wanted = true; |
e05979d0 | 534 | } |
64af7a8b | 535 | return false; |
e27c88fe | 536 | } |
dc9ca4ba | 537 | |
c474ced8 | 538 | opt->desc = desc; |
235e59cf | 539 | if (!qemu_opt_parse(opt, &local_err)) { |
584d4064 | 540 | error_propagate(errp, local_err); |
64af7a8b | 541 | return false; |
e27c88fe | 542 | } |
64af7a8b MA |
543 | |
544 | return true; | |
e27c88fe GH |
545 | } |
546 | ||
c75d7f71 | 547 | bool qemu_opt_set(QemuOpts *opts, const char *name, const char *value, |
f43e47db | 548 | Error **errp) |
384f2139 | 549 | { |
64af7a8b MA |
550 | QemuOpt *opt = opt_create(opts, name, g_strdup(value), false); |
551 | ||
552 | if (!opt_validate(opt, NULL, errp)) { | |
553 | qemu_opt_del(opt); | |
c75d7f71 | 554 | return false; |
64af7a8b | 555 | } |
c75d7f71 | 556 | return true; |
384f2139 LC |
557 | } |
558 | ||
c75d7f71 | 559 | bool qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, |
cccb7967 | 560 | Error **errp) |
f02b77c9 MK |
561 | { |
562 | QemuOpt *opt; | |
9da7197a | 563 | const QemuOptDesc *desc; |
f02b77c9 | 564 | |
9da7197a MA |
565 | desc = find_desc_by_name(opts->list->desc, name); |
566 | if (!desc && !opts_accepts_any(opts)) { | |
c6bd8c70 | 567 | error_setg(errp, QERR_INVALID_PARAMETER, name); |
c75d7f71 | 568 | return false; |
f02b77c9 MK |
569 | } |
570 | ||
9da7197a | 571 | opt = g_malloc0(sizeof(*opt)); |
f02b77c9 MK |
572 | opt->name = g_strdup(name); |
573 | opt->opts = opts; | |
9da7197a | 574 | opt->desc = desc; |
f02b77c9 | 575 | opt->value.boolean = !!val; |
ad718d01 DXW |
576 | opt->str = g_strdup(val ? "on" : "off"); |
577 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
c75d7f71 | 578 | return true; |
f02b77c9 MK |
579 | } |
580 | ||
c75d7f71 | 581 | bool qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, |
39101f25 | 582 | Error **errp) |
b83c18e2 DXW |
583 | { |
584 | QemuOpt *opt; | |
9da7197a | 585 | const QemuOptDesc *desc; |
b83c18e2 | 586 | |
9da7197a MA |
587 | desc = find_desc_by_name(opts->list->desc, name); |
588 | if (!desc && !opts_accepts_any(opts)) { | |
c6bd8c70 | 589 | error_setg(errp, QERR_INVALID_PARAMETER, name); |
c75d7f71 | 590 | return false; |
b83c18e2 DXW |
591 | } |
592 | ||
9da7197a | 593 | opt = g_malloc0(sizeof(*opt)); |
b83c18e2 DXW |
594 | opt->name = g_strdup(name); |
595 | opt->opts = opts; | |
9da7197a | 596 | opt->desc = desc; |
b83c18e2 DXW |
597 | opt->value.uint = val; |
598 | opt->str = g_strdup_printf("%" PRId64, val); | |
599 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
c75d7f71 | 600 | return true; |
b83c18e2 DXW |
601 | } |
602 | ||
1640b200 | 603 | /** |
71df1d83 MA |
604 | * For each member of @opts, call @func(@opaque, name, value, @errp). |
605 | * @func() may store an Error through @errp, but must return non-zero then. | |
1640b200 MA |
606 | * When @func() returns non-zero, break the loop and return that value. |
607 | * Return zero when the loop completes. | |
608 | */ | |
71df1d83 MA |
609 | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
610 | Error **errp) | |
48026075 GH |
611 | { |
612 | QemuOpt *opt; | |
1640b200 | 613 | int rc; |
48026075 | 614 | |
72cf2d4f | 615 | QTAILQ_FOREACH(opt, &opts->head, next) { |
71df1d83 | 616 | rc = func(opaque, opt->name, opt->str, errp); |
1640b200 MA |
617 | if (rc) { |
618 | return rc; | |
619 | } | |
71df1d83 | 620 | assert(!errp || !*errp); |
48026075 | 621 | } |
1640b200 | 622 | return 0; |
48026075 GH |
623 | } |
624 | ||
e27c88fe GH |
625 | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
626 | { | |
627 | QemuOpts *opts; | |
628 | ||
72cf2d4f | 629 | QTAILQ_FOREACH(opts, &list->head, next) { |
96bc97eb MA |
630 | if (!opts->id && !id) { |
631 | return opts; | |
e27c88fe | 632 | } |
96bc97eb MA |
633 | if (opts->id && id && !strcmp(opts->id, id)) { |
634 | return opts; | |
e27c88fe | 635 | } |
e27c88fe GH |
636 | } |
637 | return NULL; | |
638 | } | |
639 | ||
8be7e7e4 LC |
640 | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, |
641 | int fail_if_exists, Error **errp) | |
e27c88fe GH |
642 | { |
643 | QemuOpts *opts = NULL; | |
644 | ||
645 | if (id) { | |
f5bebbbb | 646 | if (!id_wellformed(id)) { |
c6bd8c70 MA |
647 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", |
648 | "an identifier"); | |
50b7b000 | 649 | error_append_hint(errp, "Identifiers consist of letters, digits, " |
543202c0 | 650 | "'-', '.', '_', starting with a letter.\n"); |
b560a9ab MA |
651 | return NULL; |
652 | } | |
e27c88fe GH |
653 | opts = qemu_opts_find(list, id); |
654 | if (opts != NULL) { | |
da93318a | 655 | if (fail_if_exists && !list->merge_lists) { |
f231b88d | 656 | error_setg(errp, "Duplicate ID '%s' for %s", id, list->name); |
e27c88fe GH |
657 | return NULL; |
658 | } else { | |
659 | return opts; | |
660 | } | |
661 | } | |
da93318a PM |
662 | } else if (list->merge_lists) { |
663 | opts = qemu_opts_find(list, NULL); | |
664 | if (opts) { | |
665 | return opts; | |
666 | } | |
e27c88fe | 667 | } |
7267c094 | 668 | opts = g_malloc0(sizeof(*opts)); |
c64f50d1 | 669 | opts->id = g_strdup(id); |
e27c88fe | 670 | opts->list = list; |
827b0813 | 671 | loc_save(&opts->loc); |
72cf2d4f BS |
672 | QTAILQ_INIT(&opts->head); |
673 | QTAILQ_INSERT_TAIL(&list->head, opts, next); | |
e27c88fe GH |
674 | return opts; |
675 | } | |
676 | ||
bb67ab02 MA |
677 | void qemu_opts_reset(QemuOptsList *list) |
678 | { | |
679 | QemuOpts *opts, *next_opts; | |
680 | ||
681 | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { | |
682 | qemu_opts_del(opts); | |
683 | } | |
684 | } | |
685 | ||
94ac7268 MA |
686 | void qemu_opts_loc_restore(QemuOpts *opts) |
687 | { | |
688 | loc_restore(&opts->loc); | |
689 | } | |
690 | ||
c75d7f71 | 691 | bool qemu_opts_set(QemuOptsList *list, const char *id, |
79087c78 | 692 | const char *name, const char *value, Error **errp) |
e27c88fe GH |
693 | { |
694 | QemuOpts *opts; | |
695 | ||
c6ecec43 MA |
696 | opts = qemu_opts_create(list, id, 1, errp); |
697 | if (!opts) { | |
c75d7f71 | 698 | return false; |
e27c88fe | 699 | } |
c75d7f71 | 700 | return qemu_opt_set(opts, name, value, errp); |
e27c88fe GH |
701 | } |
702 | ||
48026075 GH |
703 | const char *qemu_opts_id(QemuOpts *opts) |
704 | { | |
705 | return opts->id; | |
706 | } | |
707 | ||
326642bc KW |
708 | /* The id string will be g_free()d by qemu_opts_del */ |
709 | void qemu_opts_set_id(QemuOpts *opts, char *id) | |
710 | { | |
711 | opts->id = id; | |
712 | } | |
713 | ||
e27c88fe GH |
714 | void qemu_opts_del(QemuOpts *opts) |
715 | { | |
716 | QemuOpt *opt; | |
717 | ||
4782183d CL |
718 | if (opts == NULL) { |
719 | return; | |
720 | } | |
721 | ||
e27c88fe | 722 | for (;;) { |
72cf2d4f | 723 | opt = QTAILQ_FIRST(&opts->head); |
e27c88fe GH |
724 | if (opt == NULL) |
725 | break; | |
726 | qemu_opt_del(opt); | |
727 | } | |
72cf2d4f | 728 | QTAILQ_REMOVE(&opts->list->head, opts, next); |
7267c094 AL |
729 | g_free(opts->id); |
730 | g_free(opts); | |
e27c88fe GH |
731 | } |
732 | ||
fe646693 KZ |
733 | /* print value, escaping any commas in value */ |
734 | static void escaped_print(const char *value) | |
735 | { | |
736 | const char *ptr; | |
737 | ||
738 | for (ptr = value; *ptr; ++ptr) { | |
739 | if (*ptr == ',') { | |
740 | putchar(','); | |
741 | } | |
742 | putchar(*ptr); | |
743 | } | |
744 | } | |
745 | ||
746 | void qemu_opts_print(QemuOpts *opts, const char *separator) | |
e27c88fe GH |
747 | { |
748 | QemuOpt *opt; | |
09722032 | 749 | QemuOptDesc *desc = opts->list->desc; |
fe646693 KZ |
750 | const char *sep = ""; |
751 | ||
752 | if (opts->id) { | |
753 | printf("id=%s", opts->id); /* passed id_wellformed -> no commas */ | |
754 | sep = separator; | |
755 | } | |
e27c88fe | 756 | |
09722032 CL |
757 | if (desc[0].name == NULL) { |
758 | QTAILQ_FOREACH(opt, &opts->head, next) { | |
fe646693 KZ |
759 | printf("%s%s=", sep, opt->name); |
760 | escaped_print(opt->str); | |
761 | sep = separator; | |
09722032 CL |
762 | } |
763 | return; | |
764 | } | |
765 | for (; desc && desc->name; desc++) { | |
766 | const char *value; | |
da78e382 | 767 | opt = qemu_opt_find(opts, desc->name); |
09722032 CL |
768 | |
769 | value = opt ? opt->str : desc->def_value_str; | |
770 | if (!value) { | |
771 | continue; | |
772 | } | |
773 | if (desc->type == QEMU_OPT_STRING) { | |
fe646693 KZ |
774 | printf("%s%s=", sep, desc->name); |
775 | escaped_print(value); | |
09722032 CL |
776 | } else if ((desc->type == QEMU_OPT_SIZE || |
777 | desc->type == QEMU_OPT_NUMBER) && opt) { | |
43c5d8f8 | 778 | printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint); |
09722032 | 779 | } else { |
43c5d8f8 | 780 | printf("%s%s=%s", sep, desc->name, value); |
09722032 | 781 | } |
fe646693 | 782 | sep = separator; |
e27c88fe | 783 | } |
e27c88fe GH |
784 | } |
785 | ||
6129803b MA |
786 | static const char *get_opt_name_value(const char *params, |
787 | const char *firstname, | |
788 | char **name, char **value) | |
789 | { | |
790 | const char *p, *pe, *pc; | |
791 | ||
792 | pe = strchr(params, '='); | |
793 | pc = strchr(params, ','); | |
794 | ||
795 | if (!pe || (pc && pc < pe)) { | |
796 | /* found "foo,more" */ | |
797 | if (firstname) { | |
798 | /* implicitly named first option */ | |
799 | *name = g_strdup(firstname); | |
800 | p = get_opt_value(params, value); | |
801 | } else { | |
802 | /* option without value, must be a flag */ | |
803 | p = get_opt_name(params, name, ','); | |
804 | if (strncmp(*name, "no", 2) == 0) { | |
805 | memmove(*name, *name + 2, strlen(*name + 2) + 1); | |
806 | *value = g_strdup("off"); | |
807 | } else { | |
808 | *value = g_strdup("on"); | |
809 | } | |
810 | } | |
811 | } else { | |
812 | /* found "foo=bar,more" */ | |
813 | p = get_opt_name(params, name, '='); | |
814 | assert(*p == '='); | |
815 | p++; | |
816 | p = get_opt_value(p, value); | |
817 | } | |
818 | ||
819 | assert(!*p || *p == ','); | |
820 | if (*p == ',') { | |
821 | p++; | |
822 | } | |
823 | return p; | |
824 | } | |
825 | ||
c75d7f71 | 826 | static bool opts_do_parse(QemuOpts *opts, const char *params, |
e05979d0 | 827 | const char *firstname, bool prepend, |
56a9efa1 | 828 | bool *help_wanted, Error **errp) |
e27c88fe | 829 | { |
6129803b MA |
830 | char *option, *value; |
831 | const char *p; | |
64af7a8b | 832 | QemuOpt *opt; |
e27c88fe | 833 | |
6129803b MA |
834 | for (p = params; *p;) { |
835 | p = get_opt_name_value(p, firstname, &option, &value); | |
836 | firstname = NULL; | |
837 | ||
838 | if (!strcmp(option, "id")) { | |
839 | g_free(option); | |
840 | g_free(value); | |
841 | continue; | |
e27c88fe | 842 | } |
6129803b | 843 | |
64af7a8b | 844 | opt = opt_create(opts, option, value, prepend); |
e652714f | 845 | g_free(option); |
64af7a8b MA |
846 | if (!opt_validate(opt, help_wanted, errp)) { |
847 | qemu_opt_del(opt); | |
c75d7f71 | 848 | return false; |
6129803b | 849 | } |
e27c88fe | 850 | } |
c75d7f71 MA |
851 | |
852 | return true; | |
96729cbd GH |
853 | } |
854 | ||
933d1527 MA |
855 | static char *opts_parse_id(const char *params) |
856 | { | |
857 | const char *p; | |
858 | char *name, *value; | |
859 | ||
860 | for (p = params; *p;) { | |
861 | p = get_opt_name_value(p, NULL, &name, &value); | |
862 | if (!strcmp(name, "id")) { | |
863 | g_free(name); | |
864 | return value; | |
865 | } | |
866 | g_free(name); | |
867 | g_free(value); | |
868 | } | |
869 | ||
870 | return NULL; | |
871 | } | |
872 | ||
80a94855 MA |
873 | bool has_help_option(const char *params) |
874 | { | |
875 | const char *p; | |
876 | char *name, *value; | |
877 | bool ret; | |
878 | ||
879 | for (p = params; *p;) { | |
880 | p = get_opt_name_value(p, NULL, &name, &value); | |
881 | ret = is_help_option(name); | |
882 | g_free(name); | |
883 | g_free(value); | |
884 | if (ret) { | |
885 | return true; | |
886 | } | |
887 | } | |
888 | ||
889 | return false; | |
890 | } | |
891 | ||
dc523cd3 MA |
892 | /** |
893 | * Store options parsed from @params into @opts. | |
894 | * If @firstname is non-null, the first key=value in @params may omit | |
895 | * key=, and is treated as if key was @firstname. | |
896 | * On error, store an error object through @errp if non-null. | |
897 | */ | |
c75d7f71 | 898 | bool qemu_opts_do_parse(QemuOpts *opts, const char *params, |
dc523cd3 | 899 | const char *firstname, Error **errp) |
4f6dd9af | 900 | { |
c75d7f71 | 901 | return opts_do_parse(opts, params, firstname, false, NULL, errp); |
4f6dd9af JK |
902 | } |
903 | ||
904 | static QemuOpts *opts_parse(QemuOptsList *list, const char *params, | |
e05979d0 | 905 | bool permit_abbrev, bool defaults, |
56a9efa1 | 906 | bool *help_wanted, Error **errp) |
96729cbd | 907 | { |
8212c64f | 908 | const char *firstname; |
933d1527 | 909 | char *id = opts_parse_id(params); |
96729cbd | 910 | QemuOpts *opts; |
8be7e7e4 | 911 | Error *local_err = NULL; |
96729cbd | 912 | |
8212c64f MA |
913 | assert(!permit_abbrev || list->implied_opt_name); |
914 | firstname = permit_abbrev ? list->implied_opt_name : NULL; | |
915 | ||
cb77d192 MA |
916 | /* |
917 | * This code doesn't work for defaults && !list->merge_lists: when | |
918 | * params has no id=, and list has an element with !opts->id, it | |
919 | * appends a new element instead of returning the existing opts. | |
920 | * However, we got no use for this case. Guard against possible | |
921 | * (if unlikely) future misuse: | |
922 | */ | |
923 | assert(!defaults || list->merge_lists); | |
6d4cd408 | 924 | opts = qemu_opts_create(list, id, !defaults, &local_err); |
950c4e6c | 925 | g_free(id); |
8be7e7e4 | 926 | if (opts == NULL) { |
4f81273d | 927 | error_propagate(errp, local_err); |
96729cbd | 928 | return NULL; |
8be7e7e4 | 929 | } |
96729cbd | 930 | |
235e59cf MA |
931 | if (!opts_do_parse(opts, params, firstname, defaults, help_wanted, |
932 | &local_err)) { | |
4f81273d | 933 | error_propagate(errp, local_err); |
96729cbd GH |
934 | qemu_opts_del(opts); |
935 | return NULL; | |
936 | } | |
937 | ||
e27c88fe GH |
938 | return opts; |
939 | } | |
940 | ||
4f81273d MA |
941 | /** |
942 | * Create a QemuOpts in @list and with options parsed from @params. | |
943 | * If @permit_abbrev, the first key=value in @params may omit key=, | |
944 | * and is treated as if key was @list->implied_opt_name. | |
70b94331 | 945 | * On error, store an error object through @errp if non-null. |
4f81273d MA |
946 | * Return the new QemuOpts on success, null pointer on error. |
947 | */ | |
4f6dd9af | 948 | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
70b94331 MA |
949 | bool permit_abbrev, Error **errp) |
950 | { | |
e05979d0 | 951 | return opts_parse(list, params, permit_abbrev, false, NULL, errp); |
70b94331 MA |
952 | } |
953 | ||
954 | /** | |
955 | * Create a QemuOpts in @list and with options parsed from @params. | |
956 | * If @permit_abbrev, the first key=value in @params may omit key=, | |
957 | * and is treated as if key was @list->implied_opt_name. | |
958 | * Report errors with error_report_err(). This is inappropriate in | |
959 | * QMP context. Do not use this function there! | |
960 | * Return the new QemuOpts on success, null pointer on error. | |
961 | */ | |
962 | QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, | |
963 | bool permit_abbrev) | |
4f6dd9af | 964 | { |
4f81273d MA |
965 | Error *err = NULL; |
966 | QemuOpts *opts; | |
56a9efa1 | 967 | bool help_wanted = false; |
4f81273d | 968 | |
56a9efa1 | 969 | opts = opts_parse(list, params, permit_abbrev, false, &help_wanted, &err); |
70b94331 | 970 | if (err) { |
56a9efa1 | 971 | if (help_wanted) { |
63898712 | 972 | qemu_opts_print_help(list, true); |
e05979d0 MAL |
973 | error_free(err); |
974 | } else { | |
975 | error_report_err(err); | |
976 | } | |
4f81273d MA |
977 | } |
978 | return opts; | |
4f6dd9af JK |
979 | } |
980 | ||
981 | void qemu_opts_set_defaults(QemuOptsList *list, const char *params, | |
982 | int permit_abbrev) | |
983 | { | |
984 | QemuOpts *opts; | |
985 | ||
e05979d0 | 986 | opts = opts_parse(list, params, permit_abbrev, true, NULL, NULL); |
4f6dd9af JK |
987 | assert(opts); |
988 | } | |
989 | ||
c75d7f71 | 990 | static bool qemu_opts_from_qdict_entry(QemuOpts *opts, |
2500f6f3 MA |
991 | const QDictEntry *entry, |
992 | Error **errp) | |
01e7f188 | 993 | { |
2500f6f3 MA |
994 | const char *key = qdict_entry_key(entry); |
995 | QObject *obj = qdict_entry_value(entry); | |
c75d7f71 MA |
996 | char buf[32]; |
997 | g_autofree char *tmp = NULL; | |
01e7f188 | 998 | const char *value; |
01e7f188 | 999 | |
2500f6f3 | 1000 | if (!strcmp(key, "id")) { |
c75d7f71 | 1001 | return true; |
01e7f188 MA |
1002 | } |
1003 | ||
1004 | switch (qobject_type(obj)) { | |
1005 | case QTYPE_QSTRING: | |
7dc847eb | 1006 | value = qstring_get_str(qobject_to(QString, obj)); |
01e7f188 | 1007 | break; |
01b2ffce | 1008 | case QTYPE_QNUM: |
7dc847eb | 1009 | tmp = qnum_to_string(qobject_to(QNum, obj)); |
01b2ffce | 1010 | value = tmp; |
01e7f188 MA |
1011 | break; |
1012 | case QTYPE_QBOOL: | |
d35215f8 | 1013 | pstrcpy(buf, sizeof(buf), |
7dc847eb | 1014 | qbool_get_bool(qobject_to(QBool, obj)) ? "on" : "off"); |
01e7f188 MA |
1015 | value = buf; |
1016 | break; | |
1017 | default: | |
c75d7f71 | 1018 | return true; |
01e7f188 | 1019 | } |
4e89978e | 1020 | |
c75d7f71 | 1021 | return qemu_opt_set(opts, key, value, errp); |
01e7f188 MA |
1022 | } |
1023 | ||
1024 | /* | |
1025 | * Create QemuOpts from a QDict. | |
01b2ffce MAL |
1026 | * Use value of key "id" as ID if it exists and is a QString. Only |
1027 | * QStrings, QNums and QBools are copied. Entries with other types | |
1028 | * are silently ignored. | |
01e7f188 | 1029 | */ |
4e89978e LC |
1030 | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, |
1031 | Error **errp) | |
01e7f188 | 1032 | { |
8be7e7e4 | 1033 | Error *local_err = NULL; |
4e89978e | 1034 | QemuOpts *opts; |
7b1cd1c6 | 1035 | const QDictEntry *entry; |
01e7f188 | 1036 | |
c6ecec43 MA |
1037 | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, errp); |
1038 | if (!opts) { | |
01e7f188 | 1039 | return NULL; |
8be7e7e4 | 1040 | } |
01e7f188 | 1041 | |
8be7e7e4 | 1042 | assert(opts != NULL); |
4e89978e | 1043 | |
7b1cd1c6 MA |
1044 | for (entry = qdict_first(qdict); |
1045 | entry; | |
1046 | entry = qdict_next(qdict, entry)) { | |
235e59cf | 1047 | if (!qemu_opts_from_qdict_entry(opts, entry, &local_err)) { |
2500f6f3 MA |
1048 | error_propagate(errp, local_err); |
1049 | qemu_opts_del(opts); | |
1050 | return NULL; | |
1051 | } | |
4e89978e LC |
1052 | } |
1053 | ||
01e7f188 MA |
1054 | return opts; |
1055 | } | |
1056 | ||
376609cc KW |
1057 | /* |
1058 | * Adds all QDict entries to the QemuOpts that can be added and removes them | |
1059 | * from the QDict. When this function returns, the QDict contains only those | |
1060 | * entries that couldn't be added to the QemuOpts. | |
1061 | */ | |
c75d7f71 | 1062 | bool qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp) |
376609cc KW |
1063 | { |
1064 | const QDictEntry *entry, *next; | |
1065 | ||
1066 | entry = qdict_first(qdict); | |
1067 | ||
1068 | while (entry != NULL) { | |
1069 | Error *local_err = NULL; | |
376609cc KW |
1070 | |
1071 | next = qdict_next(qdict, entry); | |
1072 | ||
1073 | if (find_desc_by_name(opts->list->desc, entry->key)) { | |
235e59cf | 1074 | if (!qemu_opts_from_qdict_entry(opts, entry, &local_err)) { |
376609cc | 1075 | error_propagate(errp, local_err); |
c75d7f71 | 1076 | return false; |
376609cc | 1077 | } |
2500f6f3 | 1078 | qdict_del(qdict, entry->key); |
376609cc KW |
1079 | } |
1080 | ||
1081 | entry = next; | |
1082 | } | |
c75d7f71 MA |
1083 | |
1084 | return true; | |
376609cc KW |
1085 | } |
1086 | ||
01e7f188 | 1087 | /* |
72215395 KW |
1088 | * Convert from QemuOpts to QDict. The QDict values are of type QString. |
1089 | * | |
1090 | * If @list is given, only add those options to the QDict that are contained in | |
1091 | * the list. If @del is true, any options added to the QDict are removed from | |
1092 | * the QemuOpts, otherwise they remain there. | |
1093 | * | |
1094 | * If two options in @opts have the same name, they are processed in order | |
1095 | * so that the last one wins (consistent with the reverse iteration in | |
1096 | * qemu_opt_find()), but all of them are deleted if @del is true. | |
1097 | * | |
01e7f188 MA |
1098 | * TODO We'll want to use types appropriate for opt->desc->type, but |
1099 | * this is enough for now. | |
1100 | */ | |
72215395 KW |
1101 | QDict *qemu_opts_to_qdict_filtered(QemuOpts *opts, QDict *qdict, |
1102 | QemuOptsList *list, bool del) | |
01e7f188 | 1103 | { |
72215395 | 1104 | QemuOpt *opt, *next; |
01e7f188 MA |
1105 | |
1106 | if (!qdict) { | |
1107 | qdict = qdict_new(); | |
1108 | } | |
1109 | if (opts->id) { | |
46f5ac20 | 1110 | qdict_put_str(qdict, "id", opts->id); |
01e7f188 | 1111 | } |
72215395 KW |
1112 | QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next) { |
1113 | if (list) { | |
1114 | QemuOptDesc *desc; | |
1115 | bool found = false; | |
1116 | for (desc = list->desc; desc->name; desc++) { | |
1117 | if (!strcmp(desc->name, opt->name)) { | |
1118 | found = true; | |
1119 | break; | |
1120 | } | |
1121 | } | |
1122 | if (!found) { | |
1123 | continue; | |
1124 | } | |
1125 | } | |
28934e0c | 1126 | qdict_put_str(qdict, opt->name, opt->str); |
72215395 KW |
1127 | if (del) { |
1128 | qemu_opt_del(opt); | |
1129 | } | |
01e7f188 MA |
1130 | } |
1131 | return qdict; | |
1132 | } | |
1133 | ||
72215395 KW |
1134 | /* Copy all options in a QemuOpts to the given QDict. See |
1135 | * qemu_opts_to_qdict_filtered() for details. */ | |
1136 | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) | |
1137 | { | |
1138 | return qemu_opts_to_qdict_filtered(opts, qdict, NULL, false); | |
1139 | } | |
1140 | ||
5dc519ef MM |
1141 | /* Validate parsed opts against descriptions where no |
1142 | * descriptions were provided in the QemuOptsList. | |
1143 | */ | |
c75d7f71 | 1144 | bool qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp) |
5dc519ef MM |
1145 | { |
1146 | QemuOpt *opt; | |
6c519404 | 1147 | Error *local_err = NULL; |
5dc519ef | 1148 | |
db97ceba | 1149 | assert(opts_accepts_any(opts)); |
5dc519ef MM |
1150 | |
1151 | QTAILQ_FOREACH(opt, &opts->head, next) { | |
db97ceba DXW |
1152 | opt->desc = find_desc_by_name(desc, opt->name); |
1153 | if (!opt->desc) { | |
c6bd8c70 | 1154 | error_setg(errp, QERR_INVALID_PARAMETER, opt->name); |
c75d7f71 | 1155 | return false; |
5dc519ef MM |
1156 | } |
1157 | ||
235e59cf | 1158 | if (!qemu_opt_parse(opt, &local_err)) { |
29952866 | 1159 | error_propagate(errp, local_err); |
c75d7f71 | 1160 | return false; |
5dc519ef MM |
1161 | } |
1162 | } | |
c75d7f71 MA |
1163 | |
1164 | return true; | |
5dc519ef MM |
1165 | } |
1166 | ||
a4c7367f | 1167 | /** |
28d0de7a | 1168 | * For each member of @list, call @func(@opaque, member, @errp). |
a4c7367f | 1169 | * Call it with the current location temporarily set to the member's. |
28d0de7a | 1170 | * @func() may store an Error through @errp, but must return non-zero then. |
a4c7367f MA |
1171 | * When @func() returns non-zero, break the loop and return that value. |
1172 | * Return zero when the loop completes. | |
1173 | */ | |
1174 | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, | |
28d0de7a | 1175 | void *opaque, Error **errp) |
e27c88fe | 1176 | { |
827b0813 | 1177 | Location loc; |
e27c88fe | 1178 | QemuOpts *opts; |
37f32349 | 1179 | int rc = 0; |
e27c88fe | 1180 | |
827b0813 | 1181 | loc_push_none(&loc); |
72cf2d4f | 1182 | QTAILQ_FOREACH(opts, &list->head, next) { |
827b0813 | 1183 | loc_restore(&opts->loc); |
28d0de7a | 1184 | rc = func(opaque, opts, errp); |
a4c7367f | 1185 | if (rc) { |
37f32349 | 1186 | break; |
a4c7367f | 1187 | } |
28d0de7a | 1188 | assert(!errp || !*errp); |
e27c88fe | 1189 | } |
827b0813 | 1190 | loc_pop(&loc); |
37f32349 | 1191 | return rc; |
e27c88fe | 1192 | } |
8559e45e CL |
1193 | |
1194 | static size_t count_opts_list(QemuOptsList *list) | |
1195 | { | |
1196 | QemuOptDesc *desc = NULL; | |
1197 | size_t num_opts = 0; | |
1198 | ||
1199 | if (!list) { | |
1200 | return 0; | |
1201 | } | |
1202 | ||
1203 | desc = list->desc; | |
1204 | while (desc && desc->name) { | |
1205 | num_opts++; | |
1206 | desc++; | |
1207 | } | |
1208 | ||
1209 | return num_opts; | |
1210 | } | |
1211 | ||
8559e45e CL |
1212 | void qemu_opts_free(QemuOptsList *list) |
1213 | { | |
8559e45e CL |
1214 | g_free(list); |
1215 | } | |
a1097a26 | 1216 | |
c282e1fd CL |
1217 | /* Realloc dst option list and append options from an option list (list) |
1218 | * to it. dst could be NULL or a malloced list. | |
98d896d9 CL |
1219 | * The lifetime of dst must be shorter than the input list because the |
1220 | * QemuOptDesc->name, ->help, and ->def_value_str strings are shared. | |
a1097a26 CL |
1221 | */ |
1222 | QemuOptsList *qemu_opts_append(QemuOptsList *dst, | |
c282e1fd | 1223 | QemuOptsList *list) |
a1097a26 CL |
1224 | { |
1225 | size_t num_opts, num_dst_opts; | |
1226 | QemuOptDesc *desc; | |
1227 | bool need_init = false; | |
a7607150 | 1228 | bool need_head_update; |
a1097a26 | 1229 | |
c282e1fd | 1230 | if (!list) { |
a1097a26 CL |
1231 | return dst; |
1232 | } | |
1233 | ||
a1097a26 CL |
1234 | /* If dst is NULL, after realloc, some area of dst should be initialized |
1235 | * before adding options to it. | |
1236 | */ | |
1237 | if (!dst) { | |
1238 | need_init = true; | |
a7607150 MP |
1239 | need_head_update = true; |
1240 | } else { | |
1241 | /* Moreover, even if dst is not NULL, the realloc may move it to a | |
1242 | * different address in which case we may get a stale tail pointer | |
1243 | * in dst->head. */ | |
1244 | need_head_update = QTAILQ_EMPTY(&dst->head); | |
a1097a26 CL |
1245 | } |
1246 | ||
1247 | num_opts = count_opts_list(dst); | |
1248 | num_dst_opts = num_opts; | |
1249 | num_opts += count_opts_list(list); | |
1250 | dst = g_realloc(dst, sizeof(QemuOptsList) + | |
1251 | (num_opts + 1) * sizeof(QemuOptDesc)); | |
1252 | if (need_init) { | |
1253 | dst->name = NULL; | |
1254 | dst->implied_opt_name = NULL; | |
a1097a26 CL |
1255 | dst->merge_lists = false; |
1256 | } | |
a7607150 MP |
1257 | if (need_head_update) { |
1258 | QTAILQ_INIT(&dst->head); | |
1259 | } | |
a1097a26 CL |
1260 | dst->desc[num_dst_opts].name = NULL; |
1261 | ||
a1097a26 CL |
1262 | /* append list->desc to dst->desc */ |
1263 | if (list) { | |
1264 | desc = list->desc; | |
1265 | while (desc && desc->name) { | |
1266 | if (find_desc_by_name(dst->desc, desc->name) == NULL) { | |
98d896d9 | 1267 | dst->desc[num_dst_opts++] = *desc; |
a1097a26 CL |
1268 | dst->desc[num_dst_opts].name = NULL; |
1269 | } | |
1270 | desc++; | |
1271 | } | |
1272 | } | |
1273 | ||
a1097a26 CL |
1274 | return dst; |
1275 | } |