blob: 4236cc6b50ae5747b7e77c834eec252eda5d7b6c [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"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020034#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020036#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020037#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020038#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020039#include "schema_compile.h"
40#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010041#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020042#include "set.h"
43#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010044#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020045#include "tree_schema_internal.h"
46#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020047
Michal Vaskof1ab44f2020-10-22 08:58:32 +020048API LY_ERR
49lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
50{
Michal Vasko1d972ca2020-11-03 17:16:56 +010051 struct lysc_node *elem, *elem2;
52 const struct lysc_action *acts;
53 const struct lysc_notif *notifs;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020054 LY_ARRAY_COUNT_TYPE u;
55
56 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
57
58 LYSC_TREE_DFS_BEGIN(root, elem) {
59 /* schema node */
60 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
61
Michal Vasko1d972ca2020-11-03 17:16:56 +010062 acts = lysc_node_actions(elem);
63 LY_ARRAY_FOR(acts, u) {
64 LYSC_TREE_DFS_BEGIN(&acts[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020065 /* action subtree */
66 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
67
Michal Vasko1d972ca2020-11-03 17:16:56 +010068 LYSC_TREE_DFS_END(&acts[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020069 }
70 }
71
Michal Vasko1d972ca2020-11-03 17:16:56 +010072 notifs = lysc_node_notifs(elem);
73 LY_ARRAY_FOR(notifs, u) {
74 LYSC_TREE_DFS_BEGIN(&notifs[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020075 /* notification subtree */
76 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
77
Michal Vasko1d972ca2020-11-03 17:16:56 +010078 LYSC_TREE_DFS_END(&notifs[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020079 }
80 }
81
82 LYSC_TREE_DFS_END(root, elem);
83 }
84
85 return LY_SUCCESS;
86}
87
88API LY_ERR
89lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
90{
91 LY_ARRAY_COUNT_TYPE u;
Michal Vasko2336cf52020-11-03 17:18:15 +010092 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020093
94 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
95
96 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +010097 LY_LIST_FOR(mod->compiled->data, root) {
98 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
99 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200100
101 /* RPCs */
102 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
103 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
104 }
105
106 /* notifications */
107 LY_ARRAY_FOR(mod->compiled->notifs, u) {
108 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
109 }
110
111 return LY_SUCCESS;
112}
113
Radek Krejcib93bd412020-11-02 13:23:11 +0100114static void
115lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
116{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100117 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100118 if (first_case->child) {
119 /* there is something to return */
120 (*next) = first_case->child;
121 return;
122 }
123 }
124
125 /* no children in choice's cases, so go to the choice's sibling instead of into it */
126 (*last) = (*next);
127 (*next) = (*next)->next;
128}
129
Radek Krejcia3045382018-11-22 14:30:31 +0100130API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200131lys_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 +0100132{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100133 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100134 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200135 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100136 const struct lysc_action *actions;
137 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200138 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100139
140 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
141
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200142next:
Radek Krejcia3045382018-11-22 14:30:31 +0100143 if (!last) {
144 /* first call */
145
146 /* get know where to start */
147 if (parent) {
148 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200149 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200150 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100151 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100152 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100153 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100154 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100155 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200156 if (snode && *snode) {
157 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100158 }
Radek Krejcia3045382018-11-22 14:30:31 +0100159 }
Radek Krejcia3045382018-11-22 14:30:31 +0100160 } else {
161 /* top level data */
162 next = last = module->data;
163 }
164 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100165 /* try to get action or notification */
166 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100167 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100168 /* test if the next can be returned */
169 goto check;
170
Michal Vasko1bf09392020-03-27 12:38:10 +0100171 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100172 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100173 if (last->parent) {
174 actions = lysc_node_actions(last->parent);
175 } else {
176 actions = module->rpcs;
177 }
178 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200179 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100180 break;
181 }
182 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200183 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200184 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100185 }
186 goto repeat;
187 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100188 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100189 if (last->parent) {
190 notifs = lysc_node_notifs(last->parent);
191 } else {
192 notifs = module->notifs;
193 }
194 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200195 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100196 break;
197 }
198 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200199 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200200 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100201 }
202 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200203 } else {
204 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100205 }
206
Radek Krejcia3045382018-11-22 14:30:31 +0100207repeat:
208 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100209 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200210 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100211 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200212 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100213 } else if (!action_flag) {
214 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200215 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100216 } else if (!notif_flag) {
217 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200218 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100219 } else {
220 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100221 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100222 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100223 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100224check:
Radek Krejcia3045382018-11-22 14:30:31 +0100225 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100226 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100227 case LYS_ACTION:
228 case LYS_NOTIF:
229 case LYS_LEAF:
230 case LYS_ANYXML:
231 case LYS_ANYDATA:
232 case LYS_LIST:
233 case LYS_LEAFLIST:
234 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200235 case LYS_CASE:
236 if (options & LYS_GETNEXT_WITHCASE) {
237 break;
238 } else {
239 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100240 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200241 }
242 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100243 case LYS_CONTAINER:
244 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
245 if (((struct lysc_node_container *)next)->child) {
246 /* go into */
247 next = ((struct lysc_node_container *)next)->child;
248 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100249 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100250 next = next->next;
251 }
252 goto repeat;
253 }
254 break;
255 case LYS_CHOICE:
256 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200257 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100258 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
259 next = next->next;
260 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100261 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200262 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100263 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100264 /* go into */
265 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100266 }
Radek Krejcia3045382018-11-22 14:30:31 +0100267 }
268 goto repeat;
269 default:
270 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200271 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100272 return NULL;
273 }
274
Radek Krejcia3045382018-11-22 14:30:31 +0100275 return next;
276}
277
278API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100279lys_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 +0200280 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100281{
282 const struct lysc_node *node = NULL;
283
284 LY_CHECK_ARG_RET(NULL, module, name, NULL);
285 if (!nodetype) {
286 nodetype = 0xffff;
287 }
288
289 while ((node = lys_getnext(node, parent, module->compiled, options))) {
290 if (!(node->nodetype & nodetype)) {
291 continue;
292 }
293 if (node->module != module) {
294 continue;
295 }
296
297 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200298 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100299 return node;
300 }
301 } else {
302 if (!strcmp(node->name, name)) {
303 return node;
304 }
305 }
306 }
307 return NULL;
308}
309
Michal Vasko519fd602020-05-26 12:17:39 +0200310API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200311lys_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 +0200312{
313 LY_ERR ret = LY_SUCCESS;
314 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200315 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200316 uint32_t i;
317
318 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
319 if (!(options & LYXP_SCNODE_ALL)) {
320 options = LYXP_SCNODE;
321 }
322
323 memset(&xp_set, 0, sizeof xp_set);
324
325 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200326 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
327 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200328
329 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200330 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200331 LY_CHECK_GOTO(ret, cleanup);
332
333 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200334 ret = ly_set_new(set);
335 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200336
337 /* transform into ly_set */
338 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
339 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
340 (*set)->size = xp_set.used;
341
342 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200343 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200344 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200345 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200346 }
347 }
348
349cleanup:
350 lyxp_set_free_content(&xp_set);
351 lyxp_expr_free(ctx_node->module->ctx, exp);
352 return ret;
353}
354
Michal Vasko072de482020-08-05 13:27:21 +0200355API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200356lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
357 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
358{
359 LY_ERR ret = LY_SUCCESS;
360 struct lyxp_set xp_set = {0};
361 uint32_t i;
362
363 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
364 if (!(options & LYXP_SCNODE_ALL)) {
365 options = LYXP_SCNODE;
366 }
367
368 /* atomize expression */
369 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
370 LY_CHECK_GOTO(ret, cleanup);
371
372 /* allocate return set */
373 ret = ly_set_new(set);
374 LY_CHECK_GOTO(ret, cleanup);
375
376 /* transform into ly_set */
377 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
378 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
379 (*set)->size = xp_set.used;
380
381 for (i = 0; i < xp_set.used; ++i) {
382 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
383 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
384 LY_CHECK_GOTO(ret, cleanup);
385 }
386 }
387
388cleanup:
389 lyxp_set_free_content(&xp_set);
390 if (ret) {
391 ly_set_free(*set, NULL);
392 *set = NULL;
393 }
394 return ret;
395}
396
397API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200398lys_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 +0200399{
400 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200401 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200402 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200403 uint32_t i;
404
405 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
406 if (!(options & LYXP_SCNODE_ALL)) {
407 options = LYXP_SCNODE;
408 }
409
Michal Vasko072de482020-08-05 13:27:21 +0200410 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200411 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
412 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200413
414 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200415 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200416 LY_CHECK_GOTO(ret, cleanup);
417
418 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200419 ret = ly_set_new(set);
420 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200421
422 /* transform into ly_set */
423 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
424 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
425 (*set)->size = xp_set.used;
426
427 for (i = 0; i < xp_set.used; ++i) {
428 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 +0200429 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200430 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200431 }
432 }
433
434cleanup:
435 lyxp_set_free_content(&xp_set);
436 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200437 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200438 ly_set_free(*set, NULL);
439 *set = NULL;
440 }
Michal Vasko072de482020-08-05 13:27:21 +0200441 return ret;
442}
443
Radek Krejcibc5644c2020-10-27 14:53:17 +0100444API LY_ERR
445lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
446{
447 LY_ERR ret = LY_SUCCESS;
448 LY_ARRAY_COUNT_TYPE u, v;
449
450 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
451
452 /* allocate return set */
453 LY_CHECK_RET(ly_set_new(set));
454
455 LY_ARRAY_FOR(path, u) {
456 /* add nodes from the path */
457 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
458 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
459 LY_ARRAY_FOR(path[u].predicates, v) {
460 /* add all the keys in a predicate */
461 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
462 }
463 }
464 }
465
466cleanup:
467 if (ret) {
468 ly_set_free(*set, NULL);
469 *set = NULL;
470 }
471 return ret;
472}
473
474API LY_ERR
475lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
476 struct ly_set **set)
477{
478 LY_ERR ret = LY_SUCCESS;
479 uint8_t oper;
480 struct lyxp_expr *expr = NULL;
481 struct ly_path *p = NULL;
482
483 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
484
485 if (!ctx) {
486 ctx = ctx_node->module->ctx;
487 }
488
489 /* parse */
490 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
491 LY_CHECK_GOTO(ret, cleanup);
492
493 /* compile */
494 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
495 ret = ly_path_compile(ctx, NULL, ctx_node, expr, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
496 LY_PREF_JSON, NULL, &p);
497 LY_CHECK_GOTO(ret, cleanup);
498
499 /* resolve */
500 ret = lys_find_lypath_atoms(p, set);
501
502cleanup:
503 ly_path_free(ctx, p);
504 lyxp_expr_free(ctx, expr);
505 return ret;
506}
507
508API const struct lysc_node *
509lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
510{
511 const struct lysc_node *snode = NULL;
512 struct lyxp_expr *exp = NULL;
513 struct ly_path *p = NULL;
514 LY_ERR ret;
515 uint8_t oper;
516
517 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
518
519 if (!ctx) {
520 ctx = ctx_node->module->ctx;
521 }
522
523 /* parse */
524 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
525 LY_CHECK_GOTO(ret, cleanup);
526
527 /* compile */
528 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
529 ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
530 LY_PREF_JSON, NULL, &p);
531 LY_CHECK_GOTO(ret, cleanup);
532
533 /* get last node */
534 snode = p[LY_ARRAY_COUNT(p) - 1].node;
535
536cleanup:
537 ly_path_free(ctx, p);
538 lyxp_expr_free(ctx, exp);
539 return snode;
540}
541
Michal Vasko14654712020-02-06 08:35:21 +0100542char *
543lysc_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 +0200544 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200545{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200546 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200547 char *path = NULL;
548 int len = 0;
549
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200550 LY_CHECK_ARG_RET(NULL, node, NULL);
551 if (buffer) {
552 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
553 }
554
Radek Krejci327de162019-06-14 12:52:07 +0200555 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200556 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200557 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100558 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200559 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100560 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200561
Michal Vasko65de0402020-08-03 16:34:19 +0200562 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
563 /* schema-only node */
564 continue;
565 }
566
Michal Vasko11deea12020-08-05 13:54:50 +0200567 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200568 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100569 if (parent && (iter->parent == parent)) {
570 slash = "";
571 } else {
572 slash = "/";
573 }
Michal Vasko69730152020-10-09 16:30:07 +0200574 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200575 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200576 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100577 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200578 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100579 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200580 }
Radek Krejci327de162019-06-14 12:52:07 +0200581 } else {
582 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200583 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100584 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200585 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100586 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200587 }
Radek Krejci327de162019-06-14 12:52:07 +0200588 }
589 free(s);
590 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200591
Michal Vasko69730152020-10-09 16:30:07 +0200592 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200593 /* not enough space in buffer */
594 break;
595 }
Radek Krejci327de162019-06-14 12:52:07 +0200596 }
597
598 if (len < 0) {
599 free(path);
600 path = NULL;
601 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200602 if (buffer) {
603 strcpy(buffer, "/");
604 } else {
605 path = strdup("/");
606 }
Radek Krejci327de162019-06-14 12:52:07 +0200607 }
608 break;
609 }
610
Radek Krejci1c0c3442019-07-23 16:08:47 +0200611 if (buffer) {
612 return buffer;
613 } else {
614 return path;
615 }
Radek Krejci327de162019-06-14 12:52:07 +0200616}
617
Michal Vasko14654712020-02-06 08:35:21 +0100618API char *
619lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
620{
621 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
622}
623
Michal Vasko28d78432020-05-26 13:10:53 +0200624API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200625lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200626{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200627 struct lysc_action *act;
628 struct lysc_notif *notif;
629
Radek Krejci19cf8052020-08-18 15:02:38 +0200630 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
631
Radek Krejciaf9cd802020-10-06 21:59:47 +0200632 switch (node->nodetype) {
633 case LYS_CONTAINER:
634 case LYS_CHOICE:
635 case LYS_CASE:
636 case LYS_LEAF:
637 case LYS_LEAFLIST:
638 case LYS_LIST:
639 case LYS_ANYXML:
640 case LYS_ANYDATA:
641 if (prev_priv_p) {
642 *prev_priv_p = node->priv;
643 }
644 ((struct lysc_node *)node)->priv = priv;
645 break;
646 case LYS_RPC:
647 case LYS_ACTION:
648 act = (struct lysc_action *)node;
649 if (prev_priv_p) {
650 *prev_priv_p = act->priv;
651 }
652 act->priv = priv;
653 break;
654 case LYS_NOTIF:
655 notif = (struct lysc_notif *)node;
656 if (prev_priv_p) {
657 *prev_priv_p = notif->priv;
658 }
659 notif->priv = priv;
660 break;
661 default:
662 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200663 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200664
665 return LY_SUCCESS;
666}
667
Michal Vasko89b5c072020-10-06 13:52:44 +0200668API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100669lys_set_implemented(struct lys_module *mod, const char **features)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200670{
Michal Vasko916aefb2020-11-02 15:43:16 +0100671 LY_ERR ret = LY_SUCCESS;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200672 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200673 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200674
675 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
676
677 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200678 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200679 return LY_SUCCESS;
680 }
681
682 /* we have module from the current context */
683 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
684 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200685 assert(m != mod);
686
687 /* check collision with other implemented revision */
688 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
689 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
690 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200691 }
692
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100693 /* enable features */
694 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
695
Michal Vasko89b5c072020-10-06 13:52:44 +0200696 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200697 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200698
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200699 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200700 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200701
702 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200703 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200704
Michal Vasko89b5c072020-10-06 13:52:44 +0200705 if (mod == mod->ctx->implementing.objs[0]) {
706 /* the first module being implemented, consolidate the set */
707 if (ret) {
708 /* failure, full compile revert */
709 for (i = 0; i < mod->ctx->list.count; ++i) {
710 m = mod->ctx->list.objs[i];
711 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
712 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200713
Michal Vasko89b5c072020-10-06 13:52:44 +0200714 /* make the module correctly non-implemented again */
715 m->implemented = 0;
716 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
717 lys_precompile_augments_deviations_revert(mod->ctx, m);
718 }
Michal Vasko89b5c072020-10-06 13:52:44 +0200719 }
Michal Vasko916aefb2020-11-02 15:43:16 +0100720
721 /* recompile, do not overwrite return value */
722 lys_recompile(mod->ctx, NULL);
Michal Vasko89b5c072020-10-06 13:52:44 +0200723 }
724
725 ly_set_erase(&mod->ctx->implementing, NULL);
726 }
727 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200728}
729
Michal Vasko7c8439f2020-08-05 13:25:19 +0200730static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100731lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200732{
733 struct lysp_import *imp;
734 struct lysp_include *inc;
735 LY_ARRAY_COUNT_TYPE u, v;
736
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100737 pmod->parsing = 1;
738 LY_ARRAY_FOR(pmod->imports, u) {
739 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200740 if (!imp->module) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100741 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, NULL, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200742 }
743 /* check for importing the same module twice */
744 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100745 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200746 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
747 }
748 }
749 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100750 LY_ARRAY_FOR(pmod->includes, u) {
751 inc = &pmod->includes[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200752 if (!inc->submodule) {
753 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
754 }
755 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100756 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200757
758 return LY_SUCCESS;
759}
760
Michal Vasko3a41dff2020-07-15 14:30:28 +0200761LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200762lys_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 +0200763 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200764 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200765{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200766 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100768 struct lys_yang_parser_ctx *yangctx = NULL;
769 struct lys_yin_parser_ctx *yinctx = NULL;
770 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771
Michal Vasko3a41dff2020-07-15 14:30:28 +0200772 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100773
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100774 switch (format) {
775 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200776 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100777 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100778 break;
779 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200780 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100781 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100782 break;
783 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200784 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200785 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200787 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200788 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200789 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790
791 /* make sure that the newest revision is at position 0 */
792 lysp_sort_revisions(submod->revs);
793
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200795 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100796 if (latest_sp) {
797 if (submod->revs) {
798 if (!latest_sp->revs) {
799 /* latest has no revision, so mod is anyway newer */
800 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200801 /* the latest_sp is zeroed later when the new module is being inserted into the context */
802 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
803 submod->latest_revision = latest_sp->latest_revision;
804 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100805 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200806 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100807 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200808 } else {
809 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100810 }
811 } else {
812 submod->latest_revision = 1;
813 }
814
Radek Krejcib3289d62019-09-18 12:21:39 +0200815 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200816 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200817 }
818
819 if (latest_sp) {
820 latest_sp->latest_revision = 0;
821 }
822
Michal Vasko7a0b0762020-09-02 16:37:01 +0200823 lys_parser_fill_filepath(ctx, in, &submod->filepath);
824
Michal Vasko7c8439f2020-08-05 13:25:19 +0200825 /* resolve imports and includes */
826 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
827
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100828 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100829 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
830 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100831
David Sedlák1b623122019-08-05 15:27:49 +0200832 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100833 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200834 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100835 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200836 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200837 *submodule = submod;
838 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200839
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100840error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200841 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200842 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100843 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200844 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100845 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200846 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200847 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200848}
849
Michal Vasko45b521c2020-11-04 17:14:39 +0100850/**
851 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
852 *
853 * @param[in] mod Parsed module to add to.
854 * @return LY_SUCCESS on success.
855 * @return LY_ERR on error.
856 */
857static LY_ERR
858lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
859{
860 struct lysp_ext_instance *ext_p;
861 struct lysp_stmt *stmt;
862 struct lysp_import *imp;
863
864 /*
865 * 1) edit-config's operation
866 */
867 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
868 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
869 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
870 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
871 ext_p->flags = LYS_INTERNAL;
872 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
873 ext_p->insubstmt_index = 0;
874
875 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
876 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
877 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
878 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
879 stmt->kw = LY_STMT_TYPE;
880
881 stmt->child = calloc(1, sizeof *stmt->child);
882 stmt = stmt->child;
883 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
884 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
885 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
886 stmt->kw = LY_STMT_ENUM;
887
888 stmt->next = calloc(1, sizeof *stmt->child);
889 stmt = stmt->next;
890 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
891 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
892 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
893 stmt->kw = LY_STMT_ENUM;
894
895 stmt->next = calloc(1, sizeof *stmt->child);
896 stmt = stmt->next;
897 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
898 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
899 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
900 stmt->kw = LY_STMT_ENUM;
901
902 stmt->next = calloc(1, sizeof *stmt->child);
903 stmt = stmt->next;
904 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
905 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
906 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
907 stmt->kw = LY_STMT_ENUM;
908
909 stmt->next = calloc(1, sizeof *stmt->child);
910 stmt = stmt->next;
911 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
912 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
913 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
914 stmt->kw = LY_STMT_ENUM;
915
916 /*
917 * 2) filter's type
918 */
919 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
920 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
921 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
922 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
923 ext_p->flags = LYS_INTERNAL;
924 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
925 ext_p->insubstmt_index = 0;
926
927 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
928 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
929 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
930 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
931 stmt->kw = LY_STMT_TYPE;
932
933 stmt->child = calloc(1, sizeof *stmt->child);
934 stmt = stmt->child;
935 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
936 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
937 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
938 stmt->kw = LY_STMT_ENUM;
939
940 stmt->next = calloc(1, sizeof *stmt->child);
941 stmt = stmt->next;
942 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
943 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
944 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
945 stmt->kw = LY_STMT_ENUM;
946
947 /* if-feature for enum allowed only for YANG 1.1 modules */
948 if (mod->version >= LYS_VERSION_1_1) {
949 stmt->child = calloc(1, sizeof *stmt->child);
950 stmt = stmt->child;
951 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
952 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
953 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
954 stmt->kw = LY_STMT_IF_FEATURE;
955 }
956
957 /*
958 * 3) filter's select
959 */
960 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
961 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
962 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
963 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
964 ext_p->flags = LYS_INTERNAL;
965 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
966 ext_p->insubstmt_index = 0;
967
968 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
969 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
970 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
971 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
972 stmt->kw = LY_STMT_TYPE;
973
974 /* create new imports for the used prefixes */
975 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
976
977 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
978 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
979 imp->flags = LYS_INTERNAL;
980
981 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
982
983 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
984 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
985 imp->flags = LYS_INTERNAL;
986
987 return LY_SUCCESS;
988}
989
990/**
991 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
992 *
993 * @param[in] mod Parsed module to add to.
994 * @return LY_SUCCESS on success.
995 * @return LY_ERR on error.
996 */
997static LY_ERR
998lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
999{
1000 struct lysp_ext_instance *ext_p;
1001 struct lysp_stmt *stmt;
1002 struct lysp_import *imp;
1003
1004 /* add new extension instance */
1005 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1006
1007 /* fill in the extension instance fields */
1008 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1009 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1010 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
1011 ext_p->flags = LYS_INTERNAL;
1012 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1013 ext_p->insubstmt_index = 0;
1014
1015 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1016 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1017 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1018 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
1019 stmt->kw = LY_STMT_TYPE;
1020
1021 /* create new import for the used prefix */
1022 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1023
1024 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1025 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1026 imp->flags = LYS_INTERNAL;
1027
1028 return LY_SUCCESS;
1029}
1030
Michal Vasko3a41dff2020-07-15 14:30:28 +02001031LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001032lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001033 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 +01001034 void *check_data, const char **features, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001035{
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001036 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001037 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001038 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001039 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001040 struct lys_yang_parser_ctx *yangctx = NULL;
1041 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001042 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001043 char *filename, *rev, *dot;
1044 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +02001045
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001046 LY_CHECK_ARG_RET(ctx, ctx, in, !features || implement, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001047 if (module) {
1048 *module = NULL;
1049 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001050
1051 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001052 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001054
1055 switch (format) {
1056 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001057 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001058 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001059 break;
1060 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001061 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001062 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001063 break;
1064 default:
1065 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001066 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001067 break;
1068 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001069 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001070
1071 /* make sure that the newest revision is at position 0 */
1072 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001073 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001074 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001075 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001076
Radek Krejcib3289d62019-09-18 12:21:39 +02001077 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001078 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001079 if (latest) {
1080 if (mod->revision) {
1081 if (!latest->revision) {
1082 /* latest has no revision, so mod is anyway newer */
1083 mod->latest_revision = latest->latest_revision;
1084 /* the latest is zeroed later when the new module is being inserted into the context */
1085 } else if (strcmp(mod->revision, latest->revision) > 0) {
1086 mod->latest_revision = latest->latest_revision;
1087 /* the latest is zeroed later when the new module is being inserted into the context */
1088 } else {
1089 latest = NULL;
1090 }
1091 } else {
1092 latest = NULL;
1093 }
1094 } else {
1095 mod->latest_revision = 1;
1096 }
1097
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001098 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001099 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001100 }
1101
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001102 /* check for duplicity in the context */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001103 if (implement && ly_ctx_get_module_implemented(ctx, mod->name)) {
1104 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
1105 ret = LY_EDENIED;
1106 goto error;
1107 }
Michal Vasko22df3f02020-08-24 13:29:22 +02001108 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001109 if (mod_dup) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001110 if (mod->parsed->revs) {
1111 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
1112 mod->name, mod->parsed->revs[0].date);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001113 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001114 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
1115 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001116 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001117 ret = LY_EEXIST;
1118 goto error;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001120
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001121 switch (in->type) {
1122 case LY_IN_FILEPATH:
1123 /* check that name and revision match filename */
1124 filename = strrchr(in->method.fpath.filepath, '/');
1125 if (!filename) {
1126 filename = in->method.fpath.filepath;
1127 } else {
1128 filename++;
1129 }
1130 rev = strchr(filename, '@');
1131 dot = strrchr(filename, '.');
1132
1133 /* name */
1134 len = strlen(mod->name);
1135 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001136 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001137 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1138 }
1139 if (rev) {
1140 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001141 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001142 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001143 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001144 }
1145 }
1146
1147 break;
1148 case LY_IN_FD:
1149 case LY_IN_FILE:
1150 case LY_IN_MEMORY:
1151 /* nothing special to do */
1152 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001153 case LY_IN_ERROR:
1154 LOGINT(ctx);
1155 ret = LY_EINT;
1156 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001157 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001158 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001159
Michal Vasko7a0b0762020-09-02 16:37:01 +02001160 if (latest) {
1161 latest->latest_revision = 0;
1162 }
1163
Michal Vasko45b521c2020-11-04 17:14:39 +01001164 /* add internal data in case specific modules were parsed */
1165 if (!strcmp(mod->name, "ietf-netconf")) {
1166 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), error);
1167 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
1168 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), error);
1169 }
1170
Michal Vasko7a0b0762020-09-02 16:37:01 +02001171 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001172 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001173 LY_CHECK_GOTO(ret, error);
1174 ctx->module_set_id++;
1175
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001176 /* resolve includes and all imports */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001177 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1178
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001179 /* check name collisions */
1180 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), error_ctx);
1181 /* TODO groupings */
1182 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), error_ctx);
1183 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), error_ctx);
1184
1185 /* compile features */
1186 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), error_ctx);
1187
Michal Vasko89b5c072020-10-06 13:52:44 +02001188 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001189 /* pre-compile identities of the module */
1190 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
1191
1192 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001193 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001194 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001195 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1196 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001197 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001198 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001199 /* implement (compile) */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001200 LY_CHECK_GOTO(ret = lys_set_implemented(mod, features), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001201 }
1202
1203 if (format == LYS_IN_YANG) {
1204 yang_parser_ctx_free(yangctx);
1205 } else {
1206 yin_parser_ctx_free(yinctx);
1207 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001208 if (module) {
1209 *module = mod;
1210 }
1211 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001212
1213error_ctx:
1214 ly_set_rm(&ctx->list, mod, NULL);
1215error:
1216 lys_module_free(mod, NULL);
1217 if (pctx) {
1218 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1219 }
1220 if (format == LYS_IN_YANG) {
1221 yang_parser_ctx_free(yangctx);
1222 } else {
1223 yin_parser_ctx_free(yinctx);
1224 }
1225
1226 return ret;
1227}
1228
1229API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001230lys_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 +02001231{
1232 if (module) {
1233 *module = NULL;
1234 }
1235 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1236
1237 /* remember input position */
1238 in->func_start = in->current;
1239
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001240 return lys_create_module(ctx, in, format, 1, NULL, NULL, features, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001241}
1242
Michal Vasko3a41dff2020-07-15 14:30:28 +02001243API LY_ERR
1244lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001245{
Radek Krejci0f969882020-08-21 16:56:47 +02001246 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001247 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001248
Michal Vasko3a41dff2020-07-15 14:30:28 +02001249 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001250
Michal Vasko3a41dff2020-07-15 14:30:28 +02001251 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 +02001252
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001253 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001254 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001255
Michal Vasko3a41dff2020-07-15 14:30:28 +02001256 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001257}
1258
Michal Vasko3a41dff2020-07-15 14:30:28 +02001259API LY_ERR
1260lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001261{
Radek Krejci0f969882020-08-21 16:56:47 +02001262 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001263 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001264
Michal Vasko3a41dff2020-07-15 14:30:28 +02001265 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001266
Michal Vasko3a41dff2020-07-15 14:30:28 +02001267 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 +02001268
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001269 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001270 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001271
Michal Vasko3a41dff2020-07-15 14:30:28 +02001272 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001273}
1274
Michal Vasko3a41dff2020-07-15 14:30:28 +02001275API LY_ERR
1276lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001277{
Radek Krejci0f969882020-08-21 16:56:47 +02001278 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001279 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001280
Michal Vasko3a41dff2020-07-15 14:30:28 +02001281 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001282
Michal Vasko3a41dff2020-07-15 14:30:28 +02001283 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001284 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001285
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001286 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001287 ly_in_free(in, 0);
1288
Michal Vasko3a41dff2020-07-15 14:30:28 +02001289 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001290}
1291
1292API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001293lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001294 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001295{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001296 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001297 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001298 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001299 char *wd, *wn = NULL;
1300 DIR *dir = NULL;
1301 struct dirent *file;
1302 char *match_name = NULL;
1303 LYS_INFORMAT format_aux, match_format = 0;
1304 struct ly_set *dirs;
1305 struct stat st;
1306
1307 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1308
1309 /* start to fill the dir fifo with the context's search path (if set)
1310 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001311 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001312
1313 len = strlen(name);
1314 if (cwd) {
1315 wd = get_current_dir_name();
1316 if (!wd) {
1317 LOGMEM(NULL);
1318 goto cleanup;
1319 } else {
1320 /* add implicit current working directory (./) to be searched,
1321 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001322 ret = ly_set_add(dirs, wd, 0, NULL);
1323 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001324 implicit_cwd = 1;
1325 }
1326 }
1327 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001328 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001329 /* check for duplicities with the implicit current working directory */
1330 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1331 implicit_cwd = 0;
1332 continue;
1333 }
1334 wd = strdup(searchpaths[i]);
1335 if (!wd) {
1336 LOGMEM(NULL);
1337 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001338 } else {
1339 ret = ly_set_add(dirs, wd, 0, NULL);
1340 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001341 }
1342 }
1343 }
1344 wd = NULL;
1345
1346 /* start searching */
1347 while (dirs->count) {
1348 free(wd);
1349 free(wn); wn = NULL;
1350
1351 dirs->count--;
1352 wd = (char *)dirs->objs[dirs->count];
1353 dirs->objs[dirs->count] = NULL;
1354 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1355
1356 if (dir) {
1357 closedir(dir);
1358 }
1359 dir = opendir(wd);
1360 dir_len = strlen(wd);
1361 if (!dir) {
1362 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1363 } else {
1364 while ((file = readdir(dir))) {
1365 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1366 /* skip . and .. */
1367 continue;
1368 }
1369 free(wn);
1370 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1371 LOGMEM(NULL);
1372 goto cleanup;
1373 }
1374 if (stat(wn, &st) == -1) {
1375 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001376 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001377 continue;
1378 }
1379 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1380 /* we have another subdirectory in searchpath to explore,
1381 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001382 ret = ly_set_add(dirs, wn, 0, NULL);
1383 LY_CHECK_GOTO(ret, cleanup);
1384
Radek Krejcid33273d2018-10-25 14:55:52 +02001385 /* continue with the next item in current directory */
1386 wn = NULL;
1387 continue;
1388 } else if (!S_ISREG(st.st_mode)) {
1389 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1390 continue;
1391 }
1392
1393 /* here we know that the item is a file which can contain a module */
1394 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001395 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001396 /* different filename than the module we search for */
1397 continue;
1398 }
1399
1400 /* get type according to filename suffix */
1401 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001402 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001403 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001404 /* TODO YIN parser
1405 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1406 format_aux = LYS_IN_YIN;
1407 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001408 } else {
1409 /* not supportde suffix/file format */
1410 continue;
1411 }
1412
1413 if (revision) {
1414 /* we look for the specific revision, try to get it from the filename */
1415 if (file->d_name[len] == '@') {
1416 /* check revision from the filename */
1417 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1418 /* another revision */
1419 continue;
1420 } else {
1421 /* exact revision */
1422 free(match_name);
1423 match_name = wn;
1424 wn = NULL;
1425 match_len = dir_len + 1 + len;
1426 match_format = format_aux;
1427 goto success;
1428 }
1429 } else {
1430 /* continue trying to find exact revision match, use this only if not found */
1431 free(match_name);
1432 match_name = wn;
1433 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001434 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001435 match_format = format_aux;
1436 continue;
1437 }
1438 } else {
1439 /* remember the revision and try to find the newest one */
1440 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001441 if ((file->d_name[len] != '@') ||
1442 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 +02001443 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001444 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001445 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1446 continue;
1447 }
1448 free(match_name);
1449 }
1450
1451 match_name = wn;
1452 wn = NULL;
1453 match_len = dir_len + 1 + len;
1454 match_format = format_aux;
1455 continue;
1456 }
1457 }
1458 }
1459 }
1460
1461success:
1462 (*localfile) = match_name;
1463 match_name = NULL;
1464 if (format) {
1465 (*format) = match_format;
1466 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001467 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001468
1469cleanup:
1470 free(wn);
1471 free(wd);
1472 if (dir) {
1473 closedir(dir);
1474 }
1475 free(match_name);
1476 ly_set_free(dirs, free);
1477
1478 return ret;
1479}