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 free_range(void *range
, void *dummy
)
40 static void parse_str(StringInputVisitor
*siv
, Error
**errp
)
42 char *str
= (char *) siv
->string
;
53 start
= strtoll(str
, &endptr
, 0);
54 if (errno
== 0 && endptr
> str
) {
55 if (*endptr
== '\0') {
56 cur
= g_malloc0(sizeof(*cur
));
59 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
, cur
,
63 } else if (*endptr
== '-') {
66 end
= strtoll(str
, &endptr
, 0);
67 if (errno
== 0 && endptr
> str
&& start
<= end
&&
68 (start
> INT64_MAX
- 65536 ||
69 end
< start
+ 65536)) {
70 if (*endptr
== '\0') {
71 cur
= g_malloc0(sizeof(*cur
));
75 g_list_insert_sorted_merged(siv
->ranges
,
80 } else if (*endptr
== ',') {
82 cur
= g_malloc0(sizeof(*cur
));
86 g_list_insert_sorted_merged(siv
->ranges
,
96 } else if (*endptr
== ',') {
98 cur
= g_malloc0(sizeof(*cur
));
100 cur
->end
= start
+ 1;
101 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
,
115 g_list_foreach(siv
->ranges
, free_range
, NULL
);
116 g_list_free(siv
->ranges
);
121 start_list(Visitor
*v
, const char *name
, Error
**errp
)
123 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
125 parse_str(siv
, errp
);
127 siv
->cur_range
= g_list_first(siv
->ranges
);
128 if (siv
->cur_range
) {
129 Range
*r
= siv
->cur_range
->data
;
137 next_list(Visitor
*v
, GenericList
**list
, Error
**errp
)
139 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
143 if (!siv
->ranges
|| !siv
->cur_range
) {
147 r
= siv
->cur_range
->data
;
152 if (siv
->cur
< r
->begin
|| siv
->cur
>= r
->end
) {
153 siv
->cur_range
= g_list_next(siv
->cur_range
);
154 if (!siv
->cur_range
) {
157 r
= siv
->cur_range
->data
;
168 link
= &(*list
)->next
;
171 *link
= g_malloc0(sizeof **link
);
176 end_list(Visitor
*v
, Error
**errp
)
178 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
182 static void parse_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
185 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
188 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
193 parse_str(siv
, errp
);
199 if (!siv
->cur_range
) {
202 siv
->cur_range
= g_list_first(siv
->ranges
);
203 if (!siv
->cur_range
) {
207 r
= siv
->cur_range
->data
;
220 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
221 "an int64 value or range");
224 static void parse_type_size(Visitor
*v
, uint64_t *obj
, const char *name
,
227 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
232 parse_option_size(name
, siv
->string
, &val
, &err
);
234 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
239 error_propagate(errp
, err
);
246 static void parse_type_bool(Visitor
*v
, bool *obj
, const char *name
,
249 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
252 if (!strcasecmp(siv
->string
, "on") ||
253 !strcasecmp(siv
->string
, "yes") ||
254 !strcasecmp(siv
->string
, "true")) {
258 if (!strcasecmp(siv
->string
, "off") ||
259 !strcasecmp(siv
->string
, "no") ||
260 !strcasecmp(siv
->string
, "false")) {
266 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
270 static void parse_type_str(Visitor
*v
, char **obj
, const char *name
,
273 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
275 *obj
= g_strdup(siv
->string
);
277 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
282 static void parse_type_number(Visitor
*v
, double *obj
, const char *name
,
285 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
286 char *endp
= (char *) siv
->string
;
291 val
= strtod(siv
->string
, &endp
);
293 if (!siv
->string
|| errno
|| endp
== siv
->string
|| *endp
) {
294 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
302 static void parse_optional(Visitor
*v
, bool *present
, const char *name
,
305 StringInputVisitor
*siv
= DO_UPCAST(StringInputVisitor
, visitor
, v
);
315 Visitor
*string_input_get_visitor(StringInputVisitor
*v
)
320 void string_input_visitor_cleanup(StringInputVisitor
*v
)
322 g_list_foreach(v
->ranges
, free_range
, NULL
);
323 g_list_free(v
->ranges
);
327 StringInputVisitor
*string_input_visitor_new(const char *str
)
329 StringInputVisitor
*v
;
331 v
= g_malloc0(sizeof(*v
));
333 v
->visitor
.type_enum
= input_type_enum
;
334 v
->visitor
.type_int
= parse_type_int
;
335 v
->visitor
.type_size
= parse_type_size
;
336 v
->visitor
.type_bool
= parse_type_bool
;
337 v
->visitor
.type_str
= parse_type_str
;
338 v
->visitor
.type_number
= parse_type_number
;
339 v
->visitor
.start_list
= start_list
;
340 v
->visitor
.next_list
= next_list
;
341 v
->visitor
.end_list
= end_list
;
342 v
->visitor
.optional
= parse_optional
;