]>
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 KW |
27 | |
28 | #include "qemu-common.h" | |
1de7afc9 | 29 | #include "qemu/error-report.h" |
7b1b5d19 | 30 | #include "qapi/qmp/types.h" |
7b1b5d19 | 31 | #include "qapi/qmp/qerror.h" |
1de7afc9 | 32 | #include "qemu/option_int.h" |
d3f24367 KW |
33 | |
34 | /* | |
35 | * Extracts the name of an option from the parameter string (p points at the | |
36 | * first byte of the option name) | |
37 | * | |
38 | * The option name is delimited by delim (usually , or =) or the string end | |
39 | * and is copied into buf. If the option name is longer than buf_size, it is | |
40 | * truncated. buf is always zero terminated. | |
41 | * | |
42 | * The return value is the position of the delimiter/zero byte after the option | |
43 | * name in p. | |
44 | */ | |
45 | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) | |
46 | { | |
47 | char *q; | |
48 | ||
49 | q = buf; | |
50 | while (*p != '\0' && *p != delim) { | |
51 | if (q && (q - buf) < buf_size - 1) | |
52 | *q++ = *p; | |
53 | p++; | |
54 | } | |
55 | if (q) | |
56 | *q = '\0'; | |
57 | ||
58 | return p; | |
59 | } | |
60 | ||
61 | /* | |
62 | * Extracts the value of an option from the parameter string p (p points at the | |
63 | * first byte of the option value) | |
64 | * | |
65 | * This function is comparable to get_opt_name with the difference that the | |
66 | * delimiter is fixed to be comma which starts a new option. To specify an | |
67 | * option value that contains commas, double each comma. | |
68 | */ | |
69 | const char *get_opt_value(char *buf, int buf_size, const char *p) | |
70 | { | |
71 | char *q; | |
72 | ||
73 | q = buf; | |
74 | while (*p != '\0') { | |
75 | if (*p == ',') { | |
76 | if (*(p + 1) != ',') | |
77 | break; | |
78 | p++; | |
79 | } | |
80 | if (q && (q - buf) < buf_size - 1) | |
81 | *q++ = *p; | |
82 | p++; | |
83 | } | |
84 | if (q) | |
85 | *q = '\0'; | |
86 | ||
87 | return p; | |
88 | } | |
89 | ||
62c5802e GH |
90 | int get_next_param_value(char *buf, int buf_size, |
91 | const char *tag, const char **pstr) | |
92 | { | |
93 | const char *p; | |
94 | char option[128]; | |
95 | ||
96 | p = *pstr; | |
97 | for(;;) { | |
98 | p = get_opt_name(option, sizeof(option), p, '='); | |
99 | if (*p != '=') | |
100 | break; | |
101 | p++; | |
102 | if (!strcmp(tag, option)) { | |
103 | *pstr = get_opt_value(buf, buf_size, p); | |
104 | if (**pstr == ',') { | |
105 | (*pstr)++; | |
106 | } | |
107 | return strlen(buf); | |
108 | } else { | |
109 | p = get_opt_value(NULL, 0, p); | |
110 | } | |
111 | if (*p != ',') | |
112 | break; | |
113 | p++; | |
114 | } | |
115 | return 0; | |
116 | } | |
117 | ||
118 | int get_param_value(char *buf, int buf_size, | |
119 | const char *tag, const char *str) | |
120 | { | |
121 | return get_next_param_value(buf, buf_size, tag, &str); | |
122 | } | |
123 | ||
cf62adfa LC |
124 | static void parse_option_bool(const char *name, const char *value, bool *ret, |
125 | Error **errp) | |
67b1355b GH |
126 | { |
127 | if (value != NULL) { | |
128 | if (!strcmp(value, "on")) { | |
129 | *ret = 1; | |
130 | } else if (!strcmp(value, "off")) { | |
131 | *ret = 0; | |
132 | } else { | |
c6bd8c70 MA |
133 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
134 | name, "'on' or 'off'"); | |
67b1355b GH |
135 | } |
136 | } else { | |
137 | *ret = 1; | |
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 GH |
143 | { |
144 | char *postfix; | |
145 | uint64_t number; | |
146 | ||
147 | if (value != NULL) { | |
148 | number = strtoull(value, &postfix, 0); | |
149 | if (*postfix != '\0') { | |
c6bd8c70 | 150 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
2f39df5b | 151 | return; |
e27c88fe | 152 | } |
21c9f4cd | 153 | *ret = number; |
e27c88fe | 154 | } else { |
c6bd8c70 | 155 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number"); |
e27c88fe | 156 | } |
e27c88fe GH |
157 | } |
158 | ||
5e89db76 CL |
159 | static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc, |
160 | const char *name) | |
161 | { | |
162 | int i; | |
163 | ||
164 | for (i = 0; desc[i].name != NULL; i++) { | |
165 | if (strcmp(desc[i].name, name) == 0) { | |
166 | return &desc[i]; | |
167 | } | |
168 | } | |
169 | ||
170 | return NULL; | |
171 | } | |
172 | ||
e8cd45c7 VL |
173 | void parse_option_size(const char *name, const char *value, |
174 | uint64_t *ret, Error **errp) | |
7695019b GH |
175 | { |
176 | char *postfix; | |
177 | double sizef; | |
178 | ||
179 | if (value != NULL) { | |
180 | sizef = strtod(value, &postfix); | |
21278992 BT |
181 | if (sizef < 0 || sizef > UINT64_MAX) { |
182 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, | |
183 | "a non-negative number below 2^64"); | |
184 | return; | |
185 | } | |
7695019b GH |
186 | switch (*postfix) { |
187 | case 'T': | |
188 | sizef *= 1024; | |
0b0404bf | 189 | /* fall through */ |
7695019b GH |
190 | case 'G': |
191 | sizef *= 1024; | |
0b0404bf | 192 | /* fall through */ |
7695019b GH |
193 | case 'M': |
194 | sizef *= 1024; | |
0b0404bf | 195 | /* fall through */ |
7695019b GH |
196 | case 'K': |
197 | case 'k': | |
198 | sizef *= 1024; | |
0b0404bf | 199 | /* fall through */ |
7695019b GH |
200 | case 'b': |
201 | case '\0': | |
202 | *ret = (uint64_t) sizef; | |
203 | break; | |
204 | default: | |
c6bd8c70 | 205 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); |
50b7b000 | 206 | error_append_hint(errp, "You may use k, M, G or T suffixes for " |
543202c0 | 207 | "kilobytes, megabytes, gigabytes and terabytes.\n"); |
ec7b2ccb | 208 | return; |
7695019b GH |
209 | } |
210 | } else { | |
c6bd8c70 | 211 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size"); |
7695019b | 212 | } |
7695019b GH |
213 | } |
214 | ||
7cc07ab8 KW |
215 | bool has_help_option(const char *param) |
216 | { | |
217 | size_t buflen = strlen(param) + 1; | |
96c044af | 218 | char *buf = g_malloc(buflen); |
7cc07ab8 KW |
219 | const char *p = param; |
220 | bool result = false; | |
221 | ||
222 | while (*p) { | |
223 | p = get_opt_value(buf, buflen, p); | |
224 | if (*p) { | |
225 | p++; | |
226 | } | |
227 | ||
228 | if (is_help_option(buf)) { | |
229 | result = true; | |
230 | goto out; | |
231 | } | |
232 | } | |
233 | ||
234 | out: | |
c0462f6d | 235 | g_free(buf); |
7cc07ab8 KW |
236 | return result; |
237 | } | |
238 | ||
239 | bool is_valid_option_list(const char *param) | |
240 | { | |
241 | size_t buflen = strlen(param) + 1; | |
96c044af | 242 | char *buf = g_malloc(buflen); |
7cc07ab8 KW |
243 | const char *p = param; |
244 | bool result = true; | |
245 | ||
246 | while (*p) { | |
247 | p = get_opt_value(buf, buflen, p); | |
248 | if (*p && !*++p) { | |
249 | result = false; | |
250 | goto out; | |
251 | } | |
252 | ||
253 | if (!*buf || *buf == ',') { | |
254 | result = false; | |
255 | goto out; | |
256 | } | |
257 | } | |
258 | ||
259 | out: | |
c0462f6d | 260 | g_free(buf); |
7cc07ab8 KW |
261 | return result; |
262 | } | |
263 | ||
504189a9 CL |
264 | void qemu_opts_print_help(QemuOptsList *list) |
265 | { | |
266 | QemuOptDesc *desc; | |
267 | ||
268 | assert(list); | |
269 | desc = list->desc; | |
270 | printf("Supported options:\n"); | |
271 | while (desc && desc->name) { | |
272 | printf("%-16s %s\n", desc->name, | |
273 | desc->help ? desc->help : "No description available"); | |
274 | desc++; | |
275 | } | |
276 | } | |
e27c88fe GH |
277 | /* ------------------------------------------------------------------ */ |
278 | ||
74c3c197 | 279 | QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
e27c88fe GH |
280 | { |
281 | QemuOpt *opt; | |
282 | ||
dc9ca4ba | 283 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, 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 | ||
782730b0 CL |
331 | /* Get a known option (or its default) and remove it from the list |
332 | * all in one action. Return a malloced string of the option value. | |
333 | * Result must be freed by caller with g_free(). | |
334 | */ | |
335 | char *qemu_opt_get_del(QemuOpts *opts, const char *name) | |
336 | { | |
337 | QemuOpt *opt; | |
338 | const QemuOptDesc *desc; | |
339 | char *str = NULL; | |
340 | ||
341 | if (opts == NULL) { | |
342 | return NULL; | |
343 | } | |
344 | ||
345 | opt = qemu_opt_find(opts, name); | |
346 | if (!opt) { | |
347 | desc = find_desc_by_name(opts->list->desc, name); | |
348 | if (desc && desc->def_value_str) { | |
349 | str = g_strdup(desc->def_value_str); | |
350 | } | |
351 | return str; | |
352 | } | |
353 | str = opt->str; | |
354 | opt->str = NULL; | |
355 | qemu_opt_del_all(opts, name); | |
356 | return str; | |
357 | } | |
358 | ||
c8057f95 PM |
359 | bool qemu_opt_has_help_opt(QemuOpts *opts) |
360 | { | |
361 | QemuOpt *opt; | |
362 | ||
363 | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { | |
364 | if (is_help_option(opt->name)) { | |
365 | return true; | |
366 | } | |
367 | } | |
368 | return false; | |
369 | } | |
370 | ||
782730b0 CL |
371 | static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name, |
372 | bool defval, bool del) | |
e27c88fe | 373 | { |
435db4cf | 374 | QemuOpt *opt; |
782730b0 | 375 | bool ret = defval; |
e27c88fe | 376 | |
435db4cf CL |
377 | if (opts == NULL) { |
378 | return ret; | |
379 | } | |
380 | ||
381 | opt = qemu_opt_find(opts, name); | |
09722032 CL |
382 | if (opt == NULL) { |
383 | const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); | |
384 | if (desc && desc->def_value_str) { | |
782730b0 | 385 | parse_option_bool(name, desc->def_value_str, &ret, &error_abort); |
09722032 | 386 | } |
782730b0 | 387 | return ret; |
09722032 | 388 | } |
e27c88fe | 389 | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
782730b0 CL |
390 | ret = opt->value.boolean; |
391 | if (del) { | |
392 | qemu_opt_del_all(opts, name); | |
393 | } | |
394 | return ret; | |
e27c88fe GH |
395 | } |
396 | ||
782730b0 CL |
397 | bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
398 | { | |
399 | return qemu_opt_get_bool_helper(opts, name, defval, false); | |
400 | } | |
401 | ||
402 | bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval) | |
403 | { | |
404 | return qemu_opt_get_bool_helper(opts, name, defval, true); | |
405 | } | |
406 | ||
407 | static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name, | |
408 | uint64_t defval, bool del) | |
e27c88fe | 409 | { |
435db4cf | 410 | QemuOpt *opt; |
782730b0 | 411 | uint64_t ret = defval; |
e27c88fe | 412 | |
435db4cf CL |
413 | if (opts == NULL) { |
414 | return ret; | |
415 | } | |
416 | ||
417 | opt = qemu_opt_find(opts, name); | |
09722032 CL |
418 | if (opt == NULL) { |
419 | const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); | |
420 | if (desc && desc->def_value_str) { | |
782730b0 | 421 | parse_option_number(name, desc->def_value_str, &ret, &error_abort); |
09722032 | 422 | } |
782730b0 | 423 | return ret; |
09722032 | 424 | } |
e27c88fe | 425 | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
782730b0 CL |
426 | ret = opt->value.uint; |
427 | if (del) { | |
428 | qemu_opt_del_all(opts, name); | |
429 | } | |
430 | return ret; | |
e27c88fe GH |
431 | } |
432 | ||
782730b0 CL |
433 | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
434 | { | |
435 | return qemu_opt_get_number_helper(opts, name, defval, false); | |
436 | } | |
437 | ||
438 | uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name, | |
439 | uint64_t defval) | |
440 | { | |
441 | return qemu_opt_get_number_helper(opts, name, defval, true); | |
442 | } | |
443 | ||
444 | static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name, | |
445 | uint64_t defval, bool del) | |
e27c88fe | 446 | { |
435db4cf | 447 | QemuOpt *opt; |
782730b0 | 448 | uint64_t ret = defval; |
e27c88fe | 449 | |
435db4cf CL |
450 | if (opts == NULL) { |
451 | return ret; | |
452 | } | |
453 | ||
454 | opt = qemu_opt_find(opts, name); | |
09722032 CL |
455 | if (opt == NULL) { |
456 | const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name); | |
457 | if (desc && desc->def_value_str) { | |
782730b0 | 458 | parse_option_size(name, desc->def_value_str, &ret, &error_abort); |
09722032 | 459 | } |
782730b0 | 460 | return ret; |
09722032 | 461 | } |
e27c88fe | 462 | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
782730b0 CL |
463 | ret = opt->value.uint; |
464 | if (del) { | |
465 | qemu_opt_del_all(opts, name); | |
466 | } | |
467 | return ret; | |
468 | } | |
469 | ||
470 | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) | |
471 | { | |
472 | return qemu_opt_get_size_helper(opts, name, defval, false); | |
473 | } | |
474 | ||
475 | uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name, | |
476 | uint64_t defval) | |
477 | { | |
478 | return qemu_opt_get_size_helper(opts, name, defval, true); | |
e27c88fe GH |
479 | } |
480 | ||
6c519404 | 481 | static void qemu_opt_parse(QemuOpt *opt, Error **errp) |
e27c88fe GH |
482 | { |
483 | if (opt->desc == NULL) | |
6c519404 | 484 | return; |
2f39df5b | 485 | |
e27c88fe GH |
486 | switch (opt->desc->type) { |
487 | case QEMU_OPT_STRING: | |
488 | /* nothing */ | |
6c519404 | 489 | return; |
e27c88fe | 490 | case QEMU_OPT_BOOL: |
6c519404 | 491 | parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp); |
cf62adfa | 492 | break; |
e27c88fe | 493 | case QEMU_OPT_NUMBER: |
6c519404 | 494 | parse_option_number(opt->name, opt->str, &opt->value.uint, errp); |
2f39df5b | 495 | break; |
e27c88fe | 496 | case QEMU_OPT_SIZE: |
6c519404 | 497 | parse_option_size(opt->name, opt->str, &opt->value.uint, errp); |
ec7b2ccb | 498 | break; |
e27c88fe GH |
499 | default: |
500 | abort(); | |
501 | } | |
502 | } | |
503 | ||
c474ced8 DXW |
504 | static bool opts_accepts_any(const QemuOpts *opts) |
505 | { | |
506 | return opts->list->desc[0].name == NULL; | |
507 | } | |
508 | ||
0dd6c526 KW |
509 | int qemu_opt_unset(QemuOpts *opts, const char *name) |
510 | { | |
511 | QemuOpt *opt = qemu_opt_find(opts, name); | |
512 | ||
513 | assert(opts_accepts_any(opts)); | |
514 | ||
515 | if (opt == NULL) { | |
516 | return -1; | |
517 | } else { | |
518 | qemu_opt_del(opt); | |
519 | return 0; | |
520 | } | |
521 | } | |
522 | ||
c474ced8 DXW |
523 | static void opt_set(QemuOpts *opts, const char *name, const char *value, |
524 | bool prepend, Error **errp) | |
525 | { | |
526 | QemuOpt *opt; | |
527 | const QemuOptDesc *desc; | |
528 | Error *local_err = NULL; | |
529 | ||
530 | desc = find_desc_by_name(opts->list->desc, name); | |
531 | if (!desc && !opts_accepts_any(opts)) { | |
c6bd8c70 | 532 | error_setg(errp, QERR_INVALID_PARAMETER, name); |
c474ced8 | 533 | return; |
e27c88fe | 534 | } |
dc9ca4ba | 535 | |
7267c094 AL |
536 | opt = g_malloc0(sizeof(*opt)); |
537 | opt->name = g_strdup(name); | |
dc9ca4ba | 538 | opt->opts = opts; |
4f6dd9af JK |
539 | if (prepend) { |
540 | QTAILQ_INSERT_HEAD(&opts->head, opt, next); | |
541 | } else { | |
542 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
543 | } | |
c474ced8 | 544 | opt->desc = desc; |
c64f50d1 | 545 | opt->str = g_strdup(value); |
6c519404 | 546 | qemu_opt_parse(opt, &local_err); |
84d18f06 | 547 | if (local_err) { |
584d4064 | 548 | error_propagate(errp, local_err); |
e27c88fe | 549 | qemu_opt_del(opt); |
e27c88fe | 550 | } |
e27c88fe GH |
551 | } |
552 | ||
f43e47db MA |
553 | void qemu_opt_set(QemuOpts *opts, const char *name, const char *value, |
554 | Error **errp) | |
384f2139 LC |
555 | { |
556 | opt_set(opts, name, value, false, errp); | |
557 | } | |
558 | ||
cccb7967 MA |
559 | void qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val, |
560 | Error **errp) | |
f02b77c9 MK |
561 | { |
562 | QemuOpt *opt; | |
563 | const QemuOptDesc *desc = opts->list->desc; | |
f02b77c9 | 564 | |
ad718d01 DXW |
565 | opt = g_malloc0(sizeof(*opt)); |
566 | opt->desc = find_desc_by_name(desc, name); | |
567 | if (!opt->desc && !opts_accepts_any(opts)) { | |
c6bd8c70 | 568 | error_setg(errp, QERR_INVALID_PARAMETER, name); |
ad718d01 | 569 | g_free(opt); |
cccb7967 | 570 | return; |
f02b77c9 MK |
571 | } |
572 | ||
f02b77c9 MK |
573 | opt->name = g_strdup(name); |
574 | opt->opts = opts; | |
f02b77c9 | 575 | opt->value.boolean = !!val; |
ad718d01 DXW |
576 | opt->str = g_strdup(val ? "on" : "off"); |
577 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
f02b77c9 MK |
578 | } |
579 | ||
39101f25 MA |
580 | void qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val, |
581 | Error **errp) | |
b83c18e2 DXW |
582 | { |
583 | QemuOpt *opt; | |
584 | const QemuOptDesc *desc = opts->list->desc; | |
585 | ||
586 | opt = g_malloc0(sizeof(*opt)); | |
587 | opt->desc = find_desc_by_name(desc, name); | |
588 | if (!opt->desc && !opts_accepts_any(opts)) { | |
c6bd8c70 | 589 | error_setg(errp, QERR_INVALID_PARAMETER, name); |
b83c18e2 | 590 | g_free(opt); |
39101f25 | 591 | return; |
b83c18e2 DXW |
592 | } |
593 | ||
594 | opt->name = g_strdup(name); | |
595 | opt->opts = opts; | |
596 | opt->value.uint = val; | |
597 | opt->str = g_strdup_printf("%" PRId64, val); | |
598 | QTAILQ_INSERT_TAIL(&opts->head, opt, next); | |
b83c18e2 DXW |
599 | } |
600 | ||
1640b200 | 601 | /** |
71df1d83 MA |
602 | * For each member of @opts, call @func(@opaque, name, value, @errp). |
603 | * @func() may store an Error through @errp, but must return non-zero then. | |
1640b200 MA |
604 | * When @func() returns non-zero, break the loop and return that value. |
605 | * Return zero when the loop completes. | |
606 | */ | |
71df1d83 MA |
607 | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
608 | Error **errp) | |
48026075 GH |
609 | { |
610 | QemuOpt *opt; | |
1640b200 | 611 | int rc; |
48026075 | 612 | |
72cf2d4f | 613 | QTAILQ_FOREACH(opt, &opts->head, next) { |
71df1d83 | 614 | rc = func(opaque, opt->name, opt->str, errp); |
1640b200 MA |
615 | if (rc) { |
616 | return rc; | |
617 | } | |
71df1d83 | 618 | assert(!errp || !*errp); |
48026075 | 619 | } |
1640b200 | 620 | return 0; |
48026075 GH |
621 | } |
622 | ||
e27c88fe GH |
623 | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
624 | { | |
625 | QemuOpts *opts; | |
626 | ||
72cf2d4f | 627 | QTAILQ_FOREACH(opts, &list->head, next) { |
96bc97eb MA |
628 | if (!opts->id && !id) { |
629 | return opts; | |
e27c88fe | 630 | } |
96bc97eb MA |
631 | if (opts->id && id && !strcmp(opts->id, id)) { |
632 | return opts; | |
e27c88fe | 633 | } |
e27c88fe GH |
634 | } |
635 | return NULL; | |
636 | } | |
637 | ||
8be7e7e4 LC |
638 | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, |
639 | int fail_if_exists, Error **errp) | |
e27c88fe GH |
640 | { |
641 | QemuOpts *opts = NULL; | |
642 | ||
643 | if (id) { | |
f5bebbbb | 644 | if (!id_wellformed(id)) { |
c6bd8c70 MA |
645 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", |
646 | "an identifier"); | |
50b7b000 | 647 | error_append_hint(errp, "Identifiers consist of letters, digits, " |
543202c0 | 648 | "'-', '.', '_', starting with a letter.\n"); |
b560a9ab MA |
649 | return NULL; |
650 | } | |
e27c88fe GH |
651 | opts = qemu_opts_find(list, id); |
652 | if (opts != NULL) { | |
da93318a | 653 | if (fail_if_exists && !list->merge_lists) { |
f231b88d | 654 | error_setg(errp, "Duplicate ID '%s' for %s", id, list->name); |
e27c88fe GH |
655 | return NULL; |
656 | } else { | |
657 | return opts; | |
658 | } | |
659 | } | |
da93318a PM |
660 | } else if (list->merge_lists) { |
661 | opts = qemu_opts_find(list, NULL); | |
662 | if (opts) { | |
663 | return opts; | |
664 | } | |
e27c88fe | 665 | } |
7267c094 | 666 | opts = g_malloc0(sizeof(*opts)); |
c64f50d1 | 667 | opts->id = g_strdup(id); |
e27c88fe | 668 | opts->list = list; |
827b0813 | 669 | loc_save(&opts->loc); |
72cf2d4f BS |
670 | QTAILQ_INIT(&opts->head); |
671 | QTAILQ_INSERT_TAIL(&list->head, opts, next); | |
e27c88fe GH |
672 | return opts; |
673 | } | |
674 | ||
bb67ab02 MA |
675 | void qemu_opts_reset(QemuOptsList *list) |
676 | { | |
677 | QemuOpts *opts, *next_opts; | |
678 | ||
679 | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { | |
680 | qemu_opts_del(opts); | |
681 | } | |
682 | } | |
683 | ||
94ac7268 MA |
684 | void qemu_opts_loc_restore(QemuOpts *opts) |
685 | { | |
686 | loc_restore(&opts->loc); | |
687 | } | |
688 | ||
79087c78 MA |
689 | void qemu_opts_set(QemuOptsList *list, const char *id, |
690 | const char *name, const char *value, Error **errp) | |
e27c88fe GH |
691 | { |
692 | QemuOpts *opts; | |
8be7e7e4 | 693 | Error *local_err = NULL; |
e27c88fe | 694 | |
8be7e7e4 | 695 | opts = qemu_opts_create(list, id, 1, &local_err); |
84d18f06 | 696 | if (local_err) { |
79087c78 MA |
697 | error_propagate(errp, local_err); |
698 | return; | |
e27c88fe | 699 | } |
f43e47db | 700 | qemu_opt_set(opts, name, value, errp); |
e27c88fe GH |
701 | } |
702 | ||
48026075 GH |
703 | const char *qemu_opts_id(QemuOpts *opts) |
704 | { | |
705 | return opts->id; | |
706 | } | |
707 | ||
326642bc KW |
708 | /* The id string will be g_free()d by qemu_opts_del */ |
709 | void qemu_opts_set_id(QemuOpts *opts, char *id) | |
710 | { | |
711 | opts->id = id; | |
712 | } | |
713 | ||
e27c88fe GH |
714 | void qemu_opts_del(QemuOpts *opts) |
715 | { | |
716 | QemuOpt *opt; | |
717 | ||
4782183d CL |
718 | if (opts == NULL) { |
719 | return; | |
720 | } | |
721 | ||
e27c88fe | 722 | for (;;) { |
72cf2d4f | 723 | opt = QTAILQ_FIRST(&opts->head); |
e27c88fe GH |
724 | if (opt == NULL) |
725 | break; | |
726 | qemu_opt_del(opt); | |
727 | } | |
72cf2d4f | 728 | QTAILQ_REMOVE(&opts->list->head, opts, next); |
7267c094 AL |
729 | g_free(opts->id); |
730 | g_free(opts); | |
e27c88fe GH |
731 | } |
732 | ||
fe646693 KZ |
733 | /* print value, escaping any commas in value */ |
734 | static void escaped_print(const char *value) | |
735 | { | |
736 | const char *ptr; | |
737 | ||
738 | for (ptr = value; *ptr; ++ptr) { | |
739 | if (*ptr == ',') { | |
740 | putchar(','); | |
741 | } | |
742 | putchar(*ptr); | |
743 | } | |
744 | } | |
745 | ||
746 | void qemu_opts_print(QemuOpts *opts, const char *separator) | |
e27c88fe GH |
747 | { |
748 | QemuOpt *opt; | |
09722032 | 749 | QemuOptDesc *desc = opts->list->desc; |
fe646693 KZ |
750 | const char *sep = ""; |
751 | ||
752 | if (opts->id) { | |
753 | printf("id=%s", opts->id); /* passed id_wellformed -> no commas */ | |
754 | sep = separator; | |
755 | } | |
e27c88fe | 756 | |
09722032 CL |
757 | if (desc[0].name == NULL) { |
758 | QTAILQ_FOREACH(opt, &opts->head, next) { | |
fe646693 KZ |
759 | printf("%s%s=", sep, opt->name); |
760 | escaped_print(opt->str); | |
761 | sep = separator; | |
09722032 CL |
762 | } |
763 | return; | |
764 | } | |
765 | for (; desc && desc->name; desc++) { | |
766 | const char *value; | |
767 | QemuOpt *opt = qemu_opt_find(opts, desc->name); | |
768 | ||
769 | value = opt ? opt->str : desc->def_value_str; | |
770 | if (!value) { | |
771 | continue; | |
772 | } | |
773 | if (desc->type == QEMU_OPT_STRING) { | |
fe646693 KZ |
774 | printf("%s%s=", sep, desc->name); |
775 | escaped_print(value); | |
09722032 CL |
776 | } else if ((desc->type == QEMU_OPT_SIZE || |
777 | desc->type == QEMU_OPT_NUMBER) && opt) { | |
43c5d8f8 | 778 | printf("%s%s=%" PRId64, sep, desc->name, opt->value.uint); |
09722032 | 779 | } else { |
43c5d8f8 | 780 | printf("%s%s=%s", sep, desc->name, value); |
09722032 | 781 | } |
fe646693 | 782 | sep = separator; |
e27c88fe | 783 | } |
e27c88fe GH |
784 | } |
785 | ||
d93ae3cf MA |
786 | static void opts_do_parse(QemuOpts *opts, const char *params, |
787 | const char *firstname, bool prepend, Error **errp) | |
e27c88fe | 788 | { |
d318ff99 | 789 | char option[128], value[1024]; |
e27c88fe | 790 | const char *p,*pe,*pc; |
584d4064 | 791 | Error *local_err = NULL; |
e27c88fe | 792 | |
2cfa571f | 793 | for (p = params; *p != '\0'; p++) { |
e27c88fe GH |
794 | pe = strchr(p, '='); |
795 | pc = strchr(p, ','); | |
796 | if (!pe || (pc && pc < pe)) { | |
797 | /* found "foo,more" */ | |
798 | if (p == params && firstname) { | |
799 | /* implicitly named first option */ | |
800 | pstrcpy(option, sizeof(option), firstname); | |
801 | p = get_opt_value(value, sizeof(value), p); | |
802 | } else { | |
803 | /* option without value, probably a flag */ | |
804 | p = get_opt_name(option, sizeof(option), p, ','); | |
96729cbd | 805 | if (strncmp(option, "no", 2) == 0) { |
e27c88fe GH |
806 | memmove(option, option+2, strlen(option+2)+1); |
807 | pstrcpy(value, sizeof(value), "off"); | |
808 | } else { | |
809 | pstrcpy(value, sizeof(value), "on"); | |
810 | } | |
811 | } | |
812 | } else { | |
813 | /* found "foo=bar,more" */ | |
814 | p = get_opt_name(option, sizeof(option), p, '='); | |
815 | if (*p != '=') { | |
816 | break; | |
817 | } | |
818 | p++; | |
819 | p = get_opt_value(value, sizeof(value), p); | |
820 | } | |
821 | if (strcmp(option, "id") != 0) { | |
822 | /* store and parse */ | |
584d4064 | 823 | opt_set(opts, option, value, prepend, &local_err); |
84d18f06 | 824 | if (local_err) { |
d93ae3cf MA |
825 | error_propagate(errp, local_err); |
826 | return; | |
e27c88fe GH |
827 | } |
828 | } | |
829 | if (*p != ',') { | |
830 | break; | |
831 | } | |
e27c88fe | 832 | } |
96729cbd GH |
833 | } |
834 | ||
dc523cd3 MA |
835 | /** |
836 | * Store options parsed from @params into @opts. | |
837 | * If @firstname is non-null, the first key=value in @params may omit | |
838 | * key=, and is treated as if key was @firstname. | |
839 | * On error, store an error object through @errp if non-null. | |
840 | */ | |
841 | void qemu_opts_do_parse(QemuOpts *opts, const char *params, | |
842 | const char *firstname, Error **errp) | |
4f6dd9af | 843 | { |
dc523cd3 | 844 | opts_do_parse(opts, params, firstname, false, errp); |
4f6dd9af JK |
845 | } |
846 | ||
847 | static QemuOpts *opts_parse(QemuOptsList *list, const char *params, | |
70b94331 | 848 | bool permit_abbrev, bool defaults, Error **errp) |
96729cbd | 849 | { |
8212c64f | 850 | const char *firstname; |
d318ff99 | 851 | char value[1024], *id = NULL; |
96729cbd GH |
852 | const char *p; |
853 | QemuOpts *opts; | |
8be7e7e4 | 854 | Error *local_err = NULL; |
96729cbd | 855 | |
8212c64f MA |
856 | assert(!permit_abbrev || list->implied_opt_name); |
857 | firstname = permit_abbrev ? list->implied_opt_name : NULL; | |
858 | ||
96729cbd GH |
859 | if (strncmp(params, "id=", 3) == 0) { |
860 | get_opt_value(value, sizeof(value), params+3); | |
d510c5cf | 861 | id = value; |
96729cbd GH |
862 | } else if ((p = strstr(params, ",id=")) != NULL) { |
863 | get_opt_value(value, sizeof(value), p+4); | |
d510c5cf | 864 | id = value; |
96729cbd | 865 | } |
cb77d192 MA |
866 | |
867 | /* | |
868 | * This code doesn't work for defaults && !list->merge_lists: when | |
869 | * params has no id=, and list has an element with !opts->id, it | |
870 | * appends a new element instead of returning the existing opts. | |
871 | * However, we got no use for this case. Guard against possible | |
872 | * (if unlikely) future misuse: | |
873 | */ | |
874 | assert(!defaults || list->merge_lists); | |
6d4cd408 | 875 | opts = qemu_opts_create(list, id, !defaults, &local_err); |
8be7e7e4 | 876 | if (opts == NULL) { |
4f81273d | 877 | error_propagate(errp, local_err); |
96729cbd | 878 | return NULL; |
8be7e7e4 | 879 | } |
96729cbd | 880 | |
d93ae3cf MA |
881 | opts_do_parse(opts, params, firstname, defaults, &local_err); |
882 | if (local_err) { | |
4f81273d | 883 | error_propagate(errp, local_err); |
96729cbd GH |
884 | qemu_opts_del(opts); |
885 | return NULL; | |
886 | } | |
887 | ||
e27c88fe GH |
888 | return opts; |
889 | } | |
890 | ||
4f81273d MA |
891 | /** |
892 | * Create a QemuOpts in @list and with options parsed from @params. | |
893 | * If @permit_abbrev, the first key=value in @params may omit key=, | |
894 | * and is treated as if key was @list->implied_opt_name. | |
70b94331 | 895 | * On error, store an error object through @errp if non-null. |
4f81273d MA |
896 | * Return the new QemuOpts on success, null pointer on error. |
897 | */ | |
4f6dd9af | 898 | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
70b94331 MA |
899 | bool permit_abbrev, Error **errp) |
900 | { | |
901 | return opts_parse(list, params, permit_abbrev, false, errp); | |
902 | } | |
903 | ||
904 | /** | |
905 | * Create a QemuOpts in @list and with options parsed from @params. | |
906 | * If @permit_abbrev, the first key=value in @params may omit key=, | |
907 | * and is treated as if key was @list->implied_opt_name. | |
908 | * Report errors with error_report_err(). This is inappropriate in | |
909 | * QMP context. Do not use this function there! | |
910 | * Return the new QemuOpts on success, null pointer on error. | |
911 | */ | |
912 | QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list, const char *params, | |
913 | bool permit_abbrev) | |
4f6dd9af | 914 | { |
4f81273d MA |
915 | Error *err = NULL; |
916 | QemuOpts *opts; | |
917 | ||
918 | opts = opts_parse(list, params, permit_abbrev, false, &err); | |
70b94331 MA |
919 | if (err) { |
920 | error_report_err(err); | |
4f81273d MA |
921 | } |
922 | return opts; | |
4f6dd9af JK |
923 | } |
924 | ||
925 | void qemu_opts_set_defaults(QemuOptsList *list, const char *params, | |
926 | int permit_abbrev) | |
927 | { | |
928 | QemuOpts *opts; | |
929 | ||
4f81273d | 930 | opts = opts_parse(list, params, permit_abbrev, true, NULL); |
4f6dd9af JK |
931 | assert(opts); |
932 | } | |
933 | ||
4e89978e LC |
934 | typedef struct OptsFromQDictState { |
935 | QemuOpts *opts; | |
936 | Error **errp; | |
937 | } OptsFromQDictState; | |
938 | ||
01e7f188 MA |
939 | static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) |
940 | { | |
4e89978e | 941 | OptsFromQDictState *state = opaque; |
01e7f188 MA |
942 | char buf[32]; |
943 | const char *value; | |
944 | int n; | |
945 | ||
2767ceec | 946 | if (!strcmp(key, "id") || *state->errp) { |
01e7f188 MA |
947 | return; |
948 | } | |
949 | ||
950 | switch (qobject_type(obj)) { | |
951 | case QTYPE_QSTRING: | |
952 | value = qstring_get_str(qobject_to_qstring(obj)); | |
953 | break; | |
954 | case QTYPE_QINT: | |
955 | n = snprintf(buf, sizeof(buf), "%" PRId64, | |
956 | qint_get_int(qobject_to_qint(obj))); | |
957 | assert(n < sizeof(buf)); | |
958 | value = buf; | |
959 | break; | |
960 | case QTYPE_QFLOAT: | |
961 | n = snprintf(buf, sizeof(buf), "%.17g", | |
962 | qfloat_get_double(qobject_to_qfloat(obj))); | |
963 | assert(n < sizeof(buf)); | |
964 | value = buf; | |
965 | break; | |
966 | case QTYPE_QBOOL: | |
d35215f8 | 967 | pstrcpy(buf, sizeof(buf), |
fc48ffc3 | 968 | qbool_get_bool(qobject_to_qbool(obj)) ? "on" : "off"); |
01e7f188 MA |
969 | value = buf; |
970 | break; | |
971 | default: | |
972 | return; | |
973 | } | |
4e89978e | 974 | |
f43e47db | 975 | qemu_opt_set(state->opts, key, value, state->errp); |
01e7f188 MA |
976 | } |
977 | ||
978 | /* | |
979 | * Create QemuOpts from a QDict. | |
980 | * Use value of key "id" as ID if it exists and is a QString. | |
981 | * Only QStrings, QInts, QFloats and QBools are copied. Entries with | |
982 | * other types are silently ignored. | |
983 | */ | |
4e89978e LC |
984 | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict, |
985 | Error **errp) | |
01e7f188 | 986 | { |
4e89978e | 987 | OptsFromQDictState state; |
8be7e7e4 | 988 | Error *local_err = NULL; |
4e89978e | 989 | QemuOpts *opts; |
01e7f188 | 990 | |
8be7e7e4 LC |
991 | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1, |
992 | &local_err); | |
84d18f06 | 993 | if (local_err) { |
4e89978e | 994 | error_propagate(errp, local_err); |
01e7f188 | 995 | return NULL; |
8be7e7e4 | 996 | } |
01e7f188 | 997 | |
8be7e7e4 | 998 | assert(opts != NULL); |
4e89978e LC |
999 | |
1000 | state.errp = &local_err; | |
1001 | state.opts = opts; | |
1002 | qdict_iter(qdict, qemu_opts_from_qdict_1, &state); | |
84d18f06 | 1003 | if (local_err) { |
4e89978e LC |
1004 | error_propagate(errp, local_err); |
1005 | qemu_opts_del(opts); | |
1006 | return NULL; | |
1007 | } | |
1008 | ||
01e7f188 MA |
1009 | return opts; |
1010 | } | |
1011 | ||
376609cc KW |
1012 | /* |
1013 | * Adds all QDict entries to the QemuOpts that can be added and removes them | |
1014 | * from the QDict. When this function returns, the QDict contains only those | |
1015 | * entries that couldn't be added to the QemuOpts. | |
1016 | */ | |
1017 | void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp) | |
1018 | { | |
1019 | const QDictEntry *entry, *next; | |
1020 | ||
1021 | entry = qdict_first(qdict); | |
1022 | ||
1023 | while (entry != NULL) { | |
1024 | Error *local_err = NULL; | |
1025 | OptsFromQDictState state = { | |
1026 | .errp = &local_err, | |
1027 | .opts = opts, | |
1028 | }; | |
1029 | ||
1030 | next = qdict_next(qdict, entry); | |
1031 | ||
1032 | if (find_desc_by_name(opts->list->desc, entry->key)) { | |
1033 | qemu_opts_from_qdict_1(entry->key, entry->value, &state); | |
84d18f06 | 1034 | if (local_err) { |
376609cc KW |
1035 | error_propagate(errp, local_err); |
1036 | return; | |
1037 | } else { | |
1038 | qdict_del(qdict, entry->key); | |
1039 | } | |
1040 | } | |
1041 | ||
1042 | entry = next; | |
1043 | } | |
1044 | } | |
1045 | ||
01e7f188 MA |
1046 | /* |
1047 | * Convert from QemuOpts to QDict. | |
1048 | * The QDict values are of type QString. | |
1049 | * TODO We'll want to use types appropriate for opt->desc->type, but | |
1050 | * this is enough for now. | |
1051 | */ | |
1052 | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) | |
1053 | { | |
1054 | QemuOpt *opt; | |
1055 | QObject *val; | |
1056 | ||
1057 | if (!qdict) { | |
1058 | qdict = qdict_new(); | |
1059 | } | |
1060 | if (opts->id) { | |
1061 | qdict_put(qdict, "id", qstring_from_str(opts->id)); | |
1062 | } | |
1063 | QTAILQ_FOREACH(opt, &opts->head, next) { | |
1064 | val = QOBJECT(qstring_from_str(opt->str)); | |
1065 | qdict_put_obj(qdict, opt->name, val); | |
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; |
a4c7367f | 1107 | int rc; |
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 MA |
1113 | if (rc) { |
1114 | return rc; | |
1115 | } | |
28d0de7a | 1116 | assert(!errp || !*errp); |
e27c88fe | 1117 | } |
827b0813 | 1118 | loc_pop(&loc); |
a4c7367f | 1119 | return 0; |
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 | } |