qxl: call qemu_spice_display_init_common for secondary devices
[qemu/ar7.git] / qapi / qapi-util.c
blob46eda7d1969977ea462c79d906fed4c40bf4957e
1 /*
2 * QAPI util functions
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.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
16 #include "qapi/util.h"
18 int qapi_enum_parse(const char * const lookup[], const char *buf,
19 int max, int def, Error **errp)
21 int i;
23 if (!buf) {
24 return def;
27 for (i = 0; i < max; i++) {
28 if (!strcmp(buf, lookup[i])) {
29 return i;
33 error_setg(errp, "invalid parameter value: %s", buf);
34 return def;
38 * Parse a valid QAPI name from @str.
39 * A valid name consists of letters, digits, hyphen and underscore.
40 * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
41 * may contain only letters, digits, hyphen and period.
42 * The special exception for enumeration names is not implemented.
43 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
44 * Keep this consistent with scripts/qapi.py!
45 * If @complete, the parse fails unless it consumes @str completely.
46 * Return its length on success, -1 on failure.
48 int parse_qapi_name(const char *str, bool complete)
50 const char *p = str;
52 if (*p == '_') { /* Downstream __RFQDN_ */
53 p++;
54 if (*p != '_') {
55 return -1;
57 while (*++p) {
58 if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
59 break;
63 if (*p != '_') {
64 return -1;
66 p++;
69 if (!qemu_isalpha(*p)) {
70 return -1;
72 while (*++p) {
73 if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
74 break;
78 if (complete && *p) {
79 return -1;
81 return p - str;