]> Git Repo - qemu.git/blame - qapi/opts-visitor.c
qapi: Clean up includes
[qemu.git] / qapi / opts-visitor.c
CommitLineData
eb7ee2cb
LE
1/*
2 * Options Visitor
3 *
d9570434 4 * Copyright Red Hat, Inc. 2012, 2013
eb7ee2cb
LE
5 *
6 * Author: Laszlo Ersek <[email protected]>
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"
79ee7df8 14#include "qemu-common.h"
7b1b5d19
PB
15#include "qapi/qmp/qerror.h"
16#include "qapi/opts-visitor.h"
1de7afc9
PB
17#include "qemu/queue.h"
18#include "qemu/option_int.h"
7b1b5d19 19#include "qapi/visitor-impl.h"
eb7ee2cb
LE
20
21
d9570434
LE
22enum ListMode
23{
24 LM_NONE, /* not traversing a list of repeated options */
25 LM_STARTED, /* opts_start_list() succeeded */
d8754f40
LE
26
27 LM_IN_PROGRESS, /* opts_next_list() has been called.
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
93static void
94destroy_list(gpointer list)
95{
96 g_queue_free(list);
97}
98
99
100static void
101opts_visitor_insert(GHashTable *unprocessed_opts, const QemuOpt *opt)
102{
103 GQueue *list;
104
105 list = g_hash_table_lookup(unprocessed_opts, opt->name);
106 if (list == NULL) {
107 list = g_queue_new();
108
109 /* GHashTable will never try to free the keys -- we supply NULL as
110 * "key_destroy_func" in opts_start_struct(). Thus cast away key
111 * const-ness in order to suppress gcc's warning.
112 */
113 g_hash_table_insert(unprocessed_opts, (gpointer)opt->name, list);
114 }
115
116 /* Similarly, destroy_list() doesn't call g_queue_free_full(). */
117 g_queue_push_tail(list, (gpointer)opt);
118}
119
120
121static void
122opts_start_struct(Visitor *v, void **obj, const char *kind,
123 const char *name, size_t size, Error **errp)
124{
125 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
126 const QemuOpt *opt;
127
b7745397
MA
128 if (obj) {
129 *obj = g_malloc0(size > 0 ? size : 1);
130 }
eb7ee2cb
LE
131 if (ov->depth++ > 0) {
132 return;
133 }
134
135 ov->unprocessed_opts = g_hash_table_new_full(&g_str_hash, &g_str_equal,
136 NULL, &destroy_list);
137 QTAILQ_FOREACH(opt, &ov->opts_root->head, next) {
138 /* ensured by qemu-option.c::opts_do_parse() */
139 assert(strcmp(opt->name, "id") != 0);
140
141 opts_visitor_insert(ov->unprocessed_opts, opt);
142 }
143
144 if (ov->opts_root->id != NULL) {
145 ov->fake_id_opt = g_malloc0(sizeof *ov->fake_id_opt);
146
dc8622f2
CL
147 ov->fake_id_opt->name = g_strdup("id");
148 ov->fake_id_opt->str = g_strdup(ov->opts_root->id);
eb7ee2cb
LE
149 opts_visitor_insert(ov->unprocessed_opts, ov->fake_id_opt);
150 }
151}
152
153
154static gboolean
155ghr_true(gpointer ign_key, gpointer ign_value, gpointer ign_user_data)
156{
157 return TRUE;
158}
159
160
161static void
162opts_end_struct(Visitor *v, Error **errp)
163{
164 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
165 GQueue *any;
166
167 if (--ov->depth > 0) {
168 return;
169 }
170
171 /* we should have processed all (distinct) QemuOpt instances */
172 any = g_hash_table_find(ov->unprocessed_opts, &ghr_true, NULL);
173 if (any) {
174 const QemuOpt *first;
175
176 first = g_queue_peek_head(any);
c6bd8c70 177 error_setg(errp, QERR_INVALID_PARAMETER, first->name);
eb7ee2cb
LE
178 }
179 g_hash_table_destroy(ov->unprocessed_opts);
180 ov->unprocessed_opts = NULL;
dc8622f2
CL
181 if (ov->fake_id_opt) {
182 g_free(ov->fake_id_opt->name);
183 g_free(ov->fake_id_opt->str);
184 g_free(ov->fake_id_opt);
185 }
eb7ee2cb
LE
186 ov->fake_id_opt = NULL;
187}
188
189
190static GQueue *
191lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp)
192{
193 GQueue *list;
194
195 list = g_hash_table_lookup(ov->unprocessed_opts, name);
196 if (!list) {
c6bd8c70 197 error_setg(errp, QERR_MISSING_PARAMETER, name);
eb7ee2cb
LE
198 }
199 return list;
200}
201
202
203static void
204opts_start_list(Visitor *v, const char *name, Error **errp)
205{
206 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
207
208 /* we can't traverse a list in a list */
d9570434 209 assert(ov->list_mode == LM_NONE);
eb7ee2cb 210 ov->repeated_opts = lookup_distinct(ov, name, errp);
d9570434
LE
211 if (ov->repeated_opts != NULL) {
212 ov->list_mode = LM_STARTED;
213 }
eb7ee2cb
LE
214}
215
216
217static GenericList *
218opts_next_list(Visitor *v, GenericList **list, Error **errp)
219{
220 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
221 GenericList **link;
222
d9570434
LE
223 switch (ov->list_mode) {
224 case LM_STARTED:
225 ov->list_mode = LM_IN_PROGRESS;
eb7ee2cb 226 link = list;
d9570434
LE
227 break;
228
d8754f40
LE
229 case LM_SIGNED_INTERVAL:
230 case LM_UNSIGNED_INTERVAL:
231 link = &(*list)->next;
232
233 if (ov->list_mode == LM_SIGNED_INTERVAL) {
234 if (ov->range_next.s < ov->range_limit.s) {
235 ++ov->range_next.s;
236 break;
237 }
238 } else if (ov->range_next.u < ov->range_limit.u) {
239 ++ov->range_next.u;
240 break;
241 }
242 ov->list_mode = LM_IN_PROGRESS;
243 /* range has been completed, fall through in order to pop option */
244
d9570434 245 case LM_IN_PROGRESS: {
eb7ee2cb
LE
246 const QemuOpt *opt;
247
248 opt = g_queue_pop_head(ov->repeated_opts);
249 if (g_queue_is_empty(ov->repeated_opts)) {
250 g_hash_table_remove(ov->unprocessed_opts, opt->name);
251 return NULL;
252 }
253 link = &(*list)->next;
d9570434
LE
254 break;
255 }
256
257 default:
258 abort();
eb7ee2cb
LE
259 }
260
261 *link = g_malloc0(sizeof **link);
262 return *link;
263}
264
265
266static void
267opts_end_list(Visitor *v, Error **errp)
268{
269 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
270
d8754f40
LE
271 assert(ov->list_mode == LM_STARTED ||
272 ov->list_mode == LM_IN_PROGRESS ||
273 ov->list_mode == LM_SIGNED_INTERVAL ||
274 ov->list_mode == LM_UNSIGNED_INTERVAL);
eb7ee2cb 275 ov->repeated_opts = NULL;
d9570434 276 ov->list_mode = LM_NONE;
eb7ee2cb
LE
277}
278
279
280static const QemuOpt *
281lookup_scalar(const OptsVisitor *ov, const char *name, Error **errp)
282{
d9570434 283 if (ov->list_mode == LM_NONE) {
eb7ee2cb
LE
284 GQueue *list;
285
286 /* the last occurrence of any QemuOpt takes effect when queried by name
287 */
288 list = lookup_distinct(ov, name, errp);
289 return list ? g_queue_peek_tail(list) : NULL;
290 }
d9570434 291 assert(ov->list_mode == LM_IN_PROGRESS);
eb7ee2cb
LE
292 return g_queue_peek_head(ov->repeated_opts);
293}
294
295
296static void
297processed(OptsVisitor *ov, const char *name)
298{
d9570434 299 if (ov->list_mode == LM_NONE) {
eb7ee2cb 300 g_hash_table_remove(ov->unprocessed_opts, name);
d9570434 301 return;
eb7ee2cb 302 }
d9570434
LE
303 assert(ov->list_mode == LM_IN_PROGRESS);
304 /* do nothing */
eb7ee2cb
LE
305}
306
307
308static void
309opts_type_str(Visitor *v, char **obj, const char *name, Error **errp)
310{
311 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
312 const QemuOpt *opt;
313
314 opt = lookup_scalar(ov, name, errp);
315 if (!opt) {
316 return;
317 }
318 *obj = g_strdup(opt->str ? opt->str : "");
319 processed(ov, name);
320}
321
322
323/* mimics qemu-option.c::parse_option_bool() */
324static void
325opts_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
326{
327 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
328 const QemuOpt *opt;
329
330 opt = lookup_scalar(ov, name, errp);
331 if (!opt) {
332 return;
333 }
334
335 if (opt->str) {
336 if (strcmp(opt->str, "on") == 0 ||
337 strcmp(opt->str, "yes") == 0 ||
338 strcmp(opt->str, "y") == 0) {
339 *obj = true;
340 } else if (strcmp(opt->str, "off") == 0 ||
341 strcmp(opt->str, "no") == 0 ||
342 strcmp(opt->str, "n") == 0) {
343 *obj = false;
344 } else {
c6bd8c70
MA
345 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
346 "on|yes|y|off|no|n");
eb7ee2cb
LE
347 return;
348 }
349 } else {
350 *obj = true;
351 }
352
353 processed(ov, name);
354}
355
356
357static void
358opts_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
359{
360 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
361 const QemuOpt *opt;
362 const char *str;
363 long long val;
364 char *endptr;
365
d8754f40
LE
366 if (ov->list_mode == LM_SIGNED_INTERVAL) {
367 *obj = ov->range_next.s;
368 return;
369 }
370
eb7ee2cb
LE
371 opt = lookup_scalar(ov, name, errp);
372 if (!opt) {
373 return;
374 }
375 str = opt->str ? opt->str : "";
376
1e1c555a
LE
377 /* we've gotten past lookup_scalar() */
378 assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
379
eb7ee2cb
LE
380 errno = 0;
381 val = strtoll(str, &endptr, 0);
1e1c555a
LE
382 if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) {
383 if (*endptr == '\0') {
384 *obj = val;
385 processed(ov, name);
386 return;
387 }
388 if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
389 long long val2;
390
391 str = endptr + 1;
392 val2 = strtoll(str, &endptr, 0);
393 if (errno == 0 && endptr > str && *endptr == '\0' &&
15a849be
LE
394 INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 &&
395 (val > INT64_MAX - OPTS_VISITOR_RANGE_MAX ||
396 val2 < val + OPTS_VISITOR_RANGE_MAX)) {
1e1c555a
LE
397 ov->range_next.s = val;
398 ov->range_limit.s = val2;
399 ov->list_mode = LM_SIGNED_INTERVAL;
400
401 /* as if entering on the top */
402 *obj = ov->range_next.s;
403 return;
404 }
405 }
eb7ee2cb 406 }
c6bd8c70
MA
407 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
408 (ov->list_mode == LM_NONE) ? "an int64 value" :
409 "an int64 value or range");
eb7ee2cb
LE
410}
411
412
413static void
414opts_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
415{
416 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
417 const QemuOpt *opt;
418 const char *str;
62d090e2 419 unsigned long long val;
581a8a80 420 char *endptr;
eb7ee2cb 421
d8754f40
LE
422 if (ov->list_mode == LM_UNSIGNED_INTERVAL) {
423 *obj = ov->range_next.u;
424 return;
425 }
426
eb7ee2cb
LE
427 opt = lookup_scalar(ov, name, errp);
428 if (!opt) {
429 return;
430 }
eb7ee2cb 431 str = opt->str;
eb7ee2cb 432
581a8a80
LE
433 /* we've gotten past lookup_scalar() */
434 assert(ov->list_mode == LM_NONE || ov->list_mode == LM_IN_PROGRESS);
435
436 if (parse_uint(str, &val, &endptr, 0) == 0 && val <= UINT64_MAX) {
437 if (*endptr == '\0') {
438 *obj = val;
439 processed(ov, name);
440 return;
441 }
442 if (*endptr == '-' && ov->list_mode == LM_IN_PROGRESS) {
443 unsigned long long val2;
444
445 str = endptr + 1;
446 if (parse_uint_full(str, &val2, 0) == 0 &&
15a849be
LE
447 val2 <= UINT64_MAX && val <= val2 &&
448 val2 - val < OPTS_VISITOR_RANGE_MAX) {
581a8a80
LE
449 ov->range_next.u = val;
450 ov->range_limit.u = val2;
451 ov->list_mode = LM_UNSIGNED_INTERVAL;
452
453 /* as if entering on the top */
454 *obj = ov->range_next.u;
455 return;
456 }
457 }
eb7ee2cb 458 }
c6bd8c70
MA
459 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
460 (ov->list_mode == LM_NONE) ? "a uint64 value" :
461 "a uint64 value or range");
eb7ee2cb
LE
462}
463
464
465static void
466opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
467{
468 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
469 const QemuOpt *opt;
470 int64_t val;
471 char *endptr;
472
473 opt = lookup_scalar(ov, name, errp);
474 if (!opt) {
475 return;
476 }
477
4677bb40
MAL
478 val = qemu_strtosz_suffix(opt->str ? opt->str : "", &endptr,
479 QEMU_STRTOSZ_DEFSUFFIX_B);
cb45de67 480 if (val < 0 || *endptr) {
c6bd8c70
MA
481 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
482 "a size value representible as a non-negative int64");
eb7ee2cb
LE
483 return;
484 }
cb45de67
AK
485
486 *obj = val;
487 processed(ov, name);
eb7ee2cb
LE
488}
489
490
491static void
5cdc8831 492opts_optional(Visitor *v, bool *present, const char *name)
eb7ee2cb
LE
493{
494 OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
495
496 /* we only support a single mandatory scalar field in a list node */
d9570434 497 assert(ov->list_mode == LM_NONE);
eb7ee2cb
LE
498 *present = (lookup_distinct(ov, name, NULL) != NULL);
499}
500
501
502OptsVisitor *
503opts_visitor_new(const QemuOpts *opts)
504{
505 OptsVisitor *ov;
506
507 ov = g_malloc0(sizeof *ov);
508
509 ov->visitor.start_struct = &opts_start_struct;
510 ov->visitor.end_struct = &opts_end_struct;
511
512 ov->visitor.start_list = &opts_start_list;
513 ov->visitor.next_list = &opts_next_list;
514 ov->visitor.end_list = &opts_end_list;
515
516 /* input_type_enum() covers both "normal" enums and union discriminators.
517 * The union discriminator field is always generated as "type"; it should
518 * match the "type" QemuOpt child of any QemuOpts.
519 *
520 * input_type_enum() will remove the looked-up key from the
521 * "unprocessed_opts" hash even if the lookup fails, because the removal is
522 * done earlier in opts_type_str(). This should be harmless.
523 */
524 ov->visitor.type_enum = &input_type_enum;
525
526 ov->visitor.type_int = &opts_type_int;
527 ov->visitor.type_uint64 = &opts_type_uint64;
528 ov->visitor.type_size = &opts_type_size;
529 ov->visitor.type_bool = &opts_type_bool;
530 ov->visitor.type_str = &opts_type_str;
531
532 /* type_number() is not filled in, but this is not the first visitor to
533 * skip some mandatory methods... */
534
e2cd0f4f 535 ov->visitor.optional = &opts_optional;
eb7ee2cb
LE
536
537 ov->opts_root = opts;
538
539 return ov;
540}
541
542
543void
544opts_visitor_cleanup(OptsVisitor *ov)
545{
546 if (ov->unprocessed_opts != NULL) {
547 g_hash_table_destroy(ov->unprocessed_opts);
548 }
549 g_free(ov->fake_id_opt);
e36c8766 550 g_free(ov);
eb7ee2cb
LE
551}
552
553
554Visitor *
555opts_get_visitor(OptsVisitor *ov)
556{
557 return &ov->visitor;
558}
This page took 0.299797 seconds and 4 git commands to generate.