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