Move win32 early signal handling setup to os_setup_signal_handling()
[qemu.git] / qemu-option.c
blobacd74f91247f5b8d21e008152e3ea8b7c9e9da44
1 /*
2 * Commandline option parsing functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include <stdio.h>
27 #include <string.h>
29 #include "qemu-common.h"
30 #include "qemu-error.h"
31 #include "qemu-objects.h"
32 #include "qemu-option.h"
33 #include "qerror.h"
36 * Extracts the name of an option from the parameter string (p points at the
37 * first byte of the option name)
39 * The option name is delimited by delim (usually , or =) or the string end
40 * and is copied into buf. If the option name is longer than buf_size, it is
41 * truncated. buf is always zero terminated.
43 * The return value is the position of the delimiter/zero byte after the option
44 * name in p.
46 const char *get_opt_name(char *buf, int buf_size, const char *p, char delim)
48 char *q;
50 q = buf;
51 while (*p != '\0' && *p != delim) {
52 if (q && (q - buf) < buf_size - 1)
53 *q++ = *p;
54 p++;
56 if (q)
57 *q = '\0';
59 return p;
63 * Extracts the value of an option from the parameter string p (p points at the
64 * first byte of the option value)
66 * This function is comparable to get_opt_name with the difference that the
67 * delimiter is fixed to be comma which starts a new option. To specify an
68 * option value that contains commas, double each comma.
70 const char *get_opt_value(char *buf, int buf_size, const char *p)
72 char *q;
74 q = buf;
75 while (*p != '\0') {
76 if (*p == ',') {
77 if (*(p + 1) != ',')
78 break;
79 p++;
81 if (q && (q - buf) < buf_size - 1)
82 *q++ = *p;
83 p++;
85 if (q)
86 *q = '\0';
88 return p;
91 int get_next_param_value(char *buf, int buf_size,
92 const char *tag, const char **pstr)
94 const char *p;
95 char option[128];
97 p = *pstr;
98 for(;;) {
99 p = get_opt_name(option, sizeof(option), p, '=');
100 if (*p != '=')
101 break;
102 p++;
103 if (!strcmp(tag, option)) {
104 *pstr = get_opt_value(buf, buf_size, p);
105 if (**pstr == ',') {
106 (*pstr)++;
108 return strlen(buf);
109 } else {
110 p = get_opt_value(NULL, 0, p);
112 if (*p != ',')
113 break;
114 p++;
116 return 0;
119 int get_param_value(char *buf, int buf_size,
120 const char *tag, const char *str)
122 return get_next_param_value(buf, buf_size, tag, &str);
125 int check_params(char *buf, int buf_size,
126 const char * const *params, const char *str)
128 const char *p;
129 int i;
131 p = str;
132 while (*p != '\0') {
133 p = get_opt_name(buf, buf_size, p, '=');
134 if (*p != '=') {
135 return -1;
137 p++;
138 for (i = 0; params[i] != NULL; i++) {
139 if (!strcmp(params[i], buf)) {
140 break;
143 if (params[i] == NULL) {
144 return -1;
146 p = get_opt_value(NULL, 0, p);
147 if (*p != ',') {
148 break;
150 p++;
152 return 0;
156 * Searches an option list for an option with the given name
158 QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list,
159 const char *name)
161 while (list && list->name) {
162 if (!strcmp(list->name, name)) {
163 return list;
165 list++;
168 return NULL;
171 static int parse_option_bool(const char *name, const char *value, int *ret)
173 if (value != NULL) {
174 if (!strcmp(value, "on")) {
175 *ret = 1;
176 } else if (!strcmp(value, "off")) {
177 *ret = 0;
178 } else {
179 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
180 return -1;
182 } else {
183 *ret = 1;
185 return 0;
188 static int parse_option_number(const char *name, const char *value, uint64_t *ret)
190 char *postfix;
191 uint64_t number;
193 if (value != NULL) {
194 number = strtoull(value, &postfix, 0);
195 if (*postfix != '\0') {
196 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
197 return -1;
199 *ret = number;
200 } else {
201 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
202 return -1;
204 return 0;
207 static int parse_option_size(const char *name, const char *value, uint64_t *ret)
209 char *postfix;
210 double sizef;
212 if (value != NULL) {
213 sizef = strtod(value, &postfix);
214 switch (*postfix) {
215 case 'T':
216 sizef *= 1024;
217 case 'G':
218 sizef *= 1024;
219 case 'M':
220 sizef *= 1024;
221 case 'K':
222 case 'k':
223 sizef *= 1024;
224 case 'b':
225 case '\0':
226 *ret = (uint64_t) sizef;
227 break;
228 default:
229 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
230 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
231 "kilobytes, megabytes, gigabytes and terabytes.\n");
232 return -1;
234 } else {
235 qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
236 return -1;
238 return 0;
242 * Sets the value of a parameter in a given option list. The parsing of the
243 * value depends on the type of option:
245 * OPT_FLAG (uses value.n):
246 * If no value is given, the flag is set to 1.
247 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
249 * OPT_STRING (uses value.s):
250 * value is strdup()ed and assigned as option value
252 * OPT_SIZE (uses value.n):
253 * The value is converted to an integer. Suffixes for kilobytes etc. are
254 * allowed (powers of 1024).
256 * Returns 0 on succes, -1 in error cases
258 int set_option_parameter(QEMUOptionParameter *list, const char *name,
259 const char *value)
261 int flag;
263 // Find a matching parameter
264 list = get_option_parameter(list, name);
265 if (list == NULL) {
266 fprintf(stderr, "Unknown option '%s'\n", name);
267 return -1;
270 // Process parameter
271 switch (list->type) {
272 case OPT_FLAG:
273 if (parse_option_bool(name, value, &flag) == -1)
274 return -1;
275 list->value.n = flag;
276 break;
278 case OPT_STRING:
279 if (value != NULL) {
280 list->value.s = qemu_strdup(value);
281 } else {
282 fprintf(stderr, "Option '%s' needs a parameter\n", name);
283 return -1;
285 break;
287 case OPT_SIZE:
288 if (parse_option_size(name, value, &list->value.n) == -1)
289 return -1;
290 break;
292 default:
293 fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
294 return -1;
297 return 0;
301 * Sets the given parameter to an integer instead of a string.
302 * This function cannot be used to set string options.
304 * Returns 0 on success, -1 in error cases
306 int set_option_parameter_int(QEMUOptionParameter *list, const char *name,
307 uint64_t value)
309 // Find a matching parameter
310 list = get_option_parameter(list, name);
311 if (list == NULL) {
312 fprintf(stderr, "Unknown option '%s'\n", name);
313 return -1;
316 // Process parameter
317 switch (list->type) {
318 case OPT_FLAG:
319 case OPT_NUMBER:
320 case OPT_SIZE:
321 list->value.n = value;
322 break;
324 default:
325 return -1;
328 return 0;
332 * Frees a option list. If it contains strings, the strings are freed as well.
334 void free_option_parameters(QEMUOptionParameter *list)
336 QEMUOptionParameter *cur = list;
338 while (cur && cur->name) {
339 if (cur->type == OPT_STRING) {
340 qemu_free(cur->value.s);
342 cur++;
345 qemu_free(list);
349 * Count valid options in list
351 static size_t count_option_parameters(QEMUOptionParameter *list)
353 size_t num_options = 0;
355 while (list && list->name) {
356 num_options++;
357 list++;
360 return num_options;
364 * Append an option list (list) to an option list (dest).
366 * If dest is NULL, a new copy of list is created.
368 * Returns a pointer to the first element of dest (or the newly allocated copy)
370 QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest,
371 QEMUOptionParameter *list)
373 size_t num_options, num_dest_options;
375 num_options = count_option_parameters(dest);
376 num_dest_options = num_options;
378 num_options += count_option_parameters(list);
380 dest = qemu_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter));
382 while (list && list->name) {
383 if (get_option_parameter(dest, list->name) == NULL) {
384 dest[num_dest_options++] = *list;
385 dest[num_dest_options].name = NULL;
387 list++;
390 return dest;
394 * Parses a parameter string (param) into an option list (dest).
396 * list is the templace is. If dest is NULL, a new copy of list is created for
397 * it. If list is NULL, this function fails.
399 * A parameter string consists of one or more parameters, separated by commas.
400 * Each parameter consists of its name and possibly of a value. In the latter
401 * case, the value is delimited by an = character. To specify a value which
402 * contains commas, double each comma so it won't be recognized as the end of
403 * the parameter.
405 * For more details of the parsing see above.
407 * Returns a pointer to the first element of dest (or the newly allocated copy)
408 * or NULL in error cases
410 QEMUOptionParameter *parse_option_parameters(const char *param,
411 QEMUOptionParameter *list, QEMUOptionParameter *dest)
413 QEMUOptionParameter *allocated = NULL;
414 char name[256];
415 char value[256];
416 char *param_delim, *value_delim;
417 char next_delim;
418 size_t num_options;
420 if (list == NULL) {
421 return NULL;
424 if (dest == NULL) {
425 // Count valid options
426 num_options = count_option_parameters(list);
428 // Create a copy of the option list to fill in values
429 dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter));
430 allocated = dest;
431 memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter));
434 while (*param) {
436 // Find parameter name and value in the string
437 param_delim = strchr(param, ',');
438 value_delim = strchr(param, '=');
440 if (value_delim && (value_delim < param_delim || !param_delim)) {
441 next_delim = '=';
442 } else {
443 next_delim = ',';
444 value_delim = NULL;
447 param = get_opt_name(name, sizeof(name), param, next_delim);
448 if (value_delim) {
449 param = get_opt_value(value, sizeof(value), param + 1);
451 if (*param != '\0') {
452 param++;
455 // Set the parameter
456 if (set_option_parameter(dest, name, value_delim ? value : NULL)) {
457 goto fail;
461 return dest;
463 fail:
464 // Only free the list if it was newly allocated
465 free_option_parameters(allocated);
466 return NULL;
470 * Prints all options of a list that have a value to stdout
472 void print_option_parameters(QEMUOptionParameter *list)
474 while (list && list->name) {
475 switch (list->type) {
476 case OPT_STRING:
477 if (list->value.s != NULL) {
478 printf("%s='%s' ", list->name, list->value.s);
480 break;
481 case OPT_FLAG:
482 printf("%s=%s ", list->name, list->value.n ? "on" : "off");
483 break;
484 case OPT_SIZE:
485 case OPT_NUMBER:
486 printf("%s=%" PRId64 " ", list->name, list->value.n);
487 break;
488 default:
489 printf("%s=(unkown type) ", list->name);
490 break;
492 list++;
497 * Prints an overview of all available options
499 void print_option_help(QEMUOptionParameter *list)
501 printf("Supported options:\n");
502 while (list && list->name) {
503 printf("%-16s %s\n", list->name,
504 list->help ? list->help : "No description available");
505 list++;
509 /* ------------------------------------------------------------------ */
511 struct QemuOpt {
512 const char *name;
513 const char *str;
515 const QemuOptDesc *desc;
516 union {
517 int boolean;
518 uint64_t uint;
519 } value;
521 QemuOpts *opts;
522 QTAILQ_ENTRY(QemuOpt) next;
525 struct QemuOpts {
526 char *id;
527 QemuOptsList *list;
528 Location loc;
529 QTAILQ_HEAD(QemuOptHead, QemuOpt) head;
530 QTAILQ_ENTRY(QemuOpts) next;
533 static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name)
535 QemuOpt *opt;
537 QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) {
538 if (strcmp(opt->name, name) != 0)
539 continue;
540 return opt;
542 return NULL;
545 const char *qemu_opt_get(QemuOpts *opts, const char *name)
547 QemuOpt *opt = qemu_opt_find(opts, name);
548 return opt ? opt->str : NULL;
551 int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval)
553 QemuOpt *opt = qemu_opt_find(opts, name);
555 if (opt == NULL)
556 return defval;
557 assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL);
558 return opt->value.boolean;
561 uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval)
563 QemuOpt *opt = qemu_opt_find(opts, name);
565 if (opt == NULL)
566 return defval;
567 assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER);
568 return opt->value.uint;
571 uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval)
573 QemuOpt *opt = qemu_opt_find(opts, name);
575 if (opt == NULL)
576 return defval;
577 assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE);
578 return opt->value.uint;
581 static int qemu_opt_parse(QemuOpt *opt)
583 if (opt->desc == NULL)
584 return 0;
585 switch (opt->desc->type) {
586 case QEMU_OPT_STRING:
587 /* nothing */
588 return 0;
589 case QEMU_OPT_BOOL:
590 return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
591 case QEMU_OPT_NUMBER:
592 return parse_option_number(opt->name, opt->str, &opt->value.uint);
593 case QEMU_OPT_SIZE:
594 return parse_option_size(opt->name, opt->str, &opt->value.uint);
595 default:
596 abort();
600 static void qemu_opt_del(QemuOpt *opt)
602 QTAILQ_REMOVE(&opt->opts->head, opt, next);
603 qemu_free((/* !const */ char*)opt->name);
604 qemu_free((/* !const */ char*)opt->str);
605 qemu_free(opt);
608 int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)
610 QemuOpt *opt;
611 const QemuOptDesc *desc = opts->list->desc;
612 int i;
614 for (i = 0; desc[i].name != NULL; i++) {
615 if (strcmp(desc[i].name, name) == 0) {
616 break;
619 if (desc[i].name == NULL) {
620 if (i == 0) {
621 /* empty list -> allow any */;
622 } else {
623 qerror_report(QERR_INVALID_PARAMETER, name);
624 return -1;
628 opt = qemu_mallocz(sizeof(*opt));
629 opt->name = qemu_strdup(name);
630 opt->opts = opts;
631 QTAILQ_INSERT_TAIL(&opts->head, opt, next);
632 if (desc[i].name != NULL) {
633 opt->desc = desc+i;
635 if (value) {
636 opt->str = qemu_strdup(value);
638 if (qemu_opt_parse(opt) < 0) {
639 qemu_opt_del(opt);
640 return -1;
642 return 0;
645 int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
646 int abort_on_failure)
648 QemuOpt *opt;
649 int rc = 0;
651 QTAILQ_FOREACH(opt, &opts->head, next) {
652 rc = func(opt->name, opt->str, opaque);
653 if (abort_on_failure && rc != 0)
654 break;
656 return rc;
659 QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
661 QemuOpts *opts;
663 QTAILQ_FOREACH(opts, &list->head, next) {
664 if (!opts->id) {
665 continue;
667 if (strcmp(opts->id, id) != 0) {
668 continue;
670 return opts;
672 return NULL;
675 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
677 QemuOpts *opts = NULL;
679 if (id) {
680 opts = qemu_opts_find(list, id);
681 if (opts != NULL) {
682 if (fail_if_exists) {
683 qerror_report(QERR_DUPLICATE_ID, id, list->name);
684 return NULL;
685 } else {
686 return opts;
690 opts = qemu_mallocz(sizeof(*opts));
691 if (id) {
692 opts->id = qemu_strdup(id);
694 opts->list = list;
695 loc_save(&opts->loc);
696 QTAILQ_INIT(&opts->head);
697 QTAILQ_INSERT_TAIL(&list->head, opts, next);
698 return opts;
701 int qemu_opts_set(QemuOptsList *list, const char *id,
702 const char *name, const char *value)
704 QemuOpts *opts;
706 opts = qemu_opts_create(list, id, 1);
707 if (opts == NULL) {
708 return -1;
710 return qemu_opt_set(opts, name, value);
713 const char *qemu_opts_id(QemuOpts *opts)
715 return opts->id;
718 void qemu_opts_del(QemuOpts *opts)
720 QemuOpt *opt;
722 for (;;) {
723 opt = QTAILQ_FIRST(&opts->head);
724 if (opt == NULL)
725 break;
726 qemu_opt_del(opt);
728 QTAILQ_REMOVE(&opts->list->head, opts, next);
729 qemu_free(opts->id);
730 qemu_free(opts);
733 int qemu_opts_print(QemuOpts *opts, void *dummy)
735 QemuOpt *opt;
737 fprintf(stderr, "%s: %s:", opts->list->name,
738 opts->id ? opts->id : "<noid>");
739 QTAILQ_FOREACH(opt, &opts->head, next) {
740 fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
742 fprintf(stderr, "\n");
743 return 0;
746 int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname)
748 char option[128], value[1024];
749 const char *p,*pe,*pc;
751 for (p = params; *p != '\0'; p++) {
752 pe = strchr(p, '=');
753 pc = strchr(p, ',');
754 if (!pe || (pc && pc < pe)) {
755 /* found "foo,more" */
756 if (p == params && firstname) {
757 /* implicitly named first option */
758 pstrcpy(option, sizeof(option), firstname);
759 p = get_opt_value(value, sizeof(value), p);
760 } else {
761 /* option without value, probably a flag */
762 p = get_opt_name(option, sizeof(option), p, ',');
763 if (strncmp(option, "no", 2) == 0) {
764 memmove(option, option+2, strlen(option+2)+1);
765 pstrcpy(value, sizeof(value), "off");
766 } else {
767 pstrcpy(value, sizeof(value), "on");
770 } else {
771 /* found "foo=bar,more" */
772 p = get_opt_name(option, sizeof(option), p, '=');
773 if (*p != '=') {
774 break;
776 p++;
777 p = get_opt_value(value, sizeof(value), p);
779 if (strcmp(option, "id") != 0) {
780 /* store and parse */
781 if (qemu_opt_set(opts, option, value) == -1) {
782 return -1;
785 if (*p != ',') {
786 break;
789 return 0;
792 QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params,
793 int permit_abbrev)
795 const char *firstname;
796 char value[1024], *id = NULL;
797 const char *p;
798 QemuOpts *opts;
800 assert(!permit_abbrev || list->implied_opt_name);
801 firstname = permit_abbrev ? list->implied_opt_name : NULL;
803 if (strncmp(params, "id=", 3) == 0) {
804 get_opt_value(value, sizeof(value), params+3);
805 id = value;
806 } else if ((p = strstr(params, ",id=")) != NULL) {
807 get_opt_value(value, sizeof(value), p+4);
808 id = value;
810 opts = qemu_opts_create(list, id, 1);
811 if (opts == NULL)
812 return NULL;
814 if (qemu_opts_do_parse(opts, params, firstname) != 0) {
815 qemu_opts_del(opts);
816 return NULL;
819 return opts;
822 static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
824 char buf[32];
825 const char *value;
826 int n;
828 if (!strcmp(key, "id")) {
829 return;
832 switch (qobject_type(obj)) {
833 case QTYPE_QSTRING:
834 value = qstring_get_str(qobject_to_qstring(obj));
835 break;
836 case QTYPE_QINT:
837 n = snprintf(buf, sizeof(buf), "%" PRId64,
838 qint_get_int(qobject_to_qint(obj)));
839 assert(n < sizeof(buf));
840 value = buf;
841 break;
842 case QTYPE_QFLOAT:
843 n = snprintf(buf, sizeof(buf), "%.17g",
844 qfloat_get_double(qobject_to_qfloat(obj)));
845 assert(n < sizeof(buf));
846 value = buf;
847 break;
848 case QTYPE_QBOOL:
849 pstrcpy(buf, sizeof(buf),
850 qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off");
851 value = buf;
852 break;
853 default:
854 return;
856 qemu_opt_set(opaque, key, value);
860 * Create QemuOpts from a QDict.
861 * Use value of key "id" as ID if it exists and is a QString.
862 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
863 * other types are silently ignored.
865 QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
867 QemuOpts *opts;
869 opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
870 if (opts == NULL)
871 return NULL;
873 qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
874 return opts;
878 * Convert from QemuOpts to QDict.
879 * The QDict values are of type QString.
880 * TODO We'll want to use types appropriate for opt->desc->type, but
881 * this is enough for now.
883 QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict)
885 QemuOpt *opt;
886 QObject *val;
888 if (!qdict) {
889 qdict = qdict_new();
891 if (opts->id) {
892 qdict_put(qdict, "id", qstring_from_str(opts->id));
894 QTAILQ_FOREACH(opt, &opts->head, next) {
895 val = QOBJECT(qstring_from_str(opt->str));
896 qdict_put_obj(qdict, opt->name, val);
898 return qdict;
901 /* Validate parsed opts against descriptions where no
902 * descriptions were provided in the QemuOptsList.
904 int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc)
906 QemuOpt *opt;
908 assert(opts->list->desc[0].name == NULL);
910 QTAILQ_FOREACH(opt, &opts->head, next) {
911 int i;
913 for (i = 0; desc[i].name != NULL; i++) {
914 if (strcmp(desc[i].name, opt->name) == 0) {
915 break;
918 if (desc[i].name == NULL) {
919 qerror_report(QERR_INVALID_PARAMETER, opt->name);
920 return -1;
923 opt->desc = &desc[i];
925 if (qemu_opt_parse(opt) < 0) {
926 return -1;
930 return 0;
933 int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque,
934 int abort_on_failure)
936 Location loc;
937 QemuOpts *opts;
938 int rc = 0;
940 loc_push_none(&loc);
941 QTAILQ_FOREACH(opts, &list->head, next) {
942 loc_restore(&opts->loc);
943 rc |= func(opts, opaque);
944 if (abort_on_failure && rc != 0)
945 break;
947 loc_pop(&loc);
948 return rc;