blob: e4d26b96ec18e28a5d3000a2330bd92800f890cd [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:
Radek Krejcid33273d2018-10-25 14:55:52 +020084 if (stmt) {
85 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
86 }
Radek Krejci86d106e2018-10-18 09:53:19 +020087 return LY_EINVAL;
88}
89
90void
91lysp_sort_revisions(struct lysp_revision *revs)
92{
93 uint8_t i, r;
94 struct lysp_revision rev;
95
96 for (i = 1, r = 0; revs && i < LY_ARRAY_SIZE(revs); i++) {
Radek Krejcib7db73a2018-10-24 14:18:40 +020097 if (strcmp(revs[i].date, revs[r].date) > 0) {
Radek Krejci86d106e2018-10-18 09:53:19 +020098 r = i;
99 }
100 }
101
102 if (r) {
103 /* the newest revision is not on position 0, switch them */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200104 memcpy(&rev, &revs[0], sizeof rev);
105 memcpy(&revs[0], &revs[r], sizeof rev);
106 memcpy(&revs[r], &rev, sizeof rev);
Radek Krejci86d106e2018-10-18 09:53:19 +0200107 }
108}
Radek Krejci151a5b72018-10-19 14:21:44 +0200109
Radek Krejci086c7132018-10-26 15:29:04 +0200110void
111lys_module_implement(struct lys_module *mod)
112{
113 assert(mod);
114 if (mod->parsed) {
115 mod->parsed->implemented = 1;
116 }
117 if (mod->compiled) {
118 mod->compiled->implemented = 1;
119 }
120}
121
Radek Krejcid33273d2018-10-25 14:55:52 +0200122LY_ERR
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100123lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod)
Radek Krejci086c7132018-10-26 15:29:04 +0200124{
125 const char *submodule_data = NULL;
126 LYS_INFORMAT format = LYS_IN_UNKNOWN;
127 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
128
129 /* try to get the module from the context */
130 if (revision) {
131 *mod = (struct lys_module*)ly_ctx_get_module(ctx, name, revision);
132 } else {
133 *mod = (struct lys_module*)ly_ctx_get_module_latest(ctx, name);
134 }
135
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100136 if (!(*mod) || (require_parsed && !(*mod)->parsed)) {
137 (*mod) = NULL;
138
Radek Krejci086c7132018-10-26 15:29:04 +0200139 /* check collision with other implemented revision */
140 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
141 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
142 "Module \"%s\" is already present in other implemented revision.", name);
143 return LY_EDENIED;
144 }
145
146 /* submodule not present in the context, get the input data and parse it */
147 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
148search_clb:
149 if (ctx->imp_clb) {
150 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
151 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
152 *mod = lys_parse_mem_(ctx, submodule_data, format, revision, implement);
153 }
154 }
155 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
156 goto search_file;
157 }
158 } else {
159search_file:
160 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
161 /* module was not received from the callback or there is no callback set */
162 lys_module_localfile(ctx, name, revision, implement, mod);
163 }
164 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
165 goto search_clb;
166 }
167 }
168 } else {
169 /* we have module from the current context */
170 if (implement && (ly_ctx_get_module_implemented(ctx, name) != *mod)) {
171 /* check collision with other implemented revision */
172 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
173 "Module \"%s\" is already present in other implemented revision.", name);
174 *mod = NULL;
175 return LY_EDENIED;
176 }
177
178 /* circular check */
179 if ((*mod)->parsed->parsing) {
180 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
181 *mod = NULL;
182 return LY_EVALID;
183 }
184 }
185 if (!(*mod)) {
186 if (ly_errcode(ctx) != LY_EVALID) {
187 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, strlen(name), name, "import");
188 } else {
189 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
190 }
191 return LY_EVALID;
192 }
193
194 if (implement) {
195 /* mark the module implemented, check for collision was already done */
196 lys_module_implement(*mod);
197 }
198 if (!revision && ((*mod)->parsed->latest_revision == 1)) {
199 /* update the latest_revision flag - here we have selected the latest available schema */
200 (*mod)->parsed->latest_revision = 2;
201 }
202
203 return LY_SUCCESS;
204}
205
206LY_ERR
207lysp_load_submodule(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200208{
209 struct lys_module *submod;
210 const char *submodule_data = NULL;
211 LYS_INFORMAT format = LYS_IN_UNKNOWN;
212 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
213
214 /* Try to get submodule from the context, if already present */
Radek Krejci086c7132018-10-26 15:29:04 +0200215 inc->submodule = ly_ctx_get_submodule(ctx, mod->name, inc->name, inc->rev[0] ? inc->rev : NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200216 if (!inc->submodule) {
217 /* submodule not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200218 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200219search_clb:
Radek Krejci086c7132018-10-26 15:29:04 +0200220 if (ctx->imp_clb) {
221 if (ctx->imp_clb(mod->name, NULL, inc->name, inc->rev[0] ? inc->rev : NULL, ctx->imp_clb_data,
Radek Krejcid33273d2018-10-25 14:55:52 +0200222 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Radek Krejci086c7132018-10-26 15:29:04 +0200223 submod = lys_parse_mem_(ctx, submodule_data, format, inc->rev[0] ? inc->rev : NULL, mod->implemented);
Radek Krejcid33273d2018-10-25 14:55:52 +0200224 }
225 }
Radek Krejci086c7132018-10-26 15:29:04 +0200226 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200227 goto search_file;
228 }
229 } else {
230search_file:
Radek Krejci086c7132018-10-26 15:29:04 +0200231 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200232 /* module was not received from the callback or there is no callback set */
Radek Krejci086c7132018-10-26 15:29:04 +0200233 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, mod->implemented, &submod);
Radek Krejcid33273d2018-10-25 14:55:52 +0200234 }
Radek Krejci086c7132018-10-26 15:29:04 +0200235 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200236 goto search_clb;
237 }
238 }
239 if (submod) {
240 /* check that we have really a submodule */
241 if (!submod->parsed->submodule) {
242 /* submodule is not a submodule */
Radek Krejci086c7132018-10-26 15:29:04 +0200243 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" schema from \"%s\" is actually not a submodule.",
244 inc->name, mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200245 lys_module_free(submod, NULL);
246 /* fix list of modules in context, since it was already changed */
Radek Krejci086c7132018-10-26 15:29:04 +0200247 --ctx->list.count;
Radek Krejcid33273d2018-10-25 14:55:52 +0200248 return LY_EVALID;
249 }
250 /* check that the submodule belongs-to our module */
251 if (strcmp(mod->name, submod->parsed->belongsto)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200252 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
253 inc->name, mod->name, submod->parsed->belongsto);
254 lys_module_free(submod, NULL);
255 return LY_EVALID;
256 }
257 /* check circular dependency */
258 if (submod->parsed->parsing) {
259 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".",
260 submod->parsed->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200261 lys_module_free(submod, NULL);
262 return LY_EVALID;
263 }
264 inc->submodule = submod->parsed;
265 ++inc->submodule->refcount;
266 free(submod);
267 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200268 } else {
269 ++inc->submodule->refcount;
Radek Krejcid33273d2018-10-25 14:55:52 +0200270 }
271 if (!inc->submodule) {
Radek Krejci086c7132018-10-26 15:29:04 +0200272 if (ly_errcode(ctx) != LY_EVALID) {
273 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Invalid value \"%s\" of include statement.", inc->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200274 } else {
Radek Krejci086c7132018-10-26 15:29:04 +0200275 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Including \"%s\" submodule into \"%s\" failed.", inc->name, mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200276 }
277 return LY_EVALID;
278 }
279
280 return LY_SUCCESS;
281}
282
Radek Krejci151a5b72018-10-19 14:21:44 +0200283struct lysc_module *
284lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len)
285{
286 struct lysc_import *imp;
287
288 assert(mod);
289
290 if (!strncmp(mod->prefix, prefix, len) && mod->prefix[len] == '\0') {
291 /* it is the prefix of the module itself */
292 return mod;
293 }
294
295 /* search in imports */
296 LY_ARRAY_FOR(mod->imports, struct lysc_import, imp) {
297 if (!strncmp(imp->prefix, prefix, len) && mod->prefix[len] == '\0') {
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100298 if (!imp->module->compiled) {
299 /* shouldn't be needed, the function is internally used when
300 * the imported modules should be also compiled. But for sure
301 * and possible future optimizations, check it here */
302 lys_compile(imp->module->parsed, 0, &imp->module->compiled);
303 }
304 return imp->module->compiled;
Radek Krejci151a5b72018-10-19 14:21:44 +0200305 }
306 }
307
308 return NULL;
309}