MAINTAINERS: Add include folder to the hw/char/ section
[qemu/kevin.git] / qapi / qapi-util.c
blob63596e11c569285cd530ae6e57b61d02bab72157
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/compat-policy.h"
15 #include "qapi/error.h"
16 #include "qemu/ctype.h"
17 #include "qapi/qmp/qerror.h"
19 CompatPolicy compat_policy;
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)
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();
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)
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;
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;
58 return true;
61 const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
63 assert(val >= 0 && val < lookup->size);
65 return lookup->array[val];
68 int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
69 int def, Error **errp)
71 int i;
73 if (!buf) {
74 return def;
77 for (i = 0; i < lookup->size; i++) {
78 if (!strcmp(buf, lookup->array[i])) {
79 return i;
83 error_setg(errp, "invalid parameter value: %s", buf);
84 return def;
87 bool qapi_bool_parse(const char *name, const char *value, bool *obj, Error **errp)
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;
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;
104 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
105 "'on' or 'off'");
106 return false;
110 * Parse a valid QAPI name from @str.
111 * A valid name consists of letters, digits, hyphen and underscore.
112 * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
113 * may contain only letters, digits, hyphen and period.
114 * The special exception for enumeration names is not implemented.
115 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
116 * Keep this consistent with scripts/qapi-gen.py!
117 * If @complete, the parse fails unless it consumes @str completely.
118 * Return its length on success, -1 on failure.
120 int parse_qapi_name(const char *str, bool complete)
122 const char *p = str;
124 if (*p == '_') { /* Downstream __RFQDN_ */
125 p++;
126 if (*p != '_') {
127 return -1;
129 while (*++p) {
130 if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
131 break;
135 if (*p != '_') {
136 return -1;
138 p++;
141 if (!qemu_isalpha(*p)) {
142 return -1;
144 while (*++p) {
145 if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
146 break;
150 if (complete && *p) {
151 return -1;
153 return p - str;