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