slirp: add a fake NC-SI backend
[qemu/kevin.git] / qapi / string-input-visitor.c
blobc089491c2408989e91ef6ac06953c4a5e50656c2
1 /*
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
26 Visitor visitor;
28 GList *ranges;
29 GList *cur_range;
30 int64_t cur;
32 const char *string;
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)
43 g_free(range);
46 static int parse_str(StringInputVisitor *siv, const char *name, Error **errp)
48 char *str = (char *) siv->string;
49 long long start, end;
50 Range *cur;
51 char *endptr;
53 if (siv->ranges) {
54 return 0;
57 if (!*str) {
58 return 0;
61 do {
62 errno = 0;
63 start = strtoll(str, &endptr, 0);
64 if (errno == 0 && endptr > str) {
65 if (*endptr == '\0') {
66 cur = g_malloc0(sizeof(*cur));
67 range_set_bounds(cur, start, start);
68 siv->ranges = range_list_insert(siv->ranges, cur);
69 cur = NULL;
70 str = NULL;
71 } else if (*endptr == '-') {
72 str = endptr + 1;
73 errno = 0;
74 end = strtoll(str, &endptr, 0);
75 if (errno == 0 && endptr > str && start <= end &&
76 (start > INT64_MAX - 65536 ||
77 end < start + 65536)) {
78 if (*endptr == '\0') {
79 cur = g_malloc0(sizeof(*cur));
80 range_set_bounds(cur, start, end);
81 siv->ranges = range_list_insert(siv->ranges, cur);
82 cur = NULL;
83 str = NULL;
84 } else if (*endptr == ',') {
85 str = endptr + 1;
86 cur = g_malloc0(sizeof(*cur));
87 range_set_bounds(cur, start, end);
88 siv->ranges = range_list_insert(siv->ranges, cur);
89 cur = NULL;
90 } else {
91 goto error;
93 } else {
94 goto error;
96 } else if (*endptr == ',') {
97 str = endptr + 1;
98 cur = g_malloc0(sizeof(*cur));
99 range_set_bounds(cur, start, start);
100 siv->ranges = range_list_insert(siv->ranges, cur);
101 cur = NULL;
102 } else {
103 goto error;
105 } else {
106 goto error;
108 } while (str);
110 return 0;
111 error:
112 g_list_foreach(siv->ranges, free_range, NULL);
113 g_list_free(siv->ranges);
114 siv->ranges = NULL;
115 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
116 "an int64 value or range");
117 return -1;
120 static void
121 start_list(Visitor *v, const char *name, GenericList **list, size_t size,
122 Error **errp)
124 StringInputVisitor *siv = to_siv(v);
126 /* We don't support visits without a list */
127 assert(list);
128 siv->list = list;
130 if (parse_str(siv, name, errp) < 0) {
131 *list = NULL;
132 return;
135 siv->cur_range = g_list_first(siv->ranges);
136 if (siv->cur_range) {
137 Range *r = siv->cur_range->data;
138 if (r) {
139 siv->cur = range_lob(r);
141 *list = g_malloc0(size);
142 } else {
143 *list = NULL;
147 static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
149 StringInputVisitor *siv = to_siv(v);
150 Range *r;
152 if (!siv->ranges || !siv->cur_range) {
153 return NULL;
156 r = siv->cur_range->data;
157 if (!r) {
158 return NULL;
161 if (!range_contains(r, siv->cur)) {
162 siv->cur_range = g_list_next(siv->cur_range);
163 if (!siv->cur_range) {
164 return NULL;
166 r = siv->cur_range->data;
167 if (!r) {
168 return NULL;
170 siv->cur = range_lob(r);
173 tail->next = g_malloc0(size);
174 return tail->next;
177 static void check_list(Visitor *v, Error **errp)
179 const StringInputVisitor *siv = to_siv(v);
180 Range *r;
181 GList *cur_range;
183 if (!siv->ranges || !siv->cur_range) {
184 return;
187 r = siv->cur_range->data;
188 if (!r) {
189 return;
192 if (!range_contains(r, siv->cur)) {
193 cur_range = g_list_next(siv->cur_range);
194 if (!cur_range) {
195 return;
197 r = cur_range->data;
198 if (!r) {
199 return;
203 error_setg(errp, "Range contains too many values");
206 static void end_list(Visitor *v, void **obj)
208 StringInputVisitor *siv = to_siv(v);
210 assert(siv->list == obj);
213 static void parse_type_int64(Visitor *v, const char *name, int64_t *obj,
214 Error **errp)
216 StringInputVisitor *siv = to_siv(v);
218 if (parse_str(siv, name, errp) < 0) {
219 return;
222 if (!siv->ranges) {
223 goto error;
226 if (!siv->cur_range) {
227 Range *r;
229 siv->cur_range = g_list_first(siv->ranges);
230 if (!siv->cur_range) {
231 goto error;
234 r = siv->cur_range->data;
235 if (!r) {
236 goto error;
239 siv->cur = range_lob(r);
242 *obj = siv->cur;
243 siv->cur++;
244 return;
246 error:
247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
248 "an int64 value or range");
251 static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj,
252 Error **errp)
254 /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
255 int64_t i;
256 Error *err = NULL;
257 parse_type_int64(v, name, &i, &err);
258 if (err) {
259 error_propagate(errp, err);
260 } else {
261 *obj = i;
265 static void parse_type_size(Visitor *v, const char *name, uint64_t *obj,
266 Error **errp)
268 StringInputVisitor *siv = to_siv(v);
269 Error *err = NULL;
270 uint64_t val;
272 parse_option_size(name, siv->string, &val, &err);
273 if (err) {
274 error_propagate(errp, err);
275 return;
278 *obj = val;
281 static void parse_type_bool(Visitor *v, const char *name, bool *obj,
282 Error **errp)
284 StringInputVisitor *siv = to_siv(v);
286 if (!strcasecmp(siv->string, "on") ||
287 !strcasecmp(siv->string, "yes") ||
288 !strcasecmp(siv->string, "true")) {
289 *obj = true;
290 return;
292 if (!strcasecmp(siv->string, "off") ||
293 !strcasecmp(siv->string, "no") ||
294 !strcasecmp(siv->string, "false")) {
295 *obj = false;
296 return;
299 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
300 "boolean");
303 static void parse_type_str(Visitor *v, const char *name, char **obj,
304 Error **errp)
306 StringInputVisitor *siv = to_siv(v);
308 *obj = g_strdup(siv->string);
311 static void parse_type_number(Visitor *v, const char *name, double *obj,
312 Error **errp)
314 StringInputVisitor *siv = to_siv(v);
315 char *endp = (char *) siv->string;
316 double val;
318 errno = 0;
319 val = strtod(siv->string, &endp);
320 if (errno || endp == siv->string || *endp) {
321 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
322 "number");
323 return;
326 *obj = val;
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);
335 g_free(siv);
338 Visitor *string_input_visitor_new(const char *str)
340 StringInputVisitor *v;
342 assert(str);
343 v = g_malloc0(sizeof(*v));
345 v->visitor.type = VISITOR_INPUT;
346 v->visitor.type_int64 = parse_type_int64;
347 v->visitor.type_uint64 = parse_type_uint64;
348 v->visitor.type_size = parse_type_size;
349 v->visitor.type_bool = parse_type_bool;
350 v->visitor.type_str = parse_type_str;
351 v->visitor.type_number = parse_type_number;
352 v->visitor.start_list = start_list;
353 v->visitor.next_list = next_list;
354 v->visitor.check_list = check_list;
355 v->visitor.end_list = end_list;
356 v->visitor.free = string_input_free;
358 v->string = str;
359 return &v->visitor;