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