aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2020-10-16 17:39:01 +0100
committerPeter Maydell <peter.maydell@linaro.org>2020-10-16 17:39:01 +0100
commit7daf8f8d011cdd5d3e86930ed2bde969425c790c (patch)
tree739f3444d783df6306c599e1dfdf0eff8d757a5a /util
parent6214addcc6b6bf6b444934833144bab10cbe6a1a (diff)
parente1c4269763999e3b359fff19ad170e0110d3b457 (diff)
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
Block layer patches: - qemu-storage-daemon: Remove QemuOpts from --object parser - monitor: Fix order in monitor_cleanup() - Deprecate the sheepdog block driver # gpg: Signature made Thu 15 Oct 2020 15:48:10 BST # gpg: using RSA key DC3DEB159A9AF95D3D7456FE7F09B272C88F2FD6 # gpg: issuer "kwolf@redhat.com" # gpg: Good signature from "Kevin Wolf <kwolf@redhat.com>" [full] # Primary key fingerprint: DC3D EB15 9A9A F95D 3D74 56FE 7F09 B272 C88F 2FD6 * remotes/kevin/tags/for-upstream: block: deprecate the sheepdog block driver block: drop moderated sheepdog mailing list from MAINTAINERS file monitor: Fix order in monitor_cleanup() qemu-storage-daemon: Remove QemuOpts from --object parser qom: Add user_creatable_print_help_from_qdict() qom: Factor out helpers from user_creatable_print_help() keyval: Parse help options keyval: Fix parsing of ',' in value of implied key test-keyval: Demonstrate misparse of ',' with implied key keyval: Fix and clarify grammar Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'util')
-rw-r--r--util/keyval.c103
1 files changed, 76 insertions, 27 deletions
diff --git a/util/keyval.c b/util/keyval.c
index 13def4af54..7f625ad33c 100644
--- a/util/keyval.c
+++ b/util/keyval.c
@@ -14,10 +14,11 @@
* 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 = / [^=,.]+ /
+ * val = { / [^,]+ / | ',,' }
+ * help = 'help' | '?'
*
* Semantics defined by reduction to JSON:
*
@@ -54,6 +55,9 @@
*
* 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
@@ -71,12 +75,16 @@
* 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"
@@ -85,6 +93,7 @@
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qstring.h"
#include "qemu/cutils.h"
+#include "qemu/help_option.h"
#include "qemu/option.h"
/*
@@ -158,18 +167,23 @@ static QObject *keyval_parse_put(QDict *cur,
}
/*
- * 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;
@@ -178,11 +192,23 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
QString *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;
@@ -237,7 +263,11 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
if (key == implied_key) {
assert(!*s);
- s = params;
+ val = qstring_from_substr(params, 0, val_end - params);
+ s = val_end;
+ if (*s == ',') {
+ s++;
+ }
} else {
if (*s != '=') {
error_setg(errp, "Expected '=' after parameter '%.*s'",
@@ -245,19 +275,19 @@ static const char *keyval_parse_one(QDict *qdict, const char *params,
return NULL;
}
s++;
- }
- val = qstring_new();
- for (;;) {
- if (!*s) {
- break;
- } else if (*s == ',') {
- s++;
- if (*s != ',') {
+ val = qstring_new();
+ for (;;) {
+ if (!*s) {
break;
+ } else if (*s == ',') {
+ s++;
+ if (*s != ',') {
+ break;
+ }
}
+ qstring_append_chr(val, *s++);
}
- qstring_append_chr(val, *s++);
}
if (!keyval_parse_put(cur, key_in_cur, val, key, key_end, errp)) {
@@ -388,21 +418,32 @@ static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)
/*
* 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,
- Error **errp)
+ 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) {
qobject_unref(qdict);
return NULL;
@@ -410,6 +451,14 @@ QDict *keyval_parse(const char *params, const char *implied_key,
implied_key = NULL;
}
+ if (p_help) {
+ *p_help = help;
+ } else if (help) {
+ error_setg(errp, "Help is not available for this option");
+ qobject_unref(qdict);
+ return NULL;
+ }
+
listified = keyval_listify(qdict, NULL, errp);
if (!listified) {
qobject_unref(qdict);