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