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