2 * String parsing visitor
4 * Copyright Red Hat, Inc. 2012-2016
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
9 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/visitor-impl.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qemu/option.h"
20 #include "qemu/queue.h"
21 #include "qemu/range.h"
24 struct StringInputVisitor
35 static StringInputVisitor
*to_siv(Visitor
*v
)
37 return container_of(v
, StringInputVisitor
, visitor
);
40 static void free_range(void *range
, void *dummy
)
45 static int parse_str(StringInputVisitor
*siv
, const char *name
, Error
**errp
)
47 char *str
= (char *) siv
->string
;
58 start
= strtoll(str
, &endptr
, 0);
59 if (errno
== 0 && endptr
> str
) {
60 if (*endptr
== '\0') {
61 cur
= g_malloc0(sizeof(*cur
));
64 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
, cur
,
68 } else if (*endptr
== '-') {
71 end
= strtoll(str
, &endptr
, 0);
72 if (errno
== 0 && endptr
> str
&& start
<= end
&&
73 (start
> INT64_MAX
- 65536 ||
74 end
< start
+ 65536)) {
75 if (*endptr
== '\0') {
76 cur
= g_malloc0(sizeof(*cur
));
80 g_list_insert_sorted_merged(siv
->ranges
,
85 } else if (*endptr
== ',') {
87 cur
= g_malloc0(sizeof(*cur
));
91 g_list_insert_sorted_merged(siv
->ranges
,
101 } else if (*endptr
== ',') {
103 cur
= g_malloc0(sizeof(*cur
));
105 cur
->end
= start
+ 1;
106 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
,
120 g_list_foreach(siv
->ranges
, free_range
, NULL
);
121 g_list_free(siv
->ranges
);
123 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
124 "an int64 value or range");
129 start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
132 StringInputVisitor
*siv
= to_siv(v
);
134 /* We don't support visits without a list */
137 if (parse_str(siv
, name
, errp
) < 0) {
142 siv
->cur_range
= g_list_first(siv
->ranges
);
143 if (siv
->cur_range
) {
144 Range
*r
= siv
->cur_range
->data
;
148 *list
= g_malloc0(size
);
154 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
156 StringInputVisitor
*siv
= to_siv(v
);
159 if (!siv
->ranges
|| !siv
->cur_range
) {
163 r
= siv
->cur_range
->data
;
168 if (siv
->cur
< r
->begin
|| siv
->cur
>= r
->end
) {
169 siv
->cur_range
= g_list_next(siv
->cur_range
);
170 if (!siv
->cur_range
) {
173 r
= siv
->cur_range
->data
;
180 tail
->next
= g_malloc0(size
);
184 static void end_list(Visitor
*v
)
188 static void parse_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
191 StringInputVisitor
*siv
= to_siv(v
);
194 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
199 if (parse_str(siv
, name
, errp
) < 0) {
207 if (!siv
->cur_range
) {
210 siv
->cur_range
= g_list_first(siv
->ranges
);
211 if (!siv
->cur_range
) {
215 r
= siv
->cur_range
->data
;
228 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
229 "an int64 value or range");
232 static void parse_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
235 /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
238 parse_type_int64(v
, name
, &i
, &err
);
240 error_propagate(errp
, err
);
246 static void parse_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
249 StringInputVisitor
*siv
= to_siv(v
);
254 parse_option_size(name
, siv
->string
, &val
, &err
);
256 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
261 error_propagate(errp
, err
);
268 static void parse_type_bool(Visitor
*v
, const char *name
, bool *obj
,
271 StringInputVisitor
*siv
= to_siv(v
);
274 if (!strcasecmp(siv
->string
, "on") ||
275 !strcasecmp(siv
->string
, "yes") ||
276 !strcasecmp(siv
->string
, "true")) {
280 if (!strcasecmp(siv
->string
, "off") ||
281 !strcasecmp(siv
->string
, "no") ||
282 !strcasecmp(siv
->string
, "false")) {
288 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
292 static void parse_type_str(Visitor
*v
, const char *name
, char **obj
,
295 StringInputVisitor
*siv
= to_siv(v
);
297 *obj
= g_strdup(siv
->string
);
300 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
305 static void parse_type_number(Visitor
*v
, const char *name
, double *obj
,
308 StringInputVisitor
*siv
= to_siv(v
);
309 char *endp
= (char *) siv
->string
;
314 val
= strtod(siv
->string
, &endp
);
316 if (!siv
->string
|| errno
|| endp
== siv
->string
|| *endp
) {
317 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
325 static void parse_optional(Visitor
*v
, const char *name
, bool *present
)
327 StringInputVisitor
*siv
= to_siv(v
);
337 Visitor
*string_input_get_visitor(StringInputVisitor
*v
)
342 void string_input_visitor_cleanup(StringInputVisitor
*v
)
344 g_list_foreach(v
->ranges
, free_range
, NULL
);
345 g_list_free(v
->ranges
);
349 StringInputVisitor
*string_input_visitor_new(const char *str
)
351 StringInputVisitor
*v
;
353 v
= g_malloc0(sizeof(*v
));
355 v
->visitor
.type
= VISITOR_INPUT
;
356 v
->visitor
.type_int64
= parse_type_int64
;
357 v
->visitor
.type_uint64
= parse_type_uint64
;
358 v
->visitor
.type_size
= parse_type_size
;
359 v
->visitor
.type_bool
= parse_type_bool
;
360 v
->visitor
.type_str
= parse_type_str
;
361 v
->visitor
.type_number
= parse_type_number
;
362 v
->visitor
.start_list
= start_list
;
363 v
->visitor
.next_list
= next_list
;
364 v
->visitor
.end_list
= end_list
;
365 v
->visitor
.optional
= parse_optional
;