vhost.c: include <linux/vhost.h> last
[qemu/aliguori-queue.git] / qemu-option.c
blobf83d07c1e7ab36f7a183476ae0776b4fdafdef8f
1 /*
2 * Commandline option parsing functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include <stdio.h>
27 #include <string.h>
29 #include "qemu-common.h"
30 #include "qemu-error.h"
31 #include "qemu-objects.h"
32 #include "qemu-option.h"
35 * Extracts the name of an option from the parameter string (p points at the
36 * first byte of the option name)
38 * The option name is delimited by delim (usually , or =) or the string end
39 * and is copied into buf. If the option name is longer than buf_size, it is
40 * truncated. buf is always zero terminated.
42 * The return value is the position of the delimiter/zero byte after the option
43 * name in p.
45 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
47 char *q;
49 q = buf;
50 while (*p != '\0' && *p != delim) {
51 if (q && (q - buf) < buf_size - 1)
52 *q++ = *p;
53 p++;
55 if (q)
56 *q = '\0';
58 return p;
62 * Extracts the value of an option from the parameter string p (p points at the
63 * first byte of the option value)
65 * This function is comparable to get_opt_name with the difference that the
66 * delimiter is fixed to be comma which starts a new option. To specify an
67 * option value that contains commas, double each comma.
69 const char *get_opt_value(char *buf, int buf_size, const char *p)
71 char *q;
73 q = buf;
74 while (*p != '\0') {
75 if (*p == ',') {
76 if (*(p + 1) != ',')
77 break;
78 p++;
80 if (q && (q - buf) < buf_size - 1)
81 *q++ = *p;
82 p++;
84 if (q)
85 *q = '\0';
87 return p;
90 int get_next_param_value(char *buf, int buf_size,
91 const char *tag, const char **pstr)
93 const char *p;
94 char option[128];
96 p = *pstr;
97 for(;;) {
98 p = get_opt_name(option, sizeof(option), p, '=');
99 if (*p != '=')
100 break;
101 p++;
102 if (!strcmp(tag, option)) {
103 *pstr = get_opt_value(buf, buf_size, p);
104 if (**pstr == ',') {
105 (*pstr)++;
107 return strlen(buf);
108 } else {
109 p = get_opt_value(NULL, 0, p);
111 if (*p != ',')
112 break;
113 p++;
115 return 0;
118 int get_param_value(char *buf, int buf_size,
119 const char *tag, const char *str)
121 return get_next_param_value(buf, buf_size, tag, &str);
124 int check_params(char *buf, int buf_size,
125 const char * const *params, const char *str)
127 const char *p;
128 int i;
130 p = str;
131 while (*p != '\0') {
132 p = get_opt_name(buf, buf_size, p, '=');
133 if (*p != '=') {
134 return -1;
136 p++;
137 for (i = 0; params[i] != NULL; i++) {
138 if (!strcmp(params[i], buf)) {
139 break;
142 if (params[i] == NULL) {
143 return -1;
145 p = get_opt_value(NULL, 0, p);
146 if (*p != ',') {
147 break;
149 p++;
151 return 0;
155 * Searches an option list for an option with the given name
157 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
158 const char *name)
160 while (list && list->name) {
161 if (!strcmp(list->name, name)) {
162 return list;
164 list++;
167 return NULL;
170 static int parse_option_bool(const char *name, const char *value, int *ret)
172 if (value != NULL) {
173 if (!strcmp(value, "on")) {
174 *ret = 1;
175 } else if (!strcmp(value, "off")) {
176 *ret = 0;
177 } else {
178 fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
179 return -1;
181 } else {
182 *ret = 1;
184 return 0;
187 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
189 char *postfix;
190 uint64_t number;
192 if (value != NULL) {
193 number = strtoull(value, &postfix, 0);
194 if (*postfix != '\0') {
195 fprintf(stderr, "Option '%s' needs a number as parameter\n", name);
196 return -1;
198 *ret = number;
199 } else {
200 fprintf(stderr, "Option '%s' needs a parameter\n", name);
201 return -1;
203 return 0;
206 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
208 char *postfix;
209 double sizef;
211 if (value != NULL) {
212 sizef = strtod(value, &postfix);
213 switch (*postfix) {
214 case 'T':
215 sizef *= 1024;
216 case 'G':
217 sizef *= 1024;
218 case 'M':
219 sizef *= 1024;
220 case 'K':
221 case 'k':
222 sizef *= 1024;
223 case 'b':
224 case '\0':
225 *ret = (uint64_t) sizef;
226 break;
227 default:
228 fprintf(stderr, "Option '%s' needs size as parameter\n", name);
229 fprintf(stderr, "You may use k, M, G or T suffixes for "
230 "kilobytes, megabytes, gigabytes and terabytes.\n");
231 return -1;
233 } else {
234 fprintf(stderr, "Option '%s' needs a parameter\n", name);
235 return -1;
237 return 0;
241 * Sets the value of a parameter in a given option list. The parsing of the
242 * value depends on the type of option:
244 * OPT_FLAG (uses value.n):
245 * If no value is given, the flag is set to 1.
246 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
248 * OPT_STRING (uses value.s):
249 * value is strdup()ed and assigned as option value
251 * OPT_SIZE (uses value.n):
252 * The value is converted to an integer. Suffixes for kilobytes etc. are
253 * allowed (powers of 1024).
255 * Returns 0 on succes, -1 in error cases
257 int set_option_parameter(QEMUOptionParameter *list, const char *name,
258 const char *value)
260 int flag;
262 // Find a matching parameter
263 list = get_option_parameter(list, name);
264 if (list == NULL) {
265 fprintf(stderr, "Unknown option '%s'\n", name);
266 return -1;
269 // Process parameter
270 switch (list->type) {
271 case OPT_FLAG:
272 if (parse_option_bool(name, value, &flag) == -1)
273 return -1;
274 list->value.n = flag;
275 break;
277 case OPT_STRING:
278 if (value != NULL) {
279 list->value.s = qemu_strdup(value);
280 } else {
281 fprintf(stderr, "Option '%s' needs a parameter\n", name);
282 return -1;
284 break;
286 case OPT_SIZE:
287 if (parse_option_size(name, value, &list->value.n) == -1)
288 return -1;
289 break;
291 default:
292 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
293 return -1;
296 return 0;
300 * Sets the given parameter to an integer instead of a string.
301 * This function cannot be used to set string options.
303 * Returns 0 on success, -1 in error cases
305 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
306 uint64_t value)
308 // Find a matching parameter
309 list = get_option_parameter(list, name);
310 if (list == NULL) {
311 fprintf(stderr, "Unknown option '%s'\n", name);
312 return -1;
315 // Process parameter
316 switch (list->type) {
317 case OPT_FLAG:
318 case OPT_NUMBER:
319 case OPT_SIZE:
320 list->value.n = value;
321 break;
323 default:
324 return -1;
327 return 0;
331 * Frees a option list. If it contains strings, the strings are freed as well.
333 void free_option_parameters(QEMUOptionParameter *list)
335 QEMUOptionParameter *cur = list;
337 while (cur && cur->name) {
338 if (cur->type == OPT_STRING) {
339 qemu_free(cur->value.s);
341 cur++;
344 qemu_free(list);
348 * Parses a parameter string (param) into an option list (dest).
350 * list is the templace is. If dest is NULL, a new copy of list is created for
351 * it. If list is NULL, this function fails.
353 * A parameter string consists of one or more parameters, separated by commas.
354 * Each parameter consists of its name and possibly of a value. In the latter
355 * case, the value is delimited by an = character. To specify a value which
356 * contains commas, double each comma so it won't be recognized as the end of
357 * the parameter.
359 * For more details of the parsing see above.
361 * Returns a pointer to the first element of dest (or the newly allocated copy)
362 * or NULL in error cases
364 QEMUOptionParameter *parse_option_parameters(const char *param,
365 QEMUOptionParameter *list, QEMUOptionParameter *dest)
367 QEMUOptionParameter *cur;
368 QEMUOptionParameter *allocated = NULL;
369 char name[256];
370 char value[256];
371 char *param_delim, *value_delim;
372 char next_delim;
373 size_t num_options;
375 if (list == NULL) {
376 return NULL;
379 if (dest == NULL) {
380 // Count valid options
381 num_options = 0;
382 cur = list;
383 while (cur->name) {
384 num_options++;
385 cur++;
388 // Create a copy of the option list to fill in values
389 dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
390 allocated = dest;
391 memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
394 while (*param) {
396 // Find parameter name and value in the string
397 param_delim = strchr(param, ',');
398 value_delim = strchr(param, '=');
400 if (value_delim && (value_delim < param_delim || !param_delim)) {
401 next_delim = '=';
402 } else {
403 next_delim = ',';
404 value_delim = NULL;
407 param = get_opt_name(name, sizeof(name), param, next_delim);
408 if (value_delim) {
409 param = get_opt_value(value, sizeof(value), param + 1);
411 if (*param != '\0') {
412 param++;
415 // Set the parameter
416 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
417 goto fail;
421 return dest;
423 fail:
424 // Only free the list if it was newly allocated
425 free_option_parameters(allocated);
426 return NULL;
430 * Prints all options of a list that have a value to stdout
432 void print_option_parameters(QEMUOptionParameter *list)
434 while (list && list->name) {
435 switch (list->type) {
436 case OPT_STRING:
437 if (list->value.s != NULL) {
438 printf("%s='%s' ", list->name, list->value.s);
440 break;
441 case OPT_FLAG:
442 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
443 break;
444 case OPT_SIZE:
445 case OPT_NUMBER:
446 printf("%s=%" PRId64 " ", list->name, list->value.n);
447 break;
448 default:
449 printf("%s=(unkown type) ", list->name);
450 break;
452 list++;
457 * Prints an overview of all available options
459 void print_option_help(QEMUOptionParameter *list)
461 printf("Supported options:\n");
462 while (list && list->name) {
463 printf("%-16s %s\n", list->name,
464 list->help ? list->help : "No description available");
465 list++;
469 /* ------------------------------------------------------------------ */
471 struct QemuOpt {
472 const char *name;
473 const char *str;
475 const QemuOptDesc *desc;
476 union {
477 int boolean;
478 uint64_t uint;
479 } value;
481 QemuOpts *opts;
482 QTAILQ_ENTRY(QemuOpt) next;
485 struct QemuOpts {
486 char *id;
487 QemuOptsList *list;
488 Location loc;
489 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
490 QTAILQ_ENTRY(QemuOpts) next;
493 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
495 QemuOpt *opt;
497 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
498 if (strcmp(opt->name, name) != 0)
499 continue;
500 return opt;
502 return NULL;
505 const char *qemu_opt_get(QemuOpts *opts, const char *name)
507 QemuOpt *opt = qemu_opt_find(opts, name);
508 return opt ? opt->str : NULL;
511 int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
513 QemuOpt *opt = qemu_opt_find(opts, name);
515 if (opt == NULL)
516 return defval;
517 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
518 return opt->value.boolean;
521 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
523 QemuOpt *opt = qemu_opt_find(opts, name);
525 if (opt == NULL)
526 return defval;
527 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
528 return opt->value.uint;
531 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
533 QemuOpt *opt = qemu_opt_find(opts, name);
535 if (opt == NULL)
536 return defval;
537 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
538 return opt->value.uint;
541 static int qemu_opt_parse(QemuOpt *opt)
543 if (opt->desc == NULL)
544 return 0;
545 switch (opt->desc->type) {
546 case QEMU_OPT_STRING:
547 /* nothing */
548 return 0;
549 case QEMU_OPT_BOOL:
550 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
551 case QEMU_OPT_NUMBER:
552 return parse_option_number(opt->name, opt->str, &opt->value.uint);
553 case QEMU_OPT_SIZE:
554 return parse_option_size(opt->name, opt->str, &opt->value.uint);
555 default:
556 abort();
560 static void qemu_opt_del(QemuOpt *opt)
562 QTAILQ_REMOVE(&opt->opts->head, opt, next);
563 qemu_free((/* !const */ char*)opt->name);
564 qemu_free((/* !const */ char*)opt->str);
565 qemu_free(opt);
568 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
570 QemuOpt *opt;
571 const QemuOptDesc *desc = opts->list->desc;
572 int i;
574 for (i = 0; desc[i].name != NULL; i++) {
575 if (strcmp(desc[i].name, name) == 0) {
576 break;
579 if (desc[i].name == NULL) {
580 if (i == 0) {
581 /* empty list -> allow any */;
582 } else {
583 fprintf(stderr, "option \"%s\" is not valid for %s\n",
584 name, opts->list->name);
585 return -1;
589 opt = qemu_mallocz(sizeof(*opt));
590 opt->name = qemu_strdup(name);
591 opt->opts = opts;
592 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
593 if (desc[i].name != NULL) {
594 opt->desc = desc+i;
596 if (value) {
597 opt->str = qemu_strdup(value);
599 if (qemu_opt_parse(opt) < 0) {
600 fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str,
601 opts->list->name, opt->name);
602 qemu_opt_del(opt);
603 return -1;
605 return 0;
608 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
609 int abort_on_failure)
611 QemuOpt *opt;
612 int rc = 0;
614 QTAILQ_FOREACH(opt, &opts->head, next) {
615 rc = func(opt->name, opt->str, opaque);
616 if (abort_on_failure && rc != 0)
617 break;
619 return rc;
622 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
624 QemuOpts *opts;
626 QTAILQ_FOREACH(opts, &list->head, next) {
627 if (!opts->id) {
628 continue;
630 if (strcmp(opts->id, id) != 0) {
631 continue;
633 return opts;
635 return NULL;
638 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
640 QemuOpts *opts = NULL;
642 if (id) {
643 opts = qemu_opts_find(list, id);
644 if (opts != NULL) {
645 if (fail_if_exists) {
646 fprintf(stderr, "tried to create id \"%s\" twice for \"%s\"\n",
647 id, list->name);
648 return NULL;
649 } else {
650 return opts;
654 opts = qemu_mallocz(sizeof(*opts));
655 if (id) {
656 opts->id = qemu_strdup(id);
658 opts->list = list;
659 loc_save(&opts->loc);
660 QTAILQ_INIT(&opts->head);
661 QTAILQ_INSERT_TAIL(&list->head, opts, next);
662 return opts;
665 int qemu_opts_set(QemuOptsList *list, const char *id,
666 const char *name, const char *value)
668 QemuOpts *opts;
670 opts = qemu_opts_create(list, id, 1);
671 if (opts == NULL) {
672 return -1;
674 return qemu_opt_set(opts, name, value);
677 const char *qemu_opts_id(QemuOpts *opts)
679 return opts->id;
682 void qemu_opts_del(QemuOpts *opts)
684 QemuOpt *opt;
686 for (;;) {
687 opt = QTAILQ_FIRST(&opts->head);
688 if (opt == NULL)
689 break;
690 qemu_opt_del(opt);
692 QTAILQ_REMOVE(&opts->list->head, opts, next);
693 qemu_free(opts->id);
694 qemu_free(opts);
697 int qemu_opts_print(QemuOpts *opts, void *dummy)
699 QemuOpt *opt;
701 fprintf(stderr, "%s: %s:", opts->list->name,
702 opts->id ? opts->id : "<noid>");
703 QTAILQ_FOREACH(opt, &opts->head, next) {
704 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
706 fprintf(stderr, "\n");
707 return 0;
710 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
712 char option[128], value[1024];
713 const char *p,*pe,*pc;
715 for (p = params; *p != '\0'; p++) {
716 pe = strchr(p, '=');
717 pc = strchr(p, ',');
718 if (!pe || (pc && pc < pe)) {
719 /* found "foo,more" */
720 if (p == params && firstname) {
721 /* implicitly named first option */
722 pstrcpy(option, sizeof(option), firstname);
723 p = get_opt_value(value, sizeof(value), p);
724 } else {
725 /* option without value, probably a flag */
726 p = get_opt_name(option, sizeof(option), p, ',');
727 if (strncmp(option, "no", 2) == 0) {
728 memmove(option, option+2, strlen(option+2)+1);
729 pstrcpy(value, sizeof(value), "off");
730 } else {
731 pstrcpy(value, sizeof(value), "on");
734 } else {
735 /* found "foo=bar,more" */
736 p = get_opt_name(option, sizeof(option), p, '=');
737 if (*p != '=') {
738 break;
740 p++;
741 p = get_opt_value(value, sizeof(value), p);
743 if (strcmp(option, "id") != 0) {
744 /* store and parse */
745 if (qemu_opt_set(opts, option, value) == -1) {
746 return -1;
749 if (*p != ',') {
750 break;
753 return 0;
756 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
757 int permit_abbrev)
759 const char *firstname;
760 char value[1024], *id = NULL;
761 const char *p;
762 QemuOpts *opts;
764 assert(!permit_abbrev || list->implied_opt_name);
765 firstname = permit_abbrev ? list->implied_opt_name : NULL;
767 if (strncmp(params, "id=", 3) == 0) {
768 get_opt_value(value, sizeof(value), params+3);
769 id = qemu_strdup(value);
770 } else if ((p = strstr(params, ",id=")) != NULL) {
771 get_opt_value(value, sizeof(value), p+4);
772 id = qemu_strdup(value);
774 opts = qemu_opts_create(list, id, 1);
775 if (opts == NULL)
776 return NULL;
778 if (qemu_opts_do_parse(opts, params, firstname) != 0) {
779 qemu_opts_del(opts);
780 return NULL;
783 return opts;
786 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
788 char buf[32];
789 const char *value;
790 int n;
792 if (!strcmp(key, "id")) {
793 return;
796 switch (qobject_type(obj)) {
797 case QTYPE_QSTRING:
798 value = qstring_get_str(qobject_to_qstring(obj));
799 break;
800 case QTYPE_QINT:
801 n = snprintf(buf, sizeof(buf), "%" PRId64,
802 qint_get_int(qobject_to_qint(obj)));
803 assert(n < sizeof(buf));
804 value = buf;
805 break;
806 case QTYPE_QFLOAT:
807 n = snprintf(buf, sizeof(buf), "%.17g",
808 qfloat_get_double(qobject_to_qfloat(obj)));
809 assert(n < sizeof(buf));
810 value = buf;
811 break;
812 case QTYPE_QBOOL:
813 pstrcpy(buf, sizeof(buf),
814 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
815 value = buf;
816 break;
817 default:
818 return;
820 qemu_opt_set(opaque, key, value);
824 * Create QemuOpts from a QDict.
825 * Use value of key "id" as ID if it exists and is a QString.
826 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
827 * other types are silently ignored.
829 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
831 QemuOpts *opts;
833 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
834 if (opts == NULL)
835 return NULL;
837 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
838 return opts;
842 * Convert from QemuOpts to QDict.
843 * The QDict values are of type QString.
844 * TODO We'll want to use types appropriate for opt->desc->type, but
845 * this is enough for now.
847 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
849 QemuOpt *opt;
850 QObject *val;
852 if (!qdict) {
853 qdict = qdict_new();
855 if (opts->id) {
856 qdict_put(qdict, "id", qstring_from_str(opts->id));
858 QTAILQ_FOREACH(opt, &opts->head, next) {
859 val = QOBJECT(qstring_from_str(opt->str));
860 qdict_put_obj(qdict, opt->name, val);
862 return qdict;
865 /* Validate parsed opts against descriptions where no
866 * descriptions were provided in the QemuOptsList.
868 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
870 QemuOpt *opt;
872 assert(opts->list->desc[0].name == NULL);
874 QTAILQ_FOREACH(opt, &opts->head, next) {
875 int i;
877 for (i = 0; desc[i].name != NULL; i++) {
878 if (strcmp(desc[i].name, opt->name) == 0) {
879 break;
882 if (desc[i].name == NULL) {
883 fprintf(stderr, "option \"%s\" is not valid for %s\n",
884 opt->name, opts->list->name);
885 return -1;
888 opt->desc = &desc[i];
890 if (qemu_opt_parse(opt) < 0) {
891 return -1;
895 return 0;
898 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
899 int abort_on_failure)
901 Location loc;
902 QemuOpts *opts;
903 int rc = 0;
905 loc_push_none(&loc);
906 QTAILQ_FOREACH(opts, &list->head, next) {
907 loc_restore(&opts->loc);
908 rc |= func(opts, opaque);
909 if (abort_on_failure && rc != 0)
910 break;
912 loc_pop(&loc);
913 return rc;