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/error.h"
15 #include "qemu/ctype.h"
16 #include "qapi/qmp/qerror.h"
18 const char *qapi_enum_lookup(const QEnumLookup
*lookup
, int val
)
20 assert(val
>= 0 && val
< lookup
->size
);
22 return lookup
->array
[val
];
25 int qapi_enum_parse(const QEnumLookup
*lookup
, const char *buf
,
26 int def
, Error
**errp
)
34 for (i
= 0; i
< lookup
->size
; i
++) {
35 if (!strcmp(buf
, lookup
->array
[i
])) {
40 error_setg(errp
, "invalid parameter value: %s", buf
);
44 bool qapi_bool_parse(const char *name
, const char *value
, bool *obj
, Error
**errp
)
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")) {
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")) {
61 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
,
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.
72 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
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.
77 int parse_qapi_name(const char *str
, bool complete
)
81 if (*p
== '_') { /* Downstream __RFQDN_ */
87 if (!qemu_isalnum(*p
) && *p
!= '-' && *p
!= '.') {
98 if (!qemu_isalpha(*p
)) {
102 if (!qemu_isalnum(*p
) && *p
!= '-' && *p
!= '_') {
107 if (complete
&& *p
) {