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