]>
Commit | Line | Data |
---|---|---|
9e7dac7c PL |
1 | /* |
2 | * QAPI util functions | |
3 | * | |
4 | * Authors: | |
5 | * Hu Tao <[email protected]> | |
6 | * Peter Lieven <[email protected]> | |
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 | ||
cbf21151 | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
9e7dac7c | 15 | #include "qemu-common.h" |
5b5f825d | 16 | |
f7abe0ec | 17 | const char *qapi_enum_lookup(const QEnumLookup *lookup, int val) |
5b5f825d | 18 | { |
f7abe0ec | 19 | assert(val >= 0 && val < lookup->size); |
5b5f825d | 20 | |
f7abe0ec | 21 | return lookup->array[val]; |
5b5f825d | 22 | } |
9e7dac7c | 23 | |
f7abe0ec | 24 | int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, |
06c60b6c | 25 | int def, Error **errp) |
9e7dac7c PL |
26 | { |
27 | int i; | |
28 | ||
29 | if (!buf) { | |
30 | return def; | |
31 | } | |
32 | ||
f7abe0ec MAL |
33 | for (i = 0; i < lookup->size; i++) { |
34 | if (!strcmp(buf, lookup->array[i])) { | |
9e7dac7c PL |
35 | return i; |
36 | } | |
37 | } | |
38 | ||
39 | error_setg(errp, "invalid parameter value: %s", buf); | |
40 | return def; | |
41 | } | |
069b64e3 MA |
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. | |
b3125e73 | 49 | * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules. |
069b64e3 MA |
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 | */ | |
54 | int 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 | } |