blob: 72dcb566d8650726a01655b4dcf357f75f2f85d3 [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
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530172static int parse_option_bool(const char *name, const char *value, bool *ret)
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200173{
174 if (value != NULL) {
175 if (!strcmp(value, "on")) {
176 *ret = 1;
177 } else if (!strcmp(value, "off")) {
178 *ret = 0;
179 } else {
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100180 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200181 return -1;
182 }
183 } else {
184 *ret = 1;
185 }
186 return 0;
187}
188
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300189static void parse_option_number(const char *name, const char *value,
190 uint64_t *ret, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200191{
192 char *postfix;
193 uint64_t number;
194
195 if (value != NULL) {
196 number = strtoull(value, &postfix, 0);
197 if (*postfix != '\0') {
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300198 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
199 return;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200200 }
Gerd Hoffmann21c9f4c2009-07-29 10:39:59 +0200201 *ret = number;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200202 } else {
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300203 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200204 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200205}
206
Gerd Hoffmann76950192009-07-22 16:43:02 +0200207static int parse_option_size(const char *name, const char *value, uint64_t *ret)
208{
209 char *postfix;
210 double sizef;
211
212 if (value != NULL) {
213 sizef = strtod(value, &postfix);
214 switch (*postfix) {
215 case 'T':
216 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100217 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200218 case 'G':
219 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100220 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200221 case 'M':
222 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100223 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200224 case 'K':
225 case 'k':
226 sizef *= 1024;
Stefan Weil0b0404b2012-01-09 18:29:51 +0100227 /* fall through */
Gerd Hoffmann76950192009-07-22 16:43:02 +0200228 case 'b':
229 case '\0':
230 *ret = (uint64_t) sizef;
231 break;
232 default:
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100233 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
234 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
Gerd Hoffmann76950192009-07-22 16:43:02 +0200235 "kilobytes, megabytes, gigabytes and terabytes.\n");
236 return -1;
237 }
238 } else {
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100239 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
Gerd Hoffmann76950192009-07-22 16:43:02 +0200240 return -1;
241 }
242 return 0;
243}
244
Kevin Wolfd3f24362009-05-18 16:42:09 +0200245/*
246 * Sets the value of a parameter in a given option list. The parsing of the
247 * value depends on the type of option:
248 *
249 * OPT_FLAG (uses value.n):
250 * If no value is given, the flag is set to 1.
251 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
252 *
253 * OPT_STRING (uses value.s):
254 * value is strdup()ed and assigned as option value
255 *
256 * OPT_SIZE (uses value.n):
257 * The value is converted to an integer. Suffixes for kilobytes etc. are
258 * allowed (powers of 1024).
259 *
260 * Returns 0 on succes, -1 in error cases
261 */
262int set_option_parameter(QEMUOptionParameter *list, const char *name,
263 const char *value)
264{
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530265 bool flag;
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:
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100277 if (parse_option_bool(name, value, &flag) == -1)
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200278 return -1;
279 list->value.n = flag;
Kevin Wolfd3f24362009-05-18 16:42:09 +0200280 break;
281
282 case OPT_STRING:
283 if (value != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500284 list->value.s = g_strdup(value);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200285 } else {
286 fprintf(stderr, "Option '%s' needs a parameter\n", name);
287 return -1;
288 }
289 break;
290
291 case OPT_SIZE:
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100292 if (parse_option_size(name, value, &list->value.n) == -1)
Kevin Wolfd3f24362009-05-18 16:42:09 +0200293 return -1;
Kevin Wolfd3f24362009-05-18 16:42:09 +0200294 break;
Gerd Hoffmann76950192009-07-22 16:43:02 +0200295
Kevin Wolfd3f24362009-05-18 16:42:09 +0200296 default:
297 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
298 return -1;
299 }
300
301 return 0;
302}
303
304/*
305 * Sets the given parameter to an integer instead of a string.
306 * This function cannot be used to set string options.
307 *
308 * Returns 0 on success, -1 in error cases
309 */
310int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
311 uint64_t value)
312{
313 // Find a matching parameter
314 list = get_option_parameter(list, name);
315 if (list == NULL) {
316 fprintf(stderr, "Unknown option '%s'\n", name);
317 return -1;
318 }
319
320 // Process parameter
321 switch (list->type) {
322 case OPT_FLAG:
323 case OPT_NUMBER:
324 case OPT_SIZE:
325 list->value.n = value;
326 break;
327
328 default:
329 return -1;
330 }
331
332 return 0;
333}
334
335/*
336 * Frees a option list. If it contains strings, the strings are freed as well.
337 */
338void free_option_parameters(QEMUOptionParameter *list)
339{
340 QEMUOptionParameter *cur = list;
341
342 while (cur && cur->name) {
343 if (cur->type == OPT_STRING) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500344 g_free(cur->value.s);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200345 }
346 cur++;
347 }
348
Anthony Liguori7267c092011-08-20 22:09:37 -0500349 g_free(list);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200350}
351
352/*
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900353 * Count valid options in list
354 */
355static size_t count_option_parameters(QEMUOptionParameter *list)
356{
357 size_t num_options = 0;
358
359 while (list && list->name) {
360 num_options++;
361 list++;
362 }
363
364 return num_options;
365}
366
367/*
368 * Append an option list (list) to an option list (dest).
369 *
370 * If dest is NULL, a new copy of list is created.
371 *
372 * Returns a pointer to the first element of dest (or the newly allocated copy)
373 */
374QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
375 QEMUOptionParameter *list)
376{
377 size_t num_options, num_dest_options;
378
379 num_options = count_option_parameters(dest);
380 num_dest_options = num_options;
381
382 num_options += count_option_parameters(list);
383
Anthony Liguori7267c092011-08-20 22:09:37 -0500384 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
Kevin Wolfbd69fe82010-06-11 10:19:41 +0200385 dest[num_dest_options].name = NULL;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900386
387 while (list && list->name) {
388 if (get_option_parameter(dest, list->name) == NULL) {
389 dest[num_dest_options++] = *list;
390 dest[num_dest_options].name = NULL;
391 }
392 list++;
393 }
394
395 return dest;
396}
397
398/*
Kevin Wolfd3f24362009-05-18 16:42:09 +0200399 * Parses a parameter string (param) into an option list (dest).
400 *
Stefan Hajnoczi0e72e752010-12-07 09:35:54 +0000401 * list is the template option list. If dest is NULL, a new copy of list is
402 * created. If list is NULL, this function fails.
Kevin Wolfd3f24362009-05-18 16:42:09 +0200403 *
404 * A parameter string consists of one or more parameters, separated by commas.
405 * Each parameter consists of its name and possibly of a value. In the latter
406 * case, the value is delimited by an = character. To specify a value which
407 * contains commas, double each comma so it won't be recognized as the end of
408 * the parameter.
409 *
410 * For more details of the parsing see above.
411 *
412 * Returns a pointer to the first element of dest (or the newly allocated copy)
413 * or NULL in error cases
414 */
415QEMUOptionParameter *parse_option_parameters(const char *param,
416 QEMUOptionParameter *list, QEMUOptionParameter *dest)
417{
Kevin Wolfd3f24362009-05-18 16:42:09 +0200418 QEMUOptionParameter *allocated = NULL;
419 char name[256];
420 char value[256];
421 char *param_delim, *value_delim;
422 char next_delim;
Kevin Wolfd3f24362009-05-18 16:42:09 +0200423
424 if (list == NULL) {
425 return NULL;
426 }
427
428 if (dest == NULL) {
Stefan Hajnoczi898c2572010-12-07 09:35:53 +0000429 dest = allocated = append_option_parameters(NULL, list);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200430 }
431
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
467/*
468 * Prints all options of a list that have a value to stdout
469 */
470void print_option_parameters(QEMUOptionParameter *list)
471{
472 while (list && list->name) {
473 switch (list->type) {
474 case OPT_STRING:
475 if (list->value.s != NULL) {
476 printf("%s='%s' ", list->name, list->value.s);
477 }
478 break;
479 case OPT_FLAG:
480 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
481 break;
482 case OPT_SIZE:
483 case OPT_NUMBER:
484 printf("%s=%" PRId64 " ", list->name, list->value.n);
485 break;
486 default:
Dong Xu Wang07f35072011-11-22 18:06:26 +0800487 printf("%s=(unknown type) ", list->name);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200488 break;
489 }
490 list++;
491 }
492}
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200493
494/*
495 * Prints an overview of all available options
496 */
497void print_option_help(QEMUOptionParameter *list)
498{
499 printf("Supported options:\n");
500 while (list && list->name) {
501 printf("%-16s %s\n", list->name,
502 list->help ? list->help : "No description available");
503 list++;
504 }
505}
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200506
507/* ------------------------------------------------------------------ */
508
509struct QemuOpt {
510 const char *name;
511 const char *str;
512
Blue Swirl238431a2010-02-21 16:01:30 +0000513 const QemuOptDesc *desc;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200514 union {
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530515 bool boolean;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200516 uint64_t uint;
517 } value;
518
519 QemuOpts *opts;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000520 QTAILQ_ENTRY(QemuOpt) next;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200521};
522
523struct QemuOpts {
Jan Kiszkab09417b2009-12-01 15:24:18 +0100524 char *id;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200525 QemuOptsList *list;
Markus Armbruster827b0812010-02-18 19:46:49 +0100526 Location loc;
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100527 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000528 QTAILQ_ENTRY(QemuOpts) next;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200529};
530
531static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
532{
533 QemuOpt *opt;
534
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100535 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200536 if (strcmp(opt->name, name) != 0)
537 continue;
538 return opt;
539 }
540 return NULL;
541}
542
543const char *qemu_opt_get(QemuOpts *opts, const char *name)
544{
545 QemuOpt *opt = qemu_opt_find(opts, name);
546 return opt ? opt->str : NULL;
547}
548
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530549bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200550{
551 QemuOpt *opt = qemu_opt_find(opts, name);
552
553 if (opt == NULL)
554 return defval;
555 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
Juan Quintela1f5c1772009-09-21 13:08:34 +0200556 return opt->value.boolean;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200557}
558
559uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
560{
561 QemuOpt *opt = qemu_opt_find(opts, name);
562
563 if (opt == NULL)
564 return defval;
565 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
566 return opt->value.uint;
567}
568
569uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
570{
571 QemuOpt *opt = qemu_opt_find(opts, name);
572
573 if (opt == NULL)
574 return defval;
575 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
576 return opt->value.uint;
577}
578
579static int qemu_opt_parse(QemuOpt *opt)
580{
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300581 Error *local_err = NULL;
582
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200583 if (opt->desc == NULL)
584 return 0;
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300585
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200586 switch (opt->desc->type) {
587 case QEMU_OPT_STRING:
588 /* nothing */
589 return 0;
590 case QEMU_OPT_BOOL:
Juan Quintela1f5c1772009-09-21 13:08:34 +0200591 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200592 case QEMU_OPT_NUMBER:
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300593 parse_option_number(opt->name, opt->str, &opt->value.uint,
594 &local_err);
595 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200596 case QEMU_OPT_SIZE:
597 return parse_option_size(opt->name, opt->str, &opt->value.uint);
598 default:
599 abort();
600 }
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300601
602 if (error_is_set(&local_err)) {
603 qerror_report_err(local_err);
604 error_free(local_err);
605 return -1;
606 }
607
608 return 0;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200609}
610
611static void qemu_opt_del(QemuOpt *opt)
612{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000613 QTAILQ_REMOVE(&opt->opts->head, opt, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500614 g_free((/* !const */ char*)opt->name);
615 g_free((/* !const */ char*)opt->str);
616 g_free(opt);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200617}
618
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100619static int opt_set(QemuOpts *opts, const char *name, const char *value,
620 bool prepend)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200621{
622 QemuOpt *opt;
Blue Swirl238431a2010-02-21 16:01:30 +0000623 const QemuOptDesc *desc = opts->list->desc;
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100624 int i;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200625
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100626 for (i = 0; desc[i].name != NULL; i++) {
627 if (strcmp(desc[i].name, name) == 0) {
628 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200629 }
630 }
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100631 if (desc[i].name == NULL) {
632 if (i == 0) {
633 /* empty list -> allow any */;
634 } else {
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100635 qerror_report(QERR_INVALID_PARAMETER, name);
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100636 return -1;
637 }
638 }
639
Anthony Liguori7267c092011-08-20 22:09:37 -0500640 opt = g_malloc0(sizeof(*opt));
641 opt->name = g_strdup(name);
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100642 opt->opts = opts;
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100643 if (prepend) {
644 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
645 } else {
646 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
647 }
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100648 if (desc[i].name != NULL) {
649 opt->desc = desc+i;
650 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200651 if (value) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500652 opt->str = g_strdup(value);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200653 }
654 if (qemu_opt_parse(opt) < 0) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200655 qemu_opt_del(opt);
656 return -1;
657 }
658 return 0;
659}
660
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100661int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
662{
663 return opt_set(opts, name, value, false);
664}
665
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530666int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
667{
668 QemuOpt *opt;
669 const QemuOptDesc *desc = opts->list->desc;
670 int i;
671
672 for (i = 0; desc[i].name != NULL; i++) {
673 if (strcmp(desc[i].name, name) == 0) {
674 break;
675 }
676 }
677 if (desc[i].name == NULL) {
678 if (i == 0) {
679 /* empty list -> allow any */;
680 } else {
681 qerror_report(QERR_INVALID_PARAMETER, name);
682 return -1;
683 }
684 }
685
686 opt = g_malloc0(sizeof(*opt));
687 opt->name = g_strdup(name);
688 opt->opts = opts;
689 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
690 if (desc[i].name != NULL) {
691 opt->desc = desc+i;
692 }
693 opt->value.boolean = !!val;
694 return 0;
695}
696
Gerd Hoffmann48026072009-07-31 12:25:32 +0200697int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
698 int abort_on_failure)
699{
700 QemuOpt *opt;
701 int rc = 0;
702
Blue Swirl72cf2d42009-09-12 07:36:22 +0000703 QTAILQ_FOREACH(opt, &opts->head, next) {
Gerd Hoffmann48026072009-07-31 12:25:32 +0200704 rc = func(opt->name, opt->str, opaque);
705 if (abort_on_failure && rc != 0)
706 break;
707 }
708 return rc;
709}
710
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200711QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
712{
713 QemuOpts *opts;
714
Blue Swirl72cf2d42009-09-12 07:36:22 +0000715 QTAILQ_FOREACH(opts, &list->head, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200716 if (!opts->id) {
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100717 if (!id) {
718 return opts;
719 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200720 continue;
721 }
722 if (strcmp(opts->id, id) != 0) {
723 continue;
724 }
725 return opts;
726 }
727 return NULL;
728}
729
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200730static int id_wellformed(const char *id)
731{
732 int i;
733
734 if (!qemu_isalpha(id[0])) {
735 return 0;
736 }
737 for (i = 1; id[i]; i++) {
738 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
739 return 0;
740 }
741 }
742 return 1;
743}
744
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300745QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
746 int fail_if_exists, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200747{
748 QemuOpts *opts = NULL;
749
750 if (id) {
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200751 if (!id_wellformed(id)) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300752 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200753 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
754 return NULL;
755 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200756 opts = qemu_opts_find(list, id);
757 if (opts != NULL) {
Peter Maydellda933182012-02-08 05:41:37 +0000758 if (fail_if_exists && !list->merge_lists) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300759 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200760 return NULL;
761 } else {
762 return opts;
763 }
764 }
Peter Maydellda933182012-02-08 05:41:37 +0000765 } else if (list->merge_lists) {
766 opts = qemu_opts_find(list, NULL);
767 if (opts) {
768 return opts;
769 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200770 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500771 opts = g_malloc0(sizeof(*opts));
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200772 if (id) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500773 opts->id = g_strdup(id);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200774 }
775 opts->list = list;
Markus Armbruster827b0812010-02-18 19:46:49 +0100776 loc_save(&opts->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000777 QTAILQ_INIT(&opts->head);
778 QTAILQ_INSERT_TAIL(&list->head, opts, next);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200779 return opts;
780}
781
Markus Armbrusterbb67ab02010-06-01 10:47:34 +0200782void qemu_opts_reset(QemuOptsList *list)
783{
784 QemuOpts *opts, *next_opts;
785
786 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
787 qemu_opts_del(opts);
788 }
789}
790
Markus Armbruster94ac7262010-05-27 21:06:04 +0200791void qemu_opts_loc_restore(QemuOpts *opts)
792{
793 loc_restore(&opts->loc);
794}
795
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200796int qemu_opts_set(QemuOptsList *list, const char *id,
797 const char *name, const char *value)
798{
799 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300800 Error *local_err = NULL;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200801
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300802 opts = qemu_opts_create(list, id, 1, &local_err);
803 if (error_is_set(&local_err)) {
804 qerror_report_err(local_err);
805 error_free(local_err);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200806 return -1;
807 }
808 return qemu_opt_set(opts, name, value);
809}
810
Gerd Hoffmann48026072009-07-31 12:25:32 +0200811const char *qemu_opts_id(QemuOpts *opts)
812{
813 return opts->id;
814}
815
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200816void qemu_opts_del(QemuOpts *opts)
817{
818 QemuOpt *opt;
819
820 for (;;) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000821 opt = QTAILQ_FIRST(&opts->head);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200822 if (opt == NULL)
823 break;
824 qemu_opt_del(opt);
825 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000826 QTAILQ_REMOVE(&opts->list->head, opts, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500827 g_free(opts->id);
828 g_free(opts);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200829}
830
831int qemu_opts_print(QemuOpts *opts, void *dummy)
832{
833 QemuOpt *opt;
834
835 fprintf(stderr, "%s: %s:", opts->list->name,
836 opts->id ? opts->id : "<noid>");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000837 QTAILQ_FOREACH(opt, &opts->head, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200838 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
839 }
840 fprintf(stderr, "\n");
841 return 0;
842}
843
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100844static int opts_do_parse(QemuOpts *opts, const char *params,
845 const char *firstname, bool prepend)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200846{
Gerd Hoffmannd318ff92009-12-10 11:11:08 +0100847 char option[128], value[1024];
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200848 const char *p,*pe,*pc;
849
Mark McLoughlin2cfa5712009-10-06 12:17:02 +0100850 for (p = params; *p != '\0'; p++) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200851 pe = strchr(p, '=');
852 pc = strchr(p, ',');
853 if (!pe || (pc && pc < pe)) {
854 /* found "foo,more" */
855 if (p == params && firstname) {
856 /* implicitly named first option */
857 pstrcpy(option, sizeof(option), firstname);
858 p = get_opt_value(value, sizeof(value), p);
859 } else {
860 /* option without value, probably a flag */
861 p = get_opt_name(option, sizeof(option), p, ',');
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200862 if (strncmp(option, "no", 2) == 0) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200863 memmove(option, option+2, strlen(option+2)+1);
864 pstrcpy(value, sizeof(value), "off");
865 } else {
866 pstrcpy(value, sizeof(value), "on");
867 }
868 }
869 } else {
870 /* found "foo=bar,more" */
871 p = get_opt_name(option, sizeof(option), p, '=');
872 if (*p != '=') {
873 break;
874 }
875 p++;
876 p = get_opt_value(value, sizeof(value), p);
877 }
878 if (strcmp(option, "id") != 0) {
879 /* store and parse */
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100880 if (opt_set(opts, option, value, prepend) == -1) {
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200881 return -1;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200882 }
883 }
884 if (*p != ',') {
885 break;
886 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200887 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200888 return 0;
889}
890
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100891int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
892{
893 return opts_do_parse(opts, params, firstname, false);
894}
895
896static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
897 int permit_abbrev, bool defaults)
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200898{
Markus Armbruster8212c642010-02-10 19:52:18 +0100899 const char *firstname;
Gerd Hoffmannd318ff92009-12-10 11:11:08 +0100900 char value[1024], *id = NULL;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200901 const char *p;
902 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300903 Error *local_err = NULL;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200904
Markus Armbruster8212c642010-02-10 19:52:18 +0100905 assert(!permit_abbrev || list->implied_opt_name);
906 firstname = permit_abbrev ? list->implied_opt_name : NULL;
907
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200908 if (strncmp(params, "id=", 3) == 0) {
909 get_opt_value(value, sizeof(value), params+3);
Jan Kiszkad510c5c2010-04-29 18:24:43 +0200910 id = value;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200911 } else if ((p = strstr(params, ",id=")) != NULL) {
912 get_opt_value(value, sizeof(value), p+4);
Jan Kiszkad510c5c2010-04-29 18:24:43 +0200913 id = value;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200914 }
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100915 if (defaults) {
916 if (!id && !QTAILQ_EMPTY(&list->head)) {
917 opts = qemu_opts_find(list, NULL);
918 } else {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300919 opts = qemu_opts_create(list, id, 0, &local_err);
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100920 }
921 } else {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300922 opts = qemu_opts_create(list, id, 1, &local_err);
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100923 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300924 if (opts == NULL) {
925 if (error_is_set(&local_err)) {
926 qerror_report_err(local_err);
927 error_free(local_err);
928 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200929 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300930 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200931
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100932 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200933 qemu_opts_del(opts);
934 return NULL;
935 }
936
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200937 return opts;
938}
939
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100940QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
941 int permit_abbrev)
942{
943 return opts_parse(list, params, permit_abbrev, false);
944}
945
946void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
947 int permit_abbrev)
948{
949 QemuOpts *opts;
950
951 opts = opts_parse(list, params, permit_abbrev, true);
952 assert(opts);
953}
954
Markus Armbruster01e7f182010-02-10 20:15:29 +0100955static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
956{
957 char buf[32];
958 const char *value;
959 int n;
960
961 if (!strcmp(key, "id")) {
962 return;
963 }
964
965 switch (qobject_type(obj)) {
966 case QTYPE_QSTRING:
967 value = qstring_get_str(qobject_to_qstring(obj));
968 break;
969 case QTYPE_QINT:
970 n = snprintf(buf, sizeof(buf), "%" PRId64,
971 qint_get_int(qobject_to_qint(obj)));
972 assert(n < sizeof(buf));
973 value = buf;
974 break;
975 case QTYPE_QFLOAT:
976 n = snprintf(buf, sizeof(buf), "%.17g",
977 qfloat_get_double(qobject_to_qfloat(obj)));
978 assert(n < sizeof(buf));
979 value = buf;
980 break;
981 case QTYPE_QBOOL:
Blue Swirld35215f2010-03-18 20:48:19 +0000982 pstrcpy(buf, sizeof(buf),
983 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
Markus Armbruster01e7f182010-02-10 20:15:29 +0100984 value = buf;
985 break;
986 default:
987 return;
988 }
989 qemu_opt_set(opaque, key, value);
990}
991
992/*
993 * Create QemuOpts from a QDict.
994 * Use value of key "id" as ID if it exists and is a QString.
995 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
996 * other types are silently ignored.
997 */
998QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
999{
1000 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001001 Error *local_err = NULL;
Markus Armbruster01e7f182010-02-10 20:15:29 +01001002
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001003 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1004 &local_err);
1005 if (error_is_set(&local_err)) {
1006 qerror_report_err(local_err);
1007 error_free(local_err);
Markus Armbruster01e7f182010-02-10 20:15:29 +01001008 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001009 }
Markus Armbruster01e7f182010-02-10 20:15:29 +01001010
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001011 assert(opts != NULL);
Markus Armbruster01e7f182010-02-10 20:15:29 +01001012 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
1013 return opts;
1014}
1015
1016/*
1017 * Convert from QemuOpts to QDict.
1018 * The QDict values are of type QString.
1019 * TODO We'll want to use types appropriate for opt->desc->type, but
1020 * this is enough for now.
1021 */
1022QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1023{
1024 QemuOpt *opt;
1025 QObject *val;
1026
1027 if (!qdict) {
1028 qdict = qdict_new();
1029 }
1030 if (opts->id) {
1031 qdict_put(qdict, "id", qstring_from_str(opts->id));
1032 }
1033 QTAILQ_FOREACH(opt, &opts->head, next) {
1034 val = QOBJECT(qstring_from_str(opt->str));
1035 qdict_put_obj(qdict, opt->name, val);
1036 }
1037 return qdict;
1038}
1039
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001040/* Validate parsed opts against descriptions where no
1041 * descriptions were provided in the QemuOptsList.
1042 */
Blue Swirl238431a2010-02-21 16:01:30 +00001043int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001044{
1045 QemuOpt *opt;
1046
1047 assert(opts->list->desc[0].name == NULL);
1048
1049 QTAILQ_FOREACH(opt, &opts->head, next) {
1050 int i;
1051
1052 for (i = 0; desc[i].name != NULL; i++) {
1053 if (strcmp(desc[i].name, opt->name) == 0) {
1054 break;
1055 }
1056 }
1057 if (desc[i].name == NULL) {
Markus Armbrusterdb716e92010-03-25 17:22:37 +01001058 qerror_report(QERR_INVALID_PARAMETER, opt->name);
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001059 return -1;
1060 }
1061
1062 opt->desc = &desc[i];
1063
1064 if (qemu_opt_parse(opt) < 0) {
1065 return -1;
1066 }
1067 }
1068
1069 return 0;
1070}
1071
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001072int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1073 int abort_on_failure)
1074{
Markus Armbruster827b0812010-02-18 19:46:49 +01001075 Location loc;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001076 QemuOpts *opts;
1077 int rc = 0;
1078
Markus Armbruster827b0812010-02-18 19:46:49 +01001079 loc_push_none(&loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001080 QTAILQ_FOREACH(opts, &list->head, next) {
Markus Armbruster827b0812010-02-18 19:46:49 +01001081 loc_restore(&opts->loc);
Markus Armbruster4a2594d2010-01-29 19:48:56 +01001082 rc |= func(opts, opaque);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001083 if (abort_on_failure && rc != 0)
1084 break;
1085 }
Markus Armbruster827b0812010-02-18 19:46:49 +01001086 loc_pop(&loc);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001087 return rc;
1088}