Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Options Visitor |
| 3 | * |
Eric Blake | 08f9541 | 2016-01-29 06:48:59 -0700 | [diff] [blame] | 4 | * Copyright Red Hat, Inc. 2012-2016 |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 5 | * |
| 6 | * Author: Laszlo Ersek <lersek@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" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 15 | #include "qemu/cutils.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 16 | #include "qapi/qmp/qerror.h" |
| 17 | #include "qapi/opts-visitor.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 18 | #include "qemu/queue.h" |
| 19 | #include "qemu/option_int.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 20 | #include "qapi/visitor-impl.h" |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 21 | |
| 22 | |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 23 | enum ListMode |
| 24 | { |
| 25 | LM_NONE, /* not traversing a list of repeated options */ |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 26 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 27 | LM_IN_PROGRESS, /* opts_next_list() ready to be called. |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 28 | * |
| 29 | * Generating the next list link will consume the most |
| 30 | * recently parsed QemuOpt instance of the repeated |
| 31 | * option. |
| 32 | * |
| 33 | * Parsing a value into the list link will examine the |
| 34 | * next QemuOpt instance of the repeated option, and |
| 35 | * possibly enter LM_SIGNED_INTERVAL or |
| 36 | * LM_UNSIGNED_INTERVAL. |
| 37 | */ |
| 38 | |
| 39 | LM_SIGNED_INTERVAL, /* opts_next_list() has been called. |
| 40 | * |
| 41 | * Generating the next list link will consume the most |
| 42 | * recently stored element from the signed interval, |
| 43 | * parsed from the most recent QemuOpt instance of the |
| 44 | * repeated option. This may consume QemuOpt itself |
| 45 | * and return to LM_IN_PROGRESS. |
| 46 | * |
| 47 | * Parsing a value into the list link will store the |
| 48 | * next element of the signed interval. |
| 49 | */ |
| 50 | |
| 51 | LM_UNSIGNED_INTERVAL /* Same as above, only for an unsigned interval. */ |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | typedef enum ListMode ListMode; |
| 55 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 56 | struct OptsVisitor |
| 57 | { |
| 58 | Visitor visitor; |
| 59 | |
| 60 | /* Ownership remains with opts_visitor_new()'s caller. */ |
| 61 | const QemuOpts *opts_root; |
| 62 | |
| 63 | unsigned depth; |
| 64 | |
| 65 | /* Non-null iff depth is positive. Each key is a QemuOpt name. Each value |
| 66 | * is a non-empty GQueue, enumerating all QemuOpt occurrences with that |
| 67 | * name. */ |
| 68 | GHashTable *unprocessed_opts; |
| 69 | |
| 70 | /* The list currently being traversed with opts_start_list() / |
| 71 | * opts_next_list(). The list must have a struct element type in the |
| 72 | * schema, with a single mandatory scalar member. */ |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 73 | ListMode list_mode; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 74 | GQueue *repeated_opts; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 75 | |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 76 | /* When parsing a list of repeating options as integers, values of the form |
| 77 | * "a-b", representing a closed interval, are allowed. Elements in the |
| 78 | * range are generated individually. |
| 79 | */ |
| 80 | union { |
| 81 | int64_t s; |
| 82 | uint64_t u; |
| 83 | } range_next, range_limit; |
| 84 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 85 | /* If "opts_root->id" is set, reinstantiate it as a fake QemuOpt for |
| 86 | * uniformity. Only its "name" and "str" fields are set. "fake_id_opt" does |
| 87 | * not survive or escape the OptsVisitor object. |
| 88 | */ |
| 89 | QemuOpt *fake_id_opt; |
| 90 | }; |
| 91 | |
| 92 | |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 93 | static OptsVisitor *to_ov(Visitor *v) |
| 94 | { |
| 95 | return container_of(v, OptsVisitor, visitor); |
| 96 | } |
| 97 | |
| 98 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 99 | static void |
| 100 | destroy_list(gpointer list) |
| 101 | { |
| 102 | g_queue_free(list); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void |
| 107 | opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt) |
| 108 | { |
| 109 | GQueue *list; |
| 110 | |
| 111 | list = g_hash_table_lookup(unprocessed_opts, opt->name); |
| 112 | if (list == NULL) { |
| 113 | list = g_queue_new(); |
| 114 | |
| 115 | /* GHashTable will never try to free the keys -- we supply NULL as |
| 116 | * "key_destroy_func" in opts_start_struct(). Thus cast away key |
| 117 | * const-ness in order to suppress gcc's warning. |
| 118 | */ |
| 119 | g_hash_table_insert(unprocessed_opts, (gpointer)opt->name, list); |
| 120 | } |
| 121 | |
| 122 | /* Similarly, destroy_list() doesn't call g_queue_free_full(). */ |
| 123 | g_queue_push_tail(list, (gpointer)opt); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void |
Eric Blake | 337283d | 2016-01-29 06:48:57 -0700 | [diff] [blame] | 128 | opts_start_struct(Visitor *v, const char *name, void **obj, |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 129 | size_t size, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 130 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 131 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 132 | const QemuOpt *opt; |
| 133 | |
Markus Armbruster | b774539 | 2014-03-01 08:40:39 +0100 | [diff] [blame] | 134 | if (obj) { |
Eric Blake | e58d695 | 2016-04-28 15:45:10 -0600 | [diff] [blame] | 135 | *obj = g_malloc0(size); |
Markus Armbruster | b774539 | 2014-03-01 08:40:39 +0100 | [diff] [blame] | 136 | } |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 137 | if (ov->depth++ > 0) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal, |
| 142 | NULL, &destroy_list); |
| 143 | QTAILQ_FOREACH(opt, &ov->opts_root->head, next) { |
| 144 | /* ensured by qemu-option.c::opts_do_parse() */ |
| 145 | assert(strcmp(opt->name, "id") != 0); |
| 146 | |
| 147 | opts_visitor_insert(ov->unprocessed_opts, opt); |
| 148 | } |
| 149 | |
| 150 | if (ov->opts_root->id != NULL) { |
| 151 | ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt); |
| 152 | |
Chunyan Liu | dc8622f | 2014-06-05 17:20:44 +0800 | [diff] [blame] | 153 | ov->fake_id_opt->name = g_strdup("id"); |
| 154 | ov->fake_id_opt->str = g_strdup(ov->opts_root->id); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 155 | opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 160 | static void |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 161 | opts_check_struct(Visitor *v, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 162 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 163 | OptsVisitor *ov = to_ov(v); |
Eric Blake | f96493b | 2016-02-17 23:48:15 -0700 | [diff] [blame] | 164 | GHashTableIter iter; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 165 | GQueue *any; |
| 166 | |
Eric Blake | 21f88d0 | 2017-03-22 09:45:25 -0500 | [diff] [blame] | 167 | if (ov->depth > 1) { |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | |
| 171 | /* we should have processed all (distinct) QemuOpt instances */ |
Eric Blake | f96493b | 2016-02-17 23:48:15 -0700 | [diff] [blame] | 172 | g_hash_table_iter_init(&iter, ov->unprocessed_opts); |
| 173 | if (g_hash_table_iter_next(&iter, NULL, (void **)&any)) { |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 174 | const QemuOpt *first; |
| 175 | |
| 176 | first = g_queue_peek_head(any); |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 177 | error_setg(errp, QERR_INVALID_PARAMETER, first->name); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 178 | } |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | static void |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 183 | opts_end_struct(Visitor *v, void **obj) |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 184 | { |
| 185 | OptsVisitor *ov = to_ov(v); |
| 186 | |
| 187 | if (--ov->depth > 0) { |
| 188 | return; |
| 189 | } |
| 190 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 191 | g_hash_table_destroy(ov->unprocessed_opts); |
| 192 | ov->unprocessed_opts = NULL; |
Chunyan Liu | dc8622f | 2014-06-05 17:20:44 +0800 | [diff] [blame] | 193 | if (ov->fake_id_opt) { |
| 194 | g_free(ov->fake_id_opt->name); |
| 195 | g_free(ov->fake_id_opt->str); |
| 196 | g_free(ov->fake_id_opt); |
| 197 | } |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 198 | ov->fake_id_opt = NULL; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static GQueue * |
| 203 | lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp) |
| 204 | { |
| 205 | GQueue *list; |
| 206 | |
| 207 | list = g_hash_table_lookup(ov->unprocessed_opts, name); |
| 208 | if (!list) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 209 | error_setg(errp, QERR_MISSING_PARAMETER, name); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 210 | } |
| 211 | return list; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | static void |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 216 | opts_start_list(Visitor *v, const char *name, GenericList **list, size_t size, |
| 217 | Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 218 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 219 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 220 | |
| 221 | /* we can't traverse a list in a list */ |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 222 | assert(ov->list_mode == LM_NONE); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 223 | /* we don't support visits without a list */ |
| 224 | assert(list); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 225 | ov->repeated_opts = lookup_distinct(ov, name, errp); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 226 | if (ov->repeated_opts) { |
| 227 | ov->list_mode = LM_IN_PROGRESS; |
| 228 | *list = g_malloc0(size); |
| 229 | } else { |
| 230 | *list = NULL; |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 231 | } |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
| 235 | static GenericList * |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 236 | opts_next_list(Visitor *v, GenericList *tail, size_t size) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 237 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 238 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 239 | |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 240 | switch (ov->list_mode) { |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 241 | case LM_SIGNED_INTERVAL: |
| 242 | case LM_UNSIGNED_INTERVAL: |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 243 | if (ov->list_mode == LM_SIGNED_INTERVAL) { |
| 244 | if (ov->range_next.s < ov->range_limit.s) { |
| 245 | ++ov->range_next.s; |
| 246 | break; |
| 247 | } |
| 248 | } else if (ov->range_next.u < ov->range_limit.u) { |
| 249 | ++ov->range_next.u; |
| 250 | break; |
| 251 | } |
| 252 | ov->list_mode = LM_IN_PROGRESS; |
| 253 | /* range has been completed, fall through in order to pop option */ |
| 254 | |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 255 | case LM_IN_PROGRESS: { |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 256 | const QemuOpt *opt; |
| 257 | |
| 258 | opt = g_queue_pop_head(ov->repeated_opts); |
| 259 | if (g_queue_is_empty(ov->repeated_opts)) { |
| 260 | g_hash_table_remove(ov->unprocessed_opts, opt->name); |
| 261 | return NULL; |
| 262 | } |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | |
| 266 | default: |
| 267 | abort(); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 270 | tail->next = g_malloc0(size); |
| 271 | return tail->next; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | static void |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 276 | opts_check_list(Visitor *v, Error **errp) |
| 277 | { |
| 278 | /* |
Eric Blake | 21f88d0 | 2017-03-22 09:45:25 -0500 | [diff] [blame] | 279 | * Unvisited list elements will be reported later when checking |
| 280 | * whether unvisited struct members remain. |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 281 | */ |
| 282 | } |
| 283 | |
| 284 | |
| 285 | static void |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 286 | opts_end_list(Visitor *v, void **obj) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 287 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 288 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 289 | |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 290 | assert(ov->list_mode == LM_IN_PROGRESS || |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 291 | ov->list_mode == LM_SIGNED_INTERVAL || |
| 292 | ov->list_mode == LM_UNSIGNED_INTERVAL); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 293 | ov->repeated_opts = NULL; |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 294 | ov->list_mode = LM_NONE; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
| 298 | static const QemuOpt * |
| 299 | lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp) |
| 300 | { |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 301 | if (ov->list_mode == LM_NONE) { |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 302 | GQueue *list; |
| 303 | |
| 304 | /* the last occurrence of any QemuOpt takes effect when queried by name |
| 305 | */ |
| 306 | list = lookup_distinct(ov, name, errp); |
| 307 | return list ? g_queue_peek_tail(list) : NULL; |
| 308 | } |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 309 | assert(ov->list_mode == LM_IN_PROGRESS); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 310 | return g_queue_peek_head(ov->repeated_opts); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | static void |
| 315 | processed(OptsVisitor *ov, const char *name) |
| 316 | { |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 317 | if (ov->list_mode == LM_NONE) { |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 318 | g_hash_table_remove(ov->unprocessed_opts, name); |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 319 | return; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 320 | } |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 321 | assert(ov->list_mode == LM_IN_PROGRESS); |
| 322 | /* do nothing */ |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
| 326 | static void |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 327 | opts_type_str(Visitor *v, const char *name, char **obj, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 328 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 329 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 330 | const QemuOpt *opt; |
| 331 | |
| 332 | opt = lookup_scalar(ov, name, errp); |
| 333 | if (!opt) { |
Eric Blake | e58d695 | 2016-04-28 15:45:10 -0600 | [diff] [blame] | 334 | *obj = NULL; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | *obj = g_strdup(opt->str ? opt->str : ""); |
Eric Blake | 983f52d | 2016-04-28 15:45:09 -0600 | [diff] [blame] | 338 | /* Note that we consume a string even if this is called as part of |
| 339 | * an enum visit that later fails because the string is not a |
| 340 | * valid enum value; this is harmless because tracking what gets |
| 341 | * consumed only matters to visit_end_struct() as the final error |
| 342 | * check if there were no other failures during the visit. */ |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 343 | processed(ov, name); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /* mimics qemu-option.c::parse_option_bool() */ |
| 348 | static void |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 349 | opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 350 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 351 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 352 | const QemuOpt *opt; |
| 353 | |
| 354 | opt = lookup_scalar(ov, name, errp); |
| 355 | if (!opt) { |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | if (opt->str) { |
| 360 | if (strcmp(opt->str, "on") == 0 || |
| 361 | strcmp(opt->str, "yes") == 0 || |
| 362 | strcmp(opt->str, "y") == 0) { |
| 363 | *obj = true; |
| 364 | } else if (strcmp(opt->str, "off") == 0 || |
| 365 | strcmp(opt->str, "no") == 0 || |
| 366 | strcmp(opt->str, "n") == 0) { |
| 367 | *obj = false; |
| 368 | } else { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 369 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
| 370 | "on|yes|y|off|no|n"); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | } else { |
| 374 | *obj = true; |
| 375 | } |
| 376 | |
| 377 | processed(ov, name); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static void |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 382 | opts_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 383 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 384 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 385 | const QemuOpt *opt; |
| 386 | const char *str; |
| 387 | long long val; |
| 388 | char *endptr; |
| 389 | |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 390 | if (ov->list_mode == LM_SIGNED_INTERVAL) { |
| 391 | *obj = ov->range_next.s; |
| 392 | return; |
| 393 | } |
| 394 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 395 | opt = lookup_scalar(ov, name, errp); |
| 396 | if (!opt) { |
| 397 | return; |
| 398 | } |
| 399 | str = opt->str ? opt->str : ""; |
| 400 | |
Laszlo Ersek | 1e1c555 | 2013-08-20 00:35:35 +0200 | [diff] [blame] | 401 | /* we've gotten past lookup_scalar() */ |
| 402 | assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS); |
| 403 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 404 | errno = 0; |
| 405 | val = strtoll(str, &endptr, 0); |
Laszlo Ersek | 1e1c555 | 2013-08-20 00:35:35 +0200 | [diff] [blame] | 406 | if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) { |
| 407 | if (*endptr == '\0') { |
| 408 | *obj = val; |
| 409 | processed(ov, name); |
| 410 | return; |
| 411 | } |
| 412 | if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) { |
| 413 | long long val2; |
| 414 | |
| 415 | str = endptr + 1; |
| 416 | val2 = strtoll(str, &endptr, 0); |
| 417 | if (errno == 0 && endptr > str && *endptr == '\0' && |
Laszlo Ersek | 15a849b | 2013-08-20 00:35:38 +0200 | [diff] [blame] | 418 | INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 && |
| 419 | (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX || |
| 420 | val2 < val + OPTS_VISITOR_RANGE_MAX)) { |
Laszlo Ersek | 1e1c555 | 2013-08-20 00:35:35 +0200 | [diff] [blame] | 421 | ov->range_next.s = val; |
| 422 | ov->range_limit.s = val2; |
| 423 | ov->list_mode = LM_SIGNED_INTERVAL; |
| 424 | |
| 425 | /* as if entering on the top */ |
| 426 | *obj = ov->range_next.s; |
| 427 | return; |
| 428 | } |
| 429 | } |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 430 | } |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 431 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
| 432 | (ov->list_mode == LM_NONE) ? "an int64 value" : |
| 433 | "an int64 value or range"); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | |
| 437 | static void |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 438 | opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 439 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 440 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 441 | const QemuOpt *opt; |
| 442 | const char *str; |
Laszlo Ersek | 62d090e | 2013-08-20 00:35:36 +0200 | [diff] [blame] | 443 | unsigned long long val; |
Laszlo Ersek | 581a8a8 | 2013-08-20 00:35:37 +0200 | [diff] [blame] | 444 | char *endptr; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 445 | |
Laszlo Ersek | d8754f4 | 2013-08-20 00:35:34 +0200 | [diff] [blame] | 446 | if (ov->list_mode == LM_UNSIGNED_INTERVAL) { |
| 447 | *obj = ov->range_next.u; |
| 448 | return; |
| 449 | } |
| 450 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 451 | opt = lookup_scalar(ov, name, errp); |
| 452 | if (!opt) { |
| 453 | return; |
| 454 | } |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 455 | str = opt->str; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 456 | |
Laszlo Ersek | 581a8a8 | 2013-08-20 00:35:37 +0200 | [diff] [blame] | 457 | /* we've gotten past lookup_scalar() */ |
| 458 | assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS); |
| 459 | |
| 460 | if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) { |
| 461 | if (*endptr == '\0') { |
| 462 | *obj = val; |
| 463 | processed(ov, name); |
| 464 | return; |
| 465 | } |
| 466 | if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) { |
| 467 | unsigned long long val2; |
| 468 | |
| 469 | str = endptr + 1; |
| 470 | if (parse_uint_full(str, &val2, 0) == 0 && |
Laszlo Ersek | 15a849b | 2013-08-20 00:35:38 +0200 | [diff] [blame] | 471 | val2 <= UINT64_MAX && val <= val2 && |
| 472 | val2 - val < OPTS_VISITOR_RANGE_MAX) { |
Laszlo Ersek | 581a8a8 | 2013-08-20 00:35:37 +0200 | [diff] [blame] | 473 | ov->range_next.u = val; |
| 474 | ov->range_limit.u = val2; |
| 475 | ov->list_mode = LM_UNSIGNED_INTERVAL; |
| 476 | |
| 477 | /* as if entering on the top */ |
| 478 | *obj = ov->range_next.u; |
| 479 | return; |
| 480 | } |
| 481 | } |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 482 | } |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 483 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
| 484 | (ov->list_mode == LM_NONE) ? "a uint64 value" : |
| 485 | "a uint64 value or range"); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | static void |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 490 | opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 491 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 492 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 493 | const QemuOpt *opt; |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 494 | int err; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 495 | |
| 496 | opt = lookup_scalar(ov, name, errp); |
| 497 | if (!opt) { |
| 498 | return; |
| 499 | } |
| 500 | |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 501 | err = qemu_strtosz(opt->str ? opt->str : "", NULL, obj); |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 502 | if (err < 0) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 503 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name, |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 504 | "a size value"); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 505 | return; |
| 506 | } |
Amos Kong | cb45de6 | 2014-04-28 13:53:49 +0800 | [diff] [blame] | 507 | |
Amos Kong | cb45de6 | 2014-04-28 13:53:49 +0800 | [diff] [blame] | 508 | processed(ov, name); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
| 512 | static void |
Eric Blake | 0b2a0d6 | 2016-01-29 06:48:56 -0700 | [diff] [blame] | 513 | opts_optional(Visitor *v, const char *name, bool *present) |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 514 | { |
Eric Blake | d7bea75 | 2016-01-29 06:48:38 -0700 | [diff] [blame] | 515 | OptsVisitor *ov = to_ov(v); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 516 | |
| 517 | /* we only support a single mandatory scalar field in a list node */ |
Laszlo Ersek | d957043 | 2013-08-20 00:35:33 +0200 | [diff] [blame] | 518 | assert(ov->list_mode == LM_NONE); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 519 | *present = (lookup_distinct(ov, name, NULL) != NULL); |
| 520 | } |
| 521 | |
| 522 | |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 523 | static void |
| 524 | opts_free(Visitor *v) |
| 525 | { |
| 526 | OptsVisitor *ov = to_ov(v); |
| 527 | |
Eric Blake | 09204ea | 2016-06-09 10:48:36 -0600 | [diff] [blame] | 528 | if (ov->unprocessed_opts != NULL) { |
| 529 | g_hash_table_destroy(ov->unprocessed_opts); |
| 530 | } |
| 531 | g_free(ov->fake_id_opt); |
| 532 | g_free(ov); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
Eric Blake | 09204ea | 2016-06-09 10:48:36 -0600 | [diff] [blame] | 536 | Visitor * |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 537 | opts_visitor_new(const QemuOpts *opts) |
| 538 | { |
| 539 | OptsVisitor *ov; |
| 540 | |
Markus Armbruster | f332e83 | 2017-03-03 13:32:36 +0100 | [diff] [blame] | 541 | assert(opts); |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 542 | ov = g_malloc0(sizeof *ov); |
| 543 | |
Eric Blake | 983f52d | 2016-04-28 15:45:09 -0600 | [diff] [blame] | 544 | ov->visitor.type = VISITOR_INPUT; |
| 545 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 546 | ov->visitor.start_struct = &opts_start_struct; |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 547 | ov->visitor.check_struct = &opts_check_struct; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 548 | ov->visitor.end_struct = &opts_end_struct; |
| 549 | |
| 550 | ov->visitor.start_list = &opts_start_list; |
| 551 | ov->visitor.next_list = &opts_next_list; |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 552 | ov->visitor.check_list = &opts_check_list; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 553 | ov->visitor.end_list = &opts_end_list; |
| 554 | |
Eric Blake | 4c40314 | 2016-01-29 06:48:49 -0700 | [diff] [blame] | 555 | ov->visitor.type_int64 = &opts_type_int64; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 556 | ov->visitor.type_uint64 = &opts_type_uint64; |
| 557 | ov->visitor.type_size = &opts_type_size; |
| 558 | ov->visitor.type_bool = &opts_type_bool; |
| 559 | ov->visitor.type_str = &opts_type_str; |
| 560 | |
| 561 | /* type_number() is not filled in, but this is not the first visitor to |
| 562 | * skip some mandatory methods... */ |
| 563 | |
Markus Armbruster | e2cd0f4 | 2014-05-07 09:53:46 +0200 | [diff] [blame] | 564 | ov->visitor.optional = &opts_optional; |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 565 | ov->visitor.free = opts_free; |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 566 | |
| 567 | ov->opts_root = opts; |
| 568 | |
Laszlo Ersek | eb7ee2c | 2012-07-17 16:17:09 +0200 | [diff] [blame] | 569 | return &ov->visitor; |
| 570 | } |