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