qapi: make string input visitor parse int list
[qemu/ar7.git] / qapi / string-input-visitor.c
blob0b2490b3dabc2d2bf4aa55bf905992225f96d768
1 /*
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
24 Visitor visitor;
26 bool head;
28 GList *ranges;
29 GList *cur_range;
30 int64_t cur;
32 const char *string;
35 static void parse_str(StringInputVisitor *siv, Error **errp)
37 char *str = (char *) siv->string;
38 long long start, end;
39 Range *cur;
40 char *endptr;
42 if (siv->ranges) {
43 return;
46 errno = 0;
47 do {
48 start = strtoll(str, &endptr, 0);
49 if (errno == 0 && endptr > str && INT64_MIN <= start &&
50 start <= INT64_MAX) {
51 if (*endptr == '\0') {
52 cur = g_malloc0(sizeof(*cur));
53 cur->begin = start;
54 cur->end = start + 1;
55 siv->ranges = g_list_insert_sorted_merged(siv->ranges, cur,
56 range_compare);
57 cur = NULL;
58 str = NULL;
59 } else if (*endptr == '-') {
60 str = endptr + 1;
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));
68 cur->begin = start;
69 cur->end = end + 1;
70 siv->ranges =
71 g_list_insert_sorted_merged(siv->ranges,
72 cur,
73 range_compare);
74 cur = NULL;
75 str = NULL;
76 } else if (*endptr == ',') {
77 str = endptr + 1;
78 cur = g_malloc0(sizeof(*cur));
79 cur->begin = start;
80 cur->end = end + 1;
81 siv->ranges =
82 g_list_insert_sorted_merged(siv->ranges,
83 cur,
84 range_compare);
85 cur = NULL;
86 } else {
87 goto error;
89 } else {
90 goto error;
92 } else if (*endptr == ',') {
93 str = endptr + 1;
94 cur = g_malloc0(sizeof(*cur));
95 cur->begin = start;
96 cur->end = start + 1;
97 siv->ranges = g_list_insert_sorted_merged(siv->ranges,
98 cur,
99 range_compare);
100 cur = NULL;
101 } else {
102 goto error;
104 } else {
105 goto error;
107 } while (str);
109 return;
110 error:
111 g_list_free_full(siv->ranges, g_free);
112 assert(siv->ranges == NULL);
115 static void
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;
125 if (r) {
126 siv->cur = r->begin;
131 static GenericList *
132 next_list(Visitor *v, GenericList **list, Error **errp)
134 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
135 GenericList **link;
136 Range *r;
138 if (!siv->ranges || !siv->cur_range) {
139 return NULL;
142 r = siv->cur_range->data;
143 if (!r) {
144 return NULL;
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) {
150 return NULL;
152 r = siv->cur_range->data;
153 if (!r) {
154 return NULL;
156 siv->cur = r->begin;
159 if (siv->head) {
160 link = list;
161 siv->head = false;
162 } else {
163 link = &(*list)->next;
166 *link = g_malloc0(sizeof **link);
167 return *link;
170 static void
171 end_list(Visitor *v, Error **errp)
173 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
174 siv->head = true;
177 static void parse_type_int(Visitor *v, int64_t *obj, const char *name,
178 Error **errp)
180 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
182 if (!siv->string) {
183 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
184 "integer");
185 return;
188 parse_str(siv, errp);
190 if (!siv->ranges) {
191 goto error;
194 if (!siv->cur_range) {
195 Range *r;
197 siv->cur_range = g_list_first(siv->ranges);
198 if (!siv->cur_range) {
199 goto error;
202 r = siv->cur_range->data;
203 if (!r) {
204 goto error;
207 siv->cur = r->begin;
210 *obj = siv->cur;
211 siv->cur++;
212 return;
214 error:
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,
220 Error **errp)
222 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
223 Error *err = NULL;
224 uint64_t val;
226 if (siv->string) {
227 parse_option_size(name, siv->string, &val, &err);
228 } else {
229 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
230 "size");
231 return;
233 if (err) {
234 error_propagate(errp, err);
235 return;
238 *obj = val;
241 static void parse_type_bool(Visitor *v, bool *obj, const char *name,
242 Error **errp)
244 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
246 if (siv->string) {
247 if (!strcasecmp(siv->string, "on") ||
248 !strcasecmp(siv->string, "yes") ||
249 !strcasecmp(siv->string, "true")) {
250 *obj = true;
251 return;
253 if (!strcasecmp(siv->string, "off") ||
254 !strcasecmp(siv->string, "no") ||
255 !strcasecmp(siv->string, "false")) {
256 *obj = false;
257 return;
261 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
262 "boolean");
265 static void parse_type_str(Visitor *v, char **obj, const char *name,
266 Error **errp)
268 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
269 if (siv->string) {
270 *obj = g_strdup(siv->string);
271 } else {
272 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
273 "string");
277 static void parse_type_number(Visitor *v, double *obj, const char *name,
278 Error **errp)
280 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
281 char *endp = (char *) siv->string;
282 double val;
284 errno = 0;
285 if (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",
290 "number");
291 return;
294 *obj = val;
297 static void parse_optional(Visitor *v, bool *present, const char *name,
298 Error **errp)
300 StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
302 if (!siv->string) {
303 *present = false;
304 return;
307 *present = true;
310 Visitor *string_input_get_visitor(StringInputVisitor *v)
312 return &v->visitor;
315 void string_input_visitor_cleanup(StringInputVisitor *v)
317 g_list_free_full(v->ranges, g_free);
318 g_free(v);
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;
338 v->string = str;
339 v->head = true;
340 return v;