blob: 27891e74e7c5713aacd902f2dda417097885edfd [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"
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -030032#include "error.h"
Markus Armbruster975b63a2010-03-25 17:22:32 +010033#include "qerror.h"
Laszlo Ersekfdb17972012-07-17 16:17:08 +020034#include "qemu-option-internal.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
Luiz Capitulinoec7b2cc2012-03-21 15:03:45 -0300206static void parse_option_size(const char *name, const char *value,
207 uint64_t *ret, Error **errp)
Gerd Hoffmann76950192009-07-22 16:43:02 +0200208{
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:
Luiz Capitulinoec7b2cc2012-03-21 15:03:45 -0300233 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
Markus Armbrusterc64f27d2010-03-25 17:22:34 +0100234 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");
Luiz Capitulinoec7b2cc2012-03-21 15:03:45 -0300236 return;
Gerd Hoffmann76950192009-07-22 16:43:02 +0200237 }
238 } else {
Luiz Capitulinoec7b2cc2012-03-21 15:03:45 -0300239 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
Gerd Hoffmann76950192009-07-22 16:43:02 +0200240 }
Gerd Hoffmann76950192009-07-22 16:43:02 +0200241}
242
Kevin Wolfd3f24362009-05-18 16:42:09 +0200243/*
244 * Sets the value of a parameter in a given option list. The parsing of the
245 * value depends on the type of option:
246 *
247 * OPT_FLAG (uses value.n):
248 * If no value is given, the flag is set to 1.
249 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
250 *
251 * OPT_STRING (uses value.s):
252 * value is strdup()ed and assigned as option value
253 *
254 * OPT_SIZE (uses value.n):
255 * The value is converted to an integer. Suffixes for kilobytes etc. are
256 * allowed (powers of 1024).
257 *
258 * Returns 0 on succes, -1 in error cases
259 */
260int set_option_parameter(QEMUOptionParameter *list, const char *name,
261 const char *value)
262{
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530263 bool flag;
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300264 Error *local_err = NULL;
Gerd Hoffmann67b13552009-07-22 16:43:01 +0200265
Kevin Wolfd3f24362009-05-18 16:42:09 +0200266 // Find a matching parameter
267 list = get_option_parameter(list, name);
268 if (list == NULL) {
269 fprintf(stderr, "Unknown option '%s'\n", name);
270 return -1;
271 }
272
273 // Process parameter
274 switch (list->type) {
275 case OPT_FLAG:
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300276 parse_option_bool(name, value, &flag, &local_err);
277 if (!error_is_set(&local_err)) {
278 list->value.n = flag;
279 }
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:
Luiz Capitulinoec7b2cc2012-03-21 15:03:45 -0300292 parse_option_size(name, value, &list->value.n, &local_err);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200293 break;
Gerd Hoffmann76950192009-07-22 16:43:02 +0200294
Kevin Wolfd3f24362009-05-18 16:42:09 +0200295 default:
296 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
297 return -1;
298 }
299
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300300 if (error_is_set(&local_err)) {
301 qerror_report_err(local_err);
302 error_free(local_err);
303 return -1;
304 }
305
Kevin Wolfd3f24362009-05-18 16:42:09 +0200306 return 0;
307}
308
309/*
310 * Sets the given parameter to an integer instead of a string.
311 * This function cannot be used to set string options.
312 *
313 * Returns 0 on success, -1 in error cases
314 */
315int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
316 uint64_t value)
317{
318 // Find a matching parameter
319 list = get_option_parameter(list, name);
320 if (list == NULL) {
321 fprintf(stderr, "Unknown option '%s'\n", name);
322 return -1;
323 }
324
325 // Process parameter
326 switch (list->type) {
327 case OPT_FLAG:
328 case OPT_NUMBER:
329 case OPT_SIZE:
330 list->value.n = value;
331 break;
332
333 default:
334 return -1;
335 }
336
337 return 0;
338}
339
340/*
341 * Frees a option list. If it contains strings, the strings are freed as well.
342 */
343void free_option_parameters(QEMUOptionParameter *list)
344{
345 QEMUOptionParameter *cur = list;
346
347 while (cur && cur->name) {
348 if (cur->type == OPT_STRING) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500349 g_free(cur->value.s);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200350 }
351 cur++;
352 }
353
Anthony Liguori7267c092011-08-20 22:09:37 -0500354 g_free(list);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200355}
356
357/*
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900358 * Count valid options in list
359 */
360static size_t count_option_parameters(QEMUOptionParameter *list)
361{
362 size_t num_options = 0;
363
364 while (list && list->name) {
365 num_options++;
366 list++;
367 }
368
369 return num_options;
370}
371
372/*
373 * Append an option list (list) to an option list (dest).
374 *
375 * If dest is NULL, a new copy of list is created.
376 *
377 * Returns a pointer to the first element of dest (or the newly allocated copy)
378 */
379QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
380 QEMUOptionParameter *list)
381{
382 size_t num_options, num_dest_options;
383
384 num_options = count_option_parameters(dest);
385 num_dest_options = num_options;
386
387 num_options += count_option_parameters(list);
388
Anthony Liguori7267c092011-08-20 22:09:37 -0500389 dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
Kevin Wolfbd69fe82010-06-11 10:19:41 +0200390 dest[num_dest_options].name = NULL;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900391
392 while (list && list->name) {
393 if (get_option_parameter(dest, list->name) == NULL) {
394 dest[num_dest_options++] = *list;
395 dest[num_dest_options].name = NULL;
396 }
397 list++;
398 }
399
400 return dest;
401}
402
403/*
Kevin Wolfd3f24362009-05-18 16:42:09 +0200404 * Parses a parameter string (param) into an option list (dest).
405 *
Stefan Hajnoczi0e72e752010-12-07 09:35:54 +0000406 * list is the template option list. If dest is NULL, a new copy of list is
407 * created. If list is NULL, this function fails.
Kevin Wolfd3f24362009-05-18 16:42:09 +0200408 *
409 * A parameter string consists of one or more parameters, separated by commas.
410 * Each parameter consists of its name and possibly of a value. In the latter
411 * case, the value is delimited by an = character. To specify a value which
412 * contains commas, double each comma so it won't be recognized as the end of
413 * the parameter.
414 *
415 * For more details of the parsing see above.
416 *
417 * Returns a pointer to the first element of dest (or the newly allocated copy)
418 * or NULL in error cases
419 */
420QEMUOptionParameter *parse_option_parameters(const char *param,
421 QEMUOptionParameter *list, QEMUOptionParameter *dest)
422{
Kevin Wolfd3f24362009-05-18 16:42:09 +0200423 QEMUOptionParameter *allocated = NULL;
424 char name[256];
425 char value[256];
426 char *param_delim, *value_delim;
427 char next_delim;
Kevin Wolfd3f24362009-05-18 16:42:09 +0200428
429 if (list == NULL) {
430 return NULL;
431 }
432
433 if (dest == NULL) {
Stefan Hajnoczi898c2572010-12-07 09:35:53 +0000434 dest = allocated = append_option_parameters(NULL, list);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200435 }
436
437 while (*param) {
438
439 // Find parameter name and value in the string
440 param_delim = strchr(param, ',');
441 value_delim = strchr(param, '=');
442
443 if (value_delim && (value_delim < param_delim || !param_delim)) {
444 next_delim = '=';
445 } else {
446 next_delim = ',';
447 value_delim = NULL;
448 }
449
450 param = get_opt_name(name, sizeof(name), param, next_delim);
451 if (value_delim) {
452 param = get_opt_value(value, sizeof(value), param + 1);
453 }
454 if (*param != '\0') {
455 param++;
456 }
457
458 // Set the parameter
459 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
460 goto fail;
461 }
462 }
463
464 return dest;
465
466fail:
467 // Only free the list if it was newly allocated
468 free_option_parameters(allocated);
469 return NULL;
470}
471
472/*
473 * Prints all options of a list that have a value to stdout
474 */
475void print_option_parameters(QEMUOptionParameter *list)
476{
477 while (list && list->name) {
478 switch (list->type) {
479 case OPT_STRING:
480 if (list->value.s != NULL) {
481 printf("%s='%s' ", list->name, list->value.s);
482 }
483 break;
484 case OPT_FLAG:
485 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
486 break;
487 case OPT_SIZE:
488 case OPT_NUMBER:
489 printf("%s=%" PRId64 " ", list->name, list->value.n);
490 break;
491 default:
Dong Xu Wang07f35072011-11-22 18:06:26 +0800492 printf("%s=(unknown type) ", list->name);
Kevin Wolfd3f24362009-05-18 16:42:09 +0200493 break;
494 }
495 list++;
496 }
497}
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200498
499/*
500 * Prints an overview of all available options
501 */
502void print_option_help(QEMUOptionParameter *list)
503{
504 printf("Supported options:\n");
505 while (list && list->name) {
506 printf("%-16s %s\n", list->name,
507 list->help ? list->help : "No description available");
508 list++;
509 }
510}
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200511
512/* ------------------------------------------------------------------ */
513
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200514static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
515{
516 QemuOpt *opt;
517
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100518 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200519 if (strcmp(opt->name, name) != 0)
520 continue;
521 return opt;
522 }
523 return NULL;
524}
525
526const char *qemu_opt_get(QemuOpts *opts, const char *name)
527{
528 QemuOpt *opt = qemu_opt_find(opts, name);
529 return opt ? opt->str : NULL;
530}
531
Peter Maydellc8057f92012-08-02 13:45:54 +0100532bool qemu_opt_has_help_opt(QemuOpts *opts)
533{
534 QemuOpt *opt;
535
536 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
537 if (is_help_option(opt->name)) {
538 return true;
539 }
540 }
541 return false;
542}
543
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530544bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200545{
546 QemuOpt *opt = qemu_opt_find(opts, name);
547
548 if (opt == NULL)
549 return defval;
550 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
Juan Quintela1f5c1772009-09-21 13:08:34 +0200551 return opt->value.boolean;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200552}
553
554uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
555{
556 QemuOpt *opt = qemu_opt_find(opts, name);
557
558 if (opt == NULL)
559 return defval;
560 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
561 return opt->value.uint;
562}
563
564uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
565{
566 QemuOpt *opt = qemu_opt_find(opts, name);
567
568 if (opt == NULL)
569 return defval;
570 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
571 return opt->value.uint;
572}
573
Luiz Capitulino6c519402012-03-21 15:21:26 -0300574static void qemu_opt_parse(QemuOpt *opt, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200575{
576 if (opt->desc == NULL)
Luiz Capitulino6c519402012-03-21 15:21:26 -0300577 return;
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300578
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200579 switch (opt->desc->type) {
580 case QEMU_OPT_STRING:
581 /* nothing */
Luiz Capitulino6c519402012-03-21 15:21:26 -0300582 return;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200583 case QEMU_OPT_BOOL:
Luiz Capitulino6c519402012-03-21 15:21:26 -0300584 parse_option_bool(opt->name, opt->str, &opt->value.boolean, errp);
Luiz Capitulinocf62adf2012-03-21 14:54:42 -0300585 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200586 case QEMU_OPT_NUMBER:
Luiz Capitulino6c519402012-03-21 15:21:26 -0300587 parse_option_number(opt->name, opt->str, &opt->value.uint, errp);
Luiz Capitulino2f39df52012-03-21 14:37:44 -0300588 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200589 case QEMU_OPT_SIZE:
Luiz Capitulino6c519402012-03-21 15:21:26 -0300590 parse_option_size(opt->name, opt->str, &opt->value.uint, errp);
Luiz Capitulinoec7b2cc2012-03-21 15:03:45 -0300591 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200592 default:
593 abort();
594 }
595}
596
597static void qemu_opt_del(QemuOpt *opt)
598{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000599 QTAILQ_REMOVE(&opt->opts->head, opt, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500600 g_free((/* !const */ char*)opt->name);
601 g_free((/* !const */ char*)opt->str);
602 g_free(opt);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200603}
604
Luiz Capitulino584d4062012-03-21 16:13:24 -0300605static void opt_set(QemuOpts *opts, const char *name, const char *value,
606 bool prepend, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200607{
608 QemuOpt *opt;
Blue Swirl238431a2010-02-21 16:01:30 +0000609 const QemuOptDesc *desc = opts->list->desc;
Luiz Capitulino6c519402012-03-21 15:21:26 -0300610 Error *local_err = NULL;
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100611 int i;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200612
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100613 for (i = 0; desc[i].name != NULL; i++) {
614 if (strcmp(desc[i].name, name) == 0) {
615 break;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200616 }
617 }
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100618 if (desc[i].name == NULL) {
619 if (i == 0) {
620 /* empty list -> allow any */;
621 } else {
Luiz Capitulino584d4062012-03-21 16:13:24 -0300622 error_set(errp, QERR_INVALID_PARAMETER, name);
623 return;
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100624 }
625 }
626
Anthony Liguori7267c092011-08-20 22:09:37 -0500627 opt = g_malloc0(sizeof(*opt));
628 opt->name = g_strdup(name);
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100629 opt->opts = opts;
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100630 if (prepend) {
631 QTAILQ_INSERT_HEAD(&opts->head, opt, next);
632 } else {
633 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
634 }
Mark McLoughlindc9ca4b2009-10-06 12:17:04 +0100635 if (desc[i].name != NULL) {
636 opt->desc = desc+i;
637 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200638 if (value) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500639 opt->str = g_strdup(value);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200640 }
Luiz Capitulino6c519402012-03-21 15:21:26 -0300641 qemu_opt_parse(opt, &local_err);
642 if (error_is_set(&local_err)) {
Luiz Capitulino584d4062012-03-21 16:13:24 -0300643 error_propagate(errp, local_err);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200644 qemu_opt_del(opt);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200645 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200646}
647
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100648int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
649{
Luiz Capitulino584d4062012-03-21 16:13:24 -0300650 Error *local_err = NULL;
651
652 opt_set(opts, name, value, false, &local_err);
653 if (error_is_set(&local_err)) {
654 qerror_report_err(local_err);
655 error_free(local_err);
656 return -1;
657 }
658
659 return 0;
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100660}
661
Luiz Capitulino384f2132012-03-21 16:17:21 -0300662void qemu_opt_set_err(QemuOpts *opts, const char *name, const char *value,
663 Error **errp)
664{
665 opt_set(opts, name, value, false, errp);
666}
667
M. Mohan Kumarf02b77c2011-10-25 12:10:39 +0530668int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val)
669{
670 QemuOpt *opt;
671 const QemuOptDesc *desc = opts->list->desc;
672 int i;
673
674 for (i = 0; desc[i].name != NULL; i++) {
675 if (strcmp(desc[i].name, name) == 0) {
676 break;
677 }
678 }
679 if (desc[i].name == NULL) {
680 if (i == 0) {
681 /* empty list -> allow any */;
682 } else {
683 qerror_report(QERR_INVALID_PARAMETER, name);
684 return -1;
685 }
686 }
687
688 opt = g_malloc0(sizeof(*opt));
689 opt->name = g_strdup(name);
690 opt->opts = opts;
691 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
692 if (desc[i].name != NULL) {
693 opt->desc = desc+i;
694 }
695 opt->value.boolean = !!val;
696 return 0;
697}
698
Gerd Hoffmann48026072009-07-31 12:25:32 +0200699int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
700 int abort_on_failure)
701{
702 QemuOpt *opt;
703 int rc = 0;
704
Blue Swirl72cf2d42009-09-12 07:36:22 +0000705 QTAILQ_FOREACH(opt, &opts->head, next) {
Gerd Hoffmann48026072009-07-31 12:25:32 +0200706 rc = func(opt->name, opt->str, opaque);
707 if (abort_on_failure && rc != 0)
708 break;
709 }
710 return rc;
711}
712
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200713QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
714{
715 QemuOpts *opts;
716
Blue Swirl72cf2d42009-09-12 07:36:22 +0000717 QTAILQ_FOREACH(opts, &list->head, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200718 if (!opts->id) {
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100719 if (!id) {
720 return opts;
721 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200722 continue;
723 }
724 if (strcmp(opts->id, id) != 0) {
725 continue;
726 }
727 return opts;
728 }
729 return NULL;
730}
731
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200732static int id_wellformed(const char *id)
733{
734 int i;
735
736 if (!qemu_isalpha(id[0])) {
737 return 0;
738 }
739 for (i = 1; id[i]; i++) {
740 if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
741 return 0;
742 }
743 }
744 return 1;
745}
746
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300747QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
748 int fail_if_exists, Error **errp)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200749{
750 QemuOpts *opts = NULL;
751
752 if (id) {
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200753 if (!id_wellformed(id)) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300754 error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
Markus Armbrusterb560a9a2010-06-08 13:54:26 +0200755 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
756 return NULL;
757 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200758 opts = qemu_opts_find(list, id);
759 if (opts != NULL) {
Peter Maydellda933182012-02-08 05:41:37 +0000760 if (fail_if_exists && !list->merge_lists) {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300761 error_set(errp, QERR_DUPLICATE_ID, id, list->name);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200762 return NULL;
763 } else {
764 return opts;
765 }
766 }
Peter Maydellda933182012-02-08 05:41:37 +0000767 } else if (list->merge_lists) {
768 opts = qemu_opts_find(list, NULL);
769 if (opts) {
770 return opts;
771 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200772 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500773 opts = g_malloc0(sizeof(*opts));
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200774 if (id) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500775 opts->id = g_strdup(id);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200776 }
777 opts->list = list;
Markus Armbruster827b0812010-02-18 19:46:49 +0100778 loc_save(&opts->loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000779 QTAILQ_INIT(&opts->head);
780 QTAILQ_INSERT_TAIL(&list->head, opts, next);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200781 return opts;
782}
783
Markus Armbrusterbb67ab02010-06-01 10:47:34 +0200784void qemu_opts_reset(QemuOptsList *list)
785{
786 QemuOpts *opts, *next_opts;
787
788 QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) {
789 qemu_opts_del(opts);
790 }
791}
792
Markus Armbruster94ac7262010-05-27 21:06:04 +0200793void qemu_opts_loc_restore(QemuOpts *opts)
794{
795 loc_restore(&opts->loc);
796}
797
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200798int qemu_opts_set(QemuOptsList *list, const char *id,
799 const char *name, const char *value)
800{
801 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300802 Error *local_err = NULL;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200803
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300804 opts = qemu_opts_create(list, id, 1, &local_err);
805 if (error_is_set(&local_err)) {
806 qerror_report_err(local_err);
807 error_free(local_err);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200808 return -1;
809 }
810 return qemu_opt_set(opts, name, value);
811}
812
Gerd Hoffmann48026072009-07-31 12:25:32 +0200813const char *qemu_opts_id(QemuOpts *opts)
814{
815 return opts->id;
816}
817
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200818void qemu_opts_del(QemuOpts *opts)
819{
820 QemuOpt *opt;
821
822 for (;;) {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000823 opt = QTAILQ_FIRST(&opts->head);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200824 if (opt == NULL)
825 break;
826 qemu_opt_del(opt);
827 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000828 QTAILQ_REMOVE(&opts->list->head, opts, next);
Anthony Liguori7267c092011-08-20 22:09:37 -0500829 g_free(opts->id);
830 g_free(opts);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200831}
832
833int qemu_opts_print(QemuOpts *opts, void *dummy)
834{
835 QemuOpt *opt;
836
837 fprintf(stderr, "%s: %s:", opts->list->name,
838 opts->id ? opts->id : "<noid>");
Blue Swirl72cf2d42009-09-12 07:36:22 +0000839 QTAILQ_FOREACH(opt, &opts->head, next) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200840 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
841 }
842 fprintf(stderr, "\n");
843 return 0;
844}
845
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100846static int opts_do_parse(QemuOpts *opts, const char *params,
847 const char *firstname, bool prepend)
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200848{
Gerd Hoffmannd318ff92009-12-10 11:11:08 +0100849 char option[128], value[1024];
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200850 const char *p,*pe,*pc;
Luiz Capitulino584d4062012-03-21 16:13:24 -0300851 Error *local_err = NULL;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200852
Mark McLoughlin2cfa5712009-10-06 12:17:02 +0100853 for (p = params; *p != '\0'; p++) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200854 pe = strchr(p, '=');
855 pc = strchr(p, ',');
856 if (!pe || (pc && pc < pe)) {
857 /* found "foo,more" */
858 if (p == params && firstname) {
859 /* implicitly named first option */
860 pstrcpy(option, sizeof(option), firstname);
861 p = get_opt_value(value, sizeof(value), p);
862 } else {
863 /* option without value, probably a flag */
864 p = get_opt_name(option, sizeof(option), p, ',');
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200865 if (strncmp(option, "no", 2) == 0) {
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200866 memmove(option, option+2, strlen(option+2)+1);
867 pstrcpy(value, sizeof(value), "off");
868 } else {
869 pstrcpy(value, sizeof(value), "on");
870 }
871 }
872 } else {
873 /* found "foo=bar,more" */
874 p = get_opt_name(option, sizeof(option), p, '=');
875 if (*p != '=') {
876 break;
877 }
878 p++;
879 p = get_opt_value(value, sizeof(value), p);
880 }
881 if (strcmp(option, "id") != 0) {
882 /* store and parse */
Luiz Capitulino584d4062012-03-21 16:13:24 -0300883 opt_set(opts, option, value, prepend, &local_err);
884 if (error_is_set(&local_err)) {
885 qerror_report_err(local_err);
886 error_free(local_err);
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200887 return -1;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200888 }
889 }
890 if (*p != ',') {
891 break;
892 }
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200893 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200894 return 0;
895}
896
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100897int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
898{
899 return opts_do_parse(opts, params, firstname, false);
900}
901
902static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
903 int permit_abbrev, bool defaults)
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200904{
Markus Armbruster8212c642010-02-10 19:52:18 +0100905 const char *firstname;
Gerd Hoffmannd318ff92009-12-10 11:11:08 +0100906 char value[1024], *id = NULL;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200907 const char *p;
908 QemuOpts *opts;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300909 Error *local_err = NULL;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200910
Markus Armbruster8212c642010-02-10 19:52:18 +0100911 assert(!permit_abbrev || list->implied_opt_name);
912 firstname = permit_abbrev ? list->implied_opt_name : NULL;
913
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200914 if (strncmp(params, "id=", 3) == 0) {
915 get_opt_value(value, sizeof(value), params+3);
Jan Kiszkad510c5c2010-04-29 18:24:43 +0200916 id = value;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200917 } else if ((p = strstr(params, ",id=")) != NULL) {
918 get_opt_value(value, sizeof(value), p+4);
Jan Kiszkad510c5c2010-04-29 18:24:43 +0200919 id = value;
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200920 }
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100921 if (defaults) {
922 if (!id && !QTAILQ_EMPTY(&list->head)) {
923 opts = qemu_opts_find(list, NULL);
924 } else {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300925 opts = qemu_opts_create(list, id, 0, &local_err);
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100926 }
927 } else {
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300928 opts = qemu_opts_create(list, id, 1, &local_err);
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100929 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300930 if (opts == NULL) {
931 if (error_is_set(&local_err)) {
932 qerror_report_err(local_err);
933 error_free(local_err);
934 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200935 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300936 }
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200937
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100938 if (opts_do_parse(opts, params, firstname, defaults) != 0) {
Gerd Hoffmann96729cb2009-09-10 10:58:33 +0200939 qemu_opts_del(opts);
940 return NULL;
941 }
942
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +0200943 return opts;
944}
945
Jan Kiszka4f6dd9a2012-01-27 19:54:54 +0100946QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
947 int permit_abbrev)
948{
949 return opts_parse(list, params, permit_abbrev, false);
950}
951
952void qemu_opts_set_defaults(QemuOptsList *list, const char *params,
953 int permit_abbrev)
954{
955 QemuOpts *opts;
956
957 opts = opts_parse(list, params, permit_abbrev, true);
958 assert(opts);
959}
960
Luiz Capitulino4e899782012-04-18 17:24:01 -0300961typedef struct OptsFromQDictState {
962 QemuOpts *opts;
963 Error **errp;
964} OptsFromQDictState;
965
Markus Armbruster01e7f182010-02-10 20:15:29 +0100966static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
967{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300968 OptsFromQDictState *state = opaque;
Markus Armbruster01e7f182010-02-10 20:15:29 +0100969 char buf[32];
970 const char *value;
971 int n;
972
Luiz Capitulino4e899782012-04-18 17:24:01 -0300973 if (!strcmp(key, "id") || error_is_set(state->errp)) {
Markus Armbruster01e7f182010-02-10 20:15:29 +0100974 return;
975 }
976
977 switch (qobject_type(obj)) {
978 case QTYPE_QSTRING:
979 value = qstring_get_str(qobject_to_qstring(obj));
980 break;
981 case QTYPE_QINT:
982 n = snprintf(buf, sizeof(buf), "%" PRId64,
983 qint_get_int(qobject_to_qint(obj)));
984 assert(n < sizeof(buf));
985 value = buf;
986 break;
987 case QTYPE_QFLOAT:
988 n = snprintf(buf, sizeof(buf), "%.17g",
989 qfloat_get_double(qobject_to_qfloat(obj)));
990 assert(n < sizeof(buf));
991 value = buf;
992 break;
993 case QTYPE_QBOOL:
Blue Swirld35215f2010-03-18 20:48:19 +0000994 pstrcpy(buf, sizeof(buf),
995 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
Markus Armbruster01e7f182010-02-10 20:15:29 +0100996 value = buf;
997 break;
998 default:
999 return;
1000 }
Luiz Capitulino4e899782012-04-18 17:24:01 -03001001
1002 qemu_opt_set_err(state->opts, key, value, state->errp);
Markus Armbruster01e7f182010-02-10 20:15:29 +01001003}
1004
1005/*
1006 * Create QemuOpts from a QDict.
1007 * Use value of key "id" as ID if it exists and is a QString.
1008 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1009 * other types are silently ignored.
1010 */
Luiz Capitulino4e899782012-04-18 17:24:01 -03001011QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict,
1012 Error **errp)
Markus Armbruster01e7f182010-02-10 20:15:29 +01001013{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001014 OptsFromQDictState state;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001015 Error *local_err = NULL;
Luiz Capitulino4e899782012-04-18 17:24:01 -03001016 QemuOpts *opts;
Markus Armbruster01e7f182010-02-10 20:15:29 +01001017
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001018 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
1019 &local_err);
1020 if (error_is_set(&local_err)) {
Luiz Capitulino4e899782012-04-18 17:24:01 -03001021 error_propagate(errp, local_err);
Markus Armbruster01e7f182010-02-10 20:15:29 +01001022 return NULL;
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001023 }
Markus Armbruster01e7f182010-02-10 20:15:29 +01001024
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -03001025 assert(opts != NULL);
Luiz Capitulino4e899782012-04-18 17:24:01 -03001026
1027 state.errp = &local_err;
1028 state.opts = opts;
1029 qdict_iter(qdict, qemu_opts_from_qdict_1, &state);
1030 if (error_is_set(&local_err)) {
1031 error_propagate(errp, local_err);
1032 qemu_opts_del(opts);
1033 return NULL;
1034 }
1035
Markus Armbruster01e7f182010-02-10 20:15:29 +01001036 return opts;
1037}
1038
1039/*
1040 * Convert from QemuOpts to QDict.
1041 * The QDict values are of type QString.
1042 * TODO We'll want to use types appropriate for opt->desc->type, but
1043 * this is enough for now.
1044 */
1045QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
1046{
1047 QemuOpt *opt;
1048 QObject *val;
1049
1050 if (!qdict) {
1051 qdict = qdict_new();
1052 }
1053 if (opts->id) {
1054 qdict_put(qdict, "id", qstring_from_str(opts->id));
1055 }
1056 QTAILQ_FOREACH(opt, &opts->head, next) {
1057 val = QOBJECT(qstring_from_str(opt->str));
1058 qdict_put_obj(qdict, opt->name, val);
1059 }
1060 return qdict;
1061}
1062
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001063/* Validate parsed opts against descriptions where no
1064 * descriptions were provided in the QemuOptsList.
1065 */
Luiz Capitulino29952862012-04-12 11:58:57 -03001066void qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc, Error **errp)
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001067{
1068 QemuOpt *opt;
Luiz Capitulino6c519402012-03-21 15:21:26 -03001069 Error *local_err = NULL;
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001070
1071 assert(opts->list->desc[0].name == NULL);
1072
1073 QTAILQ_FOREACH(opt, &opts->head, next) {
1074 int i;
1075
1076 for (i = 0; desc[i].name != NULL; i++) {
1077 if (strcmp(desc[i].name, opt->name) == 0) {
1078 break;
1079 }
1080 }
1081 if (desc[i].name == NULL) {
Luiz Capitulino29952862012-04-12 11:58:57 -03001082 error_set(errp, QERR_INVALID_PARAMETER, opt->name);
1083 return;
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001084 }
1085
1086 opt->desc = &desc[i];
1087
Luiz Capitulino6c519402012-03-21 15:21:26 -03001088 qemu_opt_parse(opt, &local_err);
1089 if (error_is_set(&local_err)) {
Luiz Capitulino29952862012-04-12 11:58:57 -03001090 error_propagate(errp, local_err);
1091 return;
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001092 }
1093 }
Mark McLoughlin5dc519e2009-10-06 12:17:03 +01001094}
1095
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001096int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
1097 int abort_on_failure)
1098{
Markus Armbruster827b0812010-02-18 19:46:49 +01001099 Location loc;
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001100 QemuOpts *opts;
1101 int rc = 0;
1102
Markus Armbruster827b0812010-02-18 19:46:49 +01001103 loc_push_none(&loc);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001104 QTAILQ_FOREACH(opts, &list->head, next) {
Markus Armbruster827b0812010-02-18 19:46:49 +01001105 loc_restore(&opts->loc);
Markus Armbruster4a2594d2010-01-29 19:48:56 +01001106 rc |= func(opts, opaque);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001107 if (abort_on_failure && rc != 0)
1108 break;
1109 }
Markus Armbruster827b0812010-02-18 19:46:49 +01001110 loc_pop(&loc);
Gerd Hoffmanne27c88f2009-07-22 16:43:03 +02001111 return rc;
1112}