blob: 7d056c9f1f7f5351616fe2ae23444f77f9601610 [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 Krejci857189e2020-09-01 13:26:36 +0200132 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejcia3045382018-11-22 14:30:31 +0100133
134 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
135
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200136next:
Radek Krejcia3045382018-11-22 14:30:31 +0100137 if (!last) {
138 /* first call */
139
140 /* get know where to start */
141 if (parent) {
142 /* schema subtree */
Michal Vasko544e58a2021-01-28 14:33:41 +0100143 next = last = lysc_node_child(parent);
Radek Krejcia3045382018-11-22 14:30:31 +0100144 } else {
145 /* top level data */
146 next = last = module->data;
147 }
148 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100149 /* try to get action or notification */
150 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100151 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100152 /* test if the next can be returned */
153 goto check;
154
Michal Vasko1bf09392020-03-27 12:38:10 +0100155 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100156 action_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100157 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100158 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100159 action_flag = notif_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100160 next = last->next;
Michal Vasko20424b42020-08-31 12:29:38 +0200161 } else {
162 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100163 }
164
Radek Krejcia3045382018-11-22 14:30:31 +0100165repeat:
166 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100167 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200168 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100169 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200170 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100171 } else if (!action_flag) {
172 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200173 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100174 } else if (!notif_flag) {
175 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200176 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100177 } else {
178 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100179 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100180 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100181 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100182check:
Radek Krejcia3045382018-11-22 14:30:31 +0100183 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100184 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100185 case LYS_ACTION:
186 case LYS_NOTIF:
187 case LYS_LEAF:
188 case LYS_ANYXML:
189 case LYS_ANYDATA:
190 case LYS_LIST:
191 case LYS_LEAFLIST:
192 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200193 case LYS_CASE:
194 if (options & LYS_GETNEXT_WITHCASE) {
195 break;
196 } else {
197 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100198 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200199 }
200 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100201 case LYS_CONTAINER:
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100202 if (!(next->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100203 if (lysc_node_child(next)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100204 /* go into */
Michal Vasko544e58a2021-01-28 14:33:41 +0100205 next = lysc_node_child(next);
Radek Krejcia3045382018-11-22 14:30:31 +0100206 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100207 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100208 next = next->next;
209 }
210 goto repeat;
211 }
212 break;
213 case LYS_CHOICE:
214 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200215 break;
Michal Vasko544e58a2021-01-28 14:33:41 +0100216 } else if ((options & LYS_GETNEXT_NOCHOICE) || !lysc_node_child(next)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100217 next = next->next;
218 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100219 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100220 next = lysc_node_child(next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100221 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100222 /* go into */
223 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100224 }
Radek Krejcia3045382018-11-22 14:30:31 +0100225 }
226 goto repeat;
Michal Vasko544e58a2021-01-28 14:33:41 +0100227 case LYS_INPUT:
228 if (options & LYS_GETNEXT_OUTPUT) {
229 /* skip */
230 next = next->next;
231 } else {
232 /* go into */
233 next = lysc_node_child(next);
234 }
235 goto repeat;
236 case LYS_OUTPUT:
237 if (!(options & LYS_GETNEXT_OUTPUT)) {
238 /* skip */
239 next = next->next;
240 } else {
241 /* go into */
242 next = lysc_node_child(next);
243 }
244 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100245 default:
246 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200247 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100248 return NULL;
249 }
250
Radek Krejcia3045382018-11-22 14:30:31 +0100251 return next;
252}
253
254API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100255lys_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 +0200256 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100257{
258 const struct lysc_node *node = NULL;
259
260 LY_CHECK_ARG_RET(NULL, module, name, NULL);
261 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100262 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100263 }
264
265 while ((node = lys_getnext(node, parent, module->compiled, options))) {
266 if (!(node->nodetype & nodetype)) {
267 continue;
268 }
269 if (node->module != module) {
270 continue;
271 }
272
273 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200274 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100275 return node;
276 }
277 } else {
278 if (!strcmp(node->name, name)) {
279 return node;
280 }
281 }
282 }
283 return NULL;
284}
285
Michal Vasko519fd602020-05-26 12:17:39 +0200286API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100287lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
288 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200289{
290 LY_ERR ret = LY_SUCCESS;
291 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200292 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200293 uint32_t i;
294
Michal Vasko26512682021-01-11 11:35:40 +0100295 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200296 if (!(options & LYXP_SCNODE_ALL)) {
297 options = LYXP_SCNODE;
298 }
Michal Vasko26512682021-01-11 11:35:40 +0100299 if (!ctx) {
300 ctx = ctx_node->module->ctx;
301 }
Michal Vasko519fd602020-05-26 12:17:39 +0200302
303 memset(&xp_set, 0, sizeof xp_set);
304
305 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100306 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200307 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200308
309 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100310 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200311 LY_CHECK_GOTO(ret, cleanup);
312
313 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200314 ret = ly_set_new(set);
315 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200316
317 /* transform into ly_set */
318 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100319 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200320 (*set)->size = xp_set.used;
321
322 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200323 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200324 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200325 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200326 }
327 }
328
329cleanup:
330 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100331 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200332 return ret;
333}
334
Michal Vasko072de482020-08-05 13:27:21 +0200335API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200336lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
337 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
338{
339 LY_ERR ret = LY_SUCCESS;
340 struct lyxp_set xp_set = {0};
341 uint32_t i;
342
343 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
344 if (!(options & LYXP_SCNODE_ALL)) {
345 options = LYXP_SCNODE;
346 }
347
348 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100349 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 +0200350 LY_CHECK_GOTO(ret, cleanup);
351
352 /* allocate return set */
353 ret = ly_set_new(set);
354 LY_CHECK_GOTO(ret, cleanup);
355
356 /* transform into ly_set */
357 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
358 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
359 (*set)->size = xp_set.used;
360
361 for (i = 0; i < xp_set.used; ++i) {
Michal Vaskod97959c2020-12-10 12:18:28 +0100362 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM)) {
363 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM) ||
364 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200365 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
366 LY_CHECK_GOTO(ret, cleanup);
367 }
368 }
369
370cleanup:
371 lyxp_set_free_content(&xp_set);
372 if (ret) {
373 ly_set_free(*set, NULL);
374 *set = NULL;
375 }
376 return ret;
377}
378
379API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100380lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
381 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200382{
383 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200384 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200385 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200386 uint32_t i;
387
Michal Vasko26512682021-01-11 11:35:40 +0100388 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200389 if (!(options & LYXP_SCNODE_ALL)) {
390 options = LYXP_SCNODE;
391 }
Michal Vasko26512682021-01-11 11:35:40 +0100392 if (!ctx) {
393 ctx = ctx_node->module->ctx;
394 }
Michal Vasko072de482020-08-05 13:27:21 +0200395
Michal Vasko072de482020-08-05 13:27:21 +0200396 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100397 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200398 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200399
400 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100401 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200402 LY_CHECK_GOTO(ret, cleanup);
403
404 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200405 ret = ly_set_new(set);
406 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200407
408 /* transform into ly_set */
409 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100410 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200411 (*set)->size = xp_set.used;
412
413 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100414 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 +0200415 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200416 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200417 }
418 }
419
420cleanup:
421 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100422 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200423 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200424 ly_set_free(*set, NULL);
425 *set = NULL;
426 }
Michal Vasko072de482020-08-05 13:27:21 +0200427 return ret;
428}
429
Radek Krejcibc5644c2020-10-27 14:53:17 +0100430API LY_ERR
431lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
432{
433 LY_ERR ret = LY_SUCCESS;
434 LY_ARRAY_COUNT_TYPE u, v;
435
436 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
437
438 /* allocate return set */
439 LY_CHECK_RET(ly_set_new(set));
440
441 LY_ARRAY_FOR(path, u) {
442 /* add nodes from the path */
443 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
444 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
445 LY_ARRAY_FOR(path[u].predicates, v) {
446 /* add all the keys in a predicate */
447 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
448 }
449 }
450 }
451
452cleanup:
453 if (ret) {
454 ly_set_free(*set, NULL);
455 *set = NULL;
456 }
457 return ret;
458}
459
460API LY_ERR
461lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
462 struct ly_set **set)
463{
464 LY_ERR ret = LY_SUCCESS;
465 uint8_t oper;
466 struct lyxp_expr *expr = NULL;
467 struct ly_path *p = NULL;
468
469 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
470
471 if (!ctx) {
472 ctx = ctx_node->module->ctx;
473 }
474
475 /* parse */
476 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
477 LY_CHECK_GOTO(ret, cleanup);
478
479 /* compile */
480 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
481 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 +0100482 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100483 LY_CHECK_GOTO(ret, cleanup);
484
485 /* resolve */
486 ret = lys_find_lypath_atoms(p, set);
487
488cleanup:
489 ly_path_free(ctx, p);
490 lyxp_expr_free(ctx, expr);
491 return ret;
492}
493
494API const struct lysc_node *
495lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
496{
497 const struct lysc_node *snode = NULL;
498 struct lyxp_expr *exp = NULL;
499 struct ly_path *p = NULL;
500 LY_ERR ret;
501 uint8_t oper;
502
503 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
504
505 if (!ctx) {
506 ctx = ctx_node->module->ctx;
507 }
508
509 /* parse */
510 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
511 LY_CHECK_GOTO(ret, cleanup);
512
513 /* compile */
514 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
515 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 +0100516 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100517 LY_CHECK_GOTO(ret, cleanup);
518
519 /* get last node */
520 snode = p[LY_ARRAY_COUNT(p) - 1].node;
521
522cleanup:
523 ly_path_free(ctx, p);
524 lyxp_expr_free(ctx, exp);
525 return snode;
526}
527
Michal Vasko14654712020-02-06 08:35:21 +0100528char *
529lysc_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 +0200530 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200531{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200532 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200533 char *path = NULL;
534 int len = 0;
535
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200536 if (buffer) {
537 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100538 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200539 }
540
Radek Krejci327de162019-06-14 12:52:07 +0200541 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200542 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200543 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100544 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200545 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100546 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200547
Michal Vasko721b6f62021-02-08 08:52:53 +0100548 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko65de0402020-08-03 16:34:19 +0200549 /* schema-only node */
550 continue;
551 }
552
Michal Vasko11deea12020-08-05 13:54:50 +0200553 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200554 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100555 if (parent && (iter->parent == parent)) {
556 slash = "";
557 } else {
558 slash = "/";
559 }
Michal Vasko69730152020-10-09 16:30:07 +0200560 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200561 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200562 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100563 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200564 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100565 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200566 }
Radek Krejci327de162019-06-14 12:52:07 +0200567 } else {
568 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200569 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100570 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200571 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100572 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200573 }
Radek Krejci327de162019-06-14 12:52:07 +0200574 }
575 free(s);
576 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200577
Michal Vasko69730152020-10-09 16:30:07 +0200578 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200579 /* not enough space in buffer */
580 break;
581 }
Radek Krejci327de162019-06-14 12:52:07 +0200582 }
583
584 if (len < 0) {
585 free(path);
586 path = NULL;
587 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200588 if (buffer) {
589 strcpy(buffer, "/");
590 } else {
591 path = strdup("/");
592 }
Radek Krejci327de162019-06-14 12:52:07 +0200593 }
594 break;
595 }
596
Radek Krejci1c0c3442019-07-23 16:08:47 +0200597 if (buffer) {
598 return buffer;
599 } else {
600 return path;
601 }
Radek Krejci327de162019-06-14 12:52:07 +0200602}
603
Michal Vasko14654712020-02-06 08:35:21 +0100604API char *
605lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
606{
607 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
608}
609
Michal Vasko405cc9e2020-12-01 12:01:27 +0100610LY_ERR
611lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200612{
613 struct lys_module *m;
614
Michal Vasko405cc9e2020-12-01 12:01:27 +0100615 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200616
617 /* we have module from the current context */
618 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
619 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200620 assert(m != mod);
621
622 /* check collision with other implemented revision */
623 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
624 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
625 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200626 }
627
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100628 /* enable features */
629 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
630
Michal Vasko89b5c072020-10-06 13:52:44 +0200631 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100632 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200633
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200634 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200635 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200636
637 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100638 return lys_compile(mod, 0, unres);
639}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200640
Michal Vasko405cc9e2020-12-01 12:01:27 +0100641API LY_ERR
642lys_set_implemented(struct lys_module *mod, const char **features)
643{
644 LY_ERR ret = LY_SUCCESS, r;
645 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200646
Michal Vasko405cc9e2020-12-01 12:01:27 +0100647 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100648
Michal Vasko405cc9e2020-12-01 12:01:27 +0100649 if (mod->implemented) {
650 /* mod is already implemented, set the features */
651 r = lys_set_features(mod->parsed, features);
652 if (r == LY_EEXIST) {
653 /* no changes */
654 return LY_SUCCESS;
655 } else if (r) {
656 /* error */
657 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200658 }
659
Michal Vasko405cc9e2020-12-01 12:01:27 +0100660 /* full recompilation */
661 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200662 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100663
Michal Vasko405cc9e2020-12-01 12:01:27 +0100664 /* implement this module and any other required modules, recursively */
665 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100666
667 /* the first module being implemented is finished, resolve global unres, consolidate the set */
668 if (!ret) {
669 ret = lys_compile_unres_glob(mod->ctx, &unres);
670 }
671 if (ret) {
672 /* failure, full compile revert */
673 lys_compile_unres_glob_revert(mod->ctx, &unres);
674 }
675
676 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200677 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200678}
679
Michal Vasko7c8439f2020-08-05 13:25:19 +0200680static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100681lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200682{
683 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200684 LY_ARRAY_COUNT_TYPE u, v;
685
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100686 pmod->parsing = 1;
687 LY_ARRAY_FOR(pmod->imports, u) {
688 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200689 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100690 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
691 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200692 }
693 /* check for importing the same module twice */
694 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100695 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200696 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
697 }
698 }
699 }
Radek Krejci771928a2021-01-19 13:42:36 +0100700 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
701
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100702 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200703
704 return LY_SUCCESS;
705}
706
Michal Vasko3a41dff2020-07-15 14:30:28 +0200707LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200708lys_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 +0200709 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200710 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200711{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200712 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100714 struct lys_yang_parser_ctx *yangctx = NULL;
715 struct lys_yin_parser_ctx *yinctx = NULL;
716 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717
Michal Vasko3a41dff2020-07-15 14:30:28 +0200718 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100719
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100720 switch (format) {
721 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200722 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100723 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100724 break;
725 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200726 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100727 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 break;
729 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200730 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200731 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100732 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200733 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200734 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200735 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736
737 /* make sure that the newest revision is at position 0 */
738 lysp_sort_revisions(submod->revs);
739
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100740 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200741 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742 if (latest_sp) {
743 if (submod->revs) {
744 if (!latest_sp->revs) {
745 /* latest has no revision, so mod is anyway newer */
746 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200747 /* the latest_sp is zeroed later when the new module is being inserted into the context */
748 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
749 submod->latest_revision = latest_sp->latest_revision;
750 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100751 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200752 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100753 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200754 } else {
755 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100756 }
757 } else {
758 submod->latest_revision = 1;
759 }
760
Radek Krejcib3289d62019-09-18 12:21:39 +0200761 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200762 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200763 }
764
765 if (latest_sp) {
766 latest_sp->latest_revision = 0;
767 }
768
Michal Vasko7a0b0762020-09-02 16:37:01 +0200769 lys_parser_fill_filepath(ctx, in, &submod->filepath);
770
Michal Vasko7c8439f2020-08-05 13:25:19 +0200771 /* resolve imports and includes */
772 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
773
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100774 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100775 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
776 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100777
David Sedlák1b623122019-08-05 15:27:49 +0200778 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100779 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200780 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100781 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200782 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200783 *submodule = submod;
784 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200785
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200787 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200788 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100789 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200790 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100791 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200792 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200793 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200794}
795
Michal Vasko45b521c2020-11-04 17:14:39 +0100796/**
797 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
798 *
799 * @param[in] mod Parsed module to add to.
800 * @return LY_SUCCESS on success.
801 * @return LY_ERR on error.
802 */
803static LY_ERR
804lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
805{
806 struct lysp_ext_instance *ext_p;
807 struct lysp_stmt *stmt;
808 struct lysp_import *imp;
809
810 /*
811 * 1) edit-config's operation
812 */
813 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
814 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
815 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
816 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
817 ext_p->flags = LYS_INTERNAL;
818 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
819 ext_p->insubstmt_index = 0;
820
821 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
822 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
823 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
824 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
825 stmt->kw = LY_STMT_TYPE;
826
827 stmt->child = calloc(1, sizeof *stmt->child);
828 stmt = stmt->child;
829 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
830 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
831 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
832 stmt->kw = LY_STMT_ENUM;
833
834 stmt->next = calloc(1, sizeof *stmt->child);
835 stmt = stmt->next;
836 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
837 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
838 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
839 stmt->kw = LY_STMT_ENUM;
840
841 stmt->next = calloc(1, sizeof *stmt->child);
842 stmt = stmt->next;
843 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
844 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
845 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
846 stmt->kw = LY_STMT_ENUM;
847
848 stmt->next = calloc(1, sizeof *stmt->child);
849 stmt = stmt->next;
850 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
851 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
852 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
853 stmt->kw = LY_STMT_ENUM;
854
855 stmt->next = calloc(1, sizeof *stmt->child);
856 stmt = stmt->next;
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, "remove", 0, &stmt->arg));
860 stmt->kw = LY_STMT_ENUM;
861
862 /*
863 * 2) filter's type
864 */
865 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
866 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
867 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
868 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
869 ext_p->flags = LYS_INTERNAL;
870 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
871 ext_p->insubstmt_index = 0;
872
873 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
874 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
875 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
876 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
877 stmt->kw = LY_STMT_TYPE;
878
879 stmt->child = calloc(1, sizeof *stmt->child);
880 stmt = stmt->child;
881 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
882 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
883 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
884 stmt->kw = LY_STMT_ENUM;
885
886 stmt->next = calloc(1, sizeof *stmt->child);
887 stmt = stmt->next;
888 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
889 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
890 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
891 stmt->kw = LY_STMT_ENUM;
892
893 /* if-feature for enum allowed only for YANG 1.1 modules */
894 if (mod->version >= LYS_VERSION_1_1) {
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, "if-feature", 0, &stmt->stmt));
899 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
900 stmt->kw = LY_STMT_IF_FEATURE;
901 }
902
903 /*
904 * 3) filter's select
905 */
906 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
907 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
908 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
909 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
910 ext_p->flags = LYS_INTERNAL;
911 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
912 ext_p->insubstmt_index = 0;
913
914 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
915 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
916 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
917 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
918 stmt->kw = LY_STMT_TYPE;
919
920 /* create new imports for the used prefixes */
921 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
922
923 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
924 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
925 imp->flags = LYS_INTERNAL;
926
927 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
928
929 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
930 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
931 imp->flags = LYS_INTERNAL;
932
933 return LY_SUCCESS;
934}
935
936/**
937 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
938 *
939 * @param[in] mod Parsed module to add to.
940 * @return LY_SUCCESS on success.
941 * @return LY_ERR on error.
942 */
943static LY_ERR
944lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
945{
946 struct lysp_ext_instance *ext_p;
947 struct lysp_stmt *stmt;
948 struct lysp_import *imp;
949
950 /* add new extension instance */
951 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
952
953 /* fill in the extension instance fields */
954 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
955 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
956 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
957 ext_p->flags = LYS_INTERNAL;
958 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
959 ext_p->insubstmt_index = 0;
960
961 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
962 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
963 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
964 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
965 stmt->kw = LY_STMT_TYPE;
966
967 /* create new import for the used prefix */
968 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
969
970 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
971 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
972 imp->flags = LYS_INTERNAL;
973
974 return LY_SUCCESS;
975}
976
Michal Vasko3a41dff2020-07-15 14:30:28 +0200977LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100978lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200979 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 +0100980 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200981{
Michal Vasko8a69a1b2020-12-03 14:19:02 +0100982 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200983 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200984 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200985 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100986 struct lys_yang_parser_ctx *yangctx = NULL;
987 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200988 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200989 char *filename, *rev, *dot;
990 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +0100991 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +0200992
Michal Vasko34e334d2021-01-25 16:12:31 +0100993 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100994
Michal Vasko7a0b0762020-09-02 16:37:01 +0200995 if (module) {
996 *module = NULL;
997 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200998
Michal Vasko34e334d2021-01-25 16:12:31 +0100999 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1000 implement = 1;
1001 } else {
1002 implement = need_implemented;
Radek Krejci00a3e8a2021-01-27 08:24:49 +01001003 }
Michal Vasko34e334d2021-01-25 16:12:31 +01001004
Radek Krejci86d106e2018-10-18 09:53:19 +02001005 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001006 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001007 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001008
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001009 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001010 switch (format) {
1011 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001012 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001013 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001014 break;
1015 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001016 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001017 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001018 break;
1019 default:
1020 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001022 break;
1023 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001024 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001025
1026 /* make sure that the newest revision is at position 0 */
1027 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001028 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001029 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 +01001030 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001031
Radek Krejcib3289d62019-09-18 12:21:39 +02001032 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001033 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001034 if (latest) {
1035 if (mod->revision) {
1036 if (!latest->revision) {
1037 /* latest has no revision, so mod is anyway newer */
1038 mod->latest_revision = latest->latest_revision;
1039 /* the latest is zeroed later when the new module is being inserted into the context */
1040 } else if (strcmp(mod->revision, latest->revision) > 0) {
1041 mod->latest_revision = latest->latest_revision;
1042 /* the latest is zeroed later when the new module is being inserted into the context */
1043 } else {
1044 latest = NULL;
1045 }
1046 } else {
1047 latest = NULL;
1048 }
1049 } else {
1050 mod->latest_revision = 1;
1051 }
1052
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001054 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001055 }
1056
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001057 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001058 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001059 if (implement) {
1060 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1061 if (mod_impl && (mod_impl != mod_dup)) {
1062 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1063 mod_impl->revision ? mod_impl->revision : "<none>");
1064 ret = LY_EDENIED;
1065 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001066 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001067 }
1068 if (mod_dup) {
1069 if (implement) {
1070 if (!mod_dup->implemented) {
1071 /* just implement it */
1072 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1073 goto free_mod_cleanup;
1074 }
1075
1076 /* nothing to do */
1077 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1078 mod_dup->revision ? mod_dup->revision : "<none>");
1079 goto free_mod_cleanup;
1080 }
1081
1082 /* nothing to do */
1083 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1084 mod_dup->revision ? mod_dup->revision : "<none>");
1085 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001086 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001087
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001088 switch (in->type) {
1089 case LY_IN_FILEPATH:
1090 /* check that name and revision match filename */
1091 filename = strrchr(in->method.fpath.filepath, '/');
1092 if (!filename) {
1093 filename = in->method.fpath.filepath;
1094 } else {
1095 filename++;
1096 }
1097 rev = strchr(filename, '@');
1098 dot = strrchr(filename, '.');
1099
1100 /* name */
1101 len = strlen(mod->name);
1102 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001103 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001104 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1105 }
1106 if (rev) {
1107 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001108 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001109 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001110 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001111 }
1112 }
1113
1114 break;
1115 case LY_IN_FD:
1116 case LY_IN_FILE:
1117 case LY_IN_MEMORY:
1118 /* nothing special to do */
1119 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001120 case LY_IN_ERROR:
1121 LOGINT(ctx);
1122 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001123 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001124 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001125 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001126
Michal Vasko7a0b0762020-09-02 16:37:01 +02001127 if (latest) {
1128 latest->latest_revision = 0;
1129 }
1130
Michal Vasko45b521c2020-11-04 17:14:39 +01001131 /* add internal data in case specific modules were parsed */
1132 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001133 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001134 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001135 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001136 }
1137
Michal Vasko405cc9e2020-12-01 12:01:27 +01001138 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001139 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001140
Michal Vasko7a0b0762020-09-02 16:37:01 +02001141 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001142 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001143 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001144 ctx->module_set_id++;
1145
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001146 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001147 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001148
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001149 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001150 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001151 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001152 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1153 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001154
1155 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001156 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001157
Michal Vasko89b5c072020-10-06 13:52:44 +02001158 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001159 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001160 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001161
1162 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001163 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001164 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001165 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001166 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001167 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001168 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001169 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001170 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001171 }
1172
Michal Vasko405cc9e2020-12-01 12:01:27 +01001173 /* success */
1174 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001175
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001176free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001177 lys_module_free(mod, NULL);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001178 mod = NULL;
1179
Michal Vasko405cc9e2020-12-01 12:01:27 +01001180cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001181 if (pctx) {
1182 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1183 }
1184 if (format == LYS_IN_YANG) {
1185 yang_parser_ctx_free(yangctx);
1186 } else {
1187 yin_parser_ctx_free(yinctx);
1188 }
1189
Michal Vasko405cc9e2020-12-01 12:01:27 +01001190 if (!ret && module) {
1191 *module = mod;
1192 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001193 return ret;
1194}
1195
Radek Krejci545b4872020-11-15 10:15:12 +01001196static LYS_INFORMAT
1197lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1198{
1199 if (!format && (in->type == LY_IN_FILEPATH)) {
1200 /* unknown format - try to detect it from filename's suffix */
1201 const char *path = in->method.fpath.filepath;
1202 size_t len = strlen(path);
1203
1204 /* ignore trailing whitespaces */
1205 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1206
Radek Krejcif13b87b2020-12-01 22:02:17 +01001207 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1208 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001209 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001210 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1211 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001212 format = LYS_IN_YIN;
1213 } /* else still unknown */
1214 }
1215
1216 return format;
1217}
1218
Michal Vasko7a0b0762020-09-02 16:37:01 +02001219API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001220lys_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 +02001221{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001222 LY_ERR ret;
1223 struct lys_glob_unres unres = {0};
1224
Michal Vasko7a0b0762020-09-02 16:37:01 +02001225 if (module) {
1226 *module = NULL;
1227 }
Radek Krejci545b4872020-11-15 10:15:12 +01001228 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1229
1230 format = lys_parse_get_format(in, format);
1231 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001232
1233 /* remember input position */
1234 in->func_start = in->current;
1235
Michal Vasko405cc9e2020-12-01 12:01:27 +01001236 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1237 LY_CHECK_GOTO(ret, cleanup);
1238
1239 /* resolve global unres */
1240 ret = lys_compile_unres_glob(ctx, &unres);
1241 LY_CHECK_GOTO(ret, cleanup);
1242
1243cleanup:
1244 if (ret) {
1245 lys_compile_unres_glob_revert(ctx, &unres);
1246 }
1247 lys_compile_unres_glob_erase(ctx, &unres);
1248 if (ret && module) {
1249 *module = NULL;
1250 }
1251 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001252}
1253
Michal Vasko3a41dff2020-07-15 14:30:28 +02001254API LY_ERR
1255lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001256{
Radek Krejci0f969882020-08-21 16:56:47 +02001257 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001258 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001259
Michal Vasko3a41dff2020-07-15 14:30:28 +02001260 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001261
Michal Vasko3a41dff2020-07-15 14:30:28 +02001262 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 +02001263
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001264 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001265 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001266
Michal Vasko3a41dff2020-07-15 14:30:28 +02001267 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001268}
1269
Michal Vasko3a41dff2020-07-15 14:30:28 +02001270API LY_ERR
1271lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001272{
Radek Krejci0f969882020-08-21 16:56:47 +02001273 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001274 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001275
Michal Vasko3a41dff2020-07-15 14:30:28 +02001276 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001277
Michal Vasko3a41dff2020-07-15 14:30:28 +02001278 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 +02001279
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001280 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001281 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001282
Michal Vasko3a41dff2020-07-15 14:30:28 +02001283 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001284}
1285
Michal Vasko3a41dff2020-07-15 14:30:28 +02001286API LY_ERR
1287lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001288{
Radek Krejci0f969882020-08-21 16:56:47 +02001289 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001290 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001291
Michal Vasko3a41dff2020-07-15 14:30:28 +02001292 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001293
Michal Vasko3a41dff2020-07-15 14:30:28 +02001294 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001295 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001296
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001297 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001298 ly_in_free(in, 0);
1299
Michal Vasko3a41dff2020-07-15 14:30:28 +02001300 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001301}
1302
1303API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001304lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001305 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001306{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001307 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001308 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001309 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001310 char *wd, *wn = NULL;
1311 DIR *dir = NULL;
1312 struct dirent *file;
1313 char *match_name = NULL;
1314 LYS_INFORMAT format_aux, match_format = 0;
1315 struct ly_set *dirs;
1316 struct stat st;
1317
1318 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1319
1320 /* start to fill the dir fifo with the context's search path (if set)
1321 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001322 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001323
1324 len = strlen(name);
1325 if (cwd) {
1326 wd = get_current_dir_name();
1327 if (!wd) {
1328 LOGMEM(NULL);
1329 goto cleanup;
1330 } else {
1331 /* add implicit current working directory (./) to be searched,
1332 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001333 ret = ly_set_add(dirs, wd, 0, NULL);
1334 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001335 implicit_cwd = 1;
1336 }
1337 }
1338 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001339 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001340 /* check for duplicities with the implicit current working directory */
1341 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1342 implicit_cwd = 0;
1343 continue;
1344 }
1345 wd = strdup(searchpaths[i]);
1346 if (!wd) {
1347 LOGMEM(NULL);
1348 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001349 } else {
1350 ret = ly_set_add(dirs, wd, 0, NULL);
1351 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001352 }
1353 }
1354 }
1355 wd = NULL;
1356
1357 /* start searching */
1358 while (dirs->count) {
1359 free(wd);
1360 free(wn); wn = NULL;
1361
1362 dirs->count--;
1363 wd = (char *)dirs->objs[dirs->count];
1364 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001365 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001366
1367 if (dir) {
1368 closedir(dir);
1369 }
1370 dir = opendir(wd);
1371 dir_len = strlen(wd);
1372 if (!dir) {
1373 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1374 } else {
1375 while ((file = readdir(dir))) {
1376 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1377 /* skip . and .. */
1378 continue;
1379 }
1380 free(wn);
1381 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1382 LOGMEM(NULL);
1383 goto cleanup;
1384 }
1385 if (stat(wn, &st) == -1) {
1386 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001387 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001388 continue;
1389 }
1390 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1391 /* we have another subdirectory in searchpath to explore,
1392 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001393 ret = ly_set_add(dirs, wn, 0, NULL);
1394 LY_CHECK_GOTO(ret, cleanup);
1395
Radek Krejcid33273d2018-10-25 14:55:52 +02001396 /* continue with the next item in current directory */
1397 wn = NULL;
1398 continue;
1399 } else if (!S_ISREG(st.st_mode)) {
1400 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1401 continue;
1402 }
1403
1404 /* here we know that the item is a file which can contain a module */
1405 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001406 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001407 /* different filename than the module we search for */
1408 continue;
1409 }
1410
1411 /* get type according to filename suffix */
1412 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001413 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1414 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001415 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001416 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1417 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001418 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001419 } else {
1420 /* not supportde suffix/file format */
1421 continue;
1422 }
1423
1424 if (revision) {
1425 /* we look for the specific revision, try to get it from the filename */
1426 if (file->d_name[len] == '@') {
1427 /* check revision from the filename */
1428 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1429 /* another revision */
1430 continue;
1431 } else {
1432 /* exact revision */
1433 free(match_name);
1434 match_name = wn;
1435 wn = NULL;
1436 match_len = dir_len + 1 + len;
1437 match_format = format_aux;
1438 goto success;
1439 }
1440 } else {
1441 /* continue trying to find exact revision match, use this only if not found */
1442 free(match_name);
1443 match_name = wn;
1444 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001445 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001446 match_format = format_aux;
1447 continue;
1448 }
1449 } else {
1450 /* remember the revision and try to find the newest one */
1451 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001452 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001453 lysp_check_date(NULL, &file->d_name[len + 1],
1454 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001455 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001456 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001457 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1458 continue;
1459 }
1460 free(match_name);
1461 }
1462
1463 match_name = wn;
1464 wn = NULL;
1465 match_len = dir_len + 1 + len;
1466 match_format = format_aux;
1467 continue;
1468 }
1469 }
1470 }
1471 }
1472
1473success:
1474 (*localfile) = match_name;
1475 match_name = NULL;
1476 if (format) {
1477 (*format) = match_format;
1478 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001479 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001480
1481cleanup:
1482 free(wn);
1483 free(wd);
1484 if (dir) {
1485 closedir(dir);
1486 }
1487 free(match_name);
1488 ly_set_free(dirs, free);
1489
1490 return ret;
1491}