5 * Hu Tao <hutao@cn.fujitsu.com>
6 * Peter Lieven <pl@kamp.de>
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
,
28 case COMPAT_POLICY_INPUT_ACCEPT
:
30 case COMPAT_POLICY_INPUT_REJECT
:
31 error_set(errp
, error_class
, "%s %s %s disabled by policy",
32 adjective
, kind
, name
);
34 case COMPAT_POLICY_INPUT_CRASH
:
40 bool compat_policy_input_ok(unsigned special_features
,
41 const CompatPolicy
*policy
,
42 ErrorClass error_class
,
43 const char *kind
, const char *name
,
46 if ((special_features
& 1u << QAPI_DEPRECATED
)
47 && !compat_policy_input_ok1("Deprecated",
48 policy
->deprecated_input
,
49 error_class
, kind
, name
, errp
)) {
52 if ((special_features
& (1u << QAPI_UNSTABLE
))
53 && !compat_policy_input_ok1("Unstable",
54 policy
->unstable_input
,
55 error_class
, kind
, name
, errp
)) {
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
)
77 for (i
= 0; i
< lookup
->size
; i
++) {
78 if (!strcmp(buf
, lookup
->array
[i
])) {
83 error_setg(errp
, "invalid parameter value: %s", buf
);
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")) {
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")) {
104 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
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.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
)
124 if (*p
== '_') { /* Downstream __RFQDN_ */
130 if (!qemu_isalnum(*p
) && *p
!= '-' && *p
!= '.') {
141 if (!qemu_isalpha(*p
)) {
145 if (!qemu_isalnum(*p
) && *p
!= '-' && *p
!= '_') {
150 if (complete
&& *p
) {