blob: e9b266bb70962b1c37141ce2ca006645ad83e72e [file] [log] [blame]
Peter Lieven9e7dac72014-08-13 19:20:17 +02001/*
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
Peter Maydellcbf21152016-01-29 17:49:57 +000013#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Peter Lieven9e7dac72014-08-13 19:20:17 +020015#include "qemu-common.h"
Markus Armbruster5b5f8252017-08-24 10:46:07 +020016
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +020017const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
Markus Armbruster5b5f8252017-08-24 10:46:07 +020018{
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +020019 assert(val >= 0 && val < lookup->size);
Markus Armbruster5b5f8252017-08-24 10:46:07 +020020
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +020021 return lookup->array[val];
Markus Armbruster5b5f8252017-08-24 10:46:07 +020022}
Peter Lieven9e7dac72014-08-13 19:20:17 +020023
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +020024int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
Markus Armbruster06c60b62017-08-24 10:45:57 +020025 int def, Error **errp)
Peter Lieven9e7dac72014-08-13 19:20:17 +020026{
27 int i;
28
29 if (!buf) {
30 return def;
31 }
32
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +020033 for (i = 0; i < lookup->size; i++) {
34 if (!strcmp(buf, lookup->array[i])) {
Peter Lieven9e7dac72014-08-13 19:20:17 +020035 return i;
36 }
37 }
38
39 error_setg(errp, "invalid parameter value: %s", buf);
40 return def;
41}
Markus Armbruster069b64e2017-02-28 22:27:04 +010042
43/*
44 * Parse a valid QAPI name from @str.
45 * A valid name consists of letters, digits, hyphen and underscore.
46 * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
47 * may contain only letters, digits, hyphen and period.
48 * The special exception for enumeration names is not implemented.
Philippe Mathieu-Daudéb3125e72017-07-28 19:46:03 -030049 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
Markus Armbruster069b64e2017-02-28 22:27:04 +010050 * Keep this consistent with scripts/qapi.py!
51 * If @complete, the parse fails unless it consumes @str completely.
52 * Return its length on success, -1 on failure.
53 */
54int parse_qapi_name(const char *str, bool complete)
55{
56 const char *p = str;
57
58 if (*p == '_') { /* Downstream __RFQDN_ */
59 p++;
60 if (*p != '_') {
61 return -1;
62 }
63 while (*++p) {
64 if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
65 break;
66 }
67 }
68
69 if (*p != '_') {
70 return -1;
71 }
72 p++;
73 }
74
75 if (!qemu_isalpha(*p)) {
76 return -1;
77 }
78 while (*++p) {
79 if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
80 break;
81 }
82 }
83
84 if (complete && *p) {
85 return -1;
86 }
87 return p - str;
88}