]> git.proxmox.com Git - mirror_qemu.git/blob - qapi/qobject-input-visitor.c
1a484d54bee5b735cfe256d8736e902076b445d8
[mirror_qemu.git] / qapi / qobject-input-visitor.c
1 /*
2 * Input Visitor
3 *
4 * Copyright (C) 2012-2017 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/visitor-impl.h"
19 #include "qemu/queue.h"
20 #include "qemu-common.h"
21 #include "qapi/qmp/qjson.h"
22 #include "qapi/qmp/types.h"
23 #include "qapi/qmp/qerror.h"
24 #include "qemu/cutils.h"
25 #include "qemu/option.h"
26
27 typedef struct StackObject {
28 const char *name; /* Name of @obj in its parent, if any */
29 QObject *obj; /* QDict or QList being visited */
30 void *qapi; /* sanity check that caller uses same pointer */
31
32 GHashTable *h; /* If @obj is QDict: unvisited keys */
33 const QListEntry *entry; /* If @obj is QList: unvisited tail */
34 unsigned index; /* If @obj is QList: list index of @entry */
35
36 QSLIST_ENTRY(StackObject) node; /* parent */
37 } StackObject;
38
39 struct QObjectInputVisitor {
40 Visitor visitor;
41
42 /* Root of visit at visitor creation. */
43 QObject *root;
44
45 /* Stack of objects being visited (all entries will be either
46 * QDict or QList). */
47 QSLIST_HEAD(, StackObject) stack;
48
49 GString *errname; /* Accumulator for full_name() */
50 };
51
52 static QObjectInputVisitor *to_qiv(Visitor *v)
53 {
54 return container_of(v, QObjectInputVisitor, visitor);
55 }
56
57 static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name,
58 int n)
59 {
60 StackObject *so;
61 char buf[32];
62
63 if (qiv->errname) {
64 g_string_truncate(qiv->errname, 0);
65 } else {
66 qiv->errname = g_string_new("");
67 }
68
69 QSLIST_FOREACH(so , &qiv->stack, node) {
70 if (n) {
71 n--;
72 } else if (qobject_type(so->obj) == QTYPE_QDICT) {
73 g_string_prepend(qiv->errname, name ?: "<anonymous>");
74 g_string_prepend_c(qiv->errname, '.');
75 } else {
76 snprintf(buf, sizeof(buf), "[%u]", so->index);
77 g_string_prepend(qiv->errname, buf);
78 }
79 name = so->name;
80 }
81 assert(!n);
82
83 if (name) {
84 g_string_prepend(qiv->errname, name);
85 } else if (qiv->errname->str[0] == '.') {
86 g_string_erase(qiv->errname, 0, 1);
87 } else if (!qiv->errname->str[0]) {
88 return "<anonymous>";
89 }
90
91 return qiv->errname->str;
92 }
93
94 static const char *full_name(QObjectInputVisitor *qiv, const char *name)
95 {
96 return full_name_nth(qiv, name, 0);
97 }
98
99 static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv,
100 const char *name,
101 bool consume)
102 {
103 StackObject *tos;
104 QObject *qobj;
105 QObject *ret;
106
107 if (QSLIST_EMPTY(&qiv->stack)) {
108 /* Starting at root, name is ignored. */
109 assert(qiv->root);
110 return qiv->root;
111 }
112
113 /* We are in a container; find the next element. */
114 tos = QSLIST_FIRST(&qiv->stack);
115 qobj = tos->obj;
116 assert(qobj);
117
118 if (qobject_type(qobj) == QTYPE_QDICT) {
119 assert(name);
120 ret = qdict_get(qobject_to_qdict(qobj), name);
121 if (tos->h && consume && ret) {
122 bool removed = g_hash_table_remove(tos->h, name);
123 assert(removed);
124 }
125 } else {
126 assert(qobject_type(qobj) == QTYPE_QLIST);
127 assert(!name);
128 if (tos->entry) {
129 ret = qlist_entry_obj(tos->entry);
130 if (consume) {
131 tos->entry = qlist_next(tos->entry);
132 }
133 } else {
134 ret = NULL;
135 }
136 if (consume) {
137 tos->index++;
138 }
139 }
140
141 return ret;
142 }
143
144 static QObject *qobject_input_get_object(QObjectInputVisitor *qiv,
145 const char *name,
146 bool consume, Error **errp)
147 {
148 QObject *obj = qobject_input_try_get_object(qiv, name, consume);
149
150 if (!obj) {
151 error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name));
152 }
153 return obj;
154 }
155
156 static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv,
157 const char *name,
158 Error **errp)
159 {
160 QObject *qobj;
161 QString *qstr;
162
163 qobj = qobject_input_get_object(qiv, name, true, errp);
164 if (!qobj) {
165 return NULL;
166 }
167
168 qstr = qobject_to_qstring(qobj);
169 if (!qstr) {
170 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
171 full_name(qiv, name), "string");
172 return NULL;
173 }
174
175 return qstring_get_str(qstr);
176 }
177
178 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
179 {
180 GHashTable *h = opaque;
181 g_hash_table_insert(h, (gpointer) key, NULL);
182 }
183
184 static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
185 const char *name,
186 QObject *obj, void *qapi)
187 {
188 GHashTable *h;
189 StackObject *tos = g_new0(StackObject, 1);
190
191 assert(obj);
192 tos->name = name;
193 tos->obj = obj;
194 tos->qapi = qapi;
195
196 if (qobject_type(obj) == QTYPE_QDICT) {
197 h = g_hash_table_new(g_str_hash, g_str_equal);
198 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
199 tos->h = h;
200 } else {
201 assert(qobject_type(obj) == QTYPE_QLIST);
202 tos->entry = qlist_first(qobject_to_qlist(obj));
203 tos->index = -1;
204 }
205
206 QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
207 return tos->entry;
208 }
209
210
211 static void qobject_input_check_struct(Visitor *v, Error **errp)
212 {
213 QObjectInputVisitor *qiv = to_qiv(v);
214 StackObject *tos = QSLIST_FIRST(&qiv->stack);
215 GHashTableIter iter;
216 const char *key;
217
218 assert(tos && !tos->entry);
219
220 g_hash_table_iter_init(&iter, tos->h);
221 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
222 error_setg(errp, "Parameter '%s' is unexpected",
223 full_name(qiv, key));
224 }
225 }
226
227 static void qobject_input_stack_object_free(StackObject *tos)
228 {
229 if (tos->h) {
230 g_hash_table_unref(tos->h);
231 }
232
233 g_free(tos);
234 }
235
236 static void qobject_input_pop(Visitor *v, void **obj)
237 {
238 QObjectInputVisitor *qiv = to_qiv(v);
239 StackObject *tos = QSLIST_FIRST(&qiv->stack);
240
241 assert(tos && tos->qapi == obj);
242 QSLIST_REMOVE_HEAD(&qiv->stack, node);
243 qobject_input_stack_object_free(tos);
244 }
245
246 static void qobject_input_start_struct(Visitor *v, const char *name, void **obj,
247 size_t size, Error **errp)
248 {
249 QObjectInputVisitor *qiv = to_qiv(v);
250 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
251
252 if (obj) {
253 *obj = NULL;
254 }
255 if (!qobj) {
256 return;
257 }
258 if (qobject_type(qobj) != QTYPE_QDICT) {
259 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
260 full_name(qiv, name), "object");
261 return;
262 }
263
264 qobject_input_push(qiv, name, qobj, obj);
265
266 if (obj) {
267 *obj = g_malloc0(size);
268 }
269 }
270
271
272 static void qobject_input_start_list(Visitor *v, const char *name,
273 GenericList **list, size_t size,
274 Error **errp)
275 {
276 QObjectInputVisitor *qiv = to_qiv(v);
277 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
278 const QListEntry *entry;
279
280 if (list) {
281 *list = NULL;
282 }
283 if (!qobj) {
284 return;
285 }
286 if (qobject_type(qobj) != QTYPE_QLIST) {
287 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
288 full_name(qiv, name), "array");
289 return;
290 }
291
292 entry = qobject_input_push(qiv, name, qobj, list);
293 if (entry && list) {
294 *list = g_malloc0(size);
295 }
296 }
297
298 static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
299 size_t size)
300 {
301 QObjectInputVisitor *qiv = to_qiv(v);
302 StackObject *tos = QSLIST_FIRST(&qiv->stack);
303
304 assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
305
306 if (!tos->entry) {
307 return NULL;
308 }
309 tail->next = g_malloc0(size);
310 return tail->next;
311 }
312
313 static void qobject_input_check_list(Visitor *v, Error **errp)
314 {
315 QObjectInputVisitor *qiv = to_qiv(v);
316 StackObject *tos = QSLIST_FIRST(&qiv->stack);
317
318 assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
319
320 if (tos->entry) {
321 error_setg(errp, "Only %u list elements expected in %s",
322 tos->index + 1, full_name_nth(qiv, NULL, 1));
323 }
324 }
325
326
327 static void qobject_input_start_alternate(Visitor *v, const char *name,
328 GenericAlternate **obj, size_t size,
329 bool promote_int, Error **errp)
330 {
331 QObjectInputVisitor *qiv = to_qiv(v);
332 QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
333
334 if (!qobj) {
335 *obj = NULL;
336 return;
337 }
338 *obj = g_malloc0(size);
339 (*obj)->type = qobject_type(qobj);
340 if (promote_int && (*obj)->type == QTYPE_QINT) {
341 (*obj)->type = QTYPE_QFLOAT;
342 }
343 }
344
345 static void qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
346 Error **errp)
347 {
348 QObjectInputVisitor *qiv = to_qiv(v);
349 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
350 QInt *qint;
351
352 if (!qobj) {
353 return;
354 }
355 qint = qobject_to_qint(qobj);
356 if (!qint) {
357 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
358 full_name(qiv, name), "integer");
359 return;
360 }
361
362 *obj = qint_get_int(qint);
363 }
364
365
366 static void qobject_input_type_int64_keyval(Visitor *v, const char *name,
367 int64_t *obj, Error **errp)
368 {
369 QObjectInputVisitor *qiv = to_qiv(v);
370 const char *str = qobject_input_get_keyval(qiv, name, errp);
371
372 if (!str) {
373 return;
374 }
375
376 if (qemu_strtoi64(str, NULL, 0, obj) < 0) {
377 /* TODO report -ERANGE more nicely */
378 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
379 full_name(qiv, name), "integer");
380 }
381 }
382
383 static void qobject_input_type_uint64(Visitor *v, const char *name,
384 uint64_t *obj, Error **errp)
385 {
386 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
387 QObjectInputVisitor *qiv = to_qiv(v);
388 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
389 QInt *qint;
390
391 if (!qobj) {
392 return;
393 }
394 qint = qobject_to_qint(qobj);
395 if (!qint) {
396 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
397 full_name(qiv, name), "integer");
398 return;
399 }
400
401 *obj = qint_get_int(qint);
402 }
403
404 static void qobject_input_type_uint64_keyval(Visitor *v, const char *name,
405 uint64_t *obj, Error **errp)
406 {
407 QObjectInputVisitor *qiv = to_qiv(v);
408 const char *str = qobject_input_get_keyval(qiv, name, errp);
409
410 if (!str) {
411 return;
412 }
413
414 if (qemu_strtou64(str, NULL, 0, obj) < 0) {
415 /* TODO report -ERANGE more nicely */
416 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
417 full_name(qiv, name), "integer");
418 }
419 }
420
421 static void qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
422 Error **errp)
423 {
424 QObjectInputVisitor *qiv = to_qiv(v);
425 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
426 QBool *qbool;
427
428 if (!qobj) {
429 return;
430 }
431 qbool = qobject_to_qbool(qobj);
432 if (!qbool) {
433 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
434 full_name(qiv, name), "boolean");
435 return;
436 }
437
438 *obj = qbool_get_bool(qbool);
439 }
440
441 static void qobject_input_type_bool_keyval(Visitor *v, const char *name,
442 bool *obj, Error **errp)
443 {
444 QObjectInputVisitor *qiv = to_qiv(v);
445 const char *str = qobject_input_get_keyval(qiv, name, errp);
446
447 if (!str) {
448 return;
449 }
450
451 if (!strcmp(str, "on")) {
452 *obj = true;
453 } else if (!strcmp(str, "off")) {
454 *obj = false;
455 } else {
456 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
457 full_name(qiv, name), "'on' or 'off'");
458 }
459 }
460
461 static void qobject_input_type_str(Visitor *v, const char *name, char **obj,
462 Error **errp)
463 {
464 QObjectInputVisitor *qiv = to_qiv(v);
465 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
466 QString *qstr;
467
468 *obj = NULL;
469 if (!qobj) {
470 return;
471 }
472 qstr = qobject_to_qstring(qobj);
473 if (!qstr) {
474 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
475 full_name(qiv, name), "string");
476 return;
477 }
478
479 *obj = g_strdup(qstring_get_str(qstr));
480 }
481
482 static void qobject_input_type_number(Visitor *v, const char *name, double *obj,
483 Error **errp)
484 {
485 QObjectInputVisitor *qiv = to_qiv(v);
486 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
487 QInt *qint;
488 QFloat *qfloat;
489
490 if (!qobj) {
491 return;
492 }
493 qint = qobject_to_qint(qobj);
494 if (qint) {
495 *obj = qint_get_int(qobject_to_qint(qobj));
496 return;
497 }
498
499 qfloat = qobject_to_qfloat(qobj);
500 if (qfloat) {
501 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
502 return;
503 }
504
505 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
506 full_name(qiv, name), "number");
507 }
508
509 static void qobject_input_type_number_keyval(Visitor *v, const char *name,
510 double *obj, Error **errp)
511 {
512 QObjectInputVisitor *qiv = to_qiv(v);
513 const char *str = qobject_input_get_keyval(qiv, name, errp);
514 char *endp;
515
516 if (!str) {
517 return;
518 }
519
520 errno = 0;
521 *obj = strtod(str, &endp);
522 if (errno || endp == str || *endp) {
523 /* TODO report -ERANGE more nicely */
524 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
525 full_name(qiv, name), "number");
526 }
527 }
528
529 static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
530 Error **errp)
531 {
532 QObjectInputVisitor *qiv = to_qiv(v);
533 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
534
535 *obj = NULL;
536 if (!qobj) {
537 return;
538 }
539
540 qobject_incref(qobj);
541 *obj = qobj;
542 }
543
544 static void qobject_input_type_null(Visitor *v, const char *name, Error **errp)
545 {
546 QObjectInputVisitor *qiv = to_qiv(v);
547 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
548
549 if (!qobj) {
550 return;
551 }
552
553 if (qobject_type(qobj) != QTYPE_QNULL) {
554 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
555 full_name(qiv, name), "null");
556 }
557 }
558
559 static void qobject_input_type_size_keyval(Visitor *v, const char *name,
560 uint64_t *obj, Error **errp)
561 {
562 QObjectInputVisitor *qiv = to_qiv(v);
563 const char *str = qobject_input_get_keyval(qiv, name, errp);
564
565 if (!str) {
566 return;
567 }
568
569 if (qemu_strtosz(str, NULL, obj) < 0) {
570 /* TODO report -ERANGE more nicely */
571 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
572 full_name(qiv, name), "size");
573 }
574 }
575
576 static void qobject_input_optional(Visitor *v, const char *name, bool *present)
577 {
578 QObjectInputVisitor *qiv = to_qiv(v);
579 QObject *qobj = qobject_input_try_get_object(qiv, name, false);
580
581 if (!qobj) {
582 *present = false;
583 return;
584 }
585
586 *present = true;
587 }
588
589 static void qobject_input_free(Visitor *v)
590 {
591 QObjectInputVisitor *qiv = to_qiv(v);
592
593 while (!QSLIST_EMPTY(&qiv->stack)) {
594 StackObject *tos = QSLIST_FIRST(&qiv->stack);
595
596 QSLIST_REMOVE_HEAD(&qiv->stack, node);
597 qobject_input_stack_object_free(tos);
598 }
599
600 qobject_decref(qiv->root);
601 if (qiv->errname) {
602 g_string_free(qiv->errname, TRUE);
603 }
604 g_free(qiv);
605 }
606
607 static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
608 {
609 QObjectInputVisitor *v = g_malloc0(sizeof(*v));
610
611 assert(obj);
612
613 v->visitor.type = VISITOR_INPUT;
614 v->visitor.start_struct = qobject_input_start_struct;
615 v->visitor.check_struct = qobject_input_check_struct;
616 v->visitor.end_struct = qobject_input_pop;
617 v->visitor.start_list = qobject_input_start_list;
618 v->visitor.next_list = qobject_input_next_list;
619 v->visitor.check_list = qobject_input_check_list;
620 v->visitor.end_list = qobject_input_pop;
621 v->visitor.start_alternate = qobject_input_start_alternate;
622 v->visitor.optional = qobject_input_optional;
623 v->visitor.free = qobject_input_free;
624
625 v->root = obj;
626 qobject_incref(obj);
627
628 return v;
629 }
630
631 Visitor *qobject_input_visitor_new(QObject *obj)
632 {
633 QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
634
635 v->visitor.type_int64 = qobject_input_type_int64;
636 v->visitor.type_uint64 = qobject_input_type_uint64;
637 v->visitor.type_bool = qobject_input_type_bool;
638 v->visitor.type_str = qobject_input_type_str;
639 v->visitor.type_number = qobject_input_type_number;
640 v->visitor.type_any = qobject_input_type_any;
641 v->visitor.type_null = qobject_input_type_null;
642
643 return &v->visitor;
644 }
645
646 Visitor *qobject_input_visitor_new_keyval(QObject *obj)
647 {
648 QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
649
650 v->visitor.type_int64 = qobject_input_type_int64_keyval;
651 v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
652 v->visitor.type_bool = qobject_input_type_bool_keyval;
653 v->visitor.type_str = qobject_input_type_str;
654 v->visitor.type_number = qobject_input_type_number_keyval;
655 v->visitor.type_any = qobject_input_type_any;
656 v->visitor.type_null = qobject_input_type_null;
657 v->visitor.type_size = qobject_input_type_size_keyval;
658
659 return &v->visitor;
660 }
661
662 Visitor *qobject_input_visitor_new_str(const char *str,
663 const char *implied_key,
664 Error **errp)
665 {
666 bool is_json = str[0] == '{';
667 QObject *obj;
668 QDict *args;
669 Visitor *v;
670
671 if (is_json) {
672 obj = qobject_from_json(str, errp);
673 if (!obj) {
674 /* Work around qobject_from_json() lossage TODO fix that */
675 if (errp && !*errp) {
676 error_setg(errp, "JSON parse error");
677 return NULL;
678 }
679 return NULL;
680 }
681 args = qobject_to_qdict(obj);
682 assert(args);
683 v = qobject_input_visitor_new(QOBJECT(args));
684 } else {
685 args = keyval_parse(str, implied_key, errp);
686 if (!args) {
687 return NULL;
688 }
689 v = qobject_input_visitor_new_keyval(QOBJECT(args));
690 }
691 QDECREF(args);
692
693 return v;
694 }