blob: d7b7da76d59f2f5cb22c1d6dcdf3459821798b3f [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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
Radek Krejcib7db73a2018-10-24 14:18:40 +020017#include "common.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020018
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <assert.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020020#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020021#include <errno.h>
22#include <fcntl.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <limits.h>
24#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdlib.h>
27#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020030
Radek Krejci86d106e2018-10-18 09:53:19 +020031#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "dict.h"
33#include "log.h"
34#include "set.h"
Michal Vasko519fd602020-05-26 12:17:39 +020035#include "xpath.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree.h"
38#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020039#include "tree_schema_internal.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020040
Radek Krejcia3045382018-11-22 14:30:31 +010041API const struct lysc_node *
42lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
43{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010044 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010045 struct lysc_node **snode;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010046 int action_flag = 0, notif_flag = 0;
47 const struct lysc_action *actions;
48 const struct lysc_notif *notifs;
Radek Krejci7eb54ba2020-05-18 16:30:04 +020049 LY_ARRAY_SIZE_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +010050
51 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
52
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020053next:
Radek Krejcia3045382018-11-22 14:30:31 +010054 if (!last) {
55 /* first call */
56
57 /* get know where to start */
58 if (parent) {
59 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010060 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020061 if (((struct lysc_node_choice*)parent)->cases) {
62 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010063 }
Radek Krejci056d0a82018-12-06 16:57:25 +010064 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010065 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010066 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020067 if (snode && *snode) {
68 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010069 }
Radek Krejcia3045382018-11-22 14:30:31 +010070 }
Radek Krejcia3045382018-11-22 14:30:31 +010071 } else {
72 /* top level data */
73 next = last = module->data;
74 }
75 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010076 /* try to get action or notification */
77 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010078 }
Radek Krejci05b774b2019-02-25 13:26:18 +010079 /* test if the next can be returned */
80 goto check;
81
Michal Vasko1bf09392020-03-27 12:38:10 +010082 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010083 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010084 if (last->parent) {
85 actions = lysc_node_actions(last->parent);
86 } else {
87 actions = module->rpcs;
88 }
89 LY_ARRAY_FOR(actions, u) {
90 if (&actions[u] == (struct lysc_action*)last) {
91 break;
92 }
93 }
94 if (u + 1 < LY_ARRAY_SIZE(actions)) {
95 next = (struct lysc_node*)(&actions[u + 1]);
96 }
97 goto repeat;
98 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +010099 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100100 if (last->parent) {
101 notifs = lysc_node_notifs(last->parent);
102 } else {
103 notifs = module->notifs;
104 }
105 LY_ARRAY_FOR(notifs, u) {
106 if (&notifs[u] == (struct lysc_notif*)last) {
107 break;
108 }
109 }
110 if (u + 1 < LY_ARRAY_SIZE(notifs)) {
111 next = (struct lysc_node*)(&notifs[u + 1]);
112 }
113 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100114 }
115
116 next = last->next;
117repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100118 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
119 /* inside case (as an explicit parent, not when diving into it from choice),
120 * limit the list of children only to the specific case */
121 next = NULL;
122 }
Radek Krejcia3045382018-11-22 14:30:31 +0100123 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100124 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100125 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100126 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200127 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100128 } else if (!action_flag) {
129 action_flag = 1;
130 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
131 } else if (!notif_flag) {
132 notif_flag = 1;
133 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
134 } else {
135 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100136 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100137 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100138 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100139check:
Radek Krejcia3045382018-11-22 14:30:31 +0100140 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100141 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100142 case LYS_ACTION:
143 case LYS_NOTIF:
144 case LYS_LEAF:
145 case LYS_ANYXML:
146 case LYS_ANYDATA:
147 case LYS_LIST:
148 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100149 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100150 break;
151 case LYS_CONTAINER:
152 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
153 if (((struct lysc_node_container *)next)->child) {
154 /* go into */
155 next = ((struct lysc_node_container *)next)->child;
156 } else {
157 next = next->next;
158 }
159 goto repeat;
160 }
161 break;
162 case LYS_CHOICE:
163 if (options & LYS_GETNEXT_WITHCHOICE) {
164 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100165 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
166 next = next->next;
167 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100168 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100169 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100170 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100171 } else {
172 next = ((struct lysc_node_choice *)next)->cases->child;
173 }
Radek Krejcia3045382018-11-22 14:30:31 +0100174 }
175 goto repeat;
176 default:
177 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200178 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100179 return NULL;
180 }
181
182 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
183 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200184 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100185 next = next->next;
186 goto repeat;
187 }
188 }
189 return next;
190}
191
192API const struct lysc_node *
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800193lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath)
194{
195 const char *id = qpath;
196 const char *prefix, *name;
197 size_t prefix_len, name_len;
198 unsigned int u;
199 const struct lysc_node *node = context_node;
200 struct lys_module *mod = NULL;
201
202 LY_CHECK_ARG_RET(ctx, qpath, NULL);
203
204 while(*id) {
205 if (id[0] == '/') {
206 ++id;
207 }
208 /* precess ".." in relative paths */
209 while (!strncmp("../", id, 3)) {
210 id += 3;
211 if (!node) {
212 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath);
213 return NULL;
214 }
215 node = node->parent;
216 }
217
218 if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) {
219 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id- qpath, qpath);
220 return NULL;
221 }
222 if (prefix) {
223 if (context_node) {
224 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
225 } else {
226 for (u = 0; u < ctx->list.count; ++u) {
227 if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) {
228 struct lys_module *m = (struct lys_module *)ctx->list.objs[u];
229 if (mod) {
230 if (m->implemented) {
231 mod = m;
232 break;
233 } else if (m->latest_revision) {
234 mod = m;
235 }
236 } else {
237 mod = m;
238 }
239 }
240 }
241 }
242 }
243 if (!mod) {
244 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".",
245 id - qpath, qpath);
246 return NULL;
247 }
248
249 node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
250 if (!node) {
251 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath);
252 return NULL;
253 }
254 }
255
256 return node;
257}
258
259API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100260lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
261 uint16_t nodetype, int options)
Radek Krejcia3045382018-11-22 14:30:31 +0100262{
263 const struct lysc_node *node = NULL;
264
265 LY_CHECK_ARG_RET(NULL, module, name, NULL);
266 if (!nodetype) {
267 nodetype = 0xffff;
268 }
269
270 while ((node = lys_getnext(node, parent, module->compiled, options))) {
271 if (!(node->nodetype & nodetype)) {
272 continue;
273 }
274 if (node->module != module) {
275 continue;
276 }
277
278 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200279 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100280 return node;
281 }
282 } else {
283 if (!strcmp(node->name, name)) {
284 return node;
285 }
286 }
287 }
288 return NULL;
289}
290
Michal Vasko519fd602020-05-26 12:17:39 +0200291API LY_ERR
292lys_atomize_xpath(const struct lysc_node *ctx_node, const char *xpath, int options, struct ly_set **set)
293{
294 LY_ERR ret = LY_SUCCESS;
295 struct lyxp_set xp_set;
296 struct lyxp_expr *exp;
297 uint32_t i;
298
299 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
300 if (!(options & LYXP_SCNODE_ALL)) {
301 options = LYXP_SCNODE;
302 }
303
304 memset(&xp_set, 0, sizeof xp_set);
305
306 /* compile expression */
307 exp = lyxp_expr_parse(ctx_node->module->ctx, xpath);
308 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
309
310 /* atomize expression */
311 ret = lyxp_atomize(exp, LYD_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &xp_set, options);
312 LY_CHECK_GOTO(ret, cleanup);
313
314 /* allocate return set */
315 *set = ly_set_new();
316 LY_CHECK_ERR_GOTO(!*set, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
317
318 /* transform into ly_set */
319 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
320 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
321 (*set)->size = xp_set.used;
322
323 for (i = 0; i < xp_set.used; ++i) {
324 if (xp_set.val.nodes[i].type == LYXP_NODE_ELEM) {
325 ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST);
326 }
327 }
328
329cleanup:
330 lyxp_set_free_content(&xp_set);
331 lyxp_expr_free(ctx_node->module->ctx, exp);
332 return ret;
333}
334
Michal Vasko14654712020-02-06 08:35:21 +0100335char *
336lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
337 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200338{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200339 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200340 char *path = NULL;
341 int len = 0;
342
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200343 LY_CHECK_ARG_RET(NULL, node, NULL);
344 if (buffer) {
345 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
346 }
347
Radek Krejci327de162019-06-14 12:52:07 +0200348 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200349 case LYSC_PATH_LOG:
Michal Vasko90932a92020-02-12 14:33:03 +0100350 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200351 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200352 char *id;
Michal Vasko14654712020-02-06 08:35:21 +0100353 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200354
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100356 if (parent && (iter->parent == parent)) {
357 slash = "";
358 } else {
359 slash = "/";
360 }
Radek Krejci327de162019-06-14 12:52:07 +0200361 if (!iter->parent || iter->parent->module != iter->module) {
362 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200363 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100364 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200365 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100366 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200367 }
Radek Krejci327de162019-06-14 12:52:07 +0200368 } else {
369 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200370 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100371 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200372 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100373 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200374 }
Radek Krejci327de162019-06-14 12:52:07 +0200375 }
376 free(s);
377 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200378
379 if (buffer && buflen <= (size_t)len) {
380 /* not enough space in buffer */
381 break;
382 }
Radek Krejci327de162019-06-14 12:52:07 +0200383 }
384
385 if (len < 0) {
386 free(path);
387 path = NULL;
388 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200389 if (buffer) {
390 strcpy(buffer, "/");
391 } else {
392 path = strdup("/");
393 }
Radek Krejci327de162019-06-14 12:52:07 +0200394 }
395 break;
396 }
397
Radek Krejci1c0c3442019-07-23 16:08:47 +0200398 if (buffer) {
399 return buffer;
400 } else {
401 return path;
402 }
Radek Krejci327de162019-06-14 12:52:07 +0200403}
404
Michal Vasko14654712020-02-06 08:35:21 +0100405API char *
406lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
407{
408 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
409}
410
Michal Vasko28d78432020-05-26 13:10:53 +0200411API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100412lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200413{
Michal Vasko28d78432020-05-26 13:10:53 +0200414 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
415 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200416}
417
Radek Krejci693262f2019-04-29 15:23:20 +0200418uint8_t
419lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200420{
421 uint8_t *item;
422 uint8_t mask = 3, result;
423
424 assert(pos >= 0);
425
426 item = &list[pos / 4];
427 result = (*item) & (mask << 2 * (pos % 4));
428 return result >> 2 * (pos % 4);
429}
430
Michal Vasko28d78432020-05-26 13:10:53 +0200431static LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200432lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
433{
434 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200435 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200436
Radek Krejci693262f2019-04-29 15:23:20 +0200437 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200438 (*index_e)++;
439
440 switch (op) {
441 case LYS_IFF_F:
442 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200443 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200444 case LYS_IFF_NOT:
445 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200446 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200447 case LYS_IFF_AND:
448 case LYS_IFF_OR:
449 a = lysc_iffeature_value_(iff, index_e, index_f);
450 b = lysc_iffeature_value_(iff, index_e, index_f);
451 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200452 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
453 return LY_SUCCESS;
454 } else {
455 return LY_ENOT;
456 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200457 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200458 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
459 return LY_SUCCESS;
460 } else {
461 return LY_ENOT;
462 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200463 }
464 }
465
466 return 0;
467}
468
Michal Vasko28d78432020-05-26 13:10:53 +0200469API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200470lysc_iffeature_value(const struct lysc_iffeature *iff)
471{
472 int index_e = 0, index_f = 0;
473
474 LY_CHECK_ARG_RET(NULL, iff, -1);
475
476 if (iff->expr) {
477 return lysc_iffeature_value_(iff, &index_e, &index_f);
478 }
479 return 0;
480}
481
Radek Krejci151a5b72018-10-19 14:21:44 +0200482/**
483 * @brief Enable/Disable the specified feature in the module.
484 *
485 * If the feature is already set to the desired value, LY_SUCCESS is returned.
486 * By changing the feature, also all the feature which depends on it via their
487 * if-feature statements are again evaluated (disabled if a if-feature statemen
488 * evaluates to false).
489 *
Radek Krejci0af46292019-01-11 16:02:31 +0100490 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200491 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
492 * set all the features in the module.
493 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
494 * @return LY_ERR value.
495 */
496static LY_ERR
Radek Krejci0af46292019-01-11 16:02:31 +0100497lys_feature_change(const struct lys_module *mod, const char *name, int value)
Radek Krejci151a5b72018-10-19 14:21:44 +0200498{
499 int all = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200500 LY_ARRAY_SIZE_TYPE u, disabled_count;
501 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200502 struct lysc_feature *f, **df;
503 struct lysc_iffeature *iff;
504 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100505 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200506
Radek Krejci6e67c402019-05-02 09:55:39 +0200507 if (!strcmp(name, "*")) {
508 /* enable all */
509 all = 1;
510 }
511
Radek Krejci0af46292019-01-11 16:02:31 +0100512 if (!mod->compiled) {
513 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
514 mod->name);
515 return LY_EINVAL;
516 }
517 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200518 if (all) {
519 /* no feature to enable */
520 return LY_SUCCESS;
521 }
Radek Krejci0af46292019-01-11 16:02:31 +0100522 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200523 return LY_EINVAL;
524 }
525
Radek Krejci151a5b72018-10-19 14:21:44 +0200526 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100527 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200528
Radek Krejcica3db002018-11-01 10:31:01 +0100529run:
Radek Krejci0af46292019-01-11 16:02:31 +0100530 for (disabled_count = u = 0; u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
531 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200532 if (all || !strcmp(f->name, name)) {
533 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
534 if (all) {
535 /* skip already set features */
536 continue;
537 } else {
538 /* feature already set correctly */
539 ly_set_free(changed, NULL);
540 return LY_SUCCESS;
541 }
542 }
543
544 if (value) { /* enable */
545 /* check referenced features if they are enabled */
546 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200547 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200548 if (all) {
Radek Krejcica3db002018-11-01 10:31:01 +0100549 ++disabled_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200550 goto next;
551 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100552 LOGERR(ctx, LY_EDENIED,
Radek Krejci151a5b72018-10-19 14:21:44 +0200553 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
554 f->name);
555 ly_set_free(changed, NULL);
556 return LY_EDENIED;
557 }
558 }
559 }
560 /* enable the feature */
561 f->flags |= LYS_FENABLED;
562 } else { /* disable */
563 /* disable the feature */
564 f->flags &= ~LYS_FENABLED;
565 }
566
567 /* remember the changed feature */
568 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
569
570 if (!all) {
571 /* stop in case changing a single feature */
572 break;
573 }
574 }
575next:
576 ;
577 }
578
579 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100580 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200581 ly_set_free(changed, NULL);
582 return LY_EINVAL;
583 }
584
Radek Krejcica3db002018-11-01 10:31:01 +0100585 if (value && all && disabled_count) {
586 if (changed_count == changed->count) {
587 /* no change in last run -> not able to enable all ... */
588 /* ... print errors */
Radek Krejci0af46292019-01-11 16:02:31 +0100589 for (u = 0; disabled_count && u < LY_ARRAY_SIZE(mod->compiled->features); ++u) {
590 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100591 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100592 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100593 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100594 --disabled_count;
595 }
596 }
597 /* ... restore the original state */
598 for (u = 0; u < changed->count; ++u) {
599 f = changed->objs[u];
600 /* re-disable the feature */
601 f->flags &= ~LYS_FENABLED;
602 }
603
604 ly_set_free(changed, NULL);
605 return LY_EDENIED;
606 } else {
607 /* we did some change in last run, try it again */
608 changed_count = changed->count;
609 goto run;
610 }
611 }
612
Radek Krejci151a5b72018-10-19 14:21:44 +0200613 /* reflect change(s) in the dependent features */
614 for (u = 0; u < changed->count; ++u) {
615 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
616 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
617 * is not done - by default, features are disabled and must be explicitely enabled. */
618 f = changed->objs[u];
619 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
620 if (!((*df)->flags & LYS_FENABLED)) {
621 /* not enabled, nothing to do */
622 continue;
623 }
624 /* check the feature's if-features which could change by the previous change of our feature */
625 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200626 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200627 /* the feature must be disabled now */
628 (*df)->flags &= ~LYS_FENABLED;
629 /* add the feature into the list of changed features */
630 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
631 break;
632 }
633 }
634 }
635 }
636
637 ly_set_free(changed, NULL);
638 return LY_SUCCESS;
639}
640
641API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200642lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200643{
Radek Krejci0af46292019-01-11 16:02:31 +0100644 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200645
Radek Krejcied5acc52019-04-25 15:57:04 +0200646 return lys_feature_change((struct lys_module*)module, feature, 1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200647}
648
649API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200650lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200651{
Radek Krejci0af46292019-01-11 16:02:31 +0100652 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200653
Radek Krejcied5acc52019-04-25 15:57:04 +0200654 return lys_feature_change((struct lys_module*)module, feature, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200655}
656
657API int
658lys_feature_value(const struct lys_module *module, const char *feature)
659{
660 struct lysc_feature *f;
661 struct lysc_module *mod;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200662 LY_ARRAY_SIZE_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200663
664 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
665 mod = module->compiled;
666
667 /* search for the specified feature */
668 for (u = 0; u < LY_ARRAY_SIZE(mod->features); ++u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200669 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200670 if (!strcmp(f->name, feature)) {
671 if (f->flags & LYS_FENABLED) {
672 return 1;
673 } else {
674 return 0;
675 }
676 }
677 }
678
679 /* feature definition not found */
680 return -1;
681}
682
Michal Vaskoc193ce92020-03-06 11:04:48 +0100683API const struct lysc_node *
Radek Krejcifab954b2019-09-11 11:25:14 +0200684lysc_node_is_disabled(const struct lysc_node *node, int recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100685{
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200686 LY_ARRAY_SIZE_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100687
688 LY_CHECK_ARG_RET(NULL, node, NULL);
689
Michal Vaskoc193ce92020-03-06 11:04:48 +0100690 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100691 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100692 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100693 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200694 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100695 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100696 }
697 }
698 }
699
700 if (!recursive) {
701 return NULL;
702 }
703
Michal Vaskoc193ce92020-03-06 11:04:48 +0100704 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100705 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100706 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
707
Radek Krejcia3045382018-11-22 14:30:31 +0100708 return NULL;
709}
710
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200711LY_ERR
712lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
713{
714 struct lys_module *m;
715
716 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
717
718 if (mod->implemented) {
719 return LY_SUCCESS;
720 }
721
722 /* we have module from the current context */
723 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
724 if (m) {
725 if (m != mod) {
726 /* check collision with other implemented revision */
727 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
728 mod->name, mod->revision ? mod->revision : "module without revision");
729 return LY_EDENIED;
730 } else {
731 /* mod is already implemented */
732 return LY_SUCCESS;
733 }
734 }
735
736 /* mark the module implemented, check for collision was already done */
737 mod->implemented = value;
738
739 /* compile the schema */
740 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
741
742 return LY_SUCCESS;
743}
744
745API LY_ERR
746lys_set_implemented(struct lys_module *mod)
747{
748 return lys_set_implemented_internal(mod, 1);
749}
750
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100751struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200752lys_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 +0100753 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 +0200754{
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100755 LY_ERR ret = LY_EINVAL;
756 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100757 struct lys_yang_parser_ctx *yangctx = NULL;
758 struct lys_yin_parser_ctx *yinctx = NULL;
759 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100760
761 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
762
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100763 switch (format) {
764 case LYS_IN_YIN:
Michal Vaskob36053d2020-03-26 15:49:30 +0100765 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, data, &submod);
766 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 break;
768 case LYS_IN_YANG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100769 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, data, &submod);
770 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771 break;
772 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200773 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100774 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200775 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100776 LY_CHECK_RET(ret, NULL);
777
778 /* make sure that the newest revision is at position 0 */
779 lysp_sort_revisions(submod->revs);
780
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100781 /* decide the latest revision */
Michal Vaskob36053d2020-03-26 15:49:30 +0100782 latest_sp = ly_ctx_get_submodule(PARSER_CTX(pctx), submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100783 if (latest_sp) {
784 if (submod->revs) {
785 if (!latest_sp->revs) {
786 /* latest has no revision, so mod is anyway newer */
787 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200788 /* the latest_sp is zeroed later when the new module is being inserted into the context */
789 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
790 submod->latest_revision = latest_sp->latest_revision;
791 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200793 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200795 } else {
796 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100797 }
798 } else {
799 submod->latest_revision = 1;
800 }
801
Radek Krejcib3289d62019-09-18 12:21:39 +0200802 if (custom_check) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100803 LY_CHECK_GOTO(custom_check(PARSER_CTX(pctx), NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200804 }
805
806 if (latest_sp) {
807 latest_sp->latest_revision = 0;
808 }
809
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100810 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100811 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
812 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100813
David Sedlák1b623122019-08-05 15:27:49 +0200814 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100815 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200816 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100817 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200818 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100819 return submod;
David Sedlák1b623122019-08-05 15:27:49 +0200820
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100821error:
822 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200823 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100824 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200825 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100826 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200827 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100828 return NULL;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200829}
830
Radek Krejcid33273d2018-10-25 14:55:52 +0200831struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100832lys_parse_mem_module(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement,
Michal Vaskob36053d2020-03-26 15:49:30 +0100833 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 +0100834 void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +0200835{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100836 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200837 struct lysp_import *imp;
838 struct lysp_include *inc;
Radek Krejci9ed7a192018-10-31 16:23:51 +0100839 LY_ERR ret = LY_EINVAL;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200840 LY_ARRAY_SIZE_TYPE u, v;
Michal Vaskob36053d2020-03-26 15:49:30 +0100841 struct lys_yang_parser_ctx *yangctx = NULL;
842 struct lys_yin_parser_ctx *yinctx = NULL;
843 struct lys_parser_ctx *pctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200844
845 LY_CHECK_ARG_RET(ctx, ctx, data, NULL);
846
847 mod = calloc(1, sizeof *mod);
848 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100849 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200850
851 switch (format) {
852 case LYS_IN_YIN:
Michal Vaskob36053d2020-03-26 15:49:30 +0100853 ret = yin_parse_module(&yinctx, data, mod);
854 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200855 break;
856 case LYS_IN_YANG:
Michal Vaskob36053d2020-03-26 15:49:30 +0100857 ret = yang_parse_module(&yangctx, data, mod);
858 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200859 break;
860 default:
861 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
862 break;
863 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100864 LY_CHECK_ERR_RET(ret, lys_module_free(mod, NULL), NULL);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200865
866 /* make sure that the newest revision is at position 0 */
867 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100868 if (mod->parsed->revs) {
869 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
870 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200871
Radek Krejcib3289d62019-09-18 12:21:39 +0200872 /* decide the latest revision */
873 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
874 if (latest) {
875 if (mod->revision) {
876 if (!latest->revision) {
877 /* latest has no revision, so mod is anyway newer */
878 mod->latest_revision = latest->latest_revision;
879 /* the latest is zeroed later when the new module is being inserted into the context */
880 } else if (strcmp(mod->revision, latest->revision) > 0) {
881 mod->latest_revision = latest->latest_revision;
882 /* the latest is zeroed later when the new module is being inserted into the context */
883 } else {
884 latest = NULL;
885 }
886 } else {
887 latest = NULL;
888 }
889 } else {
890 mod->latest_revision = 1;
891 }
892
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893 if (custom_check) {
894 LY_CHECK_GOTO(custom_check(ctx, mod->parsed, NULL, check_data), error);
895 }
896
Radek Krejci86d106e2018-10-18 09:53:19 +0200897 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200898 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100899 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
900 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100901 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200902 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200904 }
905
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100906 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100907 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100908 if (mod_dup) {
909 if (mod_dup->parsed) {
910 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200911 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
913 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200914 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100915 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
916 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200917 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100918 goto error;
919 } else {
920 /* add the parsed data to the currently compiled-only module in the context */
921 mod_dup->parsed = mod->parsed;
922 mod_dup->parsed->mod = mod_dup;
923 mod->parsed = NULL;
924 lys_module_free(mod, NULL);
925 mod = mod_dup;
926 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200927 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100928 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200929
Radek Krejci0af46292019-01-11 16:02:31 +0100930 if (!mod->implemented) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100931 /* pre-compile features of the module */
Radek Krejci327de162019-06-14 12:52:07 +0200932 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100933 }
934
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 if (latest) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200936 latest->latest_revision = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200938
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100939 /* add into context */
940 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200941
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100942finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100943 /* resolve imports */
944 mod->parsed->parsing = 1;
945 LY_ARRAY_FOR(mod->parsed->imports, u) {
946 imp = &mod->parsed->imports[u];
947 if (!imp->module && lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module)) {
948 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200949 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100950 /* check for importing the same module twice */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200951 for (v = 0; v < u; ++v) {
952 if (imp->module == mod->parsed->imports[v].module) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100953 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 +0100954 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200955 }
956 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200957 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100958 LY_ARRAY_FOR(mod->parsed->includes, u) {
959 inc = &mod->parsed->includes[u];
Michal Vaskob36053d2020-03-26 15:49:30 +0100960 if (!inc->submodule && lysp_load_submodule(pctx, mod->parsed, inc)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100961 goto error_ctx;
962 }
Radek Krejci0af46292019-01-11 16:02:31 +0100963 if (!mod->implemented) {
Michal Vasko5fe75f12020-03-02 13:52:37 +0100964 /* pre-compile features of the submodule */
Radek Krejci327de162019-06-14 12:52:07 +0200965 LY_CHECK_GOTO(lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->off_features), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100966 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967 }
968 mod->parsed->parsing = 0;
969
Radek Krejci7fc68292019-06-12 13:51:09 +0200970 /* check name collisions - typedefs and TODO groupings */
Michal Vaskob36053d2020-03-26 15:49:30 +0100971 LY_CHECK_GOTO(lysp_check_typedefs(pctx, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +0200972
David Sedlák1b623122019-08-05 15:27:49 +0200973 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100974 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200975 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100976 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200977 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200978 return mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100979
980error_ctx:
981 ly_set_rm(&ctx->list, mod, NULL);
982error:
983 lys_module_free(mod, NULL);
Michal Vaskob36053d2020-03-26 15:49:30 +0100984 ly_set_erase(&pctx->tpdfs_nodes, NULL);
David Sedlák1b623122019-08-05 15:27:49 +0200985 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100986 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200987 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100988 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200989 }
990
Radek Krejcibbe09a92018-11-08 09:36:54 +0100991 return NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200992}
993
Radek Krejcid14e9692018-11-01 11:00:37 +0100994API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +0200995lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
996{
Radek Krejci096235c2019-01-11 11:12:19 +0100997 struct lys_module *mod;
998
999 mod = lys_parse_mem_module(ctx, data, format, 1, NULL, NULL);
1000 LY_CHECK_RET(!mod, NULL);
1001
1002 if (lys_compile(mod, 0)) {
1003 ly_set_rm(&ctx->list, mod, NULL);
1004 lys_module_free(mod, NULL);
1005 return NULL;
1006 }
1007 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +02001008}
1009
1010static void
1011lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
1012{
Radek Krejci65639b92018-11-27 10:51:37 +01001013 char path[PATH_MAX];
Radek Krejci86d106e2018-10-18 09:53:19 +02001014
1015#ifdef __APPLE__
1016 if (fcntl(fd, F_GETPATH, path) != -1) {
1017 *filename = lydict_insert(ctx, path, 0);
1018 }
1019#else
Radek Krejci65639b92018-11-27 10:51:37 +01001020 int len;
1021 char proc_path[32];
1022
Radek Krejci86d106e2018-10-18 09:53:19 +02001023 /* get URI if there is /proc */
1024 sprintf(proc_path, "/proc/self/fd/%d", fd);
1025 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
1026 *filename = lydict_insert(ctx, path, len);
1027 }
1028#endif
1029}
1030
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001031void *
Radek Krejcie7b95092019-05-15 11:03:07 +02001032lys_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 +01001033 lys_custom_check custom_check, void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +02001034{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001035 void *result;
1036 struct lys_module *mod = NULL;
1037 struct lysp_submodule *submod = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001038 size_t length;
1039 char *addr;
1040
1041 LY_CHECK_ARG_RET(ctx, ctx, NULL);
1042 if (fd < 0) {
1043 LOGARG(ctx, fd);
1044 return NULL;
1045 }
1046
1047 LY_CHECK_RET(ly_mmap(ctx, fd, &length, (void **)&addr), NULL);
1048 if (!addr) {
1049 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
1050 return NULL;
1051 }
1052
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 if (main_ctx) {
1054 result = submod = lys_parse_mem_submodule(ctx, addr, format, main_ctx, custom_check, check_data);
1055 } else {
1056 result = mod = lys_parse_mem_module(ctx, addr, format, implement, custom_check, check_data);
Radek Krejci096235c2019-01-11 11:12:19 +01001057 if (mod && implement && lys_compile(mod, 0)) {
1058 ly_set_rm(&ctx->list, mod, NULL);
1059 lys_module_free(mod, NULL);
1060 result = mod = NULL;
1061 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001063 ly_munmap(addr, length);
1064
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001065 if (mod && !mod->filepath) {
1066 lys_parse_set_filename(ctx, &mod->filepath, fd);
1067 } else if (submod && !submod->filepath) {
1068 lys_parse_set_filename(ctx, &submod->filepath, fd);
Radek Krejci86d106e2018-10-18 09:53:19 +02001069 }
1070
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001071 return result;
1072}
1073
1074struct lys_module *
Michal Vaskob36053d2020-03-26 15:49:30 +01001075lys_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 +01001076 void *check_data)
1077{
1078 return (struct lys_module*)lys_parse_fd_(ctx, fd, format, implement, NULL, custom_check, check_data);
1079}
1080
1081struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +02001082lys_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 +01001083 lys_custom_check custom_check, void *check_data)
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001084{
1085 assert(main_ctx);
1086 return (struct lysp_submodule*)lys_parse_fd_(ctx, fd, format, 0, main_ctx, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +02001087}
1088
Radek Krejcid14e9692018-11-01 11:00:37 +01001089API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +02001090lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
1091{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001092 return lys_parse_fd_module(ctx, fd, format, 1, NULL, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001093}
1094
Radek Krejcid33273d2018-10-25 14:55:52 +02001095struct lys_module *
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001096lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement,
Michal Vaskob36053d2020-03-26 15:49:30 +01001097 lys_custom_check custom_check, void *check_data)
Radek Krejci86d106e2018-10-18 09:53:19 +02001098{
1099 int fd;
Radek Krejcid33273d2018-10-25 14:55:52 +02001100 struct lys_module *mod;
Radek Krejci86d106e2018-10-18 09:53:19 +02001101 const char *rev, *dot, *filename;
1102 size_t len;
1103
1104 LY_CHECK_ARG_RET(ctx, ctx, path, NULL);
1105
1106 fd = open(path, O_RDONLY);
1107 LY_CHECK_ERR_RET(fd == -1, LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)), NULL);
1108
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001109 mod = lys_parse_fd_module(ctx, fd, format, implement, custom_check, check_data);
Radek Krejci86d106e2018-10-18 09:53:19 +02001110 close(fd);
1111 LY_CHECK_RET(!mod, NULL);
1112
1113 /* check that name and revision match filename */
1114 filename = strrchr(path, '/');
1115 if (!filename) {
1116 filename = path;
1117 } else {
1118 filename++;
1119 }
1120 rev = strchr(filename, '@');
1121 dot = strrchr(filename, '.');
1122
1123 /* name */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001124 len = strlen(mod->name);
1125 if (strncmp(filename, mod->name, len) ||
Radek Krejci86d106e2018-10-18 09:53:19 +02001126 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
Radek Krejci86d106e2018-10-18 09:53:19 +02001128 }
1129 if (rev) {
1130 len = dot - ++rev;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001131 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001132 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001133 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejci86d106e2018-10-18 09:53:19 +02001134 }
1135 }
1136
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001137 if (!mod->filepath) {
Radek Krejci86d106e2018-10-18 09:53:19 +02001138 /* store URI */
1139 char rpath[PATH_MAX];
1140 if (realpath(path, rpath) != NULL) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001141 mod->filepath = lydict_insert(ctx, rpath, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001142 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001143 mod->filepath = lydict_insert(ctx, path, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001144 }
1145 }
1146
1147 return mod;
1148}
1149
Radek Krejcid14e9692018-11-01 11:00:37 +01001150API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +02001151lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1152{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001153 return lys_parse_path_(ctx, path, format, 1, NULL, NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +02001154}
1155
1156API LY_ERR
1157lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1158 char **localfile, LYS_INFORMAT *format)
1159{
1160 size_t len, flen, match_len = 0, dir_len;
1161 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1162 char *wd, *wn = NULL;
1163 DIR *dir = NULL;
1164 struct dirent *file;
1165 char *match_name = NULL;
1166 LYS_INFORMAT format_aux, match_format = 0;
1167 struct ly_set *dirs;
1168 struct stat st;
1169
1170 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1171
1172 /* start to fill the dir fifo with the context's search path (if set)
1173 * and the current working directory */
1174 dirs = ly_set_new();
1175 if (!dirs) {
1176 LOGMEM(NULL);
1177 return EXIT_FAILURE;
1178 }
1179
1180 len = strlen(name);
1181 if (cwd) {
1182 wd = get_current_dir_name();
1183 if (!wd) {
1184 LOGMEM(NULL);
1185 goto cleanup;
1186 } else {
1187 /* add implicit current working directory (./) to be searched,
1188 * this directory is not searched recursively */
1189 if (ly_set_add(dirs, wd, 0) == -1) {
1190 goto cleanup;
1191 }
1192 implicit_cwd = 1;
1193 }
1194 }
1195 if (searchpaths) {
1196 for (i = 0; searchpaths[i]; i++) {
1197 /* check for duplicities with the implicit current working directory */
1198 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1199 implicit_cwd = 0;
1200 continue;
1201 }
1202 wd = strdup(searchpaths[i]);
1203 if (!wd) {
1204 LOGMEM(NULL);
1205 goto cleanup;
1206 } else if (ly_set_add(dirs, wd, 0) == -1) {
1207 goto cleanup;
1208 }
1209 }
1210 }
1211 wd = NULL;
1212
1213 /* start searching */
1214 while (dirs->count) {
1215 free(wd);
1216 free(wn); wn = NULL;
1217
1218 dirs->count--;
1219 wd = (char *)dirs->objs[dirs->count];
1220 dirs->objs[dirs->count] = NULL;
1221 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1222
1223 if (dir) {
1224 closedir(dir);
1225 }
1226 dir = opendir(wd);
1227 dir_len = strlen(wd);
1228 if (!dir) {
1229 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1230 } else {
1231 while ((file = readdir(dir))) {
1232 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1233 /* skip . and .. */
1234 continue;
1235 }
1236 free(wn);
1237 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1238 LOGMEM(NULL);
1239 goto cleanup;
1240 }
1241 if (stat(wn, &st) == -1) {
1242 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1243 file->d_name, wd, strerror(errno));
1244 continue;
1245 }
1246 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1247 /* we have another subdirectory in searchpath to explore,
1248 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1249 if (ly_set_add(dirs, wn, 0) == -1) {
1250 goto cleanup;
1251 }
1252 /* continue with the next item in current directory */
1253 wn = NULL;
1254 continue;
1255 } else if (!S_ISREG(st.st_mode)) {
1256 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1257 continue;
1258 }
1259
1260 /* here we know that the item is a file which can contain a module */
1261 if (strncmp(name, file->d_name, len) ||
1262 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1263 /* different filename than the module we search for */
1264 continue;
1265 }
1266
1267 /* get type according to filename suffix */
1268 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001269 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001270 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001271 /* TODO YIN parser
1272 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1273 format_aux = LYS_IN_YIN;
1274 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001275 } else {
1276 /* not supportde suffix/file format */
1277 continue;
1278 }
1279
1280 if (revision) {
1281 /* we look for the specific revision, try to get it from the filename */
1282 if (file->d_name[len] == '@') {
1283 /* check revision from the filename */
1284 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1285 /* another revision */
1286 continue;
1287 } else {
1288 /* exact revision */
1289 free(match_name);
1290 match_name = wn;
1291 wn = NULL;
1292 match_len = dir_len + 1 + len;
1293 match_format = format_aux;
1294 goto success;
1295 }
1296 } else {
1297 /* continue trying to find exact revision match, use this only if not found */
1298 free(match_name);
1299 match_name = wn;
1300 wn = NULL;
1301 match_len = dir_len + 1 +len;
1302 match_format = format_aux;
1303 continue;
1304 }
1305 } else {
1306 /* remember the revision and try to find the newest one */
1307 if (match_name) {
1308 if (file->d_name[len] != '@' ||
1309 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1310 continue;
1311 } else if (match_name[match_len] == '@' &&
1312 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1313 continue;
1314 }
1315 free(match_name);
1316 }
1317
1318 match_name = wn;
1319 wn = NULL;
1320 match_len = dir_len + 1 + len;
1321 match_format = format_aux;
1322 continue;
1323 }
1324 }
1325 }
1326 }
1327
1328success:
1329 (*localfile) = match_name;
1330 match_name = NULL;
1331 if (format) {
1332 (*format) = match_format;
1333 }
1334 ret = EXIT_SUCCESS;
1335
1336cleanup:
1337 free(wn);
1338 free(wd);
1339 if (dir) {
1340 closedir(dir);
1341 }
1342 free(match_name);
1343 ly_set_free(dirs, free);
1344
1345 return ret;
1346}
1347