blob: 5f033f383995e4fd5db864c2bf14351e005d59b6 [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
Radek Krejcia3045382018-11-22 14:30:31 +010045API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +020046lys_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 +010047{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010048 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010049 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +020050 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010051 const struct lysc_action *actions;
52 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020053 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +010054
55 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
56
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020057next:
Radek Krejcia3045382018-11-22 14:30:31 +010058 if (!last) {
59 /* first call */
60
61 /* get know where to start */
62 if (parent) {
63 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +020064 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020065 if (((struct lysc_node_choice *)parent)->cases) {
66 next = last = (const struct lysc_node *)&((struct lysc_node_choice *)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010067 }
Radek Krejci056d0a82018-12-06 16:57:25 +010068 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010069 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010070 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020071 if (snode && *snode) {
72 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010073 }
Radek Krejcia3045382018-11-22 14:30:31 +010074 }
Radek Krejcia3045382018-11-22 14:30:31 +010075 } else {
76 /* top level data */
77 next = last = module->data;
78 }
79 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010080 /* try to get action or notification */
81 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010082 }
Radek Krejci05b774b2019-02-25 13:26:18 +010083 /* test if the next can be returned */
84 goto check;
85
Michal Vasko1bf09392020-03-27 12:38:10 +010086 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010087 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010088 if (last->parent) {
89 actions = lysc_node_actions(last->parent);
90 } else {
91 actions = module->rpcs;
92 }
93 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +020094 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010095 break;
96 }
97 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +020098 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020099 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100100 }
101 goto repeat;
102 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100103 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100104 if (last->parent) {
105 notifs = lysc_node_notifs(last->parent);
106 } else {
107 notifs = module->notifs;
108 }
109 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200110 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100111 break;
112 }
113 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200114 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200115 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100116 }
117 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200118 } else {
119 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100120 }
121
Radek Krejcia3045382018-11-22 14:30:31 +0100122repeat:
123 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100124 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200125 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;
Michal Vasko22df3f02020-08-24 13:29:22 +0200130 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100131 } else if (!notif_flag) {
132 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200133 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100134 } 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:
149 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200150 case LYS_CASE:
151 if (options & LYS_GETNEXT_WITHCASE) {
152 break;
153 } else {
154 /* go into */
155 next = ((struct lysc_node_case *)next)->child;
156 }
157 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100158 case LYS_CONTAINER:
159 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
160 if (((struct lysc_node_container *)next)->child) {
161 /* go into */
162 next = ((struct lysc_node_container *)next)->child;
163 } else {
164 next = next->next;
165 }
166 goto repeat;
167 }
168 break;
169 case LYS_CHOICE:
170 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200171 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100172 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
173 next = next->next;
174 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100175 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100176 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200177 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100178 } else {
179 next = ((struct lysc_node_choice *)next)->cases->child;
180 }
Radek Krejcia3045382018-11-22 14:30:31 +0100181 }
182 goto repeat;
183 default:
184 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200185 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100186 return NULL;
187 }
188
189 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
190 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200191 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100192 next = next->next;
193 goto repeat;
194 }
195 }
196 return next;
197}
198
199API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100200lys_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 +0200201 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100202{
203 const struct lysc_node *node = NULL;
204
205 LY_CHECK_ARG_RET(NULL, module, name, NULL);
206 if (!nodetype) {
207 nodetype = 0xffff;
208 }
209
210 while ((node = lys_getnext(node, parent, module->compiled, options))) {
211 if (!(node->nodetype & nodetype)) {
212 continue;
213 }
214 if (node->module != module) {
215 continue;
216 }
217
218 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200219 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100220 return node;
221 }
222 } else {
223 if (!strcmp(node->name, name)) {
224 return node;
225 }
226 }
227 }
228 return NULL;
229}
230
Michal Vasko519fd602020-05-26 12:17:39 +0200231API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200232lys_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 +0200233{
234 LY_ERR ret = LY_SUCCESS;
235 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200236 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200237 uint32_t i;
238
239 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
240 if (!(options & LYXP_SCNODE_ALL)) {
241 options = LYXP_SCNODE;
242 }
243
244 memset(&xp_set, 0, sizeof xp_set);
245
246 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200247 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
248 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200249
250 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200251 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200252 LY_CHECK_GOTO(ret, cleanup);
253
254 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200255 ret = ly_set_new(set);
256 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200257
258 /* transform into ly_set */
259 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
260 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
261 (*set)->size = xp_set.used;
262
263 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200264 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200265 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200266 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200267 }
268 }
269
270cleanup:
271 lyxp_set_free_content(&xp_set);
272 lyxp_expr_free(ctx_node->module->ctx, exp);
273 return ret;
274}
275
Michal Vasko072de482020-08-05 13:27:21 +0200276API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200277lys_find_path_atoms(const struct ly_path *path, struct ly_set **set)
278{
279 LY_ERR ret = LY_SUCCESS;
280 LY_ARRAY_COUNT_TYPE u, v;
281
282 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
283
284 /* allocate return set */
285 LY_CHECK_RET(ly_set_new(set));
286
287 LY_ARRAY_FOR(path, u) {
288 /* add nodes from the path */
289 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
290 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
291 LY_ARRAY_FOR(path[u].predicates, v) {
292 /* add all the keys in a predicate */
293 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
294 }
295 }
296 }
297
298cleanup:
299 if (ret) {
300 ly_set_free(*set, NULL);
301 *set = NULL;
302 }
303 return ret;
304}
305
306API LY_ERR
307lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
308 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
309{
310 LY_ERR ret = LY_SUCCESS;
311 struct lyxp_set xp_set = {0};
312 uint32_t i;
313
314 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
315 if (!(options & LYXP_SCNODE_ALL)) {
316 options = LYXP_SCNODE;
317 }
318
319 /* atomize expression */
320 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
321 LY_CHECK_GOTO(ret, cleanup);
322
323 /* allocate return set */
324 ret = ly_set_new(set);
325 LY_CHECK_GOTO(ret, cleanup);
326
327 /* transform into ly_set */
328 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
329 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
330 (*set)->size = xp_set.used;
331
332 for (i = 0; i < xp_set.used; ++i) {
333 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
334 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
335 LY_CHECK_GOTO(ret, cleanup);
336 }
337 }
338
339cleanup:
340 lyxp_set_free_content(&xp_set);
341 if (ret) {
342 ly_set_free(*set, NULL);
343 *set = NULL;
344 }
345 return ret;
346}
347
348API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200349lys_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 +0200350{
351 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200352 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200353 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200354 uint32_t i;
355
356 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
357 if (!(options & LYXP_SCNODE_ALL)) {
358 options = LYXP_SCNODE;
359 }
360
Michal Vasko072de482020-08-05 13:27:21 +0200361 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200362 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
363 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200364
365 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200366 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200367 LY_CHECK_GOTO(ret, cleanup);
368
369 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200370 ret = ly_set_new(set);
371 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200372
373 /* transform into ly_set */
374 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
375 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
376 (*set)->size = xp_set.used;
377
378 for (i = 0; i < xp_set.used; ++i) {
379 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 +0200380 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200381 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200382 }
383 }
384
385cleanup:
386 lyxp_set_free_content(&xp_set);
387 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vasko40308e72020-10-20 16:38:40 +0200388 if (ret) {
389 ly_set_free(*set, NULL);
390 *set = NULL;
391 }
Michal Vasko072de482020-08-05 13:27:21 +0200392 return ret;
393}
394
Michal Vasko14654712020-02-06 08:35:21 +0100395char *
396lysc_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 +0200397 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200398{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200399 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200400 char *path = NULL;
401 int len = 0;
402
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200403 LY_CHECK_ARG_RET(NULL, node, NULL);
404 if (buffer) {
405 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
406 }
407
Radek Krejci327de162019-06-14 12:52:07 +0200408 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200409 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200410 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100411 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200412 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100413 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200414
Michal Vasko65de0402020-08-03 16:34:19 +0200415 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
416 /* schema-only node */
417 continue;
418 }
419
Michal Vasko11deea12020-08-05 13:54:50 +0200420 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100422 if (parent && (iter->parent == parent)) {
423 slash = "";
424 } else {
425 slash = "/";
426 }
Michal Vasko69730152020-10-09 16:30:07 +0200427 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200428 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200429 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100430 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200431 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100432 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200433 }
Radek Krejci327de162019-06-14 12:52:07 +0200434 } else {
435 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200436 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100437 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200438 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100439 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200440 }
Radek Krejci327de162019-06-14 12:52:07 +0200441 }
442 free(s);
443 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200444
Michal Vasko69730152020-10-09 16:30:07 +0200445 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200446 /* not enough space in buffer */
447 break;
448 }
Radek Krejci327de162019-06-14 12:52:07 +0200449 }
450
451 if (len < 0) {
452 free(path);
453 path = NULL;
454 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200455 if (buffer) {
456 strcpy(buffer, "/");
457 } else {
458 path = strdup("/");
459 }
Radek Krejci327de162019-06-14 12:52:07 +0200460 }
461 break;
462 }
463
Radek Krejci1c0c3442019-07-23 16:08:47 +0200464 if (buffer) {
465 return buffer;
466 } else {
467 return path;
468 }
Radek Krejci327de162019-06-14 12:52:07 +0200469}
470
Michal Vasko14654712020-02-06 08:35:21 +0100471API char *
472lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
473{
474 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
475}
476
Michal Vasko28d78432020-05-26 13:10:53 +0200477API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100478lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200479{
Michal Vasko28d78432020-05-26 13:10:53 +0200480 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
481 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200482}
483
Radek Krejci693262f2019-04-29 15:23:20 +0200484uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200485lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200486{
487 uint8_t *item;
488 uint8_t mask = 3, result;
489
Radek Krejci151a5b72018-10-19 14:21:44 +0200490 item = &list[pos / 4];
491 result = (*item) & (mask << 2 * (pos % 4));
492 return result >> 2 * (pos % 4);
493}
494
Michal Vasko28d78432020-05-26 13:10:53 +0200495static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200496lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200497{
498 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200499 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200500
Radek Krejci693262f2019-04-29 15:23:20 +0200501 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200502 (*index_e)++;
503
504 switch (op) {
505 case LYS_IFF_F:
506 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200507 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200508 case LYS_IFF_NOT:
509 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200510 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200511 case LYS_IFF_AND:
512 case LYS_IFF_OR:
513 a = lysc_iffeature_value_(iff, index_e, index_f);
514 b = lysc_iffeature_value_(iff, index_e, index_f);
515 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200516 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
517 return LY_SUCCESS;
518 } else {
519 return LY_ENOT;
520 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200521 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200522 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
523 return LY_SUCCESS;
524 } else {
525 return LY_ENOT;
526 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200527 }
528 }
529
530 return 0;
531}
532
Michal Vasko28d78432020-05-26 13:10:53 +0200533API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200534lysc_iffeature_value(const struct lysc_iffeature *iff)
535{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200536 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200537
538 LY_CHECK_ARG_RET(NULL, iff, -1);
539
540 if (iff->expr) {
541 return lysc_iffeature_value_(iff, &index_e, &index_f);
542 }
543 return 0;
544}
545
Radek Krejci151a5b72018-10-19 14:21:44 +0200546/**
547 * @brief Enable/Disable the specified feature in the module.
548 *
549 * If the feature is already set to the desired value, LY_SUCCESS is returned.
550 * By changing the feature, also all the feature which depends on it via their
551 * if-feature statements are again evaluated (disabled if a if-feature statemen
552 * evaluates to false).
553 *
Radek Krejci0af46292019-01-11 16:02:31 +0100554 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200555 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
556 * set all the features in the module.
557 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
Radek Krejci857189e2020-09-01 13:26:36 +0200558 * @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 +0200559 * @return LY_ERR value.
560 */
561static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200562lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200563{
Michal Vaskob0099a92020-08-31 14:55:23 +0200564 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200565 ly_bool all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200566 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200567 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200568 struct lysc_feature *f, **df;
569 struct lysc_iffeature *iff;
570 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100571 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200572
Radek Krejci6e67c402019-05-02 09:55:39 +0200573 if (!strcmp(name, "*")) {
574 /* enable all */
575 all = 1;
576 }
577
Michal Vasko997f9822020-10-14 13:09:05 +0200578 if (!mod->implemented) {
579 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 +0100580 return LY_EINVAL;
581 }
Radek Krejci14915cc2020-09-14 17:28:13 +0200582 if (!mod->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200583 if (all) {
584 /* no feature to enable */
585 return LY_SUCCESS;
586 }
Radek Krejci0af46292019-01-11 16:02:31 +0100587 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200588 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200589 }
590
Radek Krejciba03a5a2020-08-27 14:40:41 +0200591 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100592 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200593
Radek Krejcica3db002018-11-01 10:31:01 +0100594run:
Radek Krejci14915cc2020-09-14 17:28:13 +0200595 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) {
596 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200597 if (all || !strcmp(f->name, name)) {
598 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
599 if (all) {
600 /* skip already set features */
601 continue;
602 } else {
603 /* feature already set correctly */
Michal Vaskob0099a92020-08-31 14:55:23 +0200604 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200605 }
606 }
607
608 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200609 if (!skip_checks) {
610 /* check referenced features if they are enabled */
611 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
612 if (lysc_iffeature_value(iff) == LY_ENOT) {
613 if (all) {
614 ++disabled_count;
615 goto next;
616 } else {
Michal Vasko997f9822020-10-14 13:09:05 +0200617 LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by "
618 "its if-feature condition(s).", f->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200619 ret = LY_EDENIED;
620 goto cleanup;
Michal Vasko82c31e62020-07-17 15:30:40 +0200621 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200622 }
623 }
624 }
625 /* enable the feature */
626 f->flags |= LYS_FENABLED;
627 } else { /* disable */
628 /* disable the feature */
629 f->flags &= ~LYS_FENABLED;
630 }
631
632 /* remember the changed feature */
Radek Krejci3d92e442020-10-12 12:48:13 +0200633 ret = ly_set_add(changed, f, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200634 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200635
636 if (!all) {
637 /* stop in case changing a single feature */
638 break;
639 }
640 }
641next:
642 ;
643 }
644
645 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100646 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200647 ret = LY_ENOTFOUND;
648 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200649 }
650
Radek Krejcica3db002018-11-01 10:31:01 +0100651 if (value && all && disabled_count) {
652 if (changed_count == changed->count) {
653 /* no change in last run -> not able to enable all ... */
654 /* ... print errors */
Radek Krejci14915cc2020-09-14 17:28:13 +0200655 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) {
656 if (!(mod->features[u].flags & LYS_FENABLED)) {
Michal Vasko997f9822020-10-14 13:09:05 +0200657 LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by its if-feature "
658 "condition(s).", mod->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100659 --disabled_count;
660 }
661 }
662 /* ... restore the original state */
663 for (u = 0; u < changed->count; ++u) {
664 f = changed->objs[u];
665 /* re-disable the feature */
666 f->flags &= ~LYS_FENABLED;
667 }
668
Michal Vaskob0099a92020-08-31 14:55:23 +0200669 ret = LY_EDENIED;
670 goto cleanup;
Radek Krejcica3db002018-11-01 10:31:01 +0100671 } else {
672 /* we did some change in last run, try it again */
673 changed_count = changed->count;
674 goto run;
675 }
676 }
677
Radek Krejci151a5b72018-10-19 14:21:44 +0200678 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200679 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200680 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
681 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
682 * is not done - by default, features are disabled and must be explicitely enabled. */
683 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200684 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200685 if (!((*df)->flags & LYS_FENABLED)) {
686 /* not enabled, nothing to do */
687 continue;
688 }
689 /* check the feature's if-features which could change by the previous change of our feature */
690 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200691 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200692 /* the feature must be disabled now */
693 (*df)->flags &= ~LYS_FENABLED;
694 /* add the feature into the list of changed features */
Radek Krejci3d92e442020-10-12 12:48:13 +0200695 ret = ly_set_add(changed, *df, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200696 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200697 break;
698 }
699 }
700 }
701 }
702
Michal Vasko87a97f42020-10-06 10:30:28 +0200703 /* success */
704 ++mod->ctx->module_set_id;
705
Michal Vaskob0099a92020-08-31 14:55:23 +0200706cleanup:
Radek Krejci151a5b72018-10-19 14:21:44 +0200707 ly_set_free(changed, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200708 return ret;
Radek Krejci151a5b72018-10-19 14:21:44 +0200709}
710
711API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200712lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200713{
Radek Krejci0af46292019-01-11 16:02:31 +0100714 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200715
Michal Vasko22df3f02020-08-24 13:29:22 +0200716 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200717}
718
719API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200720lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200721{
Radek Krejci0af46292019-01-11 16:02:31 +0100722 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200723
Michal Vasko22df3f02020-08-24 13:29:22 +0200724 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200725}
726
Michal Vasko82c31e62020-07-17 15:30:40 +0200727API LY_ERR
728lys_feature_enable_force(const struct lys_module *module, const char *feature)
729{
730 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
731
Michal Vasko22df3f02020-08-24 13:29:22 +0200732 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200733}
734
735API LY_ERR
736lys_feature_disable_force(const struct lys_module *module, const char *feature)
737{
738 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
739
Michal Vasko22df3f02020-08-24 13:29:22 +0200740 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200741}
742
743API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200744lys_feature_value(const struct lys_module *module, const char *feature)
745{
Michal Vasko82c31e62020-07-17 15:30:40 +0200746 struct lysc_feature *f = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200747 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200748
749 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200750
751 /* search for the specified feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200752 LY_ARRAY_FOR(module->features, u) {
753 f = &module->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200754 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200755 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200756 }
757 }
758
759 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200760 if (!f) {
761 return LY_ENOTFOUND;
762 }
763
764 /* feature disabled */
765 if (!(f->flags & LYS_FENABLED)) {
766 return LY_ENOT;
767 }
768
769 /* check referenced features if they are enabled */
770 LY_ARRAY_FOR(f->iffeatures, u) {
771 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
772 /* if-feature disabled */
773 return LY_ENOT;
774 }
775 }
776
777 /* feature enabled */
778 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200779}
780
Michal Vaskoc193ce92020-03-06 11:04:48 +0100781API const struct lysc_node *
Radek Krejci857189e2020-09-01 13:26:36 +0200782lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100783{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200784 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100785
786 LY_CHECK_ARG_RET(NULL, node, NULL);
787
Michal Vaskoc193ce92020-03-06 11:04:48 +0100788 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100789 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100790 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100791 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200792 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100793 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100794 }
795 }
796 }
797
798 if (!recursive) {
799 return NULL;
800 }
801
Michal Vaskoc193ce92020-03-06 11:04:48 +0100802 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100803 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100804 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
805
Radek Krejcia3045382018-11-22 14:30:31 +0100806 return NULL;
807}
808
Radek Krejci19cf8052020-08-18 15:02:38 +0200809API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200810lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200811{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200812 struct lysc_action *act;
813 struct lysc_notif *notif;
814
Radek Krejci19cf8052020-08-18 15:02:38 +0200815 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
816
Radek Krejciaf9cd802020-10-06 21:59:47 +0200817 switch (node->nodetype) {
818 case LYS_CONTAINER:
819 case LYS_CHOICE:
820 case LYS_CASE:
821 case LYS_LEAF:
822 case LYS_LEAFLIST:
823 case LYS_LIST:
824 case LYS_ANYXML:
825 case LYS_ANYDATA:
826 if (prev_priv_p) {
827 *prev_priv_p = node->priv;
828 }
829 ((struct lysc_node *)node)->priv = priv;
830 break;
831 case LYS_RPC:
832 case LYS_ACTION:
833 act = (struct lysc_action *)node;
834 if (prev_priv_p) {
835 *prev_priv_p = act->priv;
836 }
837 act->priv = priv;
838 break;
839 case LYS_NOTIF:
840 notif = (struct lysc_notif *)node;
841 if (prev_priv_p) {
842 *prev_priv_p = notif->priv;
843 }
844 notif->priv = priv;
845 break;
846 default:
847 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200848 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200849
850 return LY_SUCCESS;
851}
852
Michal Vasko89b5c072020-10-06 13:52:44 +0200853API LY_ERR
854lys_set_implemented(struct lys_module *mod)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200855{
Michal Vasko89b5c072020-10-06 13:52:44 +0200856 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200857 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200858 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200859
860 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
861
862 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200863 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200864 return LY_SUCCESS;
865 }
866
867 /* we have module from the current context */
868 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
869 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200870 assert(m != mod);
871
872 /* check collision with other implemented revision */
873 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
874 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
875 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200876 }
877
Michal Vasko89b5c072020-10-06 13:52:44 +0200878 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200879 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200880
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200881 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200882 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200883
884 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200885 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200886
Michal Vasko89b5c072020-10-06 13:52:44 +0200887 if (mod == mod->ctx->implementing.objs[0]) {
888 /* the first module being implemented, consolidate the set */
889 if (ret) {
890 /* failure, full compile revert */
891 for (i = 0; i < mod->ctx->list.count; ++i) {
892 m = mod->ctx->list.objs[i];
893 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
894 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200895
Michal Vasko89b5c072020-10-06 13:52:44 +0200896 /* make the module correctly non-implemented again */
897 m->implemented = 0;
898 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
899 lys_precompile_augments_deviations_revert(mod->ctx, m);
900 }
901
902 /* free the compiled version of the module, if any */
903 lysc_module_free(m->compiled, NULL);
904 m->compiled = NULL;
905
906 if (m->implemented) {
907 /* recompile, must succeed because it was already compiled; hide messages because any
908 * warnings were already printed, are not really relevant, and would hide the real error */
909 uint32_t prev_lo = ly_log_options(0);
910 r = lys_compile(m, 0);
911 ly_log_options(prev_lo);
912 if (r) {
913 LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name);
914 }
915 }
916 }
917 }
918
919 ly_set_erase(&mod->ctx->implementing, NULL);
920 }
921 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200922}
923
Michal Vasko7c8439f2020-08-05 13:25:19 +0200924static LY_ERR
925lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
926{
927 struct lysp_import *imp;
928 struct lysp_include *inc;
929 LY_ARRAY_COUNT_TYPE u, v;
930
931 modp->parsing = 1;
932 LY_ARRAY_FOR(modp->imports, u) {
933 imp = &modp->imports[u];
934 if (!imp->module) {
935 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
936 }
937 /* check for importing the same module twice */
938 for (v = 0; v < u; ++v) {
939 if (imp->module == modp->imports[v].module) {
940 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
941 }
942 }
943 }
944 LY_ARRAY_FOR(modp->includes, u) {
945 inc = &modp->includes[u];
946 if (!inc->submodule) {
947 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
948 }
949 }
950 modp->parsing = 0;
951
952 return LY_SUCCESS;
953}
954
Michal Vasko3a41dff2020-07-15 14:30:28 +0200955LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200956lys_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 +0200957 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200958 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200959{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200960 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100961 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100962 struct lys_yang_parser_ctx *yangctx = NULL;
963 struct lys_yin_parser_ctx *yinctx = NULL;
964 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965
Michal Vasko3a41dff2020-07-15 14:30:28 +0200966 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968 switch (format) {
969 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200970 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100971 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100972 break;
973 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200974 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100975 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100976 break;
977 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200978 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200979 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100980 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200981 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200982 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200983 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100984
985 /* make sure that the newest revision is at position 0 */
986 lysp_sort_revisions(submod->revs);
987
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100988 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200989 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100990 if (latest_sp) {
991 if (submod->revs) {
992 if (!latest_sp->revs) {
993 /* latest has no revision, so mod is anyway newer */
994 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200995 /* the latest_sp is zeroed later when the new module is being inserted into the context */
996 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
997 submod->latest_revision = latest_sp->latest_revision;
998 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100999 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +02001000 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001001 }
Radek Krejcib3289d62019-09-18 12:21:39 +02001002 } else {
1003 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001004 }
1005 } else {
1006 submod->latest_revision = 1;
1007 }
1008
Radek Krejcib3289d62019-09-18 12:21:39 +02001009 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001010 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +02001011 }
1012
1013 if (latest_sp) {
1014 latest_sp->latest_revision = 0;
1015 }
1016
Michal Vasko7a0b0762020-09-02 16:37:01 +02001017 lys_parser_fill_filepath(ctx, in, &submod->filepath);
1018
Michal Vasko7c8439f2020-08-05 13:25:19 +02001019 /* resolve imports and includes */
1020 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
1021
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +01001023 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
1024 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001025
David Sedlák1b623122019-08-05 15:27:49 +02001026 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001028 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001029 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001030 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001031 *submodule = submod;
1032 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +02001033
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001034error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001035 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +02001036 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001037 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001038 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001039 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001040 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001041 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001042}
1043
Michal Vasko3a41dff2020-07-15 14:30:28 +02001044LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001045lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001046 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
1047 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001048{
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001049 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001050 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001051 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001052 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001053 struct lys_yang_parser_ctx *yangctx = NULL;
1054 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001055 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001056 char *filename, *rev, *dot;
1057 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +02001058
Michal Vasko3a41dff2020-07-15 14:30:28 +02001059 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001060 if (module) {
1061 *module = NULL;
1062 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001063
1064 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001065 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001066 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001067
1068 switch (format) {
1069 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001070 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001071 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001072 break;
1073 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001074 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001075 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001076 break;
1077 default:
1078 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001079 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001080 break;
1081 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001082 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001083
1084 /* make sure that the newest revision is at position 0 */
1085 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001086 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001087 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001088 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001089
Radek Krejcib3289d62019-09-18 12:21:39 +02001090 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001091 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001092 if (latest) {
1093 if (mod->revision) {
1094 if (!latest->revision) {
1095 /* latest has no revision, so mod is anyway newer */
1096 mod->latest_revision = latest->latest_revision;
1097 /* the latest is zeroed later when the new module is being inserted into the context */
1098 } else if (strcmp(mod->revision, latest->revision) > 0) {
1099 mod->latest_revision = latest->latest_revision;
1100 /* the latest is zeroed later when the new module is being inserted into the context */
1101 } else {
1102 latest = NULL;
1103 }
1104 } else {
1105 latest = NULL;
1106 }
1107 } else {
1108 mod->latest_revision = 1;
1109 }
1110
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001111 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001112 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001113 }
1114
Radek Krejci86d106e2018-10-18 09:53:19 +02001115 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001116 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001117 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1118 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001119 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001120 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001121 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001122 }
1123
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001124 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001125 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001126 if (mod_dup) {
1127 if (mod_dup->parsed) {
1128 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001129 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001131 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001132 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001133 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001134 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001135 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001136 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001137 goto error;
1138 } else {
1139 /* add the parsed data to the currently compiled-only module in the context */
1140 mod_dup->parsed = mod->parsed;
1141 mod_dup->parsed->mod = mod_dup;
1142 mod->parsed = NULL;
1143 lys_module_free(mod, NULL);
1144 mod = mod_dup;
1145 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001146 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001147 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001148
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001149 switch (in->type) {
1150 case LY_IN_FILEPATH:
1151 /* check that name and revision match filename */
1152 filename = strrchr(in->method.fpath.filepath, '/');
1153 if (!filename) {
1154 filename = in->method.fpath.filepath;
1155 } else {
1156 filename++;
1157 }
1158 rev = strchr(filename, '@');
1159 dot = strrchr(filename, '.');
1160
1161 /* name */
1162 len = strlen(mod->name);
1163 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001164 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001165 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1166 }
1167 if (rev) {
1168 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001169 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001170 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001171 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001172 }
1173 }
1174
1175 break;
1176 case LY_IN_FD:
1177 case LY_IN_FILE:
1178 case LY_IN_MEMORY:
1179 /* nothing special to do */
1180 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001181 case LY_IN_ERROR:
1182 LOGINT(ctx);
1183 ret = LY_EINT;
1184 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001185 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001186
1187 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001188
Michal Vasko89b5c072020-10-06 13:52:44 +02001189 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001190 /* pre-compile features and identities of the module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001191 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod->parsed, mod->parsed->features, &mod->features), error);
1192 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001193 }
1194
1195 if (latest) {
1196 latest->latest_revision = 0;
1197 }
1198
1199 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001200 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001201 LY_CHECK_GOTO(ret, error);
1202 ctx->module_set_id++;
1203
1204finish_parsing:
1205 /* resolve imports and includes */
1206 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1207
Michal Vasko89b5c072020-10-06 13:52:44 +02001208 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001209 /* pre-compile features and identities of any submodules */
1210 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001211 submod = mod->parsed->includes[u].submodule;
1212 ret = lys_feature_precompile(NULL, ctx, (struct lysp_module *)submod, submod->features, &mod->features);
1213 LY_CHECK_GOTO(ret, error);
1214 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1215 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001216 }
1217 }
1218
1219 /* check name collisions - typedefs and TODO groupings */
1220 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1221
Michal Vasko89b5c072020-10-06 13:52:44 +02001222 if (implement) {
1223 /* implement (compile) */
1224 LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001225 }
1226
1227 if (format == LYS_IN_YANG) {
1228 yang_parser_ctx_free(yangctx);
1229 } else {
1230 yin_parser_ctx_free(yinctx);
1231 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001232 if (module) {
1233 *module = mod;
1234 }
1235 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001236
1237error_ctx:
1238 ly_set_rm(&ctx->list, mod, NULL);
1239error:
1240 lys_module_free(mod, NULL);
1241 if (pctx) {
1242 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1243 }
1244 if (format == LYS_IN_YANG) {
1245 yang_parser_ctx_free(yangctx);
1246 } else {
1247 yin_parser_ctx_free(yinctx);
1248 }
1249
1250 return ret;
1251}
1252
1253API LY_ERR
1254lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1255{
1256 if (module) {
1257 *module = NULL;
1258 }
1259 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1260
1261 /* remember input position */
1262 in->func_start = in->current;
1263
1264 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001265}
1266
Michal Vasko3a41dff2020-07-15 14:30:28 +02001267API LY_ERR
1268lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001269{
Radek Krejci0f969882020-08-21 16:56:47 +02001270 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001271 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001272
Michal Vasko3a41dff2020-07-15 14:30:28 +02001273 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001274
Michal Vasko3a41dff2020-07-15 14:30:28 +02001275 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 +02001276
Michal Vasko3a41dff2020-07-15 14:30:28 +02001277 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001278 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001279
Michal Vasko3a41dff2020-07-15 14:30:28 +02001280 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001281}
1282
Michal Vasko3a41dff2020-07-15 14:30:28 +02001283API LY_ERR
1284lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001285{
Radek Krejci0f969882020-08-21 16:56:47 +02001286 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001287 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001288
Michal Vasko3a41dff2020-07-15 14:30:28 +02001289 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001290
Michal Vasko3a41dff2020-07-15 14:30:28 +02001291 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 +02001292
Michal Vasko3a41dff2020-07-15 14:30:28 +02001293 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001294 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001295
Michal Vasko3a41dff2020-07-15 14:30:28 +02001296 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001297}
1298
Michal Vasko3a41dff2020-07-15 14:30:28 +02001299API LY_ERR
1300lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001301{
Radek Krejci0f969882020-08-21 16:56:47 +02001302 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001303 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001304
Michal Vasko3a41dff2020-07-15 14:30:28 +02001305 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001306
Michal Vasko3a41dff2020-07-15 14:30:28 +02001307 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001308 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001309
Michal Vasko3a41dff2020-07-15 14:30:28 +02001310 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001311 ly_in_free(in, 0);
1312
Michal Vasko3a41dff2020-07-15 14:30:28 +02001313 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001314}
1315
1316API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001317lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001318 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001319{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001320 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001321 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001322 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001323 char *wd, *wn = NULL;
1324 DIR *dir = NULL;
1325 struct dirent *file;
1326 char *match_name = NULL;
1327 LYS_INFORMAT format_aux, match_format = 0;
1328 struct ly_set *dirs;
1329 struct stat st;
1330
1331 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1332
1333 /* start to fill the dir fifo with the context's search path (if set)
1334 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001335 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001336
1337 len = strlen(name);
1338 if (cwd) {
1339 wd = get_current_dir_name();
1340 if (!wd) {
1341 LOGMEM(NULL);
1342 goto cleanup;
1343 } else {
1344 /* add implicit current working directory (./) to be searched,
1345 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001346 ret = ly_set_add(dirs, wd, 0, NULL);
1347 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001348 implicit_cwd = 1;
1349 }
1350 }
1351 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001352 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001353 /* check for duplicities with the implicit current working directory */
1354 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1355 implicit_cwd = 0;
1356 continue;
1357 }
1358 wd = strdup(searchpaths[i]);
1359 if (!wd) {
1360 LOGMEM(NULL);
1361 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001362 } else {
1363 ret = ly_set_add(dirs, wd, 0, NULL);
1364 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001365 }
1366 }
1367 }
1368 wd = NULL;
1369
1370 /* start searching */
1371 while (dirs->count) {
1372 free(wd);
1373 free(wn); wn = NULL;
1374
1375 dirs->count--;
1376 wd = (char *)dirs->objs[dirs->count];
1377 dirs->objs[dirs->count] = NULL;
1378 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1379
1380 if (dir) {
1381 closedir(dir);
1382 }
1383 dir = opendir(wd);
1384 dir_len = strlen(wd);
1385 if (!dir) {
1386 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1387 } else {
1388 while ((file = readdir(dir))) {
1389 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1390 /* skip . and .. */
1391 continue;
1392 }
1393 free(wn);
1394 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1395 LOGMEM(NULL);
1396 goto cleanup;
1397 }
1398 if (stat(wn, &st) == -1) {
1399 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001400 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001401 continue;
1402 }
1403 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1404 /* we have another subdirectory in searchpath to explore,
1405 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001406 ret = ly_set_add(dirs, wn, 0, NULL);
1407 LY_CHECK_GOTO(ret, cleanup);
1408
Radek Krejcid33273d2018-10-25 14:55:52 +02001409 /* continue with the next item in current directory */
1410 wn = NULL;
1411 continue;
1412 } else if (!S_ISREG(st.st_mode)) {
1413 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1414 continue;
1415 }
1416
1417 /* here we know that the item is a file which can contain a module */
1418 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001419 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001420 /* different filename than the module we search for */
1421 continue;
1422 }
1423
1424 /* get type according to filename suffix */
1425 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001426 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001427 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001428 /* TODO YIN parser
1429 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1430 format_aux = LYS_IN_YIN;
1431 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001432 } else {
1433 /* not supportde suffix/file format */
1434 continue;
1435 }
1436
1437 if (revision) {
1438 /* we look for the specific revision, try to get it from the filename */
1439 if (file->d_name[len] == '@') {
1440 /* check revision from the filename */
1441 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1442 /* another revision */
1443 continue;
1444 } else {
1445 /* exact revision */
1446 free(match_name);
1447 match_name = wn;
1448 wn = NULL;
1449 match_len = dir_len + 1 + len;
1450 match_format = format_aux;
1451 goto success;
1452 }
1453 } else {
1454 /* continue trying to find exact revision match, use this only if not found */
1455 free(match_name);
1456 match_name = wn;
1457 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001458 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001459 match_format = format_aux;
1460 continue;
1461 }
1462 } else {
1463 /* remember the revision and try to find the newest one */
1464 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001465 if ((file->d_name[len] != '@') ||
1466 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 +02001467 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001468 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001469 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1470 continue;
1471 }
1472 free(match_name);
1473 }
1474
1475 match_name = wn;
1476 wn = NULL;
1477 match_len = dir_len + 1 + len;
1478 match_format = format_aux;
1479 continue;
1480 }
1481 }
1482 }
1483 }
1484
1485success:
1486 (*localfile) = match_name;
1487 match_name = NULL;
1488 if (format) {
1489 (*format) = match_format;
1490 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001491 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001492
1493cleanup:
1494 free(wn);
1495 free(wd);
1496 if (dir) {
1497 closedir(dir);
1498 }
1499 free(match_name);
1500 ly_set_free(dirs, free);
1501
1502 return ret;
1503}