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