blob: 7e094ff2e9274285503983fdfcb3f0f5217bed97 [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 Krejcica376bd2020-06-11 16:04:06 +020017#include "tree_schema.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>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdlib.h>
25#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020028
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020030#include "compat.h"
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"
Michal Vaskoafac7822020-10-20 14:22:26 +020034#include "in_internal.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020035#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020036#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020037#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile.h"
39#include "schema_compile_amend.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020040#include "set.h"
41#include "tree.h"
42#include "tree_schema_internal.h"
43#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020044
Michal Vaskof1ab44f2020-10-22 08:58:32 +020045API LY_ERR
46lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
47{
48 struct lysc_node *elem, *ops, *elem2;
49 LY_ARRAY_COUNT_TYPE u;
50
51 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
52
53 LYSC_TREE_DFS_BEGIN(root, elem) {
54 /* schema node */
55 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
56
57 ops = (struct lysc_node *)lysc_node_actions(elem);
58 LY_ARRAY_FOR(ops, u) {
59 LYSC_TREE_DFS_BEGIN(&ops[u], elem2) {
60 /* action subtree */
61 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
62
63 LYSC_TREE_DFS_END(&ops[u], elem2);
64 }
65 }
66
67 ops = (struct lysc_node *)lysc_node_notifs(elem);
68 LY_ARRAY_FOR(ops, u) {
69 LYSC_TREE_DFS_BEGIN(&ops[u], elem2) {
70 /* notification subtree */
71 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
72
73 LYSC_TREE_DFS_END(&ops[u], elem2);
74 }
75 }
76
77 LYSC_TREE_DFS_END(root, elem);
78 }
79
80 return LY_SUCCESS;
81}
82
83API LY_ERR
84lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
85{
86 LY_ARRAY_COUNT_TYPE u;
87
88 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
89
90 /* schema nodes */
91 LY_CHECK_RET(lysc_tree_dfs_full(mod->compiled->data, dfs_clb, data));
92
93 /* RPCs */
94 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
95 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
96 }
97
98 /* notifications */
99 LY_ARRAY_FOR(mod->compiled->notifs, u) {
100 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
101 }
102
103 return LY_SUCCESS;
104}
105
Radek Krejcia3045382018-11-22 14:30:31 +0100106API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200107lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100108{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100109 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100110 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200111 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100112 const struct lysc_action *actions;
113 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200114 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100115
116 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
117
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200118next:
Radek Krejcia3045382018-11-22 14:30:31 +0100119 if (!last) {
120 /* first call */
121
122 /* get know where to start */
123 if (parent) {
124 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200125 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200126 if (((struct lysc_node_choice *)parent)->cases) {
127 next = last = (const struct lysc_node *)&((struct lysc_node_choice *)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +0100128 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100129 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100130 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100131 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200132 if (snode && *snode) {
133 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100134 }
Radek Krejcia3045382018-11-22 14:30:31 +0100135 }
Radek Krejcia3045382018-11-22 14:30:31 +0100136 } else {
137 /* top level data */
138 next = last = module->data;
139 }
140 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100141 /* try to get action or notification */
142 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100143 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100144 /* test if the next can be returned */
145 goto check;
146
Michal Vasko1bf09392020-03-27 12:38:10 +0100147 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100148 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100149 if (last->parent) {
150 actions = lysc_node_actions(last->parent);
151 } else {
152 actions = module->rpcs;
153 }
154 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200155 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100156 break;
157 }
158 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200159 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200160 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100161 }
162 goto repeat;
163 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100164 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100165 if (last->parent) {
166 notifs = lysc_node_notifs(last->parent);
167 } else {
168 notifs = module->notifs;
169 }
170 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200171 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100172 break;
173 }
174 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200175 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200176 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100177 }
178 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200179 } else {
180 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100181 }
182
Radek Krejcia3045382018-11-22 14:30:31 +0100183repeat:
184 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100185 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200186 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100187 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200188 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100189 } else if (!action_flag) {
190 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200191 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100192 } else if (!notif_flag) {
193 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200194 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100195 } else {
196 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100197 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100198 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100199 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100200check:
Radek Krejcia3045382018-11-22 14:30:31 +0100201 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100202 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100203 case LYS_ACTION:
204 case LYS_NOTIF:
205 case LYS_LEAF:
206 case LYS_ANYXML:
207 case LYS_ANYDATA:
208 case LYS_LIST:
209 case LYS_LEAFLIST:
210 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200211 case LYS_CASE:
212 if (options & LYS_GETNEXT_WITHCASE) {
213 break;
214 } else {
215 /* go into */
216 next = ((struct lysc_node_case *)next)->child;
217 }
218 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100219 case LYS_CONTAINER:
220 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
221 if (((struct lysc_node_container *)next)->child) {
222 /* go into */
223 next = ((struct lysc_node_container *)next)->child;
224 } else {
225 next = next->next;
226 }
227 goto repeat;
228 }
229 break;
230 case LYS_CHOICE:
231 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200232 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100233 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
234 next = next->next;
235 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100236 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100237 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200238 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100239 } else {
240 next = ((struct lysc_node_choice *)next)->cases->child;
241 }
Radek Krejcia3045382018-11-22 14:30:31 +0100242 }
243 goto repeat;
244 default:
245 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200246 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100247 return NULL;
248 }
249
250 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
251 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200252 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100253 next = next->next;
254 goto repeat;
255 }
256 }
257 return next;
258}
259
260API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100261lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200262 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100263{
264 const struct lysc_node *node = NULL;
265
266 LY_CHECK_ARG_RET(NULL, module, name, NULL);
267 if (!nodetype) {
268 nodetype = 0xffff;
269 }
270
271 while ((node = lys_getnext(node, parent, module->compiled, options))) {
272 if (!(node->nodetype & nodetype)) {
273 continue;
274 }
275 if (node->module != module) {
276 continue;
277 }
278
279 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200280 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100281 return node;
282 }
283 } else {
284 if (!strcmp(node->name, name)) {
285 return node;
286 }
287 }
288 }
289 return NULL;
290}
291
Michal Vasko519fd602020-05-26 12:17:39 +0200292API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200293lys_find_xpath_atoms(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200294{
295 LY_ERR ret = LY_SUCCESS;
296 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200297 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200298 uint32_t i;
299
300 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
301 if (!(options & LYXP_SCNODE_ALL)) {
302 options = LYXP_SCNODE;
303 }
304
305 memset(&xp_set, 0, sizeof xp_set);
306
307 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200308 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
309 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200310
311 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200312 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200313 LY_CHECK_GOTO(ret, cleanup);
314
315 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200316 ret = ly_set_new(set);
317 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200318
319 /* transform into ly_set */
320 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
321 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
322 (*set)->size = xp_set.used;
323
324 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200325 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200326 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200327 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200328 }
329 }
330
331cleanup:
332 lyxp_set_free_content(&xp_set);
333 lyxp_expr_free(ctx_node->module->ctx, exp);
334 return ret;
335}
336
Michal Vasko072de482020-08-05 13:27:21 +0200337API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200338lys_find_path_atoms(const struct ly_path *path, struct ly_set **set)
339{
340 LY_ERR ret = LY_SUCCESS;
341 LY_ARRAY_COUNT_TYPE u, v;
342
343 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
344
345 /* allocate return set */
346 LY_CHECK_RET(ly_set_new(set));
347
348 LY_ARRAY_FOR(path, u) {
349 /* add nodes from the path */
350 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
351 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
352 LY_ARRAY_FOR(path[u].predicates, v) {
353 /* add all the keys in a predicate */
354 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
355 }
356 }
357 }
358
359cleanup:
360 if (ret) {
361 ly_set_free(*set, NULL);
362 *set = NULL;
363 }
364 return ret;
365}
366
367API LY_ERR
368lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
369 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
370{
371 LY_ERR ret = LY_SUCCESS;
372 struct lyxp_set xp_set = {0};
373 uint32_t i;
374
375 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
376 if (!(options & LYXP_SCNODE_ALL)) {
377 options = LYXP_SCNODE;
378 }
379
380 /* atomize expression */
381 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
382 LY_CHECK_GOTO(ret, cleanup);
383
384 /* allocate return set */
385 ret = ly_set_new(set);
386 LY_CHECK_GOTO(ret, cleanup);
387
388 /* transform into ly_set */
389 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
390 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
391 (*set)->size = xp_set.used;
392
393 for (i = 0; i < xp_set.used; ++i) {
394 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
395 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
396 LY_CHECK_GOTO(ret, cleanup);
397 }
398 }
399
400cleanup:
401 lyxp_set_free_content(&xp_set);
402 if (ret) {
403 ly_set_free(*set, NULL);
404 *set = NULL;
405 }
406 return ret;
407}
408
409API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200410lys_find_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200411{
412 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200413 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200414 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200415 uint32_t i;
416
417 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
418 if (!(options & LYXP_SCNODE_ALL)) {
419 options = LYXP_SCNODE;
420 }
421
Michal Vasko072de482020-08-05 13:27:21 +0200422 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200423 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
424 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200425
426 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200427 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200428 LY_CHECK_GOTO(ret, cleanup);
429
430 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200431 ret = ly_set_new(set);
432 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200433
434 /* transform into ly_set */
435 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
436 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
437 (*set)->size = xp_set.used;
438
439 for (i = 0; i < xp_set.used; ++i) {
440 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200441 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200442 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200443 }
444 }
445
446cleanup:
447 lyxp_set_free_content(&xp_set);
448 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200449 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200450 ly_set_free(*set, NULL);
451 *set = NULL;
452 }
Michal Vasko072de482020-08-05 13:27:21 +0200453 return ret;
454}
455
Michal Vasko14654712020-02-06 08:35:21 +0100456char *
457lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
Radek Krejci0f969882020-08-21 16:56:47 +0200458 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200459{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200460 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200461 char *path = NULL;
462 int len = 0;
463
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200464 LY_CHECK_ARG_RET(NULL, node, NULL);
465 if (buffer) {
466 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
467 }
468
Radek Krejci327de162019-06-14 12:52:07 +0200469 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200470 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200471 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100472 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200473 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100474 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200475
Michal Vasko65de0402020-08-03 16:34:19 +0200476 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
477 /* schema-only node */
478 continue;
479 }
480
Michal Vasko11deea12020-08-05 13:54:50 +0200481 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200482 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100483 if (parent && (iter->parent == parent)) {
484 slash = "";
485 } else {
486 slash = "/";
487 }
Michal Vasko69730152020-10-09 16:30:07 +0200488 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200489 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200490 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100491 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200492 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100493 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200494 }
Radek Krejci327de162019-06-14 12:52:07 +0200495 } else {
496 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200497 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100498 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200499 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100500 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200501 }
Radek Krejci327de162019-06-14 12:52:07 +0200502 }
503 free(s);
504 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200505
Michal Vasko69730152020-10-09 16:30:07 +0200506 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200507 /* not enough space in buffer */
508 break;
509 }
Radek Krejci327de162019-06-14 12:52:07 +0200510 }
511
512 if (len < 0) {
513 free(path);
514 path = NULL;
515 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200516 if (buffer) {
517 strcpy(buffer, "/");
518 } else {
519 path = strdup("/");
520 }
Radek Krejci327de162019-06-14 12:52:07 +0200521 }
522 break;
523 }
524
Radek Krejci1c0c3442019-07-23 16:08:47 +0200525 if (buffer) {
526 return buffer;
527 } else {
528 return path;
529 }
Radek Krejci327de162019-06-14 12:52:07 +0200530}
531
Michal Vasko14654712020-02-06 08:35:21 +0100532API char *
533lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
534{
535 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
536}
537
Michal Vasko28d78432020-05-26 13:10:53 +0200538API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100539lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200540{
Michal Vasko28d78432020-05-26 13:10:53 +0200541 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
542 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200543}
544
Radek Krejci693262f2019-04-29 15:23:20 +0200545uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200546lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200547{
548 uint8_t *item;
549 uint8_t mask = 3, result;
550
Radek Krejci151a5b72018-10-19 14:21:44 +0200551 item = &list[pos / 4];
552 result = (*item) & (mask << 2 * (pos % 4));
553 return result >> 2 * (pos % 4);
554}
555
Michal Vasko28d78432020-05-26 13:10:53 +0200556static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200557lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200558{
559 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200560 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200561
Radek Krejci693262f2019-04-29 15:23:20 +0200562 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200563 (*index_e)++;
564
565 switch (op) {
566 case LYS_IFF_F:
567 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200568 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200569 case LYS_IFF_NOT:
570 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200571 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200572 case LYS_IFF_AND:
573 case LYS_IFF_OR:
574 a = lysc_iffeature_value_(iff, index_e, index_f);
575 b = lysc_iffeature_value_(iff, index_e, index_f);
576 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200577 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
578 return LY_SUCCESS;
579 } else {
580 return LY_ENOT;
581 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200582 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200583 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
584 return LY_SUCCESS;
585 } else {
586 return LY_ENOT;
587 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200588 }
589 }
590
591 return 0;
592}
593
Michal Vasko28d78432020-05-26 13:10:53 +0200594API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200595lysc_iffeature_value(const struct lysc_iffeature *iff)
596{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200597 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200598
599 LY_CHECK_ARG_RET(NULL, iff, -1);
600
601 if (iff->expr) {
602 return lysc_iffeature_value_(iff, &index_e, &index_f);
603 }
604 return 0;
605}
606
Radek Krejci151a5b72018-10-19 14:21:44 +0200607/**
608 * @brief Enable/Disable the specified feature in the module.
609 *
610 * If the feature is already set to the desired value, LY_SUCCESS is returned.
611 * By changing the feature, also all the feature which depends on it via their
Radek Krejcia8c71e62020-10-26 08:30:50 +0100612 * if-feature statements are again evaluated (disabled if a if-feature statement
Radek Krejci151a5b72018-10-19 14:21:44 +0200613 * evaluates to false).
614 *
Radek Krejci0af46292019-01-11 16:02:31 +0100615 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200616 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
617 * set all the features in the module.
618 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
Radek Krejci857189e2020-09-01 13:26:36 +0200619 * @param[in] skip_checks Flag to skip checking of if-features and just set @p value of the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200620 * @return LY_ERR value.
621 */
622static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200623lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200624{
Michal Vaskob0099a92020-08-31 14:55:23 +0200625 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200626 ly_bool all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200627 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200628 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200629 struct lysc_feature *f, **df;
630 struct lysc_iffeature *iff;
631 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100632 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200633
Radek Krejci6e67c402019-05-02 09:55:39 +0200634 if (!strcmp(name, "*")) {
635 /* enable all */
636 all = 1;
637 }
638
Michal Vasko997f9822020-10-14 13:09:05 +0200639 if (!mod->implemented) {
640 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled.", mod->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100641 return LY_EINVAL;
642 }
Radek Krejci14915cc2020-09-14 17:28:13 +0200643 if (!mod->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200644 if (all) {
645 /* no feature to enable */
646 return LY_SUCCESS;
647 }
Radek Krejci0af46292019-01-11 16:02:31 +0100648 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200649 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200650 }
651
Radek Krejciba03a5a2020-08-27 14:40:41 +0200652 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100653 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200654
Radek Krejcica3db002018-11-01 10:31:01 +0100655run:
Radek Krejci14915cc2020-09-14 17:28:13 +0200656 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) {
657 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200658 if (all || !strcmp(f->name, name)) {
659 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
660 if (all) {
661 /* skip already set features */
662 continue;
663 } else {
664 /* feature already set correctly */
Michal Vaskob0099a92020-08-31 14:55:23 +0200665 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200666 }
667 }
668
669 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200670 if (!skip_checks) {
671 /* check referenced features if they are enabled */
672 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
673 if (lysc_iffeature_value(iff) == LY_ENOT) {
674 if (all) {
675 ++disabled_count;
676 goto next;
677 } else {
Michal Vasko997f9822020-10-14 13:09:05 +0200678 LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by "
679 "its if-feature condition(s).", f->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200680 ret = LY_EDENIED;
681 goto cleanup;
Michal Vasko82c31e62020-07-17 15:30:40 +0200682 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200683 }
684 }
685 }
686 /* enable the feature */
687 f->flags |= LYS_FENABLED;
688 } else { /* disable */
689 /* disable the feature */
690 f->flags &= ~LYS_FENABLED;
691 }
692
693 /* remember the changed feature */
Radek Krejci3d92e442020-10-12 12:48:13 +0200694 ret = ly_set_add(changed, f, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200695 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200696
697 if (!all) {
698 /* stop in case changing a single feature */
699 break;
700 }
701 }
702next:
703 ;
704 }
705
706 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100707 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200708 ret = LY_ENOTFOUND;
709 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200710 }
711
Radek Krejcica3db002018-11-01 10:31:01 +0100712 if (value && all && disabled_count) {
713 if (changed_count == changed->count) {
714 /* no change in last run -> not able to enable all ... */
715 /* ... print errors */
Radek Krejci14915cc2020-09-14 17:28:13 +0200716 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) {
717 if (!(mod->features[u].flags & LYS_FENABLED)) {
Michal Vasko997f9822020-10-14 13:09:05 +0200718 LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by its if-feature "
719 "condition(s).", mod->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100720 --disabled_count;
721 }
722 }
723 /* ... restore the original state */
724 for (u = 0; u < changed->count; ++u) {
725 f = changed->objs[u];
726 /* re-disable the feature */
727 f->flags &= ~LYS_FENABLED;
728 }
729
Michal Vaskob0099a92020-08-31 14:55:23 +0200730 ret = LY_EDENIED;
731 goto cleanup;
Radek Krejcica3db002018-11-01 10:31:01 +0100732 } else {
733 /* we did some change in last run, try it again */
734 changed_count = changed->count;
735 goto run;
736 }
737 }
738
Radek Krejci151a5b72018-10-19 14:21:44 +0200739 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200740 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200741 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
742 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
743 * is not done - by default, features are disabled and must be explicitely enabled. */
744 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200745 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200746 if (!((*df)->flags & LYS_FENABLED)) {
747 /* not enabled, nothing to do */
748 continue;
749 }
750 /* check the feature's if-features which could change by the previous change of our feature */
751 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200752 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200753 /* the feature must be disabled now */
754 (*df)->flags &= ~LYS_FENABLED;
755 /* add the feature into the list of changed features */
Radek Krejci3d92e442020-10-12 12:48:13 +0200756 ret = ly_set_add(changed, *df, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200757 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200758 break;
759 }
760 }
761 }
762 }
763
Michal Vasko87a97f42020-10-06 10:30:28 +0200764 /* success */
765 ++mod->ctx->module_set_id;
766
Michal Vaskob0099a92020-08-31 14:55:23 +0200767cleanup:
Radek Krejci151a5b72018-10-19 14:21:44 +0200768 ly_set_free(changed, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200769 return ret;
Radek Krejci151a5b72018-10-19 14:21:44 +0200770}
771
772API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200773lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200774{
Radek Krejci0af46292019-01-11 16:02:31 +0100775 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200776
Michal Vasko22df3f02020-08-24 13:29:22 +0200777 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200778}
779
780API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200781lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200782{
Radek Krejci0af46292019-01-11 16:02:31 +0100783 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200784
Michal Vasko22df3f02020-08-24 13:29:22 +0200785 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200786}
787
Michal Vasko82c31e62020-07-17 15:30:40 +0200788API LY_ERR
789lys_feature_enable_force(const struct lys_module *module, const char *feature)
790{
791 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
792
Michal Vasko22df3f02020-08-24 13:29:22 +0200793 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200794}
795
796API LY_ERR
797lys_feature_disable_force(const struct lys_module *module, const char *feature)
798{
799 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
800
Michal Vasko22df3f02020-08-24 13:29:22 +0200801 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200802}
803
804API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200805lys_feature_value(const struct lys_module *module, const char *feature)
806{
Michal Vasko82c31e62020-07-17 15:30:40 +0200807 struct lysc_feature *f = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200808 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200809
810 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200811
812 /* search for the specified feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200813 LY_ARRAY_FOR(module->features, u) {
814 f = &module->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200815 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200816 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200817 }
818 }
819
820 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200821 if (!f) {
822 return LY_ENOTFOUND;
823 }
824
825 /* feature disabled */
826 if (!(f->flags & LYS_FENABLED)) {
827 return LY_ENOT;
828 }
829
830 /* check referenced features if they are enabled */
831 LY_ARRAY_FOR(f->iffeatures, u) {
832 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
833 /* if-feature disabled */
834 return LY_ENOT;
835 }
836 }
837
838 /* feature enabled */
839 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200840}
841
Michal Vaskoc193ce92020-03-06 11:04:48 +0100842API const struct lysc_node *
Radek Krejci857189e2020-09-01 13:26:36 +0200843lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100844{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200845 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100846
847 LY_CHECK_ARG_RET(NULL, node, NULL);
848
Michal Vaskoc193ce92020-03-06 11:04:48 +0100849 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100850 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100851 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100852 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200853 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100854 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100855 }
856 }
857 }
858
859 if (!recursive) {
860 return NULL;
861 }
862
Michal Vaskoc193ce92020-03-06 11:04:48 +0100863 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100864 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100865 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
866
Radek Krejcia3045382018-11-22 14:30:31 +0100867 return NULL;
868}
869
Radek Krejci19cf8052020-08-18 15:02:38 +0200870API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200871lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200872{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200873 struct lysc_action *act;
874 struct lysc_notif *notif;
875
Radek Krejci19cf8052020-08-18 15:02:38 +0200876 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
877
Radek Krejciaf9cd802020-10-06 21:59:47 +0200878 switch (node->nodetype) {
879 case LYS_CONTAINER:
880 case LYS_CHOICE:
881 case LYS_CASE:
882 case LYS_LEAF:
883 case LYS_LEAFLIST:
884 case LYS_LIST:
885 case LYS_ANYXML:
886 case LYS_ANYDATA:
887 if (prev_priv_p) {
888 *prev_priv_p = node->priv;
889 }
890 ((struct lysc_node *)node)->priv = priv;
891 break;
892 case LYS_RPC:
893 case LYS_ACTION:
894 act = (struct lysc_action *)node;
895 if (prev_priv_p) {
896 *prev_priv_p = act->priv;
897 }
898 act->priv = priv;
899 break;
900 case LYS_NOTIF:
901 notif = (struct lysc_notif *)node;
902 if (prev_priv_p) {
903 *prev_priv_p = notif->priv;
904 }
905 notif->priv = priv;
906 break;
907 default:
908 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200909 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200910
911 return LY_SUCCESS;
912}
913
Michal Vasko89b5c072020-10-06 13:52:44 +0200914API LY_ERR
915lys_set_implemented(struct lys_module *mod)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200916{
Michal Vasko89b5c072020-10-06 13:52:44 +0200917 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200918 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200919 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200920
921 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
922
923 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200924 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200925 return LY_SUCCESS;
926 }
927
928 /* we have module from the current context */
929 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
930 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200931 assert(m != mod);
932
933 /* check collision with other implemented revision */
934 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
935 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
936 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200937 }
938
Michal Vasko89b5c072020-10-06 13:52:44 +0200939 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200940 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200941
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200942 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200943 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200944
945 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200946 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200947
Michal Vasko89b5c072020-10-06 13:52:44 +0200948 if (mod == mod->ctx->implementing.objs[0]) {
949 /* the first module being implemented, consolidate the set */
950 if (ret) {
951 /* failure, full compile revert */
952 for (i = 0; i < mod->ctx->list.count; ++i) {
953 m = mod->ctx->list.objs[i];
954 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
955 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200956
Michal Vasko89b5c072020-10-06 13:52:44 +0200957 /* make the module correctly non-implemented again */
958 m->implemented = 0;
959 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
960 lys_precompile_augments_deviations_revert(mod->ctx, m);
961 }
962
963 /* free the compiled version of the module, if any */
964 lysc_module_free(m->compiled, NULL);
965 m->compiled = NULL;
966
967 if (m->implemented) {
968 /* recompile, must succeed because it was already compiled; hide messages because any
969 * warnings were already printed, are not really relevant, and would hide the real error */
970 uint32_t prev_lo = ly_log_options(0);
971 r = lys_compile(m, 0);
972 ly_log_options(prev_lo);
973 if (r) {
974 LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name);
975 }
976 }
977 }
978 }
979
980 ly_set_erase(&mod->ctx->implementing, NULL);
981 }
982 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200983}
984
Michal Vasko7c8439f2020-08-05 13:25:19 +0200985static LY_ERR
986lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
987{
988 struct lysp_import *imp;
989 struct lysp_include *inc;
990 LY_ARRAY_COUNT_TYPE u, v;
991
992 modp->parsing = 1;
993 LY_ARRAY_FOR(modp->imports, u) {
994 imp = &modp->imports[u];
995 if (!imp->module) {
996 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
997 }
998 /* check for importing the same module twice */
999 for (v = 0; v < u; ++v) {
1000 if (imp->module == modp->imports[v].module) {
1001 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
1002 }
1003 }
1004 }
1005 LY_ARRAY_FOR(modp->includes, u) {
1006 inc = &modp->includes[u];
1007 if (!inc->submodule) {
1008 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
1009 }
1010 }
1011 modp->parsing = 0;
1012
1013 return LY_SUCCESS;
1014}
1015
Michal Vasko3a41dff2020-07-15 14:30:28 +02001016LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001017lys_parse_submodule(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vasko22df3f02020-08-24 13:29:22 +02001018 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +02001019 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001020{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +01001023 struct lys_yang_parser_ctx *yangctx = NULL;
1024 struct lys_yin_parser_ctx *yinctx = NULL;
1025 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001026
Michal Vasko3a41dff2020-07-15 14:30:28 +02001027 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001028
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001029 switch (format) {
1030 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001031 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001032 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001033 break;
1034 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001035 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001036 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001037 break;
1038 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +02001039 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +02001040 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001041 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001042 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001043 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +02001044 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001045
1046 /* make sure that the newest revision is at position 0 */
1047 lysp_sort_revisions(submod->revs);
1048
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001049 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +02001050 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001051 if (latest_sp) {
1052 if (submod->revs) {
1053 if (!latest_sp->revs) {
1054 /* latest has no revision, so mod is anyway newer */
1055 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +02001056 /* the latest_sp is zeroed later when the new module is being inserted into the context */
1057 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
1058 submod->latest_revision = latest_sp->latest_revision;
1059 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001060 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +02001061 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 }
Radek Krejcib3289d62019-09-18 12:21:39 +02001063 } else {
1064 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001065 }
1066 } else {
1067 submod->latest_revision = 1;
1068 }
1069
Radek Krejcib3289d62019-09-18 12:21:39 +02001070 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001071 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +02001072 }
1073
1074 if (latest_sp) {
1075 latest_sp->latest_revision = 0;
1076 }
1077
Michal Vasko7a0b0762020-09-02 16:37:01 +02001078 lys_parser_fill_filepath(ctx, in, &submod->filepath);
1079
Michal Vasko7c8439f2020-08-05 13:25:19 +02001080 /* resolve imports and includes */
1081 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
1082
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001083 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +01001084 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
1085 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001086
David Sedlák1b623122019-08-05 15:27:49 +02001087 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001088 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001089 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001090 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001091 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001092 *submodule = submod;
1093 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +02001094
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001095error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001096 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +02001097 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001098 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001099 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001100 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001101 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001102 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001103}
1104
Michal Vasko3a41dff2020-07-15 14:30:28 +02001105LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001106lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001107 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
1108 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001109{
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001110 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001111 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001112 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001113 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001114 struct lys_yang_parser_ctx *yangctx = NULL;
1115 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001116 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001117 char *filename, *rev, *dot;
1118 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +02001119
Michal Vasko3a41dff2020-07-15 14:30:28 +02001120 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001121 if (module) {
1122 *module = NULL;
1123 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001124
1125 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001126 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001128
1129 switch (format) {
1130 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001131 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001132 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001133 break;
1134 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001135 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001136 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001137 break;
1138 default:
1139 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001140 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001141 break;
1142 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001143 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001144
1145 /* make sure that the newest revision is at position 0 */
1146 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001147 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001148 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001149 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001150
Radek Krejcib3289d62019-09-18 12:21:39 +02001151 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001152 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001153 if (latest) {
1154 if (mod->revision) {
1155 if (!latest->revision) {
1156 /* latest has no revision, so mod is anyway newer */
1157 mod->latest_revision = latest->latest_revision;
1158 /* the latest is zeroed later when the new module is being inserted into the context */
1159 } else if (strcmp(mod->revision, latest->revision) > 0) {
1160 mod->latest_revision = latest->latest_revision;
1161 /* the latest is zeroed later when the new module is being inserted into the context */
1162 } else {
1163 latest = NULL;
1164 }
1165 } else {
1166 latest = NULL;
1167 }
1168 } else {
1169 mod->latest_revision = 1;
1170 }
1171
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001172 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001173 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001174 }
1175
Radek Krejci86d106e2018-10-18 09:53:19 +02001176 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001177 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001178 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1179 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001180 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001181 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001182 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001183 }
1184
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001185 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001186 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001187 if (mod_dup) {
1188 if (mod_dup->parsed) {
1189 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001190 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001191 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001192 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001193 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001194 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001195 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001196 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001197 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001198 goto error;
1199 } else {
1200 /* add the parsed data to the currently compiled-only module in the context */
1201 mod_dup->parsed = mod->parsed;
1202 mod_dup->parsed->mod = mod_dup;
1203 mod->parsed = NULL;
1204 lys_module_free(mod, NULL);
1205 mod = mod_dup;
1206 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001207 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001208 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001209
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001210 switch (in->type) {
1211 case LY_IN_FILEPATH:
1212 /* check that name and revision match filename */
1213 filename = strrchr(in->method.fpath.filepath, '/');
1214 if (!filename) {
1215 filename = in->method.fpath.filepath;
1216 } else {
1217 filename++;
1218 }
1219 rev = strchr(filename, '@');
1220 dot = strrchr(filename, '.');
1221
1222 /* name */
1223 len = strlen(mod->name);
1224 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001225 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001226 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1227 }
1228 if (rev) {
1229 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001230 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001231 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001232 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001233 }
1234 }
1235
1236 break;
1237 case LY_IN_FD:
1238 case LY_IN_FILE:
1239 case LY_IN_MEMORY:
1240 /* nothing special to do */
1241 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001242 case LY_IN_ERROR:
1243 LOGINT(ctx);
1244 ret = LY_EINT;
1245 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001246 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001247
1248 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001249
Michal Vasko89b5c072020-10-06 13:52:44 +02001250 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001251 /* pre-compile features and identities of the module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001252 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod->parsed, mod->parsed->features, &mod->features), error);
1253 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001254 }
1255
1256 if (latest) {
1257 latest->latest_revision = 0;
1258 }
1259
1260 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001261 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001262 LY_CHECK_GOTO(ret, error);
1263 ctx->module_set_id++;
1264
1265finish_parsing:
1266 /* resolve imports and includes */
1267 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1268
Michal Vasko89b5c072020-10-06 13:52:44 +02001269 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001270 /* pre-compile features and identities of any submodules */
1271 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001272 submod = mod->parsed->includes[u].submodule;
1273 ret = lys_feature_precompile(NULL, ctx, (struct lysp_module *)submod, submod->features, &mod->features);
1274 LY_CHECK_GOTO(ret, error);
1275 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1276 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001277 }
1278 }
1279
1280 /* check name collisions - typedefs and TODO groupings */
1281 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1282
Michal Vasko89b5c072020-10-06 13:52:44 +02001283 if (implement) {
1284 /* implement (compile) */
1285 LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001286 }
1287
1288 if (format == LYS_IN_YANG) {
1289 yang_parser_ctx_free(yangctx);
1290 } else {
1291 yin_parser_ctx_free(yinctx);
1292 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001293 if (module) {
1294 *module = mod;
1295 }
1296 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001297
1298error_ctx:
1299 ly_set_rm(&ctx->list, mod, NULL);
1300error:
1301 lys_module_free(mod, NULL);
1302 if (pctx) {
1303 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1304 }
1305 if (format == LYS_IN_YANG) {
1306 yang_parser_ctx_free(yangctx);
1307 } else {
1308 yin_parser_ctx_free(yinctx);
1309 }
1310
1311 return ret;
1312}
1313
1314API LY_ERR
1315lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1316{
1317 if (module) {
1318 *module = NULL;
1319 }
1320 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1321
1322 /* remember input position */
1323 in->func_start = in->current;
1324
1325 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001326}
1327
Michal Vasko3a41dff2020-07-15 14:30:28 +02001328API LY_ERR
1329lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001330{
Radek Krejci0f969882020-08-21 16:56:47 +02001331 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001332 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001333
Michal Vasko3a41dff2020-07-15 14:30:28 +02001334 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001335
Michal Vasko3a41dff2020-07-15 14:30:28 +02001336 LY_CHECK_ERR_RET(ret = ly_in_new_memory(data, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret);
Radek Krejci86d106e2018-10-18 09:53:19 +02001337
Michal Vasko3a41dff2020-07-15 14:30:28 +02001338 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001339 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001340
Michal Vasko3a41dff2020-07-15 14:30:28 +02001341 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001342}
1343
Michal Vasko3a41dff2020-07-15 14:30:28 +02001344API LY_ERR
1345lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001346{
Radek Krejci0f969882020-08-21 16:56:47 +02001347 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001348 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001349
Michal Vasko3a41dff2020-07-15 14:30:28 +02001350 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001351
Michal Vasko3a41dff2020-07-15 14:30:28 +02001352 LY_CHECK_ERR_RET(ret = ly_in_new_fd(fd, &in), LOGERR(ctx, ret, "Unable to create input handler."), ret);
Radek Krejci86d106e2018-10-18 09:53:19 +02001353
Michal Vasko3a41dff2020-07-15 14:30:28 +02001354 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001355 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001356
Michal Vasko3a41dff2020-07-15 14:30:28 +02001357 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001358}
1359
Michal Vasko3a41dff2020-07-15 14:30:28 +02001360API LY_ERR
1361lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001362{
Radek Krejci0f969882020-08-21 16:56:47 +02001363 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001364 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001365
Michal Vasko3a41dff2020-07-15 14:30:28 +02001366 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001367
Michal Vasko3a41dff2020-07-15 14:30:28 +02001368 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001369 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001370
Michal Vasko3a41dff2020-07-15 14:30:28 +02001371 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001372 ly_in_free(in, 0);
1373
Michal Vasko3a41dff2020-07-15 14:30:28 +02001374 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001375}
1376
1377API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001378lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001379 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001380{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001381 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001382 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001383 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001384 char *wd, *wn = NULL;
1385 DIR *dir = NULL;
1386 struct dirent *file;
1387 char *match_name = NULL;
1388 LYS_INFORMAT format_aux, match_format = 0;
1389 struct ly_set *dirs;
1390 struct stat st;
1391
1392 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1393
1394 /* start to fill the dir fifo with the context's search path (if set)
1395 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001396 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001397
1398 len = strlen(name);
1399 if (cwd) {
1400 wd = get_current_dir_name();
1401 if (!wd) {
1402 LOGMEM(NULL);
1403 goto cleanup;
1404 } else {
1405 /* add implicit current working directory (./) to be searched,
1406 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001407 ret = ly_set_add(dirs, wd, 0, NULL);
1408 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001409 implicit_cwd = 1;
1410 }
1411 }
1412 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001413 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001414 /* check for duplicities with the implicit current working directory */
1415 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1416 implicit_cwd = 0;
1417 continue;
1418 }
1419 wd = strdup(searchpaths[i]);
1420 if (!wd) {
1421 LOGMEM(NULL);
1422 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001423 } else {
1424 ret = ly_set_add(dirs, wd, 0, NULL);
1425 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001426 }
1427 }
1428 }
1429 wd = NULL;
1430
1431 /* start searching */
1432 while (dirs->count) {
1433 free(wd);
1434 free(wn); wn = NULL;
1435
1436 dirs->count--;
1437 wd = (char *)dirs->objs[dirs->count];
1438 dirs->objs[dirs->count] = NULL;
1439 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1440
1441 if (dir) {
1442 closedir(dir);
1443 }
1444 dir = opendir(wd);
1445 dir_len = strlen(wd);
1446 if (!dir) {
1447 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1448 } else {
1449 while ((file = readdir(dir))) {
1450 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1451 /* skip . and .. */
1452 continue;
1453 }
1454 free(wn);
1455 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1456 LOGMEM(NULL);
1457 goto cleanup;
1458 }
1459 if (stat(wn, &st) == -1) {
1460 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001461 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001462 continue;
1463 }
1464 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1465 /* we have another subdirectory in searchpath to explore,
1466 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001467 ret = ly_set_add(dirs, wn, 0, NULL);
1468 LY_CHECK_GOTO(ret, cleanup);
1469
Radek Krejcid33273d2018-10-25 14:55:52 +02001470 /* continue with the next item in current directory */
1471 wn = NULL;
1472 continue;
1473 } else if (!S_ISREG(st.st_mode)) {
1474 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1475 continue;
1476 }
1477
1478 /* here we know that the item is a file which can contain a module */
1479 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001480 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001481 /* different filename than the module we search for */
1482 continue;
1483 }
1484
1485 /* get type according to filename suffix */
1486 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001487 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001488 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001489 /* TODO YIN parser
1490 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1491 format_aux = LYS_IN_YIN;
1492 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001493 } else {
1494 /* not supportde suffix/file format */
1495 continue;
1496 }
1497
1498 if (revision) {
1499 /* we look for the specific revision, try to get it from the filename */
1500 if (file->d_name[len] == '@') {
1501 /* check revision from the filename */
1502 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1503 /* another revision */
1504 continue;
1505 } else {
1506 /* exact revision */
1507 free(match_name);
1508 match_name = wn;
1509 wn = NULL;
1510 match_len = dir_len + 1 + len;
1511 match_format = format_aux;
1512 goto success;
1513 }
1514 } else {
1515 /* continue trying to find exact revision match, use this only if not found */
1516 free(match_name);
1517 match_name = wn;
1518 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001519 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001520 match_format = format_aux;
1521 continue;
1522 }
1523 } else {
1524 /* remember the revision and try to find the newest one */
1525 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001526 if ((file->d_name[len] != '@') ||
1527 lysp_check_date(NULL, &file->d_name[len + 1], flen - ((format_aux == LYS_IN_YANG) ? 5 : 4) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001528 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001529 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001530 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1531 continue;
1532 }
1533 free(match_name);
1534 }
1535
1536 match_name = wn;
1537 wn = NULL;
1538 match_len = dir_len + 1 + len;
1539 match_format = format_aux;
1540 continue;
1541 }
1542 }
1543 }
1544 }
1545
1546success:
1547 (*localfile) = match_name;
1548 match_name = NULL;
1549 if (format) {
1550 (*format) = match_format;
1551 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001552 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001553
1554cleanup:
1555 free(wn);
1556 free(wd);
1557 if (dir) {
1558 closedir(dir);
1559 }
1560 free(match_name);
1561 ly_set_free(dirs, free);
1562
1563 return ret;
1564}