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