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