blob: 0e4d73661eb1915211893f9a61ecfd9f71352c99 [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 LY_CHECK_ARG_RET(NULL, node, NULL);
537 if (buffer) {
538 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100539 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200540 }
541
Radek Krejci327de162019-06-14 12:52:07 +0200542 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200543 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200544 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100545 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200546 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100547 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200548
Michal Vasko65de0402020-08-03 16:34:19 +0200549 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
550 /* schema-only node */
551 continue;
552 }
553
Michal Vasko11deea12020-08-05 13:54:50 +0200554 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200555 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100556 if (parent && (iter->parent == parent)) {
557 slash = "";
558 } else {
559 slash = "/";
560 }
Michal Vasko69730152020-10-09 16:30:07 +0200561 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200562 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200563 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100564 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200565 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100566 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200567 }
Radek Krejci327de162019-06-14 12:52:07 +0200568 } else {
569 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200570 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100571 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200572 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100573 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200574 }
Radek Krejci327de162019-06-14 12:52:07 +0200575 }
576 free(s);
577 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200578
Michal Vasko69730152020-10-09 16:30:07 +0200579 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200580 /* not enough space in buffer */
581 break;
582 }
Radek Krejci327de162019-06-14 12:52:07 +0200583 }
584
585 if (len < 0) {
586 free(path);
587 path = NULL;
588 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200589 if (buffer) {
590 strcpy(buffer, "/");
591 } else {
592 path = strdup("/");
593 }
Radek Krejci327de162019-06-14 12:52:07 +0200594 }
595 break;
596 }
597
Radek Krejci1c0c3442019-07-23 16:08:47 +0200598 if (buffer) {
599 return buffer;
600 } else {
601 return path;
602 }
Radek Krejci327de162019-06-14 12:52:07 +0200603}
604
Michal Vasko14654712020-02-06 08:35:21 +0100605API char *
606lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
607{
608 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
609}
610
Michal Vasko405cc9e2020-12-01 12:01:27 +0100611LY_ERR
612lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200613{
614 struct lys_module *m;
615
Michal Vasko405cc9e2020-12-01 12:01:27 +0100616 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200617
618 /* we have module from the current context */
619 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
620 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200621 assert(m != mod);
622
623 /* check collision with other implemented revision */
624 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
625 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
626 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200627 }
628
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100629 /* enable features */
630 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
631
Michal Vasko89b5c072020-10-06 13:52:44 +0200632 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100633 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200634
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200635 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200636 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200637
638 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100639 return lys_compile(mod, 0, unres);
640}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200641
Michal Vasko405cc9e2020-12-01 12:01:27 +0100642API LY_ERR
643lys_set_implemented(struct lys_module *mod, const char **features)
644{
645 LY_ERR ret = LY_SUCCESS, r;
646 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200647
Michal Vasko405cc9e2020-12-01 12:01:27 +0100648 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100649
Michal Vasko405cc9e2020-12-01 12:01:27 +0100650 if (mod->implemented) {
651 /* mod is already implemented, set the features */
652 r = lys_set_features(mod->parsed, features);
653 if (r == LY_EEXIST) {
654 /* no changes */
655 return LY_SUCCESS;
656 } else if (r) {
657 /* error */
658 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200659 }
660
Michal Vasko405cc9e2020-12-01 12:01:27 +0100661 /* full recompilation */
662 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200663 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100664
Michal Vasko405cc9e2020-12-01 12:01:27 +0100665 /* implement this module and any other required modules, recursively */
666 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100667
668 /* the first module being implemented is finished, resolve global unres, consolidate the set */
669 if (!ret) {
670 ret = lys_compile_unres_glob(mod->ctx, &unres);
671 }
672 if (ret) {
673 /* failure, full compile revert */
674 lys_compile_unres_glob_revert(mod->ctx, &unres);
675 }
676
677 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200678 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200679}
680
Michal Vasko7c8439f2020-08-05 13:25:19 +0200681static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100682lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200683{
684 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200685 LY_ARRAY_COUNT_TYPE u, v;
686
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100687 pmod->parsing = 1;
688 LY_ARRAY_FOR(pmod->imports, u) {
689 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200690 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100691 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
692 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200693 }
694 /* check for importing the same module twice */
695 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100696 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200697 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
698 }
699 }
700 }
Radek Krejci771928a2021-01-19 13:42:36 +0100701 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
702
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100703 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200704
705 return LY_SUCCESS;
706}
707
Michal Vasko3a41dff2020-07-15 14:30:28 +0200708LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200709lys_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 +0200710 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200711 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200712{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200713 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100714 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100715 struct lys_yang_parser_ctx *yangctx = NULL;
716 struct lys_yin_parser_ctx *yinctx = NULL;
717 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718
Michal Vasko3a41dff2020-07-15 14:30:28 +0200719 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100720
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100721 switch (format) {
722 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200723 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100724 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 break;
726 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200727 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100728 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 break;
730 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200731 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200732 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200734 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200735 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200736 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100737
738 /* make sure that the newest revision is at position 0 */
739 lysp_sort_revisions(submod->revs);
740
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200742 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100743 if (latest_sp) {
744 if (submod->revs) {
745 if (!latest_sp->revs) {
746 /* latest has no revision, so mod is anyway newer */
747 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200748 /* the latest_sp is zeroed later when the new module is being inserted into the context */
749 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
750 submod->latest_revision = latest_sp->latest_revision;
751 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100752 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200753 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100754 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200755 } else {
756 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100757 }
758 } else {
759 submod->latest_revision = 1;
760 }
761
Radek Krejcib3289d62019-09-18 12:21:39 +0200762 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200763 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200764 }
765
766 if (latest_sp) {
767 latest_sp->latest_revision = 0;
768 }
769
Michal Vasko7a0b0762020-09-02 16:37:01 +0200770 lys_parser_fill_filepath(ctx, in, &submod->filepath);
771
Michal Vasko7c8439f2020-08-05 13:25:19 +0200772 /* resolve imports and includes */
773 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
774
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100775 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100776 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
777 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100778
David Sedlák1b623122019-08-05 15:27:49 +0200779 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100780 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200781 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100782 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200783 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200784 *submodule = submod;
785 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200786
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100787error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200788 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200789 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100790 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200791 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100792 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200793 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200794 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200795}
796
Michal Vasko45b521c2020-11-04 17:14:39 +0100797/**
798 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
799 *
800 * @param[in] mod Parsed module to add to.
801 * @return LY_SUCCESS on success.
802 * @return LY_ERR on error.
803 */
804static LY_ERR
805lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
806{
807 struct lysp_ext_instance *ext_p;
808 struct lysp_stmt *stmt;
809 struct lysp_import *imp;
810
811 /*
812 * 1) edit-config's operation
813 */
814 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
815 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
816 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
817 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
818 ext_p->flags = LYS_INTERNAL;
819 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
820 ext_p->insubstmt_index = 0;
821
822 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
823 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
824 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
825 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
826 stmt->kw = LY_STMT_TYPE;
827
828 stmt->child = calloc(1, sizeof *stmt->child);
829 stmt = stmt->child;
830 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
831 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
832 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
833 stmt->kw = LY_STMT_ENUM;
834
835 stmt->next = calloc(1, sizeof *stmt->child);
836 stmt = stmt->next;
837 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
838 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
839 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
840 stmt->kw = LY_STMT_ENUM;
841
842 stmt->next = calloc(1, sizeof *stmt->child);
843 stmt = stmt->next;
844 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
845 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
846 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
847 stmt->kw = LY_STMT_ENUM;
848
849 stmt->next = calloc(1, sizeof *stmt->child);
850 stmt = stmt->next;
851 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
852 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
853 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
854 stmt->kw = LY_STMT_ENUM;
855
856 stmt->next = calloc(1, sizeof *stmt->child);
857 stmt = stmt->next;
858 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
859 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
860 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
861 stmt->kw = LY_STMT_ENUM;
862
863 /*
864 * 2) filter's type
865 */
866 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
867 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
868 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
869 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
870 ext_p->flags = LYS_INTERNAL;
871 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
872 ext_p->insubstmt_index = 0;
873
874 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
875 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
876 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
877 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
878 stmt->kw = LY_STMT_TYPE;
879
880 stmt->child = calloc(1, sizeof *stmt->child);
881 stmt = stmt->child;
882 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
883 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
884 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
885 stmt->kw = LY_STMT_ENUM;
886
887 stmt->next = calloc(1, sizeof *stmt->child);
888 stmt = stmt->next;
889 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
890 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
891 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
892 stmt->kw = LY_STMT_ENUM;
893
894 /* if-feature for enum allowed only for YANG 1.1 modules */
895 if (mod->version >= LYS_VERSION_1_1) {
896 stmt->child = calloc(1, sizeof *stmt->child);
897 stmt = stmt->child;
898 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
899 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
900 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
901 stmt->kw = LY_STMT_IF_FEATURE;
902 }
903
904 /*
905 * 3) filter's select
906 */
907 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
908 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
909 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
910 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
911 ext_p->flags = LYS_INTERNAL;
912 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
913 ext_p->insubstmt_index = 0;
914
915 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
916 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
917 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
918 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
919 stmt->kw = LY_STMT_TYPE;
920
921 /* create new imports for the used prefixes */
922 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
923
924 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
925 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
926 imp->flags = LYS_INTERNAL;
927
928 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
929
930 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
931 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
932 imp->flags = LYS_INTERNAL;
933
934 return LY_SUCCESS;
935}
936
937/**
938 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
939 *
940 * @param[in] mod Parsed module to add to.
941 * @return LY_SUCCESS on success.
942 * @return LY_ERR on error.
943 */
944static LY_ERR
945lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
946{
947 struct lysp_ext_instance *ext_p;
948 struct lysp_stmt *stmt;
949 struct lysp_import *imp;
950
951 /* add new extension instance */
952 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
953
954 /* fill in the extension instance fields */
955 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
956 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
957 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
958 ext_p->flags = LYS_INTERNAL;
959 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
960 ext_p->insubstmt_index = 0;
961
962 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
963 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
964 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
965 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
966 stmt->kw = LY_STMT_TYPE;
967
968 /* create new import for the used prefix */
969 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
970
971 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
972 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
973 imp->flags = LYS_INTERNAL;
974
975 return LY_SUCCESS;
976}
977
Michal Vasko3a41dff2020-07-15 14:30:28 +0200978LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +0100979lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200980 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 +0100981 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200982{
Michal Vasko8a69a1b2020-12-03 14:19:02 +0100983 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200984 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200985 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200986 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100987 struct lys_yang_parser_ctx *yangctx = NULL;
988 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200989 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200990 char *filename, *rev, *dot;
991 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +0100992 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +0200993
Michal Vasko34e334d2021-01-25 16:12:31 +0100994 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100995
Michal Vasko7a0b0762020-09-02 16:37:01 +0200996 if (module) {
997 *module = NULL;
998 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200999
Michal Vasko34e334d2021-01-25 16:12:31 +01001000 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1001 implement = 1;
1002 } else {
1003 implement = need_implemented;
Radek Krejci00a3e8a2021-01-27 08:24:49 +01001004 }
Michal Vasko34e334d2021-01-25 16:12:31 +01001005
Radek Krejci86d106e2018-10-18 09:53:19 +02001006 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001007 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001008 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001009
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001010 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001011 switch (format) {
1012 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001013 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001014 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001015 break;
1016 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001017 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001018 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001019 break;
1020 default:
1021 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001022 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001023 break;
1024 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001025 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001026
1027 /* make sure that the newest revision is at position 0 */
1028 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001029 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001030 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 +01001031 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001032
Radek Krejcib3289d62019-09-18 12:21:39 +02001033 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001034 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001035 if (latest) {
1036 if (mod->revision) {
1037 if (!latest->revision) {
1038 /* latest has no revision, so mod is anyway newer */
1039 mod->latest_revision = latest->latest_revision;
1040 /* the latest is zeroed later when the new module is being inserted into the context */
1041 } else if (strcmp(mod->revision, latest->revision) > 0) {
1042 mod->latest_revision = latest->latest_revision;
1043 /* the latest is zeroed later when the new module is being inserted into the context */
1044 } else {
1045 latest = NULL;
1046 }
1047 } else {
1048 latest = NULL;
1049 }
1050 } else {
1051 mod->latest_revision = 1;
1052 }
1053
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001054 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001055 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001056 }
1057
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001058 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001059 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001060 if (implement) {
1061 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1062 if (mod_impl && (mod_impl != mod_dup)) {
1063 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1064 mod_impl->revision ? mod_impl->revision : "<none>");
1065 ret = LY_EDENIED;
1066 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001067 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001068 }
1069 if (mod_dup) {
1070 if (implement) {
1071 if (!mod_dup->implemented) {
1072 /* just implement it */
1073 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1074 goto free_mod_cleanup;
1075 }
1076
1077 /* nothing to do */
1078 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1079 mod_dup->revision ? mod_dup->revision : "<none>");
1080 goto free_mod_cleanup;
1081 }
1082
1083 /* nothing to do */
1084 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1085 mod_dup->revision ? mod_dup->revision : "<none>");
1086 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001087 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001088
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001089 switch (in->type) {
1090 case LY_IN_FILEPATH:
1091 /* check that name and revision match filename */
1092 filename = strrchr(in->method.fpath.filepath, '/');
1093 if (!filename) {
1094 filename = in->method.fpath.filepath;
1095 } else {
1096 filename++;
1097 }
1098 rev = strchr(filename, '@');
1099 dot = strrchr(filename, '.');
1100
1101 /* name */
1102 len = strlen(mod->name);
1103 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001104 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001105 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1106 }
1107 if (rev) {
1108 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001109 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001110 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001111 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001112 }
1113 }
1114
1115 break;
1116 case LY_IN_FD:
1117 case LY_IN_FILE:
1118 case LY_IN_MEMORY:
1119 /* nothing special to do */
1120 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001121 case LY_IN_ERROR:
1122 LOGINT(ctx);
1123 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001124 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001125 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001126 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001127
Michal Vasko7a0b0762020-09-02 16:37:01 +02001128 if (latest) {
1129 latest->latest_revision = 0;
1130 }
1131
Michal Vasko45b521c2020-11-04 17:14:39 +01001132 /* add internal data in case specific modules were parsed */
1133 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001134 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001135 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001136 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001137 }
1138
Michal Vasko405cc9e2020-12-01 12:01:27 +01001139 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001140 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001141
Michal Vasko7a0b0762020-09-02 16:37:01 +02001142 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001143 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001144 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001145 ctx->module_set_id++;
1146
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001147 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001148 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001149
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001150 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001151 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001152 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001153 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1154 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001155
1156 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001157 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001158
Michal Vasko89b5c072020-10-06 13:52:44 +02001159 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001160 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001161 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001162
1163 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001164 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001165 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001166 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001167 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001168 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001169 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001170 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001171 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001172 }
1173
Michal Vasko405cc9e2020-12-01 12:01:27 +01001174 /* success */
1175 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001176
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001177free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001178 lys_module_free(mod, NULL);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001179 mod = NULL;
1180
Michal Vasko405cc9e2020-12-01 12:01:27 +01001181cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001182 if (pctx) {
1183 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1184 }
1185 if (format == LYS_IN_YANG) {
1186 yang_parser_ctx_free(yangctx);
1187 } else {
1188 yin_parser_ctx_free(yinctx);
1189 }
1190
Michal Vasko405cc9e2020-12-01 12:01:27 +01001191 if (!ret && module) {
1192 *module = mod;
1193 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001194 return ret;
1195}
1196
Radek Krejci545b4872020-11-15 10:15:12 +01001197static LYS_INFORMAT
1198lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1199{
1200 if (!format && (in->type == LY_IN_FILEPATH)) {
1201 /* unknown format - try to detect it from filename's suffix */
1202 const char *path = in->method.fpath.filepath;
1203 size_t len = strlen(path);
1204
1205 /* ignore trailing whitespaces */
1206 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1207
Radek Krejcif13b87b2020-12-01 22:02:17 +01001208 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1209 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001210 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001211 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1212 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001213 format = LYS_IN_YIN;
1214 } /* else still unknown */
1215 }
1216
1217 return format;
1218}
1219
Michal Vasko7a0b0762020-09-02 16:37:01 +02001220API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001221lys_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 +02001222{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001223 LY_ERR ret;
1224 struct lys_glob_unres unres = {0};
1225
Michal Vasko7a0b0762020-09-02 16:37:01 +02001226 if (module) {
1227 *module = NULL;
1228 }
Radek Krejci545b4872020-11-15 10:15:12 +01001229 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1230
1231 format = lys_parse_get_format(in, format);
1232 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001233
1234 /* remember input position */
1235 in->func_start = in->current;
1236
Michal Vasko405cc9e2020-12-01 12:01:27 +01001237 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1238 LY_CHECK_GOTO(ret, cleanup);
1239
1240 /* resolve global unres */
1241 ret = lys_compile_unres_glob(ctx, &unres);
1242 LY_CHECK_GOTO(ret, cleanup);
1243
1244cleanup:
1245 if (ret) {
1246 lys_compile_unres_glob_revert(ctx, &unres);
1247 }
1248 lys_compile_unres_glob_erase(ctx, &unres);
1249 if (ret && module) {
1250 *module = NULL;
1251 }
1252 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001253}
1254
Michal Vasko3a41dff2020-07-15 14:30:28 +02001255API LY_ERR
1256lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001257{
Radek Krejci0f969882020-08-21 16:56:47 +02001258 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001259 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001260
Michal Vasko3a41dff2020-07-15 14:30:28 +02001261 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001262
Michal Vasko3a41dff2020-07-15 14:30:28 +02001263 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 +02001264
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001266 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001267
Michal Vasko3a41dff2020-07-15 14:30:28 +02001268 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001269}
1270
Michal Vasko3a41dff2020-07-15 14:30:28 +02001271API LY_ERR
1272lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001273{
Radek Krejci0f969882020-08-21 16:56:47 +02001274 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001275 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001276
Michal Vasko3a41dff2020-07-15 14:30:28 +02001277 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001278
Michal Vasko3a41dff2020-07-15 14:30:28 +02001279 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 +02001280
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001281 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001282 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001283
Michal Vasko3a41dff2020-07-15 14:30:28 +02001284 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001285}
1286
Michal Vasko3a41dff2020-07-15 14:30:28 +02001287API LY_ERR
1288lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001289{
Radek Krejci0f969882020-08-21 16:56:47 +02001290 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001291 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001292
Michal Vasko3a41dff2020-07-15 14:30:28 +02001293 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001294
Michal Vasko3a41dff2020-07-15 14:30:28 +02001295 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001296 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001297
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001298 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001299 ly_in_free(in, 0);
1300
Michal Vasko3a41dff2020-07-15 14:30:28 +02001301 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001302}
1303
1304API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001305lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001306 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001307{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001308 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001309 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001310 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001311 char *wd, *wn = NULL;
1312 DIR *dir = NULL;
1313 struct dirent *file;
1314 char *match_name = NULL;
1315 LYS_INFORMAT format_aux, match_format = 0;
1316 struct ly_set *dirs;
1317 struct stat st;
1318
1319 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1320
1321 /* start to fill the dir fifo with the context's search path (if set)
1322 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001323 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001324
1325 len = strlen(name);
1326 if (cwd) {
1327 wd = get_current_dir_name();
1328 if (!wd) {
1329 LOGMEM(NULL);
1330 goto cleanup;
1331 } else {
1332 /* add implicit current working directory (./) to be searched,
1333 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001334 ret = ly_set_add(dirs, wd, 0, NULL);
1335 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001336 implicit_cwd = 1;
1337 }
1338 }
1339 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001340 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001341 /* check for duplicities with the implicit current working directory */
1342 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1343 implicit_cwd = 0;
1344 continue;
1345 }
1346 wd = strdup(searchpaths[i]);
1347 if (!wd) {
1348 LOGMEM(NULL);
1349 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001350 } else {
1351 ret = ly_set_add(dirs, wd, 0, NULL);
1352 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001353 }
1354 }
1355 }
1356 wd = NULL;
1357
1358 /* start searching */
1359 while (dirs->count) {
1360 free(wd);
1361 free(wn); wn = NULL;
1362
1363 dirs->count--;
1364 wd = (char *)dirs->objs[dirs->count];
1365 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001366 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001367
1368 if (dir) {
1369 closedir(dir);
1370 }
1371 dir = opendir(wd);
1372 dir_len = strlen(wd);
1373 if (!dir) {
1374 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1375 } else {
1376 while ((file = readdir(dir))) {
1377 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1378 /* skip . and .. */
1379 continue;
1380 }
1381 free(wn);
1382 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1383 LOGMEM(NULL);
1384 goto cleanup;
1385 }
1386 if (stat(wn, &st) == -1) {
1387 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001388 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001389 continue;
1390 }
1391 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1392 /* we have another subdirectory in searchpath to explore,
1393 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001394 ret = ly_set_add(dirs, wn, 0, NULL);
1395 LY_CHECK_GOTO(ret, cleanup);
1396
Radek Krejcid33273d2018-10-25 14:55:52 +02001397 /* continue with the next item in current directory */
1398 wn = NULL;
1399 continue;
1400 } else if (!S_ISREG(st.st_mode)) {
1401 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1402 continue;
1403 }
1404
1405 /* here we know that the item is a file which can contain a module */
1406 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001407 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001408 /* different filename than the module we search for */
1409 continue;
1410 }
1411
1412 /* get type according to filename suffix */
1413 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001414 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1415 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001416 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001417 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1418 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001419 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001420 } else {
1421 /* not supportde suffix/file format */
1422 continue;
1423 }
1424
1425 if (revision) {
1426 /* we look for the specific revision, try to get it from the filename */
1427 if (file->d_name[len] == '@') {
1428 /* check revision from the filename */
1429 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1430 /* another revision */
1431 continue;
1432 } else {
1433 /* exact revision */
1434 free(match_name);
1435 match_name = wn;
1436 wn = NULL;
1437 match_len = dir_len + 1 + len;
1438 match_format = format_aux;
1439 goto success;
1440 }
1441 } else {
1442 /* continue trying to find exact revision match, use this only if not found */
1443 free(match_name);
1444 match_name = wn;
1445 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001446 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001447 match_format = format_aux;
1448 continue;
1449 }
1450 } else {
1451 /* remember the revision and try to find the newest one */
1452 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001453 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001454 lysp_check_date(NULL, &file->d_name[len + 1],
1455 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001456 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001457 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001458 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1459 continue;
1460 }
1461 free(match_name);
1462 }
1463
1464 match_name = wn;
1465 wn = NULL;
1466 match_len = dir_len + 1 + len;
1467 match_format = format_aux;
1468 continue;
1469 }
1470 }
1471 }
1472 }
1473
1474success:
1475 (*localfile) = match_name;
1476 match_name = NULL;
1477 if (format) {
1478 (*format) = match_format;
1479 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001480 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001481
1482cleanup:
1483 free(wn);
1484 free(wd);
1485 if (dir) {
1486 closedir(dir);
1487 }
1488 free(match_name);
1489 ly_set_free(dirs, free);
1490
1491 return ret;
1492}