blob: 3d2dcb9693124155a0738a1fd07b3dc0d20e8032 [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 Krejci1deb5be2020-08-26 16:43:36 +020047 uint8_t 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;
Radek Krejcia3045382018-11-22 14:30:31 +0100115 }
116
117 next = last->next;
118repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100119 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
120 /* inside case (as an explicit parent, not when diving into it from choice),
121 * limit the list of children only to the specific case */
122 next = NULL;
123 }
Radek Krejcia3045382018-11-22 14:30:31 +0100124 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100125 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100126 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100127 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200128 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100129 } else if (!action_flag) {
130 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200131 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100132 } else if (!notif_flag) {
133 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200134 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100135 } else {
136 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100137 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100138 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100139 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100140check:
Radek Krejcia3045382018-11-22 14:30:31 +0100141 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100142 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100143 case LYS_ACTION:
144 case LYS_NOTIF:
145 case LYS_LEAF:
146 case LYS_ANYXML:
147 case LYS_ANYDATA:
148 case LYS_LIST:
149 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100150 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100151 break;
152 case LYS_CONTAINER:
153 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
154 if (((struct lysc_node_container *)next)->child) {
155 /* go into */
156 next = ((struct lysc_node_container *)next)->child;
157 } else {
158 next = next->next;
159 }
160 goto repeat;
161 }
162 break;
163 case LYS_CHOICE:
164 if (options & LYS_GETNEXT_WITHCHOICE) {
165 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100166 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
167 next = next->next;
168 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100169 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100170 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200171 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100172 } else {
173 next = ((struct lysc_node_choice *)next)->cases->child;
174 }
Radek Krejcia3045382018-11-22 14:30:31 +0100175 }
176 goto repeat;
177 default:
178 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200179 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100180 return NULL;
181 }
182
183 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
184 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200185 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100186 next = next->next;
187 goto repeat;
188 }
189 }
190 return next;
191}
192
193API const struct lysc_node *
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800194lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath)
195{
196 const char *id = qpath;
197 const char *prefix, *name;
198 size_t prefix_len, name_len;
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800199 const struct lysc_node *node = context_node;
200 struct lys_module *mod = NULL;
201
202 LY_CHECK_ARG_RET(ctx, qpath, NULL);
203
Michal Vaskod989ba02020-08-24 10:59:24 +0200204 while (*id) {
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800205 if (id[0] == '/') {
206 ++id;
207 }
208 /* precess ".." in relative paths */
209 while (!strncmp("../", id, 3)) {
210 id += 3;
211 if (!node) {
212 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath);
213 return NULL;
214 }
215 node = node->parent;
216 }
217
218 if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) {
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200219 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id - qpath, qpath);
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800220 return NULL;
221 }
222 if (prefix) {
223 if (context_node) {
224 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
225 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200226 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800227 if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) {
228 struct lys_module *m = (struct lys_module *)ctx->list.objs[u];
229 if (mod) {
230 if (m->implemented) {
231 mod = m;
232 break;
233 } else if (m->latest_revision) {
234 mod = m;
235 }
236 } else {
237 mod = m;
238 }
239 }
240 }
241 }
242 }
243 if (!mod) {
244 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".",
245 id - qpath, qpath);
246 return NULL;
247 }
248
249 node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
250 if (!node) {
251 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath);
252 return NULL;
253 }
254 }
255
256 return node;
257}
258
259API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100260lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200261 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100262{
263 const struct lysc_node *node = NULL;
264
265 LY_CHECK_ARG_RET(NULL, module, name, NULL);
266 if (!nodetype) {
267 nodetype = 0xffff;
268 }
269
270 while ((node = lys_getnext(node, parent, module->compiled, options))) {
271 if (!(node->nodetype & nodetype)) {
272 continue;
273 }
274 if (node->module != module) {
275 continue;
276 }
277
278 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200279 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100280 return node;
281 }
282 } else {
283 if (!strcmp(node->name, name)) {
284 return node;
285 }
286 }
287 }
288 return NULL;
289}
290
Michal Vasko519fd602020-05-26 12:17:39 +0200291API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200292lys_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 +0200293{
294 LY_ERR ret = LY_SUCCESS;
295 struct lyxp_set xp_set;
296 struct lyxp_expr *exp;
297 uint32_t i;
298
299 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
300 if (!(options & LYXP_SCNODE_ALL)) {
301 options = LYXP_SCNODE;
302 }
303
304 memset(&xp_set, 0, sizeof xp_set);
305
306 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +0200307 exp = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1);
Michal Vasko519fd602020-05-26 12:17:39 +0200308 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
309
310 /* atomize expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200311 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 +0200312 LY_CHECK_GOTO(ret, cleanup);
313
314 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200315 ret = ly_set_new(set);
316 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200317
318 /* transform into ly_set */
319 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
320 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
321 (*set)->size = xp_set.used;
322
323 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200324 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200325 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL);
326 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200327 }
328 }
329
330cleanup:
331 lyxp_set_free_content(&xp_set);
332 lyxp_expr_free(ctx_node->module->ctx, exp);
333 return ret;
334}
335
Michal Vasko072de482020-08-05 13:27:21 +0200336API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200337lys_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 +0200338{
339 LY_ERR ret = LY_SUCCESS;
340 struct lyxp_set xp_set;
341 struct lyxp_expr *exp;
342 uint32_t i;
343
344 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
345 if (!(options & LYXP_SCNODE_ALL)) {
346 options = LYXP_SCNODE;
347 }
348
349 memset(&xp_set, 0, sizeof xp_set);
350
351 /* compile expression */
352 exp = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1);
353 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
354
355 /* atomize expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200356 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 +0200357 LY_CHECK_GOTO(ret, cleanup);
358
359 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200360 ret = ly_set_new(set);
361 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200362
363 /* transform into ly_set */
364 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
365 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
366 (*set)->size = xp_set.used;
367
368 for (i = 0; i < xp_set.used; ++i) {
369 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 +0200370 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL);
371 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200372 }
373 }
374
375cleanup:
376 lyxp_set_free_content(&xp_set);
377 lyxp_expr_free(ctx_node->module->ctx, exp);
378 return ret;
379}
380
Michal Vasko14654712020-02-06 08:35:21 +0100381char *
382lysc_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 +0200383 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200384{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200385 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200386 char *path = NULL;
387 int len = 0;
388
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200389 LY_CHECK_ARG_RET(NULL, node, NULL);
390 if (buffer) {
391 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
392 }
393
Radek Krejci327de162019-06-14 12:52:07 +0200394 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200395 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200396 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100397 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200398 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100399 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200400
Michal Vasko65de0402020-08-03 16:34:19 +0200401 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
402 /* schema-only node */
403 continue;
404 }
405
Michal Vasko11deea12020-08-05 13:54:50 +0200406 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200407 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100408 if (parent && (iter->parent == parent)) {
409 slash = "";
410 } else {
411 slash = "/";
412 }
Radek Krejci327de162019-06-14 12:52:07 +0200413 if (!iter->parent || iter->parent->module != iter->module) {
414 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200415 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100416 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200417 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100418 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200419 }
Radek Krejci327de162019-06-14 12:52:07 +0200420 } else {
421 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200422 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100423 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200424 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100425 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200426 }
Radek Krejci327de162019-06-14 12:52:07 +0200427 }
428 free(s);
429 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200430
431 if (buffer && buflen <= (size_t)len) {
432 /* not enough space in buffer */
433 break;
434 }
Radek Krejci327de162019-06-14 12:52:07 +0200435 }
436
437 if (len < 0) {
438 free(path);
439 path = NULL;
440 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200441 if (buffer) {
442 strcpy(buffer, "/");
443 } else {
444 path = strdup("/");
445 }
Radek Krejci327de162019-06-14 12:52:07 +0200446 }
447 break;
448 }
449
Radek Krejci1c0c3442019-07-23 16:08:47 +0200450 if (buffer) {
451 return buffer;
452 } else {
453 return path;
454 }
Radek Krejci327de162019-06-14 12:52:07 +0200455}
456
Michal Vasko14654712020-02-06 08:35:21 +0100457API char *
458lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
459{
460 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
461}
462
Michal Vasko28d78432020-05-26 13:10:53 +0200463API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100464lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200465{
Michal Vasko28d78432020-05-26 13:10:53 +0200466 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
467 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200468}
469
Radek Krejci693262f2019-04-29 15:23:20 +0200470uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200471lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200472{
473 uint8_t *item;
474 uint8_t mask = 3, result;
475
Radek Krejci151a5b72018-10-19 14:21:44 +0200476 item = &list[pos / 4];
477 result = (*item) & (mask << 2 * (pos % 4));
478 return result >> 2 * (pos % 4);
479}
480
Michal Vasko28d78432020-05-26 13:10:53 +0200481static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200482lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200483{
484 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200485 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200486
Radek Krejci693262f2019-04-29 15:23:20 +0200487 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200488 (*index_e)++;
489
490 switch (op) {
491 case LYS_IFF_F:
492 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200493 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200494 case LYS_IFF_NOT:
495 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200496 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200497 case LYS_IFF_AND:
498 case LYS_IFF_OR:
499 a = lysc_iffeature_value_(iff, index_e, index_f);
500 b = lysc_iffeature_value_(iff, index_e, index_f);
501 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200502 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
503 return LY_SUCCESS;
504 } else {
505 return LY_ENOT;
506 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200507 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200508 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
509 return LY_SUCCESS;
510 } else {
511 return LY_ENOT;
512 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200513 }
514 }
515
516 return 0;
517}
518
Michal Vasko28d78432020-05-26 13:10:53 +0200519API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200520lysc_iffeature_value(const struct lysc_iffeature *iff)
521{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200522 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200523
524 LY_CHECK_ARG_RET(NULL, iff, -1);
525
526 if (iff->expr) {
527 return lysc_iffeature_value_(iff, &index_e, &index_f);
528 }
529 return 0;
530}
531
Radek Krejci151a5b72018-10-19 14:21:44 +0200532/**
533 * @brief Enable/Disable the specified feature in the module.
534 *
535 * If the feature is already set to the desired value, LY_SUCCESS is returned.
536 * By changing the feature, also all the feature which depends on it via their
537 * if-feature statements are again evaluated (disabled if a if-feature statemen
538 * evaluates to false).
539 *
Radek Krejci0af46292019-01-11 16:02:31 +0100540 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200541 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
542 * set all the features in the module.
543 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
544 * @return LY_ERR value.
545 */
546static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200547lys_feature_change(const struct lys_module *mod, const char *name, uint8_t value, uint8_t skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200548{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200549 uint8_t all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200550 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200551 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200552 struct lysc_feature *f, **df;
553 struct lysc_iffeature *iff;
554 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100555 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200556
Radek Krejci6e67c402019-05-02 09:55:39 +0200557 if (!strcmp(name, "*")) {
558 /* enable all */
559 all = 1;
560 }
561
Radek Krejci0af46292019-01-11 16:02:31 +0100562 if (!mod->compiled) {
563 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
564 mod->name);
565 return LY_EINVAL;
566 }
567 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200568 if (all) {
569 /* no feature to enable */
570 return LY_SUCCESS;
571 }
Radek Krejci0af46292019-01-11 16:02:31 +0100572 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200573 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200574 }
575
Radek Krejciba03a5a2020-08-27 14:40:41 +0200576 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100577 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200578
Radek Krejcica3db002018-11-01 10:31:01 +0100579run:
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200580 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->compiled->features); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +0100581 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200582 if (all || !strcmp(f->name, name)) {
583 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
584 if (all) {
585 /* skip already set features */
586 continue;
587 } else {
588 /* feature already set correctly */
589 ly_set_free(changed, NULL);
590 return LY_SUCCESS;
591 }
592 }
593
594 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200595 if (!skip_checks) {
596 /* check referenced features if they are enabled */
597 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
598 if (lysc_iffeature_value(iff) == LY_ENOT) {
599 if (all) {
600 ++disabled_count;
601 goto next;
602 } else {
603 LOGERR(ctx, LY_EDENIED,
604 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
605 f->name);
606 ly_set_free(changed, NULL);
607 return LY_EDENIED;
608 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200609 }
610 }
611 }
612 /* enable the feature */
613 f->flags |= LYS_FENABLED;
614 } else { /* disable */
615 /* disable the feature */
616 f->flags &= ~LYS_FENABLED;
617 }
618
619 /* remember the changed feature */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200620 LY_CHECK_RET(ly_set_add(changed, f, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci151a5b72018-10-19 14:21:44 +0200621
622 if (!all) {
623 /* stop in case changing a single feature */
624 break;
625 }
626 }
627next:
628 ;
629 }
630
631 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100632 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200633 ly_set_free(changed, NULL);
Michal Vasko82c31e62020-07-17 15:30:40 +0200634 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200635 }
636
Radek Krejcica3db002018-11-01 10:31:01 +0100637 if (value && all && disabled_count) {
638 if (changed_count == changed->count) {
639 /* no change in last run -> not able to enable all ... */
640 /* ... print errors */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200641 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->compiled->features); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +0100642 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100643 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100644 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100645 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100646 --disabled_count;
647 }
648 }
649 /* ... restore the original state */
650 for (u = 0; u < changed->count; ++u) {
651 f = changed->objs[u];
652 /* re-disable the feature */
653 f->flags &= ~LYS_FENABLED;
654 }
655
656 ly_set_free(changed, NULL);
657 return LY_EDENIED;
658 } else {
659 /* we did some change in last run, try it again */
660 changed_count = changed->count;
661 goto run;
662 }
663 }
664
Radek Krejci151a5b72018-10-19 14:21:44 +0200665 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200666 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200667 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
668 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
669 * is not done - by default, features are disabled and must be explicitely enabled. */
670 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200671 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200672 if (!((*df)->flags & LYS_FENABLED)) {
673 /* not enabled, nothing to do */
674 continue;
675 }
676 /* check the feature's if-features which could change by the previous change of our feature */
677 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200678 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200679 /* the feature must be disabled now */
680 (*df)->flags &= ~LYS_FENABLED;
681 /* add the feature into the list of changed features */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200682 LY_CHECK_RET(ly_set_add(changed, *df, LY_SET_OPT_USEASLIST, NULL));
Radek Krejci151a5b72018-10-19 14:21:44 +0200683 break;
684 }
685 }
686 }
687 }
688
689 ly_set_free(changed, NULL);
690 return LY_SUCCESS;
691}
692
693API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200694lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200695{
Radek Krejci0af46292019-01-11 16:02:31 +0100696 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200697
Michal Vasko22df3f02020-08-24 13:29:22 +0200698 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200699}
700
701API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200702lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200703{
Radek Krejci0af46292019-01-11 16:02:31 +0100704 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200705
Michal Vasko22df3f02020-08-24 13:29:22 +0200706 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200707}
708
Michal Vasko82c31e62020-07-17 15:30:40 +0200709API LY_ERR
710lys_feature_enable_force(const struct lys_module *module, const char *feature)
711{
712 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
713
Michal Vasko22df3f02020-08-24 13:29:22 +0200714 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200715}
716
717API LY_ERR
718lys_feature_disable_force(const struct lys_module *module, const char *feature)
719{
720 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
721
Michal Vasko22df3f02020-08-24 13:29:22 +0200722 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200723}
724
725API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200726lys_feature_value(const struct lys_module *module, const char *feature)
727{
Michal Vasko82c31e62020-07-17 15:30:40 +0200728 struct lysc_feature *f = NULL;
Radek Krejci151a5b72018-10-19 14:21:44 +0200729 struct lysc_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200730 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200731
732 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
733 mod = module->compiled;
734
735 /* search for the specified feature */
Michal Vasko82c31e62020-07-17 15:30:40 +0200736 LY_ARRAY_FOR(mod->features, u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200737 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200738 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200739 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200740 }
741 }
742
743 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200744 if (!f) {
745 return LY_ENOTFOUND;
746 }
747
748 /* feature disabled */
749 if (!(f->flags & LYS_FENABLED)) {
750 return LY_ENOT;
751 }
752
753 /* check referenced features if they are enabled */
754 LY_ARRAY_FOR(f->iffeatures, u) {
755 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
756 /* if-feature disabled */
757 return LY_ENOT;
758 }
759 }
760
761 /* feature enabled */
762 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200763}
764
Michal Vaskoc193ce92020-03-06 11:04:48 +0100765API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200766lysc_node_is_disabled(const struct lysc_node *node, uint8_t recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100767{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200768 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100769
770 LY_CHECK_ARG_RET(NULL, node, NULL);
771
Michal Vaskoc193ce92020-03-06 11:04:48 +0100772 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100773 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100774 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100775 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200776 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100777 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100778 }
779 }
780 }
781
782 if (!recursive) {
783 return NULL;
784 }
785
Michal Vaskoc193ce92020-03-06 11:04:48 +0100786 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100787 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100788 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
789
Radek Krejcia3045382018-11-22 14:30:31 +0100790 return NULL;
791}
792
Radek Krejci19cf8052020-08-18 15:02:38 +0200793API LY_ERR
Michal Vasko22df3f02020-08-24 13:29:22 +0200794lysc_node_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200795{
796 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
797
798 if (prev_priv_p) {
799 *prev_priv_p = node->priv;
800 }
801 ((struct lysc_node *)node)->priv = priv;
802
803 return LY_SUCCESS;
804}
805
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200806LY_ERR
807lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
808{
809 struct lys_module *m;
810
811 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
812
813 if (mod->implemented) {
814 return LY_SUCCESS;
815 }
816
817 /* we have module from the current context */
818 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
819 if (m) {
820 if (m != mod) {
821 /* check collision with other implemented revision */
822 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
823 mod->name, mod->revision ? mod->revision : "module without revision");
824 return LY_EDENIED;
825 } else {
826 /* mod is already implemented */
827 return LY_SUCCESS;
828 }
829 }
830
831 /* mark the module implemented, check for collision was already done */
832 mod->implemented = value;
833
834 /* compile the schema */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200835 LY_CHECK_RET(lys_compile(&mod, LYSC_OPT_INTERNAL));
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200836
837 return LY_SUCCESS;
838}
839
840API LY_ERR
841lys_set_implemented(struct lys_module *mod)
842{
843 return lys_set_implemented_internal(mod, 1);
844}
845
Michal Vasko7c8439f2020-08-05 13:25:19 +0200846static LY_ERR
847lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
848{
849 struct lysp_import *imp;
850 struct lysp_include *inc;
851 LY_ARRAY_COUNT_TYPE u, v;
852
853 modp->parsing = 1;
854 LY_ARRAY_FOR(modp->imports, u) {
855 imp = &modp->imports[u];
856 if (!imp->module) {
857 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
858 }
859 /* check for importing the same module twice */
860 for (v = 0; v < u; ++v) {
861 if (imp->module == modp->imports[v].module) {
862 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
863 }
864 }
865 }
866 LY_ARRAY_FOR(modp->includes, u) {
867 inc = &modp->includes[u];
868 if (!inc->submodule) {
869 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
870 }
871 }
872 modp->parsing = 0;
873
874 return LY_SUCCESS;
875}
876
Michal Vasko3a41dff2020-07-15 14:30:28 +0200877LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200878lys_parse_mem_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 +0200879 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200880 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200881{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100883 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100884 struct lys_yang_parser_ctx *yangctx = NULL;
885 struct lys_yin_parser_ctx *yinctx = NULL;
886 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100887
Michal Vasko3a41dff2020-07-15 14:30:28 +0200888 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100889
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100890 switch (format) {
891 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200892 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100893 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100894 break;
895 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200896 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100897 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 break;
899 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200900 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200901 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200903 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200904 LY_CHECK_GOTO(ret, error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905
906 /* make sure that the newest revision is at position 0 */
907 lysp_sort_revisions(submod->revs);
908
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100909 /* decide the latest revision */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200910 latest_sp = ly_ctx_get_submodule(ctx, submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 if (latest_sp) {
912 if (submod->revs) {
913 if (!latest_sp->revs) {
914 /* latest has no revision, so mod is anyway newer */
915 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200916 /* the latest_sp is zeroed later when the new module is being inserted into the context */
917 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
918 submod->latest_revision = latest_sp->latest_revision;
919 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200921 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200923 } else {
924 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 }
926 } else {
927 submod->latest_revision = 1;
928 }
929
Radek Krejcib3289d62019-09-18 12:21:39 +0200930 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200931 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200932 }
933
934 if (latest_sp) {
935 latest_sp->latest_revision = 0;
936 }
937
Michal Vasko7c8439f2020-08-05 13:25:19 +0200938 /* resolve imports and includes */
939 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
940
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100942 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
943 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100944
David Sedlák1b623122019-08-05 15:27:49 +0200945 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100946 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200947 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100948 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200949 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200950 *submodule = submod;
951 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200952
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100953error:
954 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200955 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100956 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200957 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100958 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200959 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200960 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200961}
962
Michal Vasko3a41dff2020-07-15 14:30:28 +0200963LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200964lys_parse_mem_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, uint8_t implement,
965 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
966 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200967{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100968 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200969 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200970 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100971 struct lys_yang_parser_ctx *yangctx = NULL;
972 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200973 struct lys_parser_ctx *pctx = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200974
Michal Vasko3a41dff2020-07-15 14:30:28 +0200975 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200976
977 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200978 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100979 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200980
981 switch (format) {
982 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100984 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200985 break;
986 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200987 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100988 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200989 break;
990 default:
991 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200992 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200993 break;
994 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200995 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200996
997 /* make sure that the newest revision is at position 0 */
998 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100999 if (mod->parsed->revs) {
1000 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
1001 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001002
Radek Krejcib3289d62019-09-18 12:21:39 +02001003 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001004 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001005 if (latest) {
1006 if (mod->revision) {
1007 if (!latest->revision) {
1008 /* latest has no revision, so mod is anyway newer */
1009 mod->latest_revision = latest->latest_revision;
1010 /* the latest is zeroed later when the new module is being inserted into the context */
1011 } else if (strcmp(mod->revision, latest->revision) > 0) {
1012 mod->latest_revision = latest->latest_revision;
1013 /* the latest is zeroed later when the new module is being inserted into the context */
1014 } else {
1015 latest = NULL;
1016 }
1017 } else {
1018 latest = NULL;
1019 }
1020 } else {
1021 mod->latest_revision = 1;
1022 }
1023
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001024 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001025 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001026 }
1027
Radek Krejci86d106e2018-10-18 09:53:19 +02001028 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001029 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001030 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1031 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001032 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001033 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001034 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001035 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +02001036 }
1037
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001038 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001039 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001040 if (mod_dup) {
1041 if (mod_dup->parsed) {
1042 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001043 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
1045 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001046 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001047 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
1048 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001049 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001050 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001051 goto error;
1052 } else {
1053 /* add the parsed data to the currently compiled-only module in the context */
1054 mod_dup->parsed = mod->parsed;
1055 mod_dup->parsed->mod = mod_dup;
1056 mod->parsed = NULL;
1057 lys_module_free(mod, NULL);
1058 mod = mod_dup;
1059 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001060 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001061 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001062
Radek Krejci0af46292019-01-11 16:02:31 +01001063 if (!mod->implemented) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001064 /* pre-compile features and identities of the module */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001065 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->dis_features), error);
1066 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->identities, &mod->dis_identities), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001067 }
1068
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001069 if (latest) {
Radek Krejcib3289d62019-09-18 12:21:39 +02001070 latest->latest_revision = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001071 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001072
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001073 /* add into context */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001074 ret = ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST, NULL);
1075 LY_CHECK_GOTO(ret, error);
Radek Krejcia46012b2020-08-12 15:41:04 +02001076 ctx->module_set_id++;
Radek Krejcid33273d2018-10-25 14:55:52 +02001077
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001078finish_parsing:
Michal Vasko7c8439f2020-08-05 13:25:19 +02001079 /* resolve imports and includes */
1080 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1081
1082 if (!mod->implemented) {
1083 /* pre-compile features and identities of any submodules */
1084 LY_ARRAY_FOR(mod->parsed->includes, u) {
1085 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->features,
1086 &mod->dis_features), error);
1087 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->identities,
1088 &mod->dis_identities), error);
Radek Krejci086c7132018-10-26 15:29:04 +02001089 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001090 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001091
Radek Krejci7fc68292019-06-12 13:51:09 +02001092 /* check name collisions - typedefs and TODO groupings */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001093 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +02001094
David Sedlák1b623122019-08-05 15:27:49 +02001095 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001096 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001097 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001098 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001099 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001100 *module = mod;
1101 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001102
1103error_ctx:
1104 ly_set_rm(&ctx->list, mod, NULL);
1105error:
1106 lys_module_free(mod, NULL);
Radek Krejcif6923e82020-07-02 16:36:53 +02001107 if (pctx) {
1108 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1109 }
David Sedlák1b623122019-08-05 15:27:49 +02001110 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001111 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001112 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001113 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001114 }
1115
Michal Vasko3a41dff2020-07-15 14:30:28 +02001116 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001117}
1118
Michal Vasko3a41dff2020-07-15 14:30:28 +02001119API LY_ERR
1120lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001121{
Radek Krejci096235c2019-01-11 11:12:19 +01001122 struct lys_module *mod;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001123 char *filename, *rev, *dot;
1124 size_t len;
Radek Krejci096235c2019-01-11 11:12:19 +01001125
Michal Vasko3a41dff2020-07-15 14:30:28 +02001126 if (module) {
1127 *module = NULL;
1128 }
1129 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001130
Michal Vasko63f3d842020-07-08 10:10:14 +02001131 /* remember input position */
1132 in->func_start = in->current;
1133
Michal Vasko3a41dff2020-07-15 14:30:28 +02001134 LY_CHECK_RET(lys_parse_mem_module(ctx, in, format, 1, NULL, NULL, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +01001135
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001136 switch (in->type) {
1137 case LY_IN_FILEPATH:
1138 /* check that name and revision match filename */
1139 filename = strrchr(in->method.fpath.filepath, '/');
1140 if (!filename) {
1141 filename = in->method.fpath.filepath;
1142 } else {
1143 filename++;
1144 }
1145 rev = strchr(filename, '@');
1146 dot = strrchr(filename, '.');
1147
1148 /* name */
1149 len = strlen(mod->name);
1150 if (strncmp(filename, mod->name, len) ||
1151 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
1152 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1153 }
1154 if (rev) {
1155 len = dot - ++rev;
1156 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
1157 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
1158 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
1159 }
1160 }
1161
1162 break;
1163 case LY_IN_FD:
1164 case LY_IN_FILE:
1165 case LY_IN_MEMORY:
1166 /* nothing special to do */
1167 break;
1168 default:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001169 LOGINT_RET(ctx);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001170 break;
Radek Krejci096235c2019-01-11 11:12:19 +01001171 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001172
1173 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001174 LY_CHECK_RET(lys_compile(&mod, 0));
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001175
Michal Vasko3a41dff2020-07-15 14:30:28 +02001176 if (module) {
1177 *module = mod;
1178 }
1179 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +02001180}
1181
Michal Vasko3a41dff2020-07-15 14:30:28 +02001182API LY_ERR
1183lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001184{
Radek Krejci0f969882020-08-21 16:56:47 +02001185 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001186 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001187
Michal Vasko3a41dff2020-07-15 14:30:28 +02001188 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001189
Michal Vasko3a41dff2020-07-15 14:30:28 +02001190 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 +02001191
Michal Vasko3a41dff2020-07-15 14:30:28 +02001192 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001193 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001194
Michal Vasko3a41dff2020-07-15 14:30:28 +02001195 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001196}
1197
Michal Vasko3a41dff2020-07-15 14:30:28 +02001198API LY_ERR
1199lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001200{
Radek Krejci0f969882020-08-21 16:56:47 +02001201 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001202 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001203
Michal Vasko3a41dff2020-07-15 14:30:28 +02001204 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001205
Michal Vasko3a41dff2020-07-15 14:30:28 +02001206 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 +02001207
Michal Vasko3a41dff2020-07-15 14:30:28 +02001208 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001209 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001210
Michal Vasko3a41dff2020-07-15 14:30:28 +02001211 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001212}
1213
Michal Vasko3a41dff2020-07-15 14:30:28 +02001214API LY_ERR
1215lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001216{
Radek Krejci0f969882020-08-21 16:56:47 +02001217 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001218 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001219
Michal Vasko3a41dff2020-07-15 14:30:28 +02001220 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001221
Michal Vasko3a41dff2020-07-15 14:30:28 +02001222 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
1223 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +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);
1227
Michal Vasko3a41dff2020-07-15 14:30:28 +02001228 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001229}
1230
1231API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02001232lys_search_localfile(const char * const *searchpaths, uint8_t cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001233 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001234{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001235 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001236 size_t len, flen, match_len = 0, dir_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001237 uint8_t implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001238 char *wd, *wn = NULL;
1239 DIR *dir = NULL;
1240 struct dirent *file;
1241 char *match_name = NULL;
1242 LYS_INFORMAT format_aux, match_format = 0;
1243 struct ly_set *dirs;
1244 struct stat st;
1245
1246 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1247
1248 /* start to fill the dir fifo with the context's search path (if set)
1249 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001250 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001251
1252 len = strlen(name);
1253 if (cwd) {
1254 wd = get_current_dir_name();
1255 if (!wd) {
1256 LOGMEM(NULL);
1257 goto cleanup;
1258 } else {
1259 /* add implicit current working directory (./) to be searched,
1260 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001261 ret = ly_set_add(dirs, wd, 0, NULL);
1262 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001263 implicit_cwd = 1;
1264 }
1265 }
1266 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001267 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001268 /* check for duplicities with the implicit current working directory */
1269 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1270 implicit_cwd = 0;
1271 continue;
1272 }
1273 wd = strdup(searchpaths[i]);
1274 if (!wd) {
1275 LOGMEM(NULL);
1276 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001277 } else {
1278 ret = ly_set_add(dirs, wd, 0, NULL);
1279 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001280 }
1281 }
1282 }
1283 wd = NULL;
1284
1285 /* start searching */
1286 while (dirs->count) {
1287 free(wd);
1288 free(wn); wn = NULL;
1289
1290 dirs->count--;
1291 wd = (char *)dirs->objs[dirs->count];
1292 dirs->objs[dirs->count] = NULL;
1293 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1294
1295 if (dir) {
1296 closedir(dir);
1297 }
1298 dir = opendir(wd);
1299 dir_len = strlen(wd);
1300 if (!dir) {
1301 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1302 } else {
1303 while ((file = readdir(dir))) {
1304 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1305 /* skip . and .. */
1306 continue;
1307 }
1308 free(wn);
1309 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1310 LOGMEM(NULL);
1311 goto cleanup;
1312 }
1313 if (stat(wn, &st) == -1) {
1314 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1315 file->d_name, wd, strerror(errno));
1316 continue;
1317 }
1318 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1319 /* we have another subdirectory in searchpath to explore,
1320 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001321 ret = ly_set_add(dirs, wn, 0, NULL);
1322 LY_CHECK_GOTO(ret, cleanup);
1323
Radek Krejcid33273d2018-10-25 14:55:52 +02001324 /* continue with the next item in current directory */
1325 wn = NULL;
1326 continue;
1327 } else if (!S_ISREG(st.st_mode)) {
1328 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1329 continue;
1330 }
1331
1332 /* here we know that the item is a file which can contain a module */
1333 if (strncmp(name, file->d_name, len) ||
1334 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1335 /* different filename than the module we search for */
1336 continue;
1337 }
1338
1339 /* get type according to filename suffix */
1340 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001341 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001342 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001343 /* TODO YIN parser
1344 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1345 format_aux = LYS_IN_YIN;
1346 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001347 } else {
1348 /* not supportde suffix/file format */
1349 continue;
1350 }
1351
1352 if (revision) {
1353 /* we look for the specific revision, try to get it from the filename */
1354 if (file->d_name[len] == '@') {
1355 /* check revision from the filename */
1356 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1357 /* another revision */
1358 continue;
1359 } else {
1360 /* exact revision */
1361 free(match_name);
1362 match_name = wn;
1363 wn = NULL;
1364 match_len = dir_len + 1 + len;
1365 match_format = format_aux;
1366 goto success;
1367 }
1368 } else {
1369 /* continue trying to find exact revision match, use this only if not found */
1370 free(match_name);
1371 match_name = wn;
1372 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001373 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001374 match_format = format_aux;
1375 continue;
1376 }
1377 } else {
1378 /* remember the revision and try to find the newest one */
1379 if (match_name) {
1380 if (file->d_name[len] != '@' ||
1381 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1382 continue;
1383 } else if (match_name[match_len] == '@' &&
1384 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1385 continue;
1386 }
1387 free(match_name);
1388 }
1389
1390 match_name = wn;
1391 wn = NULL;
1392 match_len = dir_len + 1 + len;
1393 match_format = format_aux;
1394 continue;
1395 }
1396 }
1397 }
1398 }
1399
1400success:
1401 (*localfile) = match_name;
1402 match_name = NULL;
1403 if (format) {
1404 (*format) = match_format;
1405 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001406 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001407
1408cleanup:
1409 free(wn);
1410 free(wd);
1411 if (dir) {
1412 closedir(dir);
1413 }
1414 free(match_name);
1415 ly_set_free(dirs, free);
1416
1417 return ret;
1418}