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