blob: b5da116b1f334a97059b4339940e2dfa5fdf77e4 [file] [log] [blame]
Kevin Wolfd3f24362009-05-18 16:42:09 +02001/*
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"
Markus Armbruster827b0812010-02-18 19:46:49 +010030#include "qemu-error.h"
Markus Armbruster01e7f182010-02-10 20:15:29 +010031#include "qemu-objects.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +020032#include "qemu-option.h"
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -030033#include "error.h"
Markus Armbruster975b63a2010-03-25 17:22:32 +010034#include "qerror.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +020035
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
Gerd Hoffmann62c58022009-07-22 16:43:00 +020092int get_next_param_value(char *buf, int buf_size,
93 const char *tag, const char **pstr)
94{
95 const char *p;
96 char option[128];
97
98 p = *pstr;
99 for(;;) {
100 p = get_opt_name(option, sizeof(option), p, '=');
101 if (*p != '=')
102 break;
103 p++;
104 if (!strcmp(tag, option)) {
105 *pstr = get_opt_value(buf, buf_size, p);
106 if (**pstr == ',') {
107 (*pstr)++;
108 }
109 return strlen(buf);
110 } else {
111 p = get_opt_value(NULL, 0, p);
112 }
113 if (*p != ',')
114 break;
115 p++;
116 }
117 return 0;
118}
119
120int get_param_value(char *buf, int buf_size,
121 const char *tag, const char *str)
122{
123 return get_next_param_value(buf, buf_size, tag, &str);
124}
125
126int check_params(char *buf, int buf_size,
127 const char * const *params, const char *str)
128{
129 const char *p;
130 int i;
131
132 p = str;
133 while (*p != '\0') {
134 p = get_opt_name(buf, buf_size, p, '=');
135 if (*p != '=') {
136 return -1;
137 }
138 p++;
139 for (i = 0; params[i] != NULL; i++) {
140 if (!strcmp(params[i], buf)) {
141 break;
142 }
143 }
144 if (params[i] == NULL) {
145 return -1;
146 }
147 p = get_opt_value(NULL, 0, p);
148 if (*p != ',') {
149 break;
150 }
151 p++;
152 }
153 return 0;
154}
155
Kevin Wolfd3f24362009-05-18 16:42:09 +0200156/*
157 * Searches an option list for an option with the given name
158 */
159QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
160 const char *name)
161{
162 while (list && list->name) {
163 if (!strcmp(list->name, name)) {
164 return list;
165 }
166 list++;
167 }
168
169 return NULL;
170}
171
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300172static void parse_option_bool(const char *name, const char *value, bool *ret,
173 Error **errp)
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200174{
175 if (value != NULL) {
176 if (!strcmp(value, "on")) {
177 *ret = 1;
178 } else if (!strcmp(value, "off")) {
179 *ret = 0;
180 } else {
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300181 error_set(errp,QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200182 }
183 } else {
184 *ret = 1;
185 }
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200186}
187
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300188static void parse_option_number(const char *name, const char *value,
189 uint64_t *ret, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200190{
191 char *postfix;
192 uint64_t number;
193
194 if (value != NULL) {
195 number = strtoull(value, &postfix, 0);
196 if (*postfix != '\0') {
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300197 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
198 return;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200199 }
Gerd Hoffmann21c9f4c2009-07-29 10:39:59 +0200200 *ret = number;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200201 } else {
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300202 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200203 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200204}
205
Gerd Hoffmann76950192009-07-22 16:43:02 +0200206static int parse_option_size(const char *name, const char *value, uint64_t *ret)
207{
208 char *postfix;
209 double sizef;
210
211 if (value != NULL) {
212 sizef = strtod(value, &postfix);
213 switch (*postfix) {
214 case 'T':
215 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100216 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200217 case 'G':
218 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100219 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200220 case 'M':
221 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100222 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200223 case 'K':
224 case 'k':
225 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100226 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200227 case 'b':
228 case '\0':
229 *ret = (uint64_t) sizef;
230 break;
231 default:
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100232 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
233 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
Gerd Hoffmann76950192009-07-22 16:43:02 +0200234 "kilobytes, megabytes, gigabytes and terabytes.\n");
235 return -1;
236 }
237 } else {
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100238 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
Gerd Hoffmann76950192009-07-22 16:43:02 +0200239 return -1;
240 }
241 return 0;
242}
243
Kevin Wolfd3f24362009-05-18 16:42:09 +0200244/*
245 * Sets the value of a parameter in a given option list. The parsing of the
246 * value depends on the type of option:
247 *
248 * OPT_FLAG (uses value.n):
249 * If no value is given, the flag is set to 1.
250 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
251 *
252 * OPT_STRING (uses value.s):
253 * value is strdup()ed and assigned as option value
254 *
255 * OPT_SIZE (uses value.n):
256 * The value is converted to an integer. Suffixes for kilobytes etc. are
257 * allowed (powers of 1024).
258 *
259 * Returns 0 on succes, -1 in error cases
260 */
261int set_option_parameter(QEMUOptionParameter *list, const char *name,
262 const char *value)
263{
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530264 bool flag;
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300265 Error *local_err = NULL;
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200266
Kevin Wolfd3f24362009-05-18 16:42:09 +0200267 // Find a matching parameter
268 list = get_option_parameter(list, name);
269 if (list == NULL) {
270 fprintf(stderr, "Unknown option '%s'\n", name);
271 return -1;
272 }
273
274 // Process parameter
275 switch (list->type) {
276 case OPT_FLAG:
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300277 parse_option_bool(name, value, &flag, &local_err);
278 if (!error_is_set(&local_err)) {
279 list->value.n = flag;
280 }
Kevin Wolfd3f24362009-05-18 16:42:09 +0200281 break;
282
283 case OPT_STRING:
284 if (value != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500285 list->value.s = g_strdup(value);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200286 } else {
287 fprintf(stderr, "Option '%s' needs a parameter\n", name);
288 return -1;
289 }
290 break;
291
292 case OPT_SIZE:
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100293 if (parse_option_size(name, value, &list->value.n) == -1)
Kevin Wolfd3f24362009-05-18 16:42:09 +0200294 return -1;
Kevin Wolfd3f24362009-05-18 16:42:09 +0200295 break;
Gerd Hoffmann76950192009-07-22 16:43:02 +0200296
Kevin Wolfd3f24362009-05-18 16:42:09 +0200297 default:
298 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
299 return -1;
300 }
301
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300302 if (error_is_set(&local_err)) {
303 qerror_report_err(local_err);
304 error_free(local_err);
305 return -1;
306 }
307
Kevin Wolfd3f24362009-05-18 16:42:09 +0200308 return 0;
309}
310
311/*
312 * Sets the given parameter to an integer instead of a string.
313 * This function cannot be used to set string options.
314 *
315 * Returns 0 on success, -1 in error cases
316 */
317int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
318 uint64_t value)
319{
320 // Find a matching parameter
321 list = get_option_parameter(list, name);
322 if (list == NULL) {
323 fprintf(stderr, "Unknown option '%s'\n", name);
324 return -1;
325 }
326
327 // Process parameter
328 switch (list->type) {
329 case OPT_FLAG:
330 case OPT_NUMBER:
331 case OPT_SIZE:
332 list->value.n = value;
333 break;
334
335 default:
336 return -1;
337 }
338
339 return 0;
340}
341
342/*
343 * Frees a option list. If it contains strings, the strings are freed as well.
344 */
345void free_option_parameters(QEMUOptionParameter *list)
346{
347 QEMUOptionParameter *cur = list;
348
349 while (cur && cur->name) {
350 if (cur->type == OPT_STRING) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500351 g_free(cur->value.s);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200352 }
353 cur++;
354 }
355
Anthony Liguori7267c092011-08-20 22:09:37 -0500356 g_free(list);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200357}
358
359/*
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900360 * Count valid options in list
361 */
362static size_t count_option_parameters(QEMUOptionParameter *list)
363{
364 size_t num_options = 0;
365
366 while (list && list->name) {
367 num_options++;
368 list++;
369 }
370
371 return num_options;
372}
373
374/*
375 * Append an option list (list) to an option list (dest).
376 *
377 * If dest is NULL, a new copy of list is created.
378 *
379 * Returns a pointer to the first element of dest (or the newly allocated copy)
380 */
381QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
382 QEMUOptionParameter *list)
383{
384 size_t num_options, num_dest_options;
385
386 num_options = count_option_parameters(dest);
387 num_dest_options = num_options;
388
389 num_options += count_option_parameters(list);
390
Anthony Liguori7267c092011-08-20 22:09:37 -0500391 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
Kevin Wolfbd69fe82010-06-11 10:19:41 +0200392 dest[num_dest_options].name = NULL;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900393
394 while (list && list->name) {
395 if (get_option_parameter(dest, list->name) == NULL) {
396 dest[num_dest_options++] = *list;
397 dest[num_dest_options].name = NULL;
398 }
399 list++;
400 }
401
402 return dest;
403}
404
405/*
Kevin Wolfd3f24362009-05-18 16:42:09 +0200406 * Parses a parameter string (param) into an option list (dest).
407 *
Stefan Hajnoczi0e72e752010-12-07 09:35:54 +0000408 * list is the template option list. If dest is NULL, a new copy of list is
409 * created. If list is NULL, this function fails.
Kevin Wolfd3f24362009-05-18 16:42:09 +0200410 *
411 * A parameter string consists of one or more parameters, separated by commas.
412 * Each parameter consists of its name and possibly of a value. In the latter
413 * case, the value is delimited by an = character. To specify a value which
414 * contains commas, double each comma so it won't be recognized as the end of
415 * the parameter.
416 *
417 * For more details of the parsing see above.
418 *
419 * Returns a pointer to the first element of dest (or the newly allocated copy)
420 * or NULL in error cases
421 */
422QEMUOptionParameter *parse_option_parameters(const char *param,
423 QEMUOptionParameter *list, QEMUOptionParameter *dest)
424{
Kevin Wolfd3f24362009-05-18 16:42:09 +0200425 QEMUOptionParameter *allocated = NULL;
426 char name[256];
427 char value[256];
428 char *param_delim, *value_delim;
429 char next_delim;
Kevin Wolfd3f24362009-05-18 16:42:09 +0200430
431 if (list == NULL) {
432 return NULL;
433 }
434
435 if (dest == NULL) {
Stefan Hajnoczi898c2572010-12-07 09:35:53 +0000436 dest = allocated = append_option_parameters(NULL, list);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200437 }
438
439 while (*param) {
440
441 // Find parameter name and value in the string
442 param_delim = strchr(param, ',');
443 value_delim = strchr(param, '=');
444
445 if (value_delim && (value_delim < param_delim || !param_delim)) {
446 next_delim = '=';
447 } else {
448 next_delim = ',';
449 value_delim = NULL;
450 }
451
452 param = get_opt_name(name, sizeof(name), param, next_delim);
453 if (value_delim) {
454 param = get_opt_value(value, sizeof(value), param + 1);
455 }
456 if (*param != '\0') {
457 param++;
458 }
459
460 // Set the parameter
461 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
462 goto fail;
463 }
464 }
465
466 return dest;
467
468fail:
469 // Only free the list if it was newly allocated
470 free_option_parameters(allocated);
471 return NULL;
472}
473
474/*
475 * Prints all options of a list that have a value to stdout
476 */
477void print_option_parameters(QEMUOptionParameter *list)
478{
479 while (list && list->name) {
480 switch (list->type) {
481 case OPT_STRING:
482 if (list->value.s != NULL) {
483 printf("%s='%s' ", list->name, list->value.s);
484 }
485 break;
486 case OPT_FLAG:
487 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
488 break;
489 case OPT_SIZE:
490 case OPT_NUMBER:
491 printf("%s=%" PRId64 " ", list->name, list->value.n);
492 break;
493 default:
Dong Xu Wang07f35072011-11-22 18:06:26 +0800494 printf("%s=(unknown type) ", list->name);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200495 break;
496 }
497 list++;
498 }
499}
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200500
501/*
502 * Prints an overview of all available options
503 */
504void print_option_help(QEMUOptionParameter *list)
505{
506 printf("Supported options:\n");
507 while (list && list->name) {
508 printf("%-16s %s\n", list->name,
509 list->help ? list->help : "No description available");
510 list++;
511 }
512}
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200513
514/* ------------------------------------------------------------------ */
515
516struct QemuOpt {
517 const char *name;
518 const char *str;
519
Blue Swirl238431a2010-02-21 16:01:30 +0000520 const QemuOptDesc *desc;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200521 union {
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530522 bool boolean;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200523 uint64_t uint;
524 } value;
525
526 QemuOpts *opts;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000527 QTAILQ_ENTRY(QemuOpt) next;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200528};
529
530struct QemuOpts {
Jan Kiszkab09417b2009-12-01 15:24:18 +0100531 char *id;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200532 QemuOptsList *list;
Markus Armbruster827b0812010-02-18 19:46:49 +0100533 Location loc;
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100534 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000535 QTAILQ_ENTRY(QemuOpts) next;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200536};
537
538static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
539{
540 QemuOpt *opt;
541
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100542 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200543 if (strcmp(opt->name, name) != 0)
544 continue;
545 return opt;
546 }
547 return NULL;
548}
549
550const char *qemu_opt_get(QemuOpts *opts, const char *name)
551{
552 QemuOpt *opt = qemu_opt_find(opts, name);
553 return opt ? opt->str : NULL;
554}
555
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530556bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200557{
558 QemuOpt *opt = qemu_opt_find(opts, name);
559
560 if (opt == NULL)
561 return defval;
562 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
Juan Quintela1f5c1772009-09-21 13:08:34 +0200563 return opt->value.boolean;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200564}
565
566uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
567{
568 QemuOpt *opt = qemu_opt_find(opts, name);
569
570 if (opt == NULL)
571 return defval;
572 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
573 return opt->value.uint;
574}
575
576uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
577{
578 QemuOpt *opt = qemu_opt_find(opts, name);
579
580 if (opt == NULL)
581 return defval;
582 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
583 return opt->value.uint;
584}
585
586static int qemu_opt_parse(QemuOpt *opt)
587{
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300588 Error *local_err = NULL;
589
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200590 if (opt->desc == NULL)
591 return 0;
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300592
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200593 switch (opt->desc->type) {
594 case QEMU_OPT_STRING:
595 /* nothing */
596 return 0;
597 case QEMU_OPT_BOOL:
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300598 parse_option_bool(opt->name, opt->str, &opt->value.boolean, &local_err);
599 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200600 case QEMU_OPT_NUMBER:
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300601 parse_option_number(opt->name, opt->str, &opt->value.uint,
602 &local_err);
603 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200604 case QEMU_OPT_SIZE:
605 return parse_option_size(opt->name, opt->str, &opt->value.uint);
606 default:
607 abort();
608 }
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300609
610 if (error_is_set(&local_err)) {
611 qerror_report_err(local_err);
612 error_free(local_err);
613 return -1;
614 }
615
616 return 0;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200617}
618
619static void qemu_opt_del(QemuOpt *opt)
620{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000621 QTAILQ_REMOVE(&opt->opts->head, opt, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500622 g_free((/* !const */ char*)opt->name);
623 g_free((/* !const */ char*)opt->str);
624 g_free(opt);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200625}
626
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100627static int opt_set(QemuOpts *opts, const char *name, const char *value,
628 bool prepend)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200629{
630 QemuOpt *opt;
Blue Swirl238431a2010-02-21 16:01:30 +0000631 const QemuOptDesc *desc = opts->list->desc;
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100632 int i;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200633
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100634 for (i = 0; desc[i].name != NULL; i++) {
635 if (strcmp(desc[i].name, name) == 0) {
636 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200637 }
638 }
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100639 if (desc[i].name == NULL) {
640 if (i == 0) {
641 /* empty list -> allow any */;
642 } else {
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100643 qerror_report(QERR_INVALID_PARAMETER, name);
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100644 return -1;
645 }
646 }
647
Anthony Liguori7267c092011-08-20 22:09:37 -0500648 opt = g_malloc0(sizeof(*opt));
649 opt->name = g_strdup(name);
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100650 opt->opts = opts;
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100651 if (prepend) {
652 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
653 } else {
654 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
655 }
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100656 if (desc[i].name != NULL) {
657 opt->desc = desc+i;
658 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200659 if (value) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500660 opt->str = g_strdup(value);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200661 }
662 if (qemu_opt_parse(opt) < 0) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200663 qemu_opt_del(opt);
664 return -1;
665 }
666 return 0;
667}
668
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100669int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
670{
671 return opt_set(opts, name, value, false);
672}
673
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530674int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
675{
676 QemuOpt *opt;
677 const QemuOptDesc *desc = opts->list->desc;
678 int i;
679
680 for (i = 0; desc[i].name != NULL; i++) {
681 if (strcmp(desc[i].name, name) == 0) {
682 break;
683 }
684 }
685 if (desc[i].name == NULL) {
686 if (i == 0) {
687 /* empty list -> allow any */;
688 } else {
689 qerror_report(QERR_INVALID_PARAMETER, name);
690 return -1;
691 }
692 }
693
694 opt = g_malloc0(sizeof(*opt));
695 opt->name = g_strdup(name);
696 opt->opts = opts;
697 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
698 if (desc[i].name != NULL) {
699 opt->desc = desc+i;
700 }
701 opt->value.boolean = !!val;
702 return 0;
703}
704
Gerd Hoffmann48026072009-07-31 12:25:32 +0200705int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
706 int abort_on_failure)
707{
708 QemuOpt *opt;
709 int rc = 0;
710
Blue Swirl72cf2d42009-09-12 07:36:22 +0000711 QTAILQ_FOREACH(opt, &opts->head, next) {
Gerd Hoffmann48026072009-07-31 12:25:32 +0200712 rc = func(opt->name, opt->str, opaque);
713 if (abort_on_failure && rc != 0)
714 break;
715 }
716 return rc;
717}
718
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200719QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
720{
721 QemuOpts *opts;
722
Blue Swirl72cf2d42009-09-12 07:36:22 +0000723 QTAILQ_FOREACH(opts, &list->head, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200724 if (!opts->id) {
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100725 if (!id) {
726 return opts;
727 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200728 continue;
729 }
730 if (strcmp(opts->id, id) != 0) {
731 continue;
732 }
733 return opts;
734 }
735 return NULL;
736}
737
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200738static int id_wellformed(const char *id)
739{
740 int i;
741
742 if (!qemu_isalpha(id[0])) {
743 return 0;
744 }
745 for (i = 1; id[i]; i++) {
746 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
747 return 0;
748 }
749 }
750 return 1;
751}
752
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300753QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
754 int fail_if_exists, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200755{
756 QemuOpts *opts = NULL;
757
758 if (id) {
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200759 if (!id_wellformed(id)) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300760 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200761 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
762 return NULL;
763 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200764 opts = qemu_opts_find(list, id);
765 if (opts != NULL) {
Peter Maydellda933182012-02-08 05:41:37 +0000766 if (fail_if_exists && !list->merge_lists) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300767 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200768 return NULL;
769 } else {
770 return opts;
771 }
772 }
Peter Maydellda933182012-02-08 05:41:37 +0000773 } else if (list->merge_lists) {
774 opts = qemu_opts_find(list, NULL);
775 if (opts) {
776 return opts;
777 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200778 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500779 opts = g_malloc0(sizeof(*opts));
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200780 if (id) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500781 opts->id = g_strdup(id);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200782 }
783 opts->list = list;
Markus Armbruster827b0812010-02-18 19:46:49 +0100784 loc_save(&opts->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000785 QTAILQ_INIT(&opts->head);
786 QTAILQ_INSERT_TAIL(&list->head, opts, next);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200787 return opts;
788}
789
Markus Armbrusterbb67ab02010-06-01 10:47:34 +0200790void qemu_opts_reset(QemuOptsList *list)
791{
792 QemuOpts *opts, *next_opts;
793
794 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
795 qemu_opts_del(opts);
796 }
797}
798
Markus Armbruster94ac7262010-05-27 21:06:04 +0200799void qemu_opts_loc_restore(QemuOpts *opts)
800{
801 loc_restore(&opts->loc);
802}
803
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200804int qemu_opts_set(QemuOptsList *list, const char *id,
805 const char *name, const char *value)
806{
807 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300808 Error *local_err = NULL;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200809
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300810 opts = qemu_opts_create(list, id, 1, &local_err);
811 if (error_is_set(&local_err)) {
812 qerror_report_err(local_err);
813 error_free(local_err);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200814 return -1;
815 }
816 return qemu_opt_set(opts, name, value);
817}
818
Gerd Hoffmann48026072009-07-31 12:25:32 +0200819const char *qemu_opts_id(QemuOpts *opts)
820{
821 return opts->id;
822}
823
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200824void qemu_opts_del(QemuOpts *opts)
825{
826 QemuOpt *opt;
827
828 for (;;) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000829 opt = QTAILQ_FIRST(&opts->head);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200830 if (opt == NULL)
831 break;
832 qemu_opt_del(opt);
833 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000834 QTAILQ_REMOVE(&opts->list->head, opts, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500835 g_free(opts->id);
836 g_free(opts);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200837}
838
839int qemu_opts_print(QemuOpts *opts, void *dummy)
840{
841 QemuOpt *opt;
842
843 fprintf(stderr, "%s: %s:", opts->list->name,
844 opts->id ? opts->id : "<noid>");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000845 QTAILQ_FOREACH(opt, &opts->head, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200846 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
847 }
848 fprintf(stderr, "\n");
849 return 0;
850}
851
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100852static int opts_do_parse(QemuOpts *opts, const char *params,
853 const char *firstname, bool prepend)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200854{
Gerd Hoffmannd318ff92009-12-10 11:11:08 +0100855 char option[128], value[1024];
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200856 const char *p,*pe,*pc;
857
Mark McLoughlin2cfa5712009-10-06 12:17:02 +0100858 for (p = params; *p != '\0'; p++) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200859 pe = strchr(p, '=');
860 pc = strchr(p, ',');
861 if (!pe || (pc && pc < pe)) {
862 /* found "foo,more" */
863 if (p == params && firstname) {
864 /* implicitly named first option */
865 pstrcpy(option, sizeof(option), firstname);
866 p = get_opt_value(value, sizeof(value), p);
867 } else {
868 /* option without value, probably a flag */
869 p = get_opt_name(option, sizeof(option), p, ',');
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200870 if (strncmp(option, "no", 2) == 0) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200871 memmove(option, option+2, strlen(option+2)+1);
872 pstrcpy(value, sizeof(value), "off");
873 } else {
874 pstrcpy(value, sizeof(value), "on");
875 }
876 }
877 } else {
878 /* found "foo=bar,more" */
879 p = get_opt_name(option, sizeof(option), p, '=');
880 if (*p != '=') {
881 break;
882 }
883 p++;
884 p = get_opt_value(value, sizeof(value), p);
885 }
886 if (strcmp(option, "id") != 0) {
887 /* store and parse */
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100888 if (opt_set(opts, option, value, prepend) == -1) {
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200889 return -1;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200890 }
891 }
892 if (*p != ',') {
893 break;
894 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200895 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200896 return 0;
897}
898
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100899int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
900{
901 return opts_do_parse(opts, params, firstname, false);
902}
903
904static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
905 int permit_abbrev, bool defaults)
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200906{
Markus Armbruster8212c642010-02-10 19:52:18 +0100907 const char *firstname;
Gerd Hoffmannd318ff92009-12-10 11:11:08 +0100908 char value[1024], *id = NULL;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200909 const char *p;
910 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300911 Error *local_err = NULL;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200912
Markus Armbruster8212c642010-02-10 19:52:18 +0100913 assert(!permit_abbrev || list->implied_opt_name);
914 firstname = permit_abbrev ? list->implied_opt_name : NULL;
915
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200916 if (strncmp(params, "id=", 3) == 0) {
917 get_opt_value(value, sizeof(value), params+3);
Jan Kiszkad510c5c2010-04-29 18:24:43 +0200918 id = value;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200919 } else if ((p = strstr(params, ",id=")) != NULL) {
920 get_opt_value(value, sizeof(value), p+4);
Jan Kiszkad510c5c2010-04-29 18:24:43 +0200921 id = value;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200922 }
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100923 if (defaults) {
924 if (!id && !QTAILQ_EMPTY(&list->head)) {
925 opts = qemu_opts_find(list, NULL);
926 } else {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300927 opts = qemu_opts_create(list, id, 0, &local_err);
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100928 }
929 } else {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300930 opts = qemu_opts_create(list, id, 1, &local_err);
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100931 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300932 if (opts == NULL) {
933 if (error_is_set(&local_err)) {
934 qerror_report_err(local_err);
935 error_free(local_err);
936 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200937 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300938 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200939
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100940 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200941 qemu_opts_del(opts);
942 return NULL;
943 }
944
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200945 return opts;
946}
947
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100948QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
949 int permit_abbrev)
950{
951 return opts_parse(list, params, permit_abbrev, false);
952}
953
954void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
955 int permit_abbrev)
956{
957 QemuOpts *opts;
958
959 opts = opts_parse(list, params, permit_abbrev, true);
960 assert(opts);
961}
962
Markus Armbruster01e7f182010-02-10 20:15:29 +0100963static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
964{
965 char buf[32];
966 const char *value;
967 int n;
968
969 if (!strcmp(key, "id")) {
970 return;
971 }
972
973 switch (qobject_type(obj)) {
974 case QTYPE_QSTRING:
975 value = qstring_get_str(qobject_to_qstring(obj));
976 break;
977 case QTYPE_QINT:
978 n = snprintf(buf, sizeof(buf), "%" PRId64,
979 qint_get_int(qobject_to_qint(obj)));
980 assert(n < sizeof(buf));
981 value = buf;
982 break;
983 case QTYPE_QFLOAT:
984 n = snprintf(buf, sizeof(buf), "%.17g",
985 qfloat_get_double(qobject_to_qfloat(obj)));
986 assert(n < sizeof(buf));
987 value = buf;
988 break;
989 case QTYPE_QBOOL:
Blue Swirld35215f2010-03-18 20:48:19 +0000990 pstrcpy(buf, sizeof(buf),
991 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
Markus Armbruster01e7f182010-02-10 20:15:29 +0100992 value = buf;
993 break;
994 default:
995 return;
996 }
997 qemu_opt_set(opaque, key, value);
998}
999
1000/*
1001 * Create QemuOpts from a QDict.
1002 * Use value of key "id" as ID if it exists and is a QString.
1003 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1004 * other types are silently ignored.
1005 */
1006QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
1007{
1008 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001009 Error *local_err = NULL;
Markus Armbruster01e7f182010-02-10 20:15:29 +01001010
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001011 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1012 &local_err);
1013 if (error_is_set(&local_err)) {
1014 qerror_report_err(local_err);
1015 error_free(local_err);
Markus Armbruster01e7f182010-02-10 20:15:29 +01001016 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001017 }
Markus Armbruster01e7f182010-02-10 20:15:29 +01001018
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001019 assert(opts != NULL);
Markus Armbruster01e7f182010-02-10 20:15:29 +01001020 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
1021 return opts;
1022}
1023
1024/*
1025 * Convert from QemuOpts to QDict.
1026 * The QDict values are of type QString.
1027 * TODO We'll want to use types appropriate for opt->desc->type, but
1028 * this is enough for now.
1029 */
1030QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1031{
1032 QemuOpt *opt;
1033 QObject *val;
1034
1035 if (!qdict) {
1036 qdict = qdict_new();
1037 }
1038 if (opts->id) {
1039 qdict_put(qdict, "id", qstring_from_str(opts->id));
1040 }
1041 QTAILQ_FOREACH(opt, &opts->head, next) {
1042 val = QOBJECT(qstring_from_str(opt->str));
1043 qdict_put_obj(qdict, opt->name, val);
1044 }
1045 return qdict;
1046}
1047
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001048/* Validate parsed opts against descriptions where no
1049 * descriptions were provided in the QemuOptsList.
1050 */
Blue Swirl238431a2010-02-21 16:01:30 +00001051int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001052{
1053 QemuOpt *opt;
1054
1055 assert(opts->list->desc[0].name == NULL);
1056
1057 QTAILQ_FOREACH(opt, &opts->head, next) {
1058 int i;
1059
1060 for (i = 0; desc[i].name != NULL; i++) {
1061 if (strcmp(desc[i].name, opt->name) == 0) {
1062 break;
1063 }
1064 }
1065 if (desc[i].name == NULL) {
Markus Armbrusterdb716e92010-03-25 17:22:37 +01001066 qerror_report(QERR_INVALID_PARAMETER, opt->name);
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001067 return -1;
1068 }
1069
1070 opt->desc = &desc[i];
1071
1072 if (qemu_opt_parse(opt) < 0) {
1073 return -1;
1074 }
1075 }
1076
1077 return 0;
1078}
1079
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001080int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1081 int abort_on_failure)
1082{
Markus Armbruster827b0812010-02-18 19:46:49 +01001083 Location loc;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001084 QemuOpts *opts;
1085 int rc = 0;
1086
Markus Armbruster827b0812010-02-18 19:46:49 +01001087 loc_push_none(&loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001088 QTAILQ_FOREACH(opts, &list->head, next) {
Markus Armbruster827b0812010-02-18 19:46:49 +01001089 loc_restore(&opts->loc);
Markus Armbruster4a2594d2010-01-29 19:48:56 +01001090 rc |= func(opts, opaque);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001091 if (abort_on_failure && rc != 0)
1092 break;
1093 }
Markus Armbruster827b0812010-02-18 19:46:49 +01001094 loc_pop(&loc);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001095 return rc;
1096}