]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-option.c
Generate config-host.h from config-host.mak
[mirror_qemu.git] / qemu-option.c
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"
30 #include "qemu-option.h"
31
32 /*
33 * Extracts the name of an option from the parameter string (p points at the
34 * first byte of the option name)
35 *
36 * The option name is delimited by delim (usually , or =) or the string end
37 * and is copied into buf. If the option name is longer than buf_size, it is
38 * truncated. buf is always zero terminated.
39 *
40 * The return value is the position of the delimiter/zero byte after the option
41 * name in p.
42 */
43 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
44 {
45 char *q;
46
47 q = buf;
48 while (*p != '\0' && *p != delim) {
49 if (q && (q - buf) < buf_size - 1)
50 *q++ = *p;
51 p++;
52 }
53 if (q)
54 *q = '\0';
55
56 return p;
57 }
58
59 /*
60 * Extracts the value of an option from the parameter string p (p points at the
61 * first byte of the option value)
62 *
63 * This function is comparable to get_opt_name with the difference that the
64 * delimiter is fixed to be comma which starts a new option. To specify an
65 * option value that contains commas, double each comma.
66 */
67 const char *get_opt_value(char *buf, int buf_size, const char *p)
68 {
69 char *q;
70
71 q = buf;
72 while (*p != '\0') {
73 if (*p == ',') {
74 if (*(p + 1) != ',')
75 break;
76 p++;
77 }
78 if (q && (q - buf) < buf_size - 1)
79 *q++ = *p;
80 p++;
81 }
82 if (q)
83 *q = '\0';
84
85 return p;
86 }
87
88 int get_next_param_value(char *buf, int buf_size,
89 const char *tag, const char **pstr)
90 {
91 const char *p;
92 char option[128];
93
94 p = *pstr;
95 for(;;) {
96 p = get_opt_name(option, sizeof(option), p, '=');
97 if (*p != '=')
98 break;
99 p++;
100 if (!strcmp(tag, option)) {
101 *pstr = get_opt_value(buf, buf_size, p);
102 if (**pstr == ',') {
103 (*pstr)++;
104 }
105 return strlen(buf);
106 } else {
107 p = get_opt_value(NULL, 0, p);
108 }
109 if (*p != ',')
110 break;
111 p++;
112 }
113 return 0;
114 }
115
116 int get_param_value(char *buf, int buf_size,
117 const char *tag, const char *str)
118 {
119 return get_next_param_value(buf, buf_size, tag, &str);
120 }
121
122 int check_params(char *buf, int buf_size,
123 const char * const *params, const char *str)
124 {
125 const char *p;
126 int i;
127
128 p = str;
129 while (*p != '\0') {
130 p = get_opt_name(buf, buf_size, p, '=');
131 if (*p != '=') {
132 return -1;
133 }
134 p++;
135 for (i = 0; params[i] != NULL; i++) {
136 if (!strcmp(params[i], buf)) {
137 break;
138 }
139 }
140 if (params[i] == NULL) {
141 return -1;
142 }
143 p = get_opt_value(NULL, 0, p);
144 if (*p != ',') {
145 break;
146 }
147 p++;
148 }
149 return 0;
150 }
151
152 /*
153 * Searches an option list for an option with the given name
154 */
155 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
156 const char *name)
157 {
158 while (list && list->name) {
159 if (!strcmp(list->name, name)) {
160 return list;
161 }
162 list++;
163 }
164
165 return NULL;
166 }
167
168 static int parse_option_bool(const char *name, const char *value, int *ret)
169 {
170 if (value != NULL) {
171 if (!strcmp(value, "on")) {
172 *ret = 1;
173 } else if (!strcmp(value, "off")) {
174 *ret = 0;
175 } else {
176 fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
177 return -1;
178 }
179 } else {
180 *ret = 1;
181 }
182 return 0;
183 }
184
185 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
186 {
187 char *postfix;
188 uint64_t number;
189
190 if (value != NULL) {
191 number = strtoull(value, &postfix, 0);
192 if (*postfix != '\0') {
193 fprintf(stderr, "Option '%s' needs a number as parameter\n", name);
194 return -1;
195 }
196 } else {
197 fprintf(stderr, "Option '%s' needs a parameter\n", name);
198 return -1;
199 }
200 return 0;
201 }
202
203 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
204 {
205 char *postfix;
206 double sizef;
207
208 if (value != NULL) {
209 sizef = strtod(value, &postfix);
210 switch (*postfix) {
211 case 'T':
212 sizef *= 1024;
213 case 'G':
214 sizef *= 1024;
215 case 'M':
216 sizef *= 1024;
217 case 'K':
218 case 'k':
219 sizef *= 1024;
220 case 'b':
221 case '\0':
222 *ret = (uint64_t) sizef;
223 break;
224 default:
225 fprintf(stderr, "Option '%s' needs size as parameter\n", name);
226 fprintf(stderr, "You may use k, M, G or T suffixes for "
227 "kilobytes, megabytes, gigabytes and terabytes.\n");
228 return -1;
229 }
230 } else {
231 fprintf(stderr, "Option '%s' needs a parameter\n", name);
232 return -1;
233 }
234 return 0;
235 }
236
237 /*
238 * Sets the value of a parameter in a given option list. The parsing of the
239 * value depends on the type of option:
240 *
241 * OPT_FLAG (uses value.n):
242 * If no value is given, the flag is set to 1.
243 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
244 *
245 * OPT_STRING (uses value.s):
246 * value is strdup()ed and assigned as option value
247 *
248 * OPT_SIZE (uses value.n):
249 * The value is converted to an integer. Suffixes for kilobytes etc. are
250 * allowed (powers of 1024).
251 *
252 * Returns 0 on succes, -1 in error cases
253 */
254 int set_option_parameter(QEMUOptionParameter *list, const char *name,
255 const char *value)
256 {
257 int flag;
258
259 // Find a matching parameter
260 list = get_option_parameter(list, name);
261 if (list == NULL) {
262 fprintf(stderr, "Unknown option '%s'\n", name);
263 return -1;
264 }
265
266 // Process parameter
267 switch (list->type) {
268 case OPT_FLAG:
269 if (-1 == parse_option_bool(name, value, &flag))
270 return -1;
271 list->value.n = flag;
272 break;
273
274 case OPT_STRING:
275 if (value != NULL) {
276 list->value.s = strdup(value);
277 } else {
278 fprintf(stderr, "Option '%s' needs a parameter\n", name);
279 return -1;
280 }
281 break;
282
283 case OPT_SIZE:
284 if (-1 == parse_option_size(name, value, &list->value.n))
285 return -1;
286 break;
287
288 default:
289 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
290 return -1;
291 }
292
293 return 0;
294 }
295
296 /*
297 * Sets the given parameter to an integer instead of a string.
298 * This function cannot be used to set string options.
299 *
300 * Returns 0 on success, -1 in error cases
301 */
302 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
303 uint64_t value)
304 {
305 // Find a matching parameter
306 list = get_option_parameter(list, name);
307 if (list == NULL) {
308 fprintf(stderr, "Unknown option '%s'\n", name);
309 return -1;
310 }
311
312 // Process parameter
313 switch (list->type) {
314 case OPT_FLAG:
315 case OPT_NUMBER:
316 case OPT_SIZE:
317 list->value.n = value;
318 break;
319
320 default:
321 return -1;
322 }
323
324 return 0;
325 }
326
327 /*
328 * Frees a option list. If it contains strings, the strings are freed as well.
329 */
330 void free_option_parameters(QEMUOptionParameter *list)
331 {
332 QEMUOptionParameter *cur = list;
333
334 while (cur && cur->name) {
335 if (cur->type == OPT_STRING) {
336 free(cur->value.s);
337 }
338 cur++;
339 }
340
341 free(list);
342 }
343
344 /*
345 * Parses a parameter string (param) into an option list (dest).
346 *
347 * list is the templace is. If dest is NULL, a new copy of list is created for
348 * it. If list is NULL, this function fails.
349 *
350 * A parameter string consists of one or more parameters, separated by commas.
351 * Each parameter consists of its name and possibly of a value. In the latter
352 * case, the value is delimited by an = character. To specify a value which
353 * contains commas, double each comma so it won't be recognized as the end of
354 * the parameter.
355 *
356 * For more details of the parsing see above.
357 *
358 * Returns a pointer to the first element of dest (or the newly allocated copy)
359 * or NULL in error cases
360 */
361 QEMUOptionParameter *parse_option_parameters(const char *param,
362 QEMUOptionParameter *list, QEMUOptionParameter *dest)
363 {
364 QEMUOptionParameter *cur;
365 QEMUOptionParameter *allocated = NULL;
366 char name[256];
367 char value[256];
368 char *param_delim, *value_delim;
369 char next_delim;
370 size_t num_options;
371
372 if (list == NULL) {
373 return NULL;
374 }
375
376 if (dest == NULL) {
377 // Count valid options
378 num_options = 0;
379 cur = list;
380 while (cur->name) {
381 num_options++;
382 cur++;
383 }
384
385 // Create a copy of the option list to fill in values
386 dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
387 allocated = dest;
388 memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
389 }
390
391 while (*param) {
392
393 // Find parameter name and value in the string
394 param_delim = strchr(param, ',');
395 value_delim = strchr(param, '=');
396
397 if (value_delim && (value_delim < param_delim || !param_delim)) {
398 next_delim = '=';
399 } else {
400 next_delim = ',';
401 value_delim = NULL;
402 }
403
404 param = get_opt_name(name, sizeof(name), param, next_delim);
405 if (value_delim) {
406 param = get_opt_value(value, sizeof(value), param + 1);
407 }
408 if (*param != '\0') {
409 param++;
410 }
411
412 // Set the parameter
413 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
414 goto fail;
415 }
416 }
417
418 return dest;
419
420 fail:
421 // Only free the list if it was newly allocated
422 free_option_parameters(allocated);
423 return NULL;
424 }
425
426 /*
427 * Prints all options of a list that have a value to stdout
428 */
429 void print_option_parameters(QEMUOptionParameter *list)
430 {
431 while (list && list->name) {
432 switch (list->type) {
433 case OPT_STRING:
434 if (list->value.s != NULL) {
435 printf("%s='%s' ", list->name, list->value.s);
436 }
437 break;
438 case OPT_FLAG:
439 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
440 break;
441 case OPT_SIZE:
442 case OPT_NUMBER:
443 printf("%s=%" PRId64 " ", list->name, list->value.n);
444 break;
445 default:
446 printf("%s=(unkown type) ", list->name);
447 break;
448 }
449 list++;
450 }
451 }
452
453 /*
454 * Prints an overview of all available options
455 */
456 void print_option_help(QEMUOptionParameter *list)
457 {
458 printf("Supported options:\n");
459 while (list && list->name) {
460 printf("%-16s %s\n", list->name,
461 list->help ? list->help : "No description available");
462 list++;
463 }
464 }
465
466 /* ------------------------------------------------------------------ */
467
468 struct QemuOpt {
469 const char *name;
470 const char *str;
471
472 QemuOptDesc *desc;
473 union {
474 int bool;
475 uint64_t uint;
476 } value;
477
478 QemuOpts *opts;
479 TAILQ_ENTRY(QemuOpt) next;
480 };
481
482 struct QemuOpts {
483 const char *id;
484 QemuOptsList *list;
485 TAILQ_HEAD(, QemuOpt) head;
486 TAILQ_ENTRY(QemuOpts) next;
487 };
488
489 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
490 {
491 QemuOpt *opt;
492
493 TAILQ_FOREACH(opt, &opts->head, next) {
494 if (strcmp(opt->name, name) != 0)
495 continue;
496 return opt;
497 }
498 return NULL;
499 }
500
501 const char *qemu_opt_get(QemuOpts *opts, const char *name)
502 {
503 QemuOpt *opt = qemu_opt_find(opts, name);
504 return opt ? opt->str : NULL;
505 }
506
507 int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
508 {
509 QemuOpt *opt = qemu_opt_find(opts, name);
510
511 if (opt == NULL)
512 return defval;
513 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
514 return opt->value.bool;
515 }
516
517 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
518 {
519 QemuOpt *opt = qemu_opt_find(opts, name);
520
521 if (opt == NULL)
522 return defval;
523 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
524 return opt->value.uint;
525 }
526
527 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
528 {
529 QemuOpt *opt = qemu_opt_find(opts, name);
530
531 if (opt == NULL)
532 return defval;
533 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
534 return opt->value.uint;
535 }
536
537 static int qemu_opt_parse(QemuOpt *opt)
538 {
539 if (opt->desc == NULL)
540 return 0;
541 switch (opt->desc->type) {
542 case QEMU_OPT_STRING:
543 /* nothing */
544 return 0;
545 case QEMU_OPT_BOOL:
546 return parse_option_bool(opt->name, opt->str, &opt->value.bool);
547 case QEMU_OPT_NUMBER:
548 return parse_option_number(opt->name, opt->str, &opt->value.uint);
549 case QEMU_OPT_SIZE:
550 return parse_option_size(opt->name, opt->str, &opt->value.uint);
551 default:
552 abort();
553 }
554 }
555
556 static void qemu_opt_del(QemuOpt *opt)
557 {
558 TAILQ_REMOVE(&opt->opts->head, opt, next);
559 qemu_free((/* !const */ char*)opt->name);
560 qemu_free((/* !const */ char*)opt->str);
561 qemu_free(opt);
562 }
563
564 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
565 {
566 QemuOpt *opt;
567
568 opt = qemu_opt_find(opts, name);
569 if (!opt) {
570 QemuOptDesc *desc = opts->list->desc;
571 int i;
572
573 for (i = 0; desc[i].name != NULL; i++) {
574 if (strcmp(desc[i].name, name) == 0) {
575 break;
576 }
577 }
578 if (desc[i].name == NULL) {
579 if (i == 0) {
580 /* empty list -> allow any */;
581 } else {
582 fprintf(stderr, "option \"%s\" is not valid for %s\n",
583 name, opts->list->name);
584 return -1;
585 }
586 }
587 opt = qemu_mallocz(sizeof(*opt));
588 opt->name = qemu_strdup(name);
589 opt->opts = opts;
590 TAILQ_INSERT_TAIL(&opts->head, opt, next);
591 if (desc[i].name != NULL) {
592 opt->desc = desc+i;
593 }
594 }
595 qemu_free((/* !const */ char*)opt->str);
596 opt->str = NULL;
597 if (value) {
598 opt->str = qemu_strdup(value);
599 }
600 if (qemu_opt_parse(opt) < 0) {
601 fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str,
602 opts->list->name, opt->name);
603 qemu_opt_del(opt);
604 return -1;
605 }
606 return 0;
607 }
608
609 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
610 {
611 QemuOpts *opts;
612
613 TAILQ_FOREACH(opts, &list->head, next) {
614 if (!opts->id) {
615 continue;
616 }
617 if (strcmp(opts->id, id) != 0) {
618 continue;
619 }
620 return opts;
621 }
622 return NULL;
623 }
624
625 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
626 {
627 QemuOpts *opts = NULL;
628
629 if (id) {
630 opts = qemu_opts_find(list, id);
631 if (opts != NULL) {
632 if (fail_if_exists) {
633 fprintf(stderr, "tried to create id \"%s\" twice for \"%s\"\n",
634 id, list->name);
635 return NULL;
636 } else {
637 return opts;
638 }
639 }
640 }
641 opts = qemu_mallocz(sizeof(*opts));
642 if (id) {
643 opts->id = qemu_strdup(id);
644 }
645 opts->list = list;
646 TAILQ_INIT(&opts->head);
647 TAILQ_INSERT_TAIL(&list->head, opts, next);
648 return opts;
649 }
650
651 int qemu_opts_set(QemuOptsList *list, const char *id,
652 const char *name, const char *value)
653 {
654 QemuOpts *opts;
655
656 opts = qemu_opts_create(list, id, 1);
657 if (opts == NULL) {
658 fprintf(stderr, "id \"%s\" not found for \"%s\"\n",
659 id, list->name);
660 return -1;
661 }
662 return qemu_opt_set(opts, name, value);
663 }
664
665 void qemu_opts_del(QemuOpts *opts)
666 {
667 QemuOpt *opt;
668
669 for (;;) {
670 opt = TAILQ_FIRST(&opts->head);
671 if (opt == NULL)
672 break;
673 qemu_opt_del(opt);
674 }
675 TAILQ_REMOVE(&opts->list->head, opts, next);
676 qemu_free(opts);
677 }
678
679 int qemu_opts_print(QemuOpts *opts, void *dummy)
680 {
681 QemuOpt *opt;
682
683 fprintf(stderr, "%s: %s:", opts->list->name,
684 opts->id ? opts->id : "<noid>");
685 TAILQ_FOREACH(opt, &opts->head, next) {
686 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
687 }
688 fprintf(stderr, "\n");
689 return 0;
690 }
691
692 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname)
693 {
694 char option[128], value[128], *id = NULL;
695 QemuOpts *opts;
696 const char *p,*pe,*pc;
697
698 if (get_param_value(value, sizeof(value), "id", params))
699 id = qemu_strdup(value);
700 opts = qemu_opts_create(list, id, 1);
701 if (opts == NULL)
702 return NULL;
703
704 p = params;
705 for(;;) {
706 pe = strchr(p, '=');
707 pc = strchr(p, ',');
708 if (!pe || (pc && pc < pe)) {
709 /* found "foo,more" */
710 if (p == params && firstname) {
711 /* implicitly named first option */
712 pstrcpy(option, sizeof(option), firstname);
713 p = get_opt_value(value, sizeof(value), p);
714 } else {
715 /* option without value, probably a flag */
716 p = get_opt_name(option, sizeof(option), p, ',');
717 if (strncmp(p, "no", 2) == 0) {
718 memmove(option, option+2, strlen(option+2)+1);
719 pstrcpy(value, sizeof(value), "off");
720 } else {
721 pstrcpy(value, sizeof(value), "on");
722 }
723 }
724 } else {
725 /* found "foo=bar,more" */
726 p = get_opt_name(option, sizeof(option), p, '=');
727 if (*p != '=') {
728 break;
729 }
730 p++;
731 p = get_opt_value(value, sizeof(value), p);
732 }
733 if (strcmp(option, "id") != 0) {
734 /* store and parse */
735 if (-1 == qemu_opt_set(opts, option, value)) {
736 qemu_opts_del(opts);
737 return NULL;
738 }
739 }
740 if (*p != ',') {
741 break;
742 }
743 p++;
744 }
745 return opts;
746 }
747
748 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
749 int abort_on_failure)
750 {
751 QemuOpts *opts;
752 int rc = 0;
753
754 TAILQ_FOREACH(opts, &list->head, next) {
755 rc = func(opts, opaque);
756 if (abort_on_failure && rc != 0)
757 break;
758 }
759 return rc;
760 }