qemu/util/keyval.c
<<
>>
Prefs
   1/*
   2 * Parsing KEY=VALUE,... strings
   3 *
   4 * Copyright (C) 2017 Red Hat Inc.
   5 *
   6 * Authors:
   7 *  Markus Armbruster <armbru@redhat.com>,
   8 *
   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.
  11 */
  12
  13/*
  14 * KEY=VALUE,... syntax:
  15 *
  16 *   key-vals     = [ key-val { ',' key-val } [ ',' ] ]
  17 *   key-val      = key '=' val
  18 *   key          = key-fragment { '.' key-fragment }
  19 *   key-fragment = / [^=,.]* /
  20 *   val          = { / [^,]* / | ',,' }
  21 *
  22 * Semantics defined by reduction to JSON:
  23 *
  24 *   key-vals specifies a JSON object, i.e. a tree whose root is an
  25 *   object, inner nodes other than the root are objects or arrays,
  26 *   and leaves are strings.
  27 *
  28 *   Each key-val = key-fragment '.' ... '=' val specifies a path from
  29 *   root to a leaf (left of '='), and the leaf's value (right of
  30 *   '=').
  31 *
  32 *   A path from the root is defined recursively:
  33 *       L '.' key-fragment is a child of the node denoted by path L
  34 *       key-fragment is a child of the tree root
  35 *   If key-fragment is numeric, the parent is an array and the child
  36 *   is its key-fragment-th member, counting from zero.
  37 *   Else, the parent is an object, and the child is its member named
  38 *   key-fragment.
  39 *
  40 *   This constrains inner nodes to be either array or object.  The
  41 *   constraints must be satisfiable.  Counter-example: a.b=1,a=2 is
  42 *   not, because root.a must be an object to satisfy a.b=1 and a
  43 *   string to satisfy a=2.
  44 *
  45 *   Array subscripts can occur in any order, but the set of
  46 *   subscripts must not have gaps.  For instance, a.1=v is not okay,
  47 *   because root.a[0] is missing.
  48 *
  49 *   If multiple key-val denote the same leaf, the last one determines
  50 *   the value.
  51 *
  52 * Key-fragments must be valid QAPI names or consist only of decimal
  53 * digits.
  54 *
  55 * The length of any key-fragment must be between 1 and 127.
  56 *
  57 * Design flaw: there is no way to denote an empty array or non-root
  58 * object.  While interpreting "key absent" as empty seems natural
  59 * (removing a key-val from the input string removes the member when
  60 * there are more, so why not when it's the last), it doesn't work:
  61 * "key absent" already means "optional object/array absent", which
  62 * isn't the same as "empty object/array present".
  63 *
  64 * Design flaw: scalar values can only be strings; there is no way to
  65 * denote numbers, true, false or null.  The special QObject input
  66 * visitor returned by qobject_input_visitor_new_keyval() mostly hides
  67 * this by automatically converting strings to the type the visitor
  68 * expects.  Breaks down for type 'any', where the visitor's
  69 * expectation isn't clear.  Code visiting 'any' needs to do the
  70 * conversion itself, but only when using this keyval visitor.
  71 * Awkward.  Note that we carefully restrict alternate types to avoid
  72 * similar ambiguity.
  73 *
  74 * Additional syntax for use with an implied key:
  75 *
  76 *   key-vals-ik  = val-no-key [ ',' key-vals ]
  77 *   val-no-key   = / [^=,]* /
  78 *
  79 * where no-key is syntactic sugar for implied-key=val-no-key.
  80 */
  81
  82#include "qemu/osdep.h"
  83#include "qapi/error.h"
  84#include "qapi/qmp/qstring.h"
  85#include "qemu/cutils.h"
  86#include "qemu/option.h"
  87
  88/*
  89 * Convert @key to a list index.
  90 * Convert all leading decimal digits to a (non-negative) number,
  91 * capped at INT_MAX.
  92 * If @end is non-null, assign a pointer to the first character after
  93 * the number to *@end.
  94 * Else, fail if any characters follow.
  95 * On success, return the converted number.
  96 * On failure, return a negative value.
  97 * Note: since only digits are converted, no two keys can map to the
  98 * same number, except by overflow to INT_MAX.
  99 */
 100static int key_to_index(const char *key, const char **end)
 101{
 102    int ret;
 103    unsigned long index;
 104
 105    if (*key < '0' || *key > '9') {
 106        return -EINVAL;
 107    }
 108    ret = qemu_strtoul(key, end, 10, &index);
 109    if (ret) {
 110        return ret == -ERANGE ? INT_MAX : ret;
 111    }
 112    return index <= INT_MAX ? index : INT_MAX;
 113}
 114
 115/*
 116 * Ensure @cur maps @key_in_cur the right way.
 117 * If @value is null, it needs to map to a QDict, else to this
 118 * QString.
 119 * If @cur doesn't have @key_in_cur, put an empty QDict or @value,
 120 * respectively.
 121 * Else, if it needs to map to a QDict, and already does, do nothing.
 122 * Else, if it needs to map to this QString, and already maps to a
 123 * QString, replace it by @value.
 124 * Else, fail because we have conflicting needs on how to map
 125 * @key_in_cur.
 126 * In any case, take over the reference to @value, i.e. if the caller
 127 * wants to hold on to a reference, it needs to QINCREF().
 128 * Use @key up to @key_cursor to identify the key in error messages.
 129 * On success, return the mapped value.
 130 * On failure, store an error through @errp and return NULL.
 131 */
 132static QObject *keyval_parse_put(QDict *cur,
 133                                 const char *key_in_cur, QString *value,
 134                                 const char *key, const char *key_cursor,
 135                                 Error **errp)
 136{
 137    QObject *old, *new;
 138
 139    old = qdict_get(cur, key_in_cur);
 140    if (old) {
 141        if (qobject_type(old) != (value ? QTYPE_QSTRING : QTYPE_QDICT)) {
 142            error_setg(errp, "Parameters '%.*s.*' used inconsistently",
 143                       (int)(key_cursor - key), key);
 144            QDECREF(value);
 145            return NULL;
 146        }
 147        if (!value) {
 148            return old;         /* already QDict, do nothing */
 149        }
 150        new = QOBJECT(value);   /* replacement */
 151    } else {
 152        new = value ? QOBJECT(value) : QOBJECT(qdict_new());
 153    }
 154    qdict_put_obj(cur, key_in_cur, new);
 155    return new;
 156}
 157
 158/*
 159 * Parse one KEY=VALUE from @params, store result in @qdict.
 160 * The first fragment of KEY applies to @qdict.  Subsequent fragments
 161 * apply to nested QDicts, which are created on demand.  @implied_key
 162 * is as in keyval_parse().
 163 * On success, return a pointer to the next KEY=VALUE, or else to '\0'.
 164 * On failure, return NULL.
 165 */
 166static const char *keyval_parse_one(QDict *qdict, const char *params,
 167                                    const char *implied_key,
 168                                    Error **errp)
 169{
 170    const char *key, *key_end, *s, *end;
 171    size_t len;
 172    char key_in_cur[128];
 173    QDict *cur;
 174    int ret;
 175    QObject *next;
 176    QString *val;
 177
 178    key = params;
 179    len = strcspn(params, "=,");
 180    if (implied_key && len && key[len] != '=') {
 181        /* Desugar implied key */
 182        key = implied_key;
 183        len = strlen(implied_key);
 184    }
 185    key_end = key + len;
 186
 187    /*
 188     * Loop over key fragments: @s points to current fragment, it
 189     * applies to @cur.  @key_in_cur[] holds the previous fragment.
 190     */
 191    cur = qdict;
 192    s = key;
 193    for (;;) {
 194        /* Want a key index (unless it's first) or a QAPI name */
 195        if (s != key && key_to_index(s, &end) >= 0) {
 196            len = end - s;
 197        } else {
 198            ret = parse_qapi_name(s, false);
 199            len = ret < 0 ? 0 : ret;
 200        }
 201        assert(s + len <= key_end);
 202        if (!len || (s + len < key_end && s[len] != '.')) {
 203            assert(key != implied_key);
 204            error_setg(errp, "Invalid parameter '%.*s'",
 205                       (int)(key_end - key), key);
 206            return NULL;
 207        }
 208        if (len >= sizeof(key_in_cur)) {
 209            assert(key != implied_key);
 210            error_setg(errp, "Parameter%s '%.*s' is too long",
 211                       s != key || s + len != key_end ? " fragment" : "",
 212                       (int)len, s);
 213            return NULL;
 214        }
 215
 216        if (s != key) {
 217            next = keyval_parse_put(cur, key_in_cur, NULL,
 218                                    key, s - 1, errp);
 219            if (!next) {
 220                return NULL;
 221            }
 222            cur = qobject_to_qdict(next);
 223            assert(cur);
 224        }
 225
 226        memcpy(key_in_cur, s, len);
 227        key_in_cur[len] = 0;
 228        s += len;
 229
 230        if (*s != '.') {
 231            break;
 232        }
 233        s++;
 234    }
 235
 236    if (key == implied_key) {
 237        assert(!*s);
 238        s = params;
 239    } else {
 240        if (*s != '=') {
 241            error_setg(errp, "Expected '=' after parameter '%.*s'",
 242                       (int)(s - key), key);
 243            return NULL;
 244        }
 245        s++;
 246    }
 247
 248    val = qstring_new();
 249    for (;;) {
 250        if (!*s) {
 251            break;
 252        } else if (*s == ',') {
 253            s++;
 254            if (*s != ',') {
 255                break;
 256            }
 257        }
 258        qstring_append_chr(val, *s++);
 259    }
 260
 261    if (!keyval_parse_put(cur, key_in_cur, val, key, key_end, errp)) {
 262        return NULL;
 263    }
 264    return s;
 265}
 266
 267static char *reassemble_key(GSList *key)
 268{
 269    GString *s = g_string_new("");
 270    GSList *p;
 271
 272    for (p = key; p; p = p->next) {
 273        g_string_prepend_c(s, '.');
 274        g_string_prepend(s, (char *)p->data);
 275    }
 276
 277    return g_string_free(s, FALSE);
 278}
 279
 280/*
 281 * Listify @cur recursively.
 282 * Replace QDicts whose keys are all valid list indexes by QLists.
 283 * @key_of_cur is the list of key fragments leading up to @cur.
 284 * On success, return either @cur or its replacement.
 285 * On failure, store an error through @errp and return NULL.
 286 */
 287static QObject *keyval_listify(QDict *cur, GSList *key_of_cur, Error **errp)
 288{
 289    GSList key_node;
 290    bool has_index, has_member;
 291    const QDictEntry *ent;
 292    QDict *qdict;
 293    QObject *val;
 294    char *key;
 295    size_t nelt;
 296    QObject **elt;
 297    int index, max_index, i;
 298    QList *list;
 299
 300    key_node.next = key_of_cur;
 301
 302    /*
 303     * Recursively listify @cur's members, and figure out whether @cur
 304     * itself is to be listified.
 305     */
 306    has_index = false;
 307    has_member = false;
 308    for (ent = qdict_first(cur); ent; ent = qdict_next(cur, ent)) {
 309        if (key_to_index(ent->key, NULL) >= 0) {
 310            has_index = true;
 311        } else {
 312            has_member = true;
 313        }
 314
 315        qdict = qobject_to_qdict(ent->value);
 316        if (!qdict) {
 317            continue;
 318        }
 319
 320        key_node.data = ent->key;
 321        val = keyval_listify(qdict, &key_node, errp);
 322        if (!val) {
 323            return NULL;
 324        }
 325        if (val != ent->value) {
 326            qdict_put_obj(cur, ent->key, val);
 327        }
 328    }
 329
 330    if (has_index && has_member) {
 331        key = reassemble_key(key_of_cur);
 332        error_setg(errp, "Parameters '%s*' used inconsistently", key);
 333        g_free(key);
 334        return NULL;
 335    }
 336    if (!has_index) {
 337        return QOBJECT(cur);
 338    }
 339
 340    /* Copy @cur's values to @elt[] */
 341    nelt = qdict_size(cur) + 1; /* one extra, for use as sentinel */
 342    elt = g_new0(QObject *, nelt);
 343    max_index = -1;
 344    for (ent = qdict_first(cur); ent; ent = qdict_next(cur, ent)) {
 345        index = key_to_index(ent->key, NULL);
 346        assert(index >= 0);
 347        if (index > max_index) {
 348            max_index = index;
 349        }
 350        /*
 351         * We iterate @nelt times.  If we get one exceeding @nelt
 352         * here, we will put less than @nelt values into @elt[],
 353         * triggering the error in the next loop.
 354         */
 355        if ((size_t)index >= nelt - 1) {
 356            continue;
 357        }
 358        /* Even though dict keys are distinct, indexes need not be */
 359        elt[index] = ent->value;
 360    }
 361
 362    /*
 363     * Make a list from @elt[], reporting the first missing element,
 364     * if any.
 365     * If we dropped an index >= nelt in the previous loop, this loop
 366     * will run into the sentinel and report index @nelt missing.
 367     */
 368    list = qlist_new();
 369    assert(!elt[nelt-1]);       /* need the sentinel to be null */
 370    for (i = 0; i < MIN(nelt, max_index + 1); i++) {
 371        if (!elt[i]) {
 372            key = reassemble_key(key_of_cur);
 373            error_setg(errp, "Parameter '%s%d' missing", key, i);
 374            g_free(key);
 375            g_free(elt);
 376            QDECREF(list);
 377            return NULL;
 378        }
 379        qobject_incref(elt[i]);
 380        qlist_append_obj(list, elt[i]);
 381    }
 382
 383    g_free(elt);
 384    return QOBJECT(list);
 385}
 386
 387/*
 388 * Parse @params in QEMU's traditional KEY=VALUE,... syntax.
 389 * If @implied_key, the first KEY= can be omitted.  @implied_key is
 390 * implied then, and VALUE can't be empty or contain ',' or '='.
 391 * On success, return a dictionary of the parsed keys and values.
 392 * On failure, store an error through @errp and return NULL.
 393 */
 394QDict *keyval_parse(const char *params, const char *implied_key,
 395                    Error **errp)
 396{
 397    QDict *qdict = qdict_new();
 398    QObject *listified;
 399    const char *s;
 400
 401    s = params;
 402    while (*s) {
 403        s = keyval_parse_one(qdict, s, implied_key, errp);
 404        if (!s) {
 405            QDECREF(qdict);
 406            return NULL;
 407        }
 408        implied_key = NULL;
 409    }
 410
 411    listified = keyval_listify(qdict, NULL, errp);
 412    if (!listified) {
 413        QDECREF(qdict);
 414        return NULL;
 415    }
 416    assert(listified == QOBJECT(qdict));
 417    return qdict;
 418}
 419