2 * Parsing KEY=VALUE,... strings
4 * Copyright (C) 2017 Red Hat Inc.
7 * Markus Armbruster <armbru@redhat.com>,
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 * KEY=VALUE,... syntax:
16 * key-vals = [ key-val { ',' key-val } [ ',' ] ]
17 * key-val = key '=' val
18 * key = key-fragment { '.' key-fragment }
19 * key-fragment = / [^=,.]* /
20 * val = { / [^,]* / | ',,' }
22 * Semantics defined by reduction to JSON:
24 * key-vals is a tree of objects and arrays rooted at object R
25 * where for each key-val = key-fragment . ... = val in key-vals
26 * R op key-fragment op ... = val'
27 * where (left-associative) op is
28 * array subscript L[key-fragment] for numeric key-fragment
29 * member reference L.key-fragment otherwise
30 * val' is val with ',,' replaced by ','
31 * and only R may be empty.
33 * Duplicate keys are permitted; all but the last one are ignored.
35 * The equations must have a solution. Counter-example: a.b=1,a=2
36 * doesn't have one, because R.a must be an object to satisfy a.b=1
37 * and a string to satisfy a=2.
39 * Key-fragments must be valid QAPI names or consist only of digits.
41 * The length of any key-fragment must be between 1 and 127.
43 * Design flaw: there is no way to denote an empty array or non-root
44 * object. While interpreting "key absent" as empty seems natural
45 * (removing a key-val from the input string removes the member when
46 * there are more, so why not when it's the last), it doesn't work:
47 * "key absent" already means "optional object/array absent", which
48 * isn't the same as "empty object/array present".
50 * Additional syntax for use with an implied key:
52 * key-vals-ik = val-no-key [ ',' key-vals ]
53 * val-no-key = / [^=,]* /
55 * where no-key is syntactic sugar for implied-key=val-no-key.
58 #include "qemu/osdep.h"
59 #include "qapi/error.h"
60 #include "qapi/qmp/qstring.h"
61 #include "qapi/util.h"
62 #include "qemu/cutils.h"
63 #include "qemu/option.h"
66 * Convert @key to a list index.
67 * Convert all leading digits to a (non-negative) number, capped at
69 * If @end is non-null, assign a pointer to the first character after
70 * the number to *@end.
71 * Else, fail if any characters follow.
72 * On success, return the converted number.
73 * On failure, return a negative value.
74 * Note: since only digits are converted, no two keys can map to the
75 * same number, except by overflow to INT_MAX.
77 static int key_to_index(const char *key
, const char **end
)
82 if (*key
< '0' || *key
> '9') {
85 ret
= qemu_strtoul(key
, end
, 10, &index
);
87 return ret
== -ERANGE
? INT_MAX
: ret
;
89 return index
<= INT_MAX
? index
: INT_MAX
;
93 * Ensure @cur maps @key_in_cur the right way.
94 * If @value is null, it needs to map to a QDict, else to this
96 * If @cur doesn't have @key_in_cur, put an empty QDict or @value,
98 * Else, if it needs to map to a QDict, and already does, do nothing.
99 * Else, if it needs to map to this QString, and already maps to a
100 * QString, replace it by @value.
101 * Else, fail because we have conflicting needs on how to map
103 * In any case, take over the reference to @value, i.e. if the caller
104 * wants to hold on to a reference, it needs to QINCREF().
105 * Use @key up to @key_cursor to identify the key in error messages.
106 * On success, return the mapped value.
107 * On failure, store an error through @errp and return NULL.
109 static QObject
*keyval_parse_put(QDict
*cur
,
110 const char *key_in_cur
, QString
*value
,
111 const char *key
, const char *key_cursor
,
116 old
= qdict_get(cur
, key_in_cur
);
118 if (qobject_type(old
) != (value
? QTYPE_QSTRING
: QTYPE_QDICT
)) {
119 error_setg(errp
, "Parameters '%.*s.*' used inconsistently",
120 (int)(key_cursor
- key
), key
);
125 return old
; /* already QDict, do nothing */
127 new = QOBJECT(value
); /* replacement */
129 new = value
? QOBJECT(value
) : QOBJECT(qdict_new());
131 qdict_put_obj(cur
, key_in_cur
, new);
136 * Parse one KEY=VALUE from @params, store result in @qdict.
137 * The first fragment of KEY applies to @qdict. Subsequent fragments
138 * apply to nested QDicts, which are created on demand. @implied_key
139 * is as in keyval_parse().
140 * On success, return a pointer to the next KEY=VALUE, or else to '\0'.
141 * On failure, return NULL.
143 static const char *keyval_parse_one(QDict
*qdict
, const char *params
,
144 const char *implied_key
,
147 const char *key
, *key_end
, *s
, *end
;
149 char key_in_cur
[128];
156 len
= strcspn(params
, "=,");
157 if (implied_key
&& len
&& key
[len
] != '=') {
158 /* Desugar implied key */
160 len
= strlen(implied_key
);
165 * Loop over key fragments: @s points to current fragment, it
166 * applies to @cur. @key_in_cur[] holds the previous fragment.
171 /* Want a key index (unless it's first) or a QAPI name */
172 if (s
!= key
&& key_to_index(s
, &end
) >= 0) {
175 ret
= parse_qapi_name(s
, false);
176 len
= ret
< 0 ? 0 : ret
;
178 assert(s
+ len
<= key_end
);
179 if (!len
|| (s
+ len
< key_end
&& s
[len
] != '.')) {
180 assert(key
!= implied_key
);
181 error_setg(errp
, "Invalid parameter '%.*s'",
182 (int)(key_end
- key
), key
);
185 if (len
>= sizeof(key_in_cur
)) {
186 assert(key
!= implied_key
);
187 error_setg(errp
, "Parameter%s '%.*s' is too long",
188 s
!= key
|| s
+ len
!= key_end
? " fragment" : "",
194 next
= keyval_parse_put(cur
, key_in_cur
, NULL
,
199 cur
= qobject_to_qdict(next
);
203 memcpy(key_in_cur
, s
, len
);
213 if (key
== implied_key
) {
218 error_setg(errp
, "Expected '=' after parameter '%.*s'",
219 (int)(s
- key
), key
);
229 } else if (*s
== ',') {
235 qstring_append_chr(val
, *s
++);
238 if (!keyval_parse_put(cur
, key_in_cur
, val
, key
, key_end
, errp
)) {
244 static char *reassemble_key(GSList
*key
)
246 GString
*s
= g_string_new("");
249 for (p
= key
; p
; p
= p
->next
) {
250 g_string_prepend_c(s
, '.');
251 g_string_prepend(s
, (char *)p
->data
);
254 return g_string_free(s
, FALSE
);
258 * Listify @cur recursively.
259 * Replace QDicts whose keys are all valid list indexes by QLists.
260 * @key_of_cur is the list of key fragments leading up to @cur.
261 * On success, return either @cur or its replacement.
262 * On failure, store an error through @errp and return NULL.
264 static QObject
*keyval_listify(QDict
*cur
, GSList
*key_of_cur
, Error
**errp
)
267 bool has_index
, has_member
;
268 const QDictEntry
*ent
;
274 int index
, max_index
, i
;
277 key_node
.next
= key_of_cur
;
280 * Recursively listify @cur's members, and figure out whether @cur
281 * itself is to be listified.
285 for (ent
= qdict_first(cur
); ent
; ent
= qdict_next(cur
, ent
)) {
286 if (key_to_index(ent
->key
, NULL
) >= 0) {
292 qdict
= qobject_to_qdict(ent
->value
);
297 key_node
.data
= ent
->key
;
298 val
= keyval_listify(qdict
, &key_node
, errp
);
302 if (val
!= ent
->value
) {
303 qdict_put_obj(cur
, ent
->key
, val
);
307 if (has_index
&& has_member
) {
308 key
= reassemble_key(key_of_cur
);
309 error_setg(errp
, "Parameters '%s*' used inconsistently", key
);
317 /* Copy @cur's values to @elt[] */
318 nelt
= qdict_size(cur
) + 1; /* one extra, for use as sentinel */
319 elt
= g_new0(QObject
*, nelt
);
321 for (ent
= qdict_first(cur
); ent
; ent
= qdict_next(cur
, ent
)) {
322 index
= key_to_index(ent
->key
, NULL
);
324 if (index
> max_index
) {
328 * We iterate @nelt times. If we get one exceeding @nelt
329 * here, we will put less than @nelt values into @elt[],
330 * triggering the error in the next loop.
332 if ((size_t)index
>= nelt
- 1) {
335 /* Even though dict keys are distinct, indexes need not be */
336 elt
[index
] = ent
->value
;
340 * Make a list from @elt[], reporting any missing elements.
341 * If we dropped an index >= nelt in the previous loop, this loop
342 * will run into the sentinel and report index @nelt missing.
345 assert(!elt
[nelt
-1]); /* need the sentinel to be null */
346 for (i
= 0; i
< MIN(nelt
, max_index
+ 1); i
++) {
348 key
= reassemble_key(key_of_cur
);
349 error_setg(errp
, "Parameter '%s%d' missing", key
, i
);
355 qobject_incref(elt
[i
]);
356 qlist_append_obj(list
, elt
[i
]);
360 return QOBJECT(list
);
364 * Parse @params in QEMU's traditional KEY=VALUE,... syntax.
365 * If @implied_key, the first KEY= can be omitted. @implied_key is
366 * implied then, and VALUE can't be empty or contain ',' or '='.
367 * On success, return a dictionary of the parsed keys and values.
368 * On failure, store an error through @errp and return NULL.
370 QDict
*keyval_parse(const char *params
, const char *implied_key
,
373 QDict
*qdict
= qdict_new();
379 s
= keyval_parse_one(qdict
, s
, implied_key
, errp
);
387 listified
= keyval_listify(qdict
, NULL
, errp
);
392 assert(listified
== QOBJECT(qdict
));