]>
Commit | Line | Data |
---|---|---|
9e7dac7c PL |
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 | ||
cbf21151 | 13 | #include "qemu/osdep.h" |
da34e65c | 14 | #include "qapi/error.h" |
856dfd8a | 15 | #include "qemu/ctype.h" |
372bcb25 | 16 | #include "qapi/qmp/qerror.h" |
5b5f825d | 17 | |
f7abe0ec | 18 | const char *qapi_enum_lookup(const QEnumLookup *lookup, int val) |
5b5f825d | 19 | { |
f7abe0ec | 20 | assert(val >= 0 && val < lookup->size); |
5b5f825d | 21 | |
f7abe0ec | 22 | return lookup->array[val]; |
5b5f825d | 23 | } |
9e7dac7c | 24 | |
f7abe0ec | 25 | int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, |
06c60b6c | 26 | int def, Error **errp) |
9e7dac7c PL |
27 | { |
28 | int i; | |
29 | ||
30 | if (!buf) { | |
31 | return def; | |
32 | } | |
33 | ||
f7abe0ec MAL |
34 | for (i = 0; i < lookup->size; i++) { |
35 | if (!strcmp(buf, lookup->array[i])) { | |
9e7dac7c PL |
36 | return i; |
37 | } | |
38 | } | |
39 | ||
40 | error_setg(errp, "invalid parameter value: %s", buf); | |
41 | return def; | |
42 | } | |
069b64e3 | 43 | |
372bcb25 PB |
44 | bool qapi_bool_parse(const char *name, const char *value, bool *obj, Error **errp) |
45 | { | |
46 | if (g_str_equal(value, "on") || | |
47 | g_str_equal(value, "yes") || | |
48 | g_str_equal(value, "true") || | |
49 | g_str_equal(value, "y")) { | |
50 | *obj = true; | |
51 | return true; | |
52 | } | |
53 | if (g_str_equal(value, "off") || | |
54 | g_str_equal(value, "no") || | |
55 | g_str_equal(value, "false") || | |
56 | g_str_equal(value, "n")) { | |
57 | *obj = false; | |
58 | return true; | |
59 | } | |
60 | ||
61 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, | |
62 | "'on' or 'off'"); | |
63 | return false; | |
64 | } | |
65 | ||
069b64e3 MA |
66 | /* |
67 | * Parse a valid QAPI name from @str. | |
68 | * A valid name consists of letters, digits, hyphen and underscore. | |
69 | * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN | |
70 | * may contain only letters, digits, hyphen and period. | |
71 | * The special exception for enumeration names is not implemented. | |
b3125e73 | 72 | * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules. |
069b64e3 MA |
73 | * Keep this consistent with scripts/qapi.py! |
74 | * If @complete, the parse fails unless it consumes @str completely. | |
75 | * Return its length on success, -1 on failure. | |
76 | */ | |
77 | int parse_qapi_name(const char *str, bool complete) | |
78 | { | |
79 | const char *p = str; | |
80 | ||
81 | if (*p == '_') { /* Downstream __RFQDN_ */ | |
82 | p++; | |
83 | if (*p != '_') { | |
84 | return -1; | |
85 | } | |
86 | while (*++p) { | |
87 | if (!qemu_isalnum(*p) && *p != '-' && *p != '.') { | |
88 | break; | |
89 | } | |
90 | } | |
91 | ||
92 | if (*p != '_') { | |
93 | return -1; | |
94 | } | |
95 | p++; | |
96 | } | |
97 | ||
98 | if (!qemu_isalpha(*p)) { | |
99 | return -1; | |
100 | } | |
101 | while (*++p) { | |
102 | if (!qemu_isalnum(*p) && *p != '-' && *p != '_') { | |
103 | break; | |
104 | } | |
105 | } | |
106 | ||
107 | if (complete && *p) { | |
108 | return -1; | |
109 | } | |
110 | return p - str; | |
111 | } |