]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/opts-visitor.c
qapi: Add parameter to visit_end_*
[mirror_qemu.git] / qapi / opts-visitor.c
CommitLineData
eb7ee2cb
LE
1/*
2 * Options Visitor
3 *
08f9541d 4 * Copyright Red Hat, Inc. 2012-2016
eb7ee2cb
LE
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
cbf21151 13#include "qemu/osdep.h"
da34e65c 14#include "qapi/error.h"
f348b6d1 15#include "qemu/cutils.h"
7b1b5d19
PB
16#include "qapi/qmp/qerror.h"
17#include "qapi/opts-visitor.h"
1de7afc9
PB
18#include "qemu/queue.h"
19#include "qemu/option_int.h"
7b1b5d19 20#include "qapi/visitor-impl.h"
eb7ee2cb
LE
21
22
d9570434
LE
23enum ListMode
24{
25 LM_NONE, /* not traversing a list of repeated options */
d8754f40 26
d9f62dde 27 LM_IN_PROGRESS, /* opts_next_list() ready to be called.
d8754f40
LE
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. */
d9570434
LE
52};
53
54typedef enum ListMode ListMode;
55
eb7ee2cb
LE
56struct 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. */
d9570434 73 ListMode list_mode;
eb7ee2cb 74 GQueue *repeated_opts;
eb7ee2cb 75
d8754f40
LE
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
eb7ee2cb
LE
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
d7bea75d
EB
93static OptsVisitor *to_ov(Visitor *v)
94{
95 return container_of(v, OptsVisitor, visitor);
96}
97
98
eb7ee2cb
LE
99static void
100destroy_list(gpointer list)
101{
102 g_queue_free(list);
103}
104
105
106static void
107opts_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
127static void
337283df 128opts_start_struct(Visitor *v, const char *name, void **obj,
0b2a0d6b 129 size_t size, Error **errp)
eb7ee2cb 130{
d7bea75d 131 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
132 const QemuOpt *opt;
133
b7745397 134 if (obj) {
e58d695e 135 *obj = g_malloc0(size);
b7745397 136 }
eb7ee2cb
LE
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
dc8622f2
CL
153 ov->fake_id_opt->name = g_strdup("id");
154 ov->fake_id_opt->str = g_strdup(ov->opts_root->id);
eb7ee2cb
LE
155 opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt);
156 }
157}
158
159
eb7ee2cb 160static void
15c2f669 161opts_check_struct(Visitor *v, Error **errp)
eb7ee2cb 162{
d7bea75d 163 OptsVisitor *ov = to_ov(v);
f96493b1 164 GHashTableIter iter;
eb7ee2cb
LE
165 GQueue *any;
166
15c2f669 167 if (ov->depth > 0) {
eb7ee2cb
LE
168 return;
169 }
170
171 /* we should have processed all (distinct) QemuOpt instances */
f96493b1
EB
172 g_hash_table_iter_init(&iter, ov->unprocessed_opts);
173 if (g_hash_table_iter_next(&iter, NULL, (void **)&any)) {
eb7ee2cb
LE
174 const QemuOpt *first;
175
176 first = g_queue_peek_head(any);
c6bd8c70 177 error_setg(errp, QERR_INVALID_PARAMETER, first->name);
eb7ee2cb 178 }
15c2f669
EB
179}
180
181
182static void
1158bb2a 183opts_end_struct(Visitor *v, void **obj)
15c2f669
EB
184{
185 OptsVisitor *ov = to_ov(v);
186
187 if (--ov->depth > 0) {
188 return;
189 }
190
eb7ee2cb
LE
191 g_hash_table_destroy(ov->unprocessed_opts);
192 ov->unprocessed_opts = NULL;
dc8622f2
CL
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 }
eb7ee2cb
LE
198 ov->fake_id_opt = NULL;
199}
200
201
202static GQueue *
203lookup_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) {
c6bd8c70 209 error_setg(errp, QERR_MISSING_PARAMETER, name);
eb7ee2cb
LE
210 }
211 return list;
212}
213
214
215static void
d9f62dde
EB
216opts_start_list(Visitor *v, const char *name, GenericList **list, size_t size,
217 Error **errp)
eb7ee2cb 218{
d7bea75d 219 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
220
221 /* we can't traverse a list in a list */
d9570434 222 assert(ov->list_mode == LM_NONE);
d9f62dde
EB
223 /* we don't support visits without a list */
224 assert(list);
eb7ee2cb 225 ov->repeated_opts = lookup_distinct(ov, name, errp);
d9f62dde
EB
226 if (ov->repeated_opts) {
227 ov->list_mode = LM_IN_PROGRESS;
228 *list = g_malloc0(size);
229 } else {
230 *list = NULL;
d9570434 231 }
eb7ee2cb
LE
232}
233
234
235static GenericList *
d9f62dde 236opts_next_list(Visitor *v, GenericList *tail, size_t size)
eb7ee2cb 237{
d7bea75d 238 OptsVisitor *ov = to_ov(v);
eb7ee2cb 239
d9570434 240 switch (ov->list_mode) {
d8754f40
LE
241 case LM_SIGNED_INTERVAL:
242 case LM_UNSIGNED_INTERVAL:
d8754f40
LE
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
d9570434 255 case LM_IN_PROGRESS: {
eb7ee2cb
LE
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 }
d9570434
LE
263 break;
264 }
265
266 default:
267 abort();
eb7ee2cb
LE
268 }
269
d9f62dde
EB
270 tail->next = g_malloc0(size);
271 return tail->next;
eb7ee2cb
LE
272}
273
274
275static void
1158bb2a 276opts_end_list(Visitor *v, void **obj)
eb7ee2cb 277{
d7bea75d 278 OptsVisitor *ov = to_ov(v);
eb7ee2cb 279
d9f62dde 280 assert(ov->list_mode == LM_IN_PROGRESS ||
d8754f40
LE
281 ov->list_mode == LM_SIGNED_INTERVAL ||
282 ov->list_mode == LM_UNSIGNED_INTERVAL);
eb7ee2cb 283 ov->repeated_opts = NULL;
d9570434 284 ov->list_mode = LM_NONE;
eb7ee2cb
LE
285}
286
287
288static const QemuOpt *
289lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp)
290{
d9570434 291 if (ov->list_mode == LM_NONE) {
eb7ee2cb
LE
292 GQueue *list;
293
294 /* the last occurrence of any QemuOpt takes effect when queried by name
295 */
296 list = lookup_distinct(ov, name, errp);
297 return list ? g_queue_peek_tail(list) : NULL;
298 }
d9570434 299 assert(ov->list_mode == LM_IN_PROGRESS);
eb7ee2cb
LE
300 return g_queue_peek_head(ov->repeated_opts);
301}
302
303
304static void
305processed(OptsVisitor *ov, const char *name)
306{
d9570434 307 if (ov->list_mode == LM_NONE) {
eb7ee2cb 308 g_hash_table_remove(ov->unprocessed_opts, name);
d9570434 309 return;
eb7ee2cb 310 }
d9570434
LE
311 assert(ov->list_mode == LM_IN_PROGRESS);
312 /* do nothing */
eb7ee2cb
LE
313}
314
315
316static void
0b2a0d6b 317opts_type_str(Visitor *v, const char *name, char **obj, Error **errp)
eb7ee2cb 318{
d7bea75d 319 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
320 const QemuOpt *opt;
321
322 opt = lookup_scalar(ov, name, errp);
323 if (!opt) {
e58d695e 324 *obj = NULL;
eb7ee2cb
LE
325 return;
326 }
327 *obj = g_strdup(opt->str ? opt->str : "");
983f52d4
EB
328 /* Note that we consume a string even if this is called as part of
329 * an enum visit that later fails because the string is not a
330 * valid enum value; this is harmless because tracking what gets
331 * consumed only matters to visit_end_struct() as the final error
332 * check if there were no other failures during the visit. */
eb7ee2cb
LE
333 processed(ov, name);
334}
335
336
337/* mimics qemu-option.c::parse_option_bool() */
338static void
0b2a0d6b 339opts_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
eb7ee2cb 340{
d7bea75d 341 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
342 const QemuOpt *opt;
343
344 opt = lookup_scalar(ov, name, errp);
345 if (!opt) {
346 return;
347 }
348
349 if (opt->str) {
350 if (strcmp(opt->str, "on") == 0 ||
351 strcmp(opt->str, "yes") == 0 ||
352 strcmp(opt->str, "y") == 0) {
353 *obj = true;
354 } else if (strcmp(opt->str, "off") == 0 ||
355 strcmp(opt->str, "no") == 0 ||
356 strcmp(opt->str, "n") == 0) {
357 *obj = false;
358 } else {
c6bd8c70
MA
359 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
360 "on|yes|y|off|no|n");
eb7ee2cb
LE
361 return;
362 }
363 } else {
364 *obj = true;
365 }
366
367 processed(ov, name);
368}
369
370
371static void
0b2a0d6b 372opts_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp)
eb7ee2cb 373{
d7bea75d 374 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
375 const QemuOpt *opt;
376 const char *str;
377 long long val;
378 char *endptr;
379
d8754f40
LE
380 if (ov->list_mode == LM_SIGNED_INTERVAL) {
381 *obj = ov->range_next.s;
382 return;
383 }
384
eb7ee2cb
LE
385 opt = lookup_scalar(ov, name, errp);
386 if (!opt) {
387 return;
388 }
389 str = opt->str ? opt->str : "";
390
1e1c555a
LE
391 /* we've gotten past lookup_scalar() */
392 assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
393
eb7ee2cb
LE
394 errno = 0;
395 val = strtoll(str, &endptr, 0);
1e1c555a
LE
396 if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) {
397 if (*endptr == '\0') {
398 *obj = val;
399 processed(ov, name);
400 return;
401 }
402 if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
403 long long val2;
404
405 str = endptr + 1;
406 val2 = strtoll(str, &endptr, 0);
407 if (errno == 0 && endptr > str && *endptr == '\0' &&
15a849be
LE
408 INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 &&
409 (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX ||
410 val2 < val + OPTS_VISITOR_RANGE_MAX)) {
1e1c555a
LE
411 ov->range_next.s = val;
412 ov->range_limit.s = val2;
413 ov->list_mode = LM_SIGNED_INTERVAL;
414
415 /* as if entering on the top */
416 *obj = ov->range_next.s;
417 return;
418 }
419 }
eb7ee2cb 420 }
c6bd8c70
MA
421 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
422 (ov->list_mode == LM_NONE) ? "an int64 value" :
423 "an int64 value or range");
eb7ee2cb
LE
424}
425
426
427static void
0b2a0d6b 428opts_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp)
eb7ee2cb 429{
d7bea75d 430 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
431 const QemuOpt *opt;
432 const char *str;
62d090e2 433 unsigned long long val;
581a8a80 434 char *endptr;
eb7ee2cb 435
d8754f40
LE
436 if (ov->list_mode == LM_UNSIGNED_INTERVAL) {
437 *obj = ov->range_next.u;
438 return;
439 }
440
eb7ee2cb
LE
441 opt = lookup_scalar(ov, name, errp);
442 if (!opt) {
443 return;
444 }
eb7ee2cb 445 str = opt->str;
eb7ee2cb 446
581a8a80
LE
447 /* we've gotten past lookup_scalar() */
448 assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
449
450 if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
451 if (*endptr == '\0') {
452 *obj = val;
453 processed(ov, name);
454 return;
455 }
456 if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
457 unsigned long long val2;
458
459 str = endptr + 1;
460 if (parse_uint_full(str, &val2, 0) == 0 &&
15a849be
LE
461 val2 <= UINT64_MAX && val <= val2 &&
462 val2 - val < OPTS_VISITOR_RANGE_MAX) {
581a8a80
LE
463 ov->range_next.u = val;
464 ov->range_limit.u = val2;
465 ov->list_mode = LM_UNSIGNED_INTERVAL;
466
467 /* as if entering on the top */
468 *obj = ov->range_next.u;
469 return;
470 }
471 }
eb7ee2cb 472 }
c6bd8c70
MA
473 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
474 (ov->list_mode == LM_NONE) ? "a uint64 value" :
475 "a uint64 value or range");
eb7ee2cb
LE
476}
477
478
479static void
0b2a0d6b 480opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
eb7ee2cb 481{
d7bea75d 482 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
483 const QemuOpt *opt;
484 int64_t val;
485 char *endptr;
486
487 opt = lookup_scalar(ov, name, errp);
488 if (!opt) {
489 return;
490 }
491
4677bb40
MAL
492 val = qemu_strtosz_suffix(opt->str ? opt->str : "", &endptr,
493 QEMU_STRTOSZ_DEFSUFFIX_B);
cb45de67 494 if (val < 0 || *endptr) {
c6bd8c70
MA
495 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
496 "a size value representible as a non-negative int64");
eb7ee2cb
LE
497 return;
498 }
cb45de67
AK
499
500 *obj = val;
501 processed(ov, name);
eb7ee2cb
LE
502}
503
504
505static void
0b2a0d6b 506opts_optional(Visitor *v, const char *name, bool *present)
eb7ee2cb 507{
d7bea75d 508 OptsVisitor *ov = to_ov(v);
eb7ee2cb
LE
509
510 /* we only support a single mandatory scalar field in a list node */
d9570434 511 assert(ov->list_mode == LM_NONE);
eb7ee2cb
LE
512 *present = (lookup_distinct(ov, name, NULL) != NULL);
513}
514
515
516OptsVisitor *
517opts_visitor_new(const QemuOpts *opts)
518{
519 OptsVisitor *ov;
520
521 ov = g_malloc0(sizeof *ov);
522
983f52d4
EB
523 ov->visitor.type = VISITOR_INPUT;
524
eb7ee2cb 525 ov->visitor.start_struct = &opts_start_struct;
15c2f669 526 ov->visitor.check_struct = &opts_check_struct;
eb7ee2cb
LE
527 ov->visitor.end_struct = &opts_end_struct;
528
529 ov->visitor.start_list = &opts_start_list;
530 ov->visitor.next_list = &opts_next_list;
531 ov->visitor.end_list = &opts_end_list;
532
4c40314a 533 ov->visitor.type_int64 = &opts_type_int64;
eb7ee2cb
LE
534 ov->visitor.type_uint64 = &opts_type_uint64;
535 ov->visitor.type_size = &opts_type_size;
536 ov->visitor.type_bool = &opts_type_bool;
537 ov->visitor.type_str = &opts_type_str;
538
539 /* type_number() is not filled in, but this is not the first visitor to
540 * skip some mandatory methods... */
541
e2cd0f4f 542 ov->visitor.optional = &opts_optional;
eb7ee2cb
LE
543
544 ov->opts_root = opts;
545
546 return ov;
547}
548
549
550void
551opts_visitor_cleanup(OptsVisitor *ov)
552{
553 if (ov->unprocessed_opts != NULL) {
554 g_hash_table_destroy(ov->unprocessed_opts);
555 }
556 g_free(ov->fake_id_opt);
e36c8766 557 g_free(ov);
eb7ee2cb
LE
558}
559
560
561Visitor *
562opts_get_visitor(OptsVisitor *ov)
563{
564 return &ov->visitor;
565}