blob: b3fdd0827dd0f28e7efb12d23b52282cce5907f9 [file] [log] [blame]
Paolo Bonzinia020f982012-02-09 09:36:37 +01001/*
2 * String parsing visitor
3 *
Eric Blake08f95412016-01-29 06:48:59 -07004 * Copyright Red Hat, Inc. 2012-2016
Paolo Bonzinia020f982012-02-09 09:36:37 +01005 *
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
7 *
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.
10 *
11 */
12
Peter Maydellcbf21152016-01-29 17:49:57 +000013#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Paolo Bonzinia020f982012-02-09 09:36:37 +010015#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010016#include "qapi/string-input-visitor.h"
17#include "qapi/visitor-impl.h"
18#include "qapi/qmp/qerror.h"
Max Reitz84be6292017-11-14 19:01:23 +010019#include "qapi/qmp/qnull.h"
Paolo Bonzinia5829cc2014-02-08 11:01:44 +010020#include "qemu/option.h"
Hu Tao659268f2014-06-10 19:15:27 +080021#include "qemu/queue.h"
22#include "qemu/range.h"
23
Paolo Bonzinia020f982012-02-09 09:36:37 +010024
25struct StringInputVisitor
26{
27 Visitor visitor;
Hu Tao659268f2014-06-10 19:15:27 +080028
Hu Tao659268f2014-06-10 19:15:27 +080029 GList *ranges;
30 GList *cur_range;
31 int64_t cur;
32
Paolo Bonzinia020f982012-02-09 09:36:37 +010033 const char *string;
Eric Blake1158bb22016-06-09 10:48:34 -060034 void *list; /* Only needed for sanity checking the caller */
Paolo Bonzinia020f982012-02-09 09:36:37 +010035};
36
Eric Blaked7bea752016-01-29 06:48:38 -070037static StringInputVisitor *to_siv(Visitor *v)
38{
39 return container_of(v, StringInputVisitor, visitor);
40}
41
Michael S. Tsirkin0d156682014-06-16 18:07:03 +030042static void free_range(void *range, void *dummy)
43{
44 g_free(range);
45}
46
Eric Blake74f24cb2016-04-28 15:45:30 -060047static int parse_str(StringInputVisitor *siv, const char *name, Error **errp)
Hu Tao659268f2014-06-10 19:15:27 +080048{
49 char *str = (char *) siv->string;
50 long long start, end;
51 Range *cur;
52 char *endptr;
53
54 if (siv->ranges) {
Eric Blake74f24cb2016-04-28 15:45:30 -060055 return 0;
Hu Tao659268f2014-06-10 19:15:27 +080056 }
57
Markus Armbrusterd2788222017-03-20 17:13:43 +010058 if (!*str) {
59 return 0;
60 }
61
Hu Tao659268f2014-06-10 19:15:27 +080062 do {
Michael S. Tsirkinc210ee92014-06-18 17:50:07 +030063 errno = 0;
Hu Tao659268f2014-06-10 19:15:27 +080064 start = strtoll(str, &endptr, 0);
Michael S. Tsirkinc210ee92014-06-18 17:50:07 +030065 if (errno == 0 && endptr > str) {
Hu Tao659268f2014-06-10 19:15:27 +080066 if (*endptr == '\0') {
67 cur = g_malloc0(sizeof(*cur));
Markus Armbrustera0efbf12016-07-01 13:47:47 +020068 range_set_bounds(cur, start, start);
Eric Blake7c479592016-05-31 10:41:29 -060069 siv->ranges = range_list_insert(siv->ranges, cur);
Hu Tao659268f2014-06-10 19:15:27 +080070 cur = NULL;
71 str = NULL;
72 } else if (*endptr == '-') {
73 str = endptr + 1;
Michael S. Tsirkinc210ee92014-06-18 17:50:07 +030074 errno = 0;
Hu Tao659268f2014-06-10 19:15:27 +080075 end = strtoll(str, &endptr, 0);
Michael S. Tsirkinc210ee92014-06-18 17:50:07 +030076 if (errno == 0 && endptr > str && start <= end &&
Hu Tao659268f2014-06-10 19:15:27 +080077 (start > INT64_MAX - 65536 ||
78 end < start + 65536)) {
79 if (*endptr == '\0') {
80 cur = g_malloc0(sizeof(*cur));
Markus Armbrustera0efbf12016-07-01 13:47:47 +020081 range_set_bounds(cur, start, end);
Eric Blake7c479592016-05-31 10:41:29 -060082 siv->ranges = range_list_insert(siv->ranges, cur);
Hu Tao659268f2014-06-10 19:15:27 +080083 cur = NULL;
84 str = NULL;
85 } else if (*endptr == ',') {
86 str = endptr + 1;
87 cur = g_malloc0(sizeof(*cur));
Markus Armbrustera0efbf12016-07-01 13:47:47 +020088 range_set_bounds(cur, start, end);
Eric Blake7c479592016-05-31 10:41:29 -060089 siv->ranges = range_list_insert(siv->ranges, cur);
Hu Tao659268f2014-06-10 19:15:27 +080090 cur = NULL;
91 } else {
92 goto error;
93 }
94 } else {
95 goto error;
96 }
97 } else if (*endptr == ',') {
98 str = endptr + 1;
99 cur = g_malloc0(sizeof(*cur));
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200100 range_set_bounds(cur, start, start);
Eric Blake7c479592016-05-31 10:41:29 -0600101 siv->ranges = range_list_insert(siv->ranges, cur);
Hu Tao659268f2014-06-10 19:15:27 +0800102 cur = NULL;
103 } else {
104 goto error;
105 }
106 } else {
107 goto error;
108 }
109 } while (str);
110
Eric Blake74f24cb2016-04-28 15:45:30 -0600111 return 0;
Hu Tao659268f2014-06-10 19:15:27 +0800112error:
Michael S. Tsirkin0d156682014-06-16 18:07:03 +0300113 g_list_foreach(siv->ranges, free_range, NULL);
114 g_list_free(siv->ranges);
115 siv->ranges = NULL;
Eric Blake74f24cb2016-04-28 15:45:30 -0600116 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
117 "an int64 value or range");
118 return -1;
Hu Tao659268f2014-06-10 19:15:27 +0800119}
120
121static void
Eric Blaked9f62dd2016-04-28 15:45:31 -0600122start_list(Visitor *v, const char *name, GenericList **list, size_t size,
123 Error **errp)
Hu Tao659268f2014-06-10 19:15:27 +0800124{
Eric Blaked7bea752016-01-29 06:48:38 -0700125 StringInputVisitor *siv = to_siv(v);
Hu Tao659268f2014-06-10 19:15:27 +0800126
Eric Blaked9f62dd2016-04-28 15:45:31 -0600127 /* We don't support visits without a list */
128 assert(list);
Eric Blake1158bb22016-06-09 10:48:34 -0600129 siv->list = list;
Eric Blaked9f62dd2016-04-28 15:45:31 -0600130
Eric Blake74f24cb2016-04-28 15:45:30 -0600131 if (parse_str(siv, name, errp) < 0) {
Eric Blaked9f62dd2016-04-28 15:45:31 -0600132 *list = NULL;
Eric Blake74f24cb2016-04-28 15:45:30 -0600133 return;
134 }
Hu Tao659268f2014-06-10 19:15:27 +0800135
136 siv->cur_range = g_list_first(siv->ranges);
137 if (siv->cur_range) {
138 Range *r = siv->cur_range->data;
139 if (r) {
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200140 siv->cur = range_lob(r);
Hu Tao659268f2014-06-10 19:15:27 +0800141 }
Eric Blaked9f62dd2016-04-28 15:45:31 -0600142 *list = g_malloc0(size);
143 } else {
144 *list = NULL;
Hu Tao659268f2014-06-10 19:15:27 +0800145 }
146}
147
Eric Blaked9f62dd2016-04-28 15:45:31 -0600148static GenericList *next_list(Visitor *v, GenericList *tail, size_t size)
Hu Tao659268f2014-06-10 19:15:27 +0800149{
Eric Blaked7bea752016-01-29 06:48:38 -0700150 StringInputVisitor *siv = to_siv(v);
Hu Tao659268f2014-06-10 19:15:27 +0800151 Range *r;
152
153 if (!siv->ranges || !siv->cur_range) {
154 return NULL;
155 }
156
157 r = siv->cur_range->data;
158 if (!r) {
159 return NULL;
160 }
161
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200162 if (!range_contains(r, siv->cur)) {
Hu Tao659268f2014-06-10 19:15:27 +0800163 siv->cur_range = g_list_next(siv->cur_range);
164 if (!siv->cur_range) {
165 return NULL;
166 }
167 r = siv->cur_range->data;
168 if (!r) {
169 return NULL;
170 }
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200171 siv->cur = range_lob(r);
Hu Tao659268f2014-06-10 19:15:27 +0800172 }
173
Eric Blaked9f62dd2016-04-28 15:45:31 -0600174 tail->next = g_malloc0(size);
175 return tail->next;
Hu Tao659268f2014-06-10 19:15:27 +0800176}
177
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100178static void check_list(Visitor *v, Error **errp)
179{
180 const StringInputVisitor *siv = to_siv(v);
181 Range *r;
182 GList *cur_range;
183
184 if (!siv->ranges || !siv->cur_range) {
185 return;
186 }
187
188 r = siv->cur_range->data;
189 if (!r) {
190 return;
191 }
192
193 if (!range_contains(r, siv->cur)) {
194 cur_range = g_list_next(siv->cur_range);
195 if (!cur_range) {
196 return;
197 }
198 r = cur_range->data;
199 if (!r) {
200 return;
201 }
202 }
203
204 error_setg(errp, "Range contains too many values");
205}
206
Eric Blake1158bb22016-06-09 10:48:34 -0600207static void end_list(Visitor *v, void **obj)
Hu Tao659268f2014-06-10 19:15:27 +0800208{
Eric Blake1158bb22016-06-09 10:48:34 -0600209 StringInputVisitor *siv = to_siv(v);
210
211 assert(siv->list == obj);
Hu Tao659268f2014-06-10 19:15:27 +0800212}
213
Eric Blake0b2a0d62016-01-29 06:48:56 -0700214static void parse_type_int64(Visitor *v, const char *name, int64_t *obj,
Eric Blake4c403142016-01-29 06:48:49 -0700215 Error **errp)
Paolo Bonzinia020f982012-02-09 09:36:37 +0100216{
Eric Blaked7bea752016-01-29 06:48:38 -0700217 StringInputVisitor *siv = to_siv(v);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100218
Eric Blake74f24cb2016-04-28 15:45:30 -0600219 if (parse_str(siv, name, errp) < 0) {
220 return;
221 }
Hu Tao659268f2014-06-10 19:15:27 +0800222
223 if (!siv->ranges) {
224 goto error;
225 }
226
227 if (!siv->cur_range) {
228 Range *r;
229
230 siv->cur_range = g_list_first(siv->ranges);
231 if (!siv->cur_range) {
232 goto error;
233 }
234
235 r = siv->cur_range->data;
236 if (!r) {
237 goto error;
238 }
239
Markus Armbrustera0efbf12016-07-01 13:47:47 +0200240 siv->cur = range_lob(r);
Hu Tao659268f2014-06-10 19:15:27 +0800241 }
242
243 *obj = siv->cur;
244 siv->cur++;
245 return;
246
247error:
Eric Blake0a40bda2016-04-28 15:45:28 -0600248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100249 "an int64 value or range");
Paolo Bonzinia020f982012-02-09 09:36:37 +0100250}
251
Eric Blake0b2a0d62016-01-29 06:48:56 -0700252static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj,
Eric Blakef755dea2016-01-29 06:48:50 -0700253 Error **errp)
254{
255 /* FIXME: parse_type_int64 mishandles values over INT64_MAX */
256 int64_t i;
257 Error *err = NULL;
Eric Blake0b2a0d62016-01-29 06:48:56 -0700258 parse_type_int64(v, name, &i, &err);
Eric Blakef755dea2016-01-29 06:48:50 -0700259 if (err) {
260 error_propagate(errp, err);
261 } else {
262 *obj = i;
263 }
264}
265
Eric Blake0b2a0d62016-01-29 06:48:56 -0700266static void parse_type_size(Visitor *v, const char *name, uint64_t *obj,
Paolo Bonzinia5829cc2014-02-08 11:01:44 +0100267 Error **errp)
268{
Eric Blaked7bea752016-01-29 06:48:38 -0700269 StringInputVisitor *siv = to_siv(v);
Paolo Bonzinia5829cc2014-02-08 11:01:44 +0100270 Error *err = NULL;
271 uint64_t val;
272
Markus Armbrusterf332e832017-03-03 13:32:36 +0100273 parse_option_size(name, siv->string, &val, &err);
Paolo Bonzinia5829cc2014-02-08 11:01:44 +0100274 if (err) {
275 error_propagate(errp, err);
276 return;
277 }
278
279 *obj = val;
280}
281
Eric Blake0b2a0d62016-01-29 06:48:56 -0700282static void parse_type_bool(Visitor *v, const char *name, bool *obj,
Paolo Bonzinia020f982012-02-09 09:36:37 +0100283 Error **errp)
284{
Eric Blaked7bea752016-01-29 06:48:38 -0700285 StringInputVisitor *siv = to_siv(v);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100286
Markus Armbrusterf332e832017-03-03 13:32:36 +0100287 if (!strcasecmp(siv->string, "on") ||
288 !strcasecmp(siv->string, "yes") ||
289 !strcasecmp(siv->string, "true")) {
290 *obj = true;
291 return;
292 }
293 if (!strcasecmp(siv->string, "off") ||
294 !strcasecmp(siv->string, "no") ||
295 !strcasecmp(siv->string, "false")) {
296 *obj = false;
297 return;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100298 }
299
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100300 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
301 "boolean");
Paolo Bonzinia020f982012-02-09 09:36:37 +0100302}
303
Eric Blake0b2a0d62016-01-29 06:48:56 -0700304static void parse_type_str(Visitor *v, const char *name, char **obj,
Paolo Bonzinia020f982012-02-09 09:36:37 +0100305 Error **errp)
306{
Eric Blaked7bea752016-01-29 06:48:38 -0700307 StringInputVisitor *siv = to_siv(v);
Markus Armbrusterf332e832017-03-03 13:32:36 +0100308
309 *obj = g_strdup(siv->string);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100310}
311
Eric Blake0b2a0d62016-01-29 06:48:56 -0700312static void parse_type_number(Visitor *v, const char *name, double *obj,
Paolo Bonzinia020f982012-02-09 09:36:37 +0100313 Error **errp)
314{
Eric Blaked7bea752016-01-29 06:48:38 -0700315 StringInputVisitor *siv = to_siv(v);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100316 char *endp = (char *) siv->string;
317 double val;
318
319 errno = 0;
Markus Armbrusterf332e832017-03-03 13:32:36 +0100320 val = strtod(siv->string, &endp);
321 if (errno || endp == siv->string || *endp) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100322 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
323 "number");
Paolo Bonzinia020f982012-02-09 09:36:37 +0100324 return;
325 }
326
327 *obj = val;
328}
329
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200330static void parse_type_null(Visitor *v, const char *name, QNull **obj,
331 Error **errp)
Greg Kurza7333712016-12-16 16:26:09 +0100332{
333 StringInputVisitor *siv = to_siv(v);
334
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200335 *obj = NULL;
336
Greg Kurza7333712016-12-16 16:26:09 +0100337 if (!siv->string || siv->string[0]) {
338 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
339 "null");
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200340 return;
Greg Kurza7333712016-12-16 16:26:09 +0100341 }
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200342
343 *obj = qnull();
Greg Kurza7333712016-12-16 16:26:09 +0100344}
345
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600346static void string_input_free(Visitor *v)
347{
348 StringInputVisitor *siv = to_siv(v);
349
Eric Blake7a0525c2016-06-09 10:48:37 -0600350 g_list_foreach(siv->ranges, free_range, NULL);
351 g_list_free(siv->ranges);
352 g_free(siv);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600353}
354
Eric Blake7a0525c2016-06-09 10:48:37 -0600355Visitor *string_input_visitor_new(const char *str)
Paolo Bonzinia020f982012-02-09 09:36:37 +0100356{
357 StringInputVisitor *v;
358
Markus Armbrusterf332e832017-03-03 13:32:36 +0100359 assert(str);
Paolo Bonzinia020f982012-02-09 09:36:37 +0100360 v = g_malloc0(sizeof(*v));
361
Eric Blake983f52d2016-04-28 15:45:09 -0600362 v->visitor.type = VISITOR_INPUT;
Eric Blake4c403142016-01-29 06:48:49 -0700363 v->visitor.type_int64 = parse_type_int64;
Eric Blakef755dea2016-01-29 06:48:50 -0700364 v->visitor.type_uint64 = parse_type_uint64;
Paolo Bonzinia5829cc2014-02-08 11:01:44 +0100365 v->visitor.type_size = parse_type_size;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100366 v->visitor.type_bool = parse_type_bool;
367 v->visitor.type_str = parse_type_str;
368 v->visitor.type_number = parse_type_number;
Greg Kurza7333712016-12-16 16:26:09 +0100369 v->visitor.type_null = parse_type_null;
Hu Tao659268f2014-06-10 19:15:27 +0800370 v->visitor.start_list = start_list;
371 v->visitor.next_list = next_list;
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100372 v->visitor.check_list = check_list;
Hu Tao659268f2014-06-10 19:15:27 +0800373 v->visitor.end_list = end_list;
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600374 v->visitor.free = string_input_free;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100375
376 v->string = str;
Eric Blake7a0525c2016-06-09 10:48:37 -0600377 return &v->visitor;
Paolo Bonzinia020f982012-02-09 09:36:37 +0100378}