blob: 25325e37752cf4e1b6031a6b82f5d5fe49836aaf [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
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020017
Radek Krejcica376bd2020-06-11 16:04:06 +020018#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020019
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <assert.h>
Radek Krejci545b4872020-11-15 10:15:12 +010021#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020022#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdlib.h>
27#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020030
Radek Krejcica376bd2020-06-11 16:04:06 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010037#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020038#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020039#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020040#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "schema_compile.h"
42#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010043#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020044#include "set.h"
45#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010046#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020047#include "tree_schema_internal.h"
48#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020049
Michal Vaskof1ab44f2020-10-22 08:58:32 +020050API LY_ERR
51lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
52{
Michal Vasko1d972ca2020-11-03 17:16:56 +010053 struct lysc_node *elem, *elem2;
Radek Krejci2a9fc652021-01-22 17:44:34 +010054 const struct lysc_node_action *action;
55 const struct lysc_node_notif *notif;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020056
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
Radek Krejci2a9fc652021-01-22 17:44:34 +010063 LY_LIST_FOR(lysc_node_actions(elem), action) {
64 LYSC_TREE_DFS_BEGIN(action, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020065 /* action subtree */
66 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
67
Radek Krejci2a9fc652021-01-22 17:44:34 +010068 LYSC_TREE_DFS_END(action, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020069 }
70 }
71
Radek Krejci2a9fc652021-01-22 17:44:34 +010072 LY_LIST_FOR(lysc_node_notifs(elem), notif) {
73 LYSC_TREE_DFS_BEGIN(notif, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020074 /* notification subtree */
75 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
76
Radek Krejci2a9fc652021-01-22 17:44:34 +010077 LYSC_TREE_DFS_END(notif, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020078 }
79 }
80
81 LYSC_TREE_DFS_END(root, elem);
82 }
83
84 return LY_SUCCESS;
85}
86
87API LY_ERR
88lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
89{
Michal Vasko2336cf52020-11-03 17:18:15 +010090 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020091
92 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
93
94 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +010095 LY_LIST_FOR(mod->compiled->data, root) {
96 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
97 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +020098
99 /* RPCs */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100100 LY_LIST_FOR((const struct lysc_node *)mod->compiled->rpcs, root) {
101 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200102 }
103
104 /* notifications */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100105 LY_LIST_FOR((const struct lysc_node *)mod->compiled->notifs, root) {
106 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200107 }
108
109 return LY_SUCCESS;
110}
111
Radek Krejcib93bd412020-11-02 13:23:11 +0100112static void
113lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
114{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100115 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100116 if (first_case->child) {
117 /* there is something to return */
118 (*next) = first_case->child;
119 return;
120 }
121 }
122
123 /* no children in choice's cases, so go to the choice's sibling instead of into it */
124 (*last) = (*next);
125 (*next) = (*next)->next;
126}
127
Radek Krejcia3045382018-11-22 14:30:31 +0100128API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200129lys_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 +0100130{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100131 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100132 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200133 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejcia3045382018-11-22 14:30:31 +0100134
135 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
136
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200137next:
Radek Krejcia3045382018-11-22 14:30:31 +0100138 if (!last) {
139 /* first call */
140
141 /* get know where to start */
142 if (parent) {
143 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200144 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200145 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100146 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100147 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100148 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100149 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100150 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200151 if (snode && *snode) {
152 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100153 }
Radek Krejcia3045382018-11-22 14:30:31 +0100154 }
Radek Krejcia3045382018-11-22 14:30:31 +0100155 } else {
156 /* top level data */
157 next = last = module->data;
158 }
159 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100160 /* try to get action or notification */
161 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100162 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100163 /* test if the next can be returned */
164 goto check;
165
Michal Vasko1bf09392020-03-27 12:38:10 +0100166 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100167 action_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100168 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100169 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100170 action_flag = notif_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100171 next = last->next;
Michal Vasko20424b42020-08-31 12:29:38 +0200172 } else {
173 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100174 }
175
Radek Krejcia3045382018-11-22 14:30:31 +0100176repeat:
177 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100178 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200179 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100180 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200181 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100182 } else if (!action_flag) {
183 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200184 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100185 } else if (!notif_flag) {
186 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200187 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100188 } else {
189 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100190 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100191 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100192 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100193check:
Radek Krejcia3045382018-11-22 14:30:31 +0100194 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100195 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100196 case LYS_ACTION:
197 case LYS_NOTIF:
198 case LYS_LEAF:
199 case LYS_ANYXML:
200 case LYS_ANYDATA:
201 case LYS_LIST:
202 case LYS_LEAFLIST:
203 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200204 case LYS_CASE:
205 if (options & LYS_GETNEXT_WITHCASE) {
206 break;
207 } else {
208 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100209 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200210 }
211 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100212 case LYS_CONTAINER:
213 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
214 if (((struct lysc_node_container *)next)->child) {
215 /* go into */
216 next = ((struct lysc_node_container *)next)->child;
217 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100218 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100219 next = next->next;
220 }
221 goto repeat;
222 }
223 break;
224 case LYS_CHOICE:
225 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200226 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100227 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
228 next = next->next;
229 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100230 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200231 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100232 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100233 /* go into */
234 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100235 }
Radek Krejcia3045382018-11-22 14:30:31 +0100236 }
237 goto repeat;
238 default:
239 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200240 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100241 return NULL;
242 }
243
Radek Krejcia3045382018-11-22 14:30:31 +0100244 return next;
245}
246
247API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100248lys_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 +0200249 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100250{
251 const struct lysc_node *node = NULL;
252
253 LY_CHECK_ARG_RET(NULL, module, name, NULL);
254 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100255 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100256 }
257
258 while ((node = lys_getnext(node, parent, module->compiled, options))) {
259 if (!(node->nodetype & nodetype)) {
260 continue;
261 }
262 if (node->module != module) {
263 continue;
264 }
265
266 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200267 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100268 return node;
269 }
270 } else {
271 if (!strcmp(node->name, name)) {
272 return node;
273 }
274 }
275 }
276 return NULL;
277}
278
Michal Vasko519fd602020-05-26 12:17:39 +0200279API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100280lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
281 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200282{
283 LY_ERR ret = LY_SUCCESS;
284 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200285 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200286 uint32_t i;
287
Michal Vasko26512682021-01-11 11:35:40 +0100288 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200289 if (!(options & LYXP_SCNODE_ALL)) {
290 options = LYXP_SCNODE;
291 }
Michal Vasko26512682021-01-11 11:35:40 +0100292 if (!ctx) {
293 ctx = ctx_node->module->ctx;
294 }
Michal Vasko519fd602020-05-26 12:17:39 +0200295
296 memset(&xp_set, 0, sizeof xp_set);
297
298 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100299 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200300 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200301
302 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100303 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200304 LY_CHECK_GOTO(ret, cleanup);
305
306 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200307 ret = ly_set_new(set);
308 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200309
310 /* transform into ly_set */
311 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100312 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200313 (*set)->size = xp_set.used;
314
315 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200316 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200317 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200318 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200319 }
320 }
321
322cleanup:
323 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100324 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200325 return ret;
326}
327
Michal Vasko072de482020-08-05 13:27:21 +0200328API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200329lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
330 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
331{
332 LY_ERR ret = LY_SUCCESS;
333 struct lyxp_set xp_set = {0};
334 uint32_t i;
335
336 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
337 if (!(options & LYXP_SCNODE_ALL)) {
338 options = LYXP_SCNODE;
339 }
340
341 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100342 ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
Michal Vasko40308e72020-10-20 16:38:40 +0200343 LY_CHECK_GOTO(ret, cleanup);
344
345 /* allocate return set */
346 ret = ly_set_new(set);
347 LY_CHECK_GOTO(ret, cleanup);
348
349 /* transform into ly_set */
350 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
351 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
352 (*set)->size = xp_set.used;
353
354 for (i = 0; i < xp_set.used; ++i) {
Michal Vaskod97959c2020-12-10 12:18:28 +0100355 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM)) {
356 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM) ||
357 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200358 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
359 LY_CHECK_GOTO(ret, cleanup);
360 }
361 }
362
363cleanup:
364 lyxp_set_free_content(&xp_set);
365 if (ret) {
366 ly_set_free(*set, NULL);
367 *set = NULL;
368 }
369 return ret;
370}
371
372API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100373lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
374 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200375{
376 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200377 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200378 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200379 uint32_t i;
380
Michal Vasko26512682021-01-11 11:35:40 +0100381 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200382 if (!(options & LYXP_SCNODE_ALL)) {
383 options = LYXP_SCNODE;
384 }
Michal Vasko26512682021-01-11 11:35:40 +0100385 if (!ctx) {
386 ctx = ctx_node->module->ctx;
387 }
Michal Vasko072de482020-08-05 13:27:21 +0200388
Michal Vasko072de482020-08-05 13:27:21 +0200389 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100390 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200391 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200392
393 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100394 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200395 LY_CHECK_GOTO(ret, cleanup);
396
397 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200398 ret = ly_set_new(set);
399 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200400
401 /* transform into ly_set */
402 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100403 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200404 (*set)->size = xp_set.used;
405
406 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100407 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200408 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200409 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200410 }
411 }
412
413cleanup:
414 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100415 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200416 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200417 ly_set_free(*set, NULL);
418 *set = NULL;
419 }
Michal Vasko072de482020-08-05 13:27:21 +0200420 return ret;
421}
422
Radek Krejcibc5644c2020-10-27 14:53:17 +0100423API LY_ERR
424lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
425{
426 LY_ERR ret = LY_SUCCESS;
427 LY_ARRAY_COUNT_TYPE u, v;
428
429 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
430
431 /* allocate return set */
432 LY_CHECK_RET(ly_set_new(set));
433
434 LY_ARRAY_FOR(path, u) {
435 /* add nodes from the path */
436 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
437 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
438 LY_ARRAY_FOR(path[u].predicates, v) {
439 /* add all the keys in a predicate */
440 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
441 }
442 }
443 }
444
445cleanup:
446 if (ret) {
447 ly_set_free(*set, NULL);
448 *set = NULL;
449 }
450 return ret;
451}
452
453API LY_ERR
454lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
455 struct ly_set **set)
456{
457 LY_ERR ret = LY_SUCCESS;
458 uint8_t oper;
459 struct lyxp_expr *expr = NULL;
460 struct ly_path *p = NULL;
461
462 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
463
464 if (!ctx) {
465 ctx = ctx_node->module->ctx;
466 }
467
468 /* parse */
469 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
470 LY_CHECK_GOTO(ret, cleanup);
471
472 /* compile */
473 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
474 ret = ly_path_compile(ctx, NULL, ctx_node, expr, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100475 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100476 LY_CHECK_GOTO(ret, cleanup);
477
478 /* resolve */
479 ret = lys_find_lypath_atoms(p, set);
480
481cleanup:
482 ly_path_free(ctx, p);
483 lyxp_expr_free(ctx, expr);
484 return ret;
485}
486
487API const struct lysc_node *
488lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
489{
490 const struct lysc_node *snode = NULL;
491 struct lyxp_expr *exp = NULL;
492 struct ly_path *p = NULL;
493 LY_ERR ret;
494 uint8_t oper;
495
496 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
497
498 if (!ctx) {
499 ctx = ctx_node->module->ctx;
500 }
501
502 /* parse */
503 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
504 LY_CHECK_GOTO(ret, cleanup);
505
506 /* compile */
507 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
508 ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100509 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100510 LY_CHECK_GOTO(ret, cleanup);
511
512 /* get last node */
513 snode = p[LY_ARRAY_COUNT(p) - 1].node;
514
515cleanup:
516 ly_path_free(ctx, p);
517 lyxp_expr_free(ctx, exp);
518 return snode;
519}
520
Michal Vasko14654712020-02-06 08:35:21 +0100521char *
522lysc_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 +0200523 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200524{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200525 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200526 char *path = NULL;
527 int len = 0;
528
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200529 LY_CHECK_ARG_RET(NULL, node, NULL);
530 if (buffer) {
531 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100532 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200533 }
534
Radek Krejci327de162019-06-14 12:52:07 +0200535 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200536 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200537 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100538 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200539 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100540 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200541
Michal Vasko65de0402020-08-03 16:34:19 +0200542 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
543 /* schema-only node */
544 continue;
545 }
546
Michal Vasko11deea12020-08-05 13:54:50 +0200547 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200548 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100549 if (parent && (iter->parent == parent)) {
550 slash = "";
551 } else {
552 slash = "/";
553 }
Michal Vasko69730152020-10-09 16:30:07 +0200554 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200555 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200556 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100557 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200558 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100559 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200560 }
Radek Krejci327de162019-06-14 12:52:07 +0200561 } else {
562 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200563 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100564 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200565 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100566 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200567 }
Radek Krejci327de162019-06-14 12:52:07 +0200568 }
569 free(s);
570 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200571
Michal Vasko69730152020-10-09 16:30:07 +0200572 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200573 /* not enough space in buffer */
574 break;
575 }
Radek Krejci327de162019-06-14 12:52:07 +0200576 }
577
578 if (len < 0) {
579 free(path);
580 path = NULL;
581 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200582 if (buffer) {
583 strcpy(buffer, "/");
584 } else {
585 path = strdup("/");
586 }
Radek Krejci327de162019-06-14 12:52:07 +0200587 }
588 break;
589 }
590
Radek Krejci1c0c3442019-07-23 16:08:47 +0200591 if (buffer) {
592 return buffer;
593 } else {
594 return path;
595 }
Radek Krejci327de162019-06-14 12:52:07 +0200596}
597
Michal Vasko14654712020-02-06 08:35:21 +0100598API char *
599lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
600{
601 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
602}
603
Michal Vasko28d78432020-05-26 13:10:53 +0200604API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200605lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200606{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100607 struct lysc_node_action *act;
608 struct lysc_node_notif *notif;
Radek Krejciaf9cd802020-10-06 21:59:47 +0200609
Radek Krejci19cf8052020-08-18 15:02:38 +0200610 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
611
Radek Krejciaf9cd802020-10-06 21:59:47 +0200612 switch (node->nodetype) {
613 case LYS_CONTAINER:
614 case LYS_CHOICE:
615 case LYS_CASE:
616 case LYS_LEAF:
617 case LYS_LEAFLIST:
618 case LYS_LIST:
619 case LYS_ANYXML:
620 case LYS_ANYDATA:
621 if (prev_priv_p) {
622 *prev_priv_p = node->priv;
623 }
624 ((struct lysc_node *)node)->priv = priv;
625 break;
626 case LYS_RPC:
627 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100628 act = (struct lysc_node_action *)node;
Radek Krejciaf9cd802020-10-06 21:59:47 +0200629 if (prev_priv_p) {
630 *prev_priv_p = act->priv;
631 }
632 act->priv = priv;
633 break;
634 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100635 notif = (struct lysc_node_notif *)node;
Radek Krejciaf9cd802020-10-06 21:59:47 +0200636 if (prev_priv_p) {
637 *prev_priv_p = notif->priv;
638 }
639 notif->priv = priv;
640 break;
641 default:
642 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200643 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200644
645 return LY_SUCCESS;
646}
647
Michal Vasko405cc9e2020-12-01 12:01:27 +0100648LY_ERR
649lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200650{
651 struct lys_module *m;
652
Michal Vasko405cc9e2020-12-01 12:01:27 +0100653 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200654
655 /* we have module from the current context */
656 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
657 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200658 assert(m != mod);
659
660 /* check collision with other implemented revision */
661 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
662 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
663 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200664 }
665
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100666 /* enable features */
667 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
668
Michal Vasko89b5c072020-10-06 13:52:44 +0200669 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100670 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200671
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200672 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200673 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200674
675 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100676 return lys_compile(mod, 0, unres);
677}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200678
Michal Vasko405cc9e2020-12-01 12:01:27 +0100679API LY_ERR
680lys_set_implemented(struct lys_module *mod, const char **features)
681{
682 LY_ERR ret = LY_SUCCESS, r;
683 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200684
Michal Vasko405cc9e2020-12-01 12:01:27 +0100685 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100686
Michal Vasko405cc9e2020-12-01 12:01:27 +0100687 if (mod->implemented) {
688 /* mod is already implemented, set the features */
689 r = lys_set_features(mod->parsed, features);
690 if (r == LY_EEXIST) {
691 /* no changes */
692 return LY_SUCCESS;
693 } else if (r) {
694 /* error */
695 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200696 }
697
Michal Vasko405cc9e2020-12-01 12:01:27 +0100698 /* full recompilation */
699 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200700 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100701
Michal Vasko405cc9e2020-12-01 12:01:27 +0100702 /* implement this module and any other required modules, recursively */
703 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100704
705 /* the first module being implemented is finished, resolve global unres, consolidate the set */
706 if (!ret) {
707 ret = lys_compile_unres_glob(mod->ctx, &unres);
708 }
709 if (ret) {
710 /* failure, full compile revert */
711 lys_compile_unres_glob_revert(mod->ctx, &unres);
712 }
713
714 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200715 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200716}
717
Michal Vasko7c8439f2020-08-05 13:25:19 +0200718static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100719lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200720{
721 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200722 LY_ARRAY_COUNT_TYPE u, v;
723
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100724 pmod->parsing = 1;
725 LY_ARRAY_FOR(pmod->imports, u) {
726 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200727 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100728 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
729 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200730 }
731 /* check for importing the same module twice */
732 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100733 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200734 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
735 }
736 }
737 }
Radek Krejci771928a2021-01-19 13:42:36 +0100738 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
739
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100740 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200741
742 return LY_SUCCESS;
743}
744
Michal Vasko3a41dff2020-07-15 14:30:28 +0200745LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200746lys_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 +0200747 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200748 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200749{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200750 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100751 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100752 struct lys_yang_parser_ctx *yangctx = NULL;
753 struct lys_yin_parser_ctx *yinctx = NULL;
754 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100755
Michal Vasko3a41dff2020-07-15 14:30:28 +0200756 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100757
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100758 switch (format) {
759 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200760 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100761 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762 break;
763 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200764 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100765 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100766 break;
767 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200768 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200769 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100770 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200771 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200772 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200773 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100774
775 /* make sure that the newest revision is at position 0 */
776 lysp_sort_revisions(submod->revs);
777
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100778 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200779 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100780 if (latest_sp) {
781 if (submod->revs) {
782 if (!latest_sp->revs) {
783 /* latest has no revision, so mod is anyway newer */
784 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200785 /* the latest_sp is zeroed later when the new module is being inserted into the context */
786 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
787 submod->latest_revision = latest_sp->latest_revision;
788 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100789 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200790 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100791 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200792 } else {
793 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794 }
795 } else {
796 submod->latest_revision = 1;
797 }
798
Radek Krejcib3289d62019-09-18 12:21:39 +0200799 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200800 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200801 }
802
803 if (latest_sp) {
804 latest_sp->latest_revision = 0;
805 }
806
Michal Vasko7a0b0762020-09-02 16:37:01 +0200807 lys_parser_fill_filepath(ctx, in, &submod->filepath);
808
Michal Vasko7c8439f2020-08-05 13:25:19 +0200809 /* resolve imports and includes */
810 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
811
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100812 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100813 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
814 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100815
David Sedlák1b623122019-08-05 15:27:49 +0200816 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100817 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200818 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100819 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200820 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200821 *submodule = submod;
822 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200823
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100824error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200825 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200826 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100827 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200828 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100829 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200830 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200831 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200832}
833
Michal Vasko45b521c2020-11-04 17:14:39 +0100834/**
835 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
836 *
837 * @param[in] mod Parsed module to add to.
838 * @return LY_SUCCESS on success.
839 * @return LY_ERR on error.
840 */
841static LY_ERR
842lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
843{
844 struct lysp_ext_instance *ext_p;
845 struct lysp_stmt *stmt;
846 struct lysp_import *imp;
847
848 /*
849 * 1) edit-config's operation
850 */
851 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
852 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
853 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
854 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
855 ext_p->flags = LYS_INTERNAL;
856 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
857 ext_p->insubstmt_index = 0;
858
859 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
860 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
861 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
862 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
863 stmt->kw = LY_STMT_TYPE;
864
865 stmt->child = calloc(1, sizeof *stmt->child);
866 stmt = stmt->child;
867 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
868 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
869 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
870 stmt->kw = LY_STMT_ENUM;
871
872 stmt->next = calloc(1, sizeof *stmt->child);
873 stmt = stmt->next;
874 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
875 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
876 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
877 stmt->kw = LY_STMT_ENUM;
878
879 stmt->next = calloc(1, sizeof *stmt->child);
880 stmt = stmt->next;
881 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
882 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
883 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
884 stmt->kw = LY_STMT_ENUM;
885
886 stmt->next = calloc(1, sizeof *stmt->child);
887 stmt = stmt->next;
888 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
889 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
890 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
891 stmt->kw = LY_STMT_ENUM;
892
893 stmt->next = calloc(1, sizeof *stmt->child);
894 stmt = stmt->next;
895 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
896 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
897 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
898 stmt->kw = LY_STMT_ENUM;
899
900 /*
901 * 2) filter's type
902 */
903 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
904 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
905 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
906 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
907 ext_p->flags = LYS_INTERNAL;
908 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
909 ext_p->insubstmt_index = 0;
910
911 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
912 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
913 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
914 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
915 stmt->kw = LY_STMT_TYPE;
916
917 stmt->child = calloc(1, sizeof *stmt->child);
918 stmt = stmt->child;
919 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
920 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
921 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
922 stmt->kw = LY_STMT_ENUM;
923
924 stmt->next = calloc(1, sizeof *stmt->child);
925 stmt = stmt->next;
926 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
927 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
928 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
929 stmt->kw = LY_STMT_ENUM;
930
931 /* if-feature for enum allowed only for YANG 1.1 modules */
932 if (mod->version >= LYS_VERSION_1_1) {
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, "if-feature", 0, &stmt->stmt));
937 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
938 stmt->kw = LY_STMT_IF_FEATURE;
939 }
940
941 /*
942 * 3) filter's select
943 */
944 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
945 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
946 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
947 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
948 ext_p->flags = LYS_INTERNAL;
949 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
950 ext_p->insubstmt_index = 0;
951
952 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
953 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
954 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
955 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
956 stmt->kw = LY_STMT_TYPE;
957
958 /* create new imports for the used prefixes */
959 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
960
961 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
962 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
963 imp->flags = LYS_INTERNAL;
964
965 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
966
967 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
968 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
969 imp->flags = LYS_INTERNAL;
970
971 return LY_SUCCESS;
972}
973
974/**
975 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
976 *
977 * @param[in] mod Parsed module to add to.
978 * @return LY_SUCCESS on success.
979 * @return LY_ERR on error.
980 */
981static LY_ERR
982lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
983{
984 struct lysp_ext_instance *ext_p;
985 struct lysp_stmt *stmt;
986 struct lysp_import *imp;
987
988 /* add new extension instance */
989 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
990
991 /* fill in the extension instance fields */
992 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
993 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
994 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
995 ext_p->flags = LYS_INTERNAL;
996 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
997 ext_p->insubstmt_index = 0;
998
999 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1000 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1001 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1002 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
1003 stmt->kw = LY_STMT_TYPE;
1004
1005 /* create new import for the used prefix */
1006 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1007
1008 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1009 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1010 imp->flags = LYS_INTERNAL;
1011
1012 return LY_SUCCESS;
1013}
1014
Michal Vasko3a41dff2020-07-15 14:30:28 +02001015LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +01001016lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001017 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko405cc9e2020-12-01 12:01:27 +01001018 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001019{
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001020 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001021 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001022 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001023 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001024 struct lys_yang_parser_ctx *yangctx = NULL;
1025 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001026 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001027 char *filename, *rev, *dot;
1028 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +01001029 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +02001030
Michal Vasko34e334d2021-01-25 16:12:31 +01001031 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001032
Michal Vasko7a0b0762020-09-02 16:37:01 +02001033 if (module) {
1034 *module = NULL;
1035 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001036
Michal Vasko34e334d2021-01-25 16:12:31 +01001037 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1038 implement = 1;
1039 } else {
1040 implement = need_implemented;
Radek Krejci00a3e8a2021-01-27 08:24:49 +01001041 }
Michal Vasko34e334d2021-01-25 16:12:31 +01001042
Radek Krejci86d106e2018-10-18 09:53:19 +02001043 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001044 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001045 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001046
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001047 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001048 switch (format) {
1049 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001050 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001051 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001052 break;
1053 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001054 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001055 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001056 break;
1057 default:
1058 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001059 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001060 break;
1061 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001062 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001063
1064 /* make sure that the newest revision is at position 0 */
1065 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001066 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001067 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), free_mod_cleanup);
Radek Krejci0af46292019-01-11 16:02:31 +01001068 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001069
Radek Krejcib3289d62019-09-18 12:21:39 +02001070 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001071 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001072 if (latest) {
1073 if (mod->revision) {
1074 if (!latest->revision) {
1075 /* latest has no revision, so mod is anyway newer */
1076 mod->latest_revision = latest->latest_revision;
1077 /* the latest is zeroed later when the new module is being inserted into the context */
1078 } else if (strcmp(mod->revision, latest->revision) > 0) {
1079 mod->latest_revision = latest->latest_revision;
1080 /* the latest is zeroed later when the new module is being inserted into the context */
1081 } else {
1082 latest = NULL;
1083 }
1084 } else {
1085 latest = NULL;
1086 }
1087 } else {
1088 mod->latest_revision = 1;
1089 }
1090
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001091 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001092 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001093 }
1094
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001095 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001096 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001097 if (implement) {
1098 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1099 if (mod_impl && (mod_impl != mod_dup)) {
1100 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1101 mod_impl->revision ? mod_impl->revision : "<none>");
1102 ret = LY_EDENIED;
1103 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001104 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001105 }
1106 if (mod_dup) {
1107 if (implement) {
1108 if (!mod_dup->implemented) {
1109 /* just implement it */
1110 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1111 goto free_mod_cleanup;
1112 }
1113
1114 /* nothing to do */
1115 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1116 mod_dup->revision ? mod_dup->revision : "<none>");
1117 goto free_mod_cleanup;
1118 }
1119
1120 /* nothing to do */
1121 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1122 mod_dup->revision ? mod_dup->revision : "<none>");
1123 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001124 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001125
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001126 switch (in->type) {
1127 case LY_IN_FILEPATH:
1128 /* check that name and revision match filename */
1129 filename = strrchr(in->method.fpath.filepath, '/');
1130 if (!filename) {
1131 filename = in->method.fpath.filepath;
1132 } else {
1133 filename++;
1134 }
1135 rev = strchr(filename, '@');
1136 dot = strrchr(filename, '.');
1137
1138 /* name */
1139 len = strlen(mod->name);
1140 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001141 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001142 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1143 }
1144 if (rev) {
1145 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001146 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001147 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001148 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001149 }
1150 }
1151
1152 break;
1153 case LY_IN_FD:
1154 case LY_IN_FILE:
1155 case LY_IN_MEMORY:
1156 /* nothing special to do */
1157 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001158 case LY_IN_ERROR:
1159 LOGINT(ctx);
1160 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001161 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001162 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001163 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001164
Michal Vasko7a0b0762020-09-02 16:37:01 +02001165 if (latest) {
1166 latest->latest_revision = 0;
1167 }
1168
Michal Vasko45b521c2020-11-04 17:14:39 +01001169 /* add internal data in case specific modules were parsed */
1170 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001171 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001172 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001173 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001174 }
1175
Michal Vasko405cc9e2020-12-01 12:01:27 +01001176 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001177 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001178
Michal Vasko7a0b0762020-09-02 16:37:01 +02001179 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001180 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001181 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001182 ctx->module_set_id++;
1183
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001184 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001185 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001186
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001187 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001188 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001189 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001190 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1191 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001192
1193 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001194 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001195
Michal Vasko89b5c072020-10-06 13:52:44 +02001196 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001197 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001198 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001199
1200 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001201 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001202 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001203 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001204 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001205 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001206 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001207 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001208 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001209 }
1210
Michal Vasko405cc9e2020-12-01 12:01:27 +01001211 /* success */
1212 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001213
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001214free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001215 lys_module_free(mod, NULL);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001216 mod = NULL;
1217
Michal Vasko405cc9e2020-12-01 12:01:27 +01001218cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001219 if (pctx) {
1220 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1221 }
1222 if (format == LYS_IN_YANG) {
1223 yang_parser_ctx_free(yangctx);
1224 } else {
1225 yin_parser_ctx_free(yinctx);
1226 }
1227
Michal Vasko405cc9e2020-12-01 12:01:27 +01001228 if (!ret && module) {
1229 *module = mod;
1230 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001231 return ret;
1232}
1233
Radek Krejci545b4872020-11-15 10:15:12 +01001234static LYS_INFORMAT
1235lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1236{
1237 if (!format && (in->type == LY_IN_FILEPATH)) {
1238 /* unknown format - try to detect it from filename's suffix */
1239 const char *path = in->method.fpath.filepath;
1240 size_t len = strlen(path);
1241
1242 /* ignore trailing whitespaces */
1243 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1244
Radek Krejcif13b87b2020-12-01 22:02:17 +01001245 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1246 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001247 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001248 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1249 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001250 format = LYS_IN_YIN;
1251 } /* else still unknown */
1252 }
1253
1254 return format;
1255}
1256
Michal Vasko7a0b0762020-09-02 16:37:01 +02001257API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001258lys_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 +02001259{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001260 LY_ERR ret;
1261 struct lys_glob_unres unres = {0};
1262
Michal Vasko7a0b0762020-09-02 16:37:01 +02001263 if (module) {
1264 *module = NULL;
1265 }
Radek Krejci545b4872020-11-15 10:15:12 +01001266 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1267
1268 format = lys_parse_get_format(in, format);
1269 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001270
1271 /* remember input position */
1272 in->func_start = in->current;
1273
Michal Vasko405cc9e2020-12-01 12:01:27 +01001274 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1275 LY_CHECK_GOTO(ret, cleanup);
1276
1277 /* resolve global unres */
1278 ret = lys_compile_unres_glob(ctx, &unres);
1279 LY_CHECK_GOTO(ret, cleanup);
1280
1281cleanup:
1282 if (ret) {
1283 lys_compile_unres_glob_revert(ctx, &unres);
1284 }
1285 lys_compile_unres_glob_erase(ctx, &unres);
1286 if (ret && module) {
1287 *module = NULL;
1288 }
1289 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001290}
1291
Michal Vasko3a41dff2020-07-15 14:30:28 +02001292API LY_ERR
1293lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001294{
Radek Krejci0f969882020-08-21 16:56:47 +02001295 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001296 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001297
Michal Vasko3a41dff2020-07-15 14:30:28 +02001298 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001299
Michal Vasko3a41dff2020-07-15 14:30:28 +02001300 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 +02001301
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001302 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001303 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001304
Michal Vasko3a41dff2020-07-15 14:30:28 +02001305 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001306}
1307
Michal Vasko3a41dff2020-07-15 14:30:28 +02001308API LY_ERR
1309lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001310{
Radek Krejci0f969882020-08-21 16:56:47 +02001311 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001312 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001313
Michal Vasko3a41dff2020-07-15 14:30:28 +02001314 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001315
Michal Vasko3a41dff2020-07-15 14:30:28 +02001316 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 +02001317
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001318 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001319 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001320
Michal Vasko3a41dff2020-07-15 14:30:28 +02001321 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001322}
1323
Michal Vasko3a41dff2020-07-15 14:30:28 +02001324API LY_ERR
1325lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001326{
Radek Krejci0f969882020-08-21 16:56:47 +02001327 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001328 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001329
Michal Vasko3a41dff2020-07-15 14:30:28 +02001330 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001331
Michal Vasko3a41dff2020-07-15 14:30:28 +02001332 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001333 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001334
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001335 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001336 ly_in_free(in, 0);
1337
Michal Vasko3a41dff2020-07-15 14:30:28 +02001338 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001339}
1340
1341API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001342lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001343 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001344{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001345 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001346 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001347 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001348 char *wd, *wn = NULL;
1349 DIR *dir = NULL;
1350 struct dirent *file;
1351 char *match_name = NULL;
1352 LYS_INFORMAT format_aux, match_format = 0;
1353 struct ly_set *dirs;
1354 struct stat st;
1355
1356 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1357
1358 /* start to fill the dir fifo with the context's search path (if set)
1359 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001360 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001361
1362 len = strlen(name);
1363 if (cwd) {
1364 wd = get_current_dir_name();
1365 if (!wd) {
1366 LOGMEM(NULL);
1367 goto cleanup;
1368 } else {
1369 /* add implicit current working directory (./) to be searched,
1370 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001371 ret = ly_set_add(dirs, wd, 0, NULL);
1372 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001373 implicit_cwd = 1;
1374 }
1375 }
1376 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001377 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001378 /* check for duplicities with the implicit current working directory */
1379 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1380 implicit_cwd = 0;
1381 continue;
1382 }
1383 wd = strdup(searchpaths[i]);
1384 if (!wd) {
1385 LOGMEM(NULL);
1386 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001387 } else {
1388 ret = ly_set_add(dirs, wd, 0, NULL);
1389 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001390 }
1391 }
1392 }
1393 wd = NULL;
1394
1395 /* start searching */
1396 while (dirs->count) {
1397 free(wd);
1398 free(wn); wn = NULL;
1399
1400 dirs->count--;
1401 wd = (char *)dirs->objs[dirs->count];
1402 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001403 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001404
1405 if (dir) {
1406 closedir(dir);
1407 }
1408 dir = opendir(wd);
1409 dir_len = strlen(wd);
1410 if (!dir) {
1411 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1412 } else {
1413 while ((file = readdir(dir))) {
1414 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1415 /* skip . and .. */
1416 continue;
1417 }
1418 free(wn);
1419 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1420 LOGMEM(NULL);
1421 goto cleanup;
1422 }
1423 if (stat(wn, &st) == -1) {
1424 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001425 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001426 continue;
1427 }
1428 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1429 /* we have another subdirectory in searchpath to explore,
1430 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001431 ret = ly_set_add(dirs, wn, 0, NULL);
1432 LY_CHECK_GOTO(ret, cleanup);
1433
Radek Krejcid33273d2018-10-25 14:55:52 +02001434 /* continue with the next item in current directory */
1435 wn = NULL;
1436 continue;
1437 } else if (!S_ISREG(st.st_mode)) {
1438 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1439 continue;
1440 }
1441
1442 /* here we know that the item is a file which can contain a module */
1443 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001444 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001445 /* different filename than the module we search for */
1446 continue;
1447 }
1448
1449 /* get type according to filename suffix */
1450 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001451 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1452 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001453 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001454 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1455 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001456 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001457 } else {
1458 /* not supportde suffix/file format */
1459 continue;
1460 }
1461
1462 if (revision) {
1463 /* we look for the specific revision, try to get it from the filename */
1464 if (file->d_name[len] == '@') {
1465 /* check revision from the filename */
1466 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1467 /* another revision */
1468 continue;
1469 } else {
1470 /* exact revision */
1471 free(match_name);
1472 match_name = wn;
1473 wn = NULL;
1474 match_len = dir_len + 1 + len;
1475 match_format = format_aux;
1476 goto success;
1477 }
1478 } else {
1479 /* continue trying to find exact revision match, use this only if not found */
1480 free(match_name);
1481 match_name = wn;
1482 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001483 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001484 match_format = format_aux;
1485 continue;
1486 }
1487 } else {
1488 /* remember the revision and try to find the newest one */
1489 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001490 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001491 lysp_check_date(NULL, &file->d_name[len + 1],
1492 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001493 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001494 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001495 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1496 continue;
1497 }
1498 free(match_name);
1499 }
1500
1501 match_name = wn;
1502 wn = NULL;
1503 match_len = dir_len + 1 + len;
1504 match_format = format_aux;
1505 continue;
1506 }
1507 }
1508 }
1509 }
1510
1511success:
1512 (*localfile) = match_name;
1513 match_name = NULL;
1514 if (format) {
1515 (*format) = match_format;
1516 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001517 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001518
1519cleanup:
1520 free(wn);
1521 free(wd);
1522 if (dir) {
1523 closedir(dir);
1524 }
1525 free(match_name);
1526 ly_set_free(dirs, free);
1527
1528 return ret;
1529}