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
33 void *list
; /* Only needed for sanity checking the caller */
36 static StringInputVisitor
*to_siv(Visitor
*v
)
38 return container_of(v
, StringInputVisitor
, visitor
);
41 static void free_range(void *range
, void *dummy
)
46 static int parse_str(StringInputVisitor
*siv
, const char *name
, Error
**errp
)
48 char *str
= (char *) siv
->string
;
63 start
= strtoll(str
, &endptr
, 0);
64 if (errno
== 0 && endptr
> str
) {
65 if (*endptr
== '\0') {
66 cur
= g_malloc0(sizeof(*cur
));
67 range_set_bounds(cur
, start
, start
);
68 siv
->ranges
= range_list_insert(siv
->ranges
, cur
);
71 } else if (*endptr
== '-') {
74 end
= strtoll(str
, &endptr
, 0);
75 if (errno
== 0 && endptr
> str
&& start
<= end
&&
76 (start
> INT64_MAX
- 65536 ||
77 end
< start
+ 65536)) {
78 if (*endptr
== '\0') {
79 cur
= g_malloc0(sizeof(*cur
));
80 range_set_bounds(cur
, start
, end
);
81 siv
->ranges
= range_list_insert(siv
->ranges
, cur
);
84 } else if (*endptr
== ',') {
86 cur
= g_malloc0(sizeof(*cur
));
87 range_set_bounds(cur
, start
, end
);
88 siv
->ranges
= range_list_insert(siv
->ranges
, cur
);
96 } else if (*endptr
== ',') {
98 cur
= g_malloc0(sizeof(*cur
));
99 range_set_bounds(cur
, start
, start
);
100 siv
->ranges
= range_list_insert(siv
->ranges
, cur
);
112 g_list_foreach(siv
->ranges
, free_range
, NULL
);
113 g_list_free(siv
->ranges
);
115 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
116 "an int64 value or range");
121 start_list(Visitor
*v
, const char *name
, GenericList
**list
, size_t size
,
124 StringInputVisitor
*siv
= to_siv(v
);
126 /* We don't support visits without a list */
130 if (parse_str(siv
, name
, errp
) < 0) {
135 siv
->cur_range
= g_list_first(siv
->ranges
);
136 if (siv
->cur_range
) {
137 Range
*r
= siv
->cur_range
->data
;
139 siv
->cur
= range_lob(r
);
141 *list
= g_malloc0(size
);
147 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
149 StringInputVisitor
*siv
= to_siv(v
);
152 if (!siv
->ranges
|| !siv
->cur_range
) {
156 r
= siv
->cur_range
->data
;
161 if (!range_contains(r
, siv
->cur
)) {
162 siv
->cur_range
= g_list_next(siv
->cur_range
);
163 if (!siv
->cur_range
) {
166 r
= siv
->cur_range
->data
;
170 siv
->cur
= range_lob(r
);
173 tail
->next
= g_malloc0(size
);
177 static void check_list(Visitor
*v
, Error
**errp
)
179 const StringInputVisitor
*siv
= to_siv(v
);
183 if (!siv
->ranges
|| !siv
->cur_range
) {
187 r
= siv
->cur_range
->data
;
192 if (!range_contains(r
, siv
->cur
)) {
193 cur_range
= g_list_next(siv
->cur_range
);
203 error_setg(errp
, "Range contains too many values");
206 static void end_list(Visitor
*v
, void **obj
)
208 StringInputVisitor
*siv
= to_siv(v
);
210 assert(siv
->list
== obj
);
213 static void parse_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
216 StringInputVisitor
*siv
= to_siv(v
);
218 if (parse_str(siv
, name
, errp
) < 0) {
226 if (!siv
->cur_range
) {
229 siv
->cur_range
= g_list_first(siv
->ranges
);
230 if (!siv
->cur_range
) {
234 r
= siv
->cur_range
->data
;
239 siv
->cur
= range_lob(r
);
247 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
248 "an int64 value or range");
251 static void parse_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
254 /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
257 parse_type_int64(v
, name
, &i
, &err
);
259 error_propagate(errp
, err
);
265 static void parse_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
268 StringInputVisitor
*siv
= to_siv(v
);
272 parse_option_size(name
, siv
->string
, &val
, &err
);
274 error_propagate(errp
, err
);
281 static void parse_type_bool(Visitor
*v
, const char *name
, bool *obj
,
284 StringInputVisitor
*siv
= to_siv(v
);
286 if (!strcasecmp(siv
->string
, "on") ||
287 !strcasecmp(siv
->string
, "yes") ||
288 !strcasecmp(siv
->string
, "true")) {
292 if (!strcasecmp(siv
->string
, "off") ||
293 !strcasecmp(siv
->string
, "no") ||
294 !strcasecmp(siv
->string
, "false")) {
299 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
303 static void parse_type_str(Visitor
*v
, const char *name
, char **obj
,
306 StringInputVisitor
*siv
= to_siv(v
);
308 *obj
= g_strdup(siv
->string
);
311 static void parse_type_number(Visitor
*v
, const char *name
, double *obj
,
314 StringInputVisitor
*siv
= to_siv(v
);
315 char *endp
= (char *) siv
->string
;
319 val
= strtod(siv
->string
, &endp
);
320 if (errno
|| endp
== siv
->string
|| *endp
) {
321 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
329 static void string_input_free(Visitor
*v
)
331 StringInputVisitor
*siv
= to_siv(v
);
333 g_list_foreach(siv
->ranges
, free_range
, NULL
);
334 g_list_free(siv
->ranges
);
338 Visitor
*string_input_visitor_new(const char *str
)
340 StringInputVisitor
*v
;
343 v
= g_malloc0(sizeof(*v
));
345 v
->visitor
.type
= VISITOR_INPUT
;
346 v
->visitor
.type_int64
= parse_type_int64
;
347 v
->visitor
.type_uint64
= parse_type_uint64
;
348 v
->visitor
.type_size
= parse_type_size
;
349 v
->visitor
.type_bool
= parse_type_bool
;
350 v
->visitor
.type_str
= parse_type_str
;
351 v
->visitor
.type_number
= parse_type_number
;
352 v
->visitor
.start_list
= start_list
;
353 v
->visitor
.next_list
= next_list
;
354 v
->visitor
.check_list
= check_list
;
355 v
->visitor
.end_list
= end_list
;
356 v
->visitor
.free
= string_input_free
;