blob: f0b4c7ca9d399fc420b5ab63272062494e2f0594 [file] [log] [blame]
Michael Rothc40cc0a2011-07-19 14:50:33 -05001/*
2 * Input Visitor
3 *
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +01004 * Copyright (C) 2012-2017 Red Hat, Inc.
Michael Rothc40cc0a2011-07-19 14:50:33 -05005 * 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
Peter Maydellcbf21152016-01-29 17:49:57 +000015#include "qemu/osdep.h"
Markus Armbruster5891c382017-05-22 18:42:12 +020016#include <math.h>
Markus Armbruster7ce5fc62021-10-28 12:25:19 +020017#include "qapi/compat-policy.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.h"
Daniel P. Berrangeb3db2112016-09-30 15:45:27 +010019#include "qapi/qobject-input-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010020#include "qapi/visitor-impl.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/queue.h"
Markus Armbruster9d1eab42017-02-28 22:27:06 +010022#include "qapi/qmp/qjson.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010023#include "qapi/qmp/qbool.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024#include "qapi/qmp/qdict.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010025#include "qapi/qmp/qerror.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010026#include "qapi/qmp/qlist.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010027#include "qapi/qmp/qnull.h"
28#include "qapi/qmp/qnum.h"
Markus Armbrusterfc81fa12018-02-01 12:18:40 +010029#include "qapi/qmp/qstring.h"
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +010030#include "qemu/cutils.h"
Markus Armbruster9d1eab42017-02-28 22:27:06 +010031#include "qemu/option.h"
Michael Rothc40cc0a2011-07-19 14:50:33 -050032
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010033typedef struct StackObject {
34 const char *name; /* Name of @obj in its parent, if any */
35 QObject *obj; /* QDict or QList being visited */
Eric Blake1158bb22016-06-09 10:48:34 -060036 void *qapi; /* sanity check that caller uses same pointer */
Eric Blakeb471d012016-04-28 15:45:12 -060037
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010038 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 */
Paolo Bonzini3d344c22016-07-07 17:53:18 +020041
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010042 QSLIST_ENTRY(StackObject) node; /* parent */
Michael Rothc40cc0a2011-07-19 14:50:33 -050043} StackObject;
44
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010045struct QObjectInputVisitor {
Michael Rothc40cc0a2011-07-19 14:50:33 -050046 Visitor visitor;
Eric Blakeb471d012016-04-28 15:45:12 -060047
Eric Blakece140b12016-04-28 15:45:18 -060048 /* Root of visit at visitor creation. */
49 QObject *root;
Markus Armbruster0b2c1be2017-02-28 22:27:10 +010050 bool keyval; /* Assume @root made with keyval_parse() */
Eric Blakece140b12016-04-28 15:45:18 -060051
52 /* Stack of objects being visited (all entries will be either
53 * QDict or QList). */
Paolo Bonzini3d344c22016-07-07 17:53:18 +020054 QSLIST_HEAD(, StackObject) stack;
Eric Blakeb471d012016-04-28 15:45:12 -060055
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010056 GString *errname; /* Accumulator for full_name() */
Michael Rothc40cc0a2011-07-19 14:50:33 -050057};
58
Daniel P. Berrange09e68362016-09-30 15:45:27 +010059static QObjectInputVisitor *to_qiv(Visitor *v)
Michael Rothc40cc0a2011-07-19 14:50:33 -050060{
Daniel P. Berrange09e68362016-09-30 15:45:27 +010061 return container_of(v, QObjectInputVisitor, visitor);
Michael Rothc40cc0a2011-07-19 14:50:33 -050062}
63
Markus Armbruster6c022582017-04-27 10:41:24 +020064/*
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 */
Markus Armbrustera4a1c702017-03-03 13:32:45 +010075static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name,
76 int n)
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010077{
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) {
Markus Armbrustera4a1c702017-03-03 13:32:45 +010088 if (n) {
89 n--;
90 } else if (qobject_type(so->obj) == QTYPE_QDICT) {
91 g_string_prepend(qiv->errname, name ?: "<anonymous>");
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010092 g_string_prepend_c(qiv->errname, '.');
93 } else {
Markus Armbruster0b2c1be2017-02-28 22:27:10 +010094 snprintf(buf, sizeof(buf),
95 qiv->keyval ? ".%u" : "[%u]",
96 so->index);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +010097 g_string_prepend(qiv->errname, buf);
98 }
99 name = so->name;
100 }
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100101 assert(!n);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100102
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);
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100107 } else if (!qiv->errname->str[0]) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100108 return "<anonymous>";
109 }
110
111 return qiv->errname->str;
112}
113
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100114static const char *full_name(QObjectInputVisitor *qiv, const char *name)
115{
116 return full_name_nth(qiv, name, 0);
117}
118
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100119static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv,
120 const char *name,
121 bool consume)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500122{
Eric Blakece140b12016-04-28 15:45:18 -0600123 StackObject *tos;
124 QObject *qobj;
Eric Blakee5826a22016-04-28 15:45:15 -0600125 QObject *ret;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500126
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200127 if (QSLIST_EMPTY(&qiv->stack)) {
Eric Blakece140b12016-04-28 15:45:18 -0600128 /* Starting at root, name is ignored. */
Marc-André Lureau5d0cbbc2016-09-30 13:59:46 +0400129 assert(qiv->root);
Eric Blakece140b12016-04-28 15:45:18 -0600130 return qiv->root;
131 }
132
133 /* We are in a container; find the next element. */
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200134 tos = QSLIST_FIRST(&qiv->stack);
Eric Blakece140b12016-04-28 15:45:18 -0600135 qobj = tos->obj;
Eric Blakeb471d012016-04-28 15:45:12 -0600136 assert(qobj);
137
Eric Blakece140b12016-04-28 15:45:18 -0600138 if (qobject_type(qobj) == QTYPE_QDICT) {
139 assert(name);
Max Reitz7dc847e2018-02-24 16:40:29 +0100140 ret = qdict_get(qobject_to(QDict, qobj), name);
Eric Blakee5826a22016-04-28 15:45:15 -0600141 if (tos->h && consume && ret) {
142 bool removed = g_hash_table_remove(tos->h, name);
143 assert(removed);
Paolo Bonzini47c6d3e2011-12-18 17:05:04 +0100144 }
Eric Blakece140b12016-04-28 15:45:18 -0600145 } else {
Eric Blakeb471d012016-04-28 15:45:12 -0600146 assert(qobject_type(qobj) == QTYPE_QLIST);
Eric Blakece140b12016-04-28 15:45:18 -0600147 assert(!name);
Markus Armbruster1f41a642017-03-03 13:32:47 +0100148 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 }
Eric Blakefcf3cb22016-04-28 15:45:19 -0600156 if (consume) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100157 tos->index++;
Eric Blakefcf3cb22016-04-28 15:45:19 -0600158 }
Eric Blakeb471d012016-04-28 15:45:12 -0600159 }
160
Eric Blakece140b12016-04-28 15:45:18 -0600161 return ret;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500162}
163
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100164static 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
Markus Armbrustere3934b42017-02-28 22:26:53 +0100176static 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
Max Reitz7dc847e2018-02-24 16:40:29 +0100188 qstr = qobject_to(QString, qobj);
Markus Armbrustere3934b42017-02-28 22:26:53 +0100189 if (!qstr) {
Markus Armbruster31478f22017-02-28 22:27:08 +0100190 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 }
Markus Armbrustere3934b42017-02-28 22:26:53 +0100202 }
203
204 return qstring_get_str(qstr);
205}
206
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100207static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100208 const char *name,
Markus Armbrusterb8874fb2017-03-03 13:32:32 +0100209 QObject *obj, void *qapi)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500210{
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100211 GHashTable *h;
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200212 StackObject *tos = g_new0(StackObject, 1);
Markus Armbruster7b1cd1c2020-04-15 10:30:47 +0200213 QDict *qdict = qobject_to(QDict, obj);
214 QList *qlist = qobject_to(QList, obj);
215 const QDictEntry *entry;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500216
Eric Blakeb471d012016-04-28 15:45:12 -0600217 assert(obj);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100218 tos->name = name;
Eric Blakeb471d012016-04-28 15:45:12 -0600219 tos->obj = obj;
Eric Blake1158bb22016-06-09 10:48:34 -0600220 tos->qapi = qapi;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100221
Markus Armbruster7b1cd1c2020-04-15 10:30:47 +0200222 if (qdict) {
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100223 h = g_hash_table_new(g_str_hash, g_str_equal);
Markus Armbruster7b1cd1c2020-04-15 10:30:47 +0200224 for (entry = qdict_first(qdict);
225 entry;
226 entry = qdict_next(qdict, entry)) {
227 g_hash_table_insert(h, (void *)qdict_entry_key(entry), NULL);
228 }
Eric Blakeb471d012016-04-28 15:45:12 -0600229 tos->h = h;
Markus Armbruster048abb72017-03-03 13:32:39 +0100230 } else {
Markus Armbruster7b1cd1c2020-04-15 10:30:47 +0200231 assert(qlist);
232 tos->entry = qlist_first(qlist);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100233 tos->index = -1;
Paolo Bonzinie38ac962012-03-22 12:51:10 +0100234 }
235
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200236 QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
Eric Blaked9f62dd2016-04-28 15:45:31 -0600237 return tos->entry;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500238}
239
NODA, Kai57a33d82012-04-21 22:41:27 +0900240
Markus Armbruster012d4c92020-07-07 18:05:45 +0200241static bool qobject_input_check_struct(Visitor *v, Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500242{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100243 QObjectInputVisitor *qiv = to_qiv(v);
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200244 StackObject *tos = QSLIST_FIRST(&qiv->stack);
Markus Armbruster048abb72017-03-03 13:32:39 +0100245 GHashTableIter iter;
246 const char *key;
Eric Blake15c2f662016-04-28 15:45:27 -0600247
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200248 assert(tos && !tos->entry);
Eric Blakef96493b2016-02-17 23:48:15 -0700249
Markus Armbruster048abb72017-03-03 13:32:39 +0100250 g_hash_table_iter_init(&iter, tos->h);
251 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
252 error_setg(errp, "Parameter '%s' is unexpected",
253 full_name(qiv, key));
Markus Armbruster012d4c92020-07-07 18:05:45 +0200254 return false;
Eric Blake15c2f662016-04-28 15:45:27 -0600255 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200256 return true;
Eric Blake15c2f662016-04-28 15:45:27 -0600257}
258
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100259static void qobject_input_stack_object_free(StackObject *tos)
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200260{
261 if (tos->h) {
262 g_hash_table_unref(tos->h);
263 }
264
265 g_free(tos);
266}
267
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100268static void qobject_input_pop(Visitor *v, void **obj)
Eric Blake15c2f662016-04-28 15:45:27 -0600269{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100270 QObjectInputVisitor *qiv = to_qiv(v);
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200271 StackObject *tos = QSLIST_FIRST(&qiv->stack);
Eric Blake15c2f662016-04-28 15:45:27 -0600272
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200273 assert(tos && tos->qapi == obj);
274 QSLIST_REMOVE_HEAD(&qiv->stack, node);
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100275 qobject_input_stack_object_free(tos);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500276}
277
Markus Armbruster012d4c92020-07-07 18:05:45 +0200278static bool qobject_input_start_struct(Visitor *v, const char *name, void **obj,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100279 size_t size, Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500280{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100281 QObjectInputVisitor *qiv = to_qiv(v);
282 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500283
Eric Blakee58d6952016-04-28 15:45:10 -0600284 if (obj) {
285 *obj = NULL;
286 }
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400287 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200288 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400289 }
290 if (qobject_type(qobj) != QTYPE_QDICT) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100291 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
292 full_name(qiv, name), "object");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200293 return false;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500294 }
295
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100296 qobject_input_push(qiv, name, qobj, obj);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500297
298 if (obj) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500299 *obj = g_malloc0(size);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500300 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200301 return true;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500302}
303
Markus Armbruster8b2e41d2017-04-27 10:41:26 +0200304static void qobject_input_end_struct(Visitor *v, void **obj)
305{
306 QObjectInputVisitor *qiv = to_qiv(v);
307 StackObject *tos = QSLIST_FIRST(&qiv->stack);
308
309 assert(qobject_type(tos->obj) == QTYPE_QDICT && tos->h);
310 qobject_input_pop(v, obj);
311}
312
Michael Rothc40cc0a2011-07-19 14:50:33 -0500313
Markus Armbruster012d4c92020-07-07 18:05:45 +0200314static bool qobject_input_start_list(Visitor *v, const char *name,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100315 GenericList **list, size_t size,
316 Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500317{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100318 QObjectInputVisitor *qiv = to_qiv(v);
319 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Eric Blaked9f62dd2016-04-28 15:45:31 -0600320 const QListEntry *entry;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500321
Markus Armbruster58561c22017-03-03 13:32:33 +0100322 if (list) {
323 *list = NULL;
324 }
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400325 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200326 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400327 }
328 if (qobject_type(qobj) != QTYPE_QLIST) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100329 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
330 full_name(qiv, name), "array");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200331 return false;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500332 }
333
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100334 entry = qobject_input_push(qiv, name, qobj, list);
Markus Armbruster58561c22017-03-03 13:32:33 +0100335 if (entry && list) {
336 *list = g_malloc0(size);
Eric Blaked9f62dd2016-04-28 15:45:31 -0600337 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200338 return true;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500339}
340
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100341static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
342 size_t size)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500343{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100344 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100345 StackObject *tos = QSLIST_FIRST(&qiv->stack);
Paolo Bonzini3a86a0f2012-03-22 22:38:40 +0100346
Max Reitz532fb532018-03-10 16:14:36 -0600347 assert(tos && qobject_to(QList, tos->obj));
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100348
349 if (!tos->entry) {
Michael Rothc40cc0a2011-07-19 14:50:33 -0500350 return NULL;
351 }
Eric Blaked9f62dd2016-04-28 15:45:31 -0600352 tail->next = g_malloc0(size);
353 return tail->next;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500354}
355
Markus Armbruster012d4c92020-07-07 18:05:45 +0200356static bool qobject_input_check_list(Visitor *v, Error **errp)
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100357{
358 QObjectInputVisitor *qiv = to_qiv(v);
359 StackObject *tos = QSLIST_FIRST(&qiv->stack);
360
Max Reitz532fb532018-03-10 16:14:36 -0600361 assert(tos && qobject_to(QList, tos->obj));
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100362
363 if (tos->entry) {
364 error_setg(errp, "Only %u list elements expected in %s",
365 tos->index + 1, full_name_nth(qiv, NULL, 1));
Markus Armbruster012d4c92020-07-07 18:05:45 +0200366 return false;
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100367 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200368 return true;
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100369}
370
Markus Armbruster8b2e41d2017-04-27 10:41:26 +0200371static void qobject_input_end_list(Visitor *v, void **obj)
372{
373 QObjectInputVisitor *qiv = to_qiv(v);
374 StackObject *tos = QSLIST_FIRST(&qiv->stack);
375
376 assert(qobject_type(tos->obj) == QTYPE_QLIST && !tos->h);
377 qobject_input_pop(v, obj);
378}
Michael Rothc40cc0a2011-07-19 14:50:33 -0500379
Markus Armbruster012d4c92020-07-07 18:05:45 +0200380static bool qobject_input_start_alternate(Visitor *v, const char *name,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100381 GenericAlternate **obj, size_t size,
Marc-André Lureau60390d22017-06-07 20:35:59 +0400382 Error **errp)
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200383{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100384 QObjectInputVisitor *qiv = to_qiv(v);
385 QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200386
387 if (!qobj) {
Eric Blakedbf11922016-02-17 23:48:29 -0700388 *obj = NULL;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200389 return false;
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200390 }
Eric Blakedbf11922016-02-17 23:48:29 -0700391 *obj = g_malloc0(size);
392 (*obj)->type = qobject_type(qobj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200393 return true;
Kevin Wolf69dd62d2013-07-08 16:14:21 +0200394}
395
Markus Armbruster012d4c92020-07-07 18:05:45 +0200396static bool qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100397 Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500398{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100399 QObjectInputVisitor *qiv = to_qiv(v);
400 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400401 QNum *qnum;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500402
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400403 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200404 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400405 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100406 qnum = qobject_to(QNum, qobj);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400407 if (!qnum || !qnum_get_try_int(qnum, obj)) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100408 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
409 full_name(qiv, name), "integer");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200410 return false;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500411 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200412 return true;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500413}
414
Markus Armbruster012d4c92020-07-07 18:05:45 +0200415static bool qobject_input_type_int64_keyval(Visitor *v, const char *name,
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100416 int64_t *obj, Error **errp)
417{
418 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustere3934b42017-02-28 22:26:53 +0100419 const char *str = qobject_input_get_keyval(qiv, name, errp);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100420
Markus Armbrustere3934b42017-02-28 22:26:53 +0100421 if (!str) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200422 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100423 }
424
Markus Armbrustere3934b42017-02-28 22:26:53 +0100425 if (qemu_strtoi64(str, NULL, 0, obj) < 0) {
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100426 /* TODO report -ERANGE more nicely */
427 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
428 full_name(qiv, name), "integer");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200429 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100430 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200431 return true;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100432}
433
Markus Armbruster012d4c92020-07-07 18:05:45 +0200434static bool qobject_input_type_uint64(Visitor *v, const char *name,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100435 uint64_t *obj, Error **errp)
Eric Blakef755dea2016-01-29 06:48:50 -0700436{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100437 QObjectInputVisitor *qiv = to_qiv(v);
438 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400439 QNum *qnum;
440 int64_t val;
Eric Blakef755dea2016-01-29 06:48:50 -0700441
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400442 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200443 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400444 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100445 qnum = qobject_to(QNum, qobj);
Marc-André Lureau5923f852017-06-07 20:36:03 +0400446 if (!qnum) {
447 goto err;
Eric Blakef755dea2016-01-29 06:48:50 -0700448 }
Marc-André Lureau5923f852017-06-07 20:36:03 +0400449
450 if (qnum_get_try_uint(qnum, obj)) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200451 return true;
Marc-André Lureau5923f852017-06-07 20:36:03 +0400452 }
453
454 /* Need to accept negative values for backward compatibility */
455 if (qnum_get_try_int(qnum, &val)) {
456 *obj = val;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200457 return true;
Marc-André Lureau5923f852017-06-07 20:36:03 +0400458 }
459
460err:
461 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
462 full_name(qiv, name), "uint64");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200463 return false;
Eric Blakef755dea2016-01-29 06:48:50 -0700464}
465
Markus Armbruster012d4c92020-07-07 18:05:45 +0200466static bool qobject_input_type_uint64_keyval(Visitor *v, const char *name,
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100467 uint64_t *obj, Error **errp)
468{
469 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustere3934b42017-02-28 22:26:53 +0100470 const char *str = qobject_input_get_keyval(qiv, name, errp);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100471
Markus Armbrustere3934b42017-02-28 22:26:53 +0100472 if (!str) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200473 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100474 }
475
Markus Armbrustere3934b42017-02-28 22:26:53 +0100476 if (qemu_strtou64(str, NULL, 0, obj) < 0) {
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100477 /* TODO report -ERANGE more nicely */
478 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
479 full_name(qiv, name), "integer");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200480 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100481 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200482 return true;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100483}
484
Markus Armbruster012d4c92020-07-07 18:05:45 +0200485static bool qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100486 Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500487{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100488 QObjectInputVisitor *qiv = to_qiv(v);
489 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400490 QBool *qbool;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500491
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400492 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200493 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400494 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100495 qbool = qobject_to(QBool, qobj);
Markus Armbruster14b61602015-10-15 16:15:33 +0200496 if (!qbool) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100497 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
498 full_name(qiv, name), "boolean");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200499 return false;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500500 }
501
Markus Armbruster14b61602015-10-15 16:15:33 +0200502 *obj = qbool_get_bool(qbool);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200503 return true;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500504}
505
Markus Armbruster012d4c92020-07-07 18:05:45 +0200506static bool qobject_input_type_bool_keyval(Visitor *v, const char *name,
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100507 bool *obj, Error **errp)
508{
509 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustere3934b42017-02-28 22:26:53 +0100510 const char *str = qobject_input_get_keyval(qiv, name, errp);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100511
Markus Armbrustere3934b42017-02-28 22:26:53 +0100512 if (!str) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200513 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100514 }
515
Paolo Bonzini372bcb22020-11-03 11:13:39 -0500516 if (!qapi_bool_parse(name, str, obj, NULL)) {
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100517 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
518 full_name(qiv, name), "'on' or 'off'");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200519 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100520 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200521 return true;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100522}
523
Markus Armbruster012d4c92020-07-07 18:05:45 +0200524static bool qobject_input_type_str(Visitor *v, const char *name, char **obj,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100525 Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500526{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100527 QObjectInputVisitor *qiv = to_qiv(v);
528 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400529 QString *qstr;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500530
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400531 *obj = NULL;
532 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200533 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400534 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100535 qstr = qobject_to(QString, qobj);
Markus Armbruster7f027842015-10-15 16:15:37 +0200536 if (!qstr) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100537 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
538 full_name(qiv, name), "string");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200539 return false;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500540 }
541
Markus Armbruster7f027842015-10-15 16:15:37 +0200542 *obj = g_strdup(qstring_get_str(qstr));
Markus Armbruster012d4c92020-07-07 18:05:45 +0200543 return true;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500544}
545
Markus Armbruster012d4c92020-07-07 18:05:45 +0200546static bool qobject_input_type_str_keyval(Visitor *v, const char *name,
Markus Armbruster31478f22017-02-28 22:27:08 +0100547 char **obj, Error **errp)
548{
549 QObjectInputVisitor *qiv = to_qiv(v);
550 const char *str = qobject_input_get_keyval(qiv, name, errp);
551
552 *obj = g_strdup(str);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200553 return !!str;
Markus Armbruster31478f22017-02-28 22:27:08 +0100554}
555
Markus Armbruster012d4c92020-07-07 18:05:45 +0200556static bool qobject_input_type_number(Visitor *v, const char *name, double *obj,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100557 Error **errp)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500558{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100559 QObjectInputVisitor *qiv = to_qiv(v);
560 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400561 QNum *qnum;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500562
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400563 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200564 return false;
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400565 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100566 qnum = qobject_to(QNum, qobj);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400567 if (!qnum) {
Marc-André Lureau58634042017-06-07 20:35:57 +0400568 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
569 full_name(qiv, name), "number");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200570 return false;
Michael Roth1ee51872012-05-11 12:43:24 -0500571 }
Markus Armbrusterfcf73f62015-10-15 16:15:35 +0200572
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +0400573 *obj = qnum_get_double(qnum);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200574 return true;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500575}
576
Markus Armbruster012d4c92020-07-07 18:05:45 +0200577static bool qobject_input_type_number_keyval(Visitor *v, const char *name,
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100578 double *obj, Error **errp)
579{
580 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustere3934b42017-02-28 22:26:53 +0100581 const char *str = qobject_input_get_keyval(qiv, name, errp);
David Hildenbrande08a5242018-11-21 17:44:16 +0100582 double val;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100583
Markus Armbrustere3934b42017-02-28 22:26:53 +0100584 if (!str) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200585 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100586 }
587
David Hildenbrande08a5242018-11-21 17:44:16 +0100588 if (qemu_strtod_finite(str, NULL, &val)) {
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100589 /* TODO report -ERANGE more nicely */
590 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
591 full_name(qiv, name), "number");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200592 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100593 }
David Hildenbrande08a5242018-11-21 17:44:16 +0100594
595 *obj = val;
Markus Armbruster012d4c92020-07-07 18:05:45 +0200596 return true;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100597}
598
Markus Armbruster012d4c92020-07-07 18:05:45 +0200599static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100600 Error **errp)
Markus Armbruster28770e02015-09-16 13:06:24 +0200601{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100602 QObjectInputVisitor *qiv = to_qiv(v);
603 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Markus Armbruster28770e02015-09-16 13:06:24 +0200604
Marc-André Lureau1382d4a2016-09-30 13:59:48 +0400605 *obj = NULL;
Marc-André Lureauc4897802016-09-23 00:39:26 +0400606 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200607 return false;
Marc-André Lureauc4897802016-09-23 00:39:26 +0400608 }
609
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200610 *obj = qobject_ref(qobj);
Markus Armbruster012d4c92020-07-07 18:05:45 +0200611 return true;
Markus Armbruster28770e02015-09-16 13:06:24 +0200612}
613
Markus Armbruster012d4c92020-07-07 18:05:45 +0200614static bool qobject_input_type_null(Visitor *v, const char *name,
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200615 QNull **obj, Error **errp)
Eric Blake3bc97fd2016-04-28 15:45:22 -0600616{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100617 QObjectInputVisitor *qiv = to_qiv(v);
618 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
Eric Blake3df016f2016-04-28 15:45:23 -0600619
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200620 *obj = NULL;
Marc-André Lureauc4897802016-09-23 00:39:26 +0400621 if (!qobj) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200622 return false;
Marc-André Lureauc4897802016-09-23 00:39:26 +0400623 }
624
Eric Blake3df016f2016-04-28 15:45:23 -0600625 if (qobject_type(qobj) != QTYPE_QNULL) {
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100626 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
627 full_name(qiv, name), "null");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200628 return false;
Eric Blake3df016f2016-04-28 15:45:23 -0600629 }
Markus Armbrusterd2f95f42017-06-26 18:22:59 +0200630 *obj = qnull();
Markus Armbruster012d4c92020-07-07 18:05:45 +0200631 return true;
Eric Blake3bc97fd2016-04-28 15:45:22 -0600632}
633
Markus Armbruster012d4c92020-07-07 18:05:45 +0200634static bool qobject_input_type_size_keyval(Visitor *v, const char *name,
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100635 uint64_t *obj, Error **errp)
636{
637 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustere3934b42017-02-28 22:26:53 +0100638 const char *str = qobject_input_get_keyval(qiv, name, errp);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100639
Markus Armbrustere3934b42017-02-28 22:26:53 +0100640 if (!str) {
Markus Armbruster012d4c92020-07-07 18:05:45 +0200641 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100642 }
643
Markus Armbrustere3934b42017-02-28 22:26:53 +0100644 if (qemu_strtosz(str, NULL, obj) < 0) {
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100645 /* TODO report -ERANGE more nicely */
646 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
647 full_name(qiv, name), "size");
Markus Armbruster012d4c92020-07-07 18:05:45 +0200648 return false;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100649 }
Markus Armbruster012d4c92020-07-07 18:05:45 +0200650 return true;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100651}
652
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100653static void qobject_input_optional(Visitor *v, const char *name, bool *present)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500654{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100655 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100656 QObject *qobj = qobject_input_try_get_object(qiv, name, false);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500657
658 if (!qobj) {
659 *present = false;
660 return;
661 }
662
663 *present = true;
664}
665
Markus Armbrustera1307282021-10-28 12:25:16 +0200666static bool qobject_input_policy_reject(Visitor *v, const char *name,
667 unsigned special_features,
668 Error **errp)
Markus Armbrusterdb291642021-03-18 16:55:18 +0100669{
Markus Armbruster7ce5fc62021-10-28 12:25:19 +0200670 return !compat_policy_input_ok(special_features, &v->compat_policy,
671 ERROR_CLASS_GENERIC_ERROR,
672 "parameter", name, errp);
Markus Armbrusterdb291642021-03-18 16:55:18 +0100673}
674
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100675static void qobject_input_free(Visitor *v)
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600676{
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100677 QObjectInputVisitor *qiv = to_qiv(v);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100678
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200679 while (!QSLIST_EMPTY(&qiv->stack)) {
680 StackObject *tos = QSLIST_FIRST(&qiv->stack);
681
682 QSLIST_REMOVE_HEAD(&qiv->stack, node);
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100683 qobject_input_stack_object_free(tos);
Paolo Bonzini3d344c22016-07-07 17:53:18 +0200684 }
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600685
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200686 qobject_unref(qiv->root);
Markus Armbrustera9fc37f2017-03-03 13:32:34 +0100687 if (qiv->errname) {
688 g_string_free(qiv->errname, TRUE);
689 }
Eric Blakeb70ce102016-06-09 10:48:38 -0600690 g_free(qiv);
Eric Blake2c0ef9f2016-06-09 10:48:35 -0600691}
692
Markus Armbrusterabe81bc2017-02-28 22:26:52 +0100693static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
Michael Rothc40cc0a2011-07-19 14:50:33 -0500694{
Markus Armbrusterabe81bc2017-02-28 22:26:52 +0100695 QObjectInputVisitor *v = g_malloc0(sizeof(*v));
Michael Rothc40cc0a2011-07-19 14:50:33 -0500696
Marc-André Lureau5d0cbbc2016-09-30 13:59:46 +0400697 assert(obj);
Michael Rothc40cc0a2011-07-19 14:50:33 -0500698
Eric Blake983f52d2016-04-28 15:45:09 -0600699 v->visitor.type = VISITOR_INPUT;
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100700 v->visitor.start_struct = qobject_input_start_struct;
701 v->visitor.check_struct = qobject_input_check_struct;
Markus Armbruster8b2e41d2017-04-27 10:41:26 +0200702 v->visitor.end_struct = qobject_input_end_struct;
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100703 v->visitor.start_list = qobject_input_start_list;
704 v->visitor.next_list = qobject_input_next_list;
Markus Armbrustera4a1c702017-03-03 13:32:45 +0100705 v->visitor.check_list = qobject_input_check_list;
Markus Armbruster8b2e41d2017-04-27 10:41:26 +0200706 v->visitor.end_list = qobject_input_end_list;
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100707 v->visitor.start_alternate = qobject_input_start_alternate;
Markus Armbrusterabe81bc2017-02-28 22:26:52 +0100708 v->visitor.optional = qobject_input_optional;
Markus Armbrustera1307282021-10-28 12:25:16 +0200709 v->visitor.policy_reject = qobject_input_policy_reject;
Markus Armbrusterabe81bc2017-02-28 22:26:52 +0100710 v->visitor.free = qobject_input_free;
711
Marc-André Lureauf5a74a52018-04-19 17:01:44 +0200712 v->root = qobject_ref(obj);
Markus Armbrusterabe81bc2017-02-28 22:26:52 +0100713
714 return v;
715}
716
717Visitor *qobject_input_visitor_new(QObject *obj)
718{
719 QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
720
Daniel P. Berrange09e68362016-09-30 15:45:27 +0100721 v->visitor.type_int64 = qobject_input_type_int64;
722 v->visitor.type_uint64 = qobject_input_type_uint64;
723 v->visitor.type_bool = qobject_input_type_bool;
724 v->visitor.type_str = qobject_input_type_str;
725 v->visitor.type_number = qobject_input_type_number;
726 v->visitor.type_any = qobject_input_type_any;
727 v->visitor.type_null = qobject_input_type_null;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500728
Eric Blakeb70ce102016-06-09 10:48:38 -0600729 return &v->visitor;
Michael Rothc40cc0a2011-07-19 14:50:33 -0500730}
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100731
732Visitor *qobject_input_visitor_new_keyval(QObject *obj)
733{
Markus Armbrusterabe81bc2017-02-28 22:26:52 +0100734 QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100735
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100736 v->visitor.type_int64 = qobject_input_type_int64_keyval;
737 v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
738 v->visitor.type_bool = qobject_input_type_bool_keyval;
Markus Armbruster31478f22017-02-28 22:27:08 +0100739 v->visitor.type_str = qobject_input_type_str_keyval;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100740 v->visitor.type_number = qobject_input_type_number_keyval;
741 v->visitor.type_any = qobject_input_type_any;
742 v->visitor.type_null = qobject_input_type_null;
743 v->visitor.type_size = qobject_input_type_size_keyval;
Markus Armbruster0b2c1be2017-02-28 22:27:10 +0100744 v->keyval = true;
Daniel P. Berrangecbd8acf2017-02-28 22:26:50 +0100745
746 return &v->visitor;
747}
Markus Armbruster9d1eab42017-02-28 22:27:06 +0100748
749Visitor *qobject_input_visitor_new_str(const char *str,
750 const char *implied_key,
751 Error **errp)
752{
753 bool is_json = str[0] == '{';
754 QObject *obj;
755 QDict *args;
756 Visitor *v;
757
758 if (is_json) {
759 obj = qobject_from_json(str, errp);
760 if (!obj) {
Markus Armbruster9d1eab42017-02-28 22:27:06 +0100761 return NULL;
762 }
Max Reitz7dc847e2018-02-24 16:40:29 +0100763 args = qobject_to(QDict, obj);
Markus Armbruster9d1eab42017-02-28 22:27:06 +0100764 assert(args);
765 v = qobject_input_visitor_new(QOBJECT(args));
766 } else {
Kevin Wolf8bf12c42020-10-11 09:35:02 +0200767 args = keyval_parse(str, implied_key, NULL, errp);
Markus Armbruster9d1eab42017-02-28 22:27:06 +0100768 if (!args) {
769 return NULL;
770 }
771 v = qobject_input_visitor_new_keyval(QOBJECT(args));
772 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200773 qobject_unref(args);
Markus Armbruster9d1eab42017-02-28 22:27:06 +0100774
775 return v;
776}