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