]>
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" |
7ce5fc63 | 14 | #include "qapi/compat-policy.h" |
da34e65c | 15 | #include "qapi/error.h" |
856dfd8a | 16 | #include "qemu/ctype.h" |
372bcb25 | 17 | #include "qapi/qmp/qerror.h" |
5b5f825d | 18 | |
7ce5fc63 MA |
19 | CompatPolicy compat_policy; |
20 | ||
21 | static bool compat_policy_input_ok1(const char *adjective, | |
22 | CompatPolicyInput policy, | |
23 | ErrorClass error_class, | |
24 | const char *kind, const char *name, | |
25 | Error **errp) | |
26 | { | |
27 | switch (policy) { | |
28 | case COMPAT_POLICY_INPUT_ACCEPT: | |
29 | return true; | |
30 | case COMPAT_POLICY_INPUT_REJECT: | |
31 | error_set(errp, error_class, "%s %s %s disabled by policy", | |
32 | adjective, kind, name); | |
33 | return false; | |
34 | case COMPAT_POLICY_INPUT_CRASH: | |
35 | default: | |
36 | abort(); | |
37 | } | |
38 | } | |
39 | ||
40 | bool compat_policy_input_ok(unsigned special_features, | |
41 | const CompatPolicy *policy, | |
42 | ErrorClass error_class, | |
43 | const char *kind, const char *name, | |
44 | Error **errp) | |
45 | { | |
46 | if ((special_features & 1u << QAPI_DEPRECATED) | |
47 | && !compat_policy_input_ok1("Deprecated", | |
48 | policy->deprecated_input, | |
49 | error_class, kind, name, errp)) { | |
50 | return false; | |
51 | } | |
52 | return true; | |
53 | } | |
54 | ||
f7abe0ec | 55 | const char *qapi_enum_lookup(const QEnumLookup *lookup, int val) |
5b5f825d | 56 | { |
f7abe0ec | 57 | assert(val >= 0 && val < lookup->size); |
5b5f825d | 58 | |
f7abe0ec | 59 | return lookup->array[val]; |
5b5f825d | 60 | } |
9e7dac7c | 61 | |
f7abe0ec | 62 | int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, |
06c60b6c | 63 | int def, Error **errp) |
9e7dac7c PL |
64 | { |
65 | int i; | |
66 | ||
67 | if (!buf) { | |
68 | return def; | |
69 | } | |
70 | ||
f7abe0ec MAL |
71 | for (i = 0; i < lookup->size; i++) { |
72 | if (!strcmp(buf, lookup->array[i])) { | |
9e7dac7c PL |
73 | return i; |
74 | } | |
75 | } | |
76 | ||
77 | error_setg(errp, "invalid parameter value: %s", buf); | |
78 | return def; | |
79 | } | |
069b64e3 | 80 | |
372bcb25 PB |
81 | bool qapi_bool_parse(const char *name, const char *value, bool *obj, Error **errp) |
82 | { | |
83 | if (g_str_equal(value, "on") || | |
84 | g_str_equal(value, "yes") || | |
85 | g_str_equal(value, "true") || | |
86 | g_str_equal(value, "y")) { | |
87 | *obj = true; | |
88 | return true; | |
89 | } | |
90 | if (g_str_equal(value, "off") || | |
91 | g_str_equal(value, "no") || | |
92 | g_str_equal(value, "false") || | |
93 | g_str_equal(value, "n")) { | |
94 | *obj = false; | |
95 | return true; | |
96 | } | |
97 | ||
98 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, | |
99 | "'on' or 'off'"); | |
100 | return false; | |
101 | } | |
102 | ||
069b64e3 MA |
103 | /* |
104 | * Parse a valid QAPI name from @str. | |
105 | * A valid name consists of letters, digits, hyphen and underscore. | |
106 | * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN | |
107 | * may contain only letters, digits, hyphen and period. | |
108 | * The special exception for enumeration names is not implemented. | |
b3125e73 | 109 | * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules. |
069b64e3 MA |
110 | * Keep this consistent with scripts/qapi.py! |
111 | * If @complete, the parse fails unless it consumes @str completely. | |
112 | * Return its length on success, -1 on failure. | |
113 | */ | |
114 | int parse_qapi_name(const char *str, bool complete) | |
115 | { | |
116 | const char *p = str; | |
117 | ||
118 | if (*p == '_') { /* Downstream __RFQDN_ */ | |
119 | p++; | |
120 | if (*p != '_') { | |
121 | return -1; | |
122 | } | |
123 | while (*++p) { | |
124 | if (!qemu_isalnum(*p) && *p != '-' && *p != '.') { | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
129 | if (*p != '_') { | |
130 | return -1; | |
131 | } | |
132 | p++; | |
133 | } | |
134 | ||
135 | if (!qemu_isalpha(*p)) { | |
136 | return -1; | |
137 | } | |
138 | while (*++p) { | |
139 | if (!qemu_isalnum(*p) && *p != '-' && *p != '_') { | |
140 | break; | |
141 | } | |
142 | } | |
143 | ||
144 | if (complete && *p) { | |
145 | return -1; | |
146 | } | |
147 | return p - str; | |
148 | } |