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
26 #include "qemu/osdep.h"
28 #include "qapi/error.h"
29 #include "qemu-common.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/types.h"
32 #include "qapi/qmp/qerror.h"
33 #include "qemu/option_int.h"
34 #include "qemu/cutils.h"
36 #include "qemu/help_option.h"
39 * Extracts the name of an option from the parameter string (p points at the
40 * first byte of the option name)
42 * The option name is delimited by delim (usually , or =) or the string end
43 * and is copied into buf. If the option name is longer than buf_size, it is
44 * truncated. buf is always zero terminated.
46 * The return value is the position of the delimiter/zero byte after the option
49 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
54 while (*p
!= '\0' && *p
!= delim
) {
55 if (q
&& (q
- buf
) < buf_size
- 1)
66 * Extracts the value of an option from the parameter string p (p points at the
67 * first byte of the option value)
69 * This function is comparable to get_opt_name with the difference that the
70 * delimiter is fixed to be comma which starts a new option. To specify an
71 * option value that contains commas, double each comma.
73 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
84 if (q
&& (q
- buf
) < buf_size
- 1)
94 int get_next_param_value(char *buf
, int buf_size
,
95 const char *tag
, const char **pstr
)
102 p
= get_opt_name(option
, sizeof(option
), p
, '=');
106 if (!strcmp(tag
, option
)) {
107 *pstr
= get_opt_value(buf
, buf_size
, p
);
113 p
= get_opt_value(NULL
, 0, p
);
122 int get_param_value(char *buf
, int buf_size
,
123 const char *tag
, const char *str
)
125 return get_next_param_value(buf
, buf_size
, tag
, &str
);
128 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
131 if (!strcmp(value
, "on")) {
133 } else if (!strcmp(value
, "off")) {
136 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
137 name
, "'on' or 'off'");
141 static void parse_option_number(const char *name
, const char *value
,
142 uint64_t *ret
, Error
**errp
)
147 err
= qemu_strtou64(value
, NULL
, 0, &number
);
148 if (err
== -ERANGE
) {
149 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
154 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
160 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
165 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
166 if (strcmp(desc
[i
].name
, name
) == 0) {
174 void parse_option_size(const char *name
, const char *value
,
175 uint64_t *ret
, Error
**errp
)
180 err
= qemu_strtosz(value
, NULL
, &size
);
181 if (err
== -ERANGE
) {
182 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
187 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
188 "a non-negative number below 2^64");
189 error_append_hint(errp
, "Optional suffix k, M, G, T, P or E means"
190 " kilo-, mega-, giga-, tera-, peta-\n"
191 "and exabytes, respectively.\n");
197 bool has_help_option(const char *param
)
199 size_t buflen
= strlen(param
) + 1;
200 char *buf
= g_malloc(buflen
);
201 const char *p
= param
;
205 p
= get_opt_value(buf
, buflen
, p
);
210 if (is_help_option(buf
)) {
221 bool is_valid_option_list(const char *param
)
223 size_t buflen
= strlen(param
) + 1;
224 char *buf
= g_malloc(buflen
);
225 const char *p
= param
;
229 p
= get_opt_value(buf
, buflen
, p
);
235 if (!*buf
|| *buf
== ',') {
246 void qemu_opts_print_help(QemuOptsList
*list
)
252 printf("Supported options:\n");
253 while (desc
&& desc
->name
) {
254 printf("%-16s %s\n", desc
->name
,
255 desc
->help
? desc
->help
: "No description available");
259 /* ------------------------------------------------------------------ */
261 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
265 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
266 if (strcmp(opt
->name
, name
) != 0)
273 static void qemu_opt_del(QemuOpt
*opt
)
275 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
281 /* qemu_opt_set allows many settings for the same option.
282 * This function deletes all settings for an option.
284 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
286 QemuOpt
*opt
, *next_opt
;
288 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
289 if (!strcmp(opt
->name
, name
)) {
295 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
303 opt
= qemu_opt_find(opts
, name
);
305 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
306 if (desc
&& desc
->def_value_str
) {
307 return desc
->def_value_str
;
310 return opt
? opt
->str
: NULL
;
313 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
316 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
320 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
322 QemuOpt
*ret
= iter
->opt
;
324 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
325 ret
= QTAILQ_NEXT(ret
, next
);
328 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
329 return ret
? ret
->str
: NULL
;
332 /* Get a known option (or its default) and remove it from the list
333 * all in one action. Return a malloced string of the option value.
334 * Result must be freed by caller with g_free().
336 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
339 const QemuOptDesc
*desc
;
346 opt
= qemu_opt_find(opts
, name
);
348 desc
= find_desc_by_name(opts
->list
->desc
, name
);
349 if (desc
&& desc
->def_value_str
) {
350 str
= g_strdup(desc
->def_value_str
);
356 qemu_opt_del_all(opts
, name
);
360 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
364 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
365 if (is_help_option(opt
->name
)) {
372 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
373 bool defval
, bool del
)
382 opt
= qemu_opt_find(opts
, name
);
384 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
385 if (desc
&& desc
->def_value_str
) {
386 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
390 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
391 ret
= opt
->value
.boolean
;
393 qemu_opt_del_all(opts
, name
);
398 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
400 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
403 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
405 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
408 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
409 uint64_t defval
, bool del
)
412 uint64_t ret
= defval
;
418 opt
= qemu_opt_find(opts
, name
);
420 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
421 if (desc
&& desc
->def_value_str
) {
422 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
426 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
427 ret
= opt
->value
.uint
;
429 qemu_opt_del_all(opts
, name
);
434 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
436 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
439 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
442 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
445 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
446 uint64_t defval
, bool del
)
449 uint64_t ret
= defval
;
455 opt
= qemu_opt_find(opts
, name
);
457 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
458 if (desc
&& desc
->def_value_str
) {
459 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
463 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
464 ret
= opt
->value
.uint
;
466 qemu_opt_del_all(opts
, name
);
471 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
473 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
476 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
479 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
482 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
484 if (opt
->desc
== NULL
)
487 switch (opt
->desc
->type
) {
488 case QEMU_OPT_STRING
:
492 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
494 case QEMU_OPT_NUMBER
:
495 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
498 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
505 static bool opts_accepts_any(const QemuOpts
*opts
)
507 return opts
->list
->desc
[0].name
== NULL
;
510 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
512 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
514 assert(opts_accepts_any(opts
));
524 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
525 bool prepend
, Error
**errp
)
528 const QemuOptDesc
*desc
;
529 Error
*local_err
= NULL
;
531 desc
= find_desc_by_name(opts
->list
->desc
, name
);
532 if (!desc
&& !opts_accepts_any(opts
)) {
533 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
537 opt
= g_malloc0(sizeof(*opt
));
538 opt
->name
= g_strdup(name
);
541 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
543 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
546 opt
->str
= g_strdup(value
);
548 qemu_opt_parse(opt
, &local_err
);
550 error_propagate(errp
, local_err
);
555 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
558 opt_set(opts
, name
, value
, false, errp
);
561 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
565 const QemuOptDesc
*desc
= opts
->list
->desc
;
567 opt
= g_malloc0(sizeof(*opt
));
568 opt
->desc
= find_desc_by_name(desc
, name
);
569 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
570 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
575 opt
->name
= g_strdup(name
);
577 opt
->value
.boolean
= !!val
;
578 opt
->str
= g_strdup(val
? "on" : "off");
579 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
582 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
586 const QemuOptDesc
*desc
= opts
->list
->desc
;
588 opt
= g_malloc0(sizeof(*opt
));
589 opt
->desc
= find_desc_by_name(desc
, name
);
590 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
591 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
596 opt
->name
= g_strdup(name
);
598 opt
->value
.uint
= val
;
599 opt
->str
= g_strdup_printf("%" PRId64
, val
);
600 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
604 * For each member of @opts, call @func(@opaque, name, value, @errp).
605 * @func() may store an Error through @errp, but must return non-zero then.
606 * When @func() returns non-zero, break the loop and return that value.
607 * Return zero when the loop completes.
609 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
615 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
616 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
620 assert(!errp
|| !*errp
);
625 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
629 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
630 if (!opts
->id
&& !id
) {
633 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
640 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
641 int fail_if_exists
, Error
**errp
)
643 QemuOpts
*opts
= NULL
;
646 if (!id_wellformed(id
)) {
647 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
649 error_append_hint(errp
, "Identifiers consist of letters, digits, "
650 "'-', '.', '_', starting with a letter.\n");
653 opts
= qemu_opts_find(list
, id
);
655 if (fail_if_exists
&& !list
->merge_lists
) {
656 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
662 } else if (list
->merge_lists
) {
663 opts
= qemu_opts_find(list
, NULL
);
668 opts
= g_malloc0(sizeof(*opts
));
669 opts
->id
= g_strdup(id
);
671 loc_save(&opts
->loc
);
672 QTAILQ_INIT(&opts
->head
);
673 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
677 void qemu_opts_reset(QemuOptsList
*list
)
679 QemuOpts
*opts
, *next_opts
;
681 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
686 void qemu_opts_loc_restore(QemuOpts
*opts
)
688 loc_restore(&opts
->loc
);
691 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
692 const char *name
, const char *value
, Error
**errp
)
695 Error
*local_err
= NULL
;
697 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
699 error_propagate(errp
, local_err
);
702 qemu_opt_set(opts
, name
, value
, errp
);
705 const char *qemu_opts_id(QemuOpts
*opts
)
710 /* The id string will be g_free()d by qemu_opts_del */
711 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
716 void qemu_opts_del(QemuOpts
*opts
)
725 opt
= QTAILQ_FIRST(&opts
->head
);
730 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
735 /* print value, escaping any commas in value */
736 static void escaped_print(const char *value
)
740 for (ptr
= value
; *ptr
; ++ptr
) {
748 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
751 QemuOptDesc
*desc
= opts
->list
->desc
;
752 const char *sep
= "";
755 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
759 if (desc
[0].name
== NULL
) {
760 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
761 printf("%s%s=", sep
, opt
->name
);
762 escaped_print(opt
->str
);
767 for (; desc
&& desc
->name
; desc
++) {
769 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
771 value
= opt
? opt
->str
: desc
->def_value_str
;
775 if (desc
->type
== QEMU_OPT_STRING
) {
776 printf("%s%s=", sep
, desc
->name
);
777 escaped_print(value
);
778 } else if ((desc
->type
== QEMU_OPT_SIZE
||
779 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
780 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
782 printf("%s%s=%s", sep
, desc
->name
, value
);
788 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
789 const char *firstname
, bool prepend
, Error
**errp
)
791 char option
[128], value
[1024];
792 const char *p
,*pe
,*pc
;
793 Error
*local_err
= NULL
;
795 for (p
= params
; *p
!= '\0'; p
++) {
798 if (!pe
|| (pc
&& pc
< pe
)) {
799 /* found "foo,more" */
800 if (p
== params
&& firstname
) {
801 /* implicitly named first option */
802 pstrcpy(option
, sizeof(option
), firstname
);
803 p
= get_opt_value(value
, sizeof(value
), p
);
805 /* option without value, probably a flag */
806 p
= get_opt_name(option
, sizeof(option
), p
, ',');
807 if (strncmp(option
, "no", 2) == 0) {
808 memmove(option
, option
+2, strlen(option
+2)+1);
809 pstrcpy(value
, sizeof(value
), "off");
811 pstrcpy(value
, sizeof(value
), "on");
815 /* found "foo=bar,more" */
816 p
= get_opt_name(option
, sizeof(option
), p
, '=');
821 p
= get_opt_value(value
, sizeof(value
), p
);
823 if (strcmp(option
, "id") != 0) {
824 /* store and parse */
825 opt_set(opts
, option
, value
, prepend
, &local_err
);
827 error_propagate(errp
, local_err
);
838 * Store options parsed from @params into @opts.
839 * If @firstname is non-null, the first key=value in @params may omit
840 * key=, and is treated as if key was @firstname.
841 * On error, store an error object through @errp if non-null.
843 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
844 const char *firstname
, Error
**errp
)
846 opts_do_parse(opts
, params
, firstname
, false, errp
);
849 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
850 bool permit_abbrev
, bool defaults
, Error
**errp
)
852 const char *firstname
;
853 char value
[1024], *id
= NULL
;
856 Error
*local_err
= NULL
;
858 assert(!permit_abbrev
|| list
->implied_opt_name
);
859 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
861 if (strncmp(params
, "id=", 3) == 0) {
862 get_opt_value(value
, sizeof(value
), params
+3);
864 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
865 get_opt_value(value
, sizeof(value
), p
+4);
870 * This code doesn't work for defaults && !list->merge_lists: when
871 * params has no id=, and list has an element with !opts->id, it
872 * appends a new element instead of returning the existing opts.
873 * However, we got no use for this case. Guard against possible
874 * (if unlikely) future misuse:
876 assert(!defaults
|| list
->merge_lists
);
877 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
879 error_propagate(errp
, local_err
);
883 opts_do_parse(opts
, params
, firstname
, defaults
, &local_err
);
885 error_propagate(errp
, local_err
);
894 * Create a QemuOpts in @list and with options parsed from @params.
895 * If @permit_abbrev, the first key=value in @params may omit key=,
896 * and is treated as if key was @list->implied_opt_name.
897 * On error, store an error object through @errp if non-null.
898 * Return the new QemuOpts on success, null pointer on error.
900 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
901 bool permit_abbrev
, Error
**errp
)
903 return opts_parse(list
, params
, permit_abbrev
, false, errp
);
907 * Create a QemuOpts in @list and with options parsed from @params.
908 * If @permit_abbrev, the first key=value in @params may omit key=,
909 * and is treated as if key was @list->implied_opt_name.
910 * Report errors with error_report_err(). This is inappropriate in
911 * QMP context. Do not use this function there!
912 * Return the new QemuOpts on success, null pointer on error.
914 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
920 opts
= opts_parse(list
, params
, permit_abbrev
, false, &err
);
922 error_report_err(err
);
927 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
932 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
);
936 typedef struct OptsFromQDictState
{
939 } OptsFromQDictState
;
941 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
943 OptsFromQDictState
*state
= opaque
;
948 if (!strcmp(key
, "id") || *state
->errp
) {
952 switch (qobject_type(obj
)) {
954 value
= qstring_get_str(qobject_to_qstring(obj
));
957 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
958 qint_get_int(qobject_to_qint(obj
)));
959 assert(n
< sizeof(buf
));
963 n
= snprintf(buf
, sizeof(buf
), "%.17g",
964 qfloat_get_double(qobject_to_qfloat(obj
)));
965 assert(n
< sizeof(buf
));
969 pstrcpy(buf
, sizeof(buf
),
970 qbool_get_bool(qobject_to_qbool(obj
)) ? "on" : "off");
977 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
981 * Create QemuOpts from a QDict.
982 * Use value of key "id" as ID if it exists and is a QString.
983 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
984 * other types are silently ignored.
986 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
989 OptsFromQDictState state
;
990 Error
*local_err
= NULL
;
993 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
996 error_propagate(errp
, local_err
);
1000 assert(opts
!= NULL
);
1002 state
.errp
= &local_err
;
1004 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1006 error_propagate(errp
, local_err
);
1007 qemu_opts_del(opts
);
1015 * Adds all QDict entries to the QemuOpts that can be added and removes them
1016 * from the QDict. When this function returns, the QDict contains only those
1017 * entries that couldn't be added to the QemuOpts.
1019 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1021 const QDictEntry
*entry
, *next
;
1023 entry
= qdict_first(qdict
);
1025 while (entry
!= NULL
) {
1026 Error
*local_err
= NULL
;
1027 OptsFromQDictState state
= {
1032 next
= qdict_next(qdict
, entry
);
1034 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1035 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1037 error_propagate(errp
, local_err
);
1040 qdict_del(qdict
, entry
->key
);
1049 * Convert from QemuOpts to QDict.
1050 * The QDict values are of type QString.
1051 * TODO We'll want to use types appropriate for opt->desc->type, but
1052 * this is enough for now.
1054 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1060 qdict
= qdict_new();
1063 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1065 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1066 val
= QOBJECT(qstring_from_str(opt
->str
));
1067 qdict_put_obj(qdict
, opt
->name
, val
);
1072 /* Validate parsed opts against descriptions where no
1073 * descriptions were provided in the QemuOptsList.
1075 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1078 Error
*local_err
= NULL
;
1080 assert(opts_accepts_any(opts
));
1082 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1083 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1085 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1089 qemu_opt_parse(opt
, &local_err
);
1091 error_propagate(errp
, local_err
);
1098 * For each member of @list, call @func(@opaque, member, @errp).
1099 * Call it with the current location temporarily set to the member's.
1100 * @func() may store an Error through @errp, but must return non-zero then.
1101 * When @func() returns non-zero, break the loop and return that value.
1102 * Return zero when the loop completes.
1104 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1105 void *opaque
, Error
**errp
)
1111 loc_push_none(&loc
);
1112 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1113 loc_restore(&opts
->loc
);
1114 rc
= func(opaque
, opts
, errp
);
1118 assert(!errp
|| !*errp
);
1124 static size_t count_opts_list(QemuOptsList
*list
)
1126 QemuOptDesc
*desc
= NULL
;
1127 size_t num_opts
= 0;
1134 while (desc
&& desc
->name
) {
1142 void qemu_opts_free(QemuOptsList
*list
)
1147 /* Realloc dst option list and append options from an option list (list)
1148 * to it. dst could be NULL or a malloced list.
1149 * The lifetime of dst must be shorter than the input list because the
1150 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1152 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1155 size_t num_opts
, num_dst_opts
;
1157 bool need_init
= false;
1158 bool need_head_update
;
1164 /* If dst is NULL, after realloc, some area of dst should be initialized
1165 * before adding options to it.
1169 need_head_update
= true;
1171 /* Moreover, even if dst is not NULL, the realloc may move it to a
1172 * different address in which case we may get a stale tail pointer
1174 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1177 num_opts
= count_opts_list(dst
);
1178 num_dst_opts
= num_opts
;
1179 num_opts
+= count_opts_list(list
);
1180 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1181 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1184 dst
->implied_opt_name
= NULL
;
1185 dst
->merge_lists
= false;
1187 if (need_head_update
) {
1188 QTAILQ_INIT(&dst
->head
);
1190 dst
->desc
[num_dst_opts
].name
= NULL
;
1192 /* append list->desc to dst->desc */
1195 while (desc
&& desc
->name
) {
1196 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1197 dst
->desc
[num_dst_opts
++] = *desc
;
1198 dst
->desc
[num_dst_opts
].name
= NULL
;