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