blob: a9bd79f5d5e30b8c56c6d49ceec483c404cf73b2 [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 Krejci086c7132018-10-26 15:29:04 +0200123lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct lys_module **mod)
124{
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
136 if (!(*mod)) {
137 /* check collision with other implemented revision */
138 if (implement && ly_ctx_get_module_implemented(ctx, name)) {
139 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
140 "Module \"%s\" is already present in other implemented revision.", name);
141 return LY_EDENIED;
142 }
143
144 /* submodule not present in the context, get the input data and parse it */
145 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
146search_clb:
147 if (ctx->imp_clb) {
148 if (ctx->imp_clb(name, revision, NULL, NULL, ctx->imp_clb_data,
149 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
150 *mod = lys_parse_mem_(ctx, submodule_data, format, revision, implement);
151 }
152 }
153 if (!(*mod) && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
154 goto search_file;
155 }
156 } else {
157search_file:
158 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
159 /* module was not received from the callback or there is no callback set */
160 lys_module_localfile(ctx, name, revision, implement, mod);
161 }
162 if (!(*mod) && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
163 goto search_clb;
164 }
165 }
166 } else {
167 /* we have module from the current context */
168 if (implement && (ly_ctx_get_module_implemented(ctx, name) != *mod)) {
169 /* check collision with other implemented revision */
170 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE,
171 "Module \"%s\" is already present in other implemented revision.", name);
172 *mod = NULL;
173 return LY_EDENIED;
174 }
175
176 /* circular check */
177 if ((*mod)->parsed->parsing) {
178 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (import) for module \"%s\".", name);
179 *mod = NULL;
180 return LY_EVALID;
181 }
182 }
183 if (!(*mod)) {
184 if (ly_errcode(ctx) != LY_EVALID) {
185 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, strlen(name), name, "import");
186 } else {
187 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Loading \"%s\" module failed.", name);
188 }
189 return LY_EVALID;
190 }
191
192 if (implement) {
193 /* mark the module implemented, check for collision was already done */
194 lys_module_implement(*mod);
195 }
196 if (!revision && ((*mod)->parsed->latest_revision == 1)) {
197 /* update the latest_revision flag - here we have selected the latest available schema */
198 (*mod)->parsed->latest_revision = 2;
199 }
200
201 return LY_SUCCESS;
202}
203
204LY_ERR
205lysp_load_submodule(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc)
Radek Krejcid33273d2018-10-25 14:55:52 +0200206{
207 struct lys_module *submod;
208 const char *submodule_data = NULL;
209 LYS_INFORMAT format = LYS_IN_UNKNOWN;
210 void (*submodule_data_free)(void *module_data, void *user_data) = NULL;
211
212 /* Try to get submodule from the context, if already present */
Radek Krejci086c7132018-10-26 15:29:04 +0200213 inc->submodule = ly_ctx_get_submodule(ctx, mod->name, inc->name, inc->rev[0] ? inc->rev : NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200214 if (!inc->submodule) {
215 /* submodule not present in the context, get the input data and parse it */
Radek Krejci086c7132018-10-26 15:29:04 +0200216 if (!(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200217search_clb:
Radek Krejci086c7132018-10-26 15:29:04 +0200218 if (ctx->imp_clb) {
219 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 +0200220 &format, &submodule_data, &submodule_data_free) == LY_SUCCESS) {
Radek Krejci086c7132018-10-26 15:29:04 +0200221 submod = lys_parse_mem_(ctx, submodule_data, format, inc->rev[0] ? inc->rev : NULL, mod->implemented);
Radek Krejcid33273d2018-10-25 14:55:52 +0200222 }
223 }
Radek Krejci086c7132018-10-26 15:29:04 +0200224 if (!submod && !(ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200225 goto search_file;
226 }
227 } else {
228search_file:
Radek Krejci086c7132018-10-26 15:29:04 +0200229 if (!(ctx->flags & LY_CTX_DISABLE_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200230 /* module was not received from the callback or there is no callback set */
Radek Krejci086c7132018-10-26 15:29:04 +0200231 lys_module_localfile(ctx, inc->name, inc->rev[0] ? inc->rev : NULL, mod->implemented, &submod);
Radek Krejcid33273d2018-10-25 14:55:52 +0200232 }
Radek Krejci086c7132018-10-26 15:29:04 +0200233 if (!submod && (ctx->flags & LY_CTX_PREFER_SEARCHDIRS)) {
Radek Krejcid33273d2018-10-25 14:55:52 +0200234 goto search_clb;
235 }
236 }
237 if (submod) {
238 /* check that we have really a submodule */
239 if (!submod->parsed->submodule) {
240 /* submodule is not a submodule */
Radek Krejci086c7132018-10-26 15:29:04 +0200241 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" schema from \"%s\" is actually not a submodule.",
242 inc->name, mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200243 lys_module_free(submod, NULL);
244 /* fix list of modules in context, since it was already changed */
Radek Krejci086c7132018-10-26 15:29:04 +0200245 --ctx->list.count;
Radek Krejcid33273d2018-10-25 14:55:52 +0200246 return LY_EVALID;
247 }
248 /* check that the submodule belongs-to our module */
249 if (strcmp(mod->name, submod->parsed->belongsto)) {
Radek Krejci086c7132018-10-26 15:29:04 +0200250 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Included \"%s\" submodule from \"%s\" belongs-to a different module \"%s\".",
251 inc->name, mod->name, submod->parsed->belongsto);
252 lys_module_free(submod, NULL);
253 return LY_EVALID;
254 }
255 /* check circular dependency */
256 if (submod->parsed->parsing) {
257 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "A circular dependency (include) for module \"%s\".",
258 submod->parsed->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200259 lys_module_free(submod, NULL);
260 return LY_EVALID;
261 }
262 inc->submodule = submod->parsed;
263 ++inc->submodule->refcount;
264 free(submod);
265 }
Radek Krejci2d31ea72018-10-25 15:46:42 +0200266 } else {
267 ++inc->submodule->refcount;
Radek Krejcid33273d2018-10-25 14:55:52 +0200268 }
269 if (!inc->submodule) {
Radek Krejci086c7132018-10-26 15:29:04 +0200270 if (ly_errcode(ctx) != LY_EVALID) {
271 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Invalid value \"%s\" of include statement.", inc->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200272 } else {
Radek Krejci086c7132018-10-26 15:29:04 +0200273 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 +0200274 }
275 return LY_EVALID;
276 }
277
278 return LY_SUCCESS;
279}
280
Radek Krejci151a5b72018-10-19 14:21:44 +0200281struct lysc_module *
282lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len)
283{
284 struct lysc_import *imp;
285
286 assert(mod);
287
288 if (!strncmp(mod->prefix, prefix, len) && mod->prefix[len] == '\0') {
289 /* it is the prefix of the module itself */
290 return mod;
291 }
292
293 /* search in imports */
294 LY_ARRAY_FOR(mod->imports, struct lysc_import, imp) {
295 if (!strncmp(imp->prefix, prefix, len) && mod->prefix[len] == '\0') {
296 return imp->module;
297 }
298 }
299
300 return NULL;
301}