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