blob: f5a70b76385c35dd3e63a318f091726f131732b3 [file] [log] [blame]
Radek Krejci3f5e3db2018-10-11 15:57:47 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
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 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
15#include "common.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020018#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020019#include <errno.h>
20#include <fcntl.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <limits.h>
22#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdlib.h>
25#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020028
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
31#include "log.h"
32#include "set.h"
33#include "tree.h"
34#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020035#include "tree_schema_internal.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020036
Radek Krejcia3045382018-11-22 14:30:31 +010037API const struct lysc_node *
38lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
39{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010040 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010041 struct lysc_node **snode;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010042 int action_flag = 0, notif_flag = 0;
43 const struct lysc_action *actions;
44 const struct lysc_notif *notifs;
Radek Krejci7eb54ba2020-05-18 16:30:04 +020045 LY_ARRAY_SIZE_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +010046
47 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
48
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020049next:
Radek Krejcia3045382018-11-22 14:30:31 +010050 if (!last) {
51 /* first call */
52
53 /* get know where to start */
54 if (parent) {
55 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010056 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020057 if (((struct lysc_node_choice*)parent)->cases) {
58 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010059 }
Radek Krejci056d0a82018-12-06 16:57:25 +010060 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010061 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010062 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020063 if (snode && *snode) {
64 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010065 }
Radek Krejcia3045382018-11-22 14:30:31 +010066 }
Radek Krejcia3045382018-11-22 14:30:31 +010067 } else {
68 /* top level data */
69 next = last = module->data;
70 }
71 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010072 /* try to get action or notification */
73 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010074 }
Radek Krejci05b774b2019-02-25 13:26:18 +010075 /* test if the next can be returned */
76 goto check;
77
Michal Vasko1bf09392020-03-27 12:38:10 +010078 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010079 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010080 if (last->parent) {
81 actions = lysc_node_actions(last->parent);
82 } else {
83 actions = module->rpcs;
84 }
85 LY_ARRAY_FOR(actions, u) {
86 if (&actions[u] == (struct lysc_action*)last) {
87 break;
88 }
89 }
90 if (u + 1 < LY_ARRAY_SIZE(actions)) {
91 next = (struct lysc_node*)(&actions[u + 1]);
92 }
93 goto repeat;
94 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +010095 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010096 if (last->parent) {
97 notifs = lysc_node_notifs(last->parent);
98 } else {
99 notifs = module->notifs;
100 }
101 LY_ARRAY_FOR(notifs, u) {
102 if (&notifs[u] == (struct lysc_notif*)last) {
103 break;
104 }
105 }
106 if (u + 1 < LY_ARRAY_SIZE(notifs)) {
107 next = (struct lysc_node*)(&notifs[u + 1]);
108 }
109 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100110 }
111
112 next = last->next;
113repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100114 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
115 /* inside case (as an explicit parent, not when diving into it from choice),
116 * limit the list of children only to the specific case */
117 next = NULL;
118 }
Radek Krejcia3045382018-11-22 14:30:31 +0100119 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100120 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100121 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100122 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200123 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100124 } else if (!action_flag) {
125 action_flag = 1;
126 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
127 } else if (!notif_flag) {
128 notif_flag = 1;
129 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
130 } else {
131 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100132 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100133 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100134 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100135check:
Radek Krejcia3045382018-11-22 14:30:31 +0100136 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100137 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100138 case LYS_ACTION:
139 case LYS_NOTIF:
140 case LYS_LEAF:
141 case LYS_ANYXML:
142 case LYS_ANYDATA:
143 case LYS_LIST:
144 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100145 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100146 break;
147 case LYS_CONTAINER:
148 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
149 if (((struct lysc_node_container *)next)->child) {
150 /* go into */
151 next = ((struct lysc_node_container *)next)->child;
152 } else {
153 next = next->next;
154 }
155 goto repeat;
156 }
157 break;
158 case LYS_CHOICE:
159 if (options & LYS_GETNEXT_WITHCHOICE) {
160 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100161 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
162 next = next->next;
163 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100164 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100165 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100166 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100167 } else {
168 next = ((struct lysc_node_choice *)next)->cases->child;
169 }
Radek Krejcia3045382018-11-22 14:30:31 +0100170 }
171 goto repeat;
172 default:
173 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200174 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100175 return NULL;
176 }
177
178 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
179 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200180 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100181 next = next->next;
182 goto repeat;
183 }
184 }
185 return next;
186}
187
188API const struct lysc_node *
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800189lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath)
190{
191 const char *id = qpath;
192 const char *prefix, *name;
193 size_t prefix_len, name_len;
194 unsigned int u;
195 const struct lysc_node *node = context_node;
196 struct lys_module *mod = NULL;
197
198 LY_CHECK_ARG_RET(ctx, qpath, NULL);
199
200 while(*id) {
201 if (id[0] == '/') {
202 ++id;
203 }
204 /* precess ".." in relative paths */
205 while (!strncmp("../", id, 3)) {
206 id += 3;
207 if (!node) {
208 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath);
209 return NULL;
210 }
211 node = node->parent;
212 }
213
214 if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) {
215 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id- qpath, qpath);
216 return NULL;
217 }
218 if (prefix) {
219 if (context_node) {
220 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
221 } else {
222 for (u = 0; u < ctx->list.count; ++u) {
223 if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) {
224 struct lys_module *m = (struct lys_module *)ctx->list.objs[u];
225 if (mod) {
226 if (m->implemented) {
227 mod = m;
228 break;
229 } else if (m->latest_revision) {
230 mod = m;
231 }
232 } else {
233 mod = m;
234 }
235 }
236 }
237 }
238 }
239 if (!mod) {
240 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".",
241 id - qpath, qpath);
242 return NULL;
243 }
244
245 node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
246 if (!node) {
247 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath);
248 return NULL;
249 }
250 }
251
252 return node;
253}
254
255API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100256lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
257 uint16_t nodetype, int options)
Radek Krejcia3045382018-11-22 14:30:31 +0100258{
259 const struct lysc_node *node = NULL;
260
261 LY_CHECK_ARG_RET(NULL, module, name, NULL);
262 if (!nodetype) {
263 nodetype = 0xffff;
264 }
265
266 while ((node = lys_getnext(node, parent, module->compiled, options))) {
267 if (!(node->nodetype & nodetype)) {
268 continue;
269 }
270 if (node->module != module) {
271 continue;
272 }
273
274 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200275 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100276 return node;
277 }
278 } else {
279 if (!strcmp(node->name, name)) {
280 return node;
281 }
282 }
283 }
284 return NULL;
285}
286
Michal Vasko14654712020-02-06 08:35:21 +0100287char *
288lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
289 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200290{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200291 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200292 char *path = NULL;
293 int len = 0;
294
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200295 LY_CHECK_ARG_RET(NULL, node, NULL);
296 if (buffer) {
297 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
298 }
299
Radek Krejci327de162019-06-14 12:52:07 +0200300 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200301 case LYSC_PATH_LOG:
Michal Vasko90932a92020-02-12 14:33:03 +0100302 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200303 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200304 char *id;
Michal Vasko14654712020-02-06 08:35:21 +0100305 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200306
Michal Vasko03ff5a72019-09-11 13:49:33 +0200307 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100308 if (parent && (iter->parent == parent)) {
309 slash = "";
310 } else {
311 slash = "/";
312 }
Radek Krejci327de162019-06-14 12:52:07 +0200313 if (!iter->parent || iter->parent->module != iter->module) {
314 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200315 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100316 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200317 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100318 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200319 }
Radek Krejci327de162019-06-14 12:52:07 +0200320 } else {
321 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200322 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100323 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200324 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100325 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200326 }
Radek Krejci327de162019-06-14 12:52:07 +0200327 }
328 free(s);
329 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200330
331 if (buffer && buflen <= (size_t)len) {
332 /* not enough space in buffer */
333 break;
334 }
Radek Krejci327de162019-06-14 12:52:07 +0200335 }
336
337 if (len < 0) {
338 free(path);
339 path = NULL;
340 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200341 if (buffer) {
342 strcpy(buffer, "/");
343 } else {
344 path = strdup("/");
345 }
Radek Krejci327de162019-06-14 12:52:07 +0200346 }
347 break;
348 }
349
Radek Krejci1c0c3442019-07-23 16:08:47 +0200350 if (buffer) {
351 return buffer;
352 } else {
353 return path;
354 }
Radek Krejci327de162019-06-14 12:52:07 +0200355}
356
Michal Vasko14654712020-02-06 08:35:21 +0100357API char *
358lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
359{
360 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
361}
362
Radek Krejci19a96102018-11-15 13:38:09 +0100363API int
364lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200365{
Radek Krejci19a96102018-11-15 13:38:09 +0100366 LY_CHECK_ARG_RET(NULL, feature, -1);
367 return feature->flags & LYS_FENABLED ? 1 : 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200368}
369
Radek Krejci693262f2019-04-29 15:23:20 +0200370uint8_t
371lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200372{
373 uint8_t *item;
374 uint8_t mask = 3, result;
375
376 assert(pos >= 0);
377
378 item = &list[pos / 4];
379 result = (*item) & (mask << 2 * (pos % 4));
380 return result >> 2 * (pos % 4);
381}
382
Radek Krejci151a5b72018-10-19 14:21:44 +0200383static int
384lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
385{
386 uint8_t op;
387 int a, b;
388
Radek Krejci693262f2019-04-29 15:23:20 +0200389 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200390 (*index_e)++;
391
392 switch (op) {
393 case LYS_IFF_F:
394 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200395 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200396 case LYS_IFF_NOT:
397 /* invert result */
398 return lysc_iffeature_value_(iff, index_e, index_f) ? 0 : 1;
399 case LYS_IFF_AND:
400 case LYS_IFF_OR:
401 a = lysc_iffeature_value_(iff, index_e, index_f);
402 b = lysc_iffeature_value_(iff, index_e, index_f);
403 if (op == LYS_IFF_AND) {
404 return a && b;
405 } else { /* LYS_IFF_OR */
406 return a || b;
407 }
408 }
409
410 return 0;
411}
412
413API int
414lysc_iffeature_value(const struct lysc_iffeature *iff)
415{
416 int index_e = 0, index_f = 0;
417
418 LY_CHECK_ARG_RET(NULL, iff, -1);
419
420 if (iff->expr) {
421 return lysc_iffeature_value_(iff, &index_e, &index_f);
422 }
423 return 0;
424}
425
Radek Krejci151a5b72018-10-19 14:21:44 +0200426/**
427 * @brief Enable/Disable the specified feature in the module.
428 *
429 * If the feature is already set to the desired value, LY_SUCCESS is returned.
430 * By changing the feature, also all the feature which depends on it via their
431 * if-feature statements are again evaluated (disabled if a if-feature statemen
432 * evaluates to false).
433 *
Radek Krejci0af46292019-01-11 16:02:31 +0100434 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200435 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
436 * set all the features in the module.
437 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
438 * @return LY_ERR value.
439 */
440static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100441lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200442{
443 int all = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200444 LY_ARRAY_SIZE_TYPE u, disabled_count;
445 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200446 struct lysc_feature *f, **df;
447 struct lysc_iffeature *iff;
448 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100449 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200450
Radek Krejci6e67c402019-05-02 09:55:39 +0200451 if (!strcmp(name, "*")) {
452 /* enable all */
453 all = 1;
454 }
455
Radek Krejci0af46292019-01-11 16:02:31 +0100456 if (!mod->compiled) {
457 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
458 mod->name);
459 return LY_EINVAL;
460 }
461 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200462 if (all) {
463 /* no feature to enable */
464 return LY_SUCCESS;
465 }
Radek Krejci0af46292019-01-11 16:02:31 +0100466 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200467 return LY_EINVAL;
468 }
469
Radek Krejci151a5b72018-10-19 14:21:44 +0200470 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100471 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200472
Radek Krejcica3db002018-11-01 10:31:01 +0100473run:
Radek Krejci0af46292019-01-11 16:02:31 +0100474 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
475 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200476 if (all || !strcmp(f->name, name)) {
477 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
478 if (all) {
479 /* skip already set features */
480 continue;
481 } else {
482 /* feature already set correctly */
483 ly_set_free(changed, NULL);
484 return LY_SUCCESS;
485 }
486 }
487
488 if (value) { /* enable */
489 /* check referenced features if they are enabled */
490 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
491 if (!lysc_iffeature_value(iff)) {
492 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100493 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200494 goto next;
495 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100496 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200497 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
498 f->name);
499 ly_set_free(changed, NULL);
500 return LY_EDENIED;
501 }
502 }
503 }
504 /* enable the feature */
505 f->flags |= LYS_FENABLED;
506 } else { /* disable */
507 /* disable the feature */
508 f->flags &= ~LYS_FENABLED;
509 }
510
511 /* remember the changed feature */
512 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
513
514 if (!all) {
515 /* stop in case changing a single feature */
516 break;
517 }
518 }
519next:
520 ;
521 }
522
523 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100524 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200525 ly_set_free(changed, NULL);
526 return LY_EINVAL;
527 }
528
Radek Krejcica3db002018-11-01 10:31:01 +0100529 if (value && all && disabled_count) {
530 if (changed_count == changed->count) {
531 /* no change in last run -> not able to enable all ... */
532 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100533 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
534 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100535 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100536 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100537 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100538 --disabled_count;
539 }
540 }
541 /* ... restore the original state */
542 for (u = 0; u < changed->count; ++u) {
543 f = changed->objs[u];
544 /* re-disable the feature */
545 f->flags &= ~LYS_FENABLED;
546 }
547
548 ly_set_free(changed, NULL);
549 return LY_EDENIED;
550 } else {
551 /* we did some change in last run, try it again */
552 changed_count = changed->count;
553 goto run;
554 }
555 }
556
Radek Krejci151a5b72018-10-19 14:21:44 +0200557 /* reflect change(s) in the dependent features */
558 for (u = 0; u < changed->count; ++u) {
559 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
560 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
561 * is not done - by default, features are disabled and must be explicitely enabled. */
562 f = changed->objs[u];
563 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
564 if (!((*df)->flags & LYS_FENABLED)) {
565 /* not enabled, nothing to do */
566 continue;
567 }
568 /* check the feature's if-features which could change by the previous change of our feature */
569 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
570 if (!lysc_iffeature_value(iff)) {
571 /* the feature must be disabled now */
572 (*df)->flags &= ~LYS_FENABLED;
573 /* add the feature into the list of changed features */
574 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
575 break;
576 }
577 }
578 }
579 }
580
581 ly_set_free(changed, NULL);
582 return LY_SUCCESS;
583}
584
585API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200586lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200587{
Radek Krejci0af46292019-01-11 16:02:31 +0100588 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200589
Radek Krejcied5acc52019-04-25 15:57:04 +0200590 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200591}
592
593API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200594lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200595{
Radek Krejci0af46292019-01-11 16:02:31 +0100596 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200597
Radek Krejcied5acc52019-04-25 15:57:04 +0200598 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200599}
600
601API int
602lys_feature_value(const struct lys_module *module, const char *feature)
603{
604 struct lysc_feature *f;
605 struct lysc_module *mod;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200606 LY_ARRAY_SIZE_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200607
608 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
609 mod = module->compiled;
610
611 /* search for the specified feature */
612 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200613 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200614 if (!strcmp(f->name, feature)) {
615 if (f->flags & LYS_FENABLED) {
616 return 1;
617 } else {
618 return 0;
619 }
620 }
621 }
622
623 /* feature definition not found */
624 return -1;
625}
626
Michal Vaskoc193ce92020-03-06 11:04:48 +0100627API const struct lysc_node *
Radek Krejcifab954b2019-09-11 11:25:14 +0200628lysc_node_is_disabled(const struct lysc_node *node, int recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100629{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200630 LY_ARRAY_SIZE_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100631
632 LY_CHECK_ARG_RET(NULL, node, NULL);
633
Michal Vaskoc193ce92020-03-06 11:04:48 +0100634 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100635 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100636 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100637 LY_ARRAY_FOR(node->iffeatures, u) {
638 if (!lysc_iffeature_value(&node->iffeatures[u])) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100639 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100640 }
641 }
642 }
643
644 if (!recursive) {
645 return NULL;
646 }
647
Michal Vaskoc193ce92020-03-06 11:04:48 +0100648 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100649 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100650 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
651
Radek Krejcia3045382018-11-22 14:30:31 +0100652 return NULL;
653}
654
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200655LY_ERR
656lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
657{
658 struct lys_module *m;
659
660 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
661
662 if (mod->implemented) {
663 return LY_SUCCESS;
664 }
665
666 /* we have module from the current context */
667 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
668 if (m) {
669 if (m != mod) {
670 /* check collision with other implemented revision */
671 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
672 mod->name, mod->revision ? mod->revision : "module without revision");
673 return LY_EDENIED;
674 } else {
675 /* mod is already implemented */
676 return LY_SUCCESS;
677 }
678 }
679
680 /* mark the module implemented, check for collision was already done */
681 mod->implemented = value;
682
683 /* compile the schema */
684 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
685
686 return LY_SUCCESS;
687}
688
689API LY_ERR
690lys_set_implemented(struct lys_module *mod)
691{
692 return lys_set_implemented_internal(mod, 1);
693}
694
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200696lys_parse_mem_submodule(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vaskob36053d2020-03-26 15:49:30 +0100697 LY_ERR (*custom_check)(const struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*), void *check_data)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200698{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100699 LY_ERR ret = LY_EINVAL;
700 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100701 struct lys_yang_parser_ctx *yangctx = NULL;
702 struct lys_yin_parser_ctx *yinctx = NULL;
703 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704
705 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
706
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100707 switch (format) {
708 case LYS_IN_YIN:
Michal Vaskob36053d2020-03-26 15:49:30 +0100709 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, data, &submod);
710 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100711 break;
712 case LYS_IN_YANG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100713 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, data, &submod);
714 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100715 break;
716 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200717 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200719 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100720 LY_CHECK_RET(ret, NULL);
721
722 /* make sure that the newest revision is at position 0 */
723 lysp_sort_revisions(submod->revs);
724
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 /* decide the latest revision */
Michal Vaskob36053d2020-03-26 15:49:30 +0100726 latest_sp = ly_ctx_get_submodule(PARSER_CTX(pctx), submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100727 if (latest_sp) {
728 if (submod->revs) {
729 if (!latest_sp->revs) {
730 /* latest has no revision, so mod is anyway newer */
731 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200732 /* the latest_sp is zeroed later when the new module is being inserted into the context */
733 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
734 submod->latest_revision = latest_sp->latest_revision;
735 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200737 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200739 } else {
740 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741 }
742 } else {
743 submod->latest_revision = 1;
744 }
745
Radek Krejcib3289d62019-09-18 12:21:39 +0200746 if (custom_check) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100747 LY_CHECK_GOTO(custom_check(PARSER_CTX(pctx), NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200748 }
749
750 if (latest_sp) {
751 latest_sp->latest_revision = 0;
752 }
753
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100754 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100755 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
756 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100757
David Sedlák1b623122019-08-05 15:27:49 +0200758 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100759 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200760 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100761 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200762 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100763 return submod;
David Sedlák1b623122019-08-05 15:27:49 +0200764
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100765error:
766 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200767 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100768 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200769 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100770 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200771 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100772 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200773}
774
Radek Krejcid33273d2018-10-25 14:55:52 +0200775struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100776lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
Michal Vaskob36053d2020-03-26 15:49:30 +0100777 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100778 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200779{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100780 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200781 struct lysp_import *imp;
782 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100783 LY_ERR ret = LY_EINVAL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200784 LY_ARRAY_SIZE_TYPE u, v;
Michal Vaskob36053d2020-03-26 15:49:30 +0100785 struct lys_yang_parser_ctx *yangctx = NULL;
786 struct lys_yin_parser_ctx *yinctx = NULL;
787 struct lys_parser_ctx *pctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200788
789 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
790
791 mod = calloc(1, sizeof *mod);
792 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100793 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200794
795 switch (format) {
796 case LYS_IN_YIN:
Michal Vaskob36053d2020-03-26 15:49:30 +0100797 ret = yin_parse_module(&yinctx, data, mod);
798 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200799 break;
800 case LYS_IN_YANG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100801 ret = yang_parse_module(&yangctx, data, mod);
802 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200803 break;
804 default:
805 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
806 break;
807 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100808 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200809
810 /* make sure that the newest revision is at position 0 */
811 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100812 if (mod->parsed->revs) {
813 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
814 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200815
Radek Krejcib3289d62019-09-18 12:21:39 +0200816 /* decide the latest revision */
817 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
818 if (latest) {
819 if (mod->revision) {
820 if (!latest->revision) {
821 /* latest has no revision, so mod is anyway newer */
822 mod->latest_revision = latest->latest_revision;
823 /* the latest is zeroed later when the new module is being inserted into the context */
824 } else if (strcmp(mod->revision, latest->revision) > 0) {
825 mod->latest_revision = latest->latest_revision;
826 /* the latest is zeroed later when the new module is being inserted into the context */
827 } else {
828 latest = NULL;
829 }
830 } else {
831 latest = NULL;
832 }
833 } else {
834 mod->latest_revision = 1;
835 }
836
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100837 if (custom_check) {
838 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
839 }
840
Radek Krejci86d106e2018-10-18 09:53:19 +0200841 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200842 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100843 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
844 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100845 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200846 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100847 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200848 }
849
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100850 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100851 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100852 if (mod_dup) {
853 if (mod_dup->parsed) {
854 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200855 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100856 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
857 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200858 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100859 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
860 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200861 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100862 goto error;
863 } else {
864 /* add the parsed data to the currently compiled-only module in the context */
865 mod_dup->parsed = mod->parsed;
866 mod_dup->parsed->mod = mod_dup;
867 mod->parsed = NULL;
868 lys_module_free(mod, NULL);
869 mod = mod_dup;
870 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200871 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200873
Radek Krejci0af46292019-01-11 16:02:31 +0100874 if (!mod->implemented) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100875 /* pre-compile features of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200876 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100877 }
878
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100879 if (latest) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200880 latest->latest_revision = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100881 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200882
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100883 /* add into context */
884 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200885
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100886finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100887 /* resolve imports */
888 mod->parsed->parsing = 1;
889 LY_ARRAY_FOR(mod->parsed->imports, u) {
890 imp = &mod->parsed->imports[u];
891 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
892 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200893 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100894 /* check for importing the same module twice */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200895 for (v = 0; v < u; ++v) {
896 if (imp->module == mod->parsed->imports[v].module) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Single revision of the module \"%s\" referred twice.", imp->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100898 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200899 }
900 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200901 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 LY_ARRAY_FOR(mod->parsed->includes, u) {
903 inc = &mod->parsed->includes[u];
Michal Vaskob36053d2020-03-26 15:49:30 +0100904 if (!inc->submodule && lysp_load_submodule(pctx, mod->parsed, inc)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905 goto error_ctx;
906 }
Radek Krejci0af46292019-01-11 16:02:31 +0100907 if (!mod->implemented) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100908 /* pre-compile features of the submodule */
Radek Krejci327de162019-06-14 12:52:07 +0200909 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100910 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 }
912 mod->parsed->parsing = 0;
913
Radek Krejci7fc68292019-06-12 13:51:09 +0200914 /* check name collisions - typedefs and TODO groupings */
Michal Vaskob36053d2020-03-26 15:49:30 +0100915 LY_CHECK_GOTO(lysp_check_typedefs(pctx, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200916
David Sedlák1b623122019-08-05 15:27:49 +0200917 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100918 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200919 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100920 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200921 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200922 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100923
924error_ctx:
925 ly_set_rm(&ctx->list, mod, NULL);
926error:
927 lys_module_free(mod, NULL);
Michal Vaskob36053d2020-03-26 15:49:30 +0100928 ly_set_erase(&pctx->tpdfs_nodes, NULL);
David Sedlák1b623122019-08-05 15:27:49 +0200929 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100930 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200931 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100932 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200933 }
934
Radek Krejcibbe09a92018-11-08 09:36:54 +0100935 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200936}
937
Radek Krejcid14e9692018-11-01 11:00:37 +0100938API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200939lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
940{
Radek Krejci096235c2019-01-11 11:12:19 +0100941 struct lys_module *mod;
942
943 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
944 LY_CHECK_RET(!mod, NULL);
945
946 if (lys_compile(mod, 0)) {
947 ly_set_rm(&ctx->list, mod, NULL);
948 lys_module_free(mod, NULL);
949 return NULL;
950 }
951 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +0200952}
953
954static void
955lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
956{
Radek Krejci65639b92018-11-27 10:51:37 +0100957 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +0200958
959#ifdef __APPLE__
960 if (fcntl(fd, F_GETPATH, path) != -1) {
961 *filename = lydict_insert(ctx, path, 0);
962 }
963#else
Radek Krejci65639b92018-11-27 10:51:37 +0100964 int len;
965 char proc_path[32];
966
Radek Krejci86d106e2018-10-18 09:53:19 +0200967 /* get URI if there is /proc */
968 sprintf(proc_path, "/proc/self/fd/%d", fd);
969 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
970 *filename = lydict_insert(ctx, path, len);
971 }
972#endif
973}
974
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100975void *
Radek Krejcie7b95092019-05-15 11:03:07 +0200976lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct lys_parser_ctx *main_ctx,
Michal Vaskob36053d2020-03-26 15:49:30 +0100977 lys_custom_check custom_check, void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200978{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100979 void *result;
980 struct lys_module *mod = NULL;
981 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200982 size_t length;
983 char *addr;
984
985 LY_CHECK_ARG_RET(ctx, ctx, NULL);
986 if (fd < 0) {
987 LOGARG(ctx, fd);
988 return NULL;
989 }
990
991 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
992 if (!addr) {
993 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
994 return NULL;
995 }
996
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100997 if (main_ctx) {
998 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
999 } else {
1000 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +01001001 if (mod && implement && lys_compile(mod, 0)) {
1002 ly_set_rm(&ctx->list, mod, NULL);
1003 lys_module_free(mod, NULL);
1004 result = mod = NULL;
1005 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001006 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001007 ly_munmap(addr, length);
1008
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001009 if (mod && !mod->filepath) {
1010 lys_parse_set_filename(ctx, &mod->filepath, fd);
1011 } else if (submod && !submod->filepath) {
1012 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +02001013 }
1014
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001015 return result;
1016}
1017
1018struct lys_module *
Michal Vaskob36053d2020-03-26 15:49:30 +01001019lys_parse_fd_module(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, lys_custom_check custom_check,
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001020 void *check_data)
1021{
1022 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
1023}
1024
1025struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +02001026lys_parse_fd_submodule(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 lys_custom_check custom_check, void *check_data)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001028{
1029 assert(main_ctx);
1030 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +02001031}
1032
Radek Krejcid14e9692018-11-01 11:00:37 +01001033API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +02001034lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
1035{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001036 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001037}
1038
Radek Krejcid33273d2018-10-25 14:55:52 +02001039struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001040lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
Michal Vaskob36053d2020-03-26 15:49:30 +01001041 lys_custom_check custom_check, void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +02001042{
1043 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +02001044 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +02001045 const char *rev, *dot, *filename;
1046 size_t len;
1047
1048 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
1049
1050 fd = open(path, O_RDONLY);
1051 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
1052
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +02001054 close(fd);
1055 LY_CHECK_RET(!mod, NULL);
1056
1057 /* check that name and revision match filename */
1058 filename = strrchr(path, '/');
1059 if (!filename) {
1060 filename = path;
1061 } else {
1062 filename++;
1063 }
1064 rev = strchr(filename, '@');
1065 dot = strrchr(filename, '.');
1066
1067 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001068 len = strlen(mod->name);
1069 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +02001070 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001071 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +02001072 }
1073 if (rev) {
1074 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001075 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001076 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001077 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +02001078 }
1079 }
1080
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001081 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001082 /* store URI */
1083 char rpath[PATH_MAX];
1084 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001085 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001086 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001087 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001088 }
1089 }
1090
1091 return mod;
1092}
1093
Radek Krejcid14e9692018-11-01 11:00:37 +01001094API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +02001095lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1096{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001097 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +02001098}
1099
1100API LY_ERR
1101lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1102 char **localfile, LYS_INFORMAT *format)
1103{
1104 size_t len, flen, match_len = 0, dir_len;
1105 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1106 char *wd, *wn = NULL;
1107 DIR *dir = NULL;
1108 struct dirent *file;
1109 char *match_name = NULL;
1110 LYS_INFORMAT format_aux, match_format = 0;
1111 struct ly_set *dirs;
1112 struct stat st;
1113
1114 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1115
1116 /* start to fill the dir fifo with the context's search path (if set)
1117 * and the current working directory */
1118 dirs = ly_set_new();
1119 if (!dirs) {
1120 LOGMEM(NULL);
1121 return EXIT_FAILURE;
1122 }
1123
1124 len = strlen(name);
1125 if (cwd) {
1126 wd = get_current_dir_name();
1127 if (!wd) {
1128 LOGMEM(NULL);
1129 goto cleanup;
1130 } else {
1131 /* add implicit current working directory (./) to be searched,
1132 * this directory is not searched recursively */
1133 if (ly_set_add(dirs, wd, 0) == -1) {
1134 goto cleanup;
1135 }
1136 implicit_cwd = 1;
1137 }
1138 }
1139 if (searchpaths) {
1140 for (i = 0; searchpaths[i]; i++) {
1141 /* check for duplicities with the implicit current working directory */
1142 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1143 implicit_cwd = 0;
1144 continue;
1145 }
1146 wd = strdup(searchpaths[i]);
1147 if (!wd) {
1148 LOGMEM(NULL);
1149 goto cleanup;
1150 } else if (ly_set_add(dirs, wd, 0) == -1) {
1151 goto cleanup;
1152 }
1153 }
1154 }
1155 wd = NULL;
1156
1157 /* start searching */
1158 while (dirs->count) {
1159 free(wd);
1160 free(wn); wn = NULL;
1161
1162 dirs->count--;
1163 wd = (char *)dirs->objs[dirs->count];
1164 dirs->objs[dirs->count] = NULL;
1165 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1166
1167 if (dir) {
1168 closedir(dir);
1169 }
1170 dir = opendir(wd);
1171 dir_len = strlen(wd);
1172 if (!dir) {
1173 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1174 } else {
1175 while ((file = readdir(dir))) {
1176 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1177 /* skip . and .. */
1178 continue;
1179 }
1180 free(wn);
1181 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1182 LOGMEM(NULL);
1183 goto cleanup;
1184 }
1185 if (stat(wn, &st) == -1) {
1186 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1187 file->d_name, wd, strerror(errno));
1188 continue;
1189 }
1190 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1191 /* we have another subdirectory in searchpath to explore,
1192 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1193 if (ly_set_add(dirs, wn, 0) == -1) {
1194 goto cleanup;
1195 }
1196 /* continue with the next item in current directory */
1197 wn = NULL;
1198 continue;
1199 } else if (!S_ISREG(st.st_mode)) {
1200 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1201 continue;
1202 }
1203
1204 /* here we know that the item is a file which can contain a module */
1205 if (strncmp(name, file->d_name, len) ||
1206 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1207 /* different filename than the module we search for */
1208 continue;
1209 }
1210
1211 /* get type according to filename suffix */
1212 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001213 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001214 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001215 /* TODO YIN parser
1216 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1217 format_aux = LYS_IN_YIN;
1218 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001219 } else {
1220 /* not supportde suffix/file format */
1221 continue;
1222 }
1223
1224 if (revision) {
1225 /* we look for the specific revision, try to get it from the filename */
1226 if (file->d_name[len] == '@') {
1227 /* check revision from the filename */
1228 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1229 /* another revision */
1230 continue;
1231 } else {
1232 /* exact revision */
1233 free(match_name);
1234 match_name = wn;
1235 wn = NULL;
1236 match_len = dir_len + 1 + len;
1237 match_format = format_aux;
1238 goto success;
1239 }
1240 } else {
1241 /* continue trying to find exact revision match, use this only if not found */
1242 free(match_name);
1243 match_name = wn;
1244 wn = NULL;
1245 match_len = dir_len + 1 +len;
1246 match_format = format_aux;
1247 continue;
1248 }
1249 } else {
1250 /* remember the revision and try to find the newest one */
1251 if (match_name) {
1252 if (file->d_name[len] != '@' ||
1253 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1254 continue;
1255 } else if (match_name[match_len] == '@' &&
1256 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1257 continue;
1258 }
1259 free(match_name);
1260 }
1261
1262 match_name = wn;
1263 wn = NULL;
1264 match_len = dir_len + 1 + len;
1265 match_format = format_aux;
1266 continue;
1267 }
1268 }
1269 }
1270 }
1271
1272success:
1273 (*localfile) = match_name;
1274 match_name = NULL;
1275 if (format) {
1276 (*format) = match_format;
1277 }
1278 ret = EXIT_SUCCESS;
1279
1280cleanup:
1281 free(wn);
1282 free(wd);
1283 if (dir) {
1284 closedir(dir);
1285 }
1286 free(match_name);
1287 ly_set_free(dirs, free);
1288
1289 return ret;
1290}
1291