blob: 3d56842fd6e9715e7d15b0937a5672c65f5c5527 [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 Krejci545b4872020-11-15 10:15:12 +010020#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020021#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdlib.h>
26#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020029
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020032#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020037#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020038#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020039#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "schema_compile.h"
41#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010042#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020043#include "set.h"
44#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010045#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020046#include "tree_schema_internal.h"
47#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020048
Michal Vaskof1ab44f2020-10-22 08:58:32 +020049API LY_ERR
50lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
51{
Michal Vasko1d972ca2020-11-03 17:16:56 +010052 struct lysc_node *elem, *elem2;
53 const struct lysc_action *acts;
54 const struct lysc_notif *notifs;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020055 LY_ARRAY_COUNT_TYPE u;
56
57 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
58
59 LYSC_TREE_DFS_BEGIN(root, elem) {
60 /* schema node */
61 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
62
Michal Vasko1d972ca2020-11-03 17:16:56 +010063 acts = lysc_node_actions(elem);
64 LY_ARRAY_FOR(acts, u) {
65 LYSC_TREE_DFS_BEGIN(&acts[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020066 /* action subtree */
67 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
68
Michal Vasko1d972ca2020-11-03 17:16:56 +010069 LYSC_TREE_DFS_END(&acts[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020070 }
71 }
72
Michal Vasko1d972ca2020-11-03 17:16:56 +010073 notifs = lysc_node_notifs(elem);
74 LY_ARRAY_FOR(notifs, u) {
75 LYSC_TREE_DFS_BEGIN(&notifs[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020076 /* notification subtree */
77 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
78
Michal Vasko1d972ca2020-11-03 17:16:56 +010079 LYSC_TREE_DFS_END(&notifs[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020080 }
81 }
82
83 LYSC_TREE_DFS_END(root, elem);
84 }
85
86 return LY_SUCCESS;
87}
88
89API LY_ERR
90lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
91{
92 LY_ARRAY_COUNT_TYPE u;
Michal Vasko2336cf52020-11-03 17:18:15 +010093 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020094
95 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
96
97 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +010098 LY_LIST_FOR(mod->compiled->data, root) {
99 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
100 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200101
102 /* RPCs */
103 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
104 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
105 }
106
107 /* notifications */
108 LY_ARRAY_FOR(mod->compiled->notifs, u) {
109 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
110 }
111
112 return LY_SUCCESS;
113}
114
Radek Krejcib93bd412020-11-02 13:23:11 +0100115static void
116lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
117{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100118 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100119 if (first_case->child) {
120 /* there is something to return */
121 (*next) = first_case->child;
122 return;
123 }
124 }
125
126 /* no children in choice's cases, so go to the choice's sibling instead of into it */
127 (*last) = (*next);
128 (*next) = (*next)->next;
129}
130
Radek Krejcia3045382018-11-22 14:30:31 +0100131API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200132lys_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 +0100133{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100134 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100135 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200136 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100137 const struct lysc_action *actions;
138 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200139 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100140
141 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
142
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200143next:
Radek Krejcia3045382018-11-22 14:30:31 +0100144 if (!last) {
145 /* first call */
146
147 /* get know where to start */
148 if (parent) {
149 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200150 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200151 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100152 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100153 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100154 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100155 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100156 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200157 if (snode && *snode) {
158 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100159 }
Radek Krejcia3045382018-11-22 14:30:31 +0100160 }
Radek Krejcia3045382018-11-22 14:30:31 +0100161 } else {
162 /* top level data */
163 next = last = module->data;
164 }
165 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100166 /* try to get action or notification */
167 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100168 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100169 /* test if the next can be returned */
170 goto check;
171
Michal Vasko1bf09392020-03-27 12:38:10 +0100172 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100173 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100174 if (last->parent) {
175 actions = lysc_node_actions(last->parent);
176 } else {
177 actions = module->rpcs;
178 }
179 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200180 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100181 break;
182 }
183 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200184 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200185 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100186 }
187 goto repeat;
188 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100189 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100190 if (last->parent) {
191 notifs = lysc_node_notifs(last->parent);
192 } else {
193 notifs = module->notifs;
194 }
195 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200196 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100197 break;
198 }
199 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200200 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200201 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100202 }
203 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200204 } else {
205 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100206 }
207
Radek Krejcia3045382018-11-22 14:30:31 +0100208repeat:
209 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100210 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200211 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100212 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200213 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100214 } else if (!action_flag) {
215 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200216 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100217 } else if (!notif_flag) {
218 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200219 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100220 } else {
221 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100222 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100223 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100224 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100225check:
Radek Krejcia3045382018-11-22 14:30:31 +0100226 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100227 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100228 case LYS_ACTION:
229 case LYS_NOTIF:
230 case LYS_LEAF:
231 case LYS_ANYXML:
232 case LYS_ANYDATA:
233 case LYS_LIST:
234 case LYS_LEAFLIST:
235 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200236 case LYS_CASE:
237 if (options & LYS_GETNEXT_WITHCASE) {
238 break;
239 } else {
240 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100241 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200242 }
243 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100244 case LYS_CONTAINER:
245 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
246 if (((struct lysc_node_container *)next)->child) {
247 /* go into */
248 next = ((struct lysc_node_container *)next)->child;
249 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100250 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100251 next = next->next;
252 }
253 goto repeat;
254 }
255 break;
256 case LYS_CHOICE:
257 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200258 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100259 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
260 next = next->next;
261 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100262 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200263 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100264 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100265 /* go into */
266 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100267 }
Radek Krejcia3045382018-11-22 14:30:31 +0100268 }
269 goto repeat;
270 default:
271 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200272 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100273 return NULL;
274 }
275
Radek Krejcia3045382018-11-22 14:30:31 +0100276 return next;
277}
278
279API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100280lys_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 +0200281 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100282{
283 const struct lysc_node *node = NULL;
284
285 LY_CHECK_ARG_RET(NULL, module, name, NULL);
286 if (!nodetype) {
287 nodetype = 0xffff;
288 }
289
290 while ((node = lys_getnext(node, parent, module->compiled, options))) {
291 if (!(node->nodetype & nodetype)) {
292 continue;
293 }
294 if (node->module != module) {
295 continue;
296 }
297
298 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200299 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100300 return node;
301 }
302 } else {
303 if (!strcmp(node->name, name)) {
304 return node;
305 }
306 }
307 }
308 return NULL;
309}
310
Michal Vasko519fd602020-05-26 12:17:39 +0200311API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200312lys_find_xpath_atoms(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200313{
314 LY_ERR ret = LY_SUCCESS;
315 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200316 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200317 uint32_t i;
318
319 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
320 if (!(options & LYXP_SCNODE_ALL)) {
321 options = LYXP_SCNODE;
322 }
323
324 memset(&xp_set, 0, sizeof xp_set);
325
326 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200327 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
328 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200329
330 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200331 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200332 LY_CHECK_GOTO(ret, cleanup);
333
334 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200335 ret = ly_set_new(set);
336 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200337
338 /* transform into ly_set */
339 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
340 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
341 (*set)->size = xp_set.used;
342
343 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200344 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200345 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200346 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200347 }
348 }
349
350cleanup:
351 lyxp_set_free_content(&xp_set);
352 lyxp_expr_free(ctx_node->module->ctx, exp);
353 return ret;
354}
355
Michal Vasko072de482020-08-05 13:27:21 +0200356API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200357lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
358 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
359{
360 LY_ERR ret = LY_SUCCESS;
361 struct lyxp_set xp_set = {0};
362 uint32_t i;
363
364 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
365 if (!(options & LYXP_SCNODE_ALL)) {
366 options = LYXP_SCNODE;
367 }
368
369 /* atomize expression */
370 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
371 LY_CHECK_GOTO(ret, cleanup);
372
373 /* allocate return set */
374 ret = ly_set_new(set);
375 LY_CHECK_GOTO(ret, cleanup);
376
377 /* transform into ly_set */
378 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
379 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
380 (*set)->size = xp_set.used;
381
382 for (i = 0; i < xp_set.used; ++i) {
383 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
384 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
385 LY_CHECK_GOTO(ret, cleanup);
386 }
387 }
388
389cleanup:
390 lyxp_set_free_content(&xp_set);
391 if (ret) {
392 ly_set_free(*set, NULL);
393 *set = NULL;
394 }
395 return ret;
396}
397
398API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200399lys_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 +0200400{
401 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200402 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200403 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200404 uint32_t i;
405
406 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
407 if (!(options & LYXP_SCNODE_ALL)) {
408 options = LYXP_SCNODE;
409 }
410
Michal Vasko072de482020-08-05 13:27:21 +0200411 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200412 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
413 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200414
415 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200416 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200417 LY_CHECK_GOTO(ret, cleanup);
418
419 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200420 ret = ly_set_new(set);
421 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200422
423 /* transform into ly_set */
424 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
425 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
426 (*set)->size = xp_set.used;
427
428 for (i = 0; i < xp_set.used; ++i) {
429 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200430 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200431 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200432 }
433 }
434
435cleanup:
436 lyxp_set_free_content(&xp_set);
437 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200438 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200439 ly_set_free(*set, NULL);
440 *set = NULL;
441 }
Michal Vasko072de482020-08-05 13:27:21 +0200442 return ret;
443}
444
Radek Krejcibc5644c2020-10-27 14:53:17 +0100445API LY_ERR
446lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
447{
448 LY_ERR ret = LY_SUCCESS;
449 LY_ARRAY_COUNT_TYPE u, v;
450
451 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
452
453 /* allocate return set */
454 LY_CHECK_RET(ly_set_new(set));
455
456 LY_ARRAY_FOR(path, u) {
457 /* add nodes from the path */
458 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
459 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
460 LY_ARRAY_FOR(path[u].predicates, v) {
461 /* add all the keys in a predicate */
462 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
463 }
464 }
465 }
466
467cleanup:
468 if (ret) {
469 ly_set_free(*set, NULL);
470 *set = NULL;
471 }
472 return ret;
473}
474
475API LY_ERR
476lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
477 struct ly_set **set)
478{
479 LY_ERR ret = LY_SUCCESS;
480 uint8_t oper;
481 struct lyxp_expr *expr = NULL;
482 struct ly_path *p = NULL;
483
484 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
485
486 if (!ctx) {
487 ctx = ctx_node->module->ctx;
488 }
489
490 /* parse */
491 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
492 LY_CHECK_GOTO(ret, cleanup);
493
494 /* compile */
495 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
496 ret = ly_path_compile(ctx, NULL, ctx_node, expr, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
497 LY_PREF_JSON, NULL, &p);
498 LY_CHECK_GOTO(ret, cleanup);
499
500 /* resolve */
501 ret = lys_find_lypath_atoms(p, set);
502
503cleanup:
504 ly_path_free(ctx, p);
505 lyxp_expr_free(ctx, expr);
506 return ret;
507}
508
509API const struct lysc_node *
510lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
511{
512 const struct lysc_node *snode = NULL;
513 struct lyxp_expr *exp = NULL;
514 struct ly_path *p = NULL;
515 LY_ERR ret;
516 uint8_t oper;
517
518 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
519
520 if (!ctx) {
521 ctx = ctx_node->module->ctx;
522 }
523
524 /* parse */
525 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
526 LY_CHECK_GOTO(ret, cleanup);
527
528 /* compile */
529 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
530 ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
531 LY_PREF_JSON, NULL, &p);
532 LY_CHECK_GOTO(ret, cleanup);
533
534 /* get last node */
535 snode = p[LY_ARRAY_COUNT(p) - 1].node;
536
537cleanup:
538 ly_path_free(ctx, p);
539 lyxp_expr_free(ctx, exp);
540 return snode;
541}
542
Michal Vasko14654712020-02-06 08:35:21 +0100543char *
544lysc_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 +0200545 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200546{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200547 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200548 char *path = NULL;
549 int len = 0;
550
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200551 LY_CHECK_ARG_RET(NULL, node, NULL);
552 if (buffer) {
553 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
554 }
555
Radek Krejci327de162019-06-14 12:52:07 +0200556 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200558 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100559 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200560 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100561 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200562
Michal Vasko65de0402020-08-03 16:34:19 +0200563 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
564 /* schema-only node */
565 continue;
566 }
567
Michal Vasko11deea12020-08-05 13:54:50 +0200568 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200569 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100570 if (parent && (iter->parent == parent)) {
571 slash = "";
572 } else {
573 slash = "/";
574 }
Michal Vasko69730152020-10-09 16:30:07 +0200575 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200576 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200577 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100578 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200579 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100580 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200581 }
Radek Krejci327de162019-06-14 12:52:07 +0200582 } else {
583 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200584 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100585 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200586 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100587 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200588 }
Radek Krejci327de162019-06-14 12:52:07 +0200589 }
590 free(s);
591 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200592
Michal Vasko69730152020-10-09 16:30:07 +0200593 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200594 /* not enough space in buffer */
595 break;
596 }
Radek Krejci327de162019-06-14 12:52:07 +0200597 }
598
599 if (len < 0) {
600 free(path);
601 path = NULL;
602 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200603 if (buffer) {
604 strcpy(buffer, "/");
605 } else {
606 path = strdup("/");
607 }
Radek Krejci327de162019-06-14 12:52:07 +0200608 }
609 break;
610 }
611
Radek Krejci1c0c3442019-07-23 16:08:47 +0200612 if (buffer) {
613 return buffer;
614 } else {
615 return path;
616 }
Radek Krejci327de162019-06-14 12:52:07 +0200617}
618
Michal Vasko14654712020-02-06 08:35:21 +0100619API char *
620lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
621{
622 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
623}
624
Michal Vasko28d78432020-05-26 13:10:53 +0200625API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200626lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200627{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200628 struct lysc_action *act;
629 struct lysc_notif *notif;
630
Radek Krejci19cf8052020-08-18 15:02:38 +0200631 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
632
Radek Krejciaf9cd802020-10-06 21:59:47 +0200633 switch (node->nodetype) {
634 case LYS_CONTAINER:
635 case LYS_CHOICE:
636 case LYS_CASE:
637 case LYS_LEAF:
638 case LYS_LEAFLIST:
639 case LYS_LIST:
640 case LYS_ANYXML:
641 case LYS_ANYDATA:
642 if (prev_priv_p) {
643 *prev_priv_p = node->priv;
644 }
645 ((struct lysc_node *)node)->priv = priv;
646 break;
647 case LYS_RPC:
648 case LYS_ACTION:
649 act = (struct lysc_action *)node;
650 if (prev_priv_p) {
651 *prev_priv_p = act->priv;
652 }
653 act->priv = priv;
654 break;
655 case LYS_NOTIF:
656 notif = (struct lysc_notif *)node;
657 if (prev_priv_p) {
658 *prev_priv_p = notif->priv;
659 }
660 notif->priv = priv;
661 break;
662 default:
663 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200664 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200665
666 return LY_SUCCESS;
667}
668
Michal Vasko89b5c072020-10-06 13:52:44 +0200669API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100670lys_set_implemented(struct lys_module *mod, const char **features)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200671{
Michal Vasko08c8b272020-11-24 18:11:30 +0100672 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200673 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200674 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200675
676 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
677
678 if (mod->implemented) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100679 /* mod is already implemented, set the features */
680 r = lys_set_features(mod->parsed, features);
681 if (r == LY_EEXIST) {
682 /* no changes */
683 return LY_SUCCESS;
684 } else if (r) {
685 /* error */
686 return r;
687 }
688
689 /* full recompilation */
690 lys_recompile(mod->ctx, NULL);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200691 return LY_SUCCESS;
692 }
693
694 /* we have module from the current context */
695 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
696 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200697 assert(m != mod);
698
699 /* check collision with other implemented revision */
700 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
701 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
702 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200703 }
704
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100705 /* enable features */
706 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
707
Michal Vasko89b5c072020-10-06 13:52:44 +0200708 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200709 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200710
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200711 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200712 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200713
714 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200715 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200716
Michal Vasko89b5c072020-10-06 13:52:44 +0200717 if (mod == mod->ctx->implementing.objs[0]) {
718 /* the first module being implemented, consolidate the set */
719 if (ret) {
720 /* failure, full compile revert */
721 for (i = 0; i < mod->ctx->list.count; ++i) {
722 m = mod->ctx->list.objs[i];
723 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
724 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200725
Michal Vasko89b5c072020-10-06 13:52:44 +0200726 /* make the module correctly non-implemented again */
727 m->implemented = 0;
728 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
729 lys_precompile_augments_deviations_revert(mod->ctx, m);
730 }
Michal Vasko89b5c072020-10-06 13:52:44 +0200731 }
Michal Vasko916aefb2020-11-02 15:43:16 +0100732
733 /* recompile, do not overwrite return value */
734 lys_recompile(mod->ctx, NULL);
Michal Vasko89b5c072020-10-06 13:52:44 +0200735 }
736
737 ly_set_erase(&mod->ctx->implementing, NULL);
738 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100739
Michal Vasko89b5c072020-10-06 13:52:44 +0200740 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200741}
742
Michal Vasko7c8439f2020-08-05 13:25:19 +0200743static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100744lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200745{
746 struct lysp_import *imp;
747 struct lysp_include *inc;
748 LY_ARRAY_COUNT_TYPE u, v;
749
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100750 pmod->parsing = 1;
751 LY_ARRAY_FOR(pmod->imports, u) {
752 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200753 if (!imp->module) {
Michal Vasko0550b762020-11-24 18:04:08 +0100754 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200755 }
756 /* check for importing the same module twice */
757 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100758 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200759 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
760 }
761 }
762 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100763 LY_ARRAY_FOR(pmod->includes, u) {
764 inc = &pmod->includes[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200765 if (!inc->submodule) {
766 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
767 }
768 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100769 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200770
771 return LY_SUCCESS;
772}
773
Michal Vasko3a41dff2020-07-15 14:30:28 +0200774LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200775lys_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 +0200776 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200777 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200778{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200779 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100780 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100781 struct lys_yang_parser_ctx *yangctx = NULL;
782 struct lys_yin_parser_ctx *yinctx = NULL;
783 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784
Michal Vasko3a41dff2020-07-15 14:30:28 +0200785 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100787 switch (format) {
788 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200789 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100790 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 break;
792 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200793 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100794 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100795 break;
796 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200797 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200798 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200800 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200801 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200802 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803
804 /* make sure that the newest revision is at position 0 */
805 lysp_sort_revisions(submod->revs);
806
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100807 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200808 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100809 if (latest_sp) {
810 if (submod->revs) {
811 if (!latest_sp->revs) {
812 /* latest has no revision, so mod is anyway newer */
813 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200814 /* the latest_sp is zeroed later when the new module is being inserted into the context */
815 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
816 submod->latest_revision = latest_sp->latest_revision;
817 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100818 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200819 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100820 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200821 } else {
822 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100823 }
824 } else {
825 submod->latest_revision = 1;
826 }
827
Radek Krejcib3289d62019-09-18 12:21:39 +0200828 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200829 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200830 }
831
832 if (latest_sp) {
833 latest_sp->latest_revision = 0;
834 }
835
Michal Vasko7a0b0762020-09-02 16:37:01 +0200836 lys_parser_fill_filepath(ctx, in, &submod->filepath);
837
Michal Vasko7c8439f2020-08-05 13:25:19 +0200838 /* resolve imports and includes */
839 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
840
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100841 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100842 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
843 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100844
David Sedlák1b623122019-08-05 15:27:49 +0200845 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100846 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200847 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100848 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200849 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200850 *submodule = submod;
851 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200852
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100853error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200854 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200855 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100856 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200857 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100858 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200859 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200860 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200861}
862
Michal Vasko45b521c2020-11-04 17:14:39 +0100863/**
864 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
865 *
866 * @param[in] mod Parsed module to add to.
867 * @return LY_SUCCESS on success.
868 * @return LY_ERR on error.
869 */
870static LY_ERR
871lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
872{
873 struct lysp_ext_instance *ext_p;
874 struct lysp_stmt *stmt;
875 struct lysp_import *imp;
876
877 /*
878 * 1) edit-config's operation
879 */
880 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
881 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
882 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
883 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
884 ext_p->flags = LYS_INTERNAL;
885 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
886 ext_p->insubstmt_index = 0;
887
888 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
889 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
890 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
891 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
892 stmt->kw = LY_STMT_TYPE;
893
894 stmt->child = calloc(1, sizeof *stmt->child);
895 stmt = stmt->child;
896 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
897 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
898 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
899 stmt->kw = LY_STMT_ENUM;
900
901 stmt->next = calloc(1, sizeof *stmt->child);
902 stmt = stmt->next;
903 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
904 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
905 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
906 stmt->kw = LY_STMT_ENUM;
907
908 stmt->next = calloc(1, sizeof *stmt->child);
909 stmt = stmt->next;
910 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
911 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
912 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
913 stmt->kw = LY_STMT_ENUM;
914
915 stmt->next = calloc(1, sizeof *stmt->child);
916 stmt = stmt->next;
917 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
918 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
919 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
920 stmt->kw = LY_STMT_ENUM;
921
922 stmt->next = calloc(1, sizeof *stmt->child);
923 stmt = stmt->next;
924 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
925 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
926 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
927 stmt->kw = LY_STMT_ENUM;
928
929 /*
930 * 2) filter's type
931 */
932 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
933 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
934 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
935 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
936 ext_p->flags = LYS_INTERNAL;
937 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
938 ext_p->insubstmt_index = 0;
939
940 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
941 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
942 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
943 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
944 stmt->kw = LY_STMT_TYPE;
945
946 stmt->child = calloc(1, sizeof *stmt->child);
947 stmt = stmt->child;
948 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
949 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
950 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
951 stmt->kw = LY_STMT_ENUM;
952
953 stmt->next = calloc(1, sizeof *stmt->child);
954 stmt = stmt->next;
955 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
956 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
957 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
958 stmt->kw = LY_STMT_ENUM;
959
960 /* if-feature for enum allowed only for YANG 1.1 modules */
961 if (mod->version >= LYS_VERSION_1_1) {
962 stmt->child = calloc(1, sizeof *stmt->child);
963 stmt = stmt->child;
964 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
965 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
966 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
967 stmt->kw = LY_STMT_IF_FEATURE;
968 }
969
970 /*
971 * 3) filter's select
972 */
973 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
974 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
975 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
976 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
977 ext_p->flags = LYS_INTERNAL;
978 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
979 ext_p->insubstmt_index = 0;
980
981 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
982 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
983 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
984 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
985 stmt->kw = LY_STMT_TYPE;
986
987 /* create new imports for the used prefixes */
988 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
989
990 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
991 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
992 imp->flags = LYS_INTERNAL;
993
994 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
995
996 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
997 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
998 imp->flags = LYS_INTERNAL;
999
1000 return LY_SUCCESS;
1001}
1002
1003/**
1004 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
1005 *
1006 * @param[in] mod Parsed module to add to.
1007 * @return LY_SUCCESS on success.
1008 * @return LY_ERR on error.
1009 */
1010static LY_ERR
1011lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
1012{
1013 struct lysp_ext_instance *ext_p;
1014 struct lysp_stmt *stmt;
1015 struct lysp_import *imp;
1016
1017 /* add new extension instance */
1018 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1019
1020 /* fill in the extension instance fields */
1021 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1022 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1023 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
1024 ext_p->flags = LYS_INTERNAL;
1025 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1026 ext_p->insubstmt_index = 0;
1027
1028 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1029 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1030 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1031 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
1032 stmt->kw = LY_STMT_TYPE;
1033
1034 /* create new import for the used prefix */
1035 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1036
1037 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1038 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1039 imp->flags = LYS_INTERNAL;
1040
1041 return LY_SUCCESS;
1042}
1043
Michal Vasko3a41dff2020-07-15 14:30:28 +02001044LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001045lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001046 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001047 void *check_data, const char **features, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001048{
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001049 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001050 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001051 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001052 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001053 struct lys_yang_parser_ctx *yangctx = NULL;
1054 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001055 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001056 char *filename, *rev, *dot;
1057 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +02001058
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001059 LY_CHECK_ARG_RET(ctx, ctx, in, !features || implement, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001060 if (module) {
1061 *module = NULL;
1062 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001063
1064 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001065 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001066 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001067
1068 switch (format) {
1069 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001070 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001071 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001072 break;
1073 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001074 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001075 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001076 break;
1077 default:
1078 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001079 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001080 break;
1081 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001082 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001083
1084 /* make sure that the newest revision is at position 0 */
1085 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001086 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001087 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001088 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001089
Radek Krejcib3289d62019-09-18 12:21:39 +02001090 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001091 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001092 if (latest) {
1093 if (mod->revision) {
1094 if (!latest->revision) {
1095 /* latest has no revision, so mod is anyway newer */
1096 mod->latest_revision = latest->latest_revision;
1097 /* the latest is zeroed later when the new module is being inserted into the context */
1098 } else if (strcmp(mod->revision, latest->revision) > 0) {
1099 mod->latest_revision = latest->latest_revision;
1100 /* the latest is zeroed later when the new module is being inserted into the context */
1101 } else {
1102 latest = NULL;
1103 }
1104 } else {
1105 latest = NULL;
1106 }
1107 } else {
1108 mod->latest_revision = 1;
1109 }
1110
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001111 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001112 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001113 }
1114
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001115 /* check for duplicity in the context */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001116 if (implement && ly_ctx_get_module_implemented(ctx, mod->name)) {
1117 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
1118 ret = LY_EDENIED;
1119 goto error;
1120 }
Michal Vasko22df3f02020-08-24 13:29:22 +02001121 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001122 if (mod_dup) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001123 if (mod->parsed->revs) {
1124 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
1125 mod->name, mod->parsed->revs[0].date);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001126 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001127 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
1128 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001129 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001130 ret = LY_EEXIST;
1131 goto error;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001132 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001133
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001134 switch (in->type) {
1135 case LY_IN_FILEPATH:
1136 /* check that name and revision match filename */
1137 filename = strrchr(in->method.fpath.filepath, '/');
1138 if (!filename) {
1139 filename = in->method.fpath.filepath;
1140 } else {
1141 filename++;
1142 }
1143 rev = strchr(filename, '@');
1144 dot = strrchr(filename, '.');
1145
1146 /* name */
1147 len = strlen(mod->name);
1148 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001149 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001150 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1151 }
1152 if (rev) {
1153 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001154 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001155 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001156 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001157 }
1158 }
1159
1160 break;
1161 case LY_IN_FD:
1162 case LY_IN_FILE:
1163 case LY_IN_MEMORY:
1164 /* nothing special to do */
1165 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001166 case LY_IN_ERROR:
1167 LOGINT(ctx);
1168 ret = LY_EINT;
1169 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001170 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001171 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001172
Michal Vasko7a0b0762020-09-02 16:37:01 +02001173 if (latest) {
1174 latest->latest_revision = 0;
1175 }
1176
Michal Vasko45b521c2020-11-04 17:14:39 +01001177 /* add internal data in case specific modules were parsed */
1178 if (!strcmp(mod->name, "ietf-netconf")) {
1179 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), error);
1180 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
1181 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), error);
1182 }
1183
Michal Vasko7a0b0762020-09-02 16:37:01 +02001184 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001185 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001186 LY_CHECK_GOTO(ret, error);
1187 ctx->module_set_id++;
1188
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001189 /* resolve includes and all imports */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001190 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1191
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001192 /* check name collisions */
1193 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), error_ctx);
1194 /* TODO groupings */
1195 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), error_ctx);
1196 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), error_ctx);
1197
1198 /* compile features */
1199 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), error_ctx);
1200
Michal Vasko89b5c072020-10-06 13:52:44 +02001201 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001202 /* pre-compile identities of the module */
1203 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
1204
1205 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001206 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001207 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001208 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1209 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001210 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001211 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001212 /* implement (compile) */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001213 LY_CHECK_GOTO(ret = lys_set_implemented(mod, features), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001214 }
1215
1216 if (format == LYS_IN_YANG) {
1217 yang_parser_ctx_free(yangctx);
1218 } else {
1219 yin_parser_ctx_free(yinctx);
1220 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001221 if (module) {
1222 *module = mod;
1223 }
1224 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001225
1226error_ctx:
1227 ly_set_rm(&ctx->list, mod, NULL);
1228error:
1229 lys_module_free(mod, NULL);
1230 if (pctx) {
1231 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1232 }
1233 if (format == LYS_IN_YANG) {
1234 yang_parser_ctx_free(yangctx);
1235 } else {
1236 yin_parser_ctx_free(yinctx);
1237 }
1238
1239 return ret;
1240}
1241
Radek Krejci545b4872020-11-15 10:15:12 +01001242static LYS_INFORMAT
1243lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1244{
1245 if (!format && (in->type == LY_IN_FILEPATH)) {
1246 /* unknown format - try to detect it from filename's suffix */
1247 const char *path = in->method.fpath.filepath;
1248 size_t len = strlen(path);
1249
1250 /* ignore trailing whitespaces */
1251 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1252
1253 if ((len >= 5) && !strncmp(&path[len - 5], ".yang", 5)) {
1254 format = LYS_IN_YANG;
1255 } else if ((len >= 6) && !strncmp(&path[len - 4], ".yin", 4)) {
1256 format = LYS_IN_YIN;
1257 } /* else still unknown */
1258 }
1259
1260 return format;
1261}
1262
Michal Vasko7a0b0762020-09-02 16:37:01 +02001263API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001264lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, const struct lys_module **module)
Michal Vasko7a0b0762020-09-02 16:37:01 +02001265{
1266 if (module) {
1267 *module = NULL;
1268 }
Radek Krejci545b4872020-11-15 10:15:12 +01001269 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1270
1271 format = lys_parse_get_format(in, format);
1272 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001273
1274 /* remember input position */
1275 in->func_start = in->current;
1276
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001277 return lys_create_module(ctx, in, format, 1, NULL, NULL, features, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001278}
1279
Michal Vasko3a41dff2020-07-15 14:30:28 +02001280API LY_ERR
1281lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001282{
Radek Krejci0f969882020-08-21 16:56:47 +02001283 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001284 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001285
Michal Vasko3a41dff2020-07-15 14:30:28 +02001286 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001287
Michal Vasko3a41dff2020-07-15 14:30:28 +02001288 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 +02001289
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001290 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001291 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001292
Michal Vasko3a41dff2020-07-15 14:30:28 +02001293 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001294}
1295
Michal Vasko3a41dff2020-07-15 14:30:28 +02001296API LY_ERR
1297lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001298{
Radek Krejci0f969882020-08-21 16:56:47 +02001299 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001300 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001301
Michal Vasko3a41dff2020-07-15 14:30:28 +02001302 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001303
Michal Vasko3a41dff2020-07-15 14:30:28 +02001304 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 +02001305
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001306 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001307 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001308
Michal Vasko3a41dff2020-07-15 14:30:28 +02001309 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001310}
1311
Michal Vasko3a41dff2020-07-15 14:30:28 +02001312API LY_ERR
1313lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001314{
Radek Krejci0f969882020-08-21 16:56:47 +02001315 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001316 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001317
Michal Vasko3a41dff2020-07-15 14:30:28 +02001318 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001319
Michal Vasko3a41dff2020-07-15 14:30:28 +02001320 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001321 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001322
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001323 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001324 ly_in_free(in, 0);
1325
Michal Vasko3a41dff2020-07-15 14:30:28 +02001326 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001327}
1328
1329API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001330lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001331 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001332{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001333 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001334 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001335 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001336 char *wd, *wn = NULL;
1337 DIR *dir = NULL;
1338 struct dirent *file;
1339 char *match_name = NULL;
1340 LYS_INFORMAT format_aux, match_format = 0;
1341 struct ly_set *dirs;
1342 struct stat st;
1343
1344 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1345
1346 /* start to fill the dir fifo with the context's search path (if set)
1347 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001348 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001349
1350 len = strlen(name);
1351 if (cwd) {
1352 wd = get_current_dir_name();
1353 if (!wd) {
1354 LOGMEM(NULL);
1355 goto cleanup;
1356 } else {
1357 /* add implicit current working directory (./) to be searched,
1358 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001359 ret = ly_set_add(dirs, wd, 0, NULL);
1360 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001361 implicit_cwd = 1;
1362 }
1363 }
1364 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001365 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001366 /* check for duplicities with the implicit current working directory */
1367 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1368 implicit_cwd = 0;
1369 continue;
1370 }
1371 wd = strdup(searchpaths[i]);
1372 if (!wd) {
1373 LOGMEM(NULL);
1374 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001375 } else {
1376 ret = ly_set_add(dirs, wd, 0, NULL);
1377 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001378 }
1379 }
1380 }
1381 wd = NULL;
1382
1383 /* start searching */
1384 while (dirs->count) {
1385 free(wd);
1386 free(wn); wn = NULL;
1387
1388 dirs->count--;
1389 wd = (char *)dirs->objs[dirs->count];
1390 dirs->objs[dirs->count] = NULL;
1391 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1392
1393 if (dir) {
1394 closedir(dir);
1395 }
1396 dir = opendir(wd);
1397 dir_len = strlen(wd);
1398 if (!dir) {
1399 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1400 } else {
1401 while ((file = readdir(dir))) {
1402 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1403 /* skip . and .. */
1404 continue;
1405 }
1406 free(wn);
1407 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1408 LOGMEM(NULL);
1409 goto cleanup;
1410 }
1411 if (stat(wn, &st) == -1) {
1412 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001413 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001414 continue;
1415 }
1416 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1417 /* we have another subdirectory in searchpath to explore,
1418 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001419 ret = ly_set_add(dirs, wn, 0, NULL);
1420 LY_CHECK_GOTO(ret, cleanup);
1421
Radek Krejcid33273d2018-10-25 14:55:52 +02001422 /* continue with the next item in current directory */
1423 wn = NULL;
1424 continue;
1425 } else if (!S_ISREG(st.st_mode)) {
1426 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1427 continue;
1428 }
1429
1430 /* here we know that the item is a file which can contain a module */
1431 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001432 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001433 /* different filename than the module we search for */
1434 continue;
1435 }
1436
1437 /* get type according to filename suffix */
1438 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001439 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001440 format_aux = LYS_IN_YANG;
Radek Krejci01a937f2020-11-15 10:14:12 +01001441 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1442 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001443 } else {
1444 /* not supportde suffix/file format */
1445 continue;
1446 }
1447
1448 if (revision) {
1449 /* we look for the specific revision, try to get it from the filename */
1450 if (file->d_name[len] == '@') {
1451 /* check revision from the filename */
1452 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1453 /* another revision */
1454 continue;
1455 } else {
1456 /* exact revision */
1457 free(match_name);
1458 match_name = wn;
1459 wn = NULL;
1460 match_len = dir_len + 1 + len;
1461 match_format = format_aux;
1462 goto success;
1463 }
1464 } else {
1465 /* continue trying to find exact revision match, use this only if not found */
1466 free(match_name);
1467 match_name = wn;
1468 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001469 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001470 match_format = format_aux;
1471 continue;
1472 }
1473 } else {
1474 /* remember the revision and try to find the newest one */
1475 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001476 if ((file->d_name[len] != '@') ||
1477 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 +02001478 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001479 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001480 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1481 continue;
1482 }
1483 free(match_name);
1484 }
1485
1486 match_name = wn;
1487 wn = NULL;
1488 match_len = dir_len + 1 + len;
1489 match_format = format_aux;
1490 continue;
1491 }
1492 }
1493 }
1494 }
1495
1496success:
1497 (*localfile) = match_name;
1498 match_name = NULL;
1499 if (format) {
1500 (*format) = match_format;
1501 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001502 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001503
1504cleanup:
1505 free(wn);
1506 free(wd);
1507 if (dir) {
1508 closedir(dir);
1509 }
1510 free(match_name);
1511 ly_set_free(dirs, free);
1512
1513 return ret;
1514}