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/qbool.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qapi/qmp/qnum.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qapi/qmp/qerror.h"
36 #include "qemu/option_int.h"
37 #include "qemu/cutils.h"
39 #include "qemu/help_option.h"
42 * Extracts the name of an option from the parameter string (p points at the
43 * first byte of the option name)
45 * The option name is delimited by delim (usually , or =) or the string end
46 * and is copied into option. The caller is responsible for free'ing option
47 * when no longer required.
49 * The return value is the position of the delimiter/zero byte after the option
52 static const char *get_opt_name(const char *p
, char **option
, char delim
)
54 char *offset
= strchr(p
, delim
);
57 *option
= g_strndup(p
, offset
- p
);
60 *option
= g_strdup(p
);
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(const char *p
, char **value
)
75 size_t capacity
= 0, length
;
80 offset
= qemu_strchrnul(p
, ',');
82 if (*offset
!= '\0' && *(offset
+ 1) == ',') {
85 *value
= g_renew(char, *value
, capacity
+ length
+ 1);
86 strncpy(*value
+ capacity
, p
, length
);
87 (*value
)[capacity
+ length
] = '\0';
89 if (*offset
== '\0' ||
90 *(offset
+ 1) != ',') {
94 p
+= (offset
- p
) + 2;
100 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
103 if (!strcmp(value
, "on")) {
105 } else if (!strcmp(value
, "off")) {
108 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
109 name
, "'on' or 'off'");
113 static void parse_option_number(const char *name
, const char *value
,
114 uint64_t *ret
, Error
**errp
)
119 err
= qemu_strtou64(value
, NULL
, 0, &number
);
120 if (err
== -ERANGE
) {
121 error_setg(errp
, "Value '%s' is too large for parameter '%s'",
126 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
132 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
137 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
138 if (strcmp(desc
[i
].name
, name
) == 0) {
146 void parse_option_size(const char *name
, const char *value
,
147 uint64_t *ret
, Error
**errp
)
152 err
= qemu_strtosz(value
, NULL
, &size
);
153 if (err
== -ERANGE
) {
154 error_setg(errp
, "Value '%s' is out of range for parameter '%s'",
159 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
160 "a non-negative number below 2^64");
161 error_append_hint(errp
, "Optional suffix k, M, G, T, P or E means"
162 " kilo-, mega-, giga-, tera-, peta-\n"
163 "and exabytes, respectively.\n");
169 bool has_help_option(const char *param
)
171 const char *p
= param
;
174 while (*p
&& !result
) {
177 p
= get_opt_value(p
, &value
);
182 result
= is_help_option(value
);
189 bool is_valid_option_list(const char *p
)
195 p
= get_opt_value(p
, &value
);
197 (!*value
|| *value
== ',')) {
211 static const char *opt_type_to_string(enum QemuOptType type
)
214 case QEMU_OPT_STRING
:
217 return "bool (on/off)";
218 case QEMU_OPT_NUMBER
:
224 g_assert_not_reached();
227 void qemu_opts_print_help(QemuOptsList
*list
)
231 GPtrArray
*array
= g_ptr_array_new();
235 while (desc
&& desc
->name
) {
236 GString
*str
= g_string_new(NULL
);
238 g_string_append_printf(str
, "%s.", list
->name
);
240 g_string_append_printf(str
, "%s=%s", desc
->name
,
241 opt_type_to_string(desc
->type
));
243 g_string_append_printf(str
, " - %s", desc
->help
);
245 g_ptr_array_add(array
, g_string_free(str
, false));
249 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
250 for (i
= 0; i
< array
->len
; i
++) {
251 printf("%s\n", (char *)array
->pdata
[i
]);
253 g_ptr_array_set_free_func(array
, g_free
);
254 g_ptr_array_free(array
, true);
257 /* ------------------------------------------------------------------ */
259 QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
263 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
264 if (strcmp(opt
->name
, name
) != 0)
271 static void qemu_opt_del(QemuOpt
*opt
)
273 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
279 /* qemu_opt_set allows many settings for the same option.
280 * This function deletes all settings for an option.
282 static void qemu_opt_del_all(QemuOpts
*opts
, const char *name
)
284 QemuOpt
*opt
, *next_opt
;
286 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next_opt
) {
287 if (!strcmp(opt
->name
, name
)) {
293 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
301 opt
= qemu_opt_find(opts
, name
);
303 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
304 if (desc
&& desc
->def_value_str
) {
305 return desc
->def_value_str
;
308 return opt
? opt
->str
: NULL
;
311 void qemu_opt_iter_init(QemuOptsIter
*iter
, QemuOpts
*opts
, const char *name
)
314 iter
->opt
= QTAILQ_FIRST(&opts
->head
);
318 const char *qemu_opt_iter_next(QemuOptsIter
*iter
)
320 QemuOpt
*ret
= iter
->opt
;
322 while (ret
&& !g_str_equal(iter
->name
, ret
->name
)) {
323 ret
= QTAILQ_NEXT(ret
, next
);
326 iter
->opt
= ret
? QTAILQ_NEXT(ret
, next
) : NULL
;
327 return ret
? ret
->str
: NULL
;
330 /* Get a known option (or its default) and remove it from the list
331 * all in one action. Return a malloced string of the option value.
332 * Result must be freed by caller with g_free().
334 char *qemu_opt_get_del(QemuOpts
*opts
, const char *name
)
337 const QemuOptDesc
*desc
;
344 opt
= qemu_opt_find(opts
, name
);
346 desc
= find_desc_by_name(opts
->list
->desc
, name
);
347 if (desc
&& desc
->def_value_str
) {
348 str
= g_strdup(desc
->def_value_str
);
354 qemu_opt_del_all(opts
, name
);
358 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
362 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
363 if (is_help_option(opt
->name
)) {
370 static bool qemu_opt_get_bool_helper(QemuOpts
*opts
, const char *name
,
371 bool defval
, bool del
)
380 opt
= qemu_opt_find(opts
, name
);
382 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
383 if (desc
&& desc
->def_value_str
) {
384 parse_option_bool(name
, desc
->def_value_str
, &ret
, &error_abort
);
388 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
389 ret
= opt
->value
.boolean
;
391 qemu_opt_del_all(opts
, name
);
396 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
398 return qemu_opt_get_bool_helper(opts
, name
, defval
, false);
401 bool qemu_opt_get_bool_del(QemuOpts
*opts
, const char *name
, bool defval
)
403 return qemu_opt_get_bool_helper(opts
, name
, defval
, true);
406 static uint64_t qemu_opt_get_number_helper(QemuOpts
*opts
, const char *name
,
407 uint64_t defval
, bool del
)
410 uint64_t ret
= defval
;
416 opt
= qemu_opt_find(opts
, name
);
418 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
419 if (desc
&& desc
->def_value_str
) {
420 parse_option_number(name
, desc
->def_value_str
, &ret
, &error_abort
);
424 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
425 ret
= opt
->value
.uint
;
427 qemu_opt_del_all(opts
, name
);
432 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
434 return qemu_opt_get_number_helper(opts
, name
, defval
, false);
437 uint64_t qemu_opt_get_number_del(QemuOpts
*opts
, const char *name
,
440 return qemu_opt_get_number_helper(opts
, name
, defval
, true);
443 static uint64_t qemu_opt_get_size_helper(QemuOpts
*opts
, const char *name
,
444 uint64_t defval
, bool del
)
447 uint64_t ret
= defval
;
453 opt
= qemu_opt_find(opts
, name
);
455 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
456 if (desc
&& desc
->def_value_str
) {
457 parse_option_size(name
, desc
->def_value_str
, &ret
, &error_abort
);
461 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
462 ret
= opt
->value
.uint
;
464 qemu_opt_del_all(opts
, name
);
469 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
471 return qemu_opt_get_size_helper(opts
, name
, defval
, false);
474 uint64_t qemu_opt_get_size_del(QemuOpts
*opts
, const char *name
,
477 return qemu_opt_get_size_helper(opts
, name
, defval
, true);
480 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
482 if (opt
->desc
== NULL
)
485 switch (opt
->desc
->type
) {
486 case QEMU_OPT_STRING
:
490 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
492 case QEMU_OPT_NUMBER
:
493 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
496 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
503 static bool opts_accepts_any(const QemuOpts
*opts
)
505 return opts
->list
->desc
[0].name
== NULL
;
508 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
510 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
512 assert(opts_accepts_any(opts
));
522 static void opt_set(QemuOpts
*opts
, const char *name
, char *value
,
523 bool prepend
, bool *invalidp
, Error
**errp
)
526 const QemuOptDesc
*desc
;
527 Error
*local_err
= NULL
;
529 desc
= find_desc_by_name(opts
->list
->desc
, name
);
530 if (!desc
&& !opts_accepts_any(opts
)) {
532 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
539 opt
= g_malloc0(sizeof(*opt
));
540 opt
->name
= g_strdup(name
);
543 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
545 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
549 qemu_opt_parse(opt
, &local_err
);
551 error_propagate(errp
, local_err
);
556 void qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
559 opt_set(opts
, name
, g_strdup(value
), false, NULL
, errp
);
562 void qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
,
566 const QemuOptDesc
*desc
= opts
->list
->desc
;
568 opt
= g_malloc0(sizeof(*opt
));
569 opt
->desc
= find_desc_by_name(desc
, name
);
570 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
571 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
576 opt
->name
= g_strdup(name
);
578 opt
->value
.boolean
= !!val
;
579 opt
->str
= g_strdup(val
? "on" : "off");
580 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
583 void qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
,
587 const QemuOptDesc
*desc
= opts
->list
->desc
;
589 opt
= g_malloc0(sizeof(*opt
));
590 opt
->desc
= find_desc_by_name(desc
, name
);
591 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
592 error_setg(errp
, QERR_INVALID_PARAMETER
, name
);
597 opt
->name
= g_strdup(name
);
599 opt
->value
.uint
= val
;
600 opt
->str
= g_strdup_printf("%" PRId64
, val
);
601 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
605 * For each member of @opts, call @func(@opaque, name, value, @errp).
606 * @func() may store an Error through @errp, but must return non-zero then.
607 * When @func() returns non-zero, break the loop and return that value.
608 * Return zero when the loop completes.
610 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
616 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
617 rc
= func(opaque
, opt
->name
, opt
->str
, errp
);
621 assert(!errp
|| !*errp
);
626 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
630 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
631 if (!opts
->id
&& !id
) {
634 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
641 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
642 int fail_if_exists
, Error
**errp
)
644 QemuOpts
*opts
= NULL
;
647 if (!id_wellformed(id
)) {
648 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "id",
650 error_append_hint(errp
, "Identifiers consist of letters, digits, "
651 "'-', '.', '_', starting with a letter.\n");
654 opts
= qemu_opts_find(list
, id
);
656 if (fail_if_exists
&& !list
->merge_lists
) {
657 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
663 } else if (list
->merge_lists
) {
664 opts
= qemu_opts_find(list
, NULL
);
669 opts
= g_malloc0(sizeof(*opts
));
670 opts
->id
= g_strdup(id
);
672 loc_save(&opts
->loc
);
673 QTAILQ_INIT(&opts
->head
);
674 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
678 void qemu_opts_reset(QemuOptsList
*list
)
680 QemuOpts
*opts
, *next_opts
;
682 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
687 void qemu_opts_loc_restore(QemuOpts
*opts
)
689 loc_restore(&opts
->loc
);
692 void qemu_opts_set(QemuOptsList
*list
, const char *id
,
693 const char *name
, const char *value
, Error
**errp
)
696 Error
*local_err
= NULL
;
698 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
700 error_propagate(errp
, local_err
);
703 qemu_opt_set(opts
, name
, value
, errp
);
706 const char *qemu_opts_id(QemuOpts
*opts
)
711 /* The id string will be g_free()d by qemu_opts_del */
712 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
717 void qemu_opts_del(QemuOpts
*opts
)
726 opt
= QTAILQ_FIRST(&opts
->head
);
731 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
736 /* print value, escaping any commas in value */
737 static void escaped_print(const char *value
)
741 for (ptr
= value
; *ptr
; ++ptr
) {
749 void qemu_opts_print(QemuOpts
*opts
, const char *separator
)
752 QemuOptDesc
*desc
= opts
->list
->desc
;
753 const char *sep
= "";
756 printf("id=%s", opts
->id
); /* passed id_wellformed -> no commas */
760 if (desc
[0].name
== NULL
) {
761 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
762 printf("%s%s=", sep
, opt
->name
);
763 escaped_print(opt
->str
);
768 for (; desc
&& desc
->name
; desc
++) {
770 opt
= qemu_opt_find(opts
, desc
->name
);
772 value
= opt
? opt
->str
: desc
->def_value_str
;
776 if (desc
->type
== QEMU_OPT_STRING
) {
777 printf("%s%s=", sep
, desc
->name
);
778 escaped_print(value
);
779 } else if ((desc
->type
== QEMU_OPT_SIZE
||
780 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
781 printf("%s%s=%" PRId64
, sep
, desc
->name
, opt
->value
.uint
);
783 printf("%s%s=%s", sep
, desc
->name
, value
);
789 static void opts_do_parse(QemuOpts
*opts
, const char *params
,
790 const char *firstname
, bool prepend
,
791 bool *invalidp
, Error
**errp
)
795 const char *p
,*pe
,*pc
;
796 Error
*local_err
= NULL
;
798 for (p
= params
; *p
!= '\0'; p
++) {
801 if (!pe
|| (pc
&& pc
< pe
)) {
802 /* found "foo,more" */
803 if (p
== params
&& firstname
) {
804 /* implicitly named first option */
805 option
= g_strdup(firstname
);
806 p
= get_opt_value(p
, &value
);
808 /* option without value, probably a flag */
809 p
= get_opt_name(p
, &option
, ',');
810 if (strncmp(option
, "no", 2) == 0) {
811 memmove(option
, option
+2, strlen(option
+2)+1);
812 value
= g_strdup("off");
814 value
= g_strdup("on");
818 /* found "foo=bar,more" */
819 p
= get_opt_name(p
, &option
, '=');
822 p
= get_opt_value(p
, &value
);
824 if (strcmp(option
, "id") != 0) {
825 /* store and parse */
826 opt_set(opts
, option
, value
, prepend
, invalidp
, &local_err
);
829 error_propagate(errp
, local_err
);
838 option
= value
= NULL
;
847 * Store options parsed from @params into @opts.
848 * If @firstname is non-null, the first key=value in @params may omit
849 * key=, and is treated as if key was @firstname.
850 * On error, store an error object through @errp if non-null.
852 void qemu_opts_do_parse(QemuOpts
*opts
, const char *params
,
853 const char *firstname
, Error
**errp
)
855 opts_do_parse(opts
, params
, firstname
, false, NULL
, errp
);
858 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
859 bool permit_abbrev
, bool defaults
,
860 bool *invalidp
, Error
**errp
)
862 const char *firstname
;
866 Error
*local_err
= NULL
;
868 assert(!permit_abbrev
|| list
->implied_opt_name
);
869 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
871 if (strncmp(params
, "id=", 3) == 0) {
872 get_opt_value(params
+ 3, &id
);
873 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
874 get_opt_value(p
+ 4, &id
);
878 * This code doesn't work for defaults && !list->merge_lists: when
879 * params has no id=, and list has an element with !opts->id, it
880 * appends a new element instead of returning the existing opts.
881 * However, we got no use for this case. Guard against possible
882 * (if unlikely) future misuse:
884 assert(!defaults
|| list
->merge_lists
);
885 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
888 error_propagate(errp
, local_err
);
892 opts_do_parse(opts
, params
, firstname
, defaults
, invalidp
, &local_err
);
894 error_propagate(errp
, local_err
);
903 * Create a QemuOpts in @list and with options parsed from @params.
904 * If @permit_abbrev, the first key=value in @params may omit key=,
905 * and is treated as if key was @list->implied_opt_name.
906 * On error, store an error object through @errp if non-null.
907 * Return the new QemuOpts on success, null pointer on error.
909 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
910 bool permit_abbrev
, Error
**errp
)
912 return opts_parse(list
, params
, permit_abbrev
, false, NULL
, errp
);
916 * Create a QemuOpts in @list and with options parsed from @params.
917 * If @permit_abbrev, the first key=value in @params may omit key=,
918 * and is treated as if key was @list->implied_opt_name.
919 * Report errors with error_report_err(). This is inappropriate in
920 * QMP context. Do not use this function there!
921 * Return the new QemuOpts on success, null pointer on error.
923 QemuOpts
*qemu_opts_parse_noisily(QemuOptsList
*list
, const char *params
,
928 bool invalidp
= false;
930 opts
= opts_parse(list
, params
, permit_abbrev
, false, &invalidp
, &err
);
932 if (invalidp
&& has_help_option(params
)) {
933 qemu_opts_print_help(list
);
936 error_report_err(err
);
942 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
947 opts
= opts_parse(list
, params
, permit_abbrev
, true, NULL
, NULL
);
951 typedef struct OptsFromQDictState
{
954 } OptsFromQDictState
;
956 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
958 OptsFromQDictState
*state
= opaque
;
959 char buf
[32], *tmp
= NULL
;
962 if (!strcmp(key
, "id") || *state
->errp
) {
966 switch (qobject_type(obj
)) {
968 value
= qstring_get_str(qobject_to(QString
, obj
));
971 tmp
= qnum_to_string(qobject_to(QNum
, obj
));
975 pstrcpy(buf
, sizeof(buf
),
976 qbool_get_bool(qobject_to(QBool
, obj
)) ? "on" : "off");
983 qemu_opt_set(state
->opts
, key
, value
, state
->errp
);
988 * Create QemuOpts from a QDict.
989 * Use value of key "id" as ID if it exists and is a QString. Only
990 * QStrings, QNums and QBools are copied. Entries with other types
991 * are silently ignored.
993 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
996 OptsFromQDictState state
;
997 Error
*local_err
= NULL
;
1000 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1003 error_propagate(errp
, local_err
);
1007 assert(opts
!= NULL
);
1009 state
.errp
= &local_err
;
1011 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1013 error_propagate(errp
, local_err
);
1014 qemu_opts_del(opts
);
1022 * Adds all QDict entries to the QemuOpts that can be added and removes them
1023 * from the QDict. When this function returns, the QDict contains only those
1024 * entries that couldn't be added to the QemuOpts.
1026 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1028 const QDictEntry
*entry
, *next
;
1030 entry
= qdict_first(qdict
);
1032 while (entry
!= NULL
) {
1033 Error
*local_err
= NULL
;
1034 OptsFromQDictState state
= {
1039 next
= qdict_next(qdict
, entry
);
1041 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1042 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1044 error_propagate(errp
, local_err
);
1047 qdict_del(qdict
, entry
->key
);
1056 * Convert from QemuOpts to QDict. The QDict values are of type QString.
1058 * If @list is given, only add those options to the QDict that are contained in
1059 * the list. If @del is true, any options added to the QDict are removed from
1060 * the QemuOpts, otherwise they remain there.
1062 * If two options in @opts have the same name, they are processed in order
1063 * so that the last one wins (consistent with the reverse iteration in
1064 * qemu_opt_find()), but all of them are deleted if @del is true.
1066 * TODO We'll want to use types appropriate for opt->desc->type, but
1067 * this is enough for now.
1069 QDict
*qemu_opts_to_qdict_filtered(QemuOpts
*opts
, QDict
*qdict
,
1070 QemuOptsList
*list
, bool del
)
1072 QemuOpt
*opt
, *next
;
1075 qdict
= qdict_new();
1078 qdict_put_str(qdict
, "id", opts
->id
);
1080 QTAILQ_FOREACH_SAFE(opt
, &opts
->head
, next
, next
) {
1084 for (desc
= list
->desc
; desc
->name
; desc
++) {
1085 if (!strcmp(desc
->name
, opt
->name
)) {
1094 qdict_put_str(qdict
, opt
->name
, opt
->str
);
1102 /* Copy all options in a QemuOpts to the given QDict. See
1103 * qemu_opts_to_qdict_filtered() for details. */
1104 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1106 return qemu_opts_to_qdict_filtered(opts
, qdict
, NULL
, false);
1109 /* Validate parsed opts against descriptions where no
1110 * descriptions were provided in the QemuOptsList.
1112 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1115 Error
*local_err
= NULL
;
1117 assert(opts_accepts_any(opts
));
1119 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1120 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1122 error_setg(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1126 qemu_opt_parse(opt
, &local_err
);
1128 error_propagate(errp
, local_err
);
1135 * For each member of @list, call @func(@opaque, member, @errp).
1136 * Call it with the current location temporarily set to the member's.
1137 * @func() may store an Error through @errp, but must return non-zero then.
1138 * When @func() returns non-zero, break the loop and return that value.
1139 * Return zero when the loop completes.
1141 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
,
1142 void *opaque
, Error
**errp
)
1148 loc_push_none(&loc
);
1149 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1150 loc_restore(&opts
->loc
);
1151 rc
= func(opaque
, opts
, errp
);
1155 assert(!errp
|| !*errp
);
1161 static size_t count_opts_list(QemuOptsList
*list
)
1163 QemuOptDesc
*desc
= NULL
;
1164 size_t num_opts
= 0;
1171 while (desc
&& desc
->name
) {
1179 void qemu_opts_free(QemuOptsList
*list
)
1184 /* Realloc dst option list and append options from an option list (list)
1185 * to it. dst could be NULL or a malloced list.
1186 * The lifetime of dst must be shorter than the input list because the
1187 * QemuOptDesc->name, ->help, and ->def_value_str strings are shared.
1189 QemuOptsList
*qemu_opts_append(QemuOptsList
*dst
,
1192 size_t num_opts
, num_dst_opts
;
1194 bool need_init
= false;
1195 bool need_head_update
;
1201 /* If dst is NULL, after realloc, some area of dst should be initialized
1202 * before adding options to it.
1206 need_head_update
= true;
1208 /* Moreover, even if dst is not NULL, the realloc may move it to a
1209 * different address in which case we may get a stale tail pointer
1211 need_head_update
= QTAILQ_EMPTY(&dst
->head
);
1214 num_opts
= count_opts_list(dst
);
1215 num_dst_opts
= num_opts
;
1216 num_opts
+= count_opts_list(list
);
1217 dst
= g_realloc(dst
, sizeof(QemuOptsList
) +
1218 (num_opts
+ 1) * sizeof(QemuOptDesc
));
1221 dst
->implied_opt_name
= NULL
;
1222 dst
->merge_lists
= false;
1224 if (need_head_update
) {
1225 QTAILQ_INIT(&dst
->head
);
1227 dst
->desc
[num_dst_opts
].name
= NULL
;
1229 /* append list->desc to dst->desc */
1232 while (desc
&& desc
->name
) {
1233 if (find_desc_by_name(dst
->desc
, desc
->name
) == NULL
) {
1234 dst
->desc
[num_dst_opts
++] = *desc
;
1235 dst
->desc
[num_dst_opts
].name
= NULL
;