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
29 #include "qemu-common.h"
30 #include "qemu/error-report.h"
31 #include "qapi/qmp/types.h"
32 #include "qapi/error.h"
33 #include "qapi/qmp/qerror.h"
34 #include "qemu/option_int.h"
37 * Extracts the name of an option from the parameter string (p points at the
38 * first byte of the option name)
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.
44 * The return value is the position of the delimiter/zero byte after the option
47 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
52 while (*p
!= '\0' && *p
!= delim
) {
53 if (q
&& (q
- buf
) < buf_size
- 1)
64 * Extracts the value of an option from the parameter string p (p points at the
65 * first byte of the option value)
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.
71 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
82 if (q
&& (q
- buf
) < buf_size
- 1)
92 int get_next_param_value(char *buf
, int buf_size
,
93 const char *tag
, const char **pstr
)
100 p
= get_opt_name(option
, sizeof(option
), p
, '=');
104 if (!strcmp(tag
, option
)) {
105 *pstr
= get_opt_value(buf
, buf_size
, p
);
111 p
= get_opt_value(NULL
, 0, p
);
120 int get_param_value(char *buf
, int buf_size
,
121 const char *tag
, const char *str
)
123 return get_next_param_value(buf
, buf_size
, tag
, &str
);
127 * Searches an option list for an option with the given name
129 QEMUOptionParameter
*get_option_parameter(QEMUOptionParameter
*list
,
132 while (list
&& list
->name
) {
133 if (!strcmp(list
->name
, name
)) {
142 static void parse_option_bool(const char *name
, const char *value
, bool *ret
,
146 if (!strcmp(value
, "on")) {
148 } else if (!strcmp(value
, "off")) {
151 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, name
, "'on' or 'off'");
158 static void parse_option_number(const char *name
, const char *value
,
159 uint64_t *ret
, Error
**errp
)
165 number
= strtoull(value
, &postfix
, 0);
166 if (*postfix
!= '\0') {
167 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
172 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a number");
176 static const QemuOptDesc
*find_desc_by_name(const QemuOptDesc
*desc
,
181 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
182 if (strcmp(desc
[i
].name
, name
) == 0) {
190 void parse_option_size(const char *name
, const char *value
,
191 uint64_t *ret
, Error
**errp
)
197 sizef
= strtod(value
, &postfix
);
214 *ret
= (uint64_t) sizef
;
217 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
218 #if 0 /* conversion from qerror_report() to error_set() broke this: */
219 error_printf_unless_qmp("You may use k, M, G or T suffixes for "
220 "kilobytes, megabytes, gigabytes and terabytes.\n");
225 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
, "a size");
230 * Sets the value of a parameter in a given option list. The parsing of the
231 * value depends on the type of option:
233 * OPT_FLAG (uses value.n):
234 * If no value is given, the flag is set to 1.
235 * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
237 * OPT_STRING (uses value.s):
238 * value is strdup()ed and assigned as option value
240 * OPT_SIZE (uses value.n):
241 * The value is converted to an integer. Suffixes for kilobytes etc. are
242 * allowed (powers of 1024).
244 * Returns 0 on succes, -1 in error cases
246 int set_option_parameter(QEMUOptionParameter
*list
, const char *name
,
250 Error
*local_err
= NULL
;
252 // Find a matching parameter
253 list
= get_option_parameter(list
, name
);
255 fprintf(stderr
, "Unknown option '%s'\n", name
);
260 switch (list
->type
) {
262 parse_option_bool(name
, value
, &flag
, &local_err
);
264 list
->value
.n
= flag
;
270 list
->value
.s
= g_strdup(value
);
272 fprintf(stderr
, "Option '%s' needs a parameter\n", name
);
278 parse_option_size(name
, value
, &list
->value
.n
, &local_err
);
282 fprintf(stderr
, "Bug: Option '%s' has an unknown type\n", name
);
287 qerror_report_err(local_err
);
288 error_free(local_err
);
292 list
->assigned
= true;
298 * Sets the given parameter to an integer instead of a string.
299 * This function cannot be used to set string options.
301 * Returns 0 on success, -1 in error cases
303 int set_option_parameter_int(QEMUOptionParameter
*list
, const char *name
,
306 // Find a matching parameter
307 list
= get_option_parameter(list
, name
);
309 fprintf(stderr
, "Unknown option '%s'\n", name
);
314 switch (list
->type
) {
318 list
->value
.n
= value
;
325 list
->assigned
= true;
331 * Frees a option list. If it contains strings, the strings are freed as well.
333 void free_option_parameters(QEMUOptionParameter
*list
)
335 QEMUOptionParameter
*cur
= list
;
337 while (cur
&& cur
->name
) {
338 if (cur
->type
== OPT_STRING
) {
339 g_free(cur
->value
.s
);
348 * Count valid options in list
350 static size_t count_option_parameters(QEMUOptionParameter
*list
)
352 size_t num_options
= 0;
354 while (list
&& list
->name
) {
363 * Append an option list (list) to an option list (dest).
365 * If dest is NULL, a new copy of list is created.
367 * Returns a pointer to the first element of dest (or the newly allocated copy)
369 QEMUOptionParameter
*append_option_parameters(QEMUOptionParameter
*dest
,
370 QEMUOptionParameter
*list
)
372 size_t num_options
, num_dest_options
;
374 num_options
= count_option_parameters(dest
);
375 num_dest_options
= num_options
;
377 num_options
+= count_option_parameters(list
);
379 dest
= g_realloc(dest
, (num_options
+ 1) * sizeof(QEMUOptionParameter
));
380 dest
[num_dest_options
].name
= NULL
;
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
;
394 * Parses a parameter string (param) into an option list (dest).
396 * list is the template option list. If dest is NULL, a new copy of list is
397 * created. 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
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
;
416 char *param_delim
, *value_delim
;
425 dest
= allocated
= append_option_parameters(NULL
, list
);
428 for (i
= 0; dest
[i
].name
; i
++) {
429 dest
[i
].assigned
= false;
434 // Find parameter name and value in the string
435 param_delim
= strchr(param
, ',');
436 value_delim
= strchr(param
, '=');
438 if (value_delim
&& (value_delim
< param_delim
|| !param_delim
)) {
445 param
= get_opt_name(name
, sizeof(name
), param
, next_delim
);
447 param
= get_opt_value(value
, sizeof(value
), param
+ 1);
449 if (*param
!= '\0') {
454 if (set_option_parameter(dest
, name
, value_delim
? value
: NULL
)) {
462 // Only free the list if it was newly allocated
463 free_option_parameters(allocated
);
467 bool has_help_option(const char *param
)
469 size_t buflen
= strlen(param
) + 1;
470 char *buf
= g_malloc0(buflen
);
471 const char *p
= param
;
475 p
= get_opt_value(buf
, buflen
, p
);
480 if (is_help_option(buf
)) {
491 bool is_valid_option_list(const char *param
)
493 size_t buflen
= strlen(param
) + 1;
494 char *buf
= g_malloc0(buflen
);
495 const char *p
= param
;
499 p
= get_opt_value(buf
, buflen
, p
);
505 if (!*buf
|| *buf
== ',') {
517 * Prints all options of a list that have a value to stdout
519 void print_option_parameters(QEMUOptionParameter
*list
)
521 while (list
&& list
->name
) {
522 switch (list
->type
) {
524 if (list
->value
.s
!= NULL
) {
525 printf("%s='%s' ", list
->name
, list
->value
.s
);
529 printf("%s=%s ", list
->name
, list
->value
.n
? "on" : "off");
533 printf("%s=%" PRId64
" ", list
->name
, list
->value
.n
);
536 printf("%s=(unknown type) ", list
->name
);
544 * Prints an overview of all available options
546 void print_option_help(QEMUOptionParameter
*list
)
548 printf("Supported options:\n");
549 while (list
&& list
->name
) {
550 printf("%-16s %s\n", list
->name
,
551 list
->help
? list
->help
: "No description available");
556 /* ------------------------------------------------------------------ */
558 static QemuOpt
*qemu_opt_find(QemuOpts
*opts
, const char *name
)
562 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
563 if (strcmp(opt
->name
, name
) != 0)
570 static void qemu_opt_del(QemuOpt
*opt
)
572 QTAILQ_REMOVE(&opt
->opts
->head
, opt
, next
);
578 const char *qemu_opt_get(QemuOpts
*opts
, const char *name
)
580 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
583 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
584 if (desc
&& desc
->def_value_str
) {
585 return desc
->def_value_str
;
588 return opt
? opt
->str
: NULL
;
591 bool qemu_opt_has_help_opt(QemuOpts
*opts
)
595 QTAILQ_FOREACH_REVERSE(opt
, &opts
->head
, QemuOptHead
, next
) {
596 if (is_help_option(opt
->name
)) {
603 bool qemu_opt_get_bool(QemuOpts
*opts
, const char *name
, bool defval
)
605 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
608 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
609 if (desc
&& desc
->def_value_str
) {
610 parse_option_bool(name
, desc
->def_value_str
, &defval
, &error_abort
);
614 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_BOOL
);
615 return opt
->value
.boolean
;
618 uint64_t qemu_opt_get_number(QemuOpts
*opts
, const char *name
, uint64_t defval
)
620 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
623 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
624 if (desc
&& desc
->def_value_str
) {
625 parse_option_number(name
, desc
->def_value_str
, &defval
,
630 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_NUMBER
);
631 return opt
->value
.uint
;
634 uint64_t qemu_opt_get_size(QemuOpts
*opts
, const char *name
, uint64_t defval
)
636 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
639 const QemuOptDesc
*desc
= find_desc_by_name(opts
->list
->desc
, name
);
640 if (desc
&& desc
->def_value_str
) {
641 parse_option_size(name
, desc
->def_value_str
, &defval
, &error_abort
);
645 assert(opt
->desc
&& opt
->desc
->type
== QEMU_OPT_SIZE
);
646 return opt
->value
.uint
;
649 static void qemu_opt_parse(QemuOpt
*opt
, Error
**errp
)
651 if (opt
->desc
== NULL
)
654 switch (opt
->desc
->type
) {
655 case QEMU_OPT_STRING
:
659 parse_option_bool(opt
->name
, opt
->str
, &opt
->value
.boolean
, errp
);
661 case QEMU_OPT_NUMBER
:
662 parse_option_number(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
665 parse_option_size(opt
->name
, opt
->str
, &opt
->value
.uint
, errp
);
672 static bool opts_accepts_any(const QemuOpts
*opts
)
674 return opts
->list
->desc
[0].name
== NULL
;
677 int qemu_opt_unset(QemuOpts
*opts
, const char *name
)
679 QemuOpt
*opt
= qemu_opt_find(opts
, name
);
681 assert(opts_accepts_any(opts
));
691 static void opt_set(QemuOpts
*opts
, const char *name
, const char *value
,
692 bool prepend
, Error
**errp
)
695 const QemuOptDesc
*desc
;
696 Error
*local_err
= NULL
;
698 desc
= find_desc_by_name(opts
->list
->desc
, name
);
699 if (!desc
&& !opts_accepts_any(opts
)) {
700 error_set(errp
, QERR_INVALID_PARAMETER
, name
);
704 opt
= g_malloc0(sizeof(*opt
));
705 opt
->name
= g_strdup(name
);
708 QTAILQ_INSERT_HEAD(&opts
->head
, opt
, next
);
710 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
713 opt
->str
= g_strdup(value
);
714 qemu_opt_parse(opt
, &local_err
);
716 error_propagate(errp
, local_err
);
721 int qemu_opt_set(QemuOpts
*opts
, const char *name
, const char *value
)
723 Error
*local_err
= NULL
;
725 opt_set(opts
, name
, value
, false, &local_err
);
727 qerror_report_err(local_err
);
728 error_free(local_err
);
735 void qemu_opt_set_err(QemuOpts
*opts
, const char *name
, const char *value
,
738 opt_set(opts
, name
, value
, false, errp
);
741 int qemu_opt_set_bool(QemuOpts
*opts
, const char *name
, bool val
)
744 const QemuOptDesc
*desc
= opts
->list
->desc
;
746 opt
= g_malloc0(sizeof(*opt
));
747 opt
->desc
= find_desc_by_name(desc
, name
);
748 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
749 qerror_report(QERR_INVALID_PARAMETER
, name
);
754 opt
->name
= g_strdup(name
);
756 opt
->value
.boolean
= !!val
;
757 opt
->str
= g_strdup(val
? "on" : "off");
758 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
763 int qemu_opt_set_number(QemuOpts
*opts
, const char *name
, int64_t val
)
766 const QemuOptDesc
*desc
= opts
->list
->desc
;
768 opt
= g_malloc0(sizeof(*opt
));
769 opt
->desc
= find_desc_by_name(desc
, name
);
770 if (!opt
->desc
&& !opts_accepts_any(opts
)) {
771 qerror_report(QERR_INVALID_PARAMETER
, name
);
776 opt
->name
= g_strdup(name
);
778 opt
->value
.uint
= val
;
779 opt
->str
= g_strdup_printf("%" PRId64
, val
);
780 QTAILQ_INSERT_TAIL(&opts
->head
, opt
, next
);
785 int qemu_opt_foreach(QemuOpts
*opts
, qemu_opt_loopfunc func
, void *opaque
,
786 int abort_on_failure
)
791 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
792 rc
= func(opt
->name
, opt
->str
, opaque
);
793 if (abort_on_failure
&& rc
!= 0)
799 QemuOpts
*qemu_opts_find(QemuOptsList
*list
, const char *id
)
803 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
804 if (!opts
->id
&& !id
) {
807 if (opts
->id
&& id
&& !strcmp(opts
->id
, id
)) {
814 static int id_wellformed(const char *id
)
818 if (!qemu_isalpha(id
[0])) {
821 for (i
= 1; id
[i
]; i
++) {
822 if (!qemu_isalnum(id
[i
]) && !strchr("-._", id
[i
])) {
829 QemuOpts
*qemu_opts_create(QemuOptsList
*list
, const char *id
,
830 int fail_if_exists
, Error
**errp
)
832 QemuOpts
*opts
= NULL
;
835 if (!id_wellformed(id
)) {
836 error_set(errp
,QERR_INVALID_PARAMETER_VALUE
, "id", "an identifier");
837 #if 0 /* conversion from qerror_report() to error_set() broke this: */
838 error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
842 opts
= qemu_opts_find(list
, id
);
844 if (fail_if_exists
&& !list
->merge_lists
) {
845 error_setg(errp
, "Duplicate ID '%s' for %s", id
, list
->name
);
851 } else if (list
->merge_lists
) {
852 opts
= qemu_opts_find(list
, NULL
);
857 opts
= g_malloc0(sizeof(*opts
));
858 opts
->id
= g_strdup(id
);
860 loc_save(&opts
->loc
);
861 QTAILQ_INIT(&opts
->head
);
862 QTAILQ_INSERT_TAIL(&list
->head
, opts
, next
);
866 void qemu_opts_reset(QemuOptsList
*list
)
868 QemuOpts
*opts
, *next_opts
;
870 QTAILQ_FOREACH_SAFE(opts
, &list
->head
, next
, next_opts
) {
875 void qemu_opts_loc_restore(QemuOpts
*opts
)
877 loc_restore(&opts
->loc
);
880 int qemu_opts_set(QemuOptsList
*list
, const char *id
,
881 const char *name
, const char *value
)
884 Error
*local_err
= NULL
;
886 opts
= qemu_opts_create(list
, id
, 1, &local_err
);
888 qerror_report_err(local_err
);
889 error_free(local_err
);
892 return qemu_opt_set(opts
, name
, value
);
895 const char *qemu_opts_id(QemuOpts
*opts
)
900 /* The id string will be g_free()d by qemu_opts_del */
901 void qemu_opts_set_id(QemuOpts
*opts
, char *id
)
906 void qemu_opts_del(QemuOpts
*opts
)
911 opt
= QTAILQ_FIRST(&opts
->head
);
916 QTAILQ_REMOVE(&opts
->list
->head
, opts
, next
);
921 void qemu_opts_print(QemuOpts
*opts
)
924 QemuOptDesc
*desc
= opts
->list
->desc
;
926 if (desc
[0].name
== NULL
) {
927 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
928 printf("%s=\"%s\" ", opt
->name
, opt
->str
);
932 for (; desc
&& desc
->name
; desc
++) {
934 QemuOpt
*opt
= qemu_opt_find(opts
, desc
->name
);
936 value
= opt
? opt
->str
: desc
->def_value_str
;
940 if (desc
->type
== QEMU_OPT_STRING
) {
941 printf("%s='%s' ", desc
->name
, value
);
942 } else if ((desc
->type
== QEMU_OPT_SIZE
||
943 desc
->type
== QEMU_OPT_NUMBER
) && opt
) {
944 printf("%s=%" PRId64
" ", desc
->name
, opt
->value
.uint
);
946 printf("%s=%s ", desc
->name
, value
);
951 static int opts_do_parse(QemuOpts
*opts
, const char *params
,
952 const char *firstname
, bool prepend
)
954 char option
[128], value
[1024];
955 const char *p
,*pe
,*pc
;
956 Error
*local_err
= NULL
;
958 for (p
= params
; *p
!= '\0'; p
++) {
961 if (!pe
|| (pc
&& pc
< pe
)) {
962 /* found "foo,more" */
963 if (p
== params
&& firstname
) {
964 /* implicitly named first option */
965 pstrcpy(option
, sizeof(option
), firstname
);
966 p
= get_opt_value(value
, sizeof(value
), p
);
968 /* option without value, probably a flag */
969 p
= get_opt_name(option
, sizeof(option
), p
, ',');
970 if (strncmp(option
, "no", 2) == 0) {
971 memmove(option
, option
+2, strlen(option
+2)+1);
972 pstrcpy(value
, sizeof(value
), "off");
974 pstrcpy(value
, sizeof(value
), "on");
978 /* found "foo=bar,more" */
979 p
= get_opt_name(option
, sizeof(option
), p
, '=');
984 p
= get_opt_value(value
, sizeof(value
), p
);
986 if (strcmp(option
, "id") != 0) {
987 /* store and parse */
988 opt_set(opts
, option
, value
, prepend
, &local_err
);
990 qerror_report_err(local_err
);
991 error_free(local_err
);
1002 int qemu_opts_do_parse(QemuOpts
*opts
, const char *params
, const char *firstname
)
1004 return opts_do_parse(opts
, params
, firstname
, false);
1007 static QemuOpts
*opts_parse(QemuOptsList
*list
, const char *params
,
1008 int permit_abbrev
, bool defaults
)
1010 const char *firstname
;
1011 char value
[1024], *id
= NULL
;
1014 Error
*local_err
= NULL
;
1016 assert(!permit_abbrev
|| list
->implied_opt_name
);
1017 firstname
= permit_abbrev
? list
->implied_opt_name
: NULL
;
1019 if (strncmp(params
, "id=", 3) == 0) {
1020 get_opt_value(value
, sizeof(value
), params
+3);
1022 } else if ((p
= strstr(params
, ",id=")) != NULL
) {
1023 get_opt_value(value
, sizeof(value
), p
+4);
1028 * This code doesn't work for defaults && !list->merge_lists: when
1029 * params has no id=, and list has an element with !opts->id, it
1030 * appends a new element instead of returning the existing opts.
1031 * However, we got no use for this case. Guard against possible
1032 * (if unlikely) future misuse:
1034 assert(!defaults
|| list
->merge_lists
);
1035 opts
= qemu_opts_create(list
, id
, !defaults
, &local_err
);
1038 qerror_report_err(local_err
);
1039 error_free(local_err
);
1044 if (opts_do_parse(opts
, params
, firstname
, defaults
) != 0) {
1045 qemu_opts_del(opts
);
1052 QemuOpts
*qemu_opts_parse(QemuOptsList
*list
, const char *params
,
1055 return opts_parse(list
, params
, permit_abbrev
, false);
1058 void qemu_opts_set_defaults(QemuOptsList
*list
, const char *params
,
1063 opts
= opts_parse(list
, params
, permit_abbrev
, true);
1067 typedef struct OptsFromQDictState
{
1070 } OptsFromQDictState
;
1072 static void qemu_opts_from_qdict_1(const char *key
, QObject
*obj
, void *opaque
)
1074 OptsFromQDictState
*state
= opaque
;
1079 if (!strcmp(key
, "id") || *state
->errp
) {
1083 switch (qobject_type(obj
)) {
1085 value
= qstring_get_str(qobject_to_qstring(obj
));
1088 n
= snprintf(buf
, sizeof(buf
), "%" PRId64
,
1089 qint_get_int(qobject_to_qint(obj
)));
1090 assert(n
< sizeof(buf
));
1094 n
= snprintf(buf
, sizeof(buf
), "%.17g",
1095 qfloat_get_double(qobject_to_qfloat(obj
)));
1096 assert(n
< sizeof(buf
));
1100 pstrcpy(buf
, sizeof(buf
),
1101 qbool_get_int(qobject_to_qbool(obj
)) ? "on" : "off");
1108 qemu_opt_set_err(state
->opts
, key
, value
, state
->errp
);
1112 * Create QemuOpts from a QDict.
1113 * Use value of key "id" as ID if it exists and is a QString.
1114 * Only QStrings, QInts, QFloats and QBools are copied. Entries with
1115 * other types are silently ignored.
1117 QemuOpts
*qemu_opts_from_qdict(QemuOptsList
*list
, const QDict
*qdict
,
1120 OptsFromQDictState state
;
1121 Error
*local_err
= NULL
;
1124 opts
= qemu_opts_create(list
, qdict_get_try_str(qdict
, "id"), 1,
1127 error_propagate(errp
, local_err
);
1131 assert(opts
!= NULL
);
1133 state
.errp
= &local_err
;
1135 qdict_iter(qdict
, qemu_opts_from_qdict_1
, &state
);
1137 error_propagate(errp
, local_err
);
1138 qemu_opts_del(opts
);
1146 * Adds all QDict entries to the QemuOpts that can be added and removes them
1147 * from the QDict. When this function returns, the QDict contains only those
1148 * entries that couldn't be added to the QemuOpts.
1150 void qemu_opts_absorb_qdict(QemuOpts
*opts
, QDict
*qdict
, Error
**errp
)
1152 const QDictEntry
*entry
, *next
;
1154 entry
= qdict_first(qdict
);
1156 while (entry
!= NULL
) {
1157 Error
*local_err
= NULL
;
1158 OptsFromQDictState state
= {
1163 next
= qdict_next(qdict
, entry
);
1165 if (find_desc_by_name(opts
->list
->desc
, entry
->key
)) {
1166 qemu_opts_from_qdict_1(entry
->key
, entry
->value
, &state
);
1168 error_propagate(errp
, local_err
);
1171 qdict_del(qdict
, entry
->key
);
1180 * Convert from QemuOpts to QDict.
1181 * The QDict values are of type QString.
1182 * TODO We'll want to use types appropriate for opt->desc->type, but
1183 * this is enough for now.
1185 QDict
*qemu_opts_to_qdict(QemuOpts
*opts
, QDict
*qdict
)
1191 qdict
= qdict_new();
1194 qdict_put(qdict
, "id", qstring_from_str(opts
->id
));
1196 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1197 val
= QOBJECT(qstring_from_str(opt
->str
));
1198 qdict_put_obj(qdict
, opt
->name
, val
);
1203 /* Validate parsed opts against descriptions where no
1204 * descriptions were provided in the QemuOptsList.
1206 void qemu_opts_validate(QemuOpts
*opts
, const QemuOptDesc
*desc
, Error
**errp
)
1209 Error
*local_err
= NULL
;
1211 assert(opts_accepts_any(opts
));
1213 QTAILQ_FOREACH(opt
, &opts
->head
, next
) {
1214 opt
->desc
= find_desc_by_name(desc
, opt
->name
);
1216 error_set(errp
, QERR_INVALID_PARAMETER
, opt
->name
);
1220 qemu_opt_parse(opt
, &local_err
);
1222 error_propagate(errp
, local_err
);
1228 int qemu_opts_foreach(QemuOptsList
*list
, qemu_opts_loopfunc func
, void *opaque
,
1229 int abort_on_failure
)
1235 loc_push_none(&loc
);
1236 QTAILQ_FOREACH(opts
, &list
->head
, next
) {
1237 loc_restore(&opts
->loc
);
1238 rc
|= func(opts
, opaque
);
1239 if (abort_on_failure
&& rc
!= 0)