blob: d456467b81d406edebdaef12bcc1bc66b0f178bf [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
Christian Hopps32874e12021-05-01 09:43:54 -040015#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020016
Radek Krejcica376bd2020-06-11 16:04:06 +020017#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020018
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <assert.h>
Radek Krejci545b4872020-11-15 10:15:12 +010020#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020021#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdlib.h>
26#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020029
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020032#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010034#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020037#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020038#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020039#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "schema_compile.h"
Michal Vaskof4258e12021-06-15 12:11:42 +020041#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010042#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020043#include "set.h"
44#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010045#include "tree_edit.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020046#include "tree_schema_internal.h"
47#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020048
Radek Krejcieccf6602021-02-05 19:42:54 +010049/**
50 * @brief information about YANG statements
51 */
52struct stmt_info_s stmt_attr_info[] = {
Radek Krejci6b88a462021-02-17 12:39:34 +010053 [LY_STMT_NONE] = {NULL, NULL, 0},
54 [LY_STMT_ACTION] = {"action", "name", STMT_FLAG_ID},
55 [LY_STMT_ANYDATA] = {"anydata", "name", STMT_FLAG_ID},
56 [LY_STMT_ANYXML] = {"anyxml", "name", STMT_FLAG_ID},
57 [LY_STMT_ARGUMENT] = {"argument", "name", STMT_FLAG_ID},
58 [LY_STMT_ARG_TEXT] = {"text", NULL, 0},
59 [LY_STMT_ARG_VALUE] = {"value", NULL, 0},
60 [LY_STMT_AUGMENT] = {"augment", "target-node", STMT_FLAG_ID},
61 [LY_STMT_BASE] = {"base", "name", STMT_FLAG_ID},
62 [LY_STMT_BELONGS_TO] = {"belongs-to", "module", STMT_FLAG_ID},
63 [LY_STMT_BIT] = {"bit", "name", STMT_FLAG_ID},
64 [LY_STMT_CASE] = {"case", "name", STMT_FLAG_ID},
65 [LY_STMT_CHOICE] = {"choice", "name", STMT_FLAG_ID},
66 [LY_STMT_CONFIG] = {"config", "value", STMT_FLAG_ID},
67 [LY_STMT_CONTACT] = {"contact", "text", STMT_FLAG_YIN},
68 [LY_STMT_CONTAINER] = {"container", "name", STMT_FLAG_ID},
69 [LY_STMT_DEFAULT] = {"default", "value", 0},
70 [LY_STMT_DESCRIPTION] = {"description", "text", STMT_FLAG_YIN},
71 [LY_STMT_DEVIATE] = {"deviate", "value", STMT_FLAG_ID},
72 [LY_STMT_DEVIATION] = {"deviation", "target-node", STMT_FLAG_ID},
73 [LY_STMT_ENUM] = {"enum", "name", STMT_FLAG_ID},
74 [LY_STMT_ERROR_APP_TAG] = {"error-app-tag", "value", 0},
75 [LY_STMT_ERROR_MESSAGE] = {"error-message", "value", STMT_FLAG_YIN},
76 [LY_STMT_EXTENSION] = {"extension", "name", STMT_FLAG_ID},
77 [LY_STMT_EXTENSION_INSTANCE] = {NULL, NULL, 0},
78 [LY_STMT_FEATURE] = {"feature", "name", STMT_FLAG_ID},
79 [LY_STMT_FRACTION_DIGITS] = {"fraction-digits", "value", STMT_FLAG_ID},
80 [LY_STMT_GROUPING] = {"grouping", "name", STMT_FLAG_ID},
81 [LY_STMT_IDENTITY] = {"identity", "name", STMT_FLAG_ID},
82 [LY_STMT_IF_FEATURE] = {"if-feature", "name", 0},
83 [LY_STMT_IMPORT] = {"import", "module", STMT_FLAG_ID},
84 [LY_STMT_INCLUDE] = {"include", "module", STMT_FLAG_ID},
85 [LY_STMT_INPUT] = {"input", NULL, 0},
86 [LY_STMT_KEY] = {"key", "value", 0},
87 [LY_STMT_LEAF] = {"leaf", "name", STMT_FLAG_ID},
88 [LY_STMT_LEAF_LIST] = {"leaf-list", "name", STMT_FLAG_ID},
89 [LY_STMT_LENGTH] = {"length", "value", 0},
90 [LY_STMT_LIST] = {"list", "name", STMT_FLAG_ID},
91 [LY_STMT_MANDATORY] = {"mandatory", "value", STMT_FLAG_ID},
92 [LY_STMT_MAX_ELEMENTS] = {"max-elements", "value", STMT_FLAG_ID},
93 [LY_STMT_MIN_ELEMENTS] = {"min-elements", "value", STMT_FLAG_ID},
94 [LY_STMT_MODIFIER] = {"modifier", "value", STMT_FLAG_ID},
95 [LY_STMT_MODULE] = {"module", "name", STMT_FLAG_ID},
96 [LY_STMT_MUST] = {"must", "condition", 0},
97 [LY_STMT_NAMESPACE] = {"namespace", "uri", 0},
98 [LY_STMT_NOTIFICATION] = {"notification", "name", STMT_FLAG_ID},
99 [LY_STMT_ORDERED_BY] = {"ordered-by", "value", STMT_FLAG_ID},
100 [LY_STMT_ORGANIZATION] = {"organization", "text", STMT_FLAG_YIN},
101 [LY_STMT_OUTPUT] = {"output", NULL, 0},
102 [LY_STMT_PATH] = {"path", "value", 0},
103 [LY_STMT_PATTERN] = {"pattern", "value", 0},
104 [LY_STMT_POSITION] = {"position", "value", STMT_FLAG_ID},
105 [LY_STMT_PREFIX] = {"prefix", "value", STMT_FLAG_ID},
106 [LY_STMT_PRESENCE] = {"presence", "value", 0},
107 [LY_STMT_RANGE] = {"range", "value", 0},
108 [LY_STMT_REFERENCE] = {"reference", "text", STMT_FLAG_YIN},
109 [LY_STMT_REFINE] = {"refine", "target-node", STMT_FLAG_ID},
110 [LY_STMT_REQUIRE_INSTANCE] = {"require-instance", "value", STMT_FLAG_ID},
111 [LY_STMT_REVISION] = {"revision", "date", STMT_FLAG_ID},
112 [LY_STMT_REVISION_DATE] = {"revision-date", "date", STMT_FLAG_ID},
113 [LY_STMT_RPC] = {"rpc", "name", STMT_FLAG_ID},
114 [LY_STMT_STATUS] = {"status", "value", STMT_FLAG_ID},
115 [LY_STMT_SUBMODULE] = {"submodule", "name", STMT_FLAG_ID},
116 [LY_STMT_SYNTAX_LEFT_BRACE] = {"{", NULL, 0},
117 [LY_STMT_SYNTAX_RIGHT_BRACE] = {"}", NULL, 0},
118 [LY_STMT_SYNTAX_SEMICOLON] = {";", NULL, 0},
119 [LY_STMT_TYPE] = {"type", "name", STMT_FLAG_ID},
120 [LY_STMT_TYPEDEF] = {"typedef", "name", STMT_FLAG_ID},
121 [LY_STMT_UNIQUE] = {"unique", "tag", 0},
122 [LY_STMT_UNITS] = {"units", "name", 0},
123 [LY_STMT_USES] = {"uses", "name", STMT_FLAG_ID},
124 [LY_STMT_VALUE] = {"value", "value", STMT_FLAG_ID},
125 [LY_STMT_WHEN] = {"when", "condition", 0},
126 [LY_STMT_YANG_VERSION] = {"yang-version", "value", STMT_FLAG_ID},
127 [LY_STMT_YIN_ELEMENT] = {"yin-element", "value", STMT_FLAG_ID},
Radek Krejcieccf6602021-02-05 19:42:54 +0100128};
129
Radek Krejcif8ca8192021-03-02 16:50:06 +0100130API const char *
131ly_stmt2str(enum ly_stmt stmt)
132{
Michal Vaskocc64aec2021-05-18 17:33:11 +0200133 if (stmt == LY_STMT_EXTENSION_INSTANCE) {
134 return "extension instance";
135 } else {
136 return stmt_attr_info[stmt].name;
137 }
Radek Krejcif8ca8192021-03-02 16:50:06 +0100138}
139
Radek Krejcieccf6602021-02-05 19:42:54 +0100140const char * const ly_devmod_list[] = {
141 [LYS_DEV_NOT_SUPPORTED] = "not-supported",
142 [LYS_DEV_ADD] = "add",
143 [LYS_DEV_DELETE] = "delete",
144 [LYS_DEV_REPLACE] = "replace",
145};
146
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200147API LY_ERR
148lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
149{
Michal Vasko1d972ca2020-11-03 17:16:56 +0100150 struct lysc_node *elem, *elem2;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100151 const struct lysc_node_action *action;
152 const struct lysc_node_notif *notif;
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200153
154 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
155
156 LYSC_TREE_DFS_BEGIN(root, elem) {
157 /* schema node */
158 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
159
Radek Krejci2a9fc652021-01-22 17:44:34 +0100160 LY_LIST_FOR(lysc_node_actions(elem), action) {
161 LYSC_TREE_DFS_BEGIN(action, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200162 /* action subtree */
163 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
164
Radek Krejci2a9fc652021-01-22 17:44:34 +0100165 LYSC_TREE_DFS_END(action, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200166 }
167 }
168
Radek Krejci2a9fc652021-01-22 17:44:34 +0100169 LY_LIST_FOR(lysc_node_notifs(elem), notif) {
170 LYSC_TREE_DFS_BEGIN(notif, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200171 /* notification subtree */
172 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
173
Radek Krejci2a9fc652021-01-22 17:44:34 +0100174 LYSC_TREE_DFS_END(notif, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200175 }
176 }
177
178 LYSC_TREE_DFS_END(root, elem);
179 }
180
181 return LY_SUCCESS;
182}
183
184API LY_ERR
185lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
186{
Michal Vasko2336cf52020-11-03 17:18:15 +0100187 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200188
189 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
190
191 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +0100192 LY_LIST_FOR(mod->compiled->data, root) {
193 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
194 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200195
196 /* RPCs */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100197 LY_LIST_FOR((const struct lysc_node *)mod->compiled->rpcs, root) {
198 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200199 }
200
201 /* notifications */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100202 LY_LIST_FOR((const struct lysc_node *)mod->compiled->notifs, root) {
203 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200204 }
205
206 return LY_SUCCESS;
207}
208
Radek Krejcib93bd412020-11-02 13:23:11 +0100209static void
210lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
211{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100212 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100213 if (first_case->child) {
214 /* there is something to return */
215 (*next) = first_case->child;
216 return;
217 }
218 }
219
220 /* no children in choice's cases, so go to the choice's sibling instead of into it */
221 (*last) = (*next);
222 (*next) = (*next)->next;
223}
224
Radek Krejci035dacf2021-02-12 18:25:53 +0100225/**
226 * @brief Generic getnext function for ::lys_getnext() and ::lys_getnext_ext().
227 *
228 * Gets next schema tree (sibling) node element that can be instantiated in a data tree. Returned node can
229 * be from an augment. If the @p ext is provided, the function is locked inside the schema tree defined in the
230 * extension instance.
231 *
232 * ::lys_getnext_() is supposed to be called sequentially. In the first call, the \p last parameter is usually NULL
233 * and function starts returning i) the first \p parent's child or ii) the first top level element specified in the
234 * given extension (if provided) or iii) the first top level element of the \p module.
235 * Consequent calls suppose to provide the previously returned node as the \p last parameter and still the same
236 * \p parent and \p module parameters.
237 *
238 * Without options, the function is used to traverse only the schema nodes that can be paired with corresponding
239 * data nodes in a data tree. By setting some \p options the behavior can be modified to the extent that
240 * all the schema nodes are iteratively returned.
241 *
242 * @param[in] last Previously returned schema tree node, or NULL in case of the first call.
243 * @param[in] parent Parent of the subtree where the function starts processing.
244 * @param[in] module In case of iterating on top level elements, the \p parent is NULL and
245 * module must be specified.
246 * @param[in] ext The extension instance to provide a separate schema tree. To consider the top level elements in the tree,
247 * the \p parent must be NULL. Anyway, at least one of @p parent, @p module and @p ext parameters must be specified.
248 * @param[in] options [ORed options](@ref sgetnextflags).
249 * @return Next schema tree node that can be instantiated in a data tree, NULL in case there is no such element.
250 */
251static const struct lysc_node *
252lys_getnext_(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module,
253 const struct lysc_ext_instance *ext, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100254{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100255 const struct lysc_node *next = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200256 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci035dacf2021-02-12 18:25:53 +0100257 struct lysc_node **data_p = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100258
Radek Krejci035dacf2021-02-12 18:25:53 +0100259 LY_CHECK_ARG_RET(NULL, parent || module || ext, NULL);
Radek Krejcia3045382018-11-22 14:30:31 +0100260
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200261next:
Radek Krejcia3045382018-11-22 14:30:31 +0100262 if (!last) {
263 /* first call */
264
265 /* get know where to start */
266 if (parent) {
267 /* schema subtree */
Michal Vasko544e58a2021-01-28 14:33:41 +0100268 next = last = lysc_node_child(parent);
Radek Krejcia3045382018-11-22 14:30:31 +0100269 } else {
270 /* top level data */
Radek Krejci035dacf2021-02-12 18:25:53 +0100271 if (ext) {
272 lysc_ext_substmt(ext, LY_STMT_CONTAINER /* matches all nodes */, (void **)&data_p, NULL);
273 next = last = data_p ? *data_p : NULL;
274 } else {
275 next = last = module->data;
276 }
Radek Krejcia3045382018-11-22 14:30:31 +0100277 }
278 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100279 /* try to get action or notification */
280 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100281 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100282 /* test if the next can be returned */
283 goto check;
284
Michal Vasko1bf09392020-03-27 12:38:10 +0100285 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100286 action_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100287 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100288 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100289 action_flag = notif_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100290 next = last->next;
Michal Vasko20424b42020-08-31 12:29:38 +0200291 } else {
292 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100293 }
294
Radek Krejcia3045382018-11-22 14:30:31 +0100295repeat:
296 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100297 /* possibly go back to parent */
Radek Krejci035dacf2021-02-12 18:25:53 +0100298 data_p = NULL;
Michal Vasko69730152020-10-09 16:30:07 +0200299 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100300 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200301 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100302 } else if (!action_flag) {
303 action_flag = 1;
Radek Krejci035dacf2021-02-12 18:25:53 +0100304 if (ext) {
305 lysc_ext_substmt(ext, LY_STMT_RPC /* matches also actions */, (void **)&data_p, NULL);
306 next = data_p ? *data_p : NULL;
307 } else if (parent) {
308 next = (struct lysc_node *)lysc_node_actions(parent);
309 } else {
310 next = (struct lysc_node *)module->rpcs;
311 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100312 } else if (!notif_flag) {
313 notif_flag = 1;
Radek Krejci035dacf2021-02-12 18:25:53 +0100314 if (ext) {
315 lysc_ext_substmt(ext, LY_STMT_NOTIFICATION, (void **)&data_p, NULL);
316 next = data_p ? *data_p : NULL;
317 } else if (parent) {
318 next = (struct lysc_node *)lysc_node_notifs(parent);
319 } else {
320 next = (struct lysc_node *)module->notifs;
321 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100322 } else {
323 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100324 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100325 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100326 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100327check:
Radek Krejcia3045382018-11-22 14:30:31 +0100328 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100329 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100330 case LYS_ACTION:
331 case LYS_NOTIF:
332 case LYS_LEAF:
333 case LYS_ANYXML:
334 case LYS_ANYDATA:
335 case LYS_LIST:
336 case LYS_LEAFLIST:
337 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200338 case LYS_CASE:
339 if (options & LYS_GETNEXT_WITHCASE) {
340 break;
341 } else {
342 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100343 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200344 }
345 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100346 case LYS_CONTAINER:
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100347 if (!(next->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100348 if (lysc_node_child(next)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100349 /* go into */
Michal Vasko544e58a2021-01-28 14:33:41 +0100350 next = lysc_node_child(next);
Radek Krejcia3045382018-11-22 14:30:31 +0100351 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100352 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100353 next = next->next;
354 }
355 goto repeat;
356 }
357 break;
358 case LYS_CHOICE:
359 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200360 break;
Michal Vasko544e58a2021-01-28 14:33:41 +0100361 } else if ((options & LYS_GETNEXT_NOCHOICE) || !lysc_node_child(next)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100362 next = next->next;
363 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100364 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100365 next = lysc_node_child(next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100366 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100367 /* go into */
368 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100369 }
Radek Krejcia3045382018-11-22 14:30:31 +0100370 }
371 goto repeat;
Michal Vasko544e58a2021-01-28 14:33:41 +0100372 case LYS_INPUT:
373 if (options & LYS_GETNEXT_OUTPUT) {
374 /* skip */
375 next = next->next;
376 } else {
377 /* go into */
378 next = lysc_node_child(next);
379 }
380 goto repeat;
381 case LYS_OUTPUT:
382 if (!(options & LYS_GETNEXT_OUTPUT)) {
383 /* skip */
384 next = next->next;
385 } else {
386 /* go into */
387 next = lysc_node_child(next);
388 }
389 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100390 default:
391 /* we should not be here */
Radek Krejci035dacf2021-02-12 18:25:53 +0100392 LOGINT(module ? module->mod->ctx : parent ? parent->module->ctx : ext->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100393 return NULL;
394 }
395
Radek Krejcia3045382018-11-22 14:30:31 +0100396 return next;
397}
398
399API const struct lysc_node *
Radek Krejci035dacf2021-02-12 18:25:53 +0100400lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
401{
402 return lys_getnext_(last, parent, module, NULL, options);
403}
404
405API const struct lysc_node *
406lys_getnext_ext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_ext_instance *ext, uint32_t options)
407{
408 return lys_getnext_(last, parent, NULL, ext, options);
409}
410
Radek Krejcif16e2542021-02-17 15:39:23 +0100411const struct lysc_node *
Radek Krejciba05eab2021-03-10 13:19:29 +0100412lysc_ext_find_node(const struct lysc_ext_instance *ext, const struct lys_module *module, const char *name, size_t name_len,
Radek Krejcif16e2542021-02-17 15:39:23 +0100413 uint16_t nodetype, uint32_t options)
414{
415 const struct lysc_node *node = NULL;
416
417 LY_CHECK_ARG_RET(NULL, ext, name, NULL);
418 if (!nodetype) {
419 nodetype = LYS_NODETYPE_MASK;
420 }
421
422 if (module && (module != ext->module)) {
423 return NULL;
424 }
425
426 while ((node = lys_getnext_ext(node, NULL, ext, options))) {
427 if (!(node->nodetype & nodetype)) {
428 continue;
429 }
430
431 if (name_len) {
432 if (!ly_strncmp(node->name, name, name_len)) {
433 return node;
434 }
435 } else {
436 if (!strcmp(node->name, name)) {
437 return node;
438 }
439 }
440 }
441 return NULL;
442}
443
Radek Krejci035dacf2021-02-12 18:25:53 +0100444API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100445lys_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 +0200446 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100447{
448 const struct lysc_node *node = NULL;
449
450 LY_CHECK_ARG_RET(NULL, module, name, NULL);
451 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100452 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100453 }
454
455 while ((node = lys_getnext(node, parent, module->compiled, options))) {
456 if (!(node->nodetype & nodetype)) {
457 continue;
458 }
459 if (node->module != module) {
460 continue;
461 }
462
463 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200464 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100465 return node;
466 }
467 } else {
468 if (!strcmp(node->name, name)) {
469 return node;
470 }
471 }
472 }
473 return NULL;
474}
475
Michal Vasko519fd602020-05-26 12:17:39 +0200476API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100477lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
478 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200479{
480 LY_ERR ret = LY_SUCCESS;
481 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200482 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200483 uint32_t i;
484
Michal Vasko26512682021-01-11 11:35:40 +0100485 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200486 if (!(options & LYXP_SCNODE_ALL)) {
487 options = LYXP_SCNODE;
488 }
Michal Vasko26512682021-01-11 11:35:40 +0100489 if (!ctx) {
490 ctx = ctx_node->module->ctx;
491 }
Michal Vasko519fd602020-05-26 12:17:39 +0200492
493 memset(&xp_set, 0, sizeof xp_set);
494
495 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100496 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200497 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200498
499 /* atomize expression */
Radek Krejci8df109d2021-04-23 12:19:08 +0200500 ret = lyxp_atomize(ctx, exp, NULL, LY_VALUE_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200501 LY_CHECK_GOTO(ret, cleanup);
502
503 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200504 ret = ly_set_new(set);
505 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200506
507 /* transform into ly_set */
508 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100509 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200510 (*set)->size = xp_set.used;
511
512 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200513 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200514 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200515 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200516 }
517 }
518
519cleanup:
520 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100521 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200522 return ret;
523}
524
Michal Vasko072de482020-08-05 13:27:21 +0200525API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200526lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
527 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
528{
529 LY_ERR ret = LY_SUCCESS;
530 struct lyxp_set xp_set = {0};
531 uint32_t i;
532
533 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
534 if (!(options & LYXP_SCNODE_ALL)) {
535 options = LYXP_SCNODE;
536 }
537
538 /* atomize expression */
Radek Krejci8df109d2021-04-23 12:19:08 +0200539 ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_VALUE_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
Michal Vasko40308e72020-10-20 16:38:40 +0200540 LY_CHECK_GOTO(ret, cleanup);
541
542 /* allocate return set */
543 ret = ly_set_new(set);
544 LY_CHECK_GOTO(ret, cleanup);
545
546 /* transform into ly_set */
547 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
548 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
549 (*set)->size = xp_set.used;
550
551 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200552 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM_NODE)) {
553 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) ||
554 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL) ||
Michal Vaskod97959c2020-12-10 12:18:28 +0100555 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200556 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
557 LY_CHECK_GOTO(ret, cleanup);
558 }
559 }
560
561cleanup:
562 lyxp_set_free_content(&xp_set);
563 if (ret) {
564 ly_set_free(*set, NULL);
565 *set = NULL;
566 }
567 return ret;
568}
569
570API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100571lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
572 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200573{
574 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200575 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200576 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200577 uint32_t i;
578
Michal Vasko26512682021-01-11 11:35:40 +0100579 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200580 if (!(options & LYXP_SCNODE_ALL)) {
581 options = LYXP_SCNODE;
582 }
Michal Vasko26512682021-01-11 11:35:40 +0100583 if (!ctx) {
584 ctx = ctx_node->module->ctx;
585 }
Michal Vasko072de482020-08-05 13:27:21 +0200586
Michal Vasko072de482020-08-05 13:27:21 +0200587 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100588 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200589 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200590
591 /* atomize expression */
Radek Krejci8df109d2021-04-23 12:19:08 +0200592 ret = lyxp_atomize(ctx, exp, NULL, LY_VALUE_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200593 LY_CHECK_GOTO(ret, cleanup);
594
595 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200596 ret = ly_set_new(set);
597 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200598
599 /* transform into ly_set */
600 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100601 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200602 (*set)->size = xp_set.used;
603
604 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100605 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 +0200606 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200607 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200608 }
609 }
610
611cleanup:
612 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100613 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200614 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200615 ly_set_free(*set, NULL);
616 *set = NULL;
617 }
Michal Vasko072de482020-08-05 13:27:21 +0200618 return ret;
619}
620
Radek Krejcibc5644c2020-10-27 14:53:17 +0100621API LY_ERR
622lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
623{
624 LY_ERR ret = LY_SUCCESS;
625 LY_ARRAY_COUNT_TYPE u, v;
626
627 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
628
629 /* allocate return set */
630 LY_CHECK_RET(ly_set_new(set));
631
632 LY_ARRAY_FOR(path, u) {
633 /* add nodes from the path */
634 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
635 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
636 LY_ARRAY_FOR(path[u].predicates, v) {
637 /* add all the keys in a predicate */
638 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
639 }
640 }
641 }
642
643cleanup:
644 if (ret) {
645 ly_set_free(*set, NULL);
646 *set = NULL;
647 }
648 return ret;
649}
650
651API LY_ERR
652lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
653 struct ly_set **set)
654{
655 LY_ERR ret = LY_SUCCESS;
656 uint8_t oper;
657 struct lyxp_expr *expr = NULL;
658 struct ly_path *p = NULL;
659
660 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
661
662 if (!ctx) {
663 ctx = ctx_node->module->ctx;
664 }
665
666 /* parse */
667 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
668 LY_CHECK_GOTO(ret, cleanup);
669
670 /* compile */
671 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko0884d212021-10-14 09:21:46 +0200672 ret = ly_path_compile(ctx, NULL, ctx_node, NULL, expr, oper, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100673 LY_CHECK_GOTO(ret, cleanup);
674
675 /* resolve */
676 ret = lys_find_lypath_atoms(p, set);
677
678cleanup:
679 ly_path_free(ctx, p);
680 lyxp_expr_free(ctx, expr);
681 return ret;
682}
683
684API const struct lysc_node *
685lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
686{
687 const struct lysc_node *snode = NULL;
688 struct lyxp_expr *exp = NULL;
689 struct ly_path *p = NULL;
690 LY_ERR ret;
691 uint8_t oper;
692
693 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
694
695 if (!ctx) {
696 ctx = ctx_node->module->ctx;
697 }
698
699 /* parse */
700 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
701 LY_CHECK_GOTO(ret, cleanup);
702
703 /* compile */
704 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko0884d212021-10-14 09:21:46 +0200705 ret = ly_path_compile(ctx, NULL, ctx_node, NULL, exp, oper, LY_PATH_TARGET_MANY, 0, LY_VALUE_JSON, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100706 LY_CHECK_GOTO(ret, cleanup);
707
708 /* get last node */
709 snode = p[LY_ARRAY_COUNT(p) - 1].node;
710
711cleanup:
712 ly_path_free(ctx, p);
713 lyxp_expr_free(ctx, exp);
714 return snode;
715}
716
Michal Vasko14654712020-02-06 08:35:21 +0100717char *
718lysc_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 +0200719 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200720{
Michal Vasko87dd1342021-08-23 16:17:55 +0200721 const struct lysc_node *iter, *par;
Radek Krejci327de162019-06-14 12:52:07 +0200722 char *path = NULL;
723 int len = 0;
724
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200725 if (buffer) {
726 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100727 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200728 }
729
Radek Krejci327de162019-06-14 12:52:07 +0200730 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200732 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100733 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200734 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100735 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200736
Michal Vasko721b6f62021-02-08 08:52:53 +0100737 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko65de0402020-08-03 16:34:19 +0200738 /* schema-only node */
739 continue;
740 }
741
Michal Vasko11deea12020-08-05 13:54:50 +0200742 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200743 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100744 if (parent && (iter->parent == parent)) {
745 slash = "";
746 } else {
747 slash = "/";
748 }
Michal Vasko87dd1342021-08-23 16:17:55 +0200749
750 if (pathtype == LYSC_PATH_DATA) {
751 par = lysc_data_parent(iter);
752 } else {
753 par = iter->parent;
754 }
755
756 if (!par || (par->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200757 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200758 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100759 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200760 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100761 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200762 }
Radek Krejci327de162019-06-14 12:52:07 +0200763 } else {
764 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200765 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100766 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200767 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100768 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200769 }
Radek Krejci327de162019-06-14 12:52:07 +0200770 }
771 free(s);
772 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200773
Michal Vasko69730152020-10-09 16:30:07 +0200774 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200775 /* not enough space in buffer */
776 break;
777 }
Radek Krejci327de162019-06-14 12:52:07 +0200778 }
779
780 if (len < 0) {
781 free(path);
782 path = NULL;
783 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200784 if (buffer) {
785 strcpy(buffer, "/");
786 } else {
787 path = strdup("/");
788 }
Radek Krejci327de162019-06-14 12:52:07 +0200789 }
790 break;
791 }
792
Radek Krejci1c0c3442019-07-23 16:08:47 +0200793 if (buffer) {
794 return buffer;
795 } else {
796 return path;
797 }
Radek Krejci327de162019-06-14 12:52:07 +0200798}
799
Michal Vasko14654712020-02-06 08:35:21 +0100800API char *
801lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
802{
803 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
804}
805
Michal Vasko405cc9e2020-12-01 12:01:27 +0100806LY_ERR
Michal Vasko65333882021-06-10 14:12:16 +0200807_lys_set_implemented(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Michal Vasko405cc9e2020-12-01 12:01:27 +0100808{
809 LY_ERR ret = LY_SUCCESS, r;
aPiecek6b3d5422021-07-30 15:55:43 +0200810 struct lys_module *mod_iter;
Michal Vaskoc56d6372021-10-19 12:29:00 +0200811 const char **imp_f, *all_f[] = {"*", NULL};
Michal Vasko65333882021-06-10 14:12:16 +0200812 uint32_t i;
Michal Vasko916aefb2020-11-02 15:43:16 +0100813
Michal Vasko405cc9e2020-12-01 12:01:27 +0100814 if (mod->implemented) {
815 /* mod is already implemented, set the features */
816 r = lys_set_features(mod->parsed, features);
817 if (r == LY_EEXIST) {
818 /* no changes */
819 return LY_SUCCESS;
Michal Vasko7213ac52021-06-15 11:52:13 +0200820 } else if (!r) {
821 /* mark the module as changed */
Michal Vasko01db7de2021-04-16 12:23:30 +0200822 mod->to_compile = 1;
Michal Vasko01db7de2021-04-16 12:23:30 +0200823 }
Michal Vasko7213ac52021-06-15 11:52:13 +0200824
825 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200826 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100827
Michal Vasko7213ac52021-06-15 11:52:13 +0200828 /* implement, ignore recompilation because it must always take place later */
829 r = lys_implement(mod, features, unres);
830 LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup);
Michal Vasko65333882021-06-10 14:12:16 +0200831
832 if (mod->ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
833 /* implement all the imports as well */
834 for (i = 0; i < unres->creating.count; ++i) {
835 mod = unres->creating.objs[i];
836 if (mod->implemented) {
837 continue;
838 }
839
Michal Vaskoc56d6372021-10-19 12:29:00 +0200840 imp_f = (mod->ctx->flags & LY_CTX_ENABLE_IMP_FEATURES) ? all_f : NULL;
841 r = lys_implement(mod, imp_f, unres);
Michal Vasko7213ac52021-06-15 11:52:13 +0200842 LY_CHECK_ERR_GOTO(r && (r != LY_ERECOMPILE), ret = r, cleanup);
Michal Vasko65333882021-06-10 14:12:16 +0200843 }
844 }
845
aPiecek6b3d5422021-07-30 15:55:43 +0200846 /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
847 i = 0;
848 while ((mod_iter = ly_ctx_get_module_iter(mod->ctx, &i))) {
Michal Vaskoe8b085b2021-09-02 08:20:08 +0200849 if (!strcmp(mod_iter->name, mod->name) && (mod_iter != mod) && (mod_iter->latest_revision & LYS_MOD_IMPORTED_REV)) {
850 LOGVRB("Implemented module \"%s@%s\" was not and will not be imported if the revision-date is missing"
851 " in the import statement. Instead, the revision \"%s\" is imported.", mod->name, mod->revision,
852 mod_iter->revision);
aPiecek6b3d5422021-07-30 15:55:43 +0200853 break;
854 }
855 }
856
Michal Vasko65333882021-06-10 14:12:16 +0200857cleanup:
858 return ret;
859}
860
Michal Vaskof4258e12021-06-15 12:11:42 +0200861/**
Michal Vasko7ee5be22021-06-16 17:03:34 +0200862 * @brief Check whether it may be needed to (re)compile a module from a particular dependency set
863 * and if so, add it into its dep set.
864 *
865 * Dependency set includes all modules that need to be (re)compiled in case any of the module(s)
866 * in the dep set are (re)compiled.
867 *
868 * The reason for recompilation is possible disabled nodes and updating
869 * leafref targets to point to the newly compiled modules. Using the import relation, the
870 * dependency is reflexive because of possible foreign augments and deviations, which are compiled
871 * during the target module compilation.
872 *
873 * - every module must belong to exactly one dep set
874 * - implement flag must be ignored because it can be changed during dep set compilation
Michal Vaskof4258e12021-06-15 12:11:42 +0200875 *
876 * @param[in] mod Module to process.
877 * @param[in,out] ctx_set Set with all not-yet-processed modules.
878 * @param[in,out] dep_set Current dependency set to update.
Michal Vasko775fbd02021-07-28 08:25:29 +0200879 * @param[in] aux_set Set of traversed non-compiled modules, should be empty on first call.
Michal Vaskof4258e12021-06-15 12:11:42 +0200880 * @return LY_ERR value.
881 */
882static LY_ERR
Michal Vasko775fbd02021-07-28 08:25:29 +0200883lys_unres_dep_sets_create_mod_r(struct lys_module *mod, struct ly_set *ctx_set, struct ly_set *dep_set,
884 struct ly_set *aux_set)
Michal Vaskof4258e12021-06-15 12:11:42 +0200885{
886 struct lys_module *mod2;
887 struct lysp_import *imports;
888 uint32_t i;
889 LY_ARRAY_COUNT_TYPE u, v;
890 ly_bool found;
891
Michal Vasko7ee5be22021-06-16 17:03:34 +0200892 if (!lys_has_compiled(mod) || (mod->compiled && !lys_has_recompiled(mod))) {
Michal Vasko709f9a52021-07-21 10:51:59 +0200893 /* is already in a separate dep set */
Michal Vasko7ee5be22021-06-16 17:03:34 +0200894 if (!lys_has_groupings(mod)) {
Michal Vasko709f9a52021-07-21 10:51:59 +0200895 /* break the dep set here, no modules depend on this one */
Michal Vasko7ee5be22021-06-16 17:03:34 +0200896 return LY_SUCCESS;
897 }
Michal Vasko775fbd02021-07-28 08:25:29 +0200898
899 if (ly_set_contains(aux_set, mod, NULL)) {
900 /* it was traversed */
901 return LY_SUCCESS;
902 }
903
904 /* add a new auxiliary module */
905 LY_CHECK_RET(ly_set_add(aux_set, mod, 1, NULL));
Michal Vasko7ee5be22021-06-16 17:03:34 +0200906 } else {
Michal Vasko709f9a52021-07-21 10:51:59 +0200907 if (!ly_set_contains(ctx_set, mod, &i)) {
908 /* it was already processed */
909 return LY_SUCCESS;
910 }
911
912 /* remove it from the set, we are processing it now */
913 ly_set_rm_index(ctx_set, i, NULL);
914
Michal Vasko7ee5be22021-06-16 17:03:34 +0200915 /* add a new dependent module into the dep set */
916 LY_CHECK_RET(ly_set_add(dep_set, mod, 1, NULL));
Michal Vaskof4258e12021-06-15 12:11:42 +0200917 }
918
919 /* process imports of the module and submodules */
920 imports = mod->parsed->imports;
921 LY_ARRAY_FOR(imports, u) {
Michal Vasko709f9a52021-07-21 10:51:59 +0200922 mod2 = imports[u].module;
Michal Vasko775fbd02021-07-28 08:25:29 +0200923 LY_CHECK_RET(lys_unres_dep_sets_create_mod_r(mod2, ctx_set, dep_set, aux_set));
Michal Vaskof4258e12021-06-15 12:11:42 +0200924 }
925 LY_ARRAY_FOR(mod->parsed->includes, v) {
926 imports = mod->parsed->includes[v].submodule->imports;
927 LY_ARRAY_FOR(imports, u) {
Michal Vasko709f9a52021-07-21 10:51:59 +0200928 mod2 = imports[u].module;
929 if (!lys_has_compiled(mod2) || (mod2->compiled && !lys_has_recompiled(mod2))) {
930 if (!lys_has_groupings(mod2)) {
931 /* break the dep set here, no modules depend on this one */
932 continue;
933 }
934 }
935
Michal Vasko775fbd02021-07-28 08:25:29 +0200936 LY_CHECK_RET(lys_unres_dep_sets_create_mod_r(imports[u].module, ctx_set, dep_set, aux_set));
Michal Vaskof4258e12021-06-15 12:11:42 +0200937 }
938 }
939
940 /* process modules and submodules importing this module */
Michal Vasko7ee5be22021-06-16 17:03:34 +0200941 for (i = 0; i < mod->ctx->list.count; ++i) {
942 mod2 = mod->ctx->list.objs[i];
Michal Vaskof4258e12021-06-15 12:11:42 +0200943 found = 0;
944
945 imports = mod2->parsed->imports;
946 LY_ARRAY_FOR(imports, u) {
947 if (imports[u].module == mod) {
948 found = 1;
949 break;
950 }
951 }
952
953 if (!found) {
954 LY_ARRAY_FOR(mod2->parsed->includes, v) {
955 imports = mod2->parsed->includes[v].submodule->imports;
956 LY_ARRAY_FOR(imports, u) {
957 if (imports[u].module == mod) {
958 found = 1;
959 break;
960 }
961 }
962
963 if (found) {
964 break;
965 }
966 }
967 }
968
969 if (found) {
Michal Vasko775fbd02021-07-28 08:25:29 +0200970 LY_CHECK_RET(lys_unres_dep_sets_create_mod_r(mod2, ctx_set, dep_set, aux_set));
Michal Vaskof4258e12021-06-15 12:11:42 +0200971 }
972 }
973
974 return LY_SUCCESS;
975}
976
Michal Vasko709f9a52021-07-21 10:51:59 +0200977/**
978 * @brief Add all simple modules (that have nothing to (re)compile) into separate dep sets.
979 *
980 * @param[in,out] ctx_set Set with all not-yet-processed modules.
981 * @param[in,out] main_set Set of dependency module sets.
982 * @return LY_ERR value.
983 */
984static LY_ERR
985lys_unres_dep_sets_create_single(struct ly_set *ctx_set, struct ly_set *main_set)
986{
987 LY_ERR ret = LY_SUCCESS;
988 struct lys_module *m;
989 uint32_t i = 0;
990 struct ly_set *dep_set = NULL;
991
992 while (i < ctx_set->count) {
993 m = ctx_set->objs[i];
994 if (!lys_has_compiled(m) || (m->compiled && !lys_has_recompiled(m))) {
995 /* remove it from the set, we are processing it now */
996 ly_set_rm_index(ctx_set, i, NULL);
997
998 /* this module can be in a separate dep set (but there still may be modules importing this one
999 * that depend on imports of this one in case it defines groupings) */
1000 LY_CHECK_GOTO(ret = ly_set_new(&dep_set), cleanup);
1001 LY_CHECK_GOTO(ret = ly_set_add(dep_set, m, 1, NULL), cleanup);
1002 LY_CHECK_GOTO(ret = ly_set_add(main_set, dep_set, 1, NULL), cleanup);
1003 dep_set = NULL;
1004 } else {
1005 ++i;
1006 }
1007 }
1008
1009cleanup:
1010 ly_set_free(dep_set, NULL);
1011 return ret;
1012}
1013
Michal Vaskof4258e12021-06-15 12:11:42 +02001014LY_ERR
Michal Vasko50bc09a2021-06-17 17:31:56 +02001015lys_unres_dep_sets_create(struct ly_ctx *ctx, struct ly_set *main_set, struct lys_module *mod)
Michal Vaskof4258e12021-06-15 12:11:42 +02001016{
1017 LY_ERR ret = LY_SUCCESS;
1018 struct lys_module *m;
Michal Vasko775fbd02021-07-28 08:25:29 +02001019 struct ly_set *dep_set = NULL, *ctx_set = NULL, aux_set = {0};
Michal Vaskof4258e12021-06-15 12:11:42 +02001020 uint32_t i;
Michal Vasko7ee5be22021-06-16 17:03:34 +02001021 ly_bool found;
Michal Vaskof4258e12021-06-15 12:11:42 +02001022
Michal Vasko22b26222021-07-30 11:16:47 +02001023 assert(!main_set->count);
1024
Michal Vaskof4258e12021-06-15 12:11:42 +02001025 /* start with a duplicate set of modules that we will remove from */
1026 LY_CHECK_GOTO(ret = ly_set_dup(&ctx->list, NULL, &ctx_set), cleanup);
1027
Michal Vasko709f9a52021-07-21 10:51:59 +02001028 /* first create all dep sets with single modules */
1029 LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_single(ctx_set, main_set), cleanup);
1030
1031 if (mod && !ly_set_contains(ctx_set, mod, NULL)) {
1032 /* dep set for this module has already been created, nothing else to do */
1033 goto cleanup;
1034 }
1035
Michal Vaskof4258e12021-06-15 12:11:42 +02001036 while (ctx_set->count) {
1037 /* create new dep set */
1038 LY_CHECK_GOTO(ret = ly_set_new(&dep_set), cleanup);
1039
1040 if (mod) {
1041 /* use the module create a dep set with the rest of its dependent modules */
Michal Vasko775fbd02021-07-28 08:25:29 +02001042 LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_mod_r(mod, ctx_set, dep_set, &aux_set), cleanup);
Michal Vaskof4258e12021-06-15 12:11:42 +02001043 } else {
1044 /* use first ctx mod to create a dep set with the rest of its dependent modules */
Michal Vasko775fbd02021-07-28 08:25:29 +02001045 LY_CHECK_GOTO(ret = lys_unres_dep_sets_create_mod_r(ctx_set->objs[0], ctx_set, dep_set, &aux_set), cleanup);
Michal Vaskof4258e12021-06-15 12:11:42 +02001046 }
Michal Vasko775fbd02021-07-28 08:25:29 +02001047 ly_set_erase(&aux_set, NULL);
Michal Vasko7ee5be22021-06-16 17:03:34 +02001048 assert(dep_set->count);
Michal Vaskof4258e12021-06-15 12:11:42 +02001049
1050 /* check whether there is any module that will be (re)compiled */
Michal Vasko7ee5be22021-06-16 17:03:34 +02001051 found = 0;
Michal Vaskof4258e12021-06-15 12:11:42 +02001052 for (i = 0; i < dep_set->count; ++i) {
1053 m = dep_set->objs[i];
1054 if (m->to_compile) {
Michal Vasko7ee5be22021-06-16 17:03:34 +02001055 found = 1;
Michal Vaskof4258e12021-06-15 12:11:42 +02001056 break;
1057 }
1058 }
1059
Michal Vasko7ee5be22021-06-16 17:03:34 +02001060 if (found) {
Michal Vaskof4258e12021-06-15 12:11:42 +02001061 /* if there is, all the implemented modules need to be recompiled */
1062 for (i = 0; i < dep_set->count; ++i) {
1063 m = dep_set->objs[i];
1064 if (m->implemented) {
1065 m->to_compile = 1;
1066 }
1067 }
1068 }
1069
Michal Vasko7ee5be22021-06-16 17:03:34 +02001070 /* add the dep set into main set */
1071 LY_CHECK_GOTO(ret = ly_set_add(main_set, dep_set, 1, NULL), cleanup);
Michal Vaskof4258e12021-06-15 12:11:42 +02001072 dep_set = NULL;
1073
1074 if (mod) {
1075 /* we need dep set only for this module */
1076 break;
1077 }
1078 }
1079
Michal Vaskoe558f792021-07-28 08:20:15 +02001080#ifndef NDEBUG
1081 LOGDBG(LY_LDGDEPSETS, "dep sets created (%" PRIu32 "):", main_set->count);
1082 for (i = 0; i < main_set->count; ++i) {
1083 struct ly_set *iter_set = main_set->objs[i];
1084
1085 LOGDBG(LY_LDGDEPSETS, "dep set #%" PRIu32 ":", i);
1086 for (uint32_t j = 0; j < iter_set->count; ++j) {
1087 m = iter_set->objs[j];
1088 LOGDBG(LY_LDGDEPSETS, "\t%s", m->name);
1089 }
1090 }
1091#endif
1092
Michal Vaskof4258e12021-06-15 12:11:42 +02001093cleanup:
Michal Vaskobd65c2e2021-06-16 11:49:45 +02001094 assert(ret || main_set->objs);
Michal Vasko775fbd02021-07-28 08:25:29 +02001095 ly_set_erase(&aux_set, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001096 ly_set_free(dep_set, NULL);
1097 ly_set_free(ctx_set, NULL);
1098 return ret;
1099}
1100
1101void
1102lys_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres)
1103{
1104 uint32_t i, j, idx, prev_lo;
1105 struct ly_set *dep_set;
1106 struct lys_module *m;
1107 LY_ERR ret;
1108
1109 for (i = 0; i < unres->implementing.count; ++i) {
1110 m = unres->implementing.objs[i];
1111 assert(m->implemented);
1112
1113 /* make the module correctly non-implemented again */
1114 m->implemented = 0;
1115 lys_precompile_augments_deviations_revert(ctx, m);
1116 lysc_module_free(m->compiled);
1117 m->compiled = NULL;
1118
1119 /* should not be made implemented */
1120 m->to_compile = 0;
1121 }
1122
1123 for (i = 0; i < unres->creating.count; ++i) {
1124 m = unres->creating.objs[i];
1125
Michal Vaskod297f2a2021-06-16 11:51:28 +02001126 /* remove the module from the context */
Michal Vaskof4258e12021-06-15 12:11:42 +02001127 ly_set_rm(&ctx->list, m, NULL);
Michal Vaskof4258e12021-06-15 12:11:42 +02001128
Michal Vasko22b26222021-07-30 11:16:47 +02001129 /* remove it also from dep sets */
Michal Vasko7ee5be22021-06-16 17:03:34 +02001130 for (j = 0; j < unres->dep_sets.count; ++j) {
1131 dep_set = unres->dep_sets.objs[j];
1132 if (ly_set_contains(dep_set, m, &idx)) {
1133 ly_set_rm_index(dep_set, idx, NULL);
1134 break;
Michal Vaskof4258e12021-06-15 12:11:42 +02001135 }
Michal Vaskof4258e12021-06-15 12:11:42 +02001136 }
Michal Vaskod297f2a2021-06-16 11:51:28 +02001137
1138 /* free the module */
1139 lys_module_free(m);
Michal Vaskof4258e12021-06-15 12:11:42 +02001140 }
1141
1142 if (unres->implementing.count) {
1143 /* recompile previous context because some implemented modules are no longer implemented,
1144 * we can reuse the current to_compile flags */
1145 prev_lo = ly_log_options(0);
Michal Vasko22b26222021-07-30 11:16:47 +02001146 ret = lys_compile_depset_all(ctx, &ctx->unres);
Michal Vaskof4258e12021-06-15 12:11:42 +02001147 ly_log_options(prev_lo);
1148 if (ret) {
1149 LOGINT(ctx);
1150 }
1151 }
1152}
1153
1154void
1155lys_unres_glob_erase(struct lys_glob_unres *unres)
1156{
1157 uint32_t i;
1158
1159 for (i = 0; i < unres->dep_sets.count; ++i) {
1160 ly_set_free(unres->dep_sets.objs[i], NULL);
1161 }
1162 ly_set_erase(&unres->dep_sets, NULL);
1163 ly_set_erase(&unres->implementing, NULL);
1164 ly_set_erase(&unres->creating, NULL);
1165
Michal Vaskoc130e162021-10-19 11:30:00 +02001166 assert(!unres->ds_unres.whens.count);
1167 assert(!unres->ds_unres.musts.count);
Michal Vaskof4258e12021-06-15 12:11:42 +02001168 assert(!unres->ds_unres.leafrefs.count);
aPiecekc6526b42021-07-12 15:21:39 +02001169 assert(!unres->ds_unres.disabled_leafrefs.count);
Michal Vaskof4258e12021-06-15 12:11:42 +02001170 assert(!unres->ds_unres.dflts.count);
1171 assert(!unres->ds_unres.disabled.count);
1172}
1173
Michal Vasko65333882021-06-10 14:12:16 +02001174API LY_ERR
1175lys_set_implemented(struct lys_module *mod, const char **features)
1176{
1177 LY_ERR ret = LY_SUCCESS;
Michal Vasko22b26222021-07-30 11:16:47 +02001178 struct lys_glob_unres *unres = &mod->ctx->unres;
Michal Vasko65333882021-06-10 14:12:16 +02001179
1180 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
1181
1182 /* implement */
Michal Vasko22b26222021-07-30 11:16:47 +02001183 ret = _lys_set_implemented(mod, features, unres);
Michal Vasko65333882021-06-10 14:12:16 +02001184 LY_CHECK_GOTO(ret, cleanup);
1185
Michal Vaskof4258e12021-06-15 12:11:42 +02001186 if (!(mod->ctx->flags & LY_CTX_EXPLICIT_COMPILE)) {
1187 /* create dep set for the module and mark all the modules that will be (re)compiled */
Michal Vasko22b26222021-07-30 11:16:47 +02001188 LY_CHECK_GOTO(ret = lys_unres_dep_sets_create(mod->ctx, &unres->dep_sets, mod), cleanup);
Michal Vaskof4258e12021-06-15 12:11:42 +02001189
Michal Vasko709f9a52021-07-21 10:51:59 +02001190 /* (re)compile the whole dep set (other dep sets will have no modules marked for compilation) */
Michal Vasko22b26222021-07-30 11:16:47 +02001191 LY_CHECK_GOTO(ret = lys_compile_depset_all(mod->ctx, unres), cleanup);
1192
1193 /* unres resolved */
1194 lys_unres_glob_erase(unres);
Michal Vaskof4258e12021-06-15 12:11:42 +02001195 }
1196
Michal Vasko65333882021-06-10 14:12:16 +02001197cleanup:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001198 if (ret) {
Michal Vasko22b26222021-07-30 11:16:47 +02001199 lys_unres_glob_revert(mod->ctx, unres);
1200 lys_unres_glob_erase(unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001201 }
Michal Vasko89b5c072020-10-06 13:52:44 +02001202 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +02001203}
1204
Michal Vasko7c8439f2020-08-05 13:25:19 +02001205static LY_ERR
aPiecekc3e26142021-06-22 14:25:49 +02001206lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod, struct ly_set *new_mods)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001207{
1208 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001209 LY_ARRAY_COUNT_TYPE u, v;
1210
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001211 pmod->parsing = 1;
1212 LY_ARRAY_FOR(pmod->imports, u) {
1213 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +02001214 if (!imp->module) {
aPiecekc3e26142021-06-22 14:25:49 +02001215 LY_CHECK_RET(lys_parse_load(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, new_mods, &imp->module));
aPiecekd4911ee2021-07-30 07:40:24 +02001216
1217 if (!imp->rev[0]) {
1218 /* This module must be selected for the next similar
1219 * import without revision-date to avoid incorrect
1220 * derived identities in the ::lys_module.identities.
1221 */
1222 imp->module->latest_revision |= LYS_MOD_IMPORTED_REV;
1223 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001224 }
1225 /* check for importing the same module twice */
1226 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001227 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001228 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
1229 }
1230 }
1231 }
aPiecekc3e26142021-06-22 14:25:49 +02001232 LY_CHECK_RET(lysp_load_submodules(pctx, pmod, new_mods));
Radek Krejci771928a2021-01-19 13:42:36 +01001233
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001234 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001235
1236 return LY_SUCCESS;
1237}
1238
Michal Vasko3a41dff2020-07-15 14:30:28 +02001239LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +02001240lys_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 +02001241 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
aPiecekc3e26142021-06-22 14:25:49 +02001242 void *check_data, struct ly_set *new_mods, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001243{
Michal Vasko3a41dff2020-07-15 14:30:28 +02001244 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001245 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +01001246 struct lys_yang_parser_ctx *yangctx = NULL;
1247 struct lys_yin_parser_ctx *yinctx = NULL;
1248 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001249
Michal Vasko3a41dff2020-07-15 14:30:28 +02001250 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001251
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001252 switch (format) {
1253 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +02001254 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001255 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001256 break;
1257 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001258 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001259 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001260 break;
1261 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +02001262 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +02001263 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001264 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001265 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001266 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +02001267 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001268
1269 /* make sure that the newest revision is at position 0 */
1270 lysp_sort_revisions(submod->revs);
1271
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001272 /* decide the latest revision */
Michal Vasko8dc31992021-02-22 10:30:47 +01001273 latest_sp = (struct lysp_submodule *)ly_ctx_get_submodule2_latest(submod->mod, submod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001274 if (latest_sp) {
1275 if (submod->revs) {
1276 if (!latest_sp->revs) {
1277 /* latest has no revision, so mod is anyway newer */
1278 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +02001279 /* the latest_sp is zeroed later when the new module is being inserted into the context */
1280 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
1281 submod->latest_revision = latest_sp->latest_revision;
1282 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001283 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +02001284 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001285 }
Radek Krejcib3289d62019-09-18 12:21:39 +02001286 } else {
1287 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001288 }
1289 } else {
1290 submod->latest_revision = 1;
1291 }
1292
Radek Krejcib3289d62019-09-18 12:21:39 +02001293 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +02001294 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +02001295 }
1296
1297 if (latest_sp) {
1298 latest_sp->latest_revision = 0;
1299 }
1300
Michal Vasko7a0b0762020-09-02 16:37:01 +02001301 lys_parser_fill_filepath(ctx, in, &submod->filepath);
1302
Michal Vasko7c8439f2020-08-05 13:25:19 +02001303 /* resolve imports and includes */
aPiecekc3e26142021-06-22 14:25:49 +02001304 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod, new_mods), error);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001305
David Sedlák1b623122019-08-05 15:27:49 +02001306 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001307 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001308 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001309 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001310 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001311 *submodule = submod;
1312 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +02001313
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001314error:
Radek Krejcic64661b2020-08-15 15:42:26 +02001315 if (!submod || !submod->name) {
1316 LOGERR(ctx, ret, "Parsing submodule failed.");
1317 } else {
1318 LOGERR(ctx, ret, "Parsing submodule \"%s\" failed.", submod->name);
1319 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001320 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +02001321 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001322 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001323 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001324 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001325 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001326 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001327}
1328
Michal Vasko45b521c2020-11-04 17:14:39 +01001329/**
1330 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
1331 *
1332 * @param[in] mod Parsed module to add to.
1333 * @return LY_SUCCESS on success.
1334 * @return LY_ERR on error.
1335 */
1336static LY_ERR
1337lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
1338{
1339 struct lysp_ext_instance *ext_p;
1340 struct lysp_stmt *stmt;
1341 struct lysp_import *imp;
1342
1343 /*
1344 * 1) edit-config's operation
1345 */
1346 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1347 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1348 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1349 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
Radek Krejci8df109d2021-04-23 12:19:08 +02001350 ext_p->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001351 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001352 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001353 ext_p->parent_stmt = LY_STMT_MODULE;
1354 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001355
1356 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1357 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1358 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1359 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001360 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001361 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001362 stmt->kw = LY_STMT_TYPE;
1363
1364 stmt->child = calloc(1, sizeof *stmt->child);
1365 stmt = stmt->child;
1366 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1367 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1368 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001369 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001370 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001371 stmt->kw = LY_STMT_ENUM;
1372
1373 stmt->next = calloc(1, sizeof *stmt->child);
1374 stmt = stmt->next;
1375 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1376 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1377 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001378 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001379 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001380 stmt->kw = LY_STMT_ENUM;
1381
1382 stmt->next = calloc(1, sizeof *stmt->child);
1383 stmt = stmt->next;
1384 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1385 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1386 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001387 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001388 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001389 stmt->kw = LY_STMT_ENUM;
1390
1391 stmt->next = calloc(1, sizeof *stmt->child);
1392 stmt = stmt->next;
1393 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1394 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1395 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001396 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001397 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001398 stmt->kw = LY_STMT_ENUM;
1399
1400 stmt->next = calloc(1, sizeof *stmt->child);
1401 stmt = stmt->next;
1402 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1403 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1404 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001405 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001406 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001407 stmt->kw = LY_STMT_ENUM;
1408
1409 /*
1410 * 2) filter's type
1411 */
1412 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1413 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1414 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1415 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
Radek Krejci8df109d2021-04-23 12:19:08 +02001416 ext_p->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001417 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001418 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001419 ext_p->parent_stmt = LY_STMT_MODULE;
1420 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001421
1422 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1423 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1424 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1425 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001426 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001427 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001428 stmt->kw = LY_STMT_TYPE;
1429
1430 stmt->child = calloc(1, sizeof *stmt->child);
1431 stmt = stmt->child;
1432 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1433 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1434 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001435 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001436 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001437 stmt->kw = LY_STMT_ENUM;
1438
1439 stmt->next = calloc(1, sizeof *stmt->child);
1440 stmt = stmt->next;
1441 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1442 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1443 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001444 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001445 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001446 stmt->kw = LY_STMT_ENUM;
1447
1448 /* if-feature for enum allowed only for YANG 1.1 modules */
1449 if (mod->version >= LYS_VERSION_1_1) {
1450 stmt->child = calloc(1, sizeof *stmt->child);
1451 stmt = stmt->child;
1452 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1453 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
1454 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001455 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001456 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001457 stmt->kw = LY_STMT_IF_FEATURE;
1458 }
1459
1460 /*
1461 * 3) filter's select
1462 */
1463 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1464 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1465 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1466 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
Radek Krejci8df109d2021-04-23 12:19:08 +02001467 ext_p->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001468 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001469 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001470 ext_p->parent_stmt = LY_STMT_MODULE;
1471 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001472
1473 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1474 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1475 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1476 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001477 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001478 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001479 stmt->kw = LY_STMT_TYPE;
1480
1481 /* create new imports for the used prefixes */
1482 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1483
1484 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1485 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1486 imp->flags = LYS_INTERNAL;
1487
1488 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1489
1490 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
1491 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
1492 imp->flags = LYS_INTERNAL;
1493
1494 return LY_SUCCESS;
1495}
1496
1497/**
1498 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
1499 *
1500 * @param[in] mod Parsed module to add to.
1501 * @return LY_SUCCESS on success.
1502 * @return LY_ERR on error.
1503 */
1504static LY_ERR
1505lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
1506{
1507 struct lysp_ext_instance *ext_p;
1508 struct lysp_stmt *stmt;
1509 struct lysp_import *imp;
1510
1511 /* add new extension instance */
1512 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1513
1514 /* fill in the extension instance fields */
1515 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1516 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1517 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
Radek Krejci8df109d2021-04-23 12:19:08 +02001518 ext_p->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001519 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001520 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001521 ext_p->parent_stmt = LY_STMT_MODULE;
1522 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001523
1524 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1525 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1526 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1527 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
Radek Krejci8df109d2021-04-23 12:19:08 +02001528 stmt->format = LY_VALUE_SCHEMA;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001529 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001530 stmt->kw = LY_STMT_TYPE;
1531
1532 /* create new import for the used prefix */
1533 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1534
1535 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1536 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1537 imp->flags = LYS_INTERNAL;
1538
1539 return LY_SUCCESS;
1540}
1541
Michal Vasko3a41dff2020-07-15 14:30:28 +02001542LY_ERR
Michal Vasko4e205e82021-06-08 14:01:47 +02001543lys_parse_in(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001544 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vaskodd992582021-06-10 14:34:57 +02001545 void *check_data, struct ly_set *new_mods, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001546{
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001547 struct lys_module *mod = NULL, *latest, *mod_dup = NULL;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001548 LY_ERR ret;
Michal Vaskob36053d2020-03-26 15:49:30 +01001549 struct lys_yang_parser_ctx *yangctx = NULL;
1550 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001551 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001552 char *filename, *rev, *dot;
1553 size_t len;
Radek Krejcic64661b2020-08-15 15:42:26 +02001554 ly_bool module_created = 0;
Radek Krejci86d106e2018-10-18 09:53:19 +02001555
Michal Vaskodd992582021-06-10 14:34:57 +02001556 assert(ctx && in && new_mods);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001557
Michal Vasko7a0b0762020-09-02 16:37:01 +02001558 if (module) {
1559 *module = NULL;
1560 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001561
1562 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001563 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001564 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001565
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001566 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001567 switch (format) {
1568 case LYS_IN_YIN:
aPiecekc3e26142021-06-22 14:25:49 +02001569 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001570 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001571 break;
1572 case LYS_IN_YANG:
aPiecekc3e26142021-06-22 14:25:49 +02001573 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001574 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001575 break;
1576 default:
1577 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001578 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001579 break;
1580 }
Radek Krejcic64661b2020-08-15 15:42:26 +02001581 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001582
1583 /* make sure that the newest revision is at position 0 */
1584 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001585 if (mod->parsed->revs) {
Radek Krejcic64661b2020-08-15 15:42:26 +02001586 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), cleanup);
Radek Krejci0af46292019-01-11 16:02:31 +01001587 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001588
Radek Krejcib3289d62019-09-18 12:21:39 +02001589 /* decide the latest revision */
Michal Vaskoa51ef072021-07-02 10:40:30 +02001590 latest = ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001591 if (latest) {
1592 if (mod->revision) {
1593 if (!latest->revision) {
1594 /* latest has no revision, so mod is anyway newer */
aPiecek8ca21bd2021-07-26 14:31:01 +02001595 mod->latest_revision = latest->latest_revision & (LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS);
Radek Krejcib3289d62019-09-18 12:21:39 +02001596 /* the latest is zeroed later when the new module is being inserted into the context */
1597 } else if (strcmp(mod->revision, latest->revision) > 0) {
aPiecek8ca21bd2021-07-26 14:31:01 +02001598 mod->latest_revision = latest->latest_revision & (LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS);
Radek Krejcib3289d62019-09-18 12:21:39 +02001599 /* the latest is zeroed later when the new module is being inserted into the context */
1600 } else {
1601 latest = NULL;
1602 }
1603 } else {
1604 latest = NULL;
1605 }
1606 } else {
aPiecek8ca21bd2021-07-26 14:31:01 +02001607 mod->latest_revision = LYS_MOD_LATEST_REV;
Radek Krejcib3289d62019-09-18 12:21:39 +02001608 }
1609
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001610 if (custom_check) {
Radek Krejcic64661b2020-08-15 15:42:26 +02001611 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001612 }
1613
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001614 /* check whether it is not already in the context in the same revision */
Michal Vaskoa51ef072021-07-02 10:40:30 +02001615 mod_dup = ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001616 if (mod_dup) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001617 /* nothing to do */
1618 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1619 mod_dup->revision ? mod_dup->revision : "<none>");
Radek Krejcic64661b2020-08-15 15:42:26 +02001620 goto cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001621 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001622
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001623 switch (in->type) {
1624 case LY_IN_FILEPATH:
1625 /* check that name and revision match filename */
1626 filename = strrchr(in->method.fpath.filepath, '/');
1627 if (!filename) {
1628 filename = in->method.fpath.filepath;
1629 } else {
1630 filename++;
1631 }
1632 rev = strchr(filename, '@');
1633 dot = strrchr(filename, '.');
1634
1635 /* name */
1636 len = strlen(mod->name);
1637 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001638 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001639 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1640 }
1641 if (rev) {
1642 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001643 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001644 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001645 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001646 }
1647 }
1648
1649 break;
1650 case LY_IN_FD:
1651 case LY_IN_FILE:
1652 case LY_IN_MEMORY:
1653 /* nothing special to do */
1654 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001655 case LY_IN_ERROR:
1656 LOGINT(ctx);
1657 ret = LY_EINT;
Radek Krejcic64661b2020-08-15 15:42:26 +02001658 goto cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001659 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001660 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001661
Michal Vasko7a0b0762020-09-02 16:37:01 +02001662 if (latest) {
aPiecek8ca21bd2021-07-26 14:31:01 +02001663 latest->latest_revision &= ~(LYS_MOD_LATEST_REV | LYS_MOD_LATEST_SEARCHDIRS);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001664 }
1665
Michal Vasko45b521c2020-11-04 17:14:39 +01001666 /* add internal data in case specific modules were parsed */
1667 if (!strcmp(mod->name, "ietf-netconf")) {
Radek Krejcic64661b2020-08-15 15:42:26 +02001668 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001669 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Radek Krejcic64661b2020-08-15 15:42:26 +02001670 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001671 }
1672
Michal Vasko405cc9e2020-12-01 12:01:27 +01001673 /* add the module into newly created module set, will also be freed from there on any error */
Radek Krejcic64661b2020-08-15 15:42:26 +02001674 LY_CHECK_GOTO(ret = ly_set_add(new_mods, mod, 1, NULL), cleanup);
1675 module_created = 1;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001676
Michal Vasko7a0b0762020-09-02 16:37:01 +02001677 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001678 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001679 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko794ab4b2021-03-31 09:42:19 +02001680 ctx->change_count++;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001681
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001682 /* resolve includes and all imports */
aPiecekc3e26142021-06-22 14:25:49 +02001683 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed, new_mods), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001684
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001685 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001686 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
aPiecek63e080d2021-06-29 13:53:28 +02001687 LY_CHECK_GOTO(ret = lysp_check_dup_groupings(pctx, mod->parsed), cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001688 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1689 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001690
1691 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001692 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001693
aPiecek6b3d5422021-07-30 15:55:43 +02001694 /* compile identities */
1695 LY_CHECK_GOTO(ret = lys_compile_identities(mod), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001696
Michal Vasko405cc9e2020-12-01 12:01:27 +01001697 /* success */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001698
Radek Krejcic64661b2020-08-15 15:42:26 +02001699cleanup:
Michal Vaskobe04af42021-07-16 10:48:43 +02001700 if (ret && (ret != LY_EEXIST)) {
Radek Krejcic64661b2020-08-15 15:42:26 +02001701 if (mod && mod->name) {
1702 /* there are cases when path is not available for parsing error, so this additional
1703 * message tries to add information about the module where the error occurred */
1704 struct ly_err_item *e = ly_err_last(ctx);
1705 if (e && (!e->path || !strncmp(e->path, "Line ", ly_strlen_const("Line ")))) {
1706 LOGERR(ctx, ret, "Parsing module \"%s\" failed.", mod->name);
1707 }
1708 }
1709 }
1710 if (!module_created) {
1711 lys_module_free(mod);
Michal Vasko0e02e8e2021-02-26 15:01:55 +01001712 mod = mod_dup;
1713 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001714
Michal Vasko7a0b0762020-09-02 16:37:01 +02001715 if (format == LYS_IN_YANG) {
1716 yang_parser_ctx_free(yangctx);
1717 } else {
1718 yin_parser_ctx_free(yinctx);
1719 }
1720
Michal Vasko405cc9e2020-12-01 12:01:27 +01001721 if (!ret && module) {
1722 *module = mod;
1723 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001724 return ret;
1725}
1726
Radek Krejci545b4872020-11-15 10:15:12 +01001727static LYS_INFORMAT
1728lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1729{
1730 if (!format && (in->type == LY_IN_FILEPATH)) {
1731 /* unknown format - try to detect it from filename's suffix */
1732 const char *path = in->method.fpath.filepath;
1733 size_t len = strlen(path);
1734
1735 /* ignore trailing whitespaces */
1736 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1737
Radek Krejcif13b87b2020-12-01 22:02:17 +01001738 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1739 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001740 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001741 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1742 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001743 format = LYS_IN_YIN;
1744 } /* else still unknown */
1745 }
1746
1747 return format;
1748}
1749
Michal Vasko7a0b0762020-09-02 16:37:01 +02001750API LY_ERR
Michal Vasko4de7d072021-07-09 09:13:18 +02001751lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, struct lys_module **module)
Michal Vasko7a0b0762020-09-02 16:37:01 +02001752{
Michal Vasko65333882021-06-10 14:12:16 +02001753 LY_ERR ret = LY_SUCCESS;
Michal Vasko4e205e82021-06-08 14:01:47 +02001754 struct lys_module *mod;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001755
Michal Vasko7a0b0762020-09-02 16:37:01 +02001756 if (module) {
1757 *module = NULL;
1758 }
Radek Krejci545b4872020-11-15 10:15:12 +01001759 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1760
1761 format = lys_parse_get_format(in, format);
1762 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001763
1764 /* remember input position */
1765 in->func_start = in->current;
1766
Michal Vasko4e205e82021-06-08 14:01:47 +02001767 /* parse */
Michal Vasko22b26222021-07-30 11:16:47 +02001768 ret = lys_parse_in(ctx, in, format, NULL, NULL, &ctx->unres.creating, &mod);
Michal Vasko4e205e82021-06-08 14:01:47 +02001769 LY_CHECK_GOTO(ret, cleanup);
1770
1771 /* implement */
Michal Vasko22b26222021-07-30 11:16:47 +02001772 ret = _lys_set_implemented(mod, features, &ctx->unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001773 LY_CHECK_GOTO(ret, cleanup);
1774
Michal Vaskof4258e12021-06-15 12:11:42 +02001775 if (!(ctx->flags & LY_CTX_EXPLICIT_COMPILE)) {
1776 /* create dep set for the module and mark all the modules that will be (re)compiled */
Michal Vasko22b26222021-07-30 11:16:47 +02001777 LY_CHECK_GOTO(ret = lys_unres_dep_sets_create(ctx, &ctx->unres.dep_sets, mod), cleanup);
Michal Vaskof4258e12021-06-15 12:11:42 +02001778
Michal Vasko709f9a52021-07-21 10:51:59 +02001779 /* (re)compile the whole dep set (other dep sets will have no modules marked for compilation) */
Michal Vasko22b26222021-07-30 11:16:47 +02001780 LY_CHECK_GOTO(ret = lys_compile_depset_all(ctx, &ctx->unres), cleanup);
1781
1782 /* unres resolved */
1783 lys_unres_glob_erase(&ctx->unres);
Michal Vaskof4258e12021-06-15 12:11:42 +02001784 }
1785
Michal Vasko405cc9e2020-12-01 12:01:27 +01001786cleanup:
1787 if (ret) {
Michal Vasko22b26222021-07-30 11:16:47 +02001788 lys_unres_glob_revert(ctx, &ctx->unres);
1789 lys_unres_glob_erase(&ctx->unres);
Michal Vasko87f1cf02021-06-08 14:02:47 +02001790 } else if (module) {
1791 *module = mod;
Michal Vasko405cc9e2020-12-01 12:01:27 +01001792 }
Michal Vasko405cc9e2020-12-01 12:01:27 +01001793 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001794}
1795
Michal Vasko3a41dff2020-07-15 14:30:28 +02001796API LY_ERR
Michal Vasko4de7d072021-07-09 09:13:18 +02001797lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001798{
Radek Krejci0f969882020-08-21 16:56:47 +02001799 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001800 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001801
Michal Vasko3a41dff2020-07-15 14:30:28 +02001802 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001803
Michal Vasko3a41dff2020-07-15 14:30:28 +02001804 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 +02001805
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001806 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001807 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001808
Michal Vasko3a41dff2020-07-15 14:30:28 +02001809 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001810}
1811
Michal Vasko3a41dff2020-07-15 14:30:28 +02001812API LY_ERR
Michal Vasko4de7d072021-07-09 09:13:18 +02001813lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001814{
Radek Krejci0f969882020-08-21 16:56:47 +02001815 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001816 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001817
Michal Vasko3a41dff2020-07-15 14:30:28 +02001818 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001819
Michal Vasko3a41dff2020-07-15 14:30:28 +02001820 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 +02001821
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001822 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001823 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001824
Michal Vasko3a41dff2020-07-15 14:30:28 +02001825 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001826}
1827
Michal Vasko3a41dff2020-07-15 14:30:28 +02001828API LY_ERR
Michal Vasko4de7d072021-07-09 09:13:18 +02001829lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001830{
Radek Krejci0f969882020-08-21 16:56:47 +02001831 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001832 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001833
Michal Vasko3a41dff2020-07-15 14:30:28 +02001834 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001835
Michal Vasko3a41dff2020-07-15 14:30:28 +02001836 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001837 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001838
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001839 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001840 ly_in_free(in, 0);
1841
Michal Vasko3a41dff2020-07-15 14:30:28 +02001842 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001843}
1844
1845API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001846lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001847 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001848{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001849 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001850 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001851 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001852 char *wd, *wn = NULL;
1853 DIR *dir = NULL;
1854 struct dirent *file;
1855 char *match_name = NULL;
1856 LYS_INFORMAT format_aux, match_format = 0;
1857 struct ly_set *dirs;
1858 struct stat st;
1859
1860 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1861
1862 /* start to fill the dir fifo with the context's search path (if set)
1863 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001864 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001865
1866 len = strlen(name);
1867 if (cwd) {
1868 wd = get_current_dir_name();
1869 if (!wd) {
1870 LOGMEM(NULL);
1871 goto cleanup;
1872 } else {
1873 /* add implicit current working directory (./) to be searched,
1874 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001875 ret = ly_set_add(dirs, wd, 0, NULL);
1876 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001877 implicit_cwd = 1;
1878 }
1879 }
1880 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001881 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001882 /* check for duplicities with the implicit current working directory */
1883 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1884 implicit_cwd = 0;
1885 continue;
1886 }
1887 wd = strdup(searchpaths[i]);
1888 if (!wd) {
1889 LOGMEM(NULL);
1890 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001891 } else {
1892 ret = ly_set_add(dirs, wd, 0, NULL);
1893 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001894 }
1895 }
1896 }
1897 wd = NULL;
1898
1899 /* start searching */
1900 while (dirs->count) {
1901 free(wd);
1902 free(wn); wn = NULL;
1903
1904 dirs->count--;
1905 wd = (char *)dirs->objs[dirs->count];
1906 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001907 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001908
1909 if (dir) {
1910 closedir(dir);
1911 }
1912 dir = opendir(wd);
1913 dir_len = strlen(wd);
1914 if (!dir) {
1915 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1916 } else {
1917 while ((file = readdir(dir))) {
1918 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1919 /* skip . and .. */
1920 continue;
1921 }
1922 free(wn);
1923 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1924 LOGMEM(NULL);
1925 goto cleanup;
1926 }
1927 if (stat(wn, &st) == -1) {
1928 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001929 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001930 continue;
1931 }
1932 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1933 /* we have another subdirectory in searchpath to explore,
1934 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001935 ret = ly_set_add(dirs, wn, 0, NULL);
1936 LY_CHECK_GOTO(ret, cleanup);
1937
Radek Krejcid33273d2018-10-25 14:55:52 +02001938 /* continue with the next item in current directory */
1939 wn = NULL;
1940 continue;
1941 } else if (!S_ISREG(st.st_mode)) {
1942 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1943 continue;
1944 }
1945
1946 /* here we know that the item is a file which can contain a module */
1947 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001948 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001949 /* different filename than the module we search for */
1950 continue;
1951 }
1952
1953 /* get type according to filename suffix */
1954 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001955 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1956 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001957 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001958 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1959 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001960 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001961 } else {
1962 /* not supportde suffix/file format */
1963 continue;
1964 }
1965
1966 if (revision) {
1967 /* we look for the specific revision, try to get it from the filename */
1968 if (file->d_name[len] == '@') {
1969 /* check revision from the filename */
1970 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1971 /* another revision */
1972 continue;
1973 } else {
1974 /* exact revision */
1975 free(match_name);
1976 match_name = wn;
1977 wn = NULL;
1978 match_len = dir_len + 1 + len;
1979 match_format = format_aux;
1980 goto success;
1981 }
1982 } else {
1983 /* continue trying to find exact revision match, use this only if not found */
1984 free(match_name);
1985 match_name = wn;
1986 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001987 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001988 match_format = format_aux;
1989 continue;
1990 }
1991 } else {
1992 /* remember the revision and try to find the newest one */
1993 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001994 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001995 lysp_check_date(NULL, &file->d_name[len + 1],
1996 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001997 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001998 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001999 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
2000 continue;
2001 }
2002 free(match_name);
2003 }
2004
2005 match_name = wn;
2006 wn = NULL;
2007 match_len = dir_len + 1 + len;
2008 match_format = format_aux;
2009 continue;
2010 }
2011 }
2012 }
2013 }
2014
2015success:
2016 (*localfile) = match_name;
2017 match_name = NULL;
2018 if (format) {
2019 (*format) = match_format;
2020 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02002021 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02002022
2023cleanup:
2024 free(wn);
2025 free(wd);
2026 if (dir) {
2027 closedir(dir);
2028 }
2029 free(match_name);
2030 ly_set_free(dirs, free);
2031
2032 return ret;
2033}