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 "qemu-common.h"
15 #include "qapi/string-input-visitor.h"
16 #include "qapi/visitor-impl.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/option.h"
19 #include "qemu/queue.h"
20 #include "qemu/range.h"
23 struct StringInputVisitor
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 void parse_str(StringInputVisitor
*siv
, Error
**errp
)
48 char *str
= (char *) siv
->string
;
59 start
= strtoll(str
, &endptr
, 0);
60 if (errno
== 0 && endptr
> str
) {
61 if (*endptr
== '\0') {
62 cur
= g_malloc0(sizeof(*cur
));
65 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
, cur
,
69 } else if (*endptr
== '-') {
72 end
= strtoll(str
, &endptr
, 0);
73 if (errno
== 0 && endptr
> str
&& start
<= end
&&
74 (start
> INT64_MAX
- 65536 ||
75 end
< start
+ 65536)) {
76 if (*endptr
== '\0') {
77 cur
= g_malloc0(sizeof(*cur
));
81 g_list_insert_sorted_merged(siv
->ranges
,
86 } else if (*endptr
== ',') {
88 cur
= g_malloc0(sizeof(*cur
));
92 g_list_insert_sorted_merged(siv
->ranges
,
102 } else if (*endptr
== ',') {
104 cur
= g_malloc0(sizeof(*cur
));
106 cur
->end
= start
+ 1;
107 siv
->ranges
= g_list_insert_sorted_merged(siv
->ranges
,
121 g_list_foreach(siv
->ranges
, free_range
, NULL
);
122 g_list_free(siv
->ranges
);
127 start_list(Visitor
*v
, const char *name
, Error
**errp
)
129 StringInputVisitor
*siv
= to_siv(v
);
131 parse_str(siv
, errp
);
133 siv
->cur_range
= g_list_first(siv
->ranges
);
134 if (siv
->cur_range
) {
135 Range
*r
= siv
->cur_range
->data
;
142 static GenericList
*next_list(Visitor
*v
, GenericList
**list
, size_t size
)
144 StringInputVisitor
*siv
= to_siv(v
);
148 if (!siv
->ranges
|| !siv
->cur_range
) {
152 r
= siv
->cur_range
->data
;
157 if (siv
->cur
< r
->begin
|| siv
->cur
>= r
->end
) {
158 siv
->cur_range
= g_list_next(siv
->cur_range
);
159 if (!siv
->cur_range
) {
162 r
= siv
->cur_range
->data
;
173 link
= &(*list
)->next
;
176 *link
= g_malloc0(size
);
180 static void end_list(Visitor
*v
)
182 StringInputVisitor
*siv
= to_siv(v
);
186 static void parse_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
189 StringInputVisitor
*siv
= to_siv(v
);
192 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
197 parse_str(siv
, errp
);
203 if (!siv
->cur_range
) {
206 siv
->cur_range
= g_list_first(siv
->ranges
);
207 if (!siv
->cur_range
) {
211 r
= siv
->cur_range
->data
;
224 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
225 "an int64 value or range");
228 static void parse_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
231 /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
234 parse_type_int64(v
, name
, &i
, &err
);
236 error_propagate(errp
, err
);
242 static void parse_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
245 StringInputVisitor
*siv
= to_siv(v
);
250 parse_option_size(name
, siv
->string
, &val
, &err
);
252 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
257 error_propagate(errp
, err
);
264 static void parse_type_bool(Visitor
*v
, const char *name
, bool *obj
,
267 StringInputVisitor
*siv
= to_siv(v
);
270 if (!strcasecmp(siv
->string
, "on") ||
271 !strcasecmp(siv
->string
, "yes") ||
272 !strcasecmp(siv
->string
, "true")) {
276 if (!strcasecmp(siv
->string
, "off") ||
277 !strcasecmp(siv
->string
, "no") ||
278 !strcasecmp(siv
->string
, "false")) {
284 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
288 static void parse_type_str(Visitor
*v
, const char *name
, char **obj
,
291 StringInputVisitor
*siv
= to_siv(v
);
293 *obj
= g_strdup(siv
->string
);
295 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
300 static void parse_type_number(Visitor
*v
, const char *name
, double *obj
,
303 StringInputVisitor
*siv
= to_siv(v
);
304 char *endp
= (char *) siv
->string
;
309 val
= strtod(siv
->string
, &endp
);
311 if (!siv
->string
|| errno
|| endp
== siv
->string
|| *endp
) {
312 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
320 static void parse_optional(Visitor
*v
, const char *name
, bool *present
)
322 StringInputVisitor
*siv
= to_siv(v
);
332 Visitor
*string_input_get_visitor(StringInputVisitor
*v
)
337 void string_input_visitor_cleanup(StringInputVisitor
*v
)
339 g_list_foreach(v
->ranges
, free_range
, NULL
);
340 g_list_free(v
->ranges
);
344 StringInputVisitor
*string_input_visitor_new(const char *str
)
346 StringInputVisitor
*v
;
348 v
= g_malloc0(sizeof(*v
));
350 v
->visitor
.type_enum
= input_type_enum
;
351 v
->visitor
.type_int64
= parse_type_int64
;
352 v
->visitor
.type_uint64
= parse_type_uint64
;
353 v
->visitor
.type_size
= parse_type_size
;
354 v
->visitor
.type_bool
= parse_type_bool
;
355 v
->visitor
.type_str
= parse_type_str
;
356 v
->visitor
.type_number
= parse_type_number
;
357 v
->visitor
.start_list
= start_list
;
358 v
->visitor
.next_list
= next_list
;
359 v
->visitor
.end_list
= end_list
;
360 v
->visitor
.optional
= parse_optional
;