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