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