blob: 031dc33edb22371e76c866571e63cd8984a157b3 [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 Vaskod1e53b92021-01-28 13:11:06 +0100144 next = last = lysc_node_children(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_IS_OUTPUT : LYS_IS_INPUT);
Radek Krejcia3045382018-11-22 14:30:31 +0100145 } else {
146 /* top level data */
147 next = last = module->data;
148 }
149 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100150 /* try to get action or notification */
151 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100152 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100153 /* test if the next can be returned */
154 goto check;
155
Michal Vasko1bf09392020-03-27 12:38:10 +0100156 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100157 action_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100158 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100159 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100160 action_flag = notif_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100161 next = last->next;
Michal Vasko20424b42020-08-31 12:29:38 +0200162 } else {
163 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100164 }
165
Radek Krejcia3045382018-11-22 14:30:31 +0100166repeat:
167 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100168 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200169 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100170 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200171 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100172 } else if (!action_flag) {
173 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200174 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100175 } else if (!notif_flag) {
176 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200177 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100178 } else {
179 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100180 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100181 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100182 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100183check:
Radek Krejcia3045382018-11-22 14:30:31 +0100184 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100185 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100186 case LYS_ACTION:
187 case LYS_NOTIF:
188 case LYS_LEAF:
189 case LYS_ANYXML:
190 case LYS_ANYDATA:
191 case LYS_LIST:
192 case LYS_LEAFLIST:
193 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200194 case LYS_CASE:
195 if (options & LYS_GETNEXT_WITHCASE) {
196 break;
197 } else {
198 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100199 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200200 }
201 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100202 case LYS_CONTAINER:
203 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
204 if (((struct lysc_node_container *)next)->child) {
205 /* go into */
206 next = ((struct lysc_node_container *)next)->child;
207 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100208 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100209 next = next->next;
210 }
211 goto repeat;
212 }
213 break;
214 case LYS_CHOICE:
215 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200216 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100217 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
218 next = next->next;
219 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100220 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200221 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100222 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100223 /* go into */
224 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100225 }
Radek Krejcia3045382018-11-22 14:30:31 +0100226 }
227 goto repeat;
228 default:
229 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200230 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100231 return NULL;
232 }
233
Radek Krejcia3045382018-11-22 14:30:31 +0100234 return next;
235}
236
237API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100238lys_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 +0200239 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100240{
241 const struct lysc_node *node = NULL;
242
243 LY_CHECK_ARG_RET(NULL, module, name, NULL);
244 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100245 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100246 }
247
248 while ((node = lys_getnext(node, parent, module->compiled, options))) {
249 if (!(node->nodetype & nodetype)) {
250 continue;
251 }
252 if (node->module != module) {
253 continue;
254 }
255
256 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200257 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100258 return node;
259 }
260 } else {
261 if (!strcmp(node->name, name)) {
262 return node;
263 }
264 }
265 }
266 return NULL;
267}
268
Michal Vasko519fd602020-05-26 12:17:39 +0200269API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100270lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
271 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200272{
273 LY_ERR ret = LY_SUCCESS;
274 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200275 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200276 uint32_t i;
277
Michal Vasko26512682021-01-11 11:35:40 +0100278 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200279 if (!(options & LYXP_SCNODE_ALL)) {
280 options = LYXP_SCNODE;
281 }
Michal Vasko26512682021-01-11 11:35:40 +0100282 if (!ctx) {
283 ctx = ctx_node->module->ctx;
284 }
Michal Vasko519fd602020-05-26 12:17:39 +0200285
286 memset(&xp_set, 0, sizeof xp_set);
287
288 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100289 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200290 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200291
292 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100293 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200294 LY_CHECK_GOTO(ret, cleanup);
295
296 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200297 ret = ly_set_new(set);
298 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200299
300 /* transform into ly_set */
301 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100302 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200303 (*set)->size = xp_set.used;
304
305 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200306 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200307 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200308 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200309 }
310 }
311
312cleanup:
313 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100314 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200315 return ret;
316}
317
Michal Vasko072de482020-08-05 13:27:21 +0200318API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200319lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
320 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
321{
322 LY_ERR ret = LY_SUCCESS;
323 struct lyxp_set xp_set = {0};
324 uint32_t i;
325
326 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
327 if (!(options & LYXP_SCNODE_ALL)) {
328 options = LYXP_SCNODE;
329 }
330
331 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100332 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 +0200333 LY_CHECK_GOTO(ret, cleanup);
334
335 /* allocate return set */
336 ret = ly_set_new(set);
337 LY_CHECK_GOTO(ret, cleanup);
338
339 /* transform into ly_set */
340 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
341 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
342 (*set)->size = xp_set.used;
343
344 for (i = 0; i < xp_set.used; ++i) {
Michal Vaskod97959c2020-12-10 12:18:28 +0100345 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM)) {
346 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM) ||
347 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200348 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
349 LY_CHECK_GOTO(ret, cleanup);
350 }
351 }
352
353cleanup:
354 lyxp_set_free_content(&xp_set);
355 if (ret) {
356 ly_set_free(*set, NULL);
357 *set = NULL;
358 }
359 return ret;
360}
361
362API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100363lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
364 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200365{
366 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200367 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200368 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200369 uint32_t i;
370
Michal Vasko26512682021-01-11 11:35:40 +0100371 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200372 if (!(options & LYXP_SCNODE_ALL)) {
373 options = LYXP_SCNODE;
374 }
Michal Vasko26512682021-01-11 11:35:40 +0100375 if (!ctx) {
376 ctx = ctx_node->module->ctx;
377 }
Michal Vasko072de482020-08-05 13:27:21 +0200378
Michal Vasko072de482020-08-05 13:27:21 +0200379 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100380 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200381 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200382
383 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100384 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200385 LY_CHECK_GOTO(ret, cleanup);
386
387 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200388 ret = ly_set_new(set);
389 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200390
391 /* transform into ly_set */
392 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100393 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200394 (*set)->size = xp_set.used;
395
396 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100397 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 +0200398 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200399 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200400 }
401 }
402
403cleanup:
404 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100405 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200406 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200407 ly_set_free(*set, NULL);
408 *set = NULL;
409 }
Michal Vasko072de482020-08-05 13:27:21 +0200410 return ret;
411}
412
Radek Krejcibc5644c2020-10-27 14:53:17 +0100413API LY_ERR
414lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
415{
416 LY_ERR ret = LY_SUCCESS;
417 LY_ARRAY_COUNT_TYPE u, v;
418
419 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
420
421 /* allocate return set */
422 LY_CHECK_RET(ly_set_new(set));
423
424 LY_ARRAY_FOR(path, u) {
425 /* add nodes from the path */
426 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
427 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
428 LY_ARRAY_FOR(path[u].predicates, v) {
429 /* add all the keys in a predicate */
430 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
431 }
432 }
433 }
434
435cleanup:
436 if (ret) {
437 ly_set_free(*set, NULL);
438 *set = NULL;
439 }
440 return ret;
441}
442
443API LY_ERR
444lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
445 struct ly_set **set)
446{
447 LY_ERR ret = LY_SUCCESS;
448 uint8_t oper;
449 struct lyxp_expr *expr = NULL;
450 struct ly_path *p = NULL;
451
452 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
453
454 if (!ctx) {
455 ctx = ctx_node->module->ctx;
456 }
457
458 /* parse */
459 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
460 LY_CHECK_GOTO(ret, cleanup);
461
462 /* compile */
463 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
464 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 +0100465 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100466 LY_CHECK_GOTO(ret, cleanup);
467
468 /* resolve */
469 ret = lys_find_lypath_atoms(p, set);
470
471cleanup:
472 ly_path_free(ctx, p);
473 lyxp_expr_free(ctx, expr);
474 return ret;
475}
476
477API const struct lysc_node *
478lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
479{
480 const struct lysc_node *snode = NULL;
481 struct lyxp_expr *exp = NULL;
482 struct ly_path *p = NULL;
483 LY_ERR ret;
484 uint8_t oper;
485
486 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
487
488 if (!ctx) {
489 ctx = ctx_node->module->ctx;
490 }
491
492 /* parse */
493 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
494 LY_CHECK_GOTO(ret, cleanup);
495
496 /* compile */
497 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
498 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 +0100499 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100500 LY_CHECK_GOTO(ret, cleanup);
501
502 /* get last node */
503 snode = p[LY_ARRAY_COUNT(p) - 1].node;
504
505cleanup:
506 ly_path_free(ctx, p);
507 lyxp_expr_free(ctx, exp);
508 return snode;
509}
510
Michal Vasko14654712020-02-06 08:35:21 +0100511char *
512lysc_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 +0200513 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200514{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200515 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200516 char *path = NULL;
517 int len = 0;
518
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200519 LY_CHECK_ARG_RET(NULL, node, NULL);
520 if (buffer) {
521 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100522 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200523 }
524
Radek Krejci327de162019-06-14 12:52:07 +0200525 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200526 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200527 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100528 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200529 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100530 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200531
Michal Vasko65de0402020-08-03 16:34:19 +0200532 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
533 /* schema-only node */
534 continue;
535 }
536
Michal Vasko11deea12020-08-05 13:54:50 +0200537 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200538 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100539 if (parent && (iter->parent == parent)) {
540 slash = "";
541 } else {
542 slash = "/";
543 }
Michal Vasko69730152020-10-09 16:30:07 +0200544 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200545 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200546 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100547 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200548 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100549 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200550 }
Radek Krejci327de162019-06-14 12:52:07 +0200551 } else {
552 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200553 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100554 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200555 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100556 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200557 }
Radek Krejci327de162019-06-14 12:52:07 +0200558 }
559 free(s);
560 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200561
Michal Vasko69730152020-10-09 16:30:07 +0200562 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200563 /* not enough space in buffer */
564 break;
565 }
Radek Krejci327de162019-06-14 12:52:07 +0200566 }
567
568 if (len < 0) {
569 free(path);
570 path = NULL;
571 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200572 if (buffer) {
573 strcpy(buffer, "/");
574 } else {
575 path = strdup("/");
576 }
Radek Krejci327de162019-06-14 12:52:07 +0200577 }
578 break;
579 }
580
Radek Krejci1c0c3442019-07-23 16:08:47 +0200581 if (buffer) {
582 return buffer;
583 } else {
584 return path;
585 }
Radek Krejci327de162019-06-14 12:52:07 +0200586}
587
Michal Vasko14654712020-02-06 08:35:21 +0100588API char *
589lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
590{
591 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
592}
593
Michal Vasko28d78432020-05-26 13:10:53 +0200594API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200595lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200596{
Radek Krejci2a9fc652021-01-22 17:44:34 +0100597 struct lysc_node_action *act;
598 struct lysc_node_notif *notif;
Radek Krejciaf9cd802020-10-06 21:59:47 +0200599
Radek Krejci19cf8052020-08-18 15:02:38 +0200600 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
601
Radek Krejciaf9cd802020-10-06 21:59:47 +0200602 switch (node->nodetype) {
603 case LYS_CONTAINER:
604 case LYS_CHOICE:
605 case LYS_CASE:
606 case LYS_LEAF:
607 case LYS_LEAFLIST:
608 case LYS_LIST:
609 case LYS_ANYXML:
610 case LYS_ANYDATA:
611 if (prev_priv_p) {
612 *prev_priv_p = node->priv;
613 }
614 ((struct lysc_node *)node)->priv = priv;
615 break;
616 case LYS_RPC:
617 case LYS_ACTION:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100618 act = (struct lysc_node_action *)node;
Radek Krejciaf9cd802020-10-06 21:59:47 +0200619 if (prev_priv_p) {
620 *prev_priv_p = act->priv;
621 }
622 act->priv = priv;
623 break;
624 case LYS_NOTIF:
Radek Krejci2a9fc652021-01-22 17:44:34 +0100625 notif = (struct lysc_node_notif *)node;
Radek Krejciaf9cd802020-10-06 21:59:47 +0200626 if (prev_priv_p) {
627 *prev_priv_p = notif->priv;
628 }
629 notif->priv = priv;
630 break;
631 default:
632 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200633 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200634
635 return LY_SUCCESS;
636}
637
Michal Vasko405cc9e2020-12-01 12:01:27 +0100638LY_ERR
639lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200640{
641 struct lys_module *m;
642
Michal Vasko405cc9e2020-12-01 12:01:27 +0100643 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200644
645 /* we have module from the current context */
646 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
647 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200648 assert(m != mod);
649
650 /* check collision with other implemented revision */
651 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
652 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
653 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200654 }
655
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100656 /* enable features */
657 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
658
Michal Vasko89b5c072020-10-06 13:52:44 +0200659 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100660 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200661
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200662 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200663 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200664
665 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100666 return lys_compile(mod, 0, unres);
667}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200668
Michal Vasko405cc9e2020-12-01 12:01:27 +0100669API LY_ERR
670lys_set_implemented(struct lys_module *mod, const char **features)
671{
672 LY_ERR ret = LY_SUCCESS, r;
673 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200674
Michal Vasko405cc9e2020-12-01 12:01:27 +0100675 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100676
Michal Vasko405cc9e2020-12-01 12:01:27 +0100677 if (mod->implemented) {
678 /* mod is already implemented, set the features */
679 r = lys_set_features(mod->parsed, features);
680 if (r == LY_EEXIST) {
681 /* no changes */
682 return LY_SUCCESS;
683 } else if (r) {
684 /* error */
685 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200686 }
687
Michal Vasko405cc9e2020-12-01 12:01:27 +0100688 /* full recompilation */
689 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200690 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100691
Michal Vasko405cc9e2020-12-01 12:01:27 +0100692 /* implement this module and any other required modules, recursively */
693 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100694
695 /* the first module being implemented is finished, resolve global unres, consolidate the set */
696 if (!ret) {
697 ret = lys_compile_unres_glob(mod->ctx, &unres);
698 }
699 if (ret) {
700 /* failure, full compile revert */
701 lys_compile_unres_glob_revert(mod->ctx, &unres);
702 }
703
704 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200705 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200706}
707
Michal Vasko7c8439f2020-08-05 13:25:19 +0200708static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100709lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200710{
711 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200712 LY_ARRAY_COUNT_TYPE u, v;
713
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100714 pmod->parsing = 1;
715 LY_ARRAY_FOR(pmod->imports, u) {
716 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200717 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100718 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
719 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200720 }
721 /* check for importing the same module twice */
722 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100723 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200724 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
725 }
726 }
727 }
Radek Krejci771928a2021-01-19 13:42:36 +0100728 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
729
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100730 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200731
732 return LY_SUCCESS;
733}
734
Michal Vasko3a41dff2020-07-15 14:30:28 +0200735LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200736lys_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 +0200737 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200738 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200739{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200740 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100742 struct lys_yang_parser_ctx *yangctx = NULL;
743 struct lys_yin_parser_ctx *yinctx = NULL;
744 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100745
Michal Vasko3a41dff2020-07-15 14:30:28 +0200746 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100747
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100748 switch (format) {
749 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200750 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100751 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100752 break;
753 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200754 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100755 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100756 break;
757 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200758 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200759 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100760 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200761 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200762 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200763 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100764
765 /* make sure that the newest revision is at position 0 */
766 lysp_sort_revisions(submod->revs);
767
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100768 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200769 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100770 if (latest_sp) {
771 if (submod->revs) {
772 if (!latest_sp->revs) {
773 /* latest has no revision, so mod is anyway newer */
774 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200775 /* the latest_sp is zeroed later when the new module is being inserted into the context */
776 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
777 submod->latest_revision = latest_sp->latest_revision;
778 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100779 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200780 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100781 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200782 } else {
783 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100784 }
785 } else {
786 submod->latest_revision = 1;
787 }
788
Radek Krejcib3289d62019-09-18 12:21:39 +0200789 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200790 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200791 }
792
793 if (latest_sp) {
794 latest_sp->latest_revision = 0;
795 }
796
Michal Vasko7a0b0762020-09-02 16:37:01 +0200797 lys_parser_fill_filepath(ctx, in, &submod->filepath);
798
Michal Vasko7c8439f2020-08-05 13:25:19 +0200799 /* resolve imports and includes */
800 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
801
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100802 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100803 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
804 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100805
David Sedlák1b623122019-08-05 15:27:49 +0200806 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100807 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200808 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100809 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200810 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200811 *submodule = submod;
812 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200813
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100814error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200815 lysp_module_free((struct lysp_module *)submod);
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 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200822}
823
Michal Vasko45b521c2020-11-04 17:14:39 +0100824/**
825 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
826 *
827 * @param[in] mod Parsed module to add to.
828 * @return LY_SUCCESS on success.
829 * @return LY_ERR on error.
830 */
831static LY_ERR
832lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
833{
834 struct lysp_ext_instance *ext_p;
835 struct lysp_stmt *stmt;
836 struct lysp_import *imp;
837
838 /*
839 * 1) edit-config's operation
840 */
841 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
842 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
843 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
844 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
845 ext_p->flags = LYS_INTERNAL;
846 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
847 ext_p->insubstmt_index = 0;
848
849 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
850 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
851 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
852 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
853 stmt->kw = LY_STMT_TYPE;
854
855 stmt->child = calloc(1, sizeof *stmt->child);
856 stmt = stmt->child;
857 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
858 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
859 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
860 stmt->kw = LY_STMT_ENUM;
861
862 stmt->next = calloc(1, sizeof *stmt->child);
863 stmt = stmt->next;
864 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
865 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
866 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
867 stmt->kw = LY_STMT_ENUM;
868
869 stmt->next = calloc(1, sizeof *stmt->child);
870 stmt = stmt->next;
871 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
872 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
873 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
874 stmt->kw = LY_STMT_ENUM;
875
876 stmt->next = calloc(1, sizeof *stmt->child);
877 stmt = stmt->next;
878 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
879 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
880 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
881 stmt->kw = LY_STMT_ENUM;
882
883 stmt->next = calloc(1, sizeof *stmt->child);
884 stmt = stmt->next;
885 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
886 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
887 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
888 stmt->kw = LY_STMT_ENUM;
889
890 /*
891 * 2) filter's type
892 */
893 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
894 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
895 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
896 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
897 ext_p->flags = LYS_INTERNAL;
898 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
899 ext_p->insubstmt_index = 0;
900
901 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
902 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
903 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
904 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
905 stmt->kw = LY_STMT_TYPE;
906
907 stmt->child = calloc(1, sizeof *stmt->child);
908 stmt = stmt->child;
909 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
910 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
911 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
912 stmt->kw = LY_STMT_ENUM;
913
914 stmt->next = calloc(1, sizeof *stmt->child);
915 stmt = stmt->next;
916 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
917 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
918 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
919 stmt->kw = LY_STMT_ENUM;
920
921 /* if-feature for enum allowed only for YANG 1.1 modules */
922 if (mod->version >= LYS_VERSION_1_1) {
923 stmt->child = calloc(1, sizeof *stmt->child);
924 stmt = stmt->child;
925 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
926 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
927 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
928 stmt->kw = LY_STMT_IF_FEATURE;
929 }
930
931 /*
932 * 3) filter's select
933 */
934 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
935 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
936 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
937 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
938 ext_p->flags = LYS_INTERNAL;
939 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
940 ext_p->insubstmt_index = 0;
941
942 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
943 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
944 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
945 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
946 stmt->kw = LY_STMT_TYPE;
947
948 /* create new imports for the used prefixes */
949 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
950
951 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
952 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
953 imp->flags = LYS_INTERNAL;
954
955 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
956
957 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
958 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
959 imp->flags = LYS_INTERNAL;
960
961 return LY_SUCCESS;
962}
963
964/**
965 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
966 *
967 * @param[in] mod Parsed module to add to.
968 * @return LY_SUCCESS on success.
969 * @return LY_ERR on error.
970 */
971static LY_ERR
972lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
973{
974 struct lysp_ext_instance *ext_p;
975 struct lysp_stmt *stmt;
976 struct lysp_import *imp;
977
978 /* add new extension instance */
979 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
980
981 /* fill in the extension instance fields */
982 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
983 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
984 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
985 ext_p->flags = LYS_INTERNAL;
986 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
987 ext_p->insubstmt_index = 0;
988
989 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
990 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
991 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
992 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
993 stmt->kw = LY_STMT_TYPE;
994
995 /* create new import for the used prefix */
996 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
997
998 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
999 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1000 imp->flags = LYS_INTERNAL;
1001
1002 return LY_SUCCESS;
1003}
1004
Michal Vasko3a41dff2020-07-15 14:30:28 +02001005LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +01001006lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001007 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 +01001008 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001009{
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001010 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001011 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001012 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001013 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001014 struct lys_yang_parser_ctx *yangctx = NULL;
1015 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001016 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001017 char *filename, *rev, *dot;
1018 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +01001019 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +02001020
Michal Vasko34e334d2021-01-25 16:12:31 +01001021 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001022
Michal Vasko7a0b0762020-09-02 16:37:01 +02001023 if (module) {
1024 *module = NULL;
1025 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001026
Michal Vasko34e334d2021-01-25 16:12:31 +01001027 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1028 implement = 1;
1029 } else {
1030 implement = need_implemented;
Radek Krejci00a3e8a2021-01-27 08:24:49 +01001031 }
Michal Vasko34e334d2021-01-25 16:12:31 +01001032
Radek Krejci86d106e2018-10-18 09:53:19 +02001033 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001034 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001035 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001036
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001037 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001038 switch (format) {
1039 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001040 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001041 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001042 break;
1043 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001044 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001045 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001046 break;
1047 default:
1048 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001049 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001050 break;
1051 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001052 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001053
1054 /* make sure that the newest revision is at position 0 */
1055 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001056 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001057 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 +01001058 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001059
Radek Krejcib3289d62019-09-18 12:21:39 +02001060 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001061 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001062 if (latest) {
1063 if (mod->revision) {
1064 if (!latest->revision) {
1065 /* latest has no revision, so mod is anyway newer */
1066 mod->latest_revision = latest->latest_revision;
1067 /* the latest is zeroed later when the new module is being inserted into the context */
1068 } else if (strcmp(mod->revision, latest->revision) > 0) {
1069 mod->latest_revision = latest->latest_revision;
1070 /* the latest is zeroed later when the new module is being inserted into the context */
1071 } else {
1072 latest = NULL;
1073 }
1074 } else {
1075 latest = NULL;
1076 }
1077 } else {
1078 mod->latest_revision = 1;
1079 }
1080
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001081 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001082 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001083 }
1084
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001085 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001086 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001087 if (implement) {
1088 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1089 if (mod_impl && (mod_impl != mod_dup)) {
1090 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1091 mod_impl->revision ? mod_impl->revision : "<none>");
1092 ret = LY_EDENIED;
1093 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001094 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001095 }
1096 if (mod_dup) {
1097 if (implement) {
1098 if (!mod_dup->implemented) {
1099 /* just implement it */
1100 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1101 goto free_mod_cleanup;
1102 }
1103
1104 /* nothing to do */
1105 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1106 mod_dup->revision ? mod_dup->revision : "<none>");
1107 goto free_mod_cleanup;
1108 }
1109
1110 /* nothing to do */
1111 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1112 mod_dup->revision ? mod_dup->revision : "<none>");
1113 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001114 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001115
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001116 switch (in->type) {
1117 case LY_IN_FILEPATH:
1118 /* check that name and revision match filename */
1119 filename = strrchr(in->method.fpath.filepath, '/');
1120 if (!filename) {
1121 filename = in->method.fpath.filepath;
1122 } else {
1123 filename++;
1124 }
1125 rev = strchr(filename, '@');
1126 dot = strrchr(filename, '.');
1127
1128 /* name */
1129 len = strlen(mod->name);
1130 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001131 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001132 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1133 }
1134 if (rev) {
1135 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001136 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001137 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001138 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001139 }
1140 }
1141
1142 break;
1143 case LY_IN_FD:
1144 case LY_IN_FILE:
1145 case LY_IN_MEMORY:
1146 /* nothing special to do */
1147 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001148 case LY_IN_ERROR:
1149 LOGINT(ctx);
1150 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001151 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001152 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001153 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001154
Michal Vasko7a0b0762020-09-02 16:37:01 +02001155 if (latest) {
1156 latest->latest_revision = 0;
1157 }
1158
Michal Vasko45b521c2020-11-04 17:14:39 +01001159 /* add internal data in case specific modules were parsed */
1160 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001161 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001162 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001163 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001164 }
1165
Michal Vasko405cc9e2020-12-01 12:01:27 +01001166 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001167 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001168
Michal Vasko7a0b0762020-09-02 16:37:01 +02001169 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001170 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001171 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001172 ctx->module_set_id++;
1173
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001174 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001175 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001176
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001177 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001178 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001179 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001180 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1181 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001182
1183 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001184 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001185
Michal Vasko89b5c072020-10-06 13:52:44 +02001186 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001187 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001188 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001189
1190 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001191 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001192 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001193 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001194 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001195 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001196 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001197 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001198 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001199 }
1200
Michal Vasko405cc9e2020-12-01 12:01:27 +01001201 /* success */
1202 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001203
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001204free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001205 lys_module_free(mod, NULL);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001206 mod = NULL;
1207
Michal Vasko405cc9e2020-12-01 12:01:27 +01001208cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001209 if (pctx) {
1210 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1211 }
1212 if (format == LYS_IN_YANG) {
1213 yang_parser_ctx_free(yangctx);
1214 } else {
1215 yin_parser_ctx_free(yinctx);
1216 }
1217
Michal Vasko405cc9e2020-12-01 12:01:27 +01001218 if (!ret && module) {
1219 *module = mod;
1220 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001221 return ret;
1222}
1223
Radek Krejci545b4872020-11-15 10:15:12 +01001224static LYS_INFORMAT
1225lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1226{
1227 if (!format && (in->type == LY_IN_FILEPATH)) {
1228 /* unknown format - try to detect it from filename's suffix */
1229 const char *path = in->method.fpath.filepath;
1230 size_t len = strlen(path);
1231
1232 /* ignore trailing whitespaces */
1233 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1234
Radek Krejcif13b87b2020-12-01 22:02:17 +01001235 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1236 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001237 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1239 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001240 format = LYS_IN_YIN;
1241 } /* else still unknown */
1242 }
1243
1244 return format;
1245}
1246
Michal Vasko7a0b0762020-09-02 16:37:01 +02001247API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001248lys_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 +02001249{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001250 LY_ERR ret;
1251 struct lys_glob_unres unres = {0};
1252
Michal Vasko7a0b0762020-09-02 16:37:01 +02001253 if (module) {
1254 *module = NULL;
1255 }
Radek Krejci545b4872020-11-15 10:15:12 +01001256 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1257
1258 format = lys_parse_get_format(in, format);
1259 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001260
1261 /* remember input position */
1262 in->func_start = in->current;
1263
Michal Vasko405cc9e2020-12-01 12:01:27 +01001264 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1265 LY_CHECK_GOTO(ret, cleanup);
1266
1267 /* resolve global unres */
1268 ret = lys_compile_unres_glob(ctx, &unres);
1269 LY_CHECK_GOTO(ret, cleanup);
1270
1271cleanup:
1272 if (ret) {
1273 lys_compile_unres_glob_revert(ctx, &unres);
1274 }
1275 lys_compile_unres_glob_erase(ctx, &unres);
1276 if (ret && module) {
1277 *module = NULL;
1278 }
1279 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001280}
1281
Michal Vasko3a41dff2020-07-15 14:30:28 +02001282API LY_ERR
1283lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001284{
Radek Krejci0f969882020-08-21 16:56:47 +02001285 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001286 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001287
Michal Vasko3a41dff2020-07-15 14:30:28 +02001288 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001289
Michal Vasko3a41dff2020-07-15 14:30:28 +02001290 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 +02001291
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001292 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001293 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001294
Michal Vasko3a41dff2020-07-15 14:30:28 +02001295 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001296}
1297
Michal Vasko3a41dff2020-07-15 14:30:28 +02001298API LY_ERR
1299lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001300{
Radek Krejci0f969882020-08-21 16:56:47 +02001301 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001302 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001303
Michal Vasko3a41dff2020-07-15 14:30:28 +02001304 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001305
Michal Vasko3a41dff2020-07-15 14:30:28 +02001306 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 +02001307
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001308 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001309 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001310
Michal Vasko3a41dff2020-07-15 14:30:28 +02001311 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001312}
1313
Michal Vasko3a41dff2020-07-15 14:30:28 +02001314API LY_ERR
1315lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001316{
Radek Krejci0f969882020-08-21 16:56:47 +02001317 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001318 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001319
Michal Vasko3a41dff2020-07-15 14:30:28 +02001320 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001321
Michal Vasko3a41dff2020-07-15 14:30:28 +02001322 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001323 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001324
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001325 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001326 ly_in_free(in, 0);
1327
Michal Vasko3a41dff2020-07-15 14:30:28 +02001328 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001329}
1330
1331API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001332lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001333 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001334{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001335 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001336 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001337 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001338 char *wd, *wn = NULL;
1339 DIR *dir = NULL;
1340 struct dirent *file;
1341 char *match_name = NULL;
1342 LYS_INFORMAT format_aux, match_format = 0;
1343 struct ly_set *dirs;
1344 struct stat st;
1345
1346 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1347
1348 /* start to fill the dir fifo with the context's search path (if set)
1349 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001350 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001351
1352 len = strlen(name);
1353 if (cwd) {
1354 wd = get_current_dir_name();
1355 if (!wd) {
1356 LOGMEM(NULL);
1357 goto cleanup;
1358 } else {
1359 /* add implicit current working directory (./) to be searched,
1360 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001361 ret = ly_set_add(dirs, wd, 0, NULL);
1362 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001363 implicit_cwd = 1;
1364 }
1365 }
1366 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001367 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001368 /* check for duplicities with the implicit current working directory */
1369 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1370 implicit_cwd = 0;
1371 continue;
1372 }
1373 wd = strdup(searchpaths[i]);
1374 if (!wd) {
1375 LOGMEM(NULL);
1376 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001377 } else {
1378 ret = ly_set_add(dirs, wd, 0, NULL);
1379 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001380 }
1381 }
1382 }
1383 wd = NULL;
1384
1385 /* start searching */
1386 while (dirs->count) {
1387 free(wd);
1388 free(wn); wn = NULL;
1389
1390 dirs->count--;
1391 wd = (char *)dirs->objs[dirs->count];
1392 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001393 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001394
1395 if (dir) {
1396 closedir(dir);
1397 }
1398 dir = opendir(wd);
1399 dir_len = strlen(wd);
1400 if (!dir) {
1401 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1402 } else {
1403 while ((file = readdir(dir))) {
1404 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1405 /* skip . and .. */
1406 continue;
1407 }
1408 free(wn);
1409 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1410 LOGMEM(NULL);
1411 goto cleanup;
1412 }
1413 if (stat(wn, &st) == -1) {
1414 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001415 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001416 continue;
1417 }
1418 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1419 /* we have another subdirectory in searchpath to explore,
1420 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001421 ret = ly_set_add(dirs, wn, 0, NULL);
1422 LY_CHECK_GOTO(ret, cleanup);
1423
Radek Krejcid33273d2018-10-25 14:55:52 +02001424 /* continue with the next item in current directory */
1425 wn = NULL;
1426 continue;
1427 } else if (!S_ISREG(st.st_mode)) {
1428 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1429 continue;
1430 }
1431
1432 /* here we know that the item is a file which can contain a module */
1433 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001434 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001435 /* different filename than the module we search for */
1436 continue;
1437 }
1438
1439 /* get type according to filename suffix */
1440 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001441 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1442 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001443 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001444 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1445 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001446 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001447 } else {
1448 /* not supportde suffix/file format */
1449 continue;
1450 }
1451
1452 if (revision) {
1453 /* we look for the specific revision, try to get it from the filename */
1454 if (file->d_name[len] == '@') {
1455 /* check revision from the filename */
1456 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1457 /* another revision */
1458 continue;
1459 } else {
1460 /* exact revision */
1461 free(match_name);
1462 match_name = wn;
1463 wn = NULL;
1464 match_len = dir_len + 1 + len;
1465 match_format = format_aux;
1466 goto success;
1467 }
1468 } else {
1469 /* continue trying to find exact revision match, use this only if not found */
1470 free(match_name);
1471 match_name = wn;
1472 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001473 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001474 match_format = format_aux;
1475 continue;
1476 }
1477 } else {
1478 /* remember the revision and try to find the newest one */
1479 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001480 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001481 lysp_check_date(NULL, &file->d_name[len + 1],
1482 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001483 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001484 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001485 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1486 continue;
1487 }
1488 free(match_name);
1489 }
1490
1491 match_name = wn;
1492 wn = NULL;
1493 match_len = dir_len + 1 + len;
1494 match_format = format_aux;
1495 continue;
1496 }
1497 }
1498 }
1499 }
1500
1501success:
1502 (*localfile) = match_name;
1503 match_name = NULL;
1504 if (format) {
1505 (*format) = match_format;
1506 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001507 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001508
1509cleanup:
1510 free(wn);
1511 free(wd);
1512 if (dir) {
1513 closedir(dir);
1514 }
1515 free(match_name);
1516 ly_set_free(dirs, free);
1517
1518 return ret;
1519}