2 * String parsing visitor
4 * Copyright Red Hat, Inc. 2012
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-common.h"
14 #include "qapi/string-input-visitor.h"
15 #include "qapi/visitor-impl.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qemu/option.h"
18 #include "qemu/queue.h"
19 #include "qemu/range.h"
22 struct StringInputVisitor
35 static void parse_str(StringInputVisitor
*siv
, Error
**errp
)
37 char *str
= (char *) siv
->string
;
48 start
= strtoll(str
, &endptr
, 0);
49 if (errno
== 0 && endptr
> str
&& INT64_MIN
<= start
&&
51 if (*endptr
== '\0') {
52 cur
= g_malloc0(sizeof(*cur
));
55 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
, cur
,
59 } else if (*endptr
== '-') {
61 end
= strtoll(str
, &endptr
, 0);
62 if (errno
== 0 && endptr
> str
&&
63 INT64_MIN
<= end
&& end
<= INT64_MAX
&& start
<= end
&&
64 (start
> INT64_MAX
- 65536 ||
65 end
< start
+ 65536)) {
66 if (*endptr
== '\0') {
67 cur
= g_malloc0(sizeof(*cur
));
71 g_list_insert_sorted_merged(siv
->ranges
,
76 } else if (*endptr
== ',') {
78 cur
= g_malloc0(sizeof(*cur
));
82 g_list_insert_sorted_merged(siv
->ranges
,
92 } else if (*endptr
== ',') {
94 cur
= g_malloc0(sizeof(*cur
));
97 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
,
111 g_list_free_full(siv
->ranges
, g_free
);
112 assert(siv
->ranges
== NULL
);
116 start_list(Visitor
*v
, const char *name
, Error
**errp
)
118 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
120 parse_str(siv
, errp
);
122 siv
->cur_range
= g_list_first(siv
->ranges
);
123 if (siv
->cur_range
) {
124 Range
*r
= siv
->cur_range
->data
;
132 next_list(Visitor
*v
, GenericList
**list
, Error
**errp
)
134 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
138 if (!siv
->ranges
|| !siv
->cur_range
) {
142 r
= siv
->cur_range
->data
;
147 if (siv
->cur
< r
->begin
|| siv
->cur
>= r
->end
) {
148 siv
->cur_range
= g_list_next(siv
->cur_range
);
149 if (!siv
->cur_range
) {
152 r
= siv
->cur_range
->data
;
163 link
= &(*list
)->next
;
166 *link
= g_malloc0(sizeof **link
);
171 end_list(Visitor
*v
, Error
**errp
)
173 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
177 static void parse_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
180 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
183 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
188 parse_str(siv
, errp
);
194 if (!siv
->cur_range
) {
197 siv
->cur_range
= g_list_first(siv
->ranges
);
198 if (!siv
->cur_range
) {
202 r
= siv
->cur_range
->data
;
215 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
216 "an int64 value or range");
219 static void parse_type_size(Visitor
*v
, uint64_t *obj
, const char *name
,
222 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
227 parse_option_size(name
, siv
->string
, &val
, &err
);
229 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
234 error_propagate(errp
, err
);
241 static void parse_type_bool(Visitor
*v
, bool *obj
, const char *name
,
244 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
247 if (!strcasecmp(siv
->string
, "on") ||
248 !strcasecmp(siv
->string
, "yes") ||
249 !strcasecmp(siv
->string
, "true")) {
253 if (!strcasecmp(siv
->string
, "off") ||
254 !strcasecmp(siv
->string
, "no") ||
255 !strcasecmp(siv
->string
, "false")) {
261 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
265 static void parse_type_str(Visitor
*v
, char **obj
, const char *name
,
268 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
270 *obj
= g_strdup(siv
->string
);
272 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
277 static void parse_type_number(Visitor
*v
, double *obj
, const char *name
,
280 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
281 char *endp
= (char *) siv
->string
;
286 val
= strtod(siv
->string
, &endp
);
288 if (!siv
->string
|| errno
|| endp
== siv
->string
|| *endp
) {
289 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
297 static void parse_optional(Visitor
*v
, bool *present
, const char *name
,
300 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
310 Visitor
*string_input_get_visitor(StringInputVisitor
*v
)
315 void string_input_visitor_cleanup(StringInputVisitor
*v
)
317 g_list_free_full(v
->ranges
, g_free
);
321 StringInputVisitor
*string_input_visitor_new(const char *str
)
323 StringInputVisitor
*v
;
325 v
= g_malloc0(sizeof(*v
));
327 v
->visitor
.type_enum
= input_type_enum
;
328 v
->visitor
.type_int
= parse_type_int
;
329 v
->visitor
.type_size
= parse_type_size
;
330 v
->visitor
.type_bool
= parse_type_bool
;
331 v
->visitor
.type_str
= parse_type_str
;
332 v
->visitor
.type_number
= parse_type_number
;
333 v
->visitor
.start_list
= start_list
;
334 v
->visitor
.next_list
= next_list
;
335 v
->visitor
.end_list
= end_list
;
336 v
->visitor
.optional
= parse_optional
;