blob: 54c550997f26f3cec492b9e4e72ffcc95a567fd6 [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;
54 const struct lysc_action *acts;
55 const struct lysc_notif *notifs;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020056 LY_ARRAY_COUNT_TYPE u;
57
58 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
59
60 LYSC_TREE_DFS_BEGIN(root, elem) {
61 /* schema node */
62 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
63
Michal Vasko1d972ca2020-11-03 17:16:56 +010064 acts = lysc_node_actions(elem);
65 LY_ARRAY_FOR(acts, u) {
66 LYSC_TREE_DFS_BEGIN(&acts[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020067 /* action subtree */
68 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
69
Michal Vasko1d972ca2020-11-03 17:16:56 +010070 LYSC_TREE_DFS_END(&acts[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020071 }
72 }
73
Michal Vasko1d972ca2020-11-03 17:16:56 +010074 notifs = lysc_node_notifs(elem);
75 LY_ARRAY_FOR(notifs, u) {
76 LYSC_TREE_DFS_BEGIN(&notifs[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020077 /* notification subtree */
78 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
79
Michal Vasko1d972ca2020-11-03 17:16:56 +010080 LYSC_TREE_DFS_END(&notifs[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020081 }
82 }
83
84 LYSC_TREE_DFS_END(root, elem);
85 }
86
87 return LY_SUCCESS;
88}
89
90API LY_ERR
91lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
92{
93 LY_ARRAY_COUNT_TYPE u;
Michal Vasko2336cf52020-11-03 17:18:15 +010094 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020095
96 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
97
98 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +010099 LY_LIST_FOR(mod->compiled->data, root) {
100 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
101 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200102
103 /* RPCs */
104 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
105 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
106 }
107
108 /* notifications */
109 LY_ARRAY_FOR(mod->compiled->notifs, u) {
110 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
111 }
112
113 return LY_SUCCESS;
114}
115
Radek Krejcib93bd412020-11-02 13:23:11 +0100116static void
117lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
118{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100119 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100120 if (first_case->child) {
121 /* there is something to return */
122 (*next) = first_case->child;
123 return;
124 }
125 }
126
127 /* no children in choice's cases, so go to the choice's sibling instead of into it */
128 (*last) = (*next);
129 (*next) = (*next)->next;
130}
131
Radek Krejcia3045382018-11-22 14:30:31 +0100132API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200133lys_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 +0100134{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100135 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100136 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200137 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100138 const struct lysc_action *actions;
139 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200140 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100141
142 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
143
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200144next:
Radek Krejcia3045382018-11-22 14:30:31 +0100145 if (!last) {
146 /* first call */
147
148 /* get know where to start */
149 if (parent) {
150 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200151 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200152 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100153 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100154 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100155 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100156 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100157 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200158 if (snode && *snode) {
159 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100160 }
Radek Krejcia3045382018-11-22 14:30:31 +0100161 }
Radek Krejcia3045382018-11-22 14:30:31 +0100162 } else {
163 /* top level data */
164 next = last = module->data;
165 }
166 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100167 /* try to get action or notification */
168 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100169 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100170 /* test if the next can be returned */
171 goto check;
172
Michal Vasko1bf09392020-03-27 12:38:10 +0100173 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100174 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100175 if (last->parent) {
176 actions = lysc_node_actions(last->parent);
177 } else {
178 actions = module->rpcs;
179 }
180 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200181 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100182 break;
183 }
184 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200185 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200186 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100187 }
188 goto repeat;
189 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100190 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100191 if (last->parent) {
192 notifs = lysc_node_notifs(last->parent);
193 } else {
194 notifs = module->notifs;
195 }
196 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200197 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100198 break;
199 }
200 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200201 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200202 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100203 }
204 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200205 } else {
206 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100207 }
208
Radek Krejcia3045382018-11-22 14:30:31 +0100209repeat:
210 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100211 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200212 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100213 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200214 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100215 } else if (!action_flag) {
216 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200217 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100218 } else if (!notif_flag) {
219 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200220 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100221 } else {
222 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100223 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100224 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100225 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100226check:
Radek Krejcia3045382018-11-22 14:30:31 +0100227 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100228 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100229 case LYS_ACTION:
230 case LYS_NOTIF:
231 case LYS_LEAF:
232 case LYS_ANYXML:
233 case LYS_ANYDATA:
234 case LYS_LIST:
235 case LYS_LEAFLIST:
236 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200237 case LYS_CASE:
238 if (options & LYS_GETNEXT_WITHCASE) {
239 break;
240 } else {
241 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100242 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200243 }
244 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100245 case LYS_CONTAINER:
246 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
247 if (((struct lysc_node_container *)next)->child) {
248 /* go into */
249 next = ((struct lysc_node_container *)next)->child;
250 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100251 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100252 next = next->next;
253 }
254 goto repeat;
255 }
256 break;
257 case LYS_CHOICE:
258 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200259 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100260 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
261 next = next->next;
262 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100263 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200264 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100265 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100266 /* go into */
267 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100268 }
Radek Krejcia3045382018-11-22 14:30:31 +0100269 }
270 goto repeat;
271 default:
272 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200273 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100274 return NULL;
275 }
276
Radek Krejcia3045382018-11-22 14:30:31 +0100277 return next;
278}
279
280API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100281lys_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 +0200282 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100283{
284 const struct lysc_node *node = NULL;
285
286 LY_CHECK_ARG_RET(NULL, module, name, NULL);
287 if (!nodetype) {
288 nodetype = 0xffff;
289 }
290
291 while ((node = lys_getnext(node, parent, module->compiled, options))) {
292 if (!(node->nodetype & nodetype)) {
293 continue;
294 }
295 if (node->module != module) {
296 continue;
297 }
298
299 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200300 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100301 return node;
302 }
303 } else {
304 if (!strcmp(node->name, name)) {
305 return node;
306 }
307 }
308 }
309 return NULL;
310}
311
Michal Vasko519fd602020-05-26 12:17:39 +0200312API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200313lys_find_xpath_atoms(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200314{
315 LY_ERR ret = LY_SUCCESS;
316 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200317 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200318 uint32_t i;
319
320 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
321 if (!(options & LYXP_SCNODE_ALL)) {
322 options = LYXP_SCNODE;
323 }
324
325 memset(&xp_set, 0, sizeof xp_set);
326
327 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200328 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
329 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200330
331 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200332 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200333 LY_CHECK_GOTO(ret, cleanup);
334
335 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200336 ret = ly_set_new(set);
337 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200338
339 /* transform into ly_set */
340 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
341 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
342 (*set)->size = xp_set.used;
343
344 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200345 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200346 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200347 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200348 }
349 }
350
351cleanup:
352 lyxp_set_free_content(&xp_set);
353 lyxp_expr_free(ctx_node->module->ctx, exp);
354 return ret;
355}
356
Michal Vasko072de482020-08-05 13:27:21 +0200357API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200358lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
359 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
360{
361 LY_ERR ret = LY_SUCCESS;
362 struct lyxp_set xp_set = {0};
363 uint32_t i;
364
365 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
366 if (!(options & LYXP_SCNODE_ALL)) {
367 options = LYXP_SCNODE;
368 }
369
370 /* atomize expression */
371 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
372 LY_CHECK_GOTO(ret, cleanup);
373
374 /* allocate return set */
375 ret = ly_set_new(set);
376 LY_CHECK_GOTO(ret, cleanup);
377
378 /* transform into ly_set */
379 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
380 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
381 (*set)->size = xp_set.used;
382
383 for (i = 0; i < xp_set.used; ++i) {
384 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
385 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
386 LY_CHECK_GOTO(ret, cleanup);
387 }
388 }
389
390cleanup:
391 lyxp_set_free_content(&xp_set);
392 if (ret) {
393 ly_set_free(*set, NULL);
394 *set = NULL;
395 }
396 return ret;
397}
398
399API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200400lys_find_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200401{
402 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200403 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200404 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200405 uint32_t i;
406
407 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
408 if (!(options & LYXP_SCNODE_ALL)) {
409 options = LYXP_SCNODE;
410 }
411
Michal Vasko072de482020-08-05 13:27:21 +0200412 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200413 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
414 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200415
416 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200417 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200418 LY_CHECK_GOTO(ret, cleanup);
419
420 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200421 ret = ly_set_new(set);
422 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200423
424 /* transform into ly_set */
425 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
426 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
427 (*set)->size = xp_set.used;
428
429 for (i = 0; i < xp_set.used; ++i) {
430 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200431 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200432 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200433 }
434 }
435
436cleanup:
437 lyxp_set_free_content(&xp_set);
438 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200439 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200440 ly_set_free(*set, NULL);
441 *set = NULL;
442 }
Michal Vasko072de482020-08-05 13:27:21 +0200443 return ret;
444}
445
Radek Krejcibc5644c2020-10-27 14:53:17 +0100446API LY_ERR
447lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
448{
449 LY_ERR ret = LY_SUCCESS;
450 LY_ARRAY_COUNT_TYPE u, v;
451
452 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
453
454 /* allocate return set */
455 LY_CHECK_RET(ly_set_new(set));
456
457 LY_ARRAY_FOR(path, u) {
458 /* add nodes from the path */
459 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
460 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
461 LY_ARRAY_FOR(path[u].predicates, v) {
462 /* add all the keys in a predicate */
463 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
464 }
465 }
466 }
467
468cleanup:
469 if (ret) {
470 ly_set_free(*set, NULL);
471 *set = NULL;
472 }
473 return ret;
474}
475
476API LY_ERR
477lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
478 struct ly_set **set)
479{
480 LY_ERR ret = LY_SUCCESS;
481 uint8_t oper;
482 struct lyxp_expr *expr = NULL;
483 struct ly_path *p = NULL;
484
485 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
486
487 if (!ctx) {
488 ctx = ctx_node->module->ctx;
489 }
490
491 /* parse */
492 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
493 LY_CHECK_GOTO(ret, cleanup);
494
495 /* compile */
496 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
497 ret = ly_path_compile(ctx, NULL, ctx_node, expr, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
498 LY_PREF_JSON, NULL, &p);
499 LY_CHECK_GOTO(ret, cleanup);
500
501 /* resolve */
502 ret = lys_find_lypath_atoms(p, set);
503
504cleanup:
505 ly_path_free(ctx, p);
506 lyxp_expr_free(ctx, expr);
507 return ret;
508}
509
510API const struct lysc_node *
511lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
512{
513 const struct lysc_node *snode = NULL;
514 struct lyxp_expr *exp = NULL;
515 struct ly_path *p = NULL;
516 LY_ERR ret;
517 uint8_t oper;
518
519 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
520
521 if (!ctx) {
522 ctx = ctx_node->module->ctx;
523 }
524
525 /* parse */
526 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
527 LY_CHECK_GOTO(ret, cleanup);
528
529 /* compile */
530 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
531 ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
532 LY_PREF_JSON, NULL, &p);
533 LY_CHECK_GOTO(ret, cleanup);
534
535 /* get last node */
536 snode = p[LY_ARRAY_COUNT(p) - 1].node;
537
538cleanup:
539 ly_path_free(ctx, p);
540 lyxp_expr_free(ctx, exp);
541 return snode;
542}
543
Michal Vasko14654712020-02-06 08:35:21 +0100544char *
545lysc_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 +0200546 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200547{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200548 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200549 char *path = NULL;
550 int len = 0;
551
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200552 LY_CHECK_ARG_RET(NULL, node, NULL);
553 if (buffer) {
554 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
555 }
556
Radek Krejci327de162019-06-14 12:52:07 +0200557 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200558 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200559 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100560 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200561 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100562 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200563
Michal Vasko65de0402020-08-03 16:34:19 +0200564 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
565 /* schema-only node */
566 continue;
567 }
568
Michal Vasko11deea12020-08-05 13:54:50 +0200569 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200570 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100571 if (parent && (iter->parent == parent)) {
572 slash = "";
573 } else {
574 slash = "/";
575 }
Michal Vasko69730152020-10-09 16:30:07 +0200576 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200577 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200578 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100579 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200580 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100581 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200582 }
Radek Krejci327de162019-06-14 12:52:07 +0200583 } else {
584 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200585 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100586 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200587 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100588 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200589 }
Radek Krejci327de162019-06-14 12:52:07 +0200590 }
591 free(s);
592 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200593
Michal Vasko69730152020-10-09 16:30:07 +0200594 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200595 /* not enough space in buffer */
596 break;
597 }
Radek Krejci327de162019-06-14 12:52:07 +0200598 }
599
600 if (len < 0) {
601 free(path);
602 path = NULL;
603 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200604 if (buffer) {
605 strcpy(buffer, "/");
606 } else {
607 path = strdup("/");
608 }
Radek Krejci327de162019-06-14 12:52:07 +0200609 }
610 break;
611 }
612
Radek Krejci1c0c3442019-07-23 16:08:47 +0200613 if (buffer) {
614 return buffer;
615 } else {
616 return path;
617 }
Radek Krejci327de162019-06-14 12:52:07 +0200618}
619
Michal Vasko14654712020-02-06 08:35:21 +0100620API char *
621lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
622{
623 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
624}
625
Michal Vasko28d78432020-05-26 13:10:53 +0200626API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200627lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200628{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200629 struct lysc_action *act;
630 struct lysc_notif *notif;
631
Radek Krejci19cf8052020-08-18 15:02:38 +0200632 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
633
Radek Krejciaf9cd802020-10-06 21:59:47 +0200634 switch (node->nodetype) {
635 case LYS_CONTAINER:
636 case LYS_CHOICE:
637 case LYS_CASE:
638 case LYS_LEAF:
639 case LYS_LEAFLIST:
640 case LYS_LIST:
641 case LYS_ANYXML:
642 case LYS_ANYDATA:
643 if (prev_priv_p) {
644 *prev_priv_p = node->priv;
645 }
646 ((struct lysc_node *)node)->priv = priv;
647 break;
648 case LYS_RPC:
649 case LYS_ACTION:
650 act = (struct lysc_action *)node;
651 if (prev_priv_p) {
652 *prev_priv_p = act->priv;
653 }
654 act->priv = priv;
655 break;
656 case LYS_NOTIF:
657 notif = (struct lysc_notif *)node;
658 if (prev_priv_p) {
659 *prev_priv_p = notif->priv;
660 }
661 notif->priv = priv;
662 break;
663 default:
664 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200665 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200666
667 return LY_SUCCESS;
668}
669
Michal Vasko89b5c072020-10-06 13:52:44 +0200670API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100671lys_set_implemented(struct lys_module *mod, const char **features)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200672{
Michal Vasko08c8b272020-11-24 18:11:30 +0100673 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200674 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200675 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200676
677 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
678
679 if (mod->implemented) {
Michal Vasko08c8b272020-11-24 18:11:30 +0100680 /* mod is already implemented, set the features */
681 r = lys_set_features(mod->parsed, features);
682 if (r == LY_EEXIST) {
683 /* no changes */
684 return LY_SUCCESS;
685 } else if (r) {
686 /* error */
687 return r;
688 }
689
690 /* full recompilation */
691 lys_recompile(mod->ctx, NULL);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200692 return LY_SUCCESS;
693 }
694
695 /* we have module from the current context */
696 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
697 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200698 assert(m != mod);
699
700 /* check collision with other implemented revision */
701 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
702 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
703 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200704 }
705
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100706 /* enable features */
707 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
708
Michal Vasko89b5c072020-10-06 13:52:44 +0200709 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200710 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200711
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200712 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200713 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200714
715 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200716 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200717
Michal Vasko89b5c072020-10-06 13:52:44 +0200718 if (mod == mod->ctx->implementing.objs[0]) {
719 /* the first module being implemented, consolidate the set */
720 if (ret) {
721 /* failure, full compile revert */
722 for (i = 0; i < mod->ctx->list.count; ++i) {
723 m = mod->ctx->list.objs[i];
724 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
725 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200726
Michal Vasko89b5c072020-10-06 13:52:44 +0200727 /* make the module correctly non-implemented again */
728 m->implemented = 0;
729 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
730 lys_precompile_augments_deviations_revert(mod->ctx, m);
731 }
Michal Vasko89b5c072020-10-06 13:52:44 +0200732 }
Michal Vasko916aefb2020-11-02 15:43:16 +0100733
734 /* recompile, do not overwrite return value */
735 lys_recompile(mod->ctx, NULL);
Michal Vasko89b5c072020-10-06 13:52:44 +0200736 }
737
738 ly_set_erase(&mod->ctx->implementing, NULL);
739 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100740
Michal Vasko89b5c072020-10-06 13:52:44 +0200741 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200742}
743
Michal Vasko7c8439f2020-08-05 13:25:19 +0200744static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100745lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200746{
747 struct lysp_import *imp;
748 struct lysp_include *inc;
749 LY_ARRAY_COUNT_TYPE u, v;
750
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100751 pmod->parsing = 1;
752 LY_ARRAY_FOR(pmod->imports, u) {
753 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200754 if (!imp->module) {
Michal Vasko0550b762020-11-24 18:04:08 +0100755 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200756 }
757 /* check for importing the same module twice */
758 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100759 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200760 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
761 }
762 }
763 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100764 LY_ARRAY_FOR(pmod->includes, u) {
765 inc = &pmod->includes[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200766 if (!inc->submodule) {
767 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
768 }
769 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100770 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200771
772 return LY_SUCCESS;
773}
774
Michal Vasko3a41dff2020-07-15 14:30:28 +0200775LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200776lys_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 +0200777 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200778 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200779{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200780 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100781 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100782 struct lys_yang_parser_ctx *yangctx = NULL;
783 struct lys_yin_parser_ctx *yinctx = NULL;
784 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100785
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100787
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100788 switch (format) {
789 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200790 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100791 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100792 break;
793 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200794 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100795 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100796 break;
797 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200798 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200799 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100800 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200801 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200802 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200803 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100804
805 /* make sure that the newest revision is at position 0 */
806 lysp_sort_revisions(submod->revs);
807
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100808 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200809 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100810 if (latest_sp) {
811 if (submod->revs) {
812 if (!latest_sp->revs) {
813 /* latest has no revision, so mod is anyway newer */
814 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200815 /* the latest_sp is zeroed later when the new module is being inserted into the context */
816 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
817 submod->latest_revision = latest_sp->latest_revision;
818 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100819 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200820 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100821 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200822 } else {
823 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100824 }
825 } else {
826 submod->latest_revision = 1;
827 }
828
Radek Krejcib3289d62019-09-18 12:21:39 +0200829 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200830 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200831 }
832
833 if (latest_sp) {
834 latest_sp->latest_revision = 0;
835 }
836
Michal Vasko7a0b0762020-09-02 16:37:01 +0200837 lys_parser_fill_filepath(ctx, in, &submod->filepath);
838
Michal Vasko7c8439f2020-08-05 13:25:19 +0200839 /* resolve imports and includes */
840 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
841
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100842 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100843 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
844 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845
David Sedlák1b623122019-08-05 15:27:49 +0200846 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100847 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200848 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100849 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200850 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200851 *submodule = submod;
852 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200853
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100854error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200855 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200856 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100857 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200858 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100859 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200860 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200861 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200862}
863
Michal Vasko45b521c2020-11-04 17:14:39 +0100864/**
865 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
866 *
867 * @param[in] mod Parsed module to add to.
868 * @return LY_SUCCESS on success.
869 * @return LY_ERR on error.
870 */
871static LY_ERR
872lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
873{
874 struct lysp_ext_instance *ext_p;
875 struct lysp_stmt *stmt;
876 struct lysp_import *imp;
877
878 /*
879 * 1) edit-config's operation
880 */
881 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
882 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
883 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
884 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
885 ext_p->flags = LYS_INTERNAL;
886 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
887 ext_p->insubstmt_index = 0;
888
889 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
890 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
891 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
892 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
893 stmt->kw = LY_STMT_TYPE;
894
895 stmt->child = calloc(1, sizeof *stmt->child);
896 stmt = stmt->child;
897 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
898 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
899 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
900 stmt->kw = LY_STMT_ENUM;
901
902 stmt->next = calloc(1, sizeof *stmt->child);
903 stmt = stmt->next;
904 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
905 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
906 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
907 stmt->kw = LY_STMT_ENUM;
908
909 stmt->next = calloc(1, sizeof *stmt->child);
910 stmt = stmt->next;
911 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
912 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
913 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
914 stmt->kw = LY_STMT_ENUM;
915
916 stmt->next = calloc(1, sizeof *stmt->child);
917 stmt = stmt->next;
918 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
919 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
920 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
921 stmt->kw = LY_STMT_ENUM;
922
923 stmt->next = calloc(1, sizeof *stmt->child);
924 stmt = stmt->next;
925 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
926 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
927 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
928 stmt->kw = LY_STMT_ENUM;
929
930 /*
931 * 2) filter's type
932 */
933 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
934 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
935 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
936 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
937 ext_p->flags = LYS_INTERNAL;
938 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
939 ext_p->insubstmt_index = 0;
940
941 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
942 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
943 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
944 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
945 stmt->kw = LY_STMT_TYPE;
946
947 stmt->child = calloc(1, sizeof *stmt->child);
948 stmt = stmt->child;
949 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
950 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
951 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
952 stmt->kw = LY_STMT_ENUM;
953
954 stmt->next = calloc(1, sizeof *stmt->child);
955 stmt = stmt->next;
956 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
957 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
958 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
959 stmt->kw = LY_STMT_ENUM;
960
961 /* if-feature for enum allowed only for YANG 1.1 modules */
962 if (mod->version >= LYS_VERSION_1_1) {
963 stmt->child = calloc(1, sizeof *stmt->child);
964 stmt = stmt->child;
965 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
966 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
967 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
968 stmt->kw = LY_STMT_IF_FEATURE;
969 }
970
971 /*
972 * 3) filter's select
973 */
974 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
975 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
976 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
977 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
978 ext_p->flags = LYS_INTERNAL;
979 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
980 ext_p->insubstmt_index = 0;
981
982 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
983 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
984 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
985 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
986 stmt->kw = LY_STMT_TYPE;
987
988 /* create new imports for the used prefixes */
989 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
990
991 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
992 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
993 imp->flags = LYS_INTERNAL;
994
995 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
996
997 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
998 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
999 imp->flags = LYS_INTERNAL;
1000
1001 return LY_SUCCESS;
1002}
1003
1004/**
1005 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
1006 *
1007 * @param[in] mod Parsed module to add to.
1008 * @return LY_SUCCESS on success.
1009 * @return LY_ERR on error.
1010 */
1011static LY_ERR
1012lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
1013{
1014 struct lysp_ext_instance *ext_p;
1015 struct lysp_stmt *stmt;
1016 struct lysp_import *imp;
1017
1018 /* add new extension instance */
1019 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1020
1021 /* fill in the extension instance fields */
1022 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1023 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1024 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
1025 ext_p->flags = LYS_INTERNAL;
1026 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1027 ext_p->insubstmt_index = 0;
1028
1029 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1030 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1031 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1032 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
1033 stmt->kw = LY_STMT_TYPE;
1034
1035 /* create new import for the used prefix */
1036 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1037
1038 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1039 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1040 imp->flags = LYS_INTERNAL;
1041
1042 return LY_SUCCESS;
1043}
1044
Michal Vasko3a41dff2020-07-15 14:30:28 +02001045LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001046lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001047 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001048 void *check_data, const char **features, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001049{
Radek Krejci6d6e4e42018-10-29 13:28:19 +01001050 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001051 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001052 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001053 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001054 struct lys_yang_parser_ctx *yangctx = NULL;
1055 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001056 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001057 char *filename, *rev, *dot;
1058 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +02001059
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001060 LY_CHECK_ARG_RET(ctx, ctx, in, !features || implement, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001061 if (module) {
1062 *module = NULL;
1063 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001064
1065 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001066 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001067 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001068
1069 switch (format) {
1070 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001071 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001072 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001073 break;
1074 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001075 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001076 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001077 break;
1078 default:
1079 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001080 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001081 break;
1082 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001083 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001084
1085 /* make sure that the newest revision is at position 0 */
1086 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001087 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001088 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001089 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001090
Radek Krejcib3289d62019-09-18 12:21:39 +02001091 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001092 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001093 if (latest) {
1094 if (mod->revision) {
1095 if (!latest->revision) {
1096 /* latest has no revision, so mod is anyway newer */
1097 mod->latest_revision = latest->latest_revision;
1098 /* the latest is zeroed later when the new module is being inserted into the context */
1099 } else if (strcmp(mod->revision, latest->revision) > 0) {
1100 mod->latest_revision = latest->latest_revision;
1101 /* the latest is zeroed later when the new module is being inserted into the context */
1102 } else {
1103 latest = NULL;
1104 }
1105 } else {
1106 latest = NULL;
1107 }
1108 } else {
1109 mod->latest_revision = 1;
1110 }
1111
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001112 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001113 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001114 }
1115
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001116 /* check for duplicity in the context */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001117 if (implement && ly_ctx_get_module_implemented(ctx, mod->name)) {
1118 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
1119 ret = LY_EDENIED;
1120 goto error;
1121 }
Michal Vasko22df3f02020-08-24 13:29:22 +02001122 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001123 if (mod_dup) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001124 if (mod->parsed->revs) {
1125 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
1126 mod->name, mod->parsed->revs[0].date);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001128 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
1129 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001130 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001131 ret = LY_EEXIST;
1132 goto error;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001133 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001134
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001135 switch (in->type) {
1136 case LY_IN_FILEPATH:
1137 /* check that name and revision match filename */
1138 filename = strrchr(in->method.fpath.filepath, '/');
1139 if (!filename) {
1140 filename = in->method.fpath.filepath;
1141 } else {
1142 filename++;
1143 }
1144 rev = strchr(filename, '@');
1145 dot = strrchr(filename, '.');
1146
1147 /* name */
1148 len = strlen(mod->name);
1149 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001150 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001151 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1152 }
1153 if (rev) {
1154 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001155 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001156 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001157 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001158 }
1159 }
1160
1161 break;
1162 case LY_IN_FD:
1163 case LY_IN_FILE:
1164 case LY_IN_MEMORY:
1165 /* nothing special to do */
1166 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001167 case LY_IN_ERROR:
1168 LOGINT(ctx);
1169 ret = LY_EINT;
1170 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001171 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001172 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001173
Michal Vasko7a0b0762020-09-02 16:37:01 +02001174 if (latest) {
1175 latest->latest_revision = 0;
1176 }
1177
Michal Vasko45b521c2020-11-04 17:14:39 +01001178 /* add internal data in case specific modules were parsed */
1179 if (!strcmp(mod->name, "ietf-netconf")) {
1180 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), error);
1181 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
1182 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), error);
1183 }
1184
Michal Vasko7a0b0762020-09-02 16:37:01 +02001185 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001186 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001187 LY_CHECK_GOTO(ret, error);
1188 ctx->module_set_id++;
1189
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001190 /* resolve includes and all imports */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001191 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1192
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001193 /* check name collisions */
1194 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), error_ctx);
1195 /* TODO groupings */
1196 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), error_ctx);
1197 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), error_ctx);
1198
1199 /* compile features */
1200 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), error_ctx);
1201
Michal Vasko89b5c072020-10-06 13:52:44 +02001202 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001203 /* pre-compile identities of the module */
1204 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
1205
1206 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001207 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001208 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001209 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1210 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001211 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001212 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001213 /* implement (compile) */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001214 LY_CHECK_GOTO(ret = lys_set_implemented(mod, features), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001215 }
1216
1217 if (format == LYS_IN_YANG) {
1218 yang_parser_ctx_free(yangctx);
1219 } else {
1220 yin_parser_ctx_free(yinctx);
1221 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001222 if (module) {
1223 *module = mod;
1224 }
1225 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001226
1227error_ctx:
1228 ly_set_rm(&ctx->list, mod, NULL);
1229error:
1230 lys_module_free(mod, NULL);
1231 if (pctx) {
1232 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1233 }
1234 if (format == LYS_IN_YANG) {
1235 yang_parser_ctx_free(yangctx);
1236 } else {
1237 yin_parser_ctx_free(yinctx);
1238 }
1239
1240 return ret;
1241}
1242
Radek Krejci545b4872020-11-15 10:15:12 +01001243static LYS_INFORMAT
1244lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1245{
1246 if (!format && (in->type == LY_IN_FILEPATH)) {
1247 /* unknown format - try to detect it from filename's suffix */
1248 const char *path = in->method.fpath.filepath;
1249 size_t len = strlen(path);
1250
1251 /* ignore trailing whitespaces */
1252 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1253
1254 if ((len >= 5) && !strncmp(&path[len - 5], ".yang", 5)) {
1255 format = LYS_IN_YANG;
1256 } else if ((len >= 6) && !strncmp(&path[len - 4], ".yin", 4)) {
1257 format = LYS_IN_YIN;
1258 } /* else still unknown */
1259 }
1260
1261 return format;
1262}
1263
Michal Vasko7a0b0762020-09-02 16:37:01 +02001264API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265lys_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 +02001266{
1267 if (module) {
1268 *module = NULL;
1269 }
Radek Krejci545b4872020-11-15 10:15:12 +01001270 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1271
1272 format = lys_parse_get_format(in, format);
1273 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001274
1275 /* remember input position */
1276 in->func_start = in->current;
1277
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001278 return lys_create_module(ctx, in, format, 1, NULL, NULL, features, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001279}
1280
Michal Vasko3a41dff2020-07-15 14:30:28 +02001281API LY_ERR
1282lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001283{
Radek Krejci0f969882020-08-21 16:56:47 +02001284 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001285 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001286
Michal Vasko3a41dff2020-07-15 14:30:28 +02001287 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001288
Michal Vasko3a41dff2020-07-15 14:30:28 +02001289 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 +02001290
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001291 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001292 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001293
Michal Vasko3a41dff2020-07-15 14:30:28 +02001294 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001295}
1296
Michal Vasko3a41dff2020-07-15 14:30:28 +02001297API LY_ERR
1298lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001299{
Radek Krejci0f969882020-08-21 16:56:47 +02001300 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001301 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001302
Michal Vasko3a41dff2020-07-15 14:30:28 +02001303 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001304
Michal Vasko3a41dff2020-07-15 14:30:28 +02001305 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 +02001306
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001307 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001308 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001309
Michal Vasko3a41dff2020-07-15 14:30:28 +02001310 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001311}
1312
Michal Vasko3a41dff2020-07-15 14:30:28 +02001313API LY_ERR
1314lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001315{
Radek Krejci0f969882020-08-21 16:56:47 +02001316 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001317 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001318
Michal Vasko3a41dff2020-07-15 14:30:28 +02001319 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001320
Michal Vasko3a41dff2020-07-15 14:30:28 +02001321 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001322 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001323
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001324 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001325 ly_in_free(in, 0);
1326
Michal Vasko3a41dff2020-07-15 14:30:28 +02001327 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001328}
1329
1330API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001331lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001332 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001333{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001334 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001335 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001336 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001337 char *wd, *wn = NULL;
1338 DIR *dir = NULL;
1339 struct dirent *file;
1340 char *match_name = NULL;
1341 LYS_INFORMAT format_aux, match_format = 0;
1342 struct ly_set *dirs;
1343 struct stat st;
1344
1345 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1346
1347 /* start to fill the dir fifo with the context's search path (if set)
1348 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001349 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001350
1351 len = strlen(name);
1352 if (cwd) {
1353 wd = get_current_dir_name();
1354 if (!wd) {
1355 LOGMEM(NULL);
1356 goto cleanup;
1357 } else {
1358 /* add implicit current working directory (./) to be searched,
1359 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001360 ret = ly_set_add(dirs, wd, 0, NULL);
1361 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001362 implicit_cwd = 1;
1363 }
1364 }
1365 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001366 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001367 /* check for duplicities with the implicit current working directory */
1368 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1369 implicit_cwd = 0;
1370 continue;
1371 }
1372 wd = strdup(searchpaths[i]);
1373 if (!wd) {
1374 LOGMEM(NULL);
1375 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001376 } else {
1377 ret = ly_set_add(dirs, wd, 0, NULL);
1378 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001379 }
1380 }
1381 }
1382 wd = NULL;
1383
1384 /* start searching */
1385 while (dirs->count) {
1386 free(wd);
1387 free(wn); wn = NULL;
1388
1389 dirs->count--;
1390 wd = (char *)dirs->objs[dirs->count];
1391 dirs->objs[dirs->count] = NULL;
1392 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1393
1394 if (dir) {
1395 closedir(dir);
1396 }
1397 dir = opendir(wd);
1398 dir_len = strlen(wd);
1399 if (!dir) {
1400 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1401 } else {
1402 while ((file = readdir(dir))) {
1403 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1404 /* skip . and .. */
1405 continue;
1406 }
1407 free(wn);
1408 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1409 LOGMEM(NULL);
1410 goto cleanup;
1411 }
1412 if (stat(wn, &st) == -1) {
1413 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001414 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001415 continue;
1416 }
1417 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1418 /* we have another subdirectory in searchpath to explore,
1419 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001420 ret = ly_set_add(dirs, wn, 0, NULL);
1421 LY_CHECK_GOTO(ret, cleanup);
1422
Radek Krejcid33273d2018-10-25 14:55:52 +02001423 /* continue with the next item in current directory */
1424 wn = NULL;
1425 continue;
1426 } else if (!S_ISREG(st.st_mode)) {
1427 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1428 continue;
1429 }
1430
1431 /* here we know that the item is a file which can contain a module */
1432 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001433 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001434 /* different filename than the module we search for */
1435 continue;
1436 }
1437
1438 /* get type according to filename suffix */
1439 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001440 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001441 format_aux = LYS_IN_YANG;
Radek Krejci01a937f2020-11-15 10:14:12 +01001442 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1443 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001444 } else {
1445 /* not supportde suffix/file format */
1446 continue;
1447 }
1448
1449 if (revision) {
1450 /* we look for the specific revision, try to get it from the filename */
1451 if (file->d_name[len] == '@') {
1452 /* check revision from the filename */
1453 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1454 /* another revision */
1455 continue;
1456 } else {
1457 /* exact revision */
1458 free(match_name);
1459 match_name = wn;
1460 wn = NULL;
1461 match_len = dir_len + 1 + len;
1462 match_format = format_aux;
1463 goto success;
1464 }
1465 } else {
1466 /* continue trying to find exact revision match, use this only if not found */
1467 free(match_name);
1468 match_name = wn;
1469 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001470 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001471 match_format = format_aux;
1472 continue;
1473 }
1474 } else {
1475 /* remember the revision and try to find the newest one */
1476 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001477 if ((file->d_name[len] != '@') ||
1478 lysp_check_date(NULL, &file->d_name[len + 1], flen - ((format_aux == LYS_IN_YANG) ? 5 : 4) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001479 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001480 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001481 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1482 continue;
1483 }
1484 free(match_name);
1485 }
1486
1487 match_name = wn;
1488 wn = NULL;
1489 match_len = dir_len + 1 + len;
1490 match_format = format_aux;
1491 continue;
1492 }
1493 }
1494 }
1495 }
1496
1497success:
1498 (*localfile) = match_name;
1499 match_name = NULL;
1500 if (format) {
1501 (*format) = match_format;
1502 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001503 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001504
1505cleanup:
1506 free(wn);
1507 free(wd);
1508 if (dir) {
1509 closedir(dir);
1510 }
1511 free(match_name);
1512 ly_set_free(dirs, free);
1513
1514 return ret;
1515}