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