* KEY=VALUE,... syntax:
*
* key-vals = [ key-val { ',' key-val } [ ',' ] ]
- * key-val = key '=' val
+ * key-val = key '=' val | help
* key = key-fragment { '.' key-fragment }
- * key-fragment = / [^=,.]* /
- * val = { / [^,]* / | ',,' }
+ * key-fragment = qapi-name | index
+ * qapi-name = '__' / [a-z0-9.-]+ / '_' / [A-Za-z][A-Za-z0-9_-]* /
+ * index = / [0-9]+ /
+ * val = { / [^,]+ / | ',,' }
+ * help = 'help' | '?'
*
* Semantics defined by reduction to JSON:
*
*
* The length of any key-fragment must be between 1 and 127.
*
+ * If any key-val is help, the object is to be treated as a help
+ * request.
+ *
* Design flaw: there is no way to denote an empty array or non-root
* object. While interpreting "key absent" as empty seems natural
* (removing a key-val from the input string removes the member when
* Awkward. Note that we carefully restrict alternate types to avoid
* similar ambiguity.
*
- * Additional syntax for use with an implied key:
+ * Alternative syntax for use with an implied key:
+ *
+ * key-vals = [ key-val-1st { ',' key-val } [ ',' ] ]
+ * key-val-1st = val-no-key | key-val
+ * val-no-key = / [^=,]+ / - help
*
- * key-vals-ik = val-no-key [ ',' key-vals ]
- * val-no-key = / [^=,]* /
+ * where val-no-key is syntactic sugar for implied-key=val-no-key.
*
- * where no-key is syntactic sugar for implied-key=val-no-key.
+ * Note that you can't use the sugared form when the value contains
+ * '=' or ','.
*/
#include "qemu/osdep.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qstring.h"
#include "qemu/cutils.h"
-#include "qemu/option.h"
+#include "qemu/keyval.h"
+#include "qemu/help_option.h"
/*
* Convert @key to a list index.
* Else, fail because we have conflicting needs on how to map
* @key_in_cur.
* In any case, take over the reference to @value, i.e. if the caller
- * wants to hold on to a reference, it needs to QINCREF().
+ * wants to hold on to a reference, it needs to qobject_ref().
* Use @key up to @key_cursor to identify the key in error messages.
* On success, return the mapped value.
* On failure, store an error through @errp and return NULL.
if (qobject_type(old) != (value ? QTYPE_QSTRING : QTYPE_QDICT)) {
error_setg(errp, "Parameters '%.*s.*' used inconsistently",
(int)(key_cursor - key), key);
- QDECREF(value);
+ qobject_unref(value);
return NULL;
}
if (!value) {
}
/*
- * Parse one KEY=VALUE from @params, store result in @qdict.
+ * Parse one parameter from @params.
+ *
+ * If we're looking at KEY=VALUE, store result in @qdict.
* The first fragment of KEY applies to @qdict. Subsequent fragments
* apply to nested QDicts, which are created on demand. @implied_key
* is as in keyval_parse().
- * On success, return a pointer to the next KEY=VALUE, or else to '\0'.
+ *
+ * If we're looking at "help" or "?", set *help to true.
+ *
+ * On success, return a pointer to the next parameter, or else to '\0'.
* On failure, return NULL.
*/
static const char *keyval_parse_one(QDict *qdict, const char *params,
- const char *implied_key,
+ const char *implied_key, bool *help,
Error **errp)
{
- const char *key, *key_end, *s, *end;
+ const char *key, *key_end, *val_end, *s, *end;
size_t len;
char key_in_cur[128];
QDict *cur;
int ret;
QObject *next;
- QString *val;
+ GString *val;
key = params;
+ val_end = NULL;
len = strcspn(params, "=,");
- if (implied_key && len && key[len] != '=') {
- /* Desugar implied key */
- key = implied_key;
- len = strlen(implied_key);
+ if (len && key[len] != '=') {
+ if (starts_with_help_option(key) == len) {
+ *help = true;
+ s = key + len;
+ if (*s == ',') {
+ s++;
+ }
+ return s;
+ }
+ if (implied_key) {
+ /* Desugar implied key */
+ key = implied_key;
+ val_end = params + len;
+ len = strlen(implied_key);
+ }
}
key_end = key + len;
if (key == implied_key) {
assert(!*s);
- s = params;
+ val = g_string_new_len(params, val_end - params);
+ s = val_end;
+ if (*s == ',') {
+ s++;
+ }
} else {
if (*s != '=') {
error_setg(errp, "Expected '=' after parameter '%.*s'",
return NULL;
}
s++;
- }
- val = qstring_new();
- for (;;) {
- if (!*s) {
- break;
- } else if (*s == ',') {
- s++;
- if (*s != ',') {
+ val = g_string_new(NULL);
+ for (;;) {
+ if (!*s) {
break;
+ } else if (*s == ',') {
+ s++;
+ if (*s != ',') {
+ break;
+ }
}
+ g_string_append_c(val, *s++);
}
- qstring_append_chr(val, *s++);
}
- if (!keyval_parse_put(cur, key_in_cur, val, key, key_end, errp)) {
+ if (!keyval_parse_put(cur, key_in_cur, qstring_from_gstring(val),
+ key, key_end, errp)) {
return NULL;
}
return s;
return g_string_free(s, FALSE);
}
+/*
+ * Recursive worker for keyval_merge.
+ *
+ * @str is the path that led to the * current dictionary (to be used for
+ * error messages). It is modified internally but restored before the
+ * function returns.
+ */
+static void keyval_do_merge(QDict *dest, const QDict *merged, GString *str, Error **errp)
+{
+ size_t save_len = str->len;
+ const QDictEntry *ent;
+ QObject *old_value;
+
+ for (ent = qdict_first(merged); ent; ent = qdict_next(merged, ent)) {
+ old_value = qdict_get(dest, ent->key);
+ if (old_value) {
+ if (qobject_type(old_value) != qobject_type(ent->value)) {
+ error_setg(errp, "Parameter '%s%s' used inconsistently",
+ str->str, ent->key);
+ return;
+ } else if (qobject_type(ent->value) == QTYPE_QDICT) {
+ /* Merge sub-dictionaries. */
+ g_string_append(str, ent->key);
+ g_string_append_c(str, '.');
+ keyval_do_merge(qobject_to(QDict, old_value),
+ qobject_to(QDict, ent->value),
+ str, errp);
+ g_string_truncate(str, save_len);
+ continue;
+ } else if (qobject_type(ent->value) == QTYPE_QLIST) {
+ /* Append to old list. */
+ QList *old = qobject_to(QList, old_value);
+ QList *new = qobject_to(QList, ent->value);
+ const QListEntry *item;
+ QLIST_FOREACH_ENTRY(new, item) {
+ qobject_ref(item->value);
+ qlist_append_obj(old, item->value);
+ }
+ continue;
+ } else {
+ assert(qobject_type(ent->value) == QTYPE_QSTRING);
+ }
+ }
+
+ qobject_ref(ent->value);
+ qdict_put_obj(dest, ent->key, ent->value);
+ }
+}
+
+/* Merge the @merged dictionary into @dest.
+ *
+ * The dictionaries are expected to be returned by the keyval parser, and
+ * therefore the only expected scalar type is the string. In case the same
+ * path is present in both @dest and @merged, the semantics are as follows:
+ *
+ * - lists are concatenated
+ *
+ * - dictionaries are merged recursively
+ *
+ * - for scalar values, @merged wins
+ *
+ * In case an error is reported, @dest may already have been modified.
+ *
+ * This function can be used to implement semantics analogous to QemuOpts's
+ * .merge_lists = true case, or to implement -set for options backed by QDicts.
+ *
+ * Note: while QemuOpts is commonly used so that repeated keys overwrite
+ * ("last one wins"), it can also be used so that repeated keys build up
+ * a list. keyval_merge() can only be used when the options' semantics are
+ * the former, not the latter.
+ */
+void keyval_merge(QDict *dest, const QDict *merged, Error **errp)
+{
+ GString *str;
+
+ str = g_string_new("");
+ keyval_do_merge(dest, merged, str, errp);
+ g_string_free(str, TRUE);
+}
+
/*
* Listify @cur recursively.
* Replace QDicts whose keys are all valid list indexes by QLists.
error_setg(errp, "Parameter '%s%d' missing", key, i);
g_free(key);
g_free(elt);
- QDECREF(list);
+ qobject_unref(list);
return NULL;
}
- qobject_incref(elt[i]);
+ qobject_ref(elt[i]);
qlist_append_obj(list, elt[i]);
}
/*
* Parse @params in QEMU's traditional KEY=VALUE,... syntax.
+ *
* If @implied_key, the first KEY= can be omitted. @implied_key is
* implied then, and VALUE can't be empty or contain ',' or '='.
- * On success, return a dictionary of the parsed keys and values.
- * On failure, store an error through @errp and return NULL.
+ *
+ * A parameter "help" or "?" without a value isn't added to the
+ * resulting dictionary, but instead is interpreted as help request.
+ * All other options are parsed and returned normally so that context
+ * specific help can be printed.
+ *
+ * If @p_help is not NULL, store whether help is requested there.
+ * If @p_help is NULL and help is requested, fail.
+ *
+ * On success, return @dict, now filled with the parsed keys and values.
+ *
+ * On failure, store an error through @errp and return NULL. Any keys
+ * and values parsed so far will be in @dict nevertheless.
*/
-QDict *keyval_parse(const char *params, const char *implied_key,
- Error **errp)
+QDict *keyval_parse_into(QDict *qdict, const char *params, const char *implied_key,
+ bool *p_help, Error **errp)
{
- QDict *qdict = qdict_new();
QObject *listified;
const char *s;
+ bool help = false;
s = params;
while (*s) {
- s = keyval_parse_one(qdict, s, implied_key, errp);
+ s = keyval_parse_one(qdict, s, implied_key, &help, errp);
if (!s) {
- QDECREF(qdict);
return NULL;
}
implied_key = NULL;
}
+ if (p_help) {
+ *p_help = help;
+ } else if (help) {
+ error_setg(errp, "Help is not available for this option");
+ return NULL;
+ }
+
listified = keyval_listify(qdict, NULL, errp);
if (!listified) {
- QDECREF(qdict);
return NULL;
}
assert(listified == QOBJECT(qdict));
return qdict;
}
+
+/*
+ * Parse @params in QEMU's traditional KEY=VALUE,... syntax.
+ *
+ * If @implied_key, the first KEY= can be omitted. @implied_key is
+ * implied then, and VALUE can't be empty or contain ',' or '='.
+ *
+ * A parameter "help" or "?" without a value isn't added to the
+ * resulting dictionary, but instead is interpreted as help request.
+ * All other options are parsed and returned normally so that context
+ * specific help can be printed.
+ *
+ * If @p_help is not NULL, store whether help is requested there.
+ * If @p_help is NULL and help is requested, fail.
+ *
+ * On success, return a dictionary of the parsed keys and values.
+ * On failure, store an error through @errp and return NULL.
+ */
+QDict *keyval_parse(const char *params, const char *implied_key,
+ bool *p_help, Error **errp)
+{
+ QDict *qdict = qdict_new();
+ QDict *ret = keyval_parse_into(qdict, params, implied_key, p_help, errp);
+
+ if (!ret) {
+ qobject_unref(qdict);
+ }
+ return ret;
+}