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