blob: 7bafc820acdf5bbe8a41440e8d8592658c8b209f [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 <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdlib.h>
26#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020029
Radek Krejci86d106e2018-10-18 09:53:19 +020030#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
32#include "log.h"
33#include "set.h"
Michal Vasko519fd602020-05-26 12:17:39 +020034#include "xpath.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "tree.h"
37#include "tree_schema.h"
Radek Krejci70853c52018-10-15 14:46:16 +020038#include "tree_schema_internal.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020039#include "parser_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 */
Michal Vasko004d3152020-06-11 19:59:22 +0200307 exp = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1);
Michal Vasko519fd602020-05-26 12:17:39 +0200308 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) {
Michal Vasko004d3152020-06-11 19:59:22 +0200324 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko519fd602020-05-26 12:17:39 +0200325 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 */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200740 LY_CHECK_RET(lys_compile(&mod, LYSC_OPT_INTERNAL));
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200741
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 Krejcif0e1ba52020-05-22 15:14:35 +0200995lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format)
Radek Krejci86d106e2018-10-18 09:53:19 +0200996{
Radek Krejci096235c2019-01-11 11:12:19 +0100997 struct lys_module *mod;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200998 char *filename, *rev, *dot;
999 size_t len;
Radek Krejci096235c2019-01-11 11:12:19 +01001000
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001001 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, NULL);
1002
1003 mod = lys_parse_mem_module(ctx, in->current, format, 1, NULL, NULL);
Radek Krejci096235c2019-01-11 11:12:19 +01001004 LY_CHECK_RET(!mod, NULL);
1005
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001006 switch (in->type) {
1007 case LY_IN_FILEPATH:
1008 /* check that name and revision match filename */
1009 filename = strrchr(in->method.fpath.filepath, '/');
1010 if (!filename) {
1011 filename = in->method.fpath.filepath;
1012 } else {
1013 filename++;
1014 }
1015 rev = strchr(filename, '@');
1016 dot = strrchr(filename, '.');
1017
1018 /* name */
1019 len = strlen(mod->name);
1020 if (strncmp(filename, mod->name, len) ||
1021 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
1022 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1023 }
1024 if (rev) {
1025 len = dot - ++rev;
1026 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
1027 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
1028 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
1029 }
1030 }
1031
1032 break;
1033 case LY_IN_FD:
1034 case LY_IN_FILE:
1035 case LY_IN_MEMORY:
1036 /* nothing special to do */
1037 break;
1038 default:
1039 LOGINT(ctx);
1040 break;
Radek Krejci096235c2019-01-11 11:12:19 +01001041 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001042
1043 lys_parser_fill_filepath(ctx, in, &mod->filepath);
1044 lys_compile(&mod, 0);
1045
Radek Krejci096235c2019-01-11 11:12:19 +01001046 return mod;
Radek Krejci86d106e2018-10-18 09:53:19 +02001047}
1048
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001049API struct lys_module *
1050lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
Radek Krejci86d106e2018-10-18 09:53:19 +02001051{
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001052 LY_ERR ret;
1053 struct ly_in *in = NULL;
1054 struct lys_module *result = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001055
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001056 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, NULL);
Radek Krejci65639b92018-11-27 10:51:37 +01001057
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001058 LY_CHECK_ERR_RET(ret = ly_in_new_memory(data, &in), LOGERR(ctx, ret, "Unable to create input handler."), NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001059
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001060 result = lys_parse(ctx, in, format);
1061 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001062
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 return result;
1064}
1065
Radek Krejcid14e9692018-11-01 11:00:37 +01001066API struct lys_module *
Radek Krejci86d106e2018-10-18 09:53:19 +02001067lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
1068{
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001069 LY_ERR ret;
1070 struct ly_in *in = NULL;
1071 struct lys_module *result = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001072
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001073 LY_CHECK_ARG_RET(ctx, fd != -1, format != LYS_IN_UNKNOWN, NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001074
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001075 LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, &in), LOGERR(ctx, ret, "Unable to create input handler."), NULL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001076
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001077 result = lys_parse(ctx, in, format);
1078 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001079
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001080 return result;
Radek Krejci86d106e2018-10-18 09:53:19 +02001081}
1082
Radek Krejcid14e9692018-11-01 11:00:37 +01001083API struct lys_module *
Radek Krejcid33273d2018-10-25 14:55:52 +02001084lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1085{
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001086 LY_ERR ret;
1087 struct ly_in *in = NULL;
1088 struct lys_module *result = NULL;
1089
1090 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, NULL);
1091
1092 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in), LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), NULL);
1093
1094 result = lys_parse(ctx, in, format);
1095 ly_in_free(in, 0);
1096
1097 return result;
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