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