Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Input Visitor |
| 3 | * |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 4 | * Copyright (C) 2012-2017 Red Hat, Inc. |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 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 | |
Peter Maydell | cbf2115 | 2016-01-29 17:49:57 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Markus Armbruster | 5891c38 | 2017-05-22 18:42:12 +0200 | [diff] [blame] | 16 | #include <math.h> |
Markus Armbruster | 7ce5fc6 | 2021-10-28 12:25:19 +0200 | [diff] [blame] | 17 | #include "qapi/compat-policy.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 18 | #include "qapi/error.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 19 | #include "qapi/qobject-input-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 20 | #include "qapi/visitor-impl.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 21 | #include "qemu/queue.h" |
Markus Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 22 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 23 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | #include "qapi/qmp/qdict.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 25 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 26 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 27 | #include "qapi/qmp/qnull.h" |
| 28 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | fc81fa1 | 2018-02-01 12:18:40 +0100 | [diff] [blame] | 29 | #include "qapi/qmp/qstring.h" |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 30 | #include "qemu/cutils.h" |
Markus Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 31 | #include "qemu/option.h" |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 32 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 33 | typedef struct StackObject { |
| 34 | const char *name; /* Name of @obj in its parent, if any */ |
| 35 | QObject *obj; /* QDict or QList being visited */ |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 36 | void *qapi; /* sanity check that caller uses same pointer */ |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 37 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 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 */ |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 41 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 42 | QSLIST_ENTRY(StackObject) node; /* parent */ |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 43 | } StackObject; |
| 44 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 45 | struct QObjectInputVisitor { |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 46 | Visitor visitor; |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 47 | |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 48 | /* Root of visit at visitor creation. */ |
| 49 | QObject *root; |
Markus Armbruster | 0b2c1be | 2017-02-28 22:27:10 +0100 | [diff] [blame] | 50 | bool keyval; /* Assume @root made with keyval_parse() */ |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 51 | |
| 52 | /* Stack of objects being visited (all entries will be either |
| 53 | * QDict or QList). */ |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 54 | QSLIST_HEAD(, StackObject) stack; |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 55 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 56 | GString *errname; /* Accumulator for full_name() */ |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 59 | static QObjectInputVisitor *to_qiv(Visitor *v) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 60 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 61 | return container_of(v, QObjectInputVisitor, visitor); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Markus Armbruster | 6c02258 | 2017-04-27 10:41:24 +0200 | [diff] [blame] | 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 | */ |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 75 | static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name, |
| 76 | int n) |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 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) { |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 88 | if (n) { |
| 89 | n--; |
| 90 | } else if (qobject_type(so->obj) == QTYPE_QDICT) { |
| 91 | g_string_prepend(qiv->errname, name ?: "<anonymous>"); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 92 | g_string_prepend_c(qiv->errname, '.'); |
| 93 | } else { |
Markus Armbruster | 0b2c1be | 2017-02-28 22:27:10 +0100 | [diff] [blame] | 94 | snprintf(buf, sizeof(buf), |
| 95 | qiv->keyval ? ".%u" : "[%u]", |
| 96 | so->index); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 97 | g_string_prepend(qiv->errname, buf); |
| 98 | } |
| 99 | name = so->name; |
| 100 | } |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 101 | assert(!n); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 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); |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 107 | } else if (!qiv->errname->str[0]) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 108 | return "<anonymous>"; |
| 109 | } |
| 110 | |
| 111 | return qiv->errname->str; |
| 112 | } |
| 113 | |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 114 | static const char *full_name(QObjectInputVisitor *qiv, const char *name) |
| 115 | { |
| 116 | return full_name_nth(qiv, name, 0); |
| 117 | } |
| 118 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 119 | static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv, |
| 120 | const char *name, |
| 121 | bool consume) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 122 | { |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 123 | StackObject *tos; |
| 124 | QObject *qobj; |
Eric Blake | e5826a2 | 2016-04-28 15:45:15 -0600 | [diff] [blame] | 125 | QObject *ret; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 126 | |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 127 | if (QSLIST_EMPTY(&qiv->stack)) { |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 128 | /* Starting at root, name is ignored. */ |
Marc-André Lureau | 5d0cbbc | 2016-09-30 13:59:46 +0400 | [diff] [blame] | 129 | assert(qiv->root); |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 130 | return qiv->root; |
| 131 | } |
| 132 | |
| 133 | /* We are in a container; find the next element. */ |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 134 | tos = QSLIST_FIRST(&qiv->stack); |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 135 | qobj = tos->obj; |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 136 | assert(qobj); |
| 137 | |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 138 | if (qobject_type(qobj) == QTYPE_QDICT) { |
| 139 | assert(name); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 140 | ret = qdict_get(qobject_to(QDict, qobj), name); |
Eric Blake | e5826a2 | 2016-04-28 15:45:15 -0600 | [diff] [blame] | 141 | if (tos->h && consume && ret) { |
| 142 | bool removed = g_hash_table_remove(tos->h, name); |
| 143 | assert(removed); |
Paolo Bonzini | 47c6d3e | 2011-12-18 17:05:04 +0100 | [diff] [blame] | 144 | } |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 145 | } else { |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 146 | assert(qobject_type(qobj) == QTYPE_QLIST); |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 147 | assert(!name); |
Markus Armbruster | 1f41a64 | 2017-03-03 13:32:47 +0100 | [diff] [blame] | 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 | } |
Eric Blake | fcf3cb2 | 2016-04-28 15:45:19 -0600 | [diff] [blame] | 156 | if (consume) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 157 | tos->index++; |
Eric Blake | fcf3cb2 | 2016-04-28 15:45:19 -0600 | [diff] [blame] | 158 | } |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Eric Blake | ce140b1 | 2016-04-28 15:45:18 -0600 | [diff] [blame] | 161 | return ret; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 164 | static 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 Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 176 | static 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 Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 188 | qstr = qobject_to(QString, qobj); |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 189 | if (!qstr) { |
Markus Armbruster | 31478f2 | 2017-02-28 22:27:08 +0100 | [diff] [blame] | 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 | } |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return qstring_get_str(qstr); |
| 205 | } |
| 206 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 207 | static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv, |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 208 | const char *name, |
Markus Armbruster | b8874fb | 2017-03-03 13:32:32 +0100 | [diff] [blame] | 209 | QObject *obj, void *qapi) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 210 | { |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 211 | GHashTable *h; |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 212 | StackObject *tos = g_new0(StackObject, 1); |
Markus Armbruster | 7b1cd1c | 2020-04-15 10:30:47 +0200 | [diff] [blame] | 213 | QDict *qdict = qobject_to(QDict, obj); |
| 214 | QList *qlist = qobject_to(QList, obj); |
| 215 | const QDictEntry *entry; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 216 | |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 217 | assert(obj); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 218 | tos->name = name; |
Eric Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 219 | tos->obj = obj; |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 220 | tos->qapi = qapi; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 221 | |
Markus Armbruster | 7b1cd1c | 2020-04-15 10:30:47 +0200 | [diff] [blame] | 222 | if (qdict) { |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 223 | h = g_hash_table_new(g_str_hash, g_str_equal); |
Markus Armbruster | 7b1cd1c | 2020-04-15 10:30:47 +0200 | [diff] [blame] | 224 | 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 Blake | b471d01 | 2016-04-28 15:45:12 -0600 | [diff] [blame] | 229 | tos->h = h; |
Markus Armbruster | 048abb7 | 2017-03-03 13:32:39 +0100 | [diff] [blame] | 230 | } else { |
Markus Armbruster | 7b1cd1c | 2020-04-15 10:30:47 +0200 | [diff] [blame] | 231 | assert(qlist); |
| 232 | tos->entry = qlist_first(qlist); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 233 | tos->index = -1; |
Paolo Bonzini | e38ac96 | 2012-03-22 12:51:10 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 236 | QSLIST_INSERT_HEAD(&qiv->stack, tos, node); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 237 | return tos->entry; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 238 | } |
| 239 | |
NODA, Kai | 57a33d8 | 2012-04-21 22:41:27 +0900 | [diff] [blame] | 240 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 241 | static bool qobject_input_check_struct(Visitor *v, Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 242 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 243 | QObjectInputVisitor *qiv = to_qiv(v); |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 244 | StackObject *tos = QSLIST_FIRST(&qiv->stack); |
Markus Armbruster | 048abb7 | 2017-03-03 13:32:39 +0100 | [diff] [blame] | 245 | GHashTableIter iter; |
| 246 | const char *key; |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 247 | |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 248 | assert(tos && !tos->entry); |
Eric Blake | f96493b | 2016-02-17 23:48:15 -0700 | [diff] [blame] | 249 | |
Markus Armbruster | 048abb7 | 2017-03-03 13:32:39 +0100 | [diff] [blame] | 250 | 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 Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 254 | return false; |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 255 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 256 | return true; |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 257 | } |
| 258 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 259 | static void qobject_input_stack_object_free(StackObject *tos) |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 260 | { |
| 261 | if (tos->h) { |
| 262 | g_hash_table_unref(tos->h); |
| 263 | } |
| 264 | |
| 265 | g_free(tos); |
| 266 | } |
| 267 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 268 | static void qobject_input_pop(Visitor *v, void **obj) |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 269 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 270 | QObjectInputVisitor *qiv = to_qiv(v); |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 271 | StackObject *tos = QSLIST_FIRST(&qiv->stack); |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 272 | |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 273 | assert(tos && tos->qapi == obj); |
| 274 | QSLIST_REMOVE_HEAD(&qiv->stack, node); |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 275 | qobject_input_stack_object_free(tos); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 278 | static bool qobject_input_start_struct(Visitor *v, const char *name, void **obj, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 279 | size_t size, Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 280 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 281 | QObjectInputVisitor *qiv = to_qiv(v); |
| 282 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 283 | |
Eric Blake | e58d695 | 2016-04-28 15:45:10 -0600 | [diff] [blame] | 284 | if (obj) { |
| 285 | *obj = NULL; |
| 286 | } |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 287 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 288 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 289 | } |
| 290 | if (qobject_type(qobj) != QTYPE_QDICT) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 291 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 292 | full_name(qiv, name), "object"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 293 | return false; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 294 | } |
| 295 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 296 | qobject_input_push(qiv, name, qobj, obj); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 297 | |
| 298 | if (obj) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 299 | *obj = g_malloc0(size); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 300 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 301 | return true; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Markus Armbruster | 8b2e41d | 2017-04-27 10:41:26 +0200 | [diff] [blame] | 304 | static 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 Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 313 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 314 | static bool qobject_input_start_list(Visitor *v, const char *name, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 315 | GenericList **list, size_t size, |
| 316 | Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 317 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 318 | QObjectInputVisitor *qiv = to_qiv(v); |
| 319 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 320 | const QListEntry *entry; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 321 | |
Markus Armbruster | 58561c2 | 2017-03-03 13:32:33 +0100 | [diff] [blame] | 322 | if (list) { |
| 323 | *list = NULL; |
| 324 | } |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 325 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 326 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 327 | } |
| 328 | if (qobject_type(qobj) != QTYPE_QLIST) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 329 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 330 | full_name(qiv, name), "array"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 331 | return false; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 332 | } |
| 333 | |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 334 | entry = qobject_input_push(qiv, name, qobj, list); |
Markus Armbruster | 58561c2 | 2017-03-03 13:32:33 +0100 | [diff] [blame] | 335 | if (entry && list) { |
| 336 | *list = g_malloc0(size); |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 337 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 338 | return true; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 339 | } |
| 340 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 341 | static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail, |
| 342 | size_t size) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 343 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 344 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 345 | StackObject *tos = QSLIST_FIRST(&qiv->stack); |
Paolo Bonzini | 3a86a0f | 2012-03-22 22:38:40 +0100 | [diff] [blame] | 346 | |
Max Reitz | 532fb53 | 2018-03-10 16:14:36 -0600 | [diff] [blame] | 347 | assert(tos && qobject_to(QList, tos->obj)); |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 348 | |
| 349 | if (!tos->entry) { |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
Eric Blake | d9f62dd | 2016-04-28 15:45:31 -0600 | [diff] [blame] | 352 | tail->next = g_malloc0(size); |
| 353 | return tail->next; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 356 | static bool qobject_input_check_list(Visitor *v, Error **errp) |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 357 | { |
| 358 | QObjectInputVisitor *qiv = to_qiv(v); |
| 359 | StackObject *tos = QSLIST_FIRST(&qiv->stack); |
| 360 | |
Max Reitz | 532fb53 | 2018-03-10 16:14:36 -0600 | [diff] [blame] | 361 | assert(tos && qobject_to(QList, tos->obj)); |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 362 | |
| 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 Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 366 | return false; |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 367 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 368 | return true; |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Markus Armbruster | 8b2e41d | 2017-04-27 10:41:26 +0200 | [diff] [blame] | 371 | static 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 Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 379 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 380 | static bool qobject_input_start_alternate(Visitor *v, const char *name, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 381 | GenericAlternate **obj, size_t size, |
Marc-André Lureau | 60390d2 | 2017-06-07 20:35:59 +0400 | [diff] [blame] | 382 | Error **errp) |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 383 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 384 | QObjectInputVisitor *qiv = to_qiv(v); |
| 385 | QObject *qobj = qobject_input_get_object(qiv, name, false, errp); |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 386 | |
| 387 | if (!qobj) { |
Eric Blake | dbf1192 | 2016-02-17 23:48:29 -0700 | [diff] [blame] | 388 | *obj = NULL; |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 389 | return false; |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 390 | } |
Eric Blake | dbf1192 | 2016-02-17 23:48:29 -0700 | [diff] [blame] | 391 | *obj = g_malloc0(size); |
| 392 | (*obj)->type = qobject_type(qobj); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 393 | return true; |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 396 | static bool qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 397 | Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 398 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 399 | QObjectInputVisitor *qiv = to_qiv(v); |
| 400 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 401 | QNum *qnum; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 402 | |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 403 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 404 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 405 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 406 | qnum = qobject_to(QNum, qobj); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 407 | if (!qnum || !qnum_get_try_int(qnum, obj)) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 408 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 409 | full_name(qiv, name), "integer"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 410 | return false; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 411 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 412 | return true; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 415 | static bool qobject_input_type_int64_keyval(Visitor *v, const char *name, |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 416 | int64_t *obj, Error **errp) |
| 417 | { |
| 418 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 419 | const char *str = qobject_input_get_keyval(qiv, name, errp); |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 420 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 421 | if (!str) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 422 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 425 | if (qemu_strtoi64(str, NULL, 0, obj) < 0) { |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 426 | /* TODO report -ERANGE more nicely */ |
| 427 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 428 | full_name(qiv, name), "integer"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 429 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 430 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 431 | return true; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 434 | static bool qobject_input_type_uint64(Visitor *v, const char *name, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 435 | uint64_t *obj, Error **errp) |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 436 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 437 | QObjectInputVisitor *qiv = to_qiv(v); |
| 438 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 439 | QNum *qnum; |
| 440 | int64_t val; |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 441 | |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 442 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 443 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 444 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 445 | qnum = qobject_to(QNum, qobj); |
Marc-André Lureau | 5923f85 | 2017-06-07 20:36:03 +0400 | [diff] [blame] | 446 | if (!qnum) { |
| 447 | goto err; |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 448 | } |
Marc-André Lureau | 5923f85 | 2017-06-07 20:36:03 +0400 | [diff] [blame] | 449 | |
| 450 | if (qnum_get_try_uint(qnum, obj)) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 451 | return true; |
Marc-André Lureau | 5923f85 | 2017-06-07 20:36:03 +0400 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* Need to accept negative values for backward compatibility */ |
| 455 | if (qnum_get_try_int(qnum, &val)) { |
| 456 | *obj = val; |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 457 | return true; |
Marc-André Lureau | 5923f85 | 2017-06-07 20:36:03 +0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | err: |
| 461 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 462 | full_name(qiv, name), "uint64"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 463 | return false; |
Eric Blake | f755dea | 2016-01-29 06:48:50 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 466 | static bool qobject_input_type_uint64_keyval(Visitor *v, const char *name, |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 467 | uint64_t *obj, Error **errp) |
| 468 | { |
| 469 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 470 | const char *str = qobject_input_get_keyval(qiv, name, errp); |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 471 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 472 | if (!str) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 473 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 476 | if (qemu_strtou64(str, NULL, 0, obj) < 0) { |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 477 | /* TODO report -ERANGE more nicely */ |
| 478 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 479 | full_name(qiv, name), "integer"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 480 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 481 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 482 | return true; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 485 | static bool qobject_input_type_bool(Visitor *v, const char *name, bool *obj, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 486 | Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 487 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 488 | QObjectInputVisitor *qiv = to_qiv(v); |
| 489 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 490 | QBool *qbool; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 491 | |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 492 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 493 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 494 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 495 | qbool = qobject_to(QBool, qobj); |
Markus Armbruster | 14b6160 | 2015-10-15 16:15:33 +0200 | [diff] [blame] | 496 | if (!qbool) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 497 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 498 | full_name(qiv, name), "boolean"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 499 | return false; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Markus Armbruster | 14b6160 | 2015-10-15 16:15:33 +0200 | [diff] [blame] | 502 | *obj = qbool_get_bool(qbool); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 503 | return true; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 504 | } |
| 505 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 506 | static bool qobject_input_type_bool_keyval(Visitor *v, const char *name, |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 507 | bool *obj, Error **errp) |
| 508 | { |
| 509 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 510 | const char *str = qobject_input_get_keyval(qiv, name, errp); |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 511 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 512 | if (!str) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 513 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 514 | } |
| 515 | |
Paolo Bonzini | 372bcb2 | 2020-11-03 11:13:39 -0500 | [diff] [blame] | 516 | if (!qapi_bool_parse(name, str, obj, NULL)) { |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 517 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 518 | full_name(qiv, name), "'on' or 'off'"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 519 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 520 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 521 | return true; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 524 | static bool qobject_input_type_str(Visitor *v, const char *name, char **obj, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 525 | Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 526 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 527 | QObjectInputVisitor *qiv = to_qiv(v); |
| 528 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 529 | QString *qstr; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 530 | |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 531 | *obj = NULL; |
| 532 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 533 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 534 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 535 | qstr = qobject_to(QString, qobj); |
Markus Armbruster | 7f02784 | 2015-10-15 16:15:37 +0200 | [diff] [blame] | 536 | if (!qstr) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 537 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 538 | full_name(qiv, name), "string"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 539 | return false; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Markus Armbruster | 7f02784 | 2015-10-15 16:15:37 +0200 | [diff] [blame] | 542 | *obj = g_strdup(qstring_get_str(qstr)); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 543 | return true; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 544 | } |
| 545 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 546 | static bool qobject_input_type_str_keyval(Visitor *v, const char *name, |
Markus Armbruster | 31478f2 | 2017-02-28 22:27:08 +0100 | [diff] [blame] | 547 | 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 Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 553 | return !!str; |
Markus Armbruster | 31478f2 | 2017-02-28 22:27:08 +0100 | [diff] [blame] | 554 | } |
| 555 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 556 | static bool qobject_input_type_number(Visitor *v, const char *name, double *obj, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 557 | Error **errp) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 558 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 559 | QObjectInputVisitor *qiv = to_qiv(v); |
| 560 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 561 | QNum *qnum; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 562 | |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 563 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 564 | return false; |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 565 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 566 | qnum = qobject_to(QNum, qobj); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 567 | if (!qnum) { |
Marc-André Lureau | 5863404 | 2017-06-07 20:35:57 +0400 | [diff] [blame] | 568 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 569 | full_name(qiv, name), "number"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 570 | return false; |
Michael Roth | 1ee5187 | 2012-05-11 12:43:24 -0500 | [diff] [blame] | 571 | } |
Markus Armbruster | fcf73f6 | 2015-10-15 16:15:35 +0200 | [diff] [blame] | 572 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 573 | *obj = qnum_get_double(qnum); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 574 | return true; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 575 | } |
| 576 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 577 | static bool qobject_input_type_number_keyval(Visitor *v, const char *name, |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 578 | double *obj, Error **errp) |
| 579 | { |
| 580 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 581 | const char *str = qobject_input_get_keyval(qiv, name, errp); |
David Hildenbrand | e08a524 | 2018-11-21 17:44:16 +0100 | [diff] [blame] | 582 | double val; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 583 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 584 | if (!str) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 585 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 586 | } |
| 587 | |
David Hildenbrand | e08a524 | 2018-11-21 17:44:16 +0100 | [diff] [blame] | 588 | if (qemu_strtod_finite(str, NULL, &val)) { |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 589 | /* TODO report -ERANGE more nicely */ |
| 590 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 591 | full_name(qiv, name), "number"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 592 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 593 | } |
David Hildenbrand | e08a524 | 2018-11-21 17:44:16 +0100 | [diff] [blame] | 594 | |
| 595 | *obj = val; |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 596 | return true; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 597 | } |
| 598 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 599 | static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj, |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 600 | Error **errp) |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 601 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 602 | QObjectInputVisitor *qiv = to_qiv(v); |
| 603 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 604 | |
Marc-André Lureau | 1382d4a | 2016-09-30 13:59:48 +0400 | [diff] [blame] | 605 | *obj = NULL; |
Marc-André Lureau | c489780 | 2016-09-23 00:39:26 +0400 | [diff] [blame] | 606 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 607 | return false; |
Marc-André Lureau | c489780 | 2016-09-23 00:39:26 +0400 | [diff] [blame] | 608 | } |
| 609 | |
Marc-André Lureau | f5a74a5 | 2018-04-19 17:01:44 +0200 | [diff] [blame] | 610 | *obj = qobject_ref(qobj); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 611 | return true; |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 614 | static bool qobject_input_type_null(Visitor *v, const char *name, |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 615 | QNull **obj, Error **errp) |
Eric Blake | 3bc97fd | 2016-04-28 15:45:22 -0600 | [diff] [blame] | 616 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 617 | QObjectInputVisitor *qiv = to_qiv(v); |
| 618 | QObject *qobj = qobject_input_get_object(qiv, name, true, errp); |
Eric Blake | 3df016f | 2016-04-28 15:45:23 -0600 | [diff] [blame] | 619 | |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 620 | *obj = NULL; |
Marc-André Lureau | c489780 | 2016-09-23 00:39:26 +0400 | [diff] [blame] | 621 | if (!qobj) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 622 | return false; |
Marc-André Lureau | c489780 | 2016-09-23 00:39:26 +0400 | [diff] [blame] | 623 | } |
| 624 | |
Eric Blake | 3df016f | 2016-04-28 15:45:23 -0600 | [diff] [blame] | 625 | if (qobject_type(qobj) != QTYPE_QNULL) { |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 626 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, |
| 627 | full_name(qiv, name), "null"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 628 | return false; |
Eric Blake | 3df016f | 2016-04-28 15:45:23 -0600 | [diff] [blame] | 629 | } |
Markus Armbruster | d2f95f4 | 2017-06-26 18:22:59 +0200 | [diff] [blame] | 630 | *obj = qnull(); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 631 | return true; |
Eric Blake | 3bc97fd | 2016-04-28 15:45:22 -0600 | [diff] [blame] | 632 | } |
| 633 | |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 634 | static bool qobject_input_type_size_keyval(Visitor *v, const char *name, |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 635 | uint64_t *obj, Error **errp) |
| 636 | { |
| 637 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 638 | const char *str = qobject_input_get_keyval(qiv, name, errp); |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 639 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 640 | if (!str) { |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 641 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Markus Armbruster | e3934b4 | 2017-02-28 22:26:53 +0100 | [diff] [blame] | 644 | if (qemu_strtosz(str, NULL, obj) < 0) { |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 645 | /* TODO report -ERANGE more nicely */ |
| 646 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 647 | full_name(qiv, name), "size"); |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 648 | return false; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 649 | } |
Markus Armbruster | 012d4c9 | 2020-07-07 18:05:45 +0200 | [diff] [blame] | 650 | return true; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 653 | static void qobject_input_optional(Visitor *v, const char *name, bool *present) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 654 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 655 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 656 | QObject *qobj = qobject_input_try_get_object(qiv, name, false); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 657 | |
| 658 | if (!qobj) { |
| 659 | *present = false; |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | *present = true; |
| 664 | } |
| 665 | |
Markus Armbruster | a130728 | 2021-10-28 12:25:16 +0200 | [diff] [blame] | 666 | static bool qobject_input_policy_reject(Visitor *v, const char *name, |
| 667 | unsigned special_features, |
| 668 | Error **errp) |
Markus Armbruster | db29164 | 2021-03-18 16:55:18 +0100 | [diff] [blame] | 669 | { |
Markus Armbruster | 7ce5fc6 | 2021-10-28 12:25:19 +0200 | [diff] [blame] | 670 | return !compat_policy_input_ok(special_features, &v->compat_policy, |
| 671 | ERROR_CLASS_GENERIC_ERROR, |
| 672 | "parameter", name, errp); |
Markus Armbruster | db29164 | 2021-03-18 16:55:18 +0100 | [diff] [blame] | 673 | } |
| 674 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 675 | static void qobject_input_free(Visitor *v) |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 676 | { |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 677 | QObjectInputVisitor *qiv = to_qiv(v); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 678 | |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 679 | while (!QSLIST_EMPTY(&qiv->stack)) { |
| 680 | StackObject *tos = QSLIST_FIRST(&qiv->stack); |
| 681 | |
| 682 | QSLIST_REMOVE_HEAD(&qiv->stack, node); |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 683 | qobject_input_stack_object_free(tos); |
Paolo Bonzini | 3d344c2 | 2016-07-07 17:53:18 +0200 | [diff] [blame] | 684 | } |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 685 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 686 | qobject_unref(qiv->root); |
Markus Armbruster | a9fc37f | 2017-03-03 13:32:34 +0100 | [diff] [blame] | 687 | if (qiv->errname) { |
| 688 | g_string_free(qiv->errname, TRUE); |
| 689 | } |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 690 | g_free(qiv); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 691 | } |
| 692 | |
Markus Armbruster | abe81bc | 2017-02-28 22:26:52 +0100 | [diff] [blame] | 693 | static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj) |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 694 | { |
Markus Armbruster | abe81bc | 2017-02-28 22:26:52 +0100 | [diff] [blame] | 695 | QObjectInputVisitor *v = g_malloc0(sizeof(*v)); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 696 | |
Marc-André Lureau | 5d0cbbc | 2016-09-30 13:59:46 +0400 | [diff] [blame] | 697 | assert(obj); |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 698 | |
Eric Blake | 983f52d | 2016-04-28 15:45:09 -0600 | [diff] [blame] | 699 | v->visitor.type = VISITOR_INPUT; |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 700 | v->visitor.start_struct = qobject_input_start_struct; |
| 701 | v->visitor.check_struct = qobject_input_check_struct; |
Markus Armbruster | 8b2e41d | 2017-04-27 10:41:26 +0200 | [diff] [blame] | 702 | v->visitor.end_struct = qobject_input_end_struct; |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 703 | v->visitor.start_list = qobject_input_start_list; |
| 704 | v->visitor.next_list = qobject_input_next_list; |
Markus Armbruster | a4a1c70 | 2017-03-03 13:32:45 +0100 | [diff] [blame] | 705 | v->visitor.check_list = qobject_input_check_list; |
Markus Armbruster | 8b2e41d | 2017-04-27 10:41:26 +0200 | [diff] [blame] | 706 | v->visitor.end_list = qobject_input_end_list; |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 707 | v->visitor.start_alternate = qobject_input_start_alternate; |
Markus Armbruster | abe81bc | 2017-02-28 22:26:52 +0100 | [diff] [blame] | 708 | v->visitor.optional = qobject_input_optional; |
Markus Armbruster | a130728 | 2021-10-28 12:25:16 +0200 | [diff] [blame] | 709 | v->visitor.policy_reject = qobject_input_policy_reject; |
Markus Armbruster | abe81bc | 2017-02-28 22:26:52 +0100 | [diff] [blame] | 710 | v->visitor.free = qobject_input_free; |
| 711 | |
Marc-André Lureau | f5a74a5 | 2018-04-19 17:01:44 +0200 | [diff] [blame] | 712 | v->root = qobject_ref(obj); |
Markus Armbruster | abe81bc | 2017-02-28 22:26:52 +0100 | [diff] [blame] | 713 | |
| 714 | return v; |
| 715 | } |
| 716 | |
| 717 | Visitor *qobject_input_visitor_new(QObject *obj) |
| 718 | { |
| 719 | QObjectInputVisitor *v = qobject_input_visitor_base_new(obj); |
| 720 | |
Daniel P. Berrange | 09e6836 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 721 | 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 Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 728 | |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 729 | return &v->visitor; |
Michael Roth | c40cc0a | 2011-07-19 14:50:33 -0500 | [diff] [blame] | 730 | } |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 731 | |
| 732 | Visitor *qobject_input_visitor_new_keyval(QObject *obj) |
| 733 | { |
Markus Armbruster | abe81bc | 2017-02-28 22:26:52 +0100 | [diff] [blame] | 734 | QObjectInputVisitor *v = qobject_input_visitor_base_new(obj); |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 735 | |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 736 | 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 Armbruster | 31478f2 | 2017-02-28 22:27:08 +0100 | [diff] [blame] | 739 | v->visitor.type_str = qobject_input_type_str_keyval; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 740 | 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 Armbruster | 0b2c1be | 2017-02-28 22:27:10 +0100 | [diff] [blame] | 744 | v->keyval = true; |
Daniel P. Berrange | cbd8acf | 2017-02-28 22:26:50 +0100 | [diff] [blame] | 745 | |
| 746 | return &v->visitor; |
| 747 | } |
Markus Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 748 | |
| 749 | Visitor *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 Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 761 | return NULL; |
| 762 | } |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 763 | args = qobject_to(QDict, obj); |
Markus Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 764 | assert(args); |
| 765 | v = qobject_input_visitor_new(QOBJECT(args)); |
| 766 | } else { |
Kevin Wolf | 8bf12c4 | 2020-10-11 09:35:02 +0200 | [diff] [blame] | 767 | args = keyval_parse(str, implied_key, NULL, errp); |
Markus Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 768 | if (!args) { |
| 769 | return NULL; |
| 770 | } |
| 771 | v = qobject_input_visitor_new_keyval(QOBJECT(args)); |
| 772 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 773 | qobject_unref(args); |
Markus Armbruster | 9d1eab4 | 2017-02-28 22:27:06 +0100 | [diff] [blame] | 774 | |
| 775 | return v; |
| 776 | } |