/src/openvswitch/lib/ovsdb-parser.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2009, 2011, 2013, 2015, 2016 Nicira, Inc. |
2 | | * |
3 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | * you may not use this file except in compliance with the License. |
5 | | * You may obtain a copy of the License at: |
6 | | * |
7 | | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | | * |
9 | | * Unless required by applicable law or agreed to in writing, software |
10 | | * distributed under the License is distributed on an "AS IS" BASIS, |
11 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | * See the License for the specific language governing permissions and |
13 | | * limitations under the License. |
14 | | */ |
15 | | |
16 | | #include <config.h> |
17 | | |
18 | | #include "ovsdb-parser.h" |
19 | | |
20 | | #include <ctype.h> |
21 | | #include <stdarg.h> |
22 | | |
23 | | #include "ovsdb-error.h" |
24 | | |
25 | | void |
26 | | ovsdb_parser_init(struct ovsdb_parser *parser, const struct json *json, |
27 | | const char *name, ...) |
28 | 0 | { |
29 | 0 | va_list args; |
30 | |
|
31 | 0 | va_start(args, name); |
32 | 0 | parser->name = xvasprintf(name, args); |
33 | 0 | va_end(args); |
34 | |
|
35 | 0 | sset_init(&parser->used); |
36 | 0 | parser->error = NULL; |
37 | |
|
38 | 0 | parser->json = (json && json->type == JSON_OBJECT ? json : NULL); |
39 | 0 | if (!parser->json) { |
40 | 0 | ovsdb_parser_raise_error(parser, "Object expected."); |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | | bool |
45 | | ovsdb_parser_is_id(const char *string) |
46 | 0 | { |
47 | 0 | unsigned char c; |
48 | |
|
49 | 0 | c = *string; |
50 | 0 | if (!isalpha(c) && c != '_') { |
51 | 0 | return false; |
52 | 0 | } |
53 | | |
54 | 0 | for (;;) { |
55 | 0 | c = *++string; |
56 | 0 | if (c == '\0') { |
57 | 0 | return true; |
58 | 0 | } else if (!isalpha(c) && !isdigit(c) && c != '_') { |
59 | 0 | return false; |
60 | 0 | } |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | const struct json * |
65 | | ovsdb_parser_member(struct ovsdb_parser *parser, const char *name, |
66 | | enum ovsdb_parser_types types) |
67 | 0 | { |
68 | 0 | struct json *value; |
69 | |
|
70 | 0 | if (!parser->json) { |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | value = shash_find_data(json_object(parser->json), name); |
75 | 0 | if (!value) { |
76 | 0 | if (!(types & OP_OPTIONAL)) { |
77 | 0 | ovsdb_parser_raise_error(parser, |
78 | 0 | "Required '%s' member is missing.", name); |
79 | 0 | } |
80 | 0 | return NULL; |
81 | 0 | } |
82 | | |
83 | 0 | if (((int) value->type >= 0 && value->type < JSON_N_TYPES |
84 | 0 | && types & (1u << value->type)) |
85 | 0 | || (types & OP_ID && value->type == JSON_STRING |
86 | 0 | && ovsdb_parser_is_id(value->string))) |
87 | 0 | { |
88 | 0 | sset_add(&parser->used, name); |
89 | 0 | return value; |
90 | 0 | } else { |
91 | 0 | ovsdb_parser_raise_error(parser, "Type mismatch for member '%s'.", |
92 | 0 | name); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | | void |
98 | | ovsdb_parser_raise_error(struct ovsdb_parser *parser, const char *format, ...) |
99 | 0 | { |
100 | 0 | if (!parser->error) { |
101 | 0 | struct ovsdb_error *error; |
102 | 0 | va_list args; |
103 | 0 | char *message; |
104 | |
|
105 | 0 | va_start(args, format); |
106 | 0 | message = xvasprintf(format, args); |
107 | 0 | va_end(args); |
108 | |
|
109 | 0 | error = ovsdb_syntax_error(parser->json, NULL, "Parsing %s failed: %s", |
110 | 0 | parser->name, message); |
111 | 0 | free(message); |
112 | |
|
113 | 0 | parser->error = error; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | /* If 'parser' isn't already in an error state, sets its error to 'error'. |
118 | | * Always takes ownership of 'error'. */ |
119 | | void |
120 | | ovsdb_parser_put_error(struct ovsdb_parser *parser, struct ovsdb_error *error) |
121 | 0 | { |
122 | 0 | if (!parser->error) { |
123 | 0 | parser->error = error; |
124 | 0 | } else { |
125 | 0 | ovsdb_error_destroy(error); |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | | struct ovsdb_error * |
130 | | ovsdb_parser_get_error(const struct ovsdb_parser *parser) |
131 | 0 | { |
132 | 0 | return parser->error ? ovsdb_error_clone(parser->error) : NULL; |
133 | 0 | } |
134 | | |
135 | | bool |
136 | | ovsdb_parser_has_error(const struct ovsdb_parser *parser) |
137 | 0 | { |
138 | 0 | return parser->error != NULL; |
139 | 0 | } |
140 | | |
141 | | struct ovsdb_error * |
142 | | ovsdb_parser_destroy(struct ovsdb_parser *parser) |
143 | 0 | { |
144 | 0 | free(parser->name); |
145 | 0 | sset_destroy(&parser->used); |
146 | |
|
147 | 0 | return parser->error; |
148 | 0 | } |
149 | | |
150 | | struct ovsdb_error * |
151 | | ovsdb_parser_finish(struct ovsdb_parser *parser) |
152 | 0 | { |
153 | 0 | if (!parser->error) { |
154 | 0 | const struct shash *object = json_object(parser->json); |
155 | 0 | size_t n_unused; |
156 | |
|
157 | 0 | n_unused = shash_count(object) - sset_count(&parser->used); |
158 | 0 | if (n_unused) { |
159 | 0 | struct shash_node *node; |
160 | |
|
161 | 0 | SHASH_FOR_EACH (node, object) { |
162 | 0 | if (!sset_contains(&parser->used, node->name)) { |
163 | 0 | if (n_unused > 1) { |
164 | 0 | ovsdb_parser_raise_error( |
165 | 0 | parser, |
166 | 0 | "Member '%s' and %"PRIuSIZE" other member%s " |
167 | 0 | "are present but not allowed here.", |
168 | 0 | node->name, n_unused - 1, n_unused > 2 ? "s" : ""); |
169 | 0 | } else { |
170 | 0 | ovsdb_parser_raise_error( |
171 | 0 | parser, |
172 | 0 | "Member '%s' is present but not allowed here.", |
173 | 0 | node->name); |
174 | 0 | } |
175 | 0 | break; |
176 | 0 | } |
177 | 0 | } |
178 | 0 | } |
179 | 0 | } |
180 | |
|
181 | 0 | return ovsdb_parser_destroy(parser); |
182 | 0 | } |