blob: 41a1adb23113177a04430866cb88a782fbd92d7b [file] [log] [blame]
Radek Krejci86d106e2018-10-18 09:53:19 +02001/**
2 * @file tree_schema_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Parsing and validation helper functions
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _XOPEN_SOURCE
15
16#include <ctype.h>
17#include <limits.h>
18#include <time.h>
19
20#include "libyang.h"
21#include "common.h"
22#include "tree_schema_internal.h"
23
24LY_ERR
25lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value)
26{
27 struct lysp_import *i;
28
29 if (module->prefix && &module->prefix != value && !strcmp(module->prefix, *value)) {
30 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
31 "Prefix \"%s\" already used as module prefix.", *value);
32 return LY_EEXIST;
33 }
34 if (module->imports) {
35 LY_ARRAY_FOR(module->imports, struct lysp_import, i) {
36 if (i->prefix && &i->prefix != value && !strcmp(i->prefix, *value)) {
37 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
38 "Prefix \"%s\" already used to import \"%s\" module.", *value, i->name);
39 return LY_EEXIST;
40 }
41 }
42 }
43 return LY_SUCCESS;
44}
45
46LY_ERR
47lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt)
48{
49 int i;
50 struct tm tm, tm_;
51 char *r;
52
53 LY_CHECK_ARG_RET(ctx, date, LY_EINVAL);
54 LY_CHECK_ERR_RET(date_len != LY_REV_SIZE - 1, LOGARG(ctx, date_len), LY_EINVAL);
55
56 /* check format */
57 for (i = 0; i < date_len; i++) {
58 if (i == 4 || i == 7) {
59 if (date[i] != '-') {
60 goto error;
61 }
62 } else if (!isdigit(date[i])) {
63 goto error;
64 }
65 }
66
67 /* check content, e.g. 2018-02-31 */
68 memset(&tm, 0, sizeof tm);
69 r = strptime(date, "%Y-%m-%d", &tm);
70 if (!r || r != &date[LY_REV_SIZE - 1]) {
71 goto error;
72 }
73 memcpy(&tm_, &tm, sizeof tm);
74 mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
75 if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
76 /* checking days is enough, since other errors
77 * have been checked by strptime() */
78 goto error;
79 }
80
81 return LY_SUCCESS;
82
83error:
84 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
85 return LY_EINVAL;
86}
87
88void
89lysp_sort_revisions(struct lysp_revision *revs)
90{
91 uint8_t i, r;
92 struct lysp_revision rev;
93
94 for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) {
Radek Krejci2c4e7172018-10-19 15:56:26 +020095 if (strcmp(revs[i].rev, revs[r].rev) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +020096 r = i;
97 }
98 }
99
100 if (r) {
101 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200102 memcpy(&rev, &revs[0], sizeof rev);
103 memcpy(&revs[0], &revs[r], sizeof rev);
104 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200105 }
106}
Radek Krejci151a5b72018-10-19 14:21:44 +0200107
108struct lysc_module *
109lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len)
110{
111 struct lysc_import *imp;
112
113 assert(mod);
114
115 if (!strncmp(mod->prefix, prefix, len) && mod->prefix[len] == '\0') {
116 /* it is the prefix of the module itself */
117 return mod;
118 }
119
120 /* search in imports */
121 LY_ARRAY_FOR(mod->imports, struct lysc_import, imp) {
122 if (!strncmp(imp->prefix, prefix, len) && mod->prefix[len] == '\0') {
123 return imp->module;
124 }
125 }
126
127 return NULL;
128}
David Sedlák23a59a62018-10-26 13:08:02 +0200129
David Sedlákc10e7902018-12-17 02:17:59 +0100130enum yang_keyword
131match_keyword(char *data)
132{
133/* TODO make this function usable in get_keyword function */
David Sedlák23a59a62018-10-26 13:08:02 +0200134#define MOVE_INPUT(DATA, COUNT) (data)+=COUNT;
135#define IF_KW(STR, LEN, STMT) if (!strncmp((data), STR, LEN)) {MOVE_INPUT(data, LEN);kw=STMT;}
136#define IF_KW_PREFIX(STR, LEN) if (!strncmp((data), STR, LEN)) {MOVE_INPUT(data, LEN);
137#define IF_KW_PREFIX_END }
138
David Sedlák23a59a62018-10-26 13:08:02 +0200139 enum yang_keyword kw = YANG_NONE;
140 /* read the keyword itself */
141 switch (*data) {
142 case 'a':
143 MOVE_INPUT(data, 1);
144 IF_KW("rgument", 7, YANG_ARGUMENT)
145 else IF_KW("ugment", 6, YANG_AUGMENT)
146 else IF_KW("ction", 5, YANG_ACTION)
147 else IF_KW_PREFIX("ny", 2)
148 IF_KW("data", 4, YANG_ANYDATA)
149 else IF_KW("xml", 3, YANG_ANYXML)
150 IF_KW_PREFIX_END
151 break;
152 case 'b':
153 MOVE_INPUT(data, 1);
154 IF_KW("ase", 3, YANG_BASE)
155 else IF_KW("elongs-to", 9, YANG_BELONGS_TO)
156 else IF_KW("it", 2, YANG_BIT)
157 break;
158 case 'c':
159 MOVE_INPUT(data, 1);
160 IF_KW("ase", 3, YANG_CASE)
161 else IF_KW("hoice", 5, YANG_CHOICE)
162 else IF_KW_PREFIX("on", 2)
163 IF_KW("fig", 3, YANG_CONFIG)
164 else IF_KW_PREFIX("ta", 2)
165 IF_KW("ct", 2, YANG_CONTACT)
166 else IF_KW("iner", 4, YANG_CONTAINER)
167 IF_KW_PREFIX_END
168 IF_KW_PREFIX_END
169 break;
170 case 'd':
171 MOVE_INPUT(data, 1);
172 IF_KW_PREFIX("e", 1)
173 IF_KW("fault", 5, YANG_DEFAULT)
174 else IF_KW("scription", 9, YANG_DESCRIPTION)
175 else IF_KW_PREFIX("viat", 4)
176 IF_KW("e", 1, YANG_DEVIATE)
177 else IF_KW("ion", 3, YANG_DEVIATION)
178 IF_KW_PREFIX_END
179 IF_KW_PREFIX_END
180 break;
181 case 'e':
182 MOVE_INPUT(data, 1);
183 IF_KW("num", 3, YANG_ENUM)
184 else IF_KW_PREFIX("rror-", 5)
185 IF_KW("app-tag", 7, YANG_ERROR_APP_TAG)
186 else IF_KW("message", 7, YANG_ERROR_MESSAGE)
187 IF_KW_PREFIX_END
188 else IF_KW("xtension", 8, YANG_EXTENSION)
189 break;
190 case 'f':
191 MOVE_INPUT(data, 1);
192 IF_KW("eature", 6, YANG_FEATURE)
193 else IF_KW("raction-digits", 14, YANG_FRACTION_DIGITS)
194 break;
195 case 'g':
196 MOVE_INPUT(data, 1);
197 IF_KW("rouping", 7, YANG_GROUPING)
198 break;
199 case 'i':
200 MOVE_INPUT(data, 1);
201 IF_KW("dentity", 7, YANG_IDENTITY)
202 else IF_KW("f-feature", 9, YANG_IF_FEATURE)
203 else IF_KW("mport", 5, YANG_IMPORT)
204 else IF_KW_PREFIX("n", 1)
205 IF_KW("clude", 5, YANG_INCLUDE)
206 else IF_KW("put", 3, YANG_INPUT)
207 IF_KW_PREFIX_END
208 break;
209 case 'k':
210 MOVE_INPUT(data, 1);
211 IF_KW("ey", 2, YANG_KEY)
212 break;
213 case 'l':
214 MOVE_INPUT(data, 1);
215 IF_KW_PREFIX("e", 1)
216 IF_KW("af-list", 7, YANG_LEAF_LIST)
217 else IF_KW("af", 2, YANG_LEAF)
218 else IF_KW("ngth", 4, YANG_LENGTH)
219 IF_KW_PREFIX_END
220 else IF_KW("ist", 3, YANG_LIST)
221 break;
222 case 'm':
223 MOVE_INPUT(data, 1);
224 IF_KW_PREFIX("a", 1)
225 IF_KW("ndatory", 7, YANG_MANDATORY)
226 else IF_KW("x-elements", 10, YANG_MAX_ELEMENTS)
227 IF_KW_PREFIX_END
228 else IF_KW("in-elements", 11, YANG_MIN_ELEMENTS)
229 else IF_KW("ust", 3, YANG_MUST)
230 else IF_KW_PREFIX("od", 2)
231 IF_KW("ule", 3, YANG_MODULE)
232 else IF_KW("ifier", 5, YANG_MODIFIER)
233 IF_KW_PREFIX_END
234 break;
235 case 'n':
236 MOVE_INPUT(data, 1);
237 IF_KW("amespace", 8, YANG_NAMESPACE)
238 else IF_KW("otification", 11, YANG_NOTIFICATION)
239 break;
240 case 'o':
241 MOVE_INPUT(data, 1);
242 IF_KW_PREFIX("r", 1)
243 IF_KW("dered-by", 8, YANG_ORDERED_BY)
244 else IF_KW("ganization", 10, YANG_ORGANIZATION)
245 IF_KW_PREFIX_END
246 else IF_KW("utput", 5, YANG_OUTPUT)
247 break;
248 case 'p':
249 MOVE_INPUT(data, 1);
250 IF_KW("ath", 3, YANG_PATH)
251 else IF_KW("attern", 6, YANG_PATTERN)
252 else IF_KW("osition", 7, YANG_POSITION)
253 else IF_KW_PREFIX("re", 2)
254 IF_KW("fix", 3, YANG_PREFIX)
255 else IF_KW("sence", 5, YANG_PRESENCE)
256 IF_KW_PREFIX_END
257 break;
258 case 'r':
259 MOVE_INPUT(data, 1);
260 IF_KW("ange", 4, YANG_RANGE)
261 else IF_KW_PREFIX("e", 1)
262 IF_KW_PREFIX("f", 1)
263 IF_KW("erence", 6, YANG_REFERENCE)
264 else IF_KW("ine", 3, YANG_REFINE)
265 IF_KW_PREFIX_END
266 else IF_KW("quire-instance", 14, YANG_REQUIRE_INSTANCE)
267 else IF_KW("vision-date", 11, YANG_REVISION_DATE)
268 else IF_KW("vision", 6, YANG_REVISION)
269 IF_KW_PREFIX_END
270 else IF_KW("pc", 2, YANG_RPC)
271 break;
272 case 's':
273 MOVE_INPUT(data, 1);
274 IF_KW("tatus", 5, YANG_STATUS)
275 else IF_KW("ubmodule", 8, YANG_SUBMODULE)
276 break;
277 case 't':
278 MOVE_INPUT(data, 1);
279 IF_KW("ypedef", 6, YANG_TYPEDEF)
280 else IF_KW("ype", 3, YANG_TYPE)
281 break;
282 case 'u':
283 MOVE_INPUT(data, 1);
284 IF_KW_PREFIX("ni", 2)
285 IF_KW("que", 3, YANG_UNIQUE)
286 else IF_KW("ts", 2, YANG_UNITS)
287 IF_KW_PREFIX_END
288 else IF_KW("ses", 3, YANG_USES)
289 break;
290 case 'v':
291 MOVE_INPUT(data, 1);
292 IF_KW("alue", 4, YANG_VALUE)
293 break;
294 case 'w':
295 MOVE_INPUT(data, 1);
296 IF_KW("hen", 3, YANG_WHEN)
297 break;
298 case 'y':
299 MOVE_INPUT(data, 1);
300 IF_KW("ang-version", 11, YANG_YANG_VERSION)
301 else IF_KW("in-element", 10, YANG_YIN_ELEMENT)
302 break;
303 case ';':
304 MOVE_INPUT(data, 1);
305 kw = YANG_SEMICOLON;
306 //goto success;
307 break;
308 case '{':
309 MOVE_INPUT(data, 1);
310 kw = YANG_LEFT_BRACE;
311 //goto success;
312 break;
313 case '}':
314 MOVE_INPUT(data, 1);
315 kw = YANG_RIGHT_BRACE;
316 //goto success;
317 break;
318 default:
319 break;
320 }
321
David Sedlákc10e7902018-12-17 02:17:59 +0100322 /* TODO important fix whole keyword must be matched */
David Sedlák23a59a62018-10-26 13:08:02 +0200323 return kw;
324}