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