]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-option.c
QemuOpts: add qemu_opts_print_help to replace print_option_help
[mirror_qemu.git] / util / qemu-option.c
CommitLineData
d3f24367
KW
1/*
2 * Commandline option parsing functions
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include <stdio.h>
27#include <string.h>
28
29#include "qemu-common.h"
1de7afc9 30#include "qemu/error-report.h"
7b1b5d19
PB
31#include "qapi/qmp/types.h"
32#include "qapi/error.h"
33#include "qapi/qmp/qerror.h"
1de7afc9 34#include "qemu/option_int.h"
d3f24367
KW
35
36/*
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
39 *
40 * The option name is delimited by delim (usually , or =) or the string end
41 * and is copied into buf. If the option name is longer than buf_size, it is
42 * truncated. buf is always zero terminated.
43 *
44 * The return value is the position of the delimiter/zero byte after the option
45 * name in p.
46 */
47const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
48{
49 char *q;
50
51 q = buf;
52 while (*p != '\0' && *p != delim) {
53 if (q && (q - buf) < buf_size - 1)
54 *q++ = *p;
55 p++;
56 }
57 if (q)
58 *q = '\0';
59
60 return p;
61}
62
63/*
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
66 *
67 * This function is comparable to get_opt_name with the difference that the
68 * delimiter is fixed to be comma which starts a new option. To specify an
69 * option value that contains commas, double each comma.
70 */
71const char *get_opt_value(char *buf, int buf_size, const char *p)
72{
73 char *q;
74
75 q = buf;
76 while (*p != '\0') {
77 if (*p == ',') {
78 if (*(p + 1) != ',')
79 break;
80 p++;
81 }
82 if (q && (q - buf) < buf_size - 1)
83 *q++ = *p;
84 p++;
85 }
86 if (q)
87 *q = '\0';
88
89 return p;
90}
91
62c5802e
GH
92int get_next_param_value(char *buf, int buf_size,
93 const char *tag, const char **pstr)
94{
95 const char *p;
96 char option[128];
97
98 p = *pstr;
99 for(;;) {
100 p = get_opt_name(option, sizeof(option), p, '=');
101 if (*p != '=')
102 break;
103 p++;
104 if (!strcmp(tag, option)) {
105 *pstr = get_opt_value(buf, buf_size, p);
106 if (**pstr == ',') {
107 (*pstr)++;
108 }
109 return strlen(buf);
110 } else {
111 p = get_opt_value(NULL, 0, p);
112 }
113 if (*p != ',')
114 break;
115 p++;
116 }
117 return 0;
118}
119
120int get_param_value(char *buf, int buf_size,
121 const char *tag, const char *str)
122{
123 return get_next_param_value(buf, buf_size, tag, &str);
124}
125
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
5e89db76
CL
176static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
177 const char *name)
178{
179 int i;
180
181 for (i = 0; desc[i].name != NULL; i++) {
182 if (strcmp(desc[i].name, name) == 0) {
183 return &desc[i];
184 }
185 }
186
187 return NULL;
188}
189
e8cd45c7
VL
190void parse_option_size(const char *name, const char *value,
191 uint64_t *ret, Error **errp)
7695019b
GH
192{
193 char *postfix;
194 double sizef;
195
196 if (value != NULL) {
197 sizef = strtod(value, &postfix);
198 switch (*postfix) {
199 case 'T':
200 sizef *= 1024;
0b0404bf 201 /* fall through */
7695019b
GH
202 case 'G':
203 sizef *= 1024;
0b0404bf 204 /* fall through */
7695019b
GH
205 case 'M':
206 sizef *= 1024;
0b0404bf 207 /* fall through */
7695019b
GH
208 case 'K':
209 case 'k':
210 sizef *= 1024;
0b0404bf 211 /* fall through */
7695019b
GH
212 case 'b':
213 case '\0':
214 *ret = (uint64_t) sizef;
215 break;
216 default:
ec7b2ccb 217 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
7216ae3d 218#if 0 /* conversion from qerror_report() to error_set() broke this: */
c64f27d4 219 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
7695019b 220 "kilobytes, megabytes, gigabytes and terabytes.\n");
7216ae3d 221#endif
ec7b2ccb 222 return;
7695019b
GH
223 }
224 } else {
ec7b2ccb 225 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
7695019b 226 }
7695019b
GH
227}
228
d3f24367
KW
229/*
230 * Sets the value of a parameter in a given option list. The parsing of the
231 * value depends on the type of option:
232 *
233 * OPT_FLAG (uses value.n):
234 * If no value is given, the flag is set to 1.
235 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
236 *
237 * OPT_STRING (uses value.s):
238 * value is strdup()ed and assigned as option value
239 *
240 * OPT_SIZE (uses value.n):
241 * The value is converted to an integer. Suffixes for kilobytes etc. are
242 * allowed (powers of 1024).
243 *
244 * Returns 0 on succes, -1 in error cases
245 */
246int set_option_parameter(QEMUOptionParameter *list, const char *name,
247 const char *value)
248{
f02b77c9 249 bool flag;
cf62adfa 250 Error *local_err = NULL;
67b1355b 251
d3f24367
KW
252 // Find a matching parameter
253 list = get_option_parameter(list, name);
254 if (list == NULL) {
255 fprintf(stderr, "Unknown option '%s'\n", name);
256 return -1;
257 }
258
259 // Process parameter
260 switch (list->type) {
261 case OPT_FLAG:
cf62adfa 262 parse_option_bool(name, value, &flag, &local_err);
84d18f06 263 if (!local_err) {
cf62adfa
LC
264 list->value.n = flag;
265 }
d3f24367
KW
266 break;
267
268 case OPT_STRING:
269 if (value != NULL) {
7267c094 270 list->value.s = g_strdup(value);
d3f24367
KW
271 } else {
272 fprintf(stderr, "Option '%s' needs a parameter\n", name);
273 return -1;
274 }
275 break;
276
277 case OPT_SIZE:
ec7b2ccb 278 parse_option_size(name, value, &list->value.n, &local_err);
d3f24367 279 break;
7695019b 280
d3f24367
KW
281 default:
282 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
283 return -1;
284 }
285
84d18f06 286 if (local_err) {
cf62adfa
LC
287 qerror_report_err(local_err);
288 error_free(local_err);
289 return -1;
290 }
291
d4ca092a
HR
292 list->assigned = true;
293
d3f24367
KW
294 return 0;
295}
296
297/*
298 * Sets the given parameter to an integer instead of a string.
299 * This function cannot be used to set string options.
300 *
301 * Returns 0 on success, -1 in error cases
302 */
303int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
304 uint64_t value)
305{
306 // Find a matching parameter
307 list = get_option_parameter(list, name);
308 if (list == NULL) {
309 fprintf(stderr, "Unknown option '%s'\n", name);
310 return -1;
311 }
312
313 // Process parameter
314 switch (list->type) {
315 case OPT_FLAG:
316 case OPT_NUMBER:
317 case OPT_SIZE:
318 list->value.n = value;
319 break;
320
321 default:
322 return -1;
323 }
324
d4ca092a
HR
325 list->assigned = true;
326
d3f24367
KW
327 return 0;
328}
329
330/*
331 * Frees a option list. If it contains strings, the strings are freed as well.
332 */
333void free_option_parameters(QEMUOptionParameter *list)
334{
335 QEMUOptionParameter *cur = list;
336
337 while (cur && cur->name) {
338 if (cur->type == OPT_STRING) {
7267c094 339 g_free(cur->value.s);
d3f24367
KW
340 }
341 cur++;
342 }
343
7267c094 344 g_free(list);
d3f24367
KW
345}
346
b50cbabc
MK
347/*
348 * Count valid options in list
349 */
350static size_t count_option_parameters(QEMUOptionParameter *list)
351{
352 size_t num_options = 0;
353
354 while (list && list->name) {
355 num_options++;
356 list++;
357 }
358
359 return num_options;
360}
361
362/*
363 * Append an option list (list) to an option list (dest).
364 *
365 * If dest is NULL, a new copy of list is created.
366 *
367 * Returns a pointer to the first element of dest (or the newly allocated copy)
368 */
369QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
370 QEMUOptionParameter *list)
371{
372 size_t num_options, num_dest_options;
373
374 num_options = count_option_parameters(dest);
375 num_dest_options = num_options;
376
377 num_options += count_option_parameters(list);
378
7267c094 379 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
bd69fe84 380 dest[num_dest_options].name = NULL;
b50cbabc
MK
381
382 while (list && list->name) {
383 if (get_option_parameter(dest, list->name) == NULL) {
384 dest[num_dest_options++] = *list;
385 dest[num_dest_options].name = NULL;
386 }
387 list++;
388 }
389
390 return dest;
391}
392
d3f24367
KW
393/*
394 * Parses a parameter string (param) into an option list (dest).
395 *
0e72e753
SH
396 * list is the template option list. If dest is NULL, a new copy of list is
397 * created. If list is NULL, this function fails.
d3f24367
KW
398 *
399 * A parameter string consists of one or more parameters, separated by commas.
400 * Each parameter consists of its name and possibly of a value. In the latter
401 * case, the value is delimited by an = character. To specify a value which
402 * contains commas, double each comma so it won't be recognized as the end of
403 * the parameter.
404 *
405 * For more details of the parsing see above.
406 *
407 * Returns a pointer to the first element of dest (or the newly allocated copy)
408 * or NULL in error cases
409 */
410QEMUOptionParameter *parse_option_parameters(const char *param,
411 QEMUOptionParameter *list, QEMUOptionParameter *dest)
412{
d3f24367
KW
413 QEMUOptionParameter *allocated = NULL;
414 char name[256];
415 char value[256];
416 char *param_delim, *value_delim;
417 char next_delim;
d4ca092a 418 int i;
d3f24367
KW
419
420 if (list == NULL) {
421 return NULL;
422 }
423
424 if (dest == NULL) {
898c257b 425 dest = allocated = append_option_parameters(NULL, list);
d3f24367
KW
426 }
427
d4ca092a
HR
428 for (i = 0; dest[i].name; i++) {
429 dest[i].assigned = false;
430 }
431
d3f24367
KW
432 while (*param) {
433
434 // Find parameter name and value in the string
435 param_delim = strchr(param, ',');
436 value_delim = strchr(param, '=');
437
438 if (value_delim && (value_delim < param_delim || !param_delim)) {
439 next_delim = '=';
440 } else {
441 next_delim = ',';
442 value_delim = NULL;
443 }
444
445 param = get_opt_name(name, sizeof(name), param, next_delim);
446 if (value_delim) {
447 param = get_opt_value(value, sizeof(value), param + 1);
448 }
449 if (*param != '\0') {
450 param++;
451 }
452
453 // Set the parameter
454 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
455 goto fail;
456 }
457 }
458
459 return dest;
460
461fail:
462 // Only free the list if it was newly allocated
463 free_option_parameters(allocated);
464 return NULL;
465}
466
7cc07ab8
KW
467bool has_help_option(const char *param)
468{
469 size_t buflen = strlen(param) + 1;
470 char *buf = g_malloc0(buflen);
471 const char *p = param;
472 bool result = false;
473
474 while (*p) {
475 p = get_opt_value(buf, buflen, p);
476 if (*p) {
477 p++;
478 }
479
480 if (is_help_option(buf)) {
481 result = true;
482 goto out;
483 }
484 }
485
486out:
487 free(buf);
488 return result;
489}
490
491bool is_valid_option_list(const char *param)
492{
493 size_t buflen = strlen(param) + 1;
494 char *buf = g_malloc0(buflen);
495 const char *p = param;
496 bool result = true;
497
498 while (*p) {
499 p = get_opt_value(buf, buflen, p);
500 if (*p && !*++p) {
501 result = false;
502 goto out;
503 }
504
505 if (!*buf || *buf == ',') {
506 result = false;
507 goto out;
508 }
509 }
510
511out:
512 free(buf);
513 return result;
514}
515
d3f24367
KW
516/*
517 * Prints all options of a list that have a value to stdout
518 */
519void print_option_parameters(QEMUOptionParameter *list)
520{
521 while (list && list->name) {
522 switch (list->type) {
523 case OPT_STRING:
524 if (list->value.s != NULL) {
525 printf("%s='%s' ", list->name, list->value.s);
526 }
527 break;
528 case OPT_FLAG:
529 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
530 break;
531 case OPT_SIZE:
532 case OPT_NUMBER:
533 printf("%s=%" PRId64 " ", list->name, list->value.n);
534 break;
535 default:
07f35073 536 printf("%s=(unknown type) ", list->name);
d3f24367
KW
537 break;
538 }
539 list++;
540 }
541}
db08adf5
KW
542
543/*
544 * Prints an overview of all available options
545 */
546void print_option_help(QEMUOptionParameter *list)
547{
548 printf("Supported options:\n");
549 while (list && list->name) {
550 printf("%-16s %s\n", list->name,
551 list->help ? list->help : "No description available");
552 list++;
553 }
554}
e27c88fe 555
504189a9
CL
556void qemu_opts_print_help(QemuOptsList *list)
557{
558 QemuOptDesc *desc;
559
560 assert(list);
561 desc = list->desc;
562 printf("Supported options:\n");
563 while (desc && desc->name) {
564 printf("%-16s %s\n", desc->name,
565 desc->help ? desc->help : "No description available");
566 desc++;
567 }
568}
e27c88fe
GH
569/* ------------------------------------------------------------------ */
570
e27c88fe
GH
571static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
572{
573 QemuOpt *opt;
574
dc9ca4ba 575 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
e27c88fe
GH
576 if (strcmp(opt->name, name) != 0)
577 continue;
578 return opt;
579 }
580 return NULL;
581}
582
fc345512
CL
583static void qemu_opt_del(QemuOpt *opt)
584{
585 QTAILQ_REMOVE(&opt->opts->head, opt, next);
586 g_free(opt->name);
587 g_free(opt->str);
588 g_free(opt);
589}
590
782730b0
CL
591/* qemu_opt_set allows many settings for the same option.
592 * This function deletes all settings for an option.
593 */
594static void qemu_opt_del_all(QemuOpts *opts, const char *name)
595{
596 QemuOpt *opt, *next_opt;
597
598 QTAILQ_FOREACH_SAFE(opt, &opts->head, next, next_opt) {
599 if (!strcmp(opt->name, name)) {
600 qemu_opt_del(opt);
601 }
602 }
603}
604
e27c88fe
GH
605const char *qemu_opt_get(QemuOpts *opts, const char *name)
606{
607 QemuOpt *opt = qemu_opt_find(opts, name);
09722032
CL
608
609 if (!opt) {
610 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
611 if (desc && desc->def_value_str) {
612 return desc->def_value_str;
613 }
614 }
e27c88fe
GH
615 return opt ? opt->str : NULL;
616}
617
782730b0
CL
618/* Get a known option (or its default) and remove it from the list
619 * all in one action. Return a malloced string of the option value.
620 * Result must be freed by caller with g_free().
621 */
622char *qemu_opt_get_del(QemuOpts *opts, const char *name)
623{
624 QemuOpt *opt;
625 const QemuOptDesc *desc;
626 char *str = NULL;
627
628 if (opts == NULL) {
629 return NULL;
630 }
631
632 opt = qemu_opt_find(opts, name);
633 if (!opt) {
634 desc = find_desc_by_name(opts->list->desc, name);
635 if (desc && desc->def_value_str) {
636 str = g_strdup(desc->def_value_str);
637 }
638 return str;
639 }
640 str = opt->str;
641 opt->str = NULL;
642 qemu_opt_del_all(opts, name);
643 return str;
644}
645
c8057f95
PM
646bool qemu_opt_has_help_opt(QemuOpts *opts)
647{
648 QemuOpt *opt;
649
650 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
651 if (is_help_option(opt->name)) {
652 return true;
653 }
654 }
655 return false;
656}
657
782730b0
CL
658static bool qemu_opt_get_bool_helper(QemuOpts *opts, const char *name,
659 bool defval, bool del)
e27c88fe
GH
660{
661 QemuOpt *opt = qemu_opt_find(opts, name);
782730b0 662 bool ret = defval;
e27c88fe 663
09722032
CL
664 if (opt == NULL) {
665 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
666 if (desc && desc->def_value_str) {
782730b0 667 parse_option_bool(name, desc->def_value_str, &ret, &error_abort);
09722032 668 }
782730b0 669 return ret;
09722032 670 }
e27c88fe 671 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
782730b0
CL
672 ret = opt->value.boolean;
673 if (del) {
674 qemu_opt_del_all(opts, name);
675 }
676 return ret;
e27c88fe
GH
677}
678
782730b0
CL
679bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
680{
681 return qemu_opt_get_bool_helper(opts, name, defval, false);
682}
683
684bool qemu_opt_get_bool_del(QemuOpts *opts, const char *name, bool defval)
685{
686 return qemu_opt_get_bool_helper(opts, name, defval, true);
687}
688
689static uint64_t qemu_opt_get_number_helper(QemuOpts *opts, const char *name,
690 uint64_t defval, bool del)
e27c88fe
GH
691{
692 QemuOpt *opt = qemu_opt_find(opts, name);
782730b0 693 uint64_t ret = defval;
e27c88fe 694
09722032
CL
695 if (opt == NULL) {
696 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
697 if (desc && desc->def_value_str) {
782730b0 698 parse_option_number(name, desc->def_value_str, &ret, &error_abort);
09722032 699 }
782730b0 700 return ret;
09722032 701 }
e27c88fe 702 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
782730b0
CL
703 ret = opt->value.uint;
704 if (del) {
705 qemu_opt_del_all(opts, name);
706 }
707 return ret;
e27c88fe
GH
708}
709
782730b0
CL
710uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
711{
712 return qemu_opt_get_number_helper(opts, name, defval, false);
713}
714
715uint64_t qemu_opt_get_number_del(QemuOpts *opts, const char *name,
716 uint64_t defval)
717{
718 return qemu_opt_get_number_helper(opts, name, defval, true);
719}
720
721static uint64_t qemu_opt_get_size_helper(QemuOpts *opts, const char *name,
722 uint64_t defval, bool del)
e27c88fe
GH
723{
724 QemuOpt *opt = qemu_opt_find(opts, name);
782730b0 725 uint64_t ret = defval;
e27c88fe 726
09722032
CL
727 if (opt == NULL) {
728 const QemuOptDesc *desc = find_desc_by_name(opts->list->desc, name);
729 if (desc && desc->def_value_str) {
782730b0 730 parse_option_size(name, desc->def_value_str, &ret, &error_abort);
09722032 731 }
782730b0 732 return ret;
09722032 733 }
e27c88fe 734 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
782730b0
CL
735 ret = opt->value.uint;
736 if (del) {
737 qemu_opt_del_all(opts, name);
738 }
739 return ret;
740}
741
742uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
743{
744 return qemu_opt_get_size_helper(opts, name, defval, false);
745}
746
747uint64_t qemu_opt_get_size_del(QemuOpts *opts, const char *name,
748 uint64_t defval)
749{
750 return qemu_opt_get_size_helper(opts, name, defval, true);
e27c88fe
GH
751}
752
6c519404 753static void qemu_opt_parse(QemuOpt *opt, Error **errp)
e27c88fe
GH
754{
755 if (opt->desc == NULL)
6c519404 756 return;
2f39df5b 757
e27c88fe
GH
758 switch (opt->desc->type) {
759 case QEMU_OPT_STRING:
760 /* nothing */
6c519404 761 return;
e27c88fe 762 case QEMU_OPT_BOOL:
6c519404 763 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
cf62adfa 764 break;
e27c88fe 765 case QEMU_OPT_NUMBER:
6c519404 766 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
2f39df5b 767 break;
e27c88fe 768 case QEMU_OPT_SIZE:
6c519404 769 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
ec7b2ccb 770 break;
e27c88fe
GH
771 default:
772 abort();
773 }
774}
775
c474ced8
DXW
776static bool opts_accepts_any(const QemuOpts *opts)
777{
778 return opts->list->desc[0].name == NULL;
779}
780
0dd6c526
KW
781int qemu_opt_unset(QemuOpts *opts, const char *name)
782{
783 QemuOpt *opt = qemu_opt_find(opts, name);
784
785 assert(opts_accepts_any(opts));
786
787 if (opt == NULL) {
788 return -1;
789 } else {
790 qemu_opt_del(opt);
791 return 0;
792 }
793}
794
c474ced8
DXW
795static void opt_set(QemuOpts *opts, const char *name, const char *value,
796 bool prepend, Error **errp)
797{
798 QemuOpt *opt;
799 const QemuOptDesc *desc;
800 Error *local_err = NULL;
801
802 desc = find_desc_by_name(opts->list->desc, name);
803 if (!desc && !opts_accepts_any(opts)) {
804 error_set(errp, QERR_INVALID_PARAMETER, name);
805 return;
e27c88fe 806 }
dc9ca4ba 807
7267c094
AL
808 opt = g_malloc0(sizeof(*opt));
809 opt->name = g_strdup(name);
dc9ca4ba 810 opt->opts = opts;
4f6dd9af
JK
811 if (prepend) {
812 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
813 } else {
814 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
815 }
c474ced8 816 opt->desc = desc;
c64f50d1 817 opt->str = g_strdup(value);
6c519404 818 qemu_opt_parse(opt, &local_err);
84d18f06 819 if (local_err) {
584d4064 820 error_propagate(errp, local_err);
e27c88fe 821 qemu_opt_del(opt);
e27c88fe 822 }
e27c88fe
GH
823}
824
4f6dd9af
JK
825int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
826{
584d4064
LC
827 Error *local_err = NULL;
828
829 opt_set(opts, name, value, false, &local_err);
84d18f06 830 if (local_err) {
584d4064
LC
831 qerror_report_err(local_err);
832 error_free(local_err);
833 return -1;
834 }
835
836 return 0;
4f6dd9af
JK
837}
838
384f2139
LC
839void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
840 Error **errp)
841{
842 opt_set(opts, name, value, false, errp);
843}
844
f02b77c9
MK
845int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
846{
847 QemuOpt *opt;
848 const QemuOptDesc *desc = opts->list->desc;
f02b77c9 849
ad718d01
DXW
850 opt = g_malloc0(sizeof(*opt));
851 opt->desc = find_desc_by_name(desc, name);
852 if (!opt->desc && !opts_accepts_any(opts)) {
853 qerror_report(QERR_INVALID_PARAMETER, name);
854 g_free(opt);
855 return -1;
f02b77c9
MK
856 }
857
f02b77c9
MK
858 opt->name = g_strdup(name);
859 opt->opts = opts;
f02b77c9 860 opt->value.boolean = !!val;
ad718d01
DXW
861 opt->str = g_strdup(val ? "on" : "off");
862 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
863
f02b77c9
MK
864 return 0;
865}
866
b83c18e2
DXW
867int qemu_opt_set_number(QemuOpts *opts, const char *name, int64_t val)
868{
869 QemuOpt *opt;
870 const QemuOptDesc *desc = opts->list->desc;
871
872 opt = g_malloc0(sizeof(*opt));
873 opt->desc = find_desc_by_name(desc, name);
874 if (!opt->desc && !opts_accepts_any(opts)) {
875 qerror_report(QERR_INVALID_PARAMETER, name);
876 g_free(opt);
877 return -1;
878 }
879
880 opt->name = g_strdup(name);
881 opt->opts = opts;
882 opt->value.uint = val;
883 opt->str = g_strdup_printf("%" PRId64, val);
884 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
885
886 return 0;
887}
888
48026075
GH
889int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
890 int abort_on_failure)
891{
892 QemuOpt *opt;
893 int rc = 0;
894
72cf2d4f 895 QTAILQ_FOREACH(opt, &opts->head, next) {
48026075
GH
896 rc = func(opt->name, opt->str, opaque);
897 if (abort_on_failure && rc != 0)
898 break;
899 }
900 return rc;
901}
902
e27c88fe
GH
903QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
904{
905 QemuOpts *opts;
906
72cf2d4f 907 QTAILQ_FOREACH(opts, &list->head, next) {
96bc97eb
MA
908 if (!opts->id && !id) {
909 return opts;
e27c88fe 910 }
96bc97eb
MA
911 if (opts->id && id && !strcmp(opts->id, id)) {
912 return opts;
e27c88fe 913 }
e27c88fe
GH
914 }
915 return NULL;
916}
917
b560a9ab
MA
918static int id_wellformed(const char *id)
919{
920 int i;
921
922 if (!qemu_isalpha(id[0])) {
923 return 0;
924 }
925 for (i = 1; id[i]; i++) {
926 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
927 return 0;
928 }
929 }
930 return 1;
931}
932
8be7e7e4
LC
933QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
934 int fail_if_exists, Error **errp)
e27c88fe
GH
935{
936 QemuOpts *opts = NULL;
937
938 if (id) {
b560a9ab 939 if (!id_wellformed(id)) {
8be7e7e4 940 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
7216ae3d 941#if 0 /* conversion from qerror_report() to error_set() broke this: */
b560a9ab 942 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
7216ae3d 943#endif
b560a9ab
MA
944 return NULL;
945 }
e27c88fe
GH
946 opts = qemu_opts_find(list, id);
947 if (opts != NULL) {
da93318a 948 if (fail_if_exists && !list->merge_lists) {
f231b88d 949 error_setg(errp, "Duplicate ID '%s' for %s", id, list->name);
e27c88fe
GH
950 return NULL;
951 } else {
952 return opts;
953 }
954 }
da93318a
PM
955 } else if (list->merge_lists) {
956 opts = qemu_opts_find(list, NULL);
957 if (opts) {
958 return opts;
959 }
e27c88fe 960 }
7267c094 961 opts = g_malloc0(sizeof(*opts));
c64f50d1 962 opts->id = g_strdup(id);
e27c88fe 963 opts->list = list;
827b0813 964 loc_save(&opts->loc);
72cf2d4f
BS
965 QTAILQ_INIT(&opts->head);
966 QTAILQ_INSERT_TAIL(&list->head, opts, next);
e27c88fe
GH
967 return opts;
968}
969
bb67ab02
MA
970void qemu_opts_reset(QemuOptsList *list)
971{
972 QemuOpts *opts, *next_opts;
973
974 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
975 qemu_opts_del(opts);
976 }
977}
978
94ac7268
MA
979void qemu_opts_loc_restore(QemuOpts *opts)
980{
981 loc_restore(&opts->loc);
982}
983
e27c88fe
GH
984int qemu_opts_set(QemuOptsList *list, const char *id,
985 const char *name, const char *value)
986{
987 QemuOpts *opts;
8be7e7e4 988 Error *local_err = NULL;
e27c88fe 989
8be7e7e4 990 opts = qemu_opts_create(list, id, 1, &local_err);
84d18f06 991 if (local_err) {
8be7e7e4
LC
992 qerror_report_err(local_err);
993 error_free(local_err);
e27c88fe
GH
994 return -1;
995 }
996 return qemu_opt_set(opts, name, value);
997}
998
48026075
GH
999const char *qemu_opts_id(QemuOpts *opts)
1000{
1001 return opts->id;
1002}
1003
326642bc
KW
1004/* The id string will be g_free()d by qemu_opts_del */
1005void qemu_opts_set_id(QemuOpts *opts, char *id)
1006{
1007 opts->id = id;
1008}
1009
e27c88fe
GH
1010void qemu_opts_del(QemuOpts *opts)
1011{
1012 QemuOpt *opt;
1013
1014 for (;;) {
72cf2d4f 1015 opt = QTAILQ_FIRST(&opts->head);
e27c88fe
GH
1016 if (opt == NULL)
1017 break;
1018 qemu_opt_del(opt);
1019 }
72cf2d4f 1020 QTAILQ_REMOVE(&opts->list->head, opts, next);
7267c094
AL
1021 g_free(opts->id);
1022 g_free(opts);
e27c88fe
GH
1023}
1024
e6790542 1025void qemu_opts_print(QemuOpts *opts)
e27c88fe
GH
1026{
1027 QemuOpt *opt;
09722032 1028 QemuOptDesc *desc = opts->list->desc;
e27c88fe 1029
09722032
CL
1030 if (desc[0].name == NULL) {
1031 QTAILQ_FOREACH(opt, &opts->head, next) {
1032 printf("%s=\"%s\" ", opt->name, opt->str);
1033 }
1034 return;
1035 }
1036 for (; desc && desc->name; desc++) {
1037 const char *value;
1038 QemuOpt *opt = qemu_opt_find(opts, desc->name);
1039
1040 value = opt ? opt->str : desc->def_value_str;
1041 if (!value) {
1042 continue;
1043 }
1044 if (desc->type == QEMU_OPT_STRING) {
1045 printf("%s='%s' ", desc->name, value);
1046 } else if ((desc->type == QEMU_OPT_SIZE ||
1047 desc->type == QEMU_OPT_NUMBER) && opt) {
1048 printf("%s=%" PRId64 " ", desc->name, opt->value.uint);
1049 } else {
1050 printf("%s=%s ", desc->name, value);
1051 }
e27c88fe 1052 }
e27c88fe
GH
1053}
1054
4f6dd9af
JK
1055static int opts_do_parse(QemuOpts *opts, const char *params,
1056 const char *firstname, bool prepend)
e27c88fe 1057{
d318ff99 1058 char option[128], value[1024];
e27c88fe 1059 const char *p,*pe,*pc;
584d4064 1060 Error *local_err = NULL;
e27c88fe 1061
2cfa571f 1062 for (p = params; *p != '\0'; p++) {
e27c88fe
GH
1063 pe = strchr(p, '=');
1064 pc = strchr(p, ',');
1065 if (!pe || (pc && pc < pe)) {
1066 /* found "foo,more" */
1067 if (p == params && firstname) {
1068 /* implicitly named first option */
1069 pstrcpy(option, sizeof(option), firstname);
1070 p = get_opt_value(value, sizeof(value), p);
1071 } else {
1072 /* option without value, probably a flag */
1073 p = get_opt_name(option, sizeof(option), p, ',');
96729cbd 1074 if (strncmp(option, "no", 2) == 0) {
e27c88fe
GH
1075 memmove(option, option+2, strlen(option+2)+1);
1076 pstrcpy(value, sizeof(value), "off");
1077 } else {
1078 pstrcpy(value, sizeof(value), "on");
1079 }
1080 }
1081 } else {
1082 /* found "foo=bar,more" */
1083 p = get_opt_name(option, sizeof(option), p, '=');
1084 if (*p != '=') {
1085 break;
1086 }
1087 p++;
1088 p = get_opt_value(value, sizeof(value), p);
1089 }
1090 if (strcmp(option, "id") != 0) {
1091 /* store and parse */
584d4064 1092 opt_set(opts, option, value, prepend, &local_err);
84d18f06 1093 if (local_err) {
584d4064
LC
1094 qerror_report_err(local_err);
1095 error_free(local_err);
96729cbd 1096 return -1;
e27c88fe
GH
1097 }
1098 }
1099 if (*p != ',') {
1100 break;
1101 }
e27c88fe 1102 }
96729cbd
GH
1103 return 0;
1104}
1105
4f6dd9af
JK
1106int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
1107{
1108 return opts_do_parse(opts, params, firstname, false);
1109}
1110
1111static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
1112 int permit_abbrev, bool defaults)
96729cbd 1113{
8212c64f 1114 const char *firstname;
d318ff99 1115 char value[1024], *id = NULL;
96729cbd
GH
1116 const char *p;
1117 QemuOpts *opts;
8be7e7e4 1118 Error *local_err = NULL;
96729cbd 1119
8212c64f
MA
1120 assert(!permit_abbrev || list->implied_opt_name);
1121 firstname = permit_abbrev ? list->implied_opt_name : NULL;
1122
96729cbd
GH
1123 if (strncmp(params, "id=", 3) == 0) {
1124 get_opt_value(value, sizeof(value), params+3);
d510c5cf 1125 id = value;
96729cbd
GH
1126 } else if ((p = strstr(params, ",id=")) != NULL) {
1127 get_opt_value(value, sizeof(value), p+4);
d510c5cf 1128 id = value;
96729cbd 1129 }
cb77d192
MA
1130
1131 /*
1132 * This code doesn't work for defaults && !list->merge_lists: when
1133 * params has no id=, and list has an element with !opts->id, it
1134 * appends a new element instead of returning the existing opts.
1135 * However, we got no use for this case. Guard against possible
1136 * (if unlikely) future misuse:
1137 */
1138 assert(!defaults || list->merge_lists);
6d4cd408 1139 opts = qemu_opts_create(list, id, !defaults, &local_err);
8be7e7e4 1140 if (opts == NULL) {
84d18f06 1141 if (local_err) {
8be7e7e4
LC
1142 qerror_report_err(local_err);
1143 error_free(local_err);
1144 }
96729cbd 1145 return NULL;
8be7e7e4 1146 }
96729cbd 1147
4f6dd9af 1148 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
96729cbd
GH
1149 qemu_opts_del(opts);
1150 return NULL;
1151 }
1152
e27c88fe
GH
1153 return opts;
1154}
1155
4f6dd9af
JK
1156QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
1157 int permit_abbrev)
1158{
1159 return opts_parse(list, params, permit_abbrev, false);
1160}
1161
1162void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
1163 int permit_abbrev)
1164{
1165 QemuOpts *opts;
1166
1167 opts = opts_parse(list, params, permit_abbrev, true);
1168 assert(opts);
1169}
1170
4e89978e
LC
1171typedef struct OptsFromQDictState {
1172 QemuOpts *opts;
1173 Error **errp;
1174} OptsFromQDictState;
1175
01e7f188
MA
1176static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
1177{
4e89978e 1178 OptsFromQDictState *state = opaque;
01e7f188
MA
1179 char buf[32];
1180 const char *value;
1181 int n;
1182
2767ceec 1183 if (!strcmp(key, "id") || *state->errp) {
01e7f188
MA
1184 return;
1185 }
1186
1187 switch (qobject_type(obj)) {
1188 case QTYPE_QSTRING:
1189 value = qstring_get_str(qobject_to_qstring(obj));
1190 break;
1191 case QTYPE_QINT:
1192 n = snprintf(buf, sizeof(buf), "%" PRId64,
1193 qint_get_int(qobject_to_qint(obj)));
1194 assert(n < sizeof(buf));
1195 value = buf;
1196 break;
1197 case QTYPE_QFLOAT:
1198 n = snprintf(buf, sizeof(buf), "%.17g",
1199 qfloat_get_double(qobject_to_qfloat(obj)));
1200 assert(n < sizeof(buf));
1201 value = buf;
1202 break;
1203 case QTYPE_QBOOL:
d35215f8
BS
1204 pstrcpy(buf, sizeof(buf),
1205 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
01e7f188
MA
1206 value = buf;
1207 break;
1208 default:
1209 return;
1210 }
4e89978e
LC
1211
1212 qemu_opt_set_err(state->opts, key, value, state->errp);
01e7f188
MA
1213}
1214
1215/*
1216 * Create QemuOpts from a QDict.
1217 * Use value of key "id" as ID if it exists and is a QString.
1218 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1219 * other types are silently ignored.
1220 */
4e89978e
LC
1221QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1222 Error **errp)
01e7f188 1223{
4e89978e 1224 OptsFromQDictState state;
8be7e7e4 1225 Error *local_err = NULL;
4e89978e 1226 QemuOpts *opts;
01e7f188 1227
8be7e7e4
LC
1228 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1229 &local_err);
84d18f06 1230 if (local_err) {
4e89978e 1231 error_propagate(errp, local_err);
01e7f188 1232 return NULL;
8be7e7e4 1233 }
01e7f188 1234
8be7e7e4 1235 assert(opts != NULL);
4e89978e
LC
1236
1237 state.errp = &local_err;
1238 state.opts = opts;
1239 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
84d18f06 1240 if (local_err) {
4e89978e
LC
1241 error_propagate(errp, local_err);
1242 qemu_opts_del(opts);
1243 return NULL;
1244 }
1245
01e7f188
MA
1246 return opts;
1247}
1248
376609cc
KW
1249/*
1250 * Adds all QDict entries to the QemuOpts that can be added and removes them
1251 * from the QDict. When this function returns, the QDict contains only those
1252 * entries that couldn't be added to the QemuOpts.
1253 */
1254void qemu_opts_absorb_qdict(QemuOpts *opts, QDict *qdict, Error **errp)
1255{
1256 const QDictEntry *entry, *next;
1257
1258 entry = qdict_first(qdict);
1259
1260 while (entry != NULL) {
1261 Error *local_err = NULL;
1262 OptsFromQDictState state = {
1263 .errp = &local_err,
1264 .opts = opts,
1265 };
1266
1267 next = qdict_next(qdict, entry);
1268
1269 if (find_desc_by_name(opts->list->desc, entry->key)) {
1270 qemu_opts_from_qdict_1(entry->key, entry->value, &state);
84d18f06 1271 if (local_err) {
376609cc
KW
1272 error_propagate(errp, local_err);
1273 return;
1274 } else {
1275 qdict_del(qdict, entry->key);
1276 }
1277 }
1278
1279 entry = next;
1280 }
1281}
1282
01e7f188
MA
1283/*
1284 * Convert from QemuOpts to QDict.
1285 * The QDict values are of type QString.
1286 * TODO We'll want to use types appropriate for opt->desc->type, but
1287 * this is enough for now.
1288 */
1289QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1290{
1291 QemuOpt *opt;
1292 QObject *val;
1293
1294 if (!qdict) {
1295 qdict = qdict_new();
1296 }
1297 if (opts->id) {
1298 qdict_put(qdict, "id", qstring_from_str(opts->id));
1299 }
1300 QTAILQ_FOREACH(opt, &opts->head, next) {
1301 val = QOBJECT(qstring_from_str(opt->str));
1302 qdict_put_obj(qdict, opt->name, val);
1303 }
1304 return qdict;
1305}
1306
5dc519ef
MM
1307/* Validate parsed opts against descriptions where no
1308 * descriptions were provided in the QemuOptsList.
1309 */
29952866 1310void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
5dc519ef
MM
1311{
1312 QemuOpt *opt;
6c519404 1313 Error *local_err = NULL;
5dc519ef 1314
db97ceba 1315 assert(opts_accepts_any(opts));
5dc519ef
MM
1316
1317 QTAILQ_FOREACH(opt, &opts->head, next) {
db97ceba
DXW
1318 opt->desc = find_desc_by_name(desc, opt->name);
1319 if (!opt->desc) {
29952866
LC
1320 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1321 return;
5dc519ef
MM
1322 }
1323
6c519404 1324 qemu_opt_parse(opt, &local_err);
84d18f06 1325 if (local_err) {
29952866
LC
1326 error_propagate(errp, local_err);
1327 return;
5dc519ef
MM
1328 }
1329 }
5dc519ef
MM
1330}
1331
e27c88fe
GH
1332int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1333 int abort_on_failure)
1334{
827b0813 1335 Location loc;
e27c88fe
GH
1336 QemuOpts *opts;
1337 int rc = 0;
1338
827b0813 1339 loc_push_none(&loc);
72cf2d4f 1340 QTAILQ_FOREACH(opts, &list->head, next) {
827b0813 1341 loc_restore(&opts->loc);
4a2594dd 1342 rc |= func(opts, opaque);
e27c88fe
GH
1343 if (abort_on_failure && rc != 0)
1344 break;
1345 }
827b0813 1346 loc_pop(&loc);
e27c88fe
GH
1347 return rc;
1348}