]> git.proxmox.com Git - qemu.git/blame - qemu-option.c
qemu-option: qemu_opts_validate(): fix duplicated code
[qemu.git] / 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"
827b0813 30#include "qemu-error.h"
01e7f188 31#include "qemu-objects.h"
8be7e7e4 32#include "error.h"
975b63a4 33#include "qerror.h"
fdb17976 34#include "qemu-option-internal.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
126int check_params(char *buf, int buf_size,
127 const char * const *params, const char *str)
128{
129 const char *p;
130 int i;
131
132 p = str;
133 while (*p != '\0') {
134 p = get_opt_name(buf, buf_size, p, '=');
135 if (*p != '=') {
136 return -1;
137 }
138 p++;
139 for (i = 0; params[i] != NULL; i++) {
140 if (!strcmp(params[i], buf)) {
141 break;
142 }
143 }
144 if (params[i] == NULL) {
145 return -1;
146 }
147 p = get_opt_value(NULL, 0, p);
148 if (*p != ',') {
149 break;
150 }
151 p++;
152 }
153 return 0;
154}
155
d3f24367
KW
156/*
157 * Searches an option list for an option with the given name
158 */
159QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
160 const char *name)
161{
162 while (list && list->name) {
163 if (!strcmp(list->name, name)) {
164 return list;
165 }
166 list++;
167 }
168
169 return NULL;
170}
171
cf62adfa
LC
172static void parse_option_bool(const char *name, const char *value, bool *ret,
173 Error **errp)
67b1355b
GH
174{
175 if (value != NULL) {
176 if (!strcmp(value, "on")) {
177 *ret = 1;
178 } else if (!strcmp(value, "off")) {
179 *ret = 0;
180 } else {
cf62adfa 181 error_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
67b1355b
GH
182 }
183 } else {
184 *ret = 1;
185 }
67b1355b
GH
186}
187
2f39df5b
LC
188static void parse_option_number(const char *name, const char *value,
189 uint64_t *ret, Error **errp)
e27c88fe
GH
190{
191 char *postfix;
192 uint64_t number;
193
194 if (value != NULL) {
195 number = strtoull(value, &postfix, 0);
196 if (*postfix != '\0') {
2f39df5b
LC
197 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
198 return;
e27c88fe 199 }
21c9f4cd 200 *ret = number;
e27c88fe 201 } else {
2f39df5b 202 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
e27c88fe 203 }
e27c88fe
GH
204}
205
ec7b2ccb
LC
206static void parse_option_size(const char *name, const char *value,
207 uint64_t *ret, Error **errp)
7695019b
GH
208{
209 char *postfix;
210 double sizef;
211
212 if (value != NULL) {
213 sizef = strtod(value, &postfix);
214 switch (*postfix) {
215 case 'T':
216 sizef *= 1024;
0b0404bf 217 /* fall through */
7695019b
GH
218 case 'G':
219 sizef *= 1024;
0b0404bf 220 /* fall through */
7695019b
GH
221 case 'M':
222 sizef *= 1024;
0b0404bf 223 /* fall through */
7695019b
GH
224 case 'K':
225 case 'k':
226 sizef *= 1024;
0b0404bf 227 /* fall through */
7695019b
GH
228 case 'b':
229 case '\0':
230 *ret = (uint64_t) sizef;
231 break;
232 default:
ec7b2ccb 233 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
c64f27d4 234 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
7695019b 235 "kilobytes, megabytes, gigabytes and terabytes.\n");
ec7b2ccb 236 return;
7695019b
GH
237 }
238 } else {
ec7b2ccb 239 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
7695019b 240 }
7695019b
GH
241}
242
d3f24367
KW
243/*
244 * Sets the value of a parameter in a given option list. The parsing of the
245 * value depends on the type of option:
246 *
247 * OPT_FLAG (uses value.n):
248 * If no value is given, the flag is set to 1.
249 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
250 *
251 * OPT_STRING (uses value.s):
252 * value is strdup()ed and assigned as option value
253 *
254 * OPT_SIZE (uses value.n):
255 * The value is converted to an integer. Suffixes for kilobytes etc. are
256 * allowed (powers of 1024).
257 *
258 * Returns 0 on succes, -1 in error cases
259 */
260int set_option_parameter(QEMUOptionParameter *list, const char *name,
261 const char *value)
262{
f02b77c9 263 bool flag;
cf62adfa 264 Error *local_err = NULL;
67b1355b 265
d3f24367
KW
266 // Find a matching parameter
267 list = get_option_parameter(list, name);
268 if (list == NULL) {
269 fprintf(stderr, "Unknown option '%s'\n", name);
270 return -1;
271 }
272
273 // Process parameter
274 switch (list->type) {
275 case OPT_FLAG:
cf62adfa
LC
276 parse_option_bool(name, value, &flag, &local_err);
277 if (!error_is_set(&local_err)) {
278 list->value.n = flag;
279 }
d3f24367
KW
280 break;
281
282 case OPT_STRING:
283 if (value != NULL) {
7267c094 284 list->value.s = g_strdup(value);
d3f24367
KW
285 } else {
286 fprintf(stderr, "Option '%s' needs a parameter\n", name);
287 return -1;
288 }
289 break;
290
291 case OPT_SIZE:
ec7b2ccb 292 parse_option_size(name, value, &list->value.n, &local_err);
d3f24367 293 break;
7695019b 294
d3f24367
KW
295 default:
296 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
297 return -1;
298 }
299
cf62adfa
LC
300 if (error_is_set(&local_err)) {
301 qerror_report_err(local_err);
302 error_free(local_err);
303 return -1;
304 }
305
d3f24367
KW
306 return 0;
307}
308
309/*
310 * Sets the given parameter to an integer instead of a string.
311 * This function cannot be used to set string options.
312 *
313 * Returns 0 on success, -1 in error cases
314 */
315int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
316 uint64_t value)
317{
318 // Find a matching parameter
319 list = get_option_parameter(list, name);
320 if (list == NULL) {
321 fprintf(stderr, "Unknown option '%s'\n", name);
322 return -1;
323 }
324
325 // Process parameter
326 switch (list->type) {
327 case OPT_FLAG:
328 case OPT_NUMBER:
329 case OPT_SIZE:
330 list->value.n = value;
331 break;
332
333 default:
334 return -1;
335 }
336
337 return 0;
338}
339
340/*
341 * Frees a option list. If it contains strings, the strings are freed as well.
342 */
343void free_option_parameters(QEMUOptionParameter *list)
344{
345 QEMUOptionParameter *cur = list;
346
347 while (cur && cur->name) {
348 if (cur->type == OPT_STRING) {
7267c094 349 g_free(cur->value.s);
d3f24367
KW
350 }
351 cur++;
352 }
353
7267c094 354 g_free(list);
d3f24367
KW
355}
356
b50cbabc
MK
357/*
358 * Count valid options in list
359 */
360static size_t count_option_parameters(QEMUOptionParameter *list)
361{
362 size_t num_options = 0;
363
364 while (list && list->name) {
365 num_options++;
366 list++;
367 }
368
369 return num_options;
370}
371
372/*
373 * Append an option list (list) to an option list (dest).
374 *
375 * If dest is NULL, a new copy of list is created.
376 *
377 * Returns a pointer to the first element of dest (or the newly allocated copy)
378 */
379QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
380 QEMUOptionParameter *list)
381{
382 size_t num_options, num_dest_options;
383
384 num_options = count_option_parameters(dest);
385 num_dest_options = num_options;
386
387 num_options += count_option_parameters(list);
388
7267c094 389 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
bd69fe84 390 dest[num_dest_options].name = NULL;
b50cbabc
MK
391
392 while (list && list->name) {
393 if (get_option_parameter(dest, list->name) == NULL) {
394 dest[num_dest_options++] = *list;
395 dest[num_dest_options].name = NULL;
396 }
397 list++;
398 }
399
400 return dest;
401}
402
d3f24367
KW
403/*
404 * Parses a parameter string (param) into an option list (dest).
405 *
0e72e753
SH
406 * list is the template option list. If dest is NULL, a new copy of list is
407 * created. If list is NULL, this function fails.
d3f24367
KW
408 *
409 * A parameter string consists of one or more parameters, separated by commas.
410 * Each parameter consists of its name and possibly of a value. In the latter
411 * case, the value is delimited by an = character. To specify a value which
412 * contains commas, double each comma so it won't be recognized as the end of
413 * the parameter.
414 *
415 * For more details of the parsing see above.
416 *
417 * Returns a pointer to the first element of dest (or the newly allocated copy)
418 * or NULL in error cases
419 */
420QEMUOptionParameter *parse_option_parameters(const char *param,
421 QEMUOptionParameter *list, QEMUOptionParameter *dest)
422{
d3f24367
KW
423 QEMUOptionParameter *allocated = NULL;
424 char name[256];
425 char value[256];
426 char *param_delim, *value_delim;
427 char next_delim;
d3f24367
KW
428
429 if (list == NULL) {
430 return NULL;
431 }
432
433 if (dest == NULL) {
898c257b 434 dest = allocated = append_option_parameters(NULL, list);
d3f24367
KW
435 }
436
437 while (*param) {
438
439 // Find parameter name and value in the string
440 param_delim = strchr(param, ',');
441 value_delim = strchr(param, '=');
442
443 if (value_delim && (value_delim < param_delim || !param_delim)) {
444 next_delim = '=';
445 } else {
446 next_delim = ',';
447 value_delim = NULL;
448 }
449
450 param = get_opt_name(name, sizeof(name), param, next_delim);
451 if (value_delim) {
452 param = get_opt_value(value, sizeof(value), param + 1);
453 }
454 if (*param != '\0') {
455 param++;
456 }
457
458 // Set the parameter
459 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
460 goto fail;
461 }
462 }
463
464 return dest;
465
466fail:
467 // Only free the list if it was newly allocated
468 free_option_parameters(allocated);
469 return NULL;
470}
471
472/*
473 * Prints all options of a list that have a value to stdout
474 */
475void print_option_parameters(QEMUOptionParameter *list)
476{
477 while (list && list->name) {
478 switch (list->type) {
479 case OPT_STRING:
480 if (list->value.s != NULL) {
481 printf("%s='%s' ", list->name, list->value.s);
482 }
483 break;
484 case OPT_FLAG:
485 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
486 break;
487 case OPT_SIZE:
488 case OPT_NUMBER:
489 printf("%s=%" PRId64 " ", list->name, list->value.n);
490 break;
491 default:
07f35073 492 printf("%s=(unknown type) ", list->name);
d3f24367
KW
493 break;
494 }
495 list++;
496 }
497}
db08adf5
KW
498
499/*
500 * Prints an overview of all available options
501 */
502void print_option_help(QEMUOptionParameter *list)
503{
504 printf("Supported options:\n");
505 while (list && list->name) {
506 printf("%-16s %s\n", list->name,
507 list->help ? list->help : "No description available");
508 list++;
509 }
510}
e27c88fe
GH
511
512/* ------------------------------------------------------------------ */
513
e27c88fe
GH
514static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
515{
516 QemuOpt *opt;
517
dc9ca4ba 518 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
e27c88fe
GH
519 if (strcmp(opt->name, name) != 0)
520 continue;
521 return opt;
522 }
523 return NULL;
524}
525
526const char *qemu_opt_get(QemuOpts *opts, const char *name)
527{
528 QemuOpt *opt = qemu_opt_find(opts, name);
529 return opt ? opt->str : NULL;
530}
531
c8057f95
PM
532bool qemu_opt_has_help_opt(QemuOpts *opts)
533{
534 QemuOpt *opt;
535
536 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
537 if (is_help_option(opt->name)) {
538 return true;
539 }
540 }
541 return false;
542}
543
f02b77c9 544bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
e27c88fe
GH
545{
546 QemuOpt *opt = qemu_opt_find(opts, name);
547
548 if (opt == NULL)
549 return defval;
550 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
1f5c1775 551 return opt->value.boolean;
e27c88fe
GH
552}
553
554uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
555{
556 QemuOpt *opt = qemu_opt_find(opts, name);
557
558 if (opt == NULL)
559 return defval;
560 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
561 return opt->value.uint;
562}
563
564uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
565{
566 QemuOpt *opt = qemu_opt_find(opts, name);
567
568 if (opt == NULL)
569 return defval;
570 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
571 return opt->value.uint;
572}
573
6c519404 574static void qemu_opt_parse(QemuOpt *opt, Error **errp)
e27c88fe
GH
575{
576 if (opt->desc == NULL)
6c519404 577 return;
2f39df5b 578
e27c88fe
GH
579 switch (opt->desc->type) {
580 case QEMU_OPT_STRING:
581 /* nothing */
6c519404 582 return;
e27c88fe 583 case QEMU_OPT_BOOL:
6c519404 584 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
cf62adfa 585 break;
e27c88fe 586 case QEMU_OPT_NUMBER:
6c519404 587 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
2f39df5b 588 break;
e27c88fe 589 case QEMU_OPT_SIZE:
6c519404 590 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
ec7b2ccb 591 break;
e27c88fe
GH
592 default:
593 abort();
594 }
595}
596
597static void qemu_opt_del(QemuOpt *opt)
598{
72cf2d4f 599 QTAILQ_REMOVE(&opt->opts->head, opt, next);
7267c094
AL
600 g_free((/* !const */ char*)opt->name);
601 g_free((/* !const */ char*)opt->str);
602 g_free(opt);
e27c88fe
GH
603}
604
c474ced8
DXW
605static bool opts_accepts_any(const QemuOpts *opts)
606{
607 return opts->list->desc[0].name == NULL;
608}
609
610static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
611 const char *name)
e27c88fe 612{
dc9ca4ba 613 int i;
e27c88fe 614
dc9ca4ba
MM
615 for (i = 0; desc[i].name != NULL; i++) {
616 if (strcmp(desc[i].name, name) == 0) {
c474ced8 617 return &desc[i];
e27c88fe 618 }
dc9ca4ba 619 }
c474ced8
DXW
620
621 return NULL;
622}
623
624static void opt_set(QemuOpts *opts, const char *name, const char *value,
625 bool prepend, Error **errp)
626{
627 QemuOpt *opt;
628 const QemuOptDesc *desc;
629 Error *local_err = NULL;
630
631 desc = find_desc_by_name(opts->list->desc, name);
632 if (!desc && !opts_accepts_any(opts)) {
633 error_set(errp, QERR_INVALID_PARAMETER, name);
634 return;
e27c88fe 635 }
dc9ca4ba 636
7267c094
AL
637 opt = g_malloc0(sizeof(*opt));
638 opt->name = g_strdup(name);
dc9ca4ba 639 opt->opts = opts;
4f6dd9af
JK
640 if (prepend) {
641 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
642 } else {
643 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
644 }
c474ced8 645 opt->desc = desc;
e27c88fe 646 if (value) {
7267c094 647 opt->str = g_strdup(value);
e27c88fe 648 }
6c519404
LC
649 qemu_opt_parse(opt, &local_err);
650 if (error_is_set(&local_err)) {
584d4064 651 error_propagate(errp, local_err);
e27c88fe 652 qemu_opt_del(opt);
e27c88fe 653 }
e27c88fe
GH
654}
655
4f6dd9af
JK
656int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
657{
584d4064
LC
658 Error *local_err = NULL;
659
660 opt_set(opts, name, value, false, &local_err);
661 if (error_is_set(&local_err)) {
662 qerror_report_err(local_err);
663 error_free(local_err);
664 return -1;
665 }
666
667 return 0;
4f6dd9af
JK
668}
669
384f2139
LC
670void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
671 Error **errp)
672{
673 opt_set(opts, name, value, false, errp);
674}
675
f02b77c9
MK
676int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
677{
678 QemuOpt *opt;
679 const QemuOptDesc *desc = opts->list->desc;
680 int i;
681
682 for (i = 0; desc[i].name != NULL; i++) {
683 if (strcmp(desc[i].name, name) == 0) {
684 break;
685 }
686 }
687 if (desc[i].name == NULL) {
688 if (i == 0) {
689 /* empty list -> allow any */;
690 } else {
691 qerror_report(QERR_INVALID_PARAMETER, name);
692 return -1;
693 }
694 }
695
696 opt = g_malloc0(sizeof(*opt));
697 opt->name = g_strdup(name);
698 opt->opts = opts;
699 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
700 if (desc[i].name != NULL) {
701 opt->desc = desc+i;
702 }
703 opt->value.boolean = !!val;
704 return 0;
705}
706
48026075
GH
707int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
708 int abort_on_failure)
709{
710 QemuOpt *opt;
711 int rc = 0;
712
72cf2d4f 713 QTAILQ_FOREACH(opt, &opts->head, next) {
48026075
GH
714 rc = func(opt->name, opt->str, opaque);
715 if (abort_on_failure && rc != 0)
716 break;
717 }
718 return rc;
719}
720
e27c88fe
GH
721QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
722{
723 QemuOpts *opts;
724
72cf2d4f 725 QTAILQ_FOREACH(opts, &list->head, next) {
e27c88fe 726 if (!opts->id) {
4f6dd9af
JK
727 if (!id) {
728 return opts;
729 }
e27c88fe
GH
730 continue;
731 }
732 if (strcmp(opts->id, id) != 0) {
733 continue;
734 }
735 return opts;
736 }
737 return NULL;
738}
739
b560a9ab
MA
740static int id_wellformed(const char *id)
741{
742 int i;
743
744 if (!qemu_isalpha(id[0])) {
745 return 0;
746 }
747 for (i = 1; id[i]; i++) {
748 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
749 return 0;
750 }
751 }
752 return 1;
753}
754
8be7e7e4
LC
755QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
756 int fail_if_exists, Error **errp)
e27c88fe
GH
757{
758 QemuOpts *opts = NULL;
759
760 if (id) {
b560a9ab 761 if (!id_wellformed(id)) {
8be7e7e4 762 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
b560a9ab
MA
763 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
764 return NULL;
765 }
e27c88fe
GH
766 opts = qemu_opts_find(list, id);
767 if (opts != NULL) {
da93318a 768 if (fail_if_exists && !list->merge_lists) {
8be7e7e4 769 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
e27c88fe
GH
770 return NULL;
771 } else {
772 return opts;
773 }
774 }
da93318a
PM
775 } else if (list->merge_lists) {
776 opts = qemu_opts_find(list, NULL);
777 if (opts) {
778 return opts;
779 }
e27c88fe 780 }
7267c094 781 opts = g_malloc0(sizeof(*opts));
e27c88fe 782 if (id) {
7267c094 783 opts->id = g_strdup(id);
e27c88fe
GH
784 }
785 opts->list = list;
827b0813 786 loc_save(&opts->loc);
72cf2d4f
BS
787 QTAILQ_INIT(&opts->head);
788 QTAILQ_INSERT_TAIL(&list->head, opts, next);
e27c88fe
GH
789 return opts;
790}
791
bb67ab02
MA
792void qemu_opts_reset(QemuOptsList *list)
793{
794 QemuOpts *opts, *next_opts;
795
796 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
797 qemu_opts_del(opts);
798 }
799}
800
94ac7268
MA
801void qemu_opts_loc_restore(QemuOpts *opts)
802{
803 loc_restore(&opts->loc);
804}
805
e27c88fe
GH
806int qemu_opts_set(QemuOptsList *list, const char *id,
807 const char *name, const char *value)
808{
809 QemuOpts *opts;
8be7e7e4 810 Error *local_err = NULL;
e27c88fe 811
8be7e7e4
LC
812 opts = qemu_opts_create(list, id, 1, &local_err);
813 if (error_is_set(&local_err)) {
814 qerror_report_err(local_err);
815 error_free(local_err);
e27c88fe
GH
816 return -1;
817 }
818 return qemu_opt_set(opts, name, value);
819}
820
48026075
GH
821const char *qemu_opts_id(QemuOpts *opts)
822{
823 return opts->id;
824}
825
e27c88fe
GH
826void qemu_opts_del(QemuOpts *opts)
827{
828 QemuOpt *opt;
829
830 for (;;) {
72cf2d4f 831 opt = QTAILQ_FIRST(&opts->head);
e27c88fe
GH
832 if (opt == NULL)
833 break;
834 qemu_opt_del(opt);
835 }
72cf2d4f 836 QTAILQ_REMOVE(&opts->list->head, opts, next);
7267c094
AL
837 g_free(opts->id);
838 g_free(opts);
e27c88fe
GH
839}
840
841int qemu_opts_print(QemuOpts *opts, void *dummy)
842{
843 QemuOpt *opt;
844
845 fprintf(stderr, "%s: %s:", opts->list->name,
846 opts->id ? opts->id : "<noid>");
72cf2d4f 847 QTAILQ_FOREACH(opt, &opts->head, next) {
e27c88fe
GH
848 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
849 }
850 fprintf(stderr, "\n");
851 return 0;
852}
853
4f6dd9af
JK
854static int opts_do_parse(QemuOpts *opts, const char *params,
855 const char *firstname, bool prepend)
e27c88fe 856{
d318ff99 857 char option[128], value[1024];
e27c88fe 858 const char *p,*pe,*pc;
584d4064 859 Error *local_err = NULL;
e27c88fe 860
2cfa571f 861 for (p = params; *p != '\0'; p++) {
e27c88fe
GH
862 pe = strchr(p, '=');
863 pc = strchr(p, ',');
864 if (!pe || (pc && pc < pe)) {
865 /* found "foo,more" */
866 if (p == params && firstname) {
867 /* implicitly named first option */
868 pstrcpy(option, sizeof(option), firstname);
869 p = get_opt_value(value, sizeof(value), p);
870 } else {
871 /* option without value, probably a flag */
872 p = get_opt_name(option, sizeof(option), p, ',');
96729cbd 873 if (strncmp(option, "no", 2) == 0) {
e27c88fe
GH
874 memmove(option, option+2, strlen(option+2)+1);
875 pstrcpy(value, sizeof(value), "off");
876 } else {
877 pstrcpy(value, sizeof(value), "on");
878 }
879 }
880 } else {
881 /* found "foo=bar,more" */
882 p = get_opt_name(option, sizeof(option), p, '=');
883 if (*p != '=') {
884 break;
885 }
886 p++;
887 p = get_opt_value(value, sizeof(value), p);
888 }
889 if (strcmp(option, "id") != 0) {
890 /* store and parse */
584d4064
LC
891 opt_set(opts, option, value, prepend, &local_err);
892 if (error_is_set(&local_err)) {
893 qerror_report_err(local_err);
894 error_free(local_err);
96729cbd 895 return -1;
e27c88fe
GH
896 }
897 }
898 if (*p != ',') {
899 break;
900 }
e27c88fe 901 }
96729cbd
GH
902 return 0;
903}
904
4f6dd9af
JK
905int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
906{
907 return opts_do_parse(opts, params, firstname, false);
908}
909
910static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
911 int permit_abbrev, bool defaults)
96729cbd 912{
8212c64f 913 const char *firstname;
d318ff99 914 char value[1024], *id = NULL;
96729cbd
GH
915 const char *p;
916 QemuOpts *opts;
8be7e7e4 917 Error *local_err = NULL;
96729cbd 918
8212c64f
MA
919 assert(!permit_abbrev || list->implied_opt_name);
920 firstname = permit_abbrev ? list->implied_opt_name : NULL;
921
96729cbd
GH
922 if (strncmp(params, "id=", 3) == 0) {
923 get_opt_value(value, sizeof(value), params+3);
d510c5cf 924 id = value;
96729cbd
GH
925 } else if ((p = strstr(params, ",id=")) != NULL) {
926 get_opt_value(value, sizeof(value), p+4);
d510c5cf 927 id = value;
96729cbd 928 }
4f6dd9af
JK
929 if (defaults) {
930 if (!id && !QTAILQ_EMPTY(&list->head)) {
931 opts = qemu_opts_find(list, NULL);
932 } else {
8be7e7e4 933 opts = qemu_opts_create(list, id, 0, &local_err);
4f6dd9af
JK
934 }
935 } else {
8be7e7e4 936 opts = qemu_opts_create(list, id, 1, &local_err);
4f6dd9af 937 }
8be7e7e4
LC
938 if (opts == NULL) {
939 if (error_is_set(&local_err)) {
940 qerror_report_err(local_err);
941 error_free(local_err);
942 }
96729cbd 943 return NULL;
8be7e7e4 944 }
96729cbd 945
4f6dd9af 946 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
96729cbd
GH
947 qemu_opts_del(opts);
948 return NULL;
949 }
950
e27c88fe
GH
951 return opts;
952}
953
4f6dd9af
JK
954QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
955 int permit_abbrev)
956{
957 return opts_parse(list, params, permit_abbrev, false);
958}
959
960void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
961 int permit_abbrev)
962{
963 QemuOpts *opts;
964
965 opts = opts_parse(list, params, permit_abbrev, true);
966 assert(opts);
967}
968
4e89978e
LC
969typedef struct OptsFromQDictState {
970 QemuOpts *opts;
971 Error **errp;
972} OptsFromQDictState;
973
01e7f188
MA
974static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
975{
4e89978e 976 OptsFromQDictState *state = opaque;
01e7f188
MA
977 char buf[32];
978 const char *value;
979 int n;
980
4e89978e 981 if (!strcmp(key, "id") || error_is_set(state->errp)) {
01e7f188
MA
982 return;
983 }
984
985 switch (qobject_type(obj)) {
986 case QTYPE_QSTRING:
987 value = qstring_get_str(qobject_to_qstring(obj));
988 break;
989 case QTYPE_QINT:
990 n = snprintf(buf, sizeof(buf), "%" PRId64,
991 qint_get_int(qobject_to_qint(obj)));
992 assert(n < sizeof(buf));
993 value = buf;
994 break;
995 case QTYPE_QFLOAT:
996 n = snprintf(buf, sizeof(buf), "%.17g",
997 qfloat_get_double(qobject_to_qfloat(obj)));
998 assert(n < sizeof(buf));
999 value = buf;
1000 break;
1001 case QTYPE_QBOOL:
d35215f8
BS
1002 pstrcpy(buf, sizeof(buf),
1003 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
01e7f188
MA
1004 value = buf;
1005 break;
1006 default:
1007 return;
1008 }
4e89978e
LC
1009
1010 qemu_opt_set_err(state->opts, key, value, state->errp);
01e7f188
MA
1011}
1012
1013/*
1014 * Create QemuOpts from a QDict.
1015 * Use value of key "id" as ID if it exists and is a QString.
1016 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1017 * other types are silently ignored.
1018 */
4e89978e
LC
1019QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1020 Error **errp)
01e7f188 1021{
4e89978e 1022 OptsFromQDictState state;
8be7e7e4 1023 Error *local_err = NULL;
4e89978e 1024 QemuOpts *opts;
01e7f188 1025
8be7e7e4
LC
1026 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1027 &local_err);
1028 if (error_is_set(&local_err)) {
4e89978e 1029 error_propagate(errp, local_err);
01e7f188 1030 return NULL;
8be7e7e4 1031 }
01e7f188 1032
8be7e7e4 1033 assert(opts != NULL);
4e89978e
LC
1034
1035 state.errp = &local_err;
1036 state.opts = opts;
1037 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
1038 if (error_is_set(&local_err)) {
1039 error_propagate(errp, local_err);
1040 qemu_opts_del(opts);
1041 return NULL;
1042 }
1043
01e7f188
MA
1044 return opts;
1045}
1046
1047/*
1048 * Convert from QemuOpts to QDict.
1049 * The QDict values are of type QString.
1050 * TODO We'll want to use types appropriate for opt->desc->type, but
1051 * this is enough for now.
1052 */
1053QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1054{
1055 QemuOpt *opt;
1056 QObject *val;
1057
1058 if (!qdict) {
1059 qdict = qdict_new();
1060 }
1061 if (opts->id) {
1062 qdict_put(qdict, "id", qstring_from_str(opts->id));
1063 }
1064 QTAILQ_FOREACH(opt, &opts->head, next) {
1065 val = QOBJECT(qstring_from_str(opt->str));
1066 qdict_put_obj(qdict, opt->name, val);
1067 }
1068 return qdict;
1069}
1070
5dc519ef
MM
1071/* Validate parsed opts against descriptions where no
1072 * descriptions were provided in the QemuOptsList.
1073 */
29952866 1074void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
5dc519ef
MM
1075{
1076 QemuOpt *opt;
6c519404 1077 Error *local_err = NULL;
5dc519ef 1078
db97ceba 1079 assert(opts_accepts_any(opts));
5dc519ef
MM
1080
1081 QTAILQ_FOREACH(opt, &opts->head, next) {
db97ceba
DXW
1082 opt->desc = find_desc_by_name(desc, opt->name);
1083 if (!opt->desc) {
29952866
LC
1084 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1085 return;
5dc519ef
MM
1086 }
1087
6c519404
LC
1088 qemu_opt_parse(opt, &local_err);
1089 if (error_is_set(&local_err)) {
29952866
LC
1090 error_propagate(errp, local_err);
1091 return;
5dc519ef
MM
1092 }
1093 }
5dc519ef
MM
1094}
1095
e27c88fe
GH
1096int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1097 int abort_on_failure)
1098{
827b0813 1099 Location loc;
e27c88fe
GH
1100 QemuOpts *opts;
1101 int rc = 0;
1102
827b0813 1103 loc_push_none(&loc);
72cf2d4f 1104 QTAILQ_FOREACH(opts, &list->head, next) {
827b0813 1105 loc_restore(&opts->loc);
4a2594dd 1106 rc |= func(opts, opaque);
e27c88fe
GH
1107 if (abort_on_failure && rc != 0)
1108 break;
1109 }
827b0813 1110 loc_pop(&loc);
e27c88fe
GH
1111 return rc;
1112}