blob: ee7594ff19946eb53a1b9f3e1411c86df912a2dc [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"
Peter Lieven9e7dac72014-08-13 19:20:17 +020016#include "qapi/util.h"
17
Daniel P. Berrange2e4450f2015-05-13 17:14:07 +010018int qapi_enum_parse(const char * const lookup[], const char *buf,
Markus Armbruster06c60b62017-08-24 10:45:57 +020019 int def, Error **errp)
Peter Lieven9e7dac72014-08-13 19:20:17 +020020{
21 int i;
22
23 if (!buf) {
24 return def;
25 }
26
Markus Armbruster06c60b62017-08-24 10:45:57 +020027 for (i = 0; lookup[i]; i++) {
Peter Lieven9e7dac72014-08-13 19:20:17 +020028 if (!strcmp(buf, lookup[i])) {
29 return i;
30 }
31 }
32
33 error_setg(errp, "invalid parameter value: %s", buf);
34 return def;
35}
Markus Armbruster069b64e2017-02-28 22:27:04 +010036
37/*
38 * Parse a valid QAPI name from @str.
39 * A valid name consists of letters, digits, hyphen and underscore.
40 * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
41 * may contain only letters, digits, hyphen and period.
42 * The special exception for enumeration names is not implemented.
Philippe Mathieu-Daudéb3125e72017-07-28 19:46:03 -030043 * See docs/devel/qapi-code-gen.txt for more on QAPI naming rules.
Markus Armbruster069b64e2017-02-28 22:27:04 +010044 * Keep this consistent with scripts/qapi.py!
45 * If @complete, the parse fails unless it consumes @str completely.
46 * Return its length on success, -1 on failure.
47 */
48int parse_qapi_name(const char *str, bool complete)
49{
50 const char *p = str;
51
52 if (*p == '_') { /* Downstream __RFQDN_ */
53 p++;
54 if (*p != '_') {
55 return -1;
56 }
57 while (*++p) {
58 if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
59 break;
60 }
61 }
62
63 if (*p != '_') {
64 return -1;
65 }
66 p++;
67 }
68
69 if (!qemu_isalpha(*p)) {
70 return -1;
71 }
72 while (*++p) {
73 if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
74 break;
75 }
76 }
77
78 if (complete && *p) {
79 return -1;
80 }
81 return p - str;
82}