blob: 43597db4358fc3c5d3ab2f709f1501c25c41c0c9 [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"
Radek Krejcica376bd2020-06-11 16:04:06 +020034#include "parser.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"
37#include "set.h"
38#include "tree.h"
39#include "tree_schema_internal.h"
40#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020041
Radek Krejcia3045382018-11-22 14:30:31 +010042API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +020043lys_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 +010044{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010045 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010046 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +020047 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010048 const struct lysc_action *actions;
49 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020050 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +010051
52 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
53
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020054next:
Radek Krejcia3045382018-11-22 14:30:31 +010055 if (!last) {
56 /* first call */
57
58 /* get know where to start */
59 if (parent) {
60 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010061 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020062 if (((struct lysc_node_choice *)parent)->cases) {
63 next = last = (const struct lysc_node *)&((struct lysc_node_choice *)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010064 }
Radek Krejci056d0a82018-12-06 16:57:25 +010065 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010067 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020068 if (snode && *snode) {
69 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010070 }
Radek Krejcia3045382018-11-22 14:30:31 +010071 }
Radek Krejcia3045382018-11-22 14:30:31 +010072 } else {
73 /* top level data */
74 next = last = module->data;
75 }
76 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010077 /* try to get action or notification */
78 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010079 }
Radek Krejci05b774b2019-02-25 13:26:18 +010080 /* test if the next can be returned */
81 goto check;
82
Michal Vasko1bf09392020-03-27 12:38:10 +010083 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010084 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010085 if (last->parent) {
86 actions = lysc_node_actions(last->parent);
87 } else {
88 actions = module->rpcs;
89 }
90 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +020091 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010092 break;
93 }
94 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +020095 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020096 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +010097 }
98 goto repeat;
99 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100100 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100101 if (last->parent) {
102 notifs = lysc_node_notifs(last->parent);
103 } else {
104 notifs = module->notifs;
105 }
106 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200107 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100108 break;
109 }
110 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200112 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100113 }
114 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200115 } else {
116 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100117 }
118
Radek Krejcia3045382018-11-22 14:30:31 +0100119repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100120 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
121 /* inside case (as an explicit parent, not when diving into it from choice),
122 * limit the list of children only to the specific case */
123 next = NULL;
124 }
Radek Krejcia3045382018-11-22 14:30:31 +0100125 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100126 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100127 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100128 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200129 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100130 } else if (!action_flag) {
131 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200132 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100133 } else if (!notif_flag) {
134 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200135 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100136 } else {
137 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100138 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100139 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100140 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100141check:
Radek Krejcia3045382018-11-22 14:30:31 +0100142 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100143 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100144 case LYS_ACTION:
145 case LYS_NOTIF:
146 case LYS_LEAF:
147 case LYS_ANYXML:
148 case LYS_ANYDATA:
149 case LYS_LIST:
150 case LYS_LEAFLIST:
151 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200152 case LYS_CASE:
153 if (options & LYS_GETNEXT_WITHCASE) {
154 break;
155 } else {
156 /* go into */
157 next = ((struct lysc_node_case *)next)->child;
158 }
159 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100160 case LYS_CONTAINER:
161 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
162 if (((struct lysc_node_container *)next)->child) {
163 /* go into */
164 next = ((struct lysc_node_container *)next)->child;
165 } else {
166 next = next->next;
167 }
168 goto repeat;
169 }
170 break;
171 case LYS_CHOICE:
172 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200173 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100174 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
175 next = next->next;
176 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100177 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100178 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200179 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100180 } else {
181 next = ((struct lysc_node_choice *)next)->cases->child;
182 }
Radek Krejcia3045382018-11-22 14:30:31 +0100183 }
184 goto repeat;
185 default:
186 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200187 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100188 return NULL;
189 }
190
191 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
192 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200193 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100194 next = next->next;
195 goto repeat;
196 }
197 }
198 return next;
199}
200
201API const struct lysc_node *
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800202lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath)
203{
204 const char *id = qpath;
205 const char *prefix, *name;
206 size_t prefix_len, name_len;
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800207 const struct lysc_node *node = context_node;
208 struct lys_module *mod = NULL;
209
210 LY_CHECK_ARG_RET(ctx, qpath, NULL);
211
Michal Vaskod989ba02020-08-24 10:59:24 +0200212 while (*id) {
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800213 if (id[0] == '/') {
214 ++id;
215 }
216 /* precess ".." in relative paths */
217 while (!strncmp("../", id, 3)) {
218 id += 3;
219 if (!node) {
220 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath);
221 return NULL;
222 }
223 node = node->parent;
224 }
225
226 if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) {
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200227 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id - qpath, qpath);
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800228 return NULL;
229 }
230 if (prefix) {
231 if (context_node) {
232 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
233 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200234 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800235 if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) {
236 struct lys_module *m = (struct lys_module *)ctx->list.objs[u];
237 if (mod) {
238 if (m->implemented) {
239 mod = m;
240 break;
241 } else if (m->latest_revision) {
242 mod = m;
243 }
244 } else {
245 mod = m;
246 }
247 }
248 }
249 }
250 }
251 if (!mod) {
252 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".",
253 id - qpath, qpath);
254 return NULL;
255 }
256
257 node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
258 if (!node) {
259 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath);
260 return NULL;
261 }
262 }
263
264 return node;
265}
266
267API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100268lys_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 +0200269 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100270{
271 const struct lysc_node *node = NULL;
272
273 LY_CHECK_ARG_RET(NULL, module, name, NULL);
274 if (!nodetype) {
275 nodetype = 0xffff;
276 }
277
278 while ((node = lys_getnext(node, parent, module->compiled, options))) {
279 if (!(node->nodetype & nodetype)) {
280 continue;
281 }
282 if (node->module != module) {
283 continue;
284 }
285
286 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200287 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100288 return node;
289 }
290 } else {
291 if (!strcmp(node->name, name)) {
292 return node;
293 }
294 }
295 }
296 return NULL;
297}
298
Michal Vasko519fd602020-05-26 12:17:39 +0200299API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200300lys_atomize_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200301{
302 LY_ERR ret = LY_SUCCESS;
303 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200304 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200305 uint32_t i;
306
307 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
308 if (!(options & LYXP_SCNODE_ALL)) {
309 options = LYXP_SCNODE;
310 }
311
312 memset(&xp_set, 0, sizeof xp_set);
313
314 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200315 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
316 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200317
318 /* atomize expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200319 ret = lyxp_atomize(exp, LY_PREF_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200320 LY_CHECK_GOTO(ret, cleanup);
321
322 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200323 ret = ly_set_new(set);
324 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200325
326 /* transform into ly_set */
327 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
328 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
329 (*set)->size = xp_set.used;
330
331 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200332 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200333 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL);
334 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200335 }
336 }
337
338cleanup:
339 lyxp_set_free_content(&xp_set);
340 lyxp_expr_free(ctx_node->module->ctx, exp);
341 return ret;
342}
343
Michal Vasko072de482020-08-05 13:27:21 +0200344API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200345lys_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 +0200346{
347 LY_ERR ret = LY_SUCCESS;
348 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200349 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200350 uint32_t i;
351
352 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
353 if (!(options & LYXP_SCNODE_ALL)) {
354 options = LYXP_SCNODE;
355 }
356
357 memset(&xp_set, 0, sizeof xp_set);
358
359 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200360 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
361 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200362
363 /* atomize expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200364 ret = lyxp_atomize(exp, LY_PREF_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200365 LY_CHECK_GOTO(ret, cleanup);
366
367 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200368 ret = ly_set_new(set);
369 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200370
371 /* transform into ly_set */
372 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
373 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
374 (*set)->size = xp_set.used;
375
376 for (i = 0; i < xp_set.used; ++i) {
377 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200378 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL);
379 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200380 }
381 }
382
383cleanup:
384 lyxp_set_free_content(&xp_set);
385 lyxp_expr_free(ctx_node->module->ctx, exp);
386 return ret;
387}
388
Michal Vasko14654712020-02-06 08:35:21 +0100389char *
390lysc_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 +0200391 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200392{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200393 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200394 char *path = NULL;
395 int len = 0;
396
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200397 LY_CHECK_ARG_RET(NULL, node, NULL);
398 if (buffer) {
399 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
400 }
401
Radek Krejci327de162019-06-14 12:52:07 +0200402 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200403 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200404 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100405 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200406 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100407 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200408
Michal Vasko65de0402020-08-03 16:34:19 +0200409 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
410 /* schema-only node */
411 continue;
412 }
413
Michal Vasko11deea12020-08-05 13:54:50 +0200414 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100416 if (parent && (iter->parent == parent)) {
417 slash = "";
418 } else {
419 slash = "/";
420 }
Radek Krejci327de162019-06-14 12:52:07 +0200421 if (!iter->parent || iter->parent->module != iter->module) {
422 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200423 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100424 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200425 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100426 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200427 }
Radek Krejci327de162019-06-14 12:52:07 +0200428 } else {
429 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200430 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100431 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200432 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100433 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200434 }
Radek Krejci327de162019-06-14 12:52:07 +0200435 }
436 free(s);
437 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200438
439 if (buffer && buflen <= (size_t)len) {
440 /* not enough space in buffer */
441 break;
442 }
Radek Krejci327de162019-06-14 12:52:07 +0200443 }
444
445 if (len < 0) {
446 free(path);
447 path = NULL;
448 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200449 if (buffer) {
450 strcpy(buffer, "/");
451 } else {
452 path = strdup("/");
453 }
Radek Krejci327de162019-06-14 12:52:07 +0200454 }
455 break;
456 }
457
Radek Krejci1c0c3442019-07-23 16:08:47 +0200458 if (buffer) {
459 return buffer;
460 } else {
461 return path;
462 }
Radek Krejci327de162019-06-14 12:52:07 +0200463}
464
Michal Vasko14654712020-02-06 08:35:21 +0100465API char *
466lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
467{
468 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
469}
470
Michal Vasko28d78432020-05-26 13:10:53 +0200471API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100472lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200473{
Michal Vasko28d78432020-05-26 13:10:53 +0200474 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
475 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200476}
477
Radek Krejci693262f2019-04-29 15:23:20 +0200478uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200479lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200480{
481 uint8_t *item;
482 uint8_t mask = 3, result;
483
Radek Krejci151a5b72018-10-19 14:21:44 +0200484 item = &list[pos / 4];
485 result = (*item) & (mask << 2 * (pos % 4));
486 return result >> 2 * (pos % 4);
487}
488
Michal Vasko28d78432020-05-26 13:10:53 +0200489static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200490lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200491{
492 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200493 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200494
Radek Krejci693262f2019-04-29 15:23:20 +0200495 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200496 (*index_e)++;
497
498 switch (op) {
499 case LYS_IFF_F:
500 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200501 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200502 case LYS_IFF_NOT:
503 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200504 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200505 case LYS_IFF_AND:
506 case LYS_IFF_OR:
507 a = lysc_iffeature_value_(iff, index_e, index_f);
508 b = lysc_iffeature_value_(iff, index_e, index_f);
509 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200510 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
511 return LY_SUCCESS;
512 } else {
513 return LY_ENOT;
514 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200515 } else { /* LYS_IFF_OR */
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 }
522 }
523
524 return 0;
525}
526
Michal Vasko28d78432020-05-26 13:10:53 +0200527API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200528lysc_iffeature_value(const struct lysc_iffeature *iff)
529{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200530 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200531
532 LY_CHECK_ARG_RET(NULL, iff, -1);
533
534 if (iff->expr) {
535 return lysc_iffeature_value_(iff, &index_e, &index_f);
536 }
537 return 0;
538}
539
Radek Krejci151a5b72018-10-19 14:21:44 +0200540/**
541 * @brief Enable/Disable the specified feature in the module.
542 *
543 * If the feature is already set to the desired value, LY_SUCCESS is returned.
544 * By changing the feature, also all the feature which depends on it via their
545 * if-feature statements are again evaluated (disabled if a if-feature statemen
546 * evaluates to false).
547 *
Radek Krejci0af46292019-01-11 16:02:31 +0100548 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200549 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
550 * set all the features in the module.
551 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
Radek Krejci857189e2020-09-01 13:26:36 +0200552 * @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 +0200553 * @return LY_ERR value.
554 */
555static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200556lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200557{
Michal Vaskob0099a92020-08-31 14:55:23 +0200558 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200559 ly_bool all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200560 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200561 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200562 struct lysc_feature *f, **df;
563 struct lysc_iffeature *iff;
564 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100565 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200566
Radek Krejci6e67c402019-05-02 09:55:39 +0200567 if (!strcmp(name, "*")) {
568 /* enable all */
569 all = 1;
570 }
571
Radek Krejci0af46292019-01-11 16:02:31 +0100572 if (!mod->compiled) {
573 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
574 mod->name);
575 return LY_EINVAL;
576 }
Radek Krejci14915cc2020-09-14 17:28:13 +0200577 if (!mod->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200578 if (all) {
579 /* no feature to enable */
580 return LY_SUCCESS;
581 }
Radek Krejci0af46292019-01-11 16:02:31 +0100582 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200583 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200584 }
585
Radek Krejciba03a5a2020-08-27 14:40:41 +0200586 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100587 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200588
Radek Krejcica3db002018-11-01 10:31:01 +0100589run:
Radek Krejci14915cc2020-09-14 17:28:13 +0200590 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) {
591 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200592 if (all || !strcmp(f->name, name)) {
593 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
594 if (all) {
595 /* skip already set features */
596 continue;
597 } else {
598 /* feature already set correctly */
Michal Vaskob0099a92020-08-31 14:55:23 +0200599 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200600 }
601 }
602
603 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200604 if (!skip_checks) {
605 /* check referenced features if they are enabled */
606 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
607 if (lysc_iffeature_value(iff) == LY_ENOT) {
608 if (all) {
609 ++disabled_count;
610 goto next;
611 } else {
612 LOGERR(ctx, LY_EDENIED,
613 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
614 f->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200615 ret = LY_EDENIED;
616 goto cleanup;
Michal Vasko82c31e62020-07-17 15:30:40 +0200617 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200618 }
619 }
620 }
621 /* enable the feature */
622 f->flags |= LYS_FENABLED;
623 } else { /* disable */
624 /* disable the feature */
625 f->flags &= ~LYS_FENABLED;
626 }
627
628 /* remember the changed feature */
Michal Vaskob0099a92020-08-31 14:55:23 +0200629 ret = ly_set_add(changed, f, LY_SET_OPT_USEASLIST, NULL);
630 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200631
632 if (!all) {
633 /* stop in case changing a single feature */
634 break;
635 }
636 }
637next:
638 ;
639 }
640
641 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100642 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200643 ret = LY_ENOTFOUND;
644 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200645 }
646
Radek Krejcica3db002018-11-01 10:31:01 +0100647 if (value && all && disabled_count) {
648 if (changed_count == changed->count) {
649 /* no change in last run -> not able to enable all ... */
650 /* ... print errors */
Radek Krejci14915cc2020-09-14 17:28:13 +0200651 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) {
652 if (!(mod->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100653 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100654 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci14915cc2020-09-14 17:28:13 +0200655 mod->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100656 --disabled_count;
657 }
658 }
659 /* ... restore the original state */
660 for (u = 0; u < changed->count; ++u) {
661 f = changed->objs[u];
662 /* re-disable the feature */
663 f->flags &= ~LYS_FENABLED;
664 }
665
Michal Vaskob0099a92020-08-31 14:55:23 +0200666 ret = LY_EDENIED;
667 goto cleanup;
Radek Krejcica3db002018-11-01 10:31:01 +0100668 } else {
669 /* we did some change in last run, try it again */
670 changed_count = changed->count;
671 goto run;
672 }
673 }
674
Radek Krejci151a5b72018-10-19 14:21:44 +0200675 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200676 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200677 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
678 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
679 * is not done - by default, features are disabled and must be explicitely enabled. */
680 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200681 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200682 if (!((*df)->flags & LYS_FENABLED)) {
683 /* not enabled, nothing to do */
684 continue;
685 }
686 /* check the feature's if-features which could change by the previous change of our feature */
687 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200688 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200689 /* the feature must be disabled now */
690 (*df)->flags &= ~LYS_FENABLED;
691 /* add the feature into the list of changed features */
Michal Vaskob0099a92020-08-31 14:55:23 +0200692 ret = ly_set_add(changed, *df, LY_SET_OPT_USEASLIST, NULL);
693 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200694 break;
695 }
696 }
697 }
698 }
699
Michal Vaskob0099a92020-08-31 14:55:23 +0200700cleanup:
Radek Krejci151a5b72018-10-19 14:21:44 +0200701 ly_set_free(changed, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200702 return ret;
Radek Krejci151a5b72018-10-19 14:21:44 +0200703}
704
705API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200706lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200707{
Radek Krejci0af46292019-01-11 16:02:31 +0100708 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200709
Michal Vasko22df3f02020-08-24 13:29:22 +0200710 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200711}
712
713API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200714lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200715{
Radek Krejci0af46292019-01-11 16:02:31 +0100716 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200717
Michal Vasko22df3f02020-08-24 13:29:22 +0200718 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200719}
720
Michal Vasko82c31e62020-07-17 15:30:40 +0200721API LY_ERR
722lys_feature_enable_force(const struct lys_module *module, const char *feature)
723{
724 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
725
Michal Vasko22df3f02020-08-24 13:29:22 +0200726 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200727}
728
729API LY_ERR
730lys_feature_disable_force(const struct lys_module *module, const char *feature)
731{
732 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
733
Michal Vasko22df3f02020-08-24 13:29:22 +0200734 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200735}
736
737API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200738lys_feature_value(const struct lys_module *module, const char *feature)
739{
Michal Vasko82c31e62020-07-17 15:30:40 +0200740 struct lysc_feature *f = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200741 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200742
743 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200744
745 /* search for the specified feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200746 LY_ARRAY_FOR(module->features, u) {
747 f = &module->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200748 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200749 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200750 }
751 }
752
753 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200754 if (!f) {
755 return LY_ENOTFOUND;
756 }
757
758 /* feature disabled */
759 if (!(f->flags & LYS_FENABLED)) {
760 return LY_ENOT;
761 }
762
763 /* check referenced features if they are enabled */
764 LY_ARRAY_FOR(f->iffeatures, u) {
765 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
766 /* if-feature disabled */
767 return LY_ENOT;
768 }
769 }
770
771 /* feature enabled */
772 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200773}
774
Michal Vaskoc193ce92020-03-06 11:04:48 +0100775API const struct lysc_node *
Radek Krejci857189e2020-09-01 13:26:36 +0200776lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100777{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200778 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100779
780 LY_CHECK_ARG_RET(NULL, node, NULL);
781
Michal Vaskoc193ce92020-03-06 11:04:48 +0100782 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100783 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100784 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100785 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200786 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100787 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100788 }
789 }
790 }
791
792 if (!recursive) {
793 return NULL;
794 }
795
Michal Vaskoc193ce92020-03-06 11:04:48 +0100796 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100797 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100798 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
799
Radek Krejcia3045382018-11-22 14:30:31 +0100800 return NULL;
801}
802
Radek Krejci19cf8052020-08-18 15:02:38 +0200803API LY_ERR
Michal Vasko22df3f02020-08-24 13:29:22 +0200804lysc_node_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200805{
806 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
807
808 if (prev_priv_p) {
809 *prev_priv_p = node->priv;
810 }
811 ((struct lysc_node *)node)->priv = priv;
812
813 return LY_SUCCESS;
814}
815
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200816LY_ERR
817lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
818{
819 struct lys_module *m;
820
821 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
822
823 if (mod->implemented) {
824 return LY_SUCCESS;
825 }
826
827 /* we have module from the current context */
828 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
829 if (m) {
830 if (m != mod) {
831 /* check collision with other implemented revision */
832 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
833 mod->name, mod->revision ? mod->revision : "module without revision");
834 return LY_EDENIED;
835 } else {
836 /* mod is already implemented */
837 return LY_SUCCESS;
838 }
839 }
840
841 /* mark the module implemented, check for collision was already done */
842 mod->implemented = value;
843
844 /* compile the schema */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200845 LY_CHECK_RET(lys_compile(mod, LYSC_OPT_INTERNAL));
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200846
847 return LY_SUCCESS;
848}
849
850API LY_ERR
851lys_set_implemented(struct lys_module *mod)
852{
853 return lys_set_implemented_internal(mod, 1);
854}
855
Michal Vasko7c8439f2020-08-05 13:25:19 +0200856static LY_ERR
857lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
858{
859 struct lysp_import *imp;
860 struct lysp_include *inc;
861 LY_ARRAY_COUNT_TYPE u, v;
862
863 modp->parsing = 1;
864 LY_ARRAY_FOR(modp->imports, u) {
865 imp = &modp->imports[u];
866 if (!imp->module) {
867 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
868 }
869 /* check for importing the same module twice */
870 for (v = 0; v < u; ++v) {
871 if (imp->module == modp->imports[v].module) {
872 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
873 }
874 }
875 }
876 LY_ARRAY_FOR(modp->includes, u) {
877 inc = &modp->includes[u];
878 if (!inc->submodule) {
879 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
880 }
881 }
882 modp->parsing = 0;
883
884 return LY_SUCCESS;
885}
886
Michal Vasko3a41dff2020-07-15 14:30:28 +0200887LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200888lys_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 +0200889 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200890 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200891{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200892 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100894 struct lys_yang_parser_ctx *yangctx = NULL;
895 struct lys_yin_parser_ctx *yinctx = NULL;
896 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897
Michal Vasko3a41dff2020-07-15 14:30:28 +0200898 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100899
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100900 switch (format) {
901 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200902 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100903 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100904 break;
905 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200906 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100907 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100908 break;
909 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200910 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200911 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200913 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200914 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200915 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916
917 /* make sure that the newest revision is at position 0 */
918 lysp_sort_revisions(submod->revs);
919
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 /* decide the latest revision */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200921 latest_sp = ly_ctx_get_submodule(ctx, submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 if (latest_sp) {
923 if (submod->revs) {
924 if (!latest_sp->revs) {
925 /* latest has no revision, so mod is anyway newer */
926 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200927 /* the latest_sp is zeroed later when the new module is being inserted into the context */
928 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
929 submod->latest_revision = latest_sp->latest_revision;
930 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100931 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200932 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100933 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200934 } else {
935 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 }
937 } else {
938 submod->latest_revision = 1;
939 }
940
Radek Krejcib3289d62019-09-18 12:21:39 +0200941 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200942 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200943 }
944
945 if (latest_sp) {
946 latest_sp->latest_revision = 0;
947 }
948
Michal Vasko7a0b0762020-09-02 16:37:01 +0200949 lys_parser_fill_filepath(ctx, in, &submod->filepath);
950
Michal Vasko7c8439f2020-08-05 13:25:19 +0200951 /* resolve imports and includes */
952 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
953
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100954 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100955 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
956 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100957
David Sedlák1b623122019-08-05 15:27:49 +0200958 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100959 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200960 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100961 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200962 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200963 *submodule = submod;
964 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200965
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100966error:
967 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200968 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100969 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200970 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100971 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200972 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200973 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200974}
975
Michal Vasko3a41dff2020-07-15 14:30:28 +0200976LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200977lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200978 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
979 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200980{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100981 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200982 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200983 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100984 struct lys_yang_parser_ctx *yangctx = NULL;
985 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200986 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200987 char *filename, *rev, *dot;
988 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +0200989
Michal Vasko3a41dff2020-07-15 14:30:28 +0200990 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200991 if (module) {
992 *module = NULL;
993 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200994
995 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200996 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100997 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200998
999 switch (format) {
1000 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001001 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001002 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001003 break;
1004 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001005 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001006 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001007 break;
1008 default:
1009 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001010 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001011 break;
1012 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001013 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001014
1015 /* make sure that the newest revision is at position 0 */
1016 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001017 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001018 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001019 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001020
Radek Krejcib3289d62019-09-18 12:21:39 +02001021 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001022 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001023 if (latest) {
1024 if (mod->revision) {
1025 if (!latest->revision) {
1026 /* latest has no revision, so mod is anyway newer */
1027 mod->latest_revision = latest->latest_revision;
1028 /* the latest is zeroed later when the new module is being inserted into the context */
1029 } else if (strcmp(mod->revision, latest->revision) > 0) {
1030 mod->latest_revision = latest->latest_revision;
1031 /* the latest is zeroed later when the new module is being inserted into the context */
1032 } else {
1033 latest = NULL;
1034 }
1035 } else {
1036 latest = NULL;
1037 }
1038 } else {
1039 mod->latest_revision = 1;
1040 }
1041
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001042 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001043 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 }
1045
Radek Krejci86d106e2018-10-18 09:53:19 +02001046 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001047 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001048 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1049 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001050 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001051 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001052 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +02001054 }
1055
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001056 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001057 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001058 if (mod_dup) {
1059 if (mod_dup->parsed) {
1060 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001061 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
1063 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001064 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001065 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
1066 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001067 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001068 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001069 goto error;
1070 } else {
1071 /* add the parsed data to the currently compiled-only module in the context */
1072 mod_dup->parsed = mod->parsed;
1073 mod_dup->parsed->mod = mod_dup;
1074 mod->parsed = NULL;
1075 lys_module_free(mod, NULL);
1076 mod = mod_dup;
1077 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001078 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001079 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001080
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001081 switch (in->type) {
1082 case LY_IN_FILEPATH:
1083 /* check that name and revision match filename */
1084 filename = strrchr(in->method.fpath.filepath, '/');
1085 if (!filename) {
1086 filename = in->method.fpath.filepath;
1087 } else {
1088 filename++;
1089 }
1090 rev = strchr(filename, '@');
1091 dot = strrchr(filename, '.');
1092
1093 /* name */
1094 len = strlen(mod->name);
1095 if (strncmp(filename, mod->name, len) ||
1096 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
1097 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1098 }
1099 if (rev) {
1100 len = dot - ++rev;
1101 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
1102 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
1103 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
1104 }
1105 }
1106
1107 break;
1108 case LY_IN_FD:
1109 case LY_IN_FILE:
1110 case LY_IN_MEMORY:
1111 /* nothing special to do */
1112 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001113 case LY_IN_ERROR:
1114 LOGINT(ctx);
1115 ret = LY_EINT;
1116 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001117 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001118
1119 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001120
Michal Vasko7a0b0762020-09-02 16:37:01 +02001121 if (!mod->implemented) {
1122 /* pre-compile features and identities of the module */
Radek Krejci14915cc2020-09-14 17:28:13 +02001123 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->features), error);
Radek Krejci80d281e2020-09-14 17:42:54 +02001124 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->identities, &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001125 }
1126
1127 if (latest) {
1128 latest->latest_revision = 0;
1129 }
1130
1131 /* add into context */
1132 ret = ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST, NULL);
1133 LY_CHECK_GOTO(ret, error);
1134 ctx->module_set_id++;
1135
1136finish_parsing:
1137 /* resolve imports and includes */
1138 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1139
1140 if (!mod->implemented) {
1141 /* pre-compile features and identities of any submodules */
1142 LY_ARRAY_FOR(mod->parsed->includes, u) {
1143 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->features,
Radek Krejci14915cc2020-09-14 17:28:13 +02001144 &mod->features), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001145 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->identities,
Radek Krejci80d281e2020-09-14 17:42:54 +02001146 &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001147 }
1148 }
1149
1150 /* check name collisions - typedefs and TODO groupings */
1151 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1152
1153 /* compile */
1154 if (!mod->compiled) {
1155 ret = lys_compile(mod, 0);
1156 LY_CHECK_GOTO(ret, error_ctx);
1157 }
1158
1159 if (format == LYS_IN_YANG) {
1160 yang_parser_ctx_free(yangctx);
1161 } else {
1162 yin_parser_ctx_free(yinctx);
1163 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001164 if (module) {
1165 *module = mod;
1166 }
1167 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001168
1169error_ctx:
1170 ly_set_rm(&ctx->list, mod, NULL);
1171error:
1172 lys_module_free(mod, NULL);
1173 if (pctx) {
1174 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1175 }
1176 if (format == LYS_IN_YANG) {
1177 yang_parser_ctx_free(yangctx);
1178 } else {
1179 yin_parser_ctx_free(yinctx);
1180 }
1181
1182 return ret;
1183}
1184
1185API LY_ERR
1186lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1187{
1188 if (module) {
1189 *module = NULL;
1190 }
1191 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1192
1193 /* remember input position */
1194 in->func_start = in->current;
1195
1196 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001197}
1198
Michal Vasko3a41dff2020-07-15 14:30:28 +02001199API LY_ERR
1200lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001201{
Radek Krejci0f969882020-08-21 16:56:47 +02001202 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001203 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001204
Michal Vasko3a41dff2020-07-15 14:30:28 +02001205 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001206
Michal Vasko3a41dff2020-07-15 14:30:28 +02001207 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 +02001208
Michal Vasko3a41dff2020-07-15 14:30:28 +02001209 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001210 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001211
Michal Vasko3a41dff2020-07-15 14:30:28 +02001212 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001213}
1214
Michal Vasko3a41dff2020-07-15 14:30:28 +02001215API LY_ERR
1216lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001217{
Radek Krejci0f969882020-08-21 16:56:47 +02001218 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001219 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001220
Michal Vasko3a41dff2020-07-15 14:30:28 +02001221 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001222
Michal Vasko3a41dff2020-07-15 14:30:28 +02001223 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 +02001224
Michal Vasko3a41dff2020-07-15 14:30:28 +02001225 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001226 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001227
Michal Vasko3a41dff2020-07-15 14:30:28 +02001228 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001229}
1230
Michal Vasko3a41dff2020-07-15 14:30:28 +02001231API LY_ERR
1232lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001233{
Radek Krejci0f969882020-08-21 16:56:47 +02001234 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001235 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001236
Michal Vasko3a41dff2020-07-15 14:30:28 +02001237 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001238
Michal Vasko3a41dff2020-07-15 14:30:28 +02001239 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
1240 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001241
Michal Vasko3a41dff2020-07-15 14:30:28 +02001242 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001243 ly_in_free(in, 0);
1244
Michal Vasko3a41dff2020-07-15 14:30:28 +02001245 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001246}
1247
1248API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001249lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001250 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001251{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001252 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001253 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001254 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001255 char *wd, *wn = NULL;
1256 DIR *dir = NULL;
1257 struct dirent *file;
1258 char *match_name = NULL;
1259 LYS_INFORMAT format_aux, match_format = 0;
1260 struct ly_set *dirs;
1261 struct stat st;
1262
1263 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1264
1265 /* start to fill the dir fifo with the context's search path (if set)
1266 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001267 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001268
1269 len = strlen(name);
1270 if (cwd) {
1271 wd = get_current_dir_name();
1272 if (!wd) {
1273 LOGMEM(NULL);
1274 goto cleanup;
1275 } else {
1276 /* add implicit current working directory (./) to be searched,
1277 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001278 ret = ly_set_add(dirs, wd, 0, NULL);
1279 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001280 implicit_cwd = 1;
1281 }
1282 }
1283 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001284 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001285 /* check for duplicities with the implicit current working directory */
1286 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1287 implicit_cwd = 0;
1288 continue;
1289 }
1290 wd = strdup(searchpaths[i]);
1291 if (!wd) {
1292 LOGMEM(NULL);
1293 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001294 } else {
1295 ret = ly_set_add(dirs, wd, 0, NULL);
1296 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001297 }
1298 }
1299 }
1300 wd = NULL;
1301
1302 /* start searching */
1303 while (dirs->count) {
1304 free(wd);
1305 free(wn); wn = NULL;
1306
1307 dirs->count--;
1308 wd = (char *)dirs->objs[dirs->count];
1309 dirs->objs[dirs->count] = NULL;
1310 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1311
1312 if (dir) {
1313 closedir(dir);
1314 }
1315 dir = opendir(wd);
1316 dir_len = strlen(wd);
1317 if (!dir) {
1318 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1319 } else {
1320 while ((file = readdir(dir))) {
1321 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1322 /* skip . and .. */
1323 continue;
1324 }
1325 free(wn);
1326 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1327 LOGMEM(NULL);
1328 goto cleanup;
1329 }
1330 if (stat(wn, &st) == -1) {
1331 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1332 file->d_name, wd, strerror(errno));
1333 continue;
1334 }
1335 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1336 /* we have another subdirectory in searchpath to explore,
1337 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001338 ret = ly_set_add(dirs, wn, 0, NULL);
1339 LY_CHECK_GOTO(ret, cleanup);
1340
Radek Krejcid33273d2018-10-25 14:55:52 +02001341 /* continue with the next item in current directory */
1342 wn = NULL;
1343 continue;
1344 } else if (!S_ISREG(st.st_mode)) {
1345 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1346 continue;
1347 }
1348
1349 /* here we know that the item is a file which can contain a module */
1350 if (strncmp(name, file->d_name, len) ||
1351 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1352 /* different filename than the module we search for */
1353 continue;
1354 }
1355
1356 /* get type according to filename suffix */
1357 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001358 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001359 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001360 /* TODO YIN parser
1361 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1362 format_aux = LYS_IN_YIN;
1363 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001364 } else {
1365 /* not supportde suffix/file format */
1366 continue;
1367 }
1368
1369 if (revision) {
1370 /* we look for the specific revision, try to get it from the filename */
1371 if (file->d_name[len] == '@') {
1372 /* check revision from the filename */
1373 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1374 /* another revision */
1375 continue;
1376 } else {
1377 /* exact revision */
1378 free(match_name);
1379 match_name = wn;
1380 wn = NULL;
1381 match_len = dir_len + 1 + len;
1382 match_format = format_aux;
1383 goto success;
1384 }
1385 } else {
1386 /* continue trying to find exact revision match, use this only if not found */
1387 free(match_name);
1388 match_name = wn;
1389 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001390 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001391 match_format = format_aux;
1392 continue;
1393 }
1394 } else {
1395 /* remember the revision and try to find the newest one */
1396 if (match_name) {
1397 if (file->d_name[len] != '@' ||
1398 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1399 continue;
1400 } else if (match_name[match_len] == '@' &&
1401 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1402 continue;
1403 }
1404 free(match_name);
1405 }
1406
1407 match_name = wn;
1408 wn = NULL;
1409 match_len = dir_len + 1 + len;
1410 match_format = format_aux;
1411 continue;
1412 }
1413 }
1414 }
1415 }
1416
1417success:
1418 (*localfile) = match_name;
1419 match_name = NULL;
1420 if (format) {
1421 (*format) = match_format;
1422 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001423 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001424
1425cleanup:
1426 free(wn);
1427 free(wd);
1428 if (dir) {
1429 closedir(dir);
1430 }
1431 free(match_name);
1432 ly_set_free(dirs, free);
1433
1434 return ret;
1435}