blob: 1b9433c279c5d92d76d7a1aec26bc40f0d6297ac [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 */
Michal Vasko69730152020-10-09 16:30:07 +020061 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:
120 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100121 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200122 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100123 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200124 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100125 } else if (!action_flag) {
126 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200127 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100128 } else if (!notif_flag) {
129 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200130 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100131 } else {
132 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100133 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100134 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100135 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100136check:
Radek Krejcia3045382018-11-22 14:30:31 +0100137 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100138 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100139 case LYS_ACTION:
140 case LYS_NOTIF:
141 case LYS_LEAF:
142 case LYS_ANYXML:
143 case LYS_ANYDATA:
144 case LYS_LIST:
145 case LYS_LEAFLIST:
146 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200147 case LYS_CASE:
148 if (options & LYS_GETNEXT_WITHCASE) {
149 break;
150 } else {
151 /* go into */
152 next = ((struct lysc_node_case *)next)->child;
153 }
154 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100155 case LYS_CONTAINER:
156 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
157 if (((struct lysc_node_container *)next)->child) {
158 /* go into */
159 next = ((struct lysc_node_container *)next)->child;
160 } else {
161 next = next->next;
162 }
163 goto repeat;
164 }
165 break;
166 case LYS_CHOICE:
167 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200168 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100169 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
170 next = next->next;
171 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100172 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100173 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200174 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100175 } else {
176 next = ((struct lysc_node_choice *)next)->cases->child;
177 }
Radek Krejcia3045382018-11-22 14:30:31 +0100178 }
179 goto repeat;
180 default:
181 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200182 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100183 return NULL;
184 }
185
186 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
187 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200188 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100189 next = next->next;
190 goto repeat;
191 }
192 }
193 return next;
194}
195
196API const struct lysc_node *
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800197lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath)
198{
199 const char *id = qpath;
200 const char *prefix, *name;
201 size_t prefix_len, name_len;
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800202 const struct lysc_node *node = context_node;
203 struct lys_module *mod = NULL;
204
205 LY_CHECK_ARG_RET(ctx, qpath, NULL);
206
Michal Vaskod989ba02020-08-24 10:59:24 +0200207 while (*id) {
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800208 if (id[0] == '/') {
209 ++id;
210 }
211 /* precess ".." in relative paths */
212 while (!strncmp("../", id, 3)) {
213 id += 3;
214 if (!node) {
215 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath);
216 return NULL;
217 }
218 node = node->parent;
219 }
220
221 if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) {
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200222 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id - qpath, qpath);
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800223 return NULL;
224 }
225 if (prefix) {
226 if (context_node) {
227 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
228 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200229 for (uint32_t u = 0; u < ctx->list.count; ++u) {
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800230 if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) {
231 struct lys_module *m = (struct lys_module *)ctx->list.objs[u];
232 if (mod) {
233 if (m->implemented) {
234 mod = m;
235 break;
236 } else if (m->latest_revision) {
237 mod = m;
238 }
239 } else {
240 mod = m;
241 }
242 }
243 }
244 }
245 }
246 if (!mod) {
247 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200248 id - qpath, qpath);
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800249 return NULL;
250 }
251
252 node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
253 if (!node) {
254 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath);
255 return NULL;
256 }
257 }
258
259 return node;
260}
261
262API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100263lys_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 +0200264 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100265{
266 const struct lysc_node *node = NULL;
267
268 LY_CHECK_ARG_RET(NULL, module, name, NULL);
269 if (!nodetype) {
270 nodetype = 0xffff;
271 }
272
273 while ((node = lys_getnext(node, parent, module->compiled, options))) {
274 if (!(node->nodetype & nodetype)) {
275 continue;
276 }
277 if (node->module != module) {
278 continue;
279 }
280
281 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200282 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100283 return node;
284 }
285 } else {
286 if (!strcmp(node->name, name)) {
287 return node;
288 }
289 }
290 }
291 return NULL;
292}
293
Michal Vasko519fd602020-05-26 12:17:39 +0200294API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200295lys_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 +0200296{
297 LY_ERR ret = LY_SUCCESS;
298 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200299 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200300 uint32_t i;
301
302 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
303 if (!(options & LYXP_SCNODE_ALL)) {
304 options = LYXP_SCNODE;
305 }
306
307 memset(&xp_set, 0, sizeof xp_set);
308
309 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200310 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
311 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200312
313 /* atomize expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200314 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 +0200315 LY_CHECK_GOTO(ret, cleanup);
316
317 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200318 ret = ly_set_new(set);
319 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200320
321 /* transform into ly_set */
322 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
323 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
324 (*set)->size = xp_set.used;
325
326 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200327 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejciba03a5a2020-08-27 14:40:41 +0200328 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL);
329 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200330 }
331 }
332
333cleanup:
334 lyxp_set_free_content(&xp_set);
335 lyxp_expr_free(ctx_node->module->ctx, exp);
336 return ret;
337}
338
Michal Vasko072de482020-08-05 13:27:21 +0200339API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200340lys_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 +0200341{
342 LY_ERR ret = LY_SUCCESS;
343 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200344 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200345 uint32_t i;
346
347 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
348 if (!(options & LYXP_SCNODE_ALL)) {
349 options = LYXP_SCNODE;
350 }
351
352 memset(&xp_set, 0, sizeof xp_set);
353
354 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200355 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
356 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200357
358 /* atomize expression */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200359 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 +0200360 LY_CHECK_GOTO(ret, cleanup);
361
362 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200363 ret = ly_set_new(set);
364 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200365
366 /* transform into ly_set */
367 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
368 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
369 (*set)->size = xp_set.used;
370
371 for (i = 0; i < xp_set.used; ++i) {
372 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 +0200373 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST, NULL);
374 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200375 }
376 }
377
378cleanup:
379 lyxp_set_free_content(&xp_set);
380 lyxp_expr_free(ctx_node->module->ctx, exp);
381 return ret;
382}
383
Michal Vasko14654712020-02-06 08:35:21 +0100384char *
385lysc_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 +0200386 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200387{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200389 char *path = NULL;
390 int len = 0;
391
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200392 LY_CHECK_ARG_RET(NULL, node, NULL);
393 if (buffer) {
394 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
395 }
396
Radek Krejci327de162019-06-14 12:52:07 +0200397 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200398 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200399 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100400 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200401 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100402 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200403
Michal Vasko65de0402020-08-03 16:34:19 +0200404 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
405 /* schema-only node */
406 continue;
407 }
408
Michal Vasko11deea12020-08-05 13:54:50 +0200409 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200410 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100411 if (parent && (iter->parent == parent)) {
412 slash = "";
413 } else {
414 slash = "/";
415 }
Michal Vasko69730152020-10-09 16:30:07 +0200416 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200417 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200418 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100419 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200420 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100421 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200422 }
Radek Krejci327de162019-06-14 12:52:07 +0200423 } else {
424 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200425 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100426 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200427 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100428 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200429 }
Radek Krejci327de162019-06-14 12:52:07 +0200430 }
431 free(s);
432 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200433
Michal Vasko69730152020-10-09 16:30:07 +0200434 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200435 /* not enough space in buffer */
436 break;
437 }
Radek Krejci327de162019-06-14 12:52:07 +0200438 }
439
440 if (len < 0) {
441 free(path);
442 path = NULL;
443 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200444 if (buffer) {
445 strcpy(buffer, "/");
446 } else {
447 path = strdup("/");
448 }
Radek Krejci327de162019-06-14 12:52:07 +0200449 }
450 break;
451 }
452
Radek Krejci1c0c3442019-07-23 16:08:47 +0200453 if (buffer) {
454 return buffer;
455 } else {
456 return path;
457 }
Radek Krejci327de162019-06-14 12:52:07 +0200458}
459
Michal Vasko14654712020-02-06 08:35:21 +0100460API char *
461lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
462{
463 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
464}
465
Michal Vasko28d78432020-05-26 13:10:53 +0200466API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100467lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200468{
Michal Vasko28d78432020-05-26 13:10:53 +0200469 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
470 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200471}
472
Radek Krejci693262f2019-04-29 15:23:20 +0200473uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200474lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200475{
476 uint8_t *item;
477 uint8_t mask = 3, result;
478
Radek Krejci151a5b72018-10-19 14:21:44 +0200479 item = &list[pos / 4];
480 result = (*item) & (mask << 2 * (pos % 4));
481 return result >> 2 * (pos % 4);
482}
483
Michal Vasko28d78432020-05-26 13:10:53 +0200484static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200485lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200486{
487 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200488 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200489
Radek Krejci693262f2019-04-29 15:23:20 +0200490 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200491 (*index_e)++;
492
493 switch (op) {
494 case LYS_IFF_F:
495 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200496 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200497 case LYS_IFF_NOT:
498 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200499 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200500 case LYS_IFF_AND:
501 case LYS_IFF_OR:
502 a = lysc_iffeature_value_(iff, index_e, index_f);
503 b = lysc_iffeature_value_(iff, index_e, index_f);
504 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200505 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
506 return LY_SUCCESS;
507 } else {
508 return LY_ENOT;
509 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200510 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200511 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
512 return LY_SUCCESS;
513 } else {
514 return LY_ENOT;
515 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200516 }
517 }
518
519 return 0;
520}
521
Michal Vasko28d78432020-05-26 13:10:53 +0200522API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200523lysc_iffeature_value(const struct lysc_iffeature *iff)
524{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200525 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200526
527 LY_CHECK_ARG_RET(NULL, iff, -1);
528
529 if (iff->expr) {
530 return lysc_iffeature_value_(iff, &index_e, &index_f);
531 }
532 return 0;
533}
534
Radek Krejci151a5b72018-10-19 14:21:44 +0200535/**
536 * @brief Enable/Disable the specified feature in the module.
537 *
538 * If the feature is already set to the desired value, LY_SUCCESS is returned.
539 * By changing the feature, also all the feature which depends on it via their
540 * if-feature statements are again evaluated (disabled if a if-feature statemen
541 * evaluates to false).
542 *
Radek Krejci0af46292019-01-11 16:02:31 +0100543 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200544 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
545 * set all the features in the module.
546 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
Radek Krejci857189e2020-09-01 13:26:36 +0200547 * @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 +0200548 * @return LY_ERR value.
549 */
550static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200551lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200552{
Michal Vaskob0099a92020-08-31 14:55:23 +0200553 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200554 ly_bool all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200555 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200556 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200557 struct lysc_feature *f, **df;
558 struct lysc_iffeature *iff;
559 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100560 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200561
Radek Krejci6e67c402019-05-02 09:55:39 +0200562 if (!strcmp(name, "*")) {
563 /* enable all */
564 all = 1;
565 }
566
Radek Krejci0af46292019-01-11 16:02:31 +0100567 if (!mod->compiled) {
568 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
Michal Vasko69730152020-10-09 16:30:07 +0200569 mod->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100570 return LY_EINVAL;
571 }
Radek Krejci14915cc2020-09-14 17:28:13 +0200572 if (!mod->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200573 if (all) {
574 /* no feature to enable */
575 return LY_SUCCESS;
576 }
Radek Krejci0af46292019-01-11 16:02:31 +0100577 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200578 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200579 }
580
Radek Krejciba03a5a2020-08-27 14:40:41 +0200581 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100582 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200583
Radek Krejcica3db002018-11-01 10:31:01 +0100584run:
Radek Krejci14915cc2020-09-14 17:28:13 +0200585 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) {
586 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200587 if (all || !strcmp(f->name, name)) {
588 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
589 if (all) {
590 /* skip already set features */
591 continue;
592 } else {
593 /* feature already set correctly */
Michal Vaskob0099a92020-08-31 14:55:23 +0200594 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200595 }
596 }
597
598 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200599 if (!skip_checks) {
600 /* check referenced features if they are enabled */
601 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
602 if (lysc_iffeature_value(iff) == LY_ENOT) {
603 if (all) {
604 ++disabled_count;
605 goto next;
606 } else {
607 LOGERR(ctx, LY_EDENIED,
Michal Vasko69730152020-10-09 16:30:07 +0200608 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
609 f->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200610 ret = LY_EDENIED;
611 goto cleanup;
Michal Vasko82c31e62020-07-17 15:30:40 +0200612 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200613 }
614 }
615 }
616 /* enable the feature */
617 f->flags |= LYS_FENABLED;
618 } else { /* disable */
619 /* disable the feature */
620 f->flags &= ~LYS_FENABLED;
621 }
622
623 /* remember the changed feature */
Michal Vaskob0099a92020-08-31 14:55:23 +0200624 ret = ly_set_add(changed, f, LY_SET_OPT_USEASLIST, NULL);
625 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200626
627 if (!all) {
628 /* stop in case changing a single feature */
629 break;
630 }
631 }
632next:
633 ;
634 }
635
636 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100637 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200638 ret = LY_ENOTFOUND;
639 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200640 }
641
Radek Krejcica3db002018-11-01 10:31:01 +0100642 if (value && all && disabled_count) {
643 if (changed_count == changed->count) {
644 /* no change in last run -> not able to enable all ... */
645 /* ... print errors */
Radek Krejci14915cc2020-09-14 17:28:13 +0200646 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) {
647 if (!(mod->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100648 LOGERR(ctx, LY_EDENIED,
Michal Vasko69730152020-10-09 16:30:07 +0200649 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
650 mod->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100651 --disabled_count;
652 }
653 }
654 /* ... restore the original state */
655 for (u = 0; u < changed->count; ++u) {
656 f = changed->objs[u];
657 /* re-disable the feature */
658 f->flags &= ~LYS_FENABLED;
659 }
660
Michal Vaskob0099a92020-08-31 14:55:23 +0200661 ret = LY_EDENIED;
662 goto cleanup;
Radek Krejcica3db002018-11-01 10:31:01 +0100663 } else {
664 /* we did some change in last run, try it again */
665 changed_count = changed->count;
666 goto run;
667 }
668 }
669
Radek Krejci151a5b72018-10-19 14:21:44 +0200670 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200671 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200672 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
673 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
674 * is not done - by default, features are disabled and must be explicitely enabled. */
675 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200676 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200677 if (!((*df)->flags & LYS_FENABLED)) {
678 /* not enabled, nothing to do */
679 continue;
680 }
681 /* check the feature's if-features which could change by the previous change of our feature */
682 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200683 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200684 /* the feature must be disabled now */
685 (*df)->flags &= ~LYS_FENABLED;
686 /* add the feature into the list of changed features */
Michal Vaskob0099a92020-08-31 14:55:23 +0200687 ret = ly_set_add(changed, *df, LY_SET_OPT_USEASLIST, NULL);
688 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200689 break;
690 }
691 }
692 }
693 }
694
Michal Vasko87a97f42020-10-06 10:30:28 +0200695 /* success */
696 ++mod->ctx->module_set_id;
697
Michal Vaskob0099a92020-08-31 14:55:23 +0200698cleanup:
Radek Krejci151a5b72018-10-19 14:21:44 +0200699 ly_set_free(changed, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200700 return ret;
Radek Krejci151a5b72018-10-19 14:21:44 +0200701}
702
703API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200704lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200705{
Radek Krejci0af46292019-01-11 16:02:31 +0100706 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200707
Michal Vasko22df3f02020-08-24 13:29:22 +0200708 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200709}
710
711API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200712lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200713{
Radek Krejci0af46292019-01-11 16:02:31 +0100714 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200715
Michal Vasko22df3f02020-08-24 13:29:22 +0200716 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200717}
718
Michal Vasko82c31e62020-07-17 15:30:40 +0200719API LY_ERR
720lys_feature_enable_force(const struct lys_module *module, const char *feature)
721{
722 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
723
Michal Vasko22df3f02020-08-24 13:29:22 +0200724 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200725}
726
727API LY_ERR
728lys_feature_disable_force(const struct lys_module *module, const char *feature)
729{
730 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
731
Michal Vasko22df3f02020-08-24 13:29:22 +0200732 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200733}
734
735API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200736lys_feature_value(const struct lys_module *module, const char *feature)
737{
Michal Vasko82c31e62020-07-17 15:30:40 +0200738 struct lysc_feature *f = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200739 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200740
741 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200742
743 /* search for the specified feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200744 LY_ARRAY_FOR(module->features, u) {
745 f = &module->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200746 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200747 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200748 }
749 }
750
751 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200752 if (!f) {
753 return LY_ENOTFOUND;
754 }
755
756 /* feature disabled */
757 if (!(f->flags & LYS_FENABLED)) {
758 return LY_ENOT;
759 }
760
761 /* check referenced features if they are enabled */
762 LY_ARRAY_FOR(f->iffeatures, u) {
763 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
764 /* if-feature disabled */
765 return LY_ENOT;
766 }
767 }
768
769 /* feature enabled */
770 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200771}
772
Michal Vaskoc193ce92020-03-06 11:04:48 +0100773API const struct lysc_node *
Radek Krejci857189e2020-09-01 13:26:36 +0200774lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100775{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200776 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100777
778 LY_CHECK_ARG_RET(NULL, node, NULL);
779
Michal Vaskoc193ce92020-03-06 11:04:48 +0100780 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100781 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100782 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100783 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200784 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100785 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100786 }
787 }
788 }
789
790 if (!recursive) {
791 return NULL;
792 }
793
Michal Vaskoc193ce92020-03-06 11:04:48 +0100794 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100795 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100796 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
797
Radek Krejcia3045382018-11-22 14:30:31 +0100798 return NULL;
799}
800
Radek Krejci19cf8052020-08-18 15:02:38 +0200801API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200802lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200803{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200804 struct lysc_action *act;
805 struct lysc_notif *notif;
806
Radek Krejci19cf8052020-08-18 15:02:38 +0200807 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
808
Radek Krejciaf9cd802020-10-06 21:59:47 +0200809 switch (node->nodetype) {
810 case LYS_CONTAINER:
811 case LYS_CHOICE:
812 case LYS_CASE:
813 case LYS_LEAF:
814 case LYS_LEAFLIST:
815 case LYS_LIST:
816 case LYS_ANYXML:
817 case LYS_ANYDATA:
818 if (prev_priv_p) {
819 *prev_priv_p = node->priv;
820 }
821 ((struct lysc_node *)node)->priv = priv;
822 break;
823 case LYS_RPC:
824 case LYS_ACTION:
825 act = (struct lysc_action *)node;
826 if (prev_priv_p) {
827 *prev_priv_p = act->priv;
828 }
829 act->priv = priv;
830 break;
831 case LYS_NOTIF:
832 notif = (struct lysc_notif *)node;
833 if (prev_priv_p) {
834 *prev_priv_p = notif->priv;
835 }
836 notif->priv = priv;
837 break;
838 default:
839 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200840 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200841
842 return LY_SUCCESS;
843}
844
Michal Vasko89b5c072020-10-06 13:52:44 +0200845API LY_ERR
846lys_set_implemented(struct lys_module *mod)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200847{
Michal Vasko89b5c072020-10-06 13:52:44 +0200848 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200849 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200850 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200851
852 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
853
854 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200855 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200856 return LY_SUCCESS;
857 }
858
859 /* we have module from the current context */
860 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
861 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200862 assert(m != mod);
863
864 /* check collision with other implemented revision */
865 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
866 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
867 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200868 }
869
Michal Vasko89b5c072020-10-06 13:52:44 +0200870 /* add the module into newly implemented module set */
871 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, LY_SET_OPT_USEASLIST, NULL));
872
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200873 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200874 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200875
876 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200877 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200878
Michal Vasko89b5c072020-10-06 13:52:44 +0200879 if (mod == mod->ctx->implementing.objs[0]) {
880 /* the first module being implemented, consolidate the set */
881 if (ret) {
882 /* failure, full compile revert */
883 for (i = 0; i < mod->ctx->list.count; ++i) {
884 m = mod->ctx->list.objs[i];
885 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
886 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200887
Michal Vasko89b5c072020-10-06 13:52:44 +0200888 /* make the module correctly non-implemented again */
889 m->implemented = 0;
890 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
891 lys_precompile_augments_deviations_revert(mod->ctx, m);
892 }
893
894 /* free the compiled version of the module, if any */
895 lysc_module_free(m->compiled, NULL);
896 m->compiled = NULL;
897
898 if (m->implemented) {
899 /* recompile, must succeed because it was already compiled; hide messages because any
900 * warnings were already printed, are not really relevant, and would hide the real error */
901 uint32_t prev_lo = ly_log_options(0);
902 r = lys_compile(m, 0);
903 ly_log_options(prev_lo);
904 if (r) {
905 LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name);
906 }
907 }
908 }
909 }
910
911 ly_set_erase(&mod->ctx->implementing, NULL);
912 }
913 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200914}
915
Michal Vasko7c8439f2020-08-05 13:25:19 +0200916static LY_ERR
917lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
918{
919 struct lysp_import *imp;
920 struct lysp_include *inc;
921 LY_ARRAY_COUNT_TYPE u, v;
922
923 modp->parsing = 1;
924 LY_ARRAY_FOR(modp->imports, u) {
925 imp = &modp->imports[u];
926 if (!imp->module) {
927 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
928 }
929 /* check for importing the same module twice */
930 for (v = 0; v < u; ++v) {
931 if (imp->module == modp->imports[v].module) {
932 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
933 }
934 }
935 }
936 LY_ARRAY_FOR(modp->includes, u) {
937 inc = &modp->includes[u];
938 if (!inc->submodule) {
939 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
940 }
941 }
942 modp->parsing = 0;
943
944 return LY_SUCCESS;
945}
946
Michal Vasko3a41dff2020-07-15 14:30:28 +0200947LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200948lys_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 +0200949 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200950 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200951{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200952 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100953 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100954 struct lys_yang_parser_ctx *yangctx = NULL;
955 struct lys_yin_parser_ctx *yinctx = NULL;
956 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100957
Michal Vasko3a41dff2020-07-15 14:30:28 +0200958 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100959
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100960 switch (format) {
961 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200962 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100963 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100964 break;
965 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200966 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100967 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968 break;
969 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200970 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200971 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100972 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200973 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200974 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200975 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100976
977 /* make sure that the newest revision is at position 0 */
978 lysp_sort_revisions(submod->revs);
979
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100980 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200981 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100982 if (latest_sp) {
983 if (submod->revs) {
984 if (!latest_sp->revs) {
985 /* latest has no revision, so mod is anyway newer */
986 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200987 /* the latest_sp is zeroed later when the new module is being inserted into the context */
988 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
989 submod->latest_revision = latest_sp->latest_revision;
990 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100991 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200992 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100993 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200994 } else {
995 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100996 }
997 } else {
998 submod->latest_revision = 1;
999 }
1000
Radek Krejcib3289d62019-09-18 12:21:39 +02001001 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001002 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +02001003 }
1004
1005 if (latest_sp) {
1006 latest_sp->latest_revision = 0;
1007 }
1008
Michal Vasko7a0b0762020-09-02 16:37:01 +02001009 lys_parser_fill_filepath(ctx, in, &submod->filepath);
1010
Michal Vasko7c8439f2020-08-05 13:25:19 +02001011 /* resolve imports and includes */
1012 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
1013
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001014 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +01001015 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
1016 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001017
David Sedlák1b623122019-08-05 15:27:49 +02001018 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001019 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001020 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001021 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001022 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001023 *submodule = submod;
1024 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +02001025
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001026error:
1027 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +02001028 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001029 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001030 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001031 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001032 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001033 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001034}
1035
Michal Vasko3a41dff2020-07-15 14:30:28 +02001036LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001037lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001038 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
1039 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001040{
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001041 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001042 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001043 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001044 struct lys_yang_parser_ctx *yangctx = NULL;
1045 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001046 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001047 char *filename, *rev, *dot;
1048 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +02001049
Michal Vasko3a41dff2020-07-15 14:30:28 +02001050 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001051 if (module) {
1052 *module = NULL;
1053 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001054
1055 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001056 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001057 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001058
1059 switch (format) {
1060 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001061 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001062 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001063 break;
1064 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001065 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001066 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001067 break;
1068 default:
1069 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001070 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001071 break;
1072 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001073 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001074
1075 /* make sure that the newest revision is at position 0 */
1076 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001077 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001078 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001079 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001080
Radek Krejcib3289d62019-09-18 12:21:39 +02001081 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001082 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001083 if (latest) {
1084 if (mod->revision) {
1085 if (!latest->revision) {
1086 /* latest has no revision, so mod is anyway newer */
1087 mod->latest_revision = latest->latest_revision;
1088 /* the latest is zeroed later when the new module is being inserted into the context */
1089 } else if (strcmp(mod->revision, latest->revision) > 0) {
1090 mod->latest_revision = latest->latest_revision;
1091 /* the latest is zeroed later when the new module is being inserted into the context */
1092 } else {
1093 latest = NULL;
1094 }
1095 } else {
1096 latest = NULL;
1097 }
1098 } else {
1099 mod->latest_revision = 1;
1100 }
1101
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001102 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001103 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001104 }
1105
Radek Krejci86d106e2018-10-18 09:53:19 +02001106 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001107 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001108 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1109 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001110 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001111 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001112 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001113 }
1114
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001115 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001116 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001117 if (mod_dup) {
1118 if (mod_dup->parsed) {
1119 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001120 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001122 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001123 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001124 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001125 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001126 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001127 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001128 goto error;
1129 } else {
1130 /* add the parsed data to the currently compiled-only module in the context */
1131 mod_dup->parsed = mod->parsed;
1132 mod_dup->parsed->mod = mod_dup;
1133 mod->parsed = NULL;
1134 lys_module_free(mod, NULL);
1135 mod = mod_dup;
1136 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001137 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001138 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001139
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001140 switch (in->type) {
1141 case LY_IN_FILEPATH:
1142 /* check that name and revision match filename */
1143 filename = strrchr(in->method.fpath.filepath, '/');
1144 if (!filename) {
1145 filename = in->method.fpath.filepath;
1146 } else {
1147 filename++;
1148 }
1149 rev = strchr(filename, '@');
1150 dot = strrchr(filename, '.');
1151
1152 /* name */
1153 len = strlen(mod->name);
1154 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001155 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001156 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1157 }
1158 if (rev) {
1159 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001160 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001161 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001162 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001163 }
1164 }
1165
1166 break;
1167 case LY_IN_FD:
1168 case LY_IN_FILE:
1169 case LY_IN_MEMORY:
1170 /* nothing special to do */
1171 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001172 case LY_IN_ERROR:
1173 LOGINT(ctx);
1174 ret = LY_EINT;
1175 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001176 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001177
1178 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001179
Michal Vasko89b5c072020-10-06 13:52:44 +02001180 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001181 /* pre-compile features and identities of the module */
Radek Krejci14915cc2020-09-14 17:28:13 +02001182 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->features), error);
Radek Krejci80d281e2020-09-14 17:42:54 +02001183 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->identities, &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001184 }
1185
1186 if (latest) {
1187 latest->latest_revision = 0;
1188 }
1189
1190 /* add into context */
1191 ret = ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST, NULL);
1192 LY_CHECK_GOTO(ret, error);
1193 ctx->module_set_id++;
1194
1195finish_parsing:
1196 /* resolve imports and includes */
1197 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1198
Michal Vasko89b5c072020-10-06 13:52:44 +02001199 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001200 /* pre-compile features and identities of any submodules */
1201 LY_ARRAY_FOR(mod->parsed->includes, u) {
1202 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->features,
Michal Vasko69730152020-10-09 16:30:07 +02001203 &mod->features), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001204 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->identities,
Michal Vasko69730152020-10-09 16:30:07 +02001205 &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001206 }
1207 }
1208
1209 /* check name collisions - typedefs and TODO groupings */
1210 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1211
Michal Vasko89b5c072020-10-06 13:52:44 +02001212 if (implement) {
1213 /* implement (compile) */
1214 LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001215 }
1216
1217 if (format == LYS_IN_YANG) {
1218 yang_parser_ctx_free(yangctx);
1219 } else {
1220 yin_parser_ctx_free(yinctx);
1221 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001222 if (module) {
1223 *module = mod;
1224 }
1225 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001226
1227error_ctx:
1228 ly_set_rm(&ctx->list, mod, NULL);
1229error:
1230 lys_module_free(mod, NULL);
1231 if (pctx) {
1232 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1233 }
1234 if (format == LYS_IN_YANG) {
1235 yang_parser_ctx_free(yangctx);
1236 } else {
1237 yin_parser_ctx_free(yinctx);
1238 }
1239
1240 return ret;
1241}
1242
1243API LY_ERR
1244lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1245{
1246 if (module) {
1247 *module = NULL;
1248 }
1249 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1250
1251 /* remember input position */
1252 in->func_start = in->current;
1253
1254 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001255}
1256
Michal Vasko3a41dff2020-07-15 14:30:28 +02001257API LY_ERR
1258lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001259{
Radek Krejci0f969882020-08-21 16:56:47 +02001260 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001261 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001262
Michal Vasko3a41dff2020-07-15 14:30:28 +02001263 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001264
Michal Vasko3a41dff2020-07-15 14:30:28 +02001265 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 +02001266
Michal Vasko3a41dff2020-07-15 14:30:28 +02001267 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001268 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001269
Michal Vasko3a41dff2020-07-15 14:30:28 +02001270 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001271}
1272
Michal Vasko3a41dff2020-07-15 14:30:28 +02001273API LY_ERR
1274lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001275{
Radek Krejci0f969882020-08-21 16:56:47 +02001276 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001277 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001278
Michal Vasko3a41dff2020-07-15 14:30:28 +02001279 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001280
Michal Vasko3a41dff2020-07-15 14:30:28 +02001281 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 +02001282
Michal Vasko3a41dff2020-07-15 14:30:28 +02001283 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001284 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001285
Michal Vasko3a41dff2020-07-15 14:30:28 +02001286 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001287}
1288
Michal Vasko3a41dff2020-07-15 14:30:28 +02001289API LY_ERR
1290lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001291{
Radek Krejci0f969882020-08-21 16:56:47 +02001292 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001293 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001294
Michal Vasko3a41dff2020-07-15 14:30:28 +02001295 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001296
Michal Vasko3a41dff2020-07-15 14:30:28 +02001297 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001298 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001299
Michal Vasko3a41dff2020-07-15 14:30:28 +02001300 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001301 ly_in_free(in, 0);
1302
Michal Vasko3a41dff2020-07-15 14:30:28 +02001303 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001304}
1305
1306API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001307lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001308 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001309{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001310 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001311 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001312 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001313 char *wd, *wn = NULL;
1314 DIR *dir = NULL;
1315 struct dirent *file;
1316 char *match_name = NULL;
1317 LYS_INFORMAT format_aux, match_format = 0;
1318 struct ly_set *dirs;
1319 struct stat st;
1320
1321 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1322
1323 /* start to fill the dir fifo with the context's search path (if set)
1324 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001325 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001326
1327 len = strlen(name);
1328 if (cwd) {
1329 wd = get_current_dir_name();
1330 if (!wd) {
1331 LOGMEM(NULL);
1332 goto cleanup;
1333 } else {
1334 /* add implicit current working directory (./) to be searched,
1335 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001336 ret = ly_set_add(dirs, wd, 0, NULL);
1337 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001338 implicit_cwd = 1;
1339 }
1340 }
1341 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001342 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001343 /* check for duplicities with the implicit current working directory */
1344 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1345 implicit_cwd = 0;
1346 continue;
1347 }
1348 wd = strdup(searchpaths[i]);
1349 if (!wd) {
1350 LOGMEM(NULL);
1351 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001352 } else {
1353 ret = ly_set_add(dirs, wd, 0, NULL);
1354 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001355 }
1356 }
1357 }
1358 wd = NULL;
1359
1360 /* start searching */
1361 while (dirs->count) {
1362 free(wd);
1363 free(wn); wn = NULL;
1364
1365 dirs->count--;
1366 wd = (char *)dirs->objs[dirs->count];
1367 dirs->objs[dirs->count] = NULL;
1368 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1369
1370 if (dir) {
1371 closedir(dir);
1372 }
1373 dir = opendir(wd);
1374 dir_len = strlen(wd);
1375 if (!dir) {
1376 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1377 } else {
1378 while ((file = readdir(dir))) {
1379 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1380 /* skip . and .. */
1381 continue;
1382 }
1383 free(wn);
1384 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1385 LOGMEM(NULL);
1386 goto cleanup;
1387 }
1388 if (stat(wn, &st) == -1) {
1389 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001390 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001391 continue;
1392 }
1393 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1394 /* we have another subdirectory in searchpath to explore,
1395 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001396 ret = ly_set_add(dirs, wn, 0, NULL);
1397 LY_CHECK_GOTO(ret, cleanup);
1398
Radek Krejcid33273d2018-10-25 14:55:52 +02001399 /* continue with the next item in current directory */
1400 wn = NULL;
1401 continue;
1402 } else if (!S_ISREG(st.st_mode)) {
1403 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1404 continue;
1405 }
1406
1407 /* here we know that the item is a file which can contain a module */
1408 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001409 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001410 /* different filename than the module we search for */
1411 continue;
1412 }
1413
1414 /* get type according to filename suffix */
1415 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001416 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001417 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001418 /* TODO YIN parser
1419 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1420 format_aux = LYS_IN_YIN;
1421 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001422 } else {
1423 /* not supportde suffix/file format */
1424 continue;
1425 }
1426
1427 if (revision) {
1428 /* we look for the specific revision, try to get it from the filename */
1429 if (file->d_name[len] == '@') {
1430 /* check revision from the filename */
1431 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1432 /* another revision */
1433 continue;
1434 } else {
1435 /* exact revision */
1436 free(match_name);
1437 match_name = wn;
1438 wn = NULL;
1439 match_len = dir_len + 1 + len;
1440 match_format = format_aux;
1441 goto success;
1442 }
1443 } else {
1444 /* continue trying to find exact revision match, use this only if not found */
1445 free(match_name);
1446 match_name = wn;
1447 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001448 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001449 match_format = format_aux;
1450 continue;
1451 }
1452 } else {
1453 /* remember the revision and try to find the newest one */
1454 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001455 if ((file->d_name[len] != '@') ||
1456 lysp_check_date(NULL, &file->d_name[len + 1], flen - ((format_aux == LYS_IN_YANG) ? 5 : 4) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001457 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001458 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001459 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1460 continue;
1461 }
1462 free(match_name);
1463 }
1464
1465 match_name = wn;
1466 wn = NULL;
1467 match_len = dir_len + 1 + len;
1468 match_format = format_aux;
1469 continue;
1470 }
1471 }
1472 }
1473 }
1474
1475success:
1476 (*localfile) = match_name;
1477 match_name = NULL;
1478 if (format) {
1479 (*format) = match_format;
1480 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001481 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001482
1483cleanup:
1484 free(wn);
1485 free(wd);
1486 if (dir) {
1487 closedir(dir);
1488 }
1489 free(match_name);
1490 ly_set_free(dirs, free);
1491
1492 return ret;
1493}