1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qapi/compat-policy.h"
15#include "qapi/error.h"
16#include "qemu/ctype.h"
17#include "qapi/qmp/qerror.h"
18
19CompatPolicy compat_policy;
20
21static 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
40bool 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 if ((special_features & (1u << QAPI_UNSTABLE))
53 && !compat_policy_input_ok1("Unstable",
54 policy->unstable_input,
55 error_class, kind, name, errp)) {
56 return false;
57 }
58 return true;
59}
60
61const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
62{
63 assert(val >= 0 && val < lookup->size);
64
65 return lookup->array[val];
66}
67
68int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
69 int def, Error **errp)
70{
71 int i;
72
73 if (!buf) {
74 return def;
75 }
76
77 for (i = 0; i < lookup->size; i++) {
78 if (!strcmp(buf, lookup->array[i])) {
79 return i;
80 }
81 }
82
83 error_setg(errp, "invalid parameter value: %s", buf);
84 return def;
85}
86
87bool qapi_bool_parse(const char *name, const char *value, bool *obj, Error **errp)
88{
89 if (g_str_equal(value, "on") ||
90 g_str_equal(value, "yes") ||
91 g_str_equal(value, "true") ||
92 g_str_equal(value, "y")) {
93 *obj = true;
94 return true;
95 }
96 if (g_str_equal(value, "off") ||
97 g_str_equal(value, "no") ||
98 g_str_equal(value, "false") ||
99 g_str_equal(value, "n")) {
100 *obj = false;
101 return true;
102 }
103
104 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
105 "'on' or 'off'");
106 return false;
107}
108
109
110
111
112
113
114
115
116
117
118
119
120int parse_qapi_name(const char *str, bool complete)
121{
122 const char *p = str;
123
124 if (*p == '_') {
125 p++;
126 if (*p != '_') {
127 return -1;
128 }
129 while (*++p) {
130 if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
131 break;
132 }
133 }
134
135 if (*p != '_') {
136 return -1;
137 }
138 p++;
139 }
140
141 if (!qemu_isalpha(*p)) {
142 return -1;
143 }
144 while (*++p) {
145 if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
146 break;
147 }
148 }
149
150 if (complete && *p) {
151 return -1;
152 }
153 return p - str;
154}
155