Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * String parsing visitor |
| 3 | * |
Eric Blake | 08f9541 | 2016-01-29 06:48:59 -0700 | [diff] [blame] | 4 | * Copyright Red Hat, Inc. 2012-2016 |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 5 | * |
| 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 Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 14 | #include "qapi/error.h" |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 15 | #include "qemu-common.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 16 | #include "qapi/string-input-visitor.h" |
| 17 | #include "qapi/visitor-impl.h" |
| 18 | #include "qapi/qmp/qerror.h" |
Max Reitz | 84be629 | 2017-11-14 19:01:23 +0100 | [diff] [blame] | 19 | #include "qapi/qmp/qnull.h" |
Paolo Bonzini | a5829cc | 2014-02-08 11:01:44 +0100 | [diff] [blame] | 20 | #include "qemu/option.h" |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 21 | #include "qemu/queue.h" |
| 22 | #include "qemu/range.h" |
| 23 | |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 24 | |
| 25 | struct StringInputVisitor |
| 26 | { |
| 27 | Visitor visitor; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 28 | |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 29 | GList *ranges; |
| 30 | GList *cur_range; |
| 31 | int64_t cur; |
| 32 | |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 33 | const char *string; |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 34 | void *list; /* Only needed for sanity checking the caller */ |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 37 | static StringInputVisitor *to_siv(Visitor *v) |
| 38 | { |
| 39 | return container_of(v, StringInputVisitor, visitor); |
| 40 | } |
| 41 | |
Michael S. Tsirkin | 0d15668 | 2014-06-16 18:07:03 +0300 | [diff] [blame] | 42 | static void free_range(void *range, void *dummy) |
| 43 | { |
| 44 | g_free(range); |
| 45 | } |
| 46 | |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 47 | static int parse_str(StringInputVisitor *siv, const char *name, Error **errp) |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 48 | { |
| 49 | char *str = (char *) siv->string; |
| 50 | long long start, end; |
| 51 | Range *cur; |
| 52 | char *endptr; |
| 53 | |
| 54 | if (siv->ranges) { |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 55 | return 0; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 56 | } |
| 57 | |
Markus Armbruster | d278822 | 2017-03-20 17:13:43 +0100 | [diff] [blame] | 58 | if (!*str) { |
| 59 | return 0; |
| 60 | } |
| 61 | |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 62 | do { |
Michael S. Tsirkin | c210ee9 | 2014-06-18 17:50:07 +0300 | [diff] [blame] | 63 | errno = 0; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 64 | start = strtoll(str, &endptr, 0); |
Michael S. Tsirkin | c210ee9 | 2014-06-18 17:50:07 +0300 | [diff] [blame] | 65 | if (errno == 0 && endptr > str) { |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 66 | if (*endptr == '\0') { |
| 67 | cur = g_malloc0(sizeof(*cur)); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 68 | range_set_bounds(cur, start, start); |
Eric Blake | 7c47959 | 2016-05-31 10:41:29 -0600 | [diff] [blame] | 69 | siv->ranges = range_list_insert(siv->ranges, cur); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 70 | cur = NULL; |
| 71 | str = NULL; |
| 72 | } else if (*endptr == '-') { |
| 73 | str = endptr + 1; |
Michael S. Tsirkin | c210ee9 | 2014-06-18 17:50:07 +0300 | [diff] [blame] | 74 | errno = 0; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 75 | end = strtoll(str, &endptr, 0); |
Michael S. Tsirkin | c210ee9 | 2014-06-18 17:50:07 +0300 | [diff] [blame] | 76 | if (errno == 0 && endptr > str && start <= end && |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 77 | (start > INT64_MAX - 65536 || |
| 78 | end < start + 65536)) { |
| 79 | if (*endptr == '\0') { |
| 80 | cur = g_malloc0(sizeof(*cur)); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 81 | range_set_bounds(cur, start, end); |
Eric Blake | 7c47959 | 2016-05-31 10:41:29 -0600 | [diff] [blame] | 82 | siv->ranges = range_list_insert(siv->ranges, cur); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 83 | cur = NULL; |
| 84 | str = NULL; |
| 85 | } else if (*endptr == ',') { |
| 86 | str = endptr + 1; |
| 87 | cur = g_malloc0(sizeof(*cur)); |
Markus Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 88 | range_set_bounds(cur, start, end); |
Eric Blake | 7c47959 | 2016-05-31 10:41:29 -0600 | [diff] [blame] | 89 | siv->ranges = range_list_insert(siv->ranges, cur); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 90 | 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 Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 100 | range_set_bounds(cur, start, start); |
Eric Blake | 7c47959 | 2016-05-31 10:41:29 -0600 | [diff] [blame] | 101 | siv->ranges = range_list_insert(siv->ranges, cur); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 102 | cur = NULL; |
| 103 | } else { |
| 104 | goto error; |
| 105 | } |
| 106 | } else { |
| 107 | goto error; |
| 108 | } |
| 109 | } while (str); |
| 110 | |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 111 | return 0; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 112 | error: |
Michael S. Tsirkin | 0d15668 | 2014-06-16 18:07:03 +0300 | [diff] [blame] | 113 | g_list_foreach(siv->ranges, free_range, NULL); |
| 114 | g_list_free(siv->ranges); |
| 115 | siv->ranges = NULL; |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 116 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", |
| 117 | "an int64 value or range"); |
| 118 | return -1; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 122 | start_list(Visitor *v, const char *name, GenericList **list, size_t size, |
| 123 | Error **errp) |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 124 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 125 | StringInputVisitor *siv = to_siv(v); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 126 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 127 | /* We don't support visits without a list */ |
| 128 | assert(list); |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 129 | siv->list = list; |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 130 | |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 131 | if (parse_str(siv, name, errp) < 0) { |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 132 | *list = NULL; |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 133 | return; |
| 134 | } |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 135 | |
| 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 Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 140 | siv->cur = range_lob(r); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 141 | } |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 142 | *list = g_malloc0(size); |
| 143 | } else { |
| 144 | *list = NULL; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 148 | static GenericList *next_list(Visitor *v, GenericList *tail, size_t size) |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 149 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 150 | StringInputVisitor *siv = to_siv(v); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 151 | 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 Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 162 | if (!range_contains(r, siv->cur)) { |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 163 | 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 Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 171 | siv->cur = range_lob(r); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 174 | tail->next = g_malloc0(size); |
| 175 | return tail->next; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 178 | static 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 Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 207 | static void end_list(Visitor *v, void **obj) |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 208 | { |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 209 | StringInputVisitor *siv = to_siv(v); |
| 210 | |
| 211 | assert(siv->list == obj); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 214 | static void parse_type_int64(Visitor *v, const char *name, int64_t *obj, |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 215 | Error **errp) |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 216 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 217 | StringInputVisitor *siv = to_siv(v); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 218 | |
Eric Blake | 74f24cb | 2016-04-28 15:45:30 -0600 | [diff] [blame] | 219 | if (parse_str(siv, name, errp) < 0) { |
| 220 | return; |
| 221 | } |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 222 | |
| 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 Armbruster | a0efbf1 | 2016-07-01 13:47:47 +0200 | [diff] [blame] | 240 | siv->cur = range_lob(r); |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | *obj = siv->cur; |
| 244 | siv->cur++; |
| 245 | return; |
| 246 | |
| 247 | error: |
Eric Blake | 0a40bda | 2016-04-28 15:45:28 -0600 | [diff] [blame] | 248 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 249 | "an int64 value or range"); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 252 | static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj, |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 253 | Error **errp) |
| 254 | { |
| 255 | /* FIXME: parse_type_int64 mishandles values over INT64_MAX */ |
| 256 | int64_t i; |
| 257 | Error *err = NULL; |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 258 | parse_type_int64(v, name, &i, &err); |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 259 | if (err) { |
| 260 | error_propagate(errp, err); |
| 261 | } else { |
| 262 | *obj = i; |
| 263 | } |
| 264 | } |
| 265 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 266 | static void parse_type_size(Visitor *v, const char *name, uint64_t *obj, |
Paolo Bonzini | a5829cc | 2014-02-08 11:01:44 +0100 | [diff] [blame] | 267 | Error **errp) |
| 268 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 269 | StringInputVisitor *siv = to_siv(v); |
Paolo Bonzini | a5829cc | 2014-02-08 11:01:44 +0100 | [diff] [blame] | 270 | Error *err = NULL; |
| 271 | uint64_t val; |
| 272 | |
Markus Armbruster | f332e83 | 2017-03-03 13:32:36 +0100 | [diff] [blame] | 273 | parse_option_size(name, siv->string, &val, &err); |
Paolo Bonzini | a5829cc | 2014-02-08 11:01:44 +0100 | [diff] [blame] | 274 | if (err) { |
| 275 | error_propagate(errp, err); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | *obj = val; |
| 280 | } |
| 281 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 282 | static void parse_type_bool(Visitor *v, const char *name, bool *obj, |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 283 | Error **errp) |
| 284 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 285 | StringInputVisitor *siv = to_siv(v); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 286 | |
Markus Armbruster | f332e83 | 2017-03-03 13:32:36 +0100 | [diff] [blame] | 287 | 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 Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 298 | } |
| 299 | |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 300 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", |
| 301 | "boolean"); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 304 | static void parse_type_str(Visitor *v, const char *name, char **obj, |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 305 | Error **errp) |
| 306 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 307 | StringInputVisitor *siv = to_siv(v); |
Markus Armbruster | f332e83 | 2017-03-03 13:32:36 +0100 | [diff] [blame] | 308 | |
| 309 | *obj = g_strdup(siv->string); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 312 | static void parse_type_number(Visitor *v, const char *name, double *obj, |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 313 | Error **errp) |
| 314 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 315 | StringInputVisitor *siv = to_siv(v); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 316 | char *endp = (char *) siv->string; |
| 317 | double val; |
| 318 | |
| 319 | errno = 0; |
Markus Armbruster | f332e83 | 2017-03-03 13:32:36 +0100 | [diff] [blame] | 320 | val = strtod(siv->string, &endp); |
| 321 | if (errno || endp == siv->string || *endp) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 322 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", |
| 323 | "number"); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
| 327 | *obj = val; |
| 328 | } |
| 329 | |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 330 | static void parse_type_null(Visitor *v, const char *name, QNull **obj, |
| 331 | Error **errp) |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 332 | { |
| 333 | StringInputVisitor *siv = to_siv(v); |
| 334 | |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 335 | *obj = NULL; |
| 336 | |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 337 | if (!siv->string || siv->string[0]) { |
| 338 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", |
| 339 | "null"); |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 340 | return; |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 341 | } |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 342 | |
| 343 | *obj = qnull(); |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 346 | static void string_input_free(Visitor *v) |
| 347 | { |
| 348 | StringInputVisitor *siv = to_siv(v); |
| 349 | |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 350 | g_list_foreach(siv->ranges, free_range, NULL); |
| 351 | g_list_free(siv->ranges); |
| 352 | g_free(siv); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 353 | } |
| 354 | |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 355 | Visitor *string_input_visitor_new(const char *str) |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 356 | { |
| 357 | StringInputVisitor *v; |
| 358 | |
Markus Armbruster | f332e83 | 2017-03-03 13:32:36 +0100 | [diff] [blame] | 359 | assert(str); |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 360 | v = g_malloc0(sizeof(*v)); |
| 361 | |
Eric Blake | 983f52d | 2016-04-28 15:45:09 -0600 | [diff] [blame] | 362 | v->visitor.type = VISITOR_INPUT; |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 363 | v->visitor.type_int64 = parse_type_int64; |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 364 | v->visitor.type_uint64 = parse_type_uint64; |
Paolo Bonzini | a5829cc | 2014-02-08 11:01:44 +0100 | [diff] [blame] | 365 | v->visitor.type_size = parse_type_size; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 366 | v->visitor.type_bool = parse_type_bool; |
| 367 | v->visitor.type_str = parse_type_str; |
| 368 | v->visitor.type_number = parse_type_number; |
Greg Kurz | a733371 | 2016-12-16 16:26:09 +0100 | [diff] [blame] | 369 | v->visitor.type_null = parse_type_null; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 370 | v->visitor.start_list = start_list; |
| 371 | v->visitor.next_list = next_list; |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 372 | v->visitor.check_list = check_list; |
Hu Tao | 659268f | 2014-06-10 19:15:27 +0800 | [diff] [blame] | 373 | v->visitor.end_list = end_list; |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 374 | v->visitor.free = string_input_free; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 375 | |
| 376 | v->string = str; |
Eric Blake | 7a0525c | 2016-06-09 10:48:37 -0600 | [diff] [blame] | 377 | return &v->visitor; |
Paolo Bonzini | a020f98 | 2012-02-09 09:36:37 +0100 | [diff] [blame] | 378 | } |