qemu/qapi/qapi-util.c
<<
>>
Prefs
   1/*
   2 * QAPI util functions
   3 *
   4 * Authors:
   5 *  Hu Tao       <hutao@cn.fujitsu.com>
   6 *  Peter Lieven <pl@kamp.de>
   7 * 
   8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
   9 * See the COPYING.LIB file in the top-level directory.
  10 *
  11 */
  12
  13#include "qemu/osdep.h"
  14#include "qapi/error.h"
  15#include "qemu/ctype.h"
  16
  17const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
  18{
  19    assert(val >= 0 && val < lookup->size);
  20
  21    return lookup->array[val];
  22}
  23
  24int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
  25                    int def, Error **errp)
  26{
  27    int i;
  28
  29    if (!buf) {
  30        return def;
  31    }
  32
  33    for (i = 0; i < lookup->size; i++) {
  34        if (!strcmp(buf, lookup->array[i])) {
  35            return i;
  36        }
  37    }
  38
  39    error_setg(errp, "invalid parameter value: %s", buf);
  40    return def;
  41}
  42
  43/*
  44 * Parse a valid QAPI name from @str.
  45 * A valid name consists of letters, digits, hyphen and underscore.
  46 * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
  47 * may contain only letters, digits, hyphen and period.
  48 * The special exception for enumeration names is not implemented.
  49 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
  50 * Keep this consistent with scripts/qapi.py!
  51 * If @complete, the parse fails unless it consumes @str completely.
  52 * Return its length on success, -1 on failure.
  53 */
  54int parse_qapi_name(const char *str, bool complete)
  55{
  56    const char *p = str;
  57
  58    if (*p == '_') {            /* Downstream __RFQDN_ */
  59        p++;
  60        if (*p != '_') {
  61            return -1;
  62        }
  63        while (*++p) {
  64            if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
  65                break;
  66            }
  67        }
  68
  69        if (*p != '_') {
  70            return -1;
  71        }
  72        p++;
  73    }
  74
  75    if (!qemu_isalpha(*p)) {
  76        return -1;
  77    }
  78    while (*++p) {
  79        if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
  80            break;
  81        }
  82    }
  83
  84    if (complete && *p) {
  85        return -1;
  86    }
  87    return p - str;
  88}
  89