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;
59 start = strtoll(str, &endptr, 0);
60 if (errno == 0 && endptr > str) {
61 if (*endptr == '\0') {
62 cur = g_malloc0(sizeof(*cur));
63 range_set_bounds(cur, start, start);
64 siv->ranges = range_list_insert(siv->ranges, cur);
67 } else if (*endptr == '-') {
70 end = strtoll(str, &endptr, 0);
71 if (errno == 0 && endptr > str && start <= end &&
72 (start > INT64_MAX - 65536 ||
73 end < start + 65536)) {
74 if (*endptr == '\0') {
75 cur = g_malloc0(sizeof(*cur));
76 range_set_bounds(cur, start, end);
77 siv->ranges = range_list_insert(siv->ranges, cur);
80 } else if (*endptr == ',') {
82 cur = g_malloc0(sizeof(*cur));
83 range_set_bounds(cur, start, end);
84 siv->ranges = range_list_insert(siv->ranges, cur);
92 } else if (*endptr == ',') {
94 cur = g_malloc0(sizeof(*cur));
95 range_set_bounds(cur, start, start);
96 siv->ranges = range_list_insert(siv->ranges, cur);
108 g_list_foreach(siv->ranges, free_range, NULL);
109 g_list_free(siv->ranges);
111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
112 "an int64 value or range");
117 start_list(Visitor *v, const char *name, GenericList **list, size_t size,
120 StringInputVisitor *siv = to_siv(v);
122 /* We don't support visits without a list */
126 if (parse_str(siv, name, errp) < 0) {
131 siv->cur_range = g_list_first(siv->ranges);
132 if (siv->cur_range) {
133 Range *r = siv->cur_range->data;
135 siv->cur = range_lob(r);
137 *list = g_malloc0(size);
143 static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
145 StringInputVisitor *siv = to_siv(v);
148 if (!siv->ranges || !siv->cur_range) {
152 r = siv->cur_range->data;
157 if (!range_contains(r, siv->cur)) {
158 siv->cur_range = g_list_next(siv->cur_range);
159 if (!siv->cur_range) {
162 r = siv->cur_range->data;
166 siv->cur = range_lob(r);
169 tail->next = g_malloc0(size);
173 static void end_list(Visitor *v, void **obj)
175 StringInputVisitor *siv = to_siv(v);
177 assert(siv->list == obj);
180 static void parse_type_int64(Visitor *v, const char *name, int64_t *obj,
183 StringInputVisitor *siv = to_siv(v);
186 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
191 if (parse_str(siv, name, errp) < 0) {
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;
212 siv->cur = range_lob(r);
220 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
221 "an int64 value or range");
224 static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj,
227 /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
230 parse_type_int64(v, name, &i, &err);
232 error_propagate(errp, err);
238 static void parse_type_size(Visitor *v, const char *name, uint64_t *obj,
241 StringInputVisitor *siv = to_siv(v);
246 parse_option_size(name, siv->string, &val, &err);
248 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
253 error_propagate(errp, err);
260 static void parse_type_bool(Visitor *v, const char *name, bool *obj,
263 StringInputVisitor *siv = to_siv(v);
266 if (!strcasecmp(siv->string, "on") ||
267 !strcasecmp(siv->string, "yes") ||
268 !strcasecmp(siv->string, "true")) {
272 if (!strcasecmp(siv->string, "off") ||
273 !strcasecmp(siv->string, "no") ||
274 !strcasecmp(siv->string, "false")) {
280 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
284 static void parse_type_str(Visitor *v, const char *name, char **obj,
287 StringInputVisitor *siv = to_siv(v);
289 *obj = g_strdup(siv->string);
292 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
297 static void parse_type_number(Visitor *v, const char *name, double *obj,
300 StringInputVisitor *siv = to_siv(v);
301 char *endp = (char *) siv->string;
306 val = strtod(siv->string, &endp);
308 if (!siv->string || errno || endp == siv->string || *endp) {
309 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
317 static void parse_optional(Visitor *v, const char *name, bool *present)
319 StringInputVisitor *siv = to_siv(v);
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;
342 v = g_malloc0(sizeof(*v));
344 v->visitor.type = VISITOR_INPUT;
345 v->visitor.type_int64 = parse_type_int64;
346 v->visitor.type_uint64 = parse_type_uint64;
347 v->visitor.type_size = parse_type_size;
348 v->visitor.type_bool = parse_type_bool;
349 v->visitor.type_str = parse_type_str;
350 v->visitor.type_number = parse_type_number;
351 v->visitor.start_list = start_list;
352 v->visitor.next_list = next_list;
353 v->visitor.end_list = end_list;
354 v->visitor.optional = parse_optional;
355 v->visitor.free = string_input_free;