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