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 defines a tree of objects rooted at 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 member reference L.key-fragment
28 * val' is val with ',,' replaced by ','
29 * and only R may be empty.
31 * Duplicate keys are permitted; all but the last one are ignored.
33 * The equations must have a solution. Counter-example: a.b=1,a=2
34 * doesn't have one, because R.a must be an object to satisfy a.b=1
35 * and a string to satisfy a=2.
37 * The length of any key-fragment must be between 1 and 127.
39 * Design flaw: there is no way to denote an empty non-root object.
40 * While interpreting "key absent" as empty object seems natural
41 * (removing a key-val from the input string removes the member when
42 * there are more, so why not when it's the last), it doesn't work:
43 * "key absent" already means "optional object absent", which isn't
44 * the same as "empty object present".
46 * Additional syntax for use with an implied key:
48 * key-vals-ik = val-no-key [ ',' key-vals ]
49 * val-no-key = / [^=,]* /
51 * where no-key is syntactic sugar for implied-key=val-no-key.
54 * TODO support key-fragment with __RFQDN_ prefix (downstream extensions)
57 #include "qemu/osdep.h"
58 #include "qapi/error.h"
59 #include "qapi/qmp/qstring.h"
60 #include "qemu/option.h"
63 * Ensure @cur maps @key_in_cur the right way.
64 * If @value is null, it needs to map to a QDict, else to this
66 * If @cur doesn't have @key_in_cur, put an empty QDict or @value,
68 * Else, if it needs to map to a QDict, and already does, do nothing.
69 * Else, if it needs to map to this QString, and already maps to a
70 * QString, replace it by @value.
71 * Else, fail because we have conflicting needs on how to map
73 * In any case, take over the reference to @value, i.e. if the caller
74 * wants to hold on to a reference, it needs to QINCREF().
75 * Use @key up to @key_cursor to identify the key in error messages.
76 * On success, return the mapped value.
77 * On failure, store an error through @errp and return NULL.
79 static QObject
*keyval_parse_put(QDict
*cur
,
80 const char *key_in_cur
, QString
*value
,
81 const char *key
, const char *key_cursor
,
86 old
= qdict_get(cur
, key_in_cur
);
88 if (qobject_type(old
) != (value
? QTYPE_QSTRING
: QTYPE_QDICT
)) {
89 error_setg(errp
, "Parameters '%.*s.*' used inconsistently",
90 (int)(key_cursor
- key
), key
);
95 return old
; /* already QDict, do nothing */
97 new = QOBJECT(value
); /* replacement */
99 new = value
? QOBJECT(value
) : QOBJECT(qdict_new());
101 qdict_put_obj(cur
, key_in_cur
, new);
106 * Parse one KEY=VALUE from @params, store result in @qdict.
107 * The first fragment of KEY applies to @qdict. Subsequent fragments
108 * apply to nested QDicts, which are created on demand. @implied_key
109 * is as in keyval_parse().
110 * On success, return a pointer to the next KEY=VALUE, or else to '\0'.
111 * On failure, return NULL.
113 static const char *keyval_parse_one(QDict
*qdict
, const char *params
,
114 const char *implied_key
,
117 const char *key
, *key_end
, *s
;
119 char key_in_cur
[128];
125 len
= strcspn(params
, "=,");
126 if (implied_key
&& len
&& key
[len
] != '=') {
127 /* Desugar implied key */
129 len
= strlen(implied_key
);
134 * Loop over key fragments: @s points to current fragment, it
135 * applies to @cur. @key_in_cur[] holds the previous fragment.
140 for (len
= 0; s
+ len
< key_end
&& s
[len
] != '.'; len
++) {
143 assert(key
!= implied_key
);
144 error_setg(errp
, "Invalid parameter '%.*s'",
145 (int)(key_end
- key
), key
);
148 if (len
>= sizeof(key_in_cur
)) {
149 assert(key
!= implied_key
);
150 error_setg(errp
, "Parameter%s '%.*s' is too long",
151 s
!= key
|| s
+ len
!= key_end
? " fragment" : "",
157 next
= keyval_parse_put(cur
, key_in_cur
, NULL
,
162 cur
= qobject_to_qdict(next
);
166 memcpy(key_in_cur
, s
, len
);
176 if (key
== implied_key
) {
181 error_setg(errp
, "Expected '=' after parameter '%.*s'",
182 (int)(s
- key
), key
);
192 } else if (*s
== ',') {
198 qstring_append_chr(val
, *s
++);
201 if (!keyval_parse_put(cur
, key_in_cur
, val
, key
, key_end
, errp
)) {
208 * Parse @params in QEMU's traditional KEY=VALUE,... syntax.
209 * If @implied_key, the first KEY= can be omitted. @implied_key is
210 * implied then, and VALUE can't be empty or contain ',' or '='.
211 * On success, return a dictionary of the parsed keys and values.
212 * On failure, store an error through @errp and return NULL.
214 QDict
*keyval_parse(const char *params
, const char *implied_key
,
217 QDict
*qdict
= qdict_new();
222 s
= keyval_parse_one(qdict
, s
, implied_key
, errp
);