blob: 4918644109cd3e925cfd1c77dccf822c4e8cf345 [file] [log] [blame]
Radek Krejci3f5e3db2018-10-11 15:57:47 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
Radek Krejcif8dc59a2020-11-25 13:47:44 +010016#define _POSIX_C_SOURCE 200809L /* strdup */
Radek Krejci535ea9f2020-05-29 16:01:05 +020017
Radek Krejcica376bd2020-06-11 16:04:06 +020018#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020019
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <assert.h>
Radek Krejci545b4872020-11-15 10:15:12 +010021#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020022#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020025#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdlib.h>
27#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020029#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020030
Radek Krejcica376bd2020-06-11 16:04:06 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020036#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010037#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020038#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020039#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020040#include "path.h"
Radek Krejci6b88a462021-02-17 12:39:34 +010041#include "plugins_exts.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "schema_compile.h"
43#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010044#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020045#include "set.h"
46#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010047#include "tree_data.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020048#include "tree_schema_internal.h"
49#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020050
Radek Krejcieccf6602021-02-05 19:42:54 +010051/**
52 * @brief information about YANG statements
53 */
54struct stmt_info_s stmt_attr_info[] = {
Radek Krejci6b88a462021-02-17 12:39:34 +010055 [LY_STMT_NONE] = {NULL, NULL, 0},
56 [LY_STMT_ACTION] = {"action", "name", STMT_FLAG_ID},
57 [LY_STMT_ANYDATA] = {"anydata", "name", STMT_FLAG_ID},
58 [LY_STMT_ANYXML] = {"anyxml", "name", STMT_FLAG_ID},
59 [LY_STMT_ARGUMENT] = {"argument", "name", STMT_FLAG_ID},
60 [LY_STMT_ARG_TEXT] = {"text", NULL, 0},
61 [LY_STMT_ARG_VALUE] = {"value", NULL, 0},
62 [LY_STMT_AUGMENT] = {"augment", "target-node", STMT_FLAG_ID},
63 [LY_STMT_BASE] = {"base", "name", STMT_FLAG_ID},
64 [LY_STMT_BELONGS_TO] = {"belongs-to", "module", STMT_FLAG_ID},
65 [LY_STMT_BIT] = {"bit", "name", STMT_FLAG_ID},
66 [LY_STMT_CASE] = {"case", "name", STMT_FLAG_ID},
67 [LY_STMT_CHOICE] = {"choice", "name", STMT_FLAG_ID},
68 [LY_STMT_CONFIG] = {"config", "value", STMT_FLAG_ID},
69 [LY_STMT_CONTACT] = {"contact", "text", STMT_FLAG_YIN},
70 [LY_STMT_CONTAINER] = {"container", "name", STMT_FLAG_ID},
71 [LY_STMT_DEFAULT] = {"default", "value", 0},
72 [LY_STMT_DESCRIPTION] = {"description", "text", STMT_FLAG_YIN},
73 [LY_STMT_DEVIATE] = {"deviate", "value", STMT_FLAG_ID},
74 [LY_STMT_DEVIATION] = {"deviation", "target-node", STMT_FLAG_ID},
75 [LY_STMT_ENUM] = {"enum", "name", STMT_FLAG_ID},
76 [LY_STMT_ERROR_APP_TAG] = {"error-app-tag", "value", 0},
77 [LY_STMT_ERROR_MESSAGE] = {"error-message", "value", STMT_FLAG_YIN},
78 [LY_STMT_EXTENSION] = {"extension", "name", STMT_FLAG_ID},
79 [LY_STMT_EXTENSION_INSTANCE] = {NULL, NULL, 0},
80 [LY_STMT_FEATURE] = {"feature", "name", STMT_FLAG_ID},
81 [LY_STMT_FRACTION_DIGITS] = {"fraction-digits", "value", STMT_FLAG_ID},
82 [LY_STMT_GROUPING] = {"grouping", "name", STMT_FLAG_ID},
83 [LY_STMT_IDENTITY] = {"identity", "name", STMT_FLAG_ID},
84 [LY_STMT_IF_FEATURE] = {"if-feature", "name", 0},
85 [LY_STMT_IMPORT] = {"import", "module", STMT_FLAG_ID},
86 [LY_STMT_INCLUDE] = {"include", "module", STMT_FLAG_ID},
87 [LY_STMT_INPUT] = {"input", NULL, 0},
88 [LY_STMT_KEY] = {"key", "value", 0},
89 [LY_STMT_LEAF] = {"leaf", "name", STMT_FLAG_ID},
90 [LY_STMT_LEAF_LIST] = {"leaf-list", "name", STMT_FLAG_ID},
91 [LY_STMT_LENGTH] = {"length", "value", 0},
92 [LY_STMT_LIST] = {"list", "name", STMT_FLAG_ID},
93 [LY_STMT_MANDATORY] = {"mandatory", "value", STMT_FLAG_ID},
94 [LY_STMT_MAX_ELEMENTS] = {"max-elements", "value", STMT_FLAG_ID},
95 [LY_STMT_MIN_ELEMENTS] = {"min-elements", "value", STMT_FLAG_ID},
96 [LY_STMT_MODIFIER] = {"modifier", "value", STMT_FLAG_ID},
97 [LY_STMT_MODULE] = {"module", "name", STMT_FLAG_ID},
98 [LY_STMT_MUST] = {"must", "condition", 0},
99 [LY_STMT_NAMESPACE] = {"namespace", "uri", 0},
100 [LY_STMT_NOTIFICATION] = {"notification", "name", STMT_FLAG_ID},
101 [LY_STMT_ORDERED_BY] = {"ordered-by", "value", STMT_FLAG_ID},
102 [LY_STMT_ORGANIZATION] = {"organization", "text", STMT_FLAG_YIN},
103 [LY_STMT_OUTPUT] = {"output", NULL, 0},
104 [LY_STMT_PATH] = {"path", "value", 0},
105 [LY_STMT_PATTERN] = {"pattern", "value", 0},
106 [LY_STMT_POSITION] = {"position", "value", STMT_FLAG_ID},
107 [LY_STMT_PREFIX] = {"prefix", "value", STMT_FLAG_ID},
108 [LY_STMT_PRESENCE] = {"presence", "value", 0},
109 [LY_STMT_RANGE] = {"range", "value", 0},
110 [LY_STMT_REFERENCE] = {"reference", "text", STMT_FLAG_YIN},
111 [LY_STMT_REFINE] = {"refine", "target-node", STMT_FLAG_ID},
112 [LY_STMT_REQUIRE_INSTANCE] = {"require-instance", "value", STMT_FLAG_ID},
113 [LY_STMT_REVISION] = {"revision", "date", STMT_FLAG_ID},
114 [LY_STMT_REVISION_DATE] = {"revision-date", "date", STMT_FLAG_ID},
115 [LY_STMT_RPC] = {"rpc", "name", STMT_FLAG_ID},
116 [LY_STMT_STATUS] = {"status", "value", STMT_FLAG_ID},
117 [LY_STMT_SUBMODULE] = {"submodule", "name", STMT_FLAG_ID},
118 [LY_STMT_SYNTAX_LEFT_BRACE] = {"{", NULL, 0},
119 [LY_STMT_SYNTAX_RIGHT_BRACE] = {"}", NULL, 0},
120 [LY_STMT_SYNTAX_SEMICOLON] = {";", NULL, 0},
121 [LY_STMT_TYPE] = {"type", "name", STMT_FLAG_ID},
122 [LY_STMT_TYPEDEF] = {"typedef", "name", STMT_FLAG_ID},
123 [LY_STMT_UNIQUE] = {"unique", "tag", 0},
124 [LY_STMT_UNITS] = {"units", "name", 0},
125 [LY_STMT_USES] = {"uses", "name", STMT_FLAG_ID},
126 [LY_STMT_VALUE] = {"value", "value", STMT_FLAG_ID},
127 [LY_STMT_WHEN] = {"when", "condition", 0},
128 [LY_STMT_YANG_VERSION] = {"yang-version", "value", STMT_FLAG_ID},
129 [LY_STMT_YIN_ELEMENT] = {"yin-element", "value", STMT_FLAG_ID},
Radek Krejcieccf6602021-02-05 19:42:54 +0100130};
131
Radek Krejcif8ca8192021-03-02 16:50:06 +0100132API const char *
133ly_stmt2str(enum ly_stmt stmt)
134{
135 return stmt_attr_info[stmt].name;
136}
137
Radek Krejcieccf6602021-02-05 19:42:54 +0100138const char * const ly_devmod_list[] = {
139 [LYS_DEV_NOT_SUPPORTED] = "not-supported",
140 [LYS_DEV_ADD] = "add",
141 [LYS_DEV_DELETE] = "delete",
142 [LYS_DEV_REPLACE] = "replace",
143};
144
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200145API LY_ERR
146lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
147{
Michal Vasko1d972ca2020-11-03 17:16:56 +0100148 struct lysc_node *elem, *elem2;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100149 const struct lysc_node_action *action;
150 const struct lysc_node_notif *notif;
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200151
152 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
153
154 LYSC_TREE_DFS_BEGIN(root, elem) {
155 /* schema node */
156 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
157
Radek Krejci2a9fc652021-01-22 17:44:34 +0100158 LY_LIST_FOR(lysc_node_actions(elem), action) {
159 LYSC_TREE_DFS_BEGIN(action, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200160 /* action subtree */
161 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
162
Radek Krejci2a9fc652021-01-22 17:44:34 +0100163 LYSC_TREE_DFS_END(action, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200164 }
165 }
166
Radek Krejci2a9fc652021-01-22 17:44:34 +0100167 LY_LIST_FOR(lysc_node_notifs(elem), notif) {
168 LYSC_TREE_DFS_BEGIN(notif, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200169 /* notification subtree */
170 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
171
Radek Krejci2a9fc652021-01-22 17:44:34 +0100172 LYSC_TREE_DFS_END(notif, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200173 }
174 }
175
176 LYSC_TREE_DFS_END(root, elem);
177 }
178
179 return LY_SUCCESS;
180}
181
182API LY_ERR
183lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
184{
Michal Vasko2336cf52020-11-03 17:18:15 +0100185 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200186
187 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
188
189 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +0100190 LY_LIST_FOR(mod->compiled->data, root) {
191 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
192 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200193
194 /* RPCs */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100195 LY_LIST_FOR((const struct lysc_node *)mod->compiled->rpcs, root) {
196 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200197 }
198
199 /* notifications */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100200 LY_LIST_FOR((const struct lysc_node *)mod->compiled->notifs, root) {
201 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200202 }
203
204 return LY_SUCCESS;
205}
206
Radek Krejcib93bd412020-11-02 13:23:11 +0100207static void
208lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
209{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100210 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100211 if (first_case->child) {
212 /* there is something to return */
213 (*next) = first_case->child;
214 return;
215 }
216 }
217
218 /* no children in choice's cases, so go to the choice's sibling instead of into it */
219 (*last) = (*next);
220 (*next) = (*next)->next;
221}
222
Radek Krejci035dacf2021-02-12 18:25:53 +0100223/**
224 * @brief Generic getnext function for ::lys_getnext() and ::lys_getnext_ext().
225 *
226 * Gets next schema tree (sibling) node element that can be instantiated in a data tree. Returned node can
227 * be from an augment. If the @p ext is provided, the function is locked inside the schema tree defined in the
228 * extension instance.
229 *
230 * ::lys_getnext_() is supposed to be called sequentially. In the first call, the \p last parameter is usually NULL
231 * and function starts returning i) the first \p parent's child or ii) the first top level element specified in the
232 * given extension (if provided) or iii) the first top level element of the \p module.
233 * Consequent calls suppose to provide the previously returned node as the \p last parameter and still the same
234 * \p parent and \p module parameters.
235 *
236 * Without options, the function is used to traverse only the schema nodes that can be paired with corresponding
237 * data nodes in a data tree. By setting some \p options the behavior can be modified to the extent that
238 * all the schema nodes are iteratively returned.
239 *
240 * @param[in] last Previously returned schema tree node, or NULL in case of the first call.
241 * @param[in] parent Parent of the subtree where the function starts processing.
242 * @param[in] module In case of iterating on top level elements, the \p parent is NULL and
243 * module must be specified.
244 * @param[in] ext The extension instance to provide a separate schema tree. To consider the top level elements in the tree,
245 * the \p parent must be NULL. Anyway, at least one of @p parent, @p module and @p ext parameters must be specified.
246 * @param[in] options [ORed options](@ref sgetnextflags).
247 * @return Next schema tree node that can be instantiated in a data tree, NULL in case there is no such element.
248 */
249static const struct lysc_node *
250lys_getnext_(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module,
251 const struct lysc_ext_instance *ext, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100252{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100253 const struct lysc_node *next = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200254 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci035dacf2021-02-12 18:25:53 +0100255 struct lysc_node **data_p = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100256
Radek Krejci035dacf2021-02-12 18:25:53 +0100257 LY_CHECK_ARG_RET(NULL, parent || module || ext, NULL);
Radek Krejcia3045382018-11-22 14:30:31 +0100258
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200259next:
Radek Krejcia3045382018-11-22 14:30:31 +0100260 if (!last) {
261 /* first call */
262
263 /* get know where to start */
264 if (parent) {
265 /* schema subtree */
Michal Vasko544e58a2021-01-28 14:33:41 +0100266 next = last = lysc_node_child(parent);
Radek Krejcia3045382018-11-22 14:30:31 +0100267 } else {
268 /* top level data */
Radek Krejci035dacf2021-02-12 18:25:53 +0100269 if (ext) {
270 lysc_ext_substmt(ext, LY_STMT_CONTAINER /* matches all nodes */, (void **)&data_p, NULL);
271 next = last = data_p ? *data_p : NULL;
272 } else {
273 next = last = module->data;
274 }
Radek Krejcia3045382018-11-22 14:30:31 +0100275 }
276 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100277 /* try to get action or notification */
278 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100279 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100280 /* test if the next can be returned */
281 goto check;
282
Michal Vasko1bf09392020-03-27 12:38:10 +0100283 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100284 action_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100285 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100286 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100287 action_flag = notif_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100288 next = last->next;
Michal Vasko20424b42020-08-31 12:29:38 +0200289 } else {
290 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100291 }
292
Radek Krejcia3045382018-11-22 14:30:31 +0100293repeat:
294 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100295 /* possibly go back to parent */
Radek Krejci035dacf2021-02-12 18:25:53 +0100296 data_p = NULL;
Michal Vasko69730152020-10-09 16:30:07 +0200297 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100298 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200299 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100300 } else if (!action_flag) {
301 action_flag = 1;
Radek Krejci035dacf2021-02-12 18:25:53 +0100302 if (ext) {
303 lysc_ext_substmt(ext, LY_STMT_RPC /* matches also actions */, (void **)&data_p, NULL);
304 next = data_p ? *data_p : NULL;
305 } else if (parent) {
306 next = (struct lysc_node *)lysc_node_actions(parent);
307 } else {
308 next = (struct lysc_node *)module->rpcs;
309 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100310 } else if (!notif_flag) {
311 notif_flag = 1;
Radek Krejci035dacf2021-02-12 18:25:53 +0100312 if (ext) {
313 lysc_ext_substmt(ext, LY_STMT_NOTIFICATION, (void **)&data_p, NULL);
314 next = data_p ? *data_p : NULL;
315 } else if (parent) {
316 next = (struct lysc_node *)lysc_node_notifs(parent);
317 } else {
318 next = (struct lysc_node *)module->notifs;
319 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100320 } else {
321 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100322 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100323 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100324 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100325check:
Radek Krejcia3045382018-11-22 14:30:31 +0100326 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100327 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100328 case LYS_ACTION:
329 case LYS_NOTIF:
330 case LYS_LEAF:
331 case LYS_ANYXML:
332 case LYS_ANYDATA:
333 case LYS_LIST:
334 case LYS_LEAFLIST:
335 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200336 case LYS_CASE:
337 if (options & LYS_GETNEXT_WITHCASE) {
338 break;
339 } else {
340 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100341 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200342 }
343 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100344 case LYS_CONTAINER:
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100345 if (!(next->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100346 if (lysc_node_child(next)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100347 /* go into */
Michal Vasko544e58a2021-01-28 14:33:41 +0100348 next = lysc_node_child(next);
Radek Krejcia3045382018-11-22 14:30:31 +0100349 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100350 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100351 next = next->next;
352 }
353 goto repeat;
354 }
355 break;
356 case LYS_CHOICE:
357 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200358 break;
Michal Vasko544e58a2021-01-28 14:33:41 +0100359 } else if ((options & LYS_GETNEXT_NOCHOICE) || !lysc_node_child(next)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100360 next = next->next;
361 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100362 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100363 next = lysc_node_child(next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100364 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100365 /* go into */
366 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100367 }
Radek Krejcia3045382018-11-22 14:30:31 +0100368 }
369 goto repeat;
Michal Vasko544e58a2021-01-28 14:33:41 +0100370 case LYS_INPUT:
371 if (options & LYS_GETNEXT_OUTPUT) {
372 /* skip */
373 next = next->next;
374 } else {
375 /* go into */
376 next = lysc_node_child(next);
377 }
378 goto repeat;
379 case LYS_OUTPUT:
380 if (!(options & LYS_GETNEXT_OUTPUT)) {
381 /* skip */
382 next = next->next;
383 } else {
384 /* go into */
385 next = lysc_node_child(next);
386 }
387 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100388 default:
389 /* we should not be here */
Radek Krejci035dacf2021-02-12 18:25:53 +0100390 LOGINT(module ? module->mod->ctx : parent ? parent->module->ctx : ext->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100391 return NULL;
392 }
393
Radek Krejcia3045382018-11-22 14:30:31 +0100394 return next;
395}
396
397API const struct lysc_node *
Radek Krejci035dacf2021-02-12 18:25:53 +0100398lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
399{
400 return lys_getnext_(last, parent, module, NULL, options);
401}
402
403API const struct lysc_node *
404lys_getnext_ext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_ext_instance *ext, uint32_t options)
405{
406 return lys_getnext_(last, parent, NULL, ext, options);
407}
408
Radek Krejcif16e2542021-02-17 15:39:23 +0100409const struct lysc_node *
410lys_find_ext_instance_node(const struct lysc_ext_instance *ext, const struct lys_module *module, const char *name, size_t name_len,
411 uint16_t nodetype, uint32_t options)
412{
413 const struct lysc_node *node = NULL;
414
415 LY_CHECK_ARG_RET(NULL, ext, name, NULL);
416 if (!nodetype) {
417 nodetype = LYS_NODETYPE_MASK;
418 }
419
420 if (module && (module != ext->module)) {
421 return NULL;
422 }
423
424 while ((node = lys_getnext_ext(node, NULL, ext, options))) {
425 if (!(node->nodetype & nodetype)) {
426 continue;
427 }
428
429 if (name_len) {
430 if (!ly_strncmp(node->name, name, name_len)) {
431 return node;
432 }
433 } else {
434 if (!strcmp(node->name, name)) {
435 return node;
436 }
437 }
438 }
439 return NULL;
440}
441
Radek Krejci035dacf2021-02-12 18:25:53 +0100442API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100443lys_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 +0200444 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100445{
446 const struct lysc_node *node = NULL;
447
448 LY_CHECK_ARG_RET(NULL, module, name, NULL);
449 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100450 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100451 }
452
453 while ((node = lys_getnext(node, parent, module->compiled, options))) {
454 if (!(node->nodetype & nodetype)) {
455 continue;
456 }
457 if (node->module != module) {
458 continue;
459 }
460
461 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200462 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100463 return node;
464 }
465 } else {
466 if (!strcmp(node->name, name)) {
467 return node;
468 }
469 }
470 }
471 return NULL;
472}
473
Michal Vasko519fd602020-05-26 12:17:39 +0200474API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100475lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
476 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200477{
478 LY_ERR ret = LY_SUCCESS;
479 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200480 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200481 uint32_t i;
482
Michal Vasko26512682021-01-11 11:35:40 +0100483 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200484 if (!(options & LYXP_SCNODE_ALL)) {
485 options = LYXP_SCNODE;
486 }
Michal Vasko26512682021-01-11 11:35:40 +0100487 if (!ctx) {
488 ctx = ctx_node->module->ctx;
489 }
Michal Vasko519fd602020-05-26 12:17:39 +0200490
491 memset(&xp_set, 0, sizeof xp_set);
492
493 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100494 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200495 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200496
497 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100498 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200499 LY_CHECK_GOTO(ret, cleanup);
500
501 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200502 ret = ly_set_new(set);
503 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200504
505 /* transform into ly_set */
506 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100507 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200508 (*set)->size = xp_set.used;
509
510 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200511 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200512 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200513 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200514 }
515 }
516
517cleanup:
518 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100519 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200520 return ret;
521}
522
Michal Vasko072de482020-08-05 13:27:21 +0200523API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200524lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
525 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
526{
527 LY_ERR ret = LY_SUCCESS;
528 struct lyxp_set xp_set = {0};
529 uint32_t i;
530
531 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
532 if (!(options & LYXP_SCNODE_ALL)) {
533 options = LYXP_SCNODE;
534 }
535
536 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100537 ret = lyxp_atomize(cur_mod->ctx, expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
Michal Vasko40308e72020-10-20 16:38:40 +0200538 LY_CHECK_GOTO(ret, cleanup);
539
540 /* allocate return set */
541 ret = ly_set_new(set);
542 LY_CHECK_GOTO(ret, cleanup);
543
544 /* transform into ly_set */
545 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
546 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
547 (*set)->size = xp_set.used;
548
549 for (i = 0; i < xp_set.used; ++i) {
Michal Vaskod97959c2020-12-10 12:18:28 +0100550 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM)) {
551 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM) ||
552 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200553 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
554 LY_CHECK_GOTO(ret, cleanup);
555 }
556 }
557
558cleanup:
559 lyxp_set_free_content(&xp_set);
560 if (ret) {
561 ly_set_free(*set, NULL);
562 *set = NULL;
563 }
564 return ret;
565}
566
567API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100568lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
569 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200570{
571 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200572 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200573 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200574 uint32_t i;
575
Michal Vasko26512682021-01-11 11:35:40 +0100576 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200577 if (!(options & LYXP_SCNODE_ALL)) {
578 options = LYXP_SCNODE;
579 }
Michal Vasko26512682021-01-11 11:35:40 +0100580 if (!ctx) {
581 ctx = ctx_node->module->ctx;
582 }
Michal Vasko072de482020-08-05 13:27:21 +0200583
Michal Vasko072de482020-08-05 13:27:21 +0200584 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100585 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200586 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200587
588 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100589 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200590 LY_CHECK_GOTO(ret, cleanup);
591
592 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200593 ret = ly_set_new(set);
594 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200595
596 /* transform into ly_set */
597 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100598 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200599 (*set)->size = xp_set.used;
600
601 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100602 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 +0200603 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200604 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200605 }
606 }
607
608cleanup:
609 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100610 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200611 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200612 ly_set_free(*set, NULL);
613 *set = NULL;
614 }
Michal Vasko072de482020-08-05 13:27:21 +0200615 return ret;
616}
617
Radek Krejcibc5644c2020-10-27 14:53:17 +0100618API LY_ERR
619lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
620{
621 LY_ERR ret = LY_SUCCESS;
622 LY_ARRAY_COUNT_TYPE u, v;
623
624 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
625
626 /* allocate return set */
627 LY_CHECK_RET(ly_set_new(set));
628
629 LY_ARRAY_FOR(path, u) {
630 /* add nodes from the path */
631 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
632 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
633 LY_ARRAY_FOR(path[u].predicates, v) {
634 /* add all the keys in a predicate */
635 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
636 }
637 }
638 }
639
640cleanup:
641 if (ret) {
642 ly_set_free(*set, NULL);
643 *set = NULL;
644 }
645 return ret;
646}
647
648API LY_ERR
649lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
650 struct ly_set **set)
651{
652 LY_ERR ret = LY_SUCCESS;
653 uint8_t oper;
654 struct lyxp_expr *expr = NULL;
655 struct ly_path *p = NULL;
656
657 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
658
659 if (!ctx) {
660 ctx = ctx_node->module->ctx;
661 }
662
663 /* parse */
664 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
665 LY_CHECK_GOTO(ret, cleanup);
666
667 /* compile */
668 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
669 ret = ly_path_compile(ctx, NULL, ctx_node, expr, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100670 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100671 LY_CHECK_GOTO(ret, cleanup);
672
673 /* resolve */
674 ret = lys_find_lypath_atoms(p, set);
675
676cleanup:
677 ly_path_free(ctx, p);
678 lyxp_expr_free(ctx, expr);
679 return ret;
680}
681
682API const struct lysc_node *
683lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
684{
685 const struct lysc_node *snode = NULL;
686 struct lyxp_expr *exp = NULL;
687 struct ly_path *p = NULL;
688 LY_ERR ret;
689 uint8_t oper;
690
691 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
692
693 if (!ctx) {
694 ctx = ctx_node->module->ctx;
695 }
696
697 /* parse */
698 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
699 LY_CHECK_GOTO(ret, cleanup);
700
701 /* compile */
702 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
703 ret = ly_path_compile(ctx, NULL, ctx_node, exp, LY_PATH_LREF_FALSE, oper, LY_PATH_TARGET_MANY,
Michal Vasko405cc9e2020-12-01 12:01:27 +0100704 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100705 LY_CHECK_GOTO(ret, cleanup);
706
707 /* get last node */
708 snode = p[LY_ARRAY_COUNT(p) - 1].node;
709
710cleanup:
711 ly_path_free(ctx, p);
712 lyxp_expr_free(ctx, exp);
713 return snode;
714}
715
Michal Vasko14654712020-02-06 08:35:21 +0100716char *
717lysc_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 +0200718 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200719{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200720 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200721 char *path = NULL;
722 int len = 0;
723
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200724 if (buffer) {
725 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100726 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200727 }
728
Radek Krejci327de162019-06-14 12:52:07 +0200729 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200730 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200731 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100732 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200733 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100734 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200735
Michal Vasko721b6f62021-02-08 08:52:53 +0100736 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko65de0402020-08-03 16:34:19 +0200737 /* schema-only node */
738 continue;
739 }
740
Michal Vasko11deea12020-08-05 13:54:50 +0200741 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200742 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100743 if (parent && (iter->parent == parent)) {
744 slash = "";
745 } else {
746 slash = "/";
747 }
Michal Vasko69730152020-10-09 16:30:07 +0200748 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200749 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200750 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100751 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200752 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100753 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200754 }
Radek Krejci327de162019-06-14 12:52:07 +0200755 } else {
756 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200757 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100758 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200759 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100760 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200761 }
Radek Krejci327de162019-06-14 12:52:07 +0200762 }
763 free(s);
764 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200765
Michal Vasko69730152020-10-09 16:30:07 +0200766 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200767 /* not enough space in buffer */
768 break;
769 }
Radek Krejci327de162019-06-14 12:52:07 +0200770 }
771
772 if (len < 0) {
773 free(path);
774 path = NULL;
775 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200776 if (buffer) {
777 strcpy(buffer, "/");
778 } else {
779 path = strdup("/");
780 }
Radek Krejci327de162019-06-14 12:52:07 +0200781 }
782 break;
783 }
784
Radek Krejci1c0c3442019-07-23 16:08:47 +0200785 if (buffer) {
786 return buffer;
787 } else {
788 return path;
789 }
Radek Krejci327de162019-06-14 12:52:07 +0200790}
791
Michal Vasko14654712020-02-06 08:35:21 +0100792API char *
793lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
794{
795 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
796}
797
Michal Vasko405cc9e2020-12-01 12:01:27 +0100798LY_ERR
799lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200800{
801 struct lys_module *m;
802
Michal Vasko405cc9e2020-12-01 12:01:27 +0100803 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200804
805 /* we have module from the current context */
806 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
807 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200808 assert(m != mod);
809
810 /* check collision with other implemented revision */
811 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
812 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
813 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200814 }
815
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100816 /* enable features */
817 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
818
Michal Vasko89b5c072020-10-06 13:52:44 +0200819 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100820 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200821
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200822 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200823 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200824
825 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100826 return lys_compile(mod, 0, unres);
827}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200828
Michal Vasko405cc9e2020-12-01 12:01:27 +0100829API LY_ERR
830lys_set_implemented(struct lys_module *mod, const char **features)
831{
832 LY_ERR ret = LY_SUCCESS, r;
833 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200834
Michal Vasko405cc9e2020-12-01 12:01:27 +0100835 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100836
Michal Vasko405cc9e2020-12-01 12:01:27 +0100837 if (mod->implemented) {
838 /* mod is already implemented, set the features */
839 r = lys_set_features(mod->parsed, features);
840 if (r == LY_EEXIST) {
841 /* no changes */
842 return LY_SUCCESS;
843 } else if (r) {
844 /* error */
845 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200846 }
847
Michal Vasko405cc9e2020-12-01 12:01:27 +0100848 /* full recompilation */
849 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200850 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100851
Michal Vasko405cc9e2020-12-01 12:01:27 +0100852 /* implement this module and any other required modules, recursively */
853 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100854
855 /* the first module being implemented is finished, resolve global unres, consolidate the set */
856 if (!ret) {
857 ret = lys_compile_unres_glob(mod->ctx, &unres);
858 }
859 if (ret) {
860 /* failure, full compile revert */
861 lys_compile_unres_glob_revert(mod->ctx, &unres);
862 }
863
864 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200865 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200866}
867
Michal Vasko7c8439f2020-08-05 13:25:19 +0200868static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100869lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200870{
871 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200872 LY_ARRAY_COUNT_TYPE u, v;
873
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100874 pmod->parsing = 1;
875 LY_ARRAY_FOR(pmod->imports, u) {
876 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200877 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100878 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
879 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200880 }
881 /* check for importing the same module twice */
882 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100883 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200884 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
885 }
886 }
887 }
Radek Krejci771928a2021-01-19 13:42:36 +0100888 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
889
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100890 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200891
892 return LY_SUCCESS;
893}
894
Michal Vasko3a41dff2020-07-15 14:30:28 +0200895LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200896lys_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 +0200897 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200898 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200899{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200900 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100901 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100902 struct lys_yang_parser_ctx *yangctx = NULL;
903 struct lys_yin_parser_ctx *yinctx = NULL;
904 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905
Michal Vasko3a41dff2020-07-15 14:30:28 +0200906 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100907
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100908 switch (format) {
909 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200910 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100911 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 break;
913 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200914 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100915 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 break;
917 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200918 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200919 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200921 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200922 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200923 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924
925 /* make sure that the newest revision is at position 0 */
926 lysp_sort_revisions(submod->revs);
927
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100928 /* decide the latest revision */
Michal Vasko8dc31992021-02-22 10:30:47 +0100929 latest_sp = (struct lysp_submodule *)ly_ctx_get_submodule2_latest(submod->mod, submod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100930 if (latest_sp) {
931 if (submod->revs) {
932 if (!latest_sp->revs) {
933 /* latest has no revision, so mod is anyway newer */
934 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200935 /* the latest_sp is zeroed later when the new module is being inserted into the context */
936 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
937 submod->latest_revision = latest_sp->latest_revision;
938 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100939 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200940 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200942 } else {
943 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100944 }
945 } else {
946 submod->latest_revision = 1;
947 }
948
Radek Krejcib3289d62019-09-18 12:21:39 +0200949 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200950 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200951 }
952
953 if (latest_sp) {
954 latest_sp->latest_revision = 0;
955 }
956
Michal Vasko7a0b0762020-09-02 16:37:01 +0200957 lys_parser_fill_filepath(ctx, in, &submod->filepath);
958
Michal Vasko7c8439f2020-08-05 13:25:19 +0200959 /* resolve imports and includes */
960 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
961
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100962 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100963 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
964 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965
David Sedlák1b623122019-08-05 15:27:49 +0200966 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100967 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200968 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100969 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200970 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200971 *submodule = submod;
972 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200973
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100974error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200975 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200976 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100977 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200978 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100979 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200980 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200981 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200982}
983
Michal Vasko45b521c2020-11-04 17:14:39 +0100984/**
985 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
986 *
987 * @param[in] mod Parsed module to add to.
988 * @return LY_SUCCESS on success.
989 * @return LY_ERR on error.
990 */
991static LY_ERR
992lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
993{
994 struct lysp_ext_instance *ext_p;
995 struct lysp_stmt *stmt;
996 struct lysp_import *imp;
997
998 /*
999 * 1) edit-config's operation
1000 */
1001 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1002 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1003 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1004 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001005 ext_p->format = LY_PREF_SCHEMA;
1006 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001007 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001008 ext_p->parent_stmt = LY_STMT_MODULE;
1009 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001010
1011 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1012 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1013 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1014 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001015 stmt->format = LY_PREF_SCHEMA;
1016 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001017 stmt->kw = LY_STMT_TYPE;
1018
1019 stmt->child = calloc(1, sizeof *stmt->child);
1020 stmt = stmt->child;
1021 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1022 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1023 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001024 stmt->format = LY_PREF_SCHEMA;
1025 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001026 stmt->kw = LY_STMT_ENUM;
1027
1028 stmt->next = calloc(1, sizeof *stmt->child);
1029 stmt = stmt->next;
1030 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1031 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1032 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001033 stmt->format = LY_PREF_SCHEMA;
1034 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001035 stmt->kw = LY_STMT_ENUM;
1036
1037 stmt->next = calloc(1, sizeof *stmt->child);
1038 stmt = stmt->next;
1039 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1040 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1041 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001042 stmt->format = LY_PREF_SCHEMA;
1043 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001044 stmt->kw = LY_STMT_ENUM;
1045
1046 stmt->next = calloc(1, sizeof *stmt->child);
1047 stmt = stmt->next;
1048 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1049 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1050 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001051 stmt->format = LY_PREF_SCHEMA;
1052 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001053 stmt->kw = LY_STMT_ENUM;
1054
1055 stmt->next = calloc(1, sizeof *stmt->child);
1056 stmt = stmt->next;
1057 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1058 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1059 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001060 stmt->format = LY_PREF_SCHEMA;
1061 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001062 stmt->kw = LY_STMT_ENUM;
1063
1064 /*
1065 * 2) filter's type
1066 */
1067 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1068 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1069 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1070 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001071 ext_p->format = LY_PREF_SCHEMA;
1072 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001073 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001074 ext_p->parent_stmt = LY_STMT_MODULE;
1075 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001076
1077 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1078 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1079 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1080 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001081 stmt->format = LY_PREF_SCHEMA;
1082 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001083 stmt->kw = LY_STMT_TYPE;
1084
1085 stmt->child = calloc(1, sizeof *stmt->child);
1086 stmt = stmt->child;
1087 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1088 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1089 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001090 stmt->format = LY_PREF_SCHEMA;
1091 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001092 stmt->kw = LY_STMT_ENUM;
1093
1094 stmt->next = calloc(1, sizeof *stmt->child);
1095 stmt = stmt->next;
1096 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1097 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1098 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001099 stmt->format = LY_PREF_SCHEMA;
1100 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001101 stmt->kw = LY_STMT_ENUM;
1102
1103 /* if-feature for enum allowed only for YANG 1.1 modules */
1104 if (mod->version >= LYS_VERSION_1_1) {
1105 stmt->child = calloc(1, sizeof *stmt->child);
1106 stmt = stmt->child;
1107 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1108 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
1109 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001110 stmt->format = LY_PREF_SCHEMA;
1111 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001112 stmt->kw = LY_STMT_IF_FEATURE;
1113 }
1114
1115 /*
1116 * 3) filter's select
1117 */
1118 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1119 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1120 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1121 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001122 ext_p->format = LY_PREF_SCHEMA;
1123 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001124 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001125 ext_p->parent_stmt = LY_STMT_MODULE;
1126 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001127
1128 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1129 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1130 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1131 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001132 stmt->format = LY_PREF_SCHEMA;
1133 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001134 stmt->kw = LY_STMT_TYPE;
1135
1136 /* create new imports for the used prefixes */
1137 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1138
1139 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1140 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1141 imp->flags = LYS_INTERNAL;
1142
1143 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1144
1145 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
1146 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
1147 imp->flags = LYS_INTERNAL;
1148
1149 return LY_SUCCESS;
1150}
1151
1152/**
1153 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
1154 *
1155 * @param[in] mod Parsed module to add to.
1156 * @return LY_SUCCESS on success.
1157 * @return LY_ERR on error.
1158 */
1159static LY_ERR
1160lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
1161{
1162 struct lysp_ext_instance *ext_p;
1163 struct lysp_stmt *stmt;
1164 struct lysp_import *imp;
1165
1166 /* add new extension instance */
1167 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1168
1169 /* fill in the extension instance fields */
1170 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1171 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1172 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001173 ext_p->format = LY_PREF_SCHEMA;
1174 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001175 ext_p->flags = LYS_INTERNAL;
Radek Krejciab430862021-03-02 20:13:40 +01001176 ext_p->parent_stmt = LY_STMT_MODULE;
1177 ext_p->parent_stmt_index = 0;
Michal Vasko45b521c2020-11-04 17:14:39 +01001178
1179 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1180 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1181 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1182 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001183 stmt->format = LY_PREF_SCHEMA;
1184 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001185 stmt->kw = LY_STMT_TYPE;
1186
1187 /* create new import for the used prefix */
1188 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1189
1190 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1191 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1192 imp->flags = LYS_INTERNAL;
1193
1194 return LY_SUCCESS;
1195}
1196
Michal Vasko3a41dff2020-07-15 14:30:28 +02001197LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +01001198lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001199 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko405cc9e2020-12-01 12:01:27 +01001200 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001201{
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001202 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001203 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001204 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001205 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001206 struct lys_yang_parser_ctx *yangctx = NULL;
1207 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001208 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001209 char *filename, *rev, *dot;
1210 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +01001211 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +02001212
Michal Vasko34e334d2021-01-25 16:12:31 +01001213 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001214
Michal Vasko7a0b0762020-09-02 16:37:01 +02001215 if (module) {
1216 *module = NULL;
1217 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001218
Michal Vasko34e334d2021-01-25 16:12:31 +01001219 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1220 implement = 1;
1221 } else {
1222 implement = need_implemented;
Radek Krejci00a3e8a2021-01-27 08:24:49 +01001223 }
Michal Vasko34e334d2021-01-25 16:12:31 +01001224
Radek Krejci86d106e2018-10-18 09:53:19 +02001225 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001226 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001227 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001228
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001229 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001230 switch (format) {
1231 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001232 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001233 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001234 break;
1235 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001236 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001237 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001238 break;
1239 default:
1240 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001241 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001242 break;
1243 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001244 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001245
1246 /* make sure that the newest revision is at position 0 */
1247 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001248 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001249 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), free_mod_cleanup);
Radek Krejci0af46292019-01-11 16:02:31 +01001250 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001251
Radek Krejcib3289d62019-09-18 12:21:39 +02001252 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001253 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001254 if (latest) {
1255 if (mod->revision) {
1256 if (!latest->revision) {
1257 /* latest has no revision, so mod is anyway newer */
1258 mod->latest_revision = latest->latest_revision;
1259 /* the latest is zeroed later when the new module is being inserted into the context */
1260 } else if (strcmp(mod->revision, latest->revision) > 0) {
1261 mod->latest_revision = latest->latest_revision;
1262 /* the latest is zeroed later when the new module is being inserted into the context */
1263 } else {
1264 latest = NULL;
1265 }
1266 } else {
1267 latest = NULL;
1268 }
1269 } else {
1270 mod->latest_revision = 1;
1271 }
1272
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001273 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001274 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001275 }
1276
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001277 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001278 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001279 if (implement) {
1280 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1281 if (mod_impl && (mod_impl != mod_dup)) {
1282 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1283 mod_impl->revision ? mod_impl->revision : "<none>");
1284 ret = LY_EDENIED;
1285 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001286 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001287 }
1288 if (mod_dup) {
1289 if (implement) {
1290 if (!mod_dup->implemented) {
1291 /* just implement it */
1292 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1293 goto free_mod_cleanup;
1294 }
1295
1296 /* nothing to do */
1297 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1298 mod_dup->revision ? mod_dup->revision : "<none>");
1299 goto free_mod_cleanup;
1300 }
1301
1302 /* nothing to do */
1303 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1304 mod_dup->revision ? mod_dup->revision : "<none>");
1305 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001306 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001307
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001308 switch (in->type) {
1309 case LY_IN_FILEPATH:
1310 /* check that name and revision match filename */
1311 filename = strrchr(in->method.fpath.filepath, '/');
1312 if (!filename) {
1313 filename = in->method.fpath.filepath;
1314 } else {
1315 filename++;
1316 }
1317 rev = strchr(filename, '@');
1318 dot = strrchr(filename, '.');
1319
1320 /* name */
1321 len = strlen(mod->name);
1322 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001323 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001324 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1325 }
1326 if (rev) {
1327 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001328 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001329 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001330 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001331 }
1332 }
1333
1334 break;
1335 case LY_IN_FD:
1336 case LY_IN_FILE:
1337 case LY_IN_MEMORY:
1338 /* nothing special to do */
1339 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001340 case LY_IN_ERROR:
1341 LOGINT(ctx);
1342 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001343 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001344 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001345 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001346
Michal Vasko7a0b0762020-09-02 16:37:01 +02001347 if (latest) {
1348 latest->latest_revision = 0;
1349 }
1350
Michal Vasko45b521c2020-11-04 17:14:39 +01001351 /* add internal data in case specific modules were parsed */
1352 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001353 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001354 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001355 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001356 }
1357
Michal Vasko405cc9e2020-12-01 12:01:27 +01001358 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001359 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001360
Michal Vasko7a0b0762020-09-02 16:37:01 +02001361 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001362 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001363 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001364 ctx->module_set_id++;
1365
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001366 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001367 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001368
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001369 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001370 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001371 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001372 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1373 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001374
1375 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001376 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001377
Michal Vasko89b5c072020-10-06 13:52:44 +02001378 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001379 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001380 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001381
1382 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001383 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001384 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001385 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001386 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001387 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001388 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001389 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001390 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001391 }
1392
Michal Vasko405cc9e2020-12-01 12:01:27 +01001393 /* success */
1394 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001395
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001396free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001397 lys_module_free(mod, NULL);
Michal Vasko0e02e8e2021-02-26 15:01:55 +01001398 if (ret) {
1399 mod = NULL;
1400 } else {
1401 /* return the existing module */
1402 assert(mod_dup);
1403 mod = mod_dup;
1404 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001405
Michal Vasko405cc9e2020-12-01 12:01:27 +01001406cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001407 if (pctx) {
1408 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1409 }
1410 if (format == LYS_IN_YANG) {
1411 yang_parser_ctx_free(yangctx);
1412 } else {
1413 yin_parser_ctx_free(yinctx);
1414 }
1415
Michal Vasko405cc9e2020-12-01 12:01:27 +01001416 if (!ret && module) {
1417 *module = mod;
1418 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001419 return ret;
1420}
1421
Radek Krejci545b4872020-11-15 10:15:12 +01001422static LYS_INFORMAT
1423lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1424{
1425 if (!format && (in->type == LY_IN_FILEPATH)) {
1426 /* unknown format - try to detect it from filename's suffix */
1427 const char *path = in->method.fpath.filepath;
1428 size_t len = strlen(path);
1429
1430 /* ignore trailing whitespaces */
1431 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1432
Radek Krejcif13b87b2020-12-01 22:02:17 +01001433 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1434 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001435 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001436 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1437 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001438 format = LYS_IN_YIN;
1439 } /* else still unknown */
1440 }
1441
1442 return format;
1443}
1444
Michal Vasko7a0b0762020-09-02 16:37:01 +02001445API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001446lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, const struct lys_module **module)
Michal Vasko7a0b0762020-09-02 16:37:01 +02001447{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001448 LY_ERR ret;
1449 struct lys_glob_unres unres = {0};
1450
Michal Vasko7a0b0762020-09-02 16:37:01 +02001451 if (module) {
1452 *module = NULL;
1453 }
Radek Krejci545b4872020-11-15 10:15:12 +01001454 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1455
1456 format = lys_parse_get_format(in, format);
1457 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001458
1459 /* remember input position */
1460 in->func_start = in->current;
1461
Michal Vasko405cc9e2020-12-01 12:01:27 +01001462 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1463 LY_CHECK_GOTO(ret, cleanup);
1464
1465 /* resolve global unres */
1466 ret = lys_compile_unres_glob(ctx, &unres);
1467 LY_CHECK_GOTO(ret, cleanup);
1468
1469cleanup:
1470 if (ret) {
1471 lys_compile_unres_glob_revert(ctx, &unres);
1472 }
1473 lys_compile_unres_glob_erase(ctx, &unres);
1474 if (ret && module) {
1475 *module = NULL;
1476 }
1477 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001478}
1479
Michal Vasko3a41dff2020-07-15 14:30:28 +02001480API LY_ERR
1481lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001482{
Radek Krejci0f969882020-08-21 16:56:47 +02001483 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001484 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001485
Michal Vasko3a41dff2020-07-15 14:30:28 +02001486 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001487
Michal Vasko3a41dff2020-07-15 14:30:28 +02001488 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 +02001489
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001490 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001491 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001492
Michal Vasko3a41dff2020-07-15 14:30:28 +02001493 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001494}
1495
Michal Vasko3a41dff2020-07-15 14:30:28 +02001496API LY_ERR
1497lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001498{
Radek Krejci0f969882020-08-21 16:56:47 +02001499 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001500 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001501
Michal Vasko3a41dff2020-07-15 14:30:28 +02001502 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001503
Michal Vasko3a41dff2020-07-15 14:30:28 +02001504 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 +02001505
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001506 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001507 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001508
Michal Vasko3a41dff2020-07-15 14:30:28 +02001509 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001510}
1511
Michal Vasko3a41dff2020-07-15 14:30:28 +02001512API LY_ERR
1513lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001514{
Radek Krejci0f969882020-08-21 16:56:47 +02001515 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001516 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001517
Michal Vasko3a41dff2020-07-15 14:30:28 +02001518 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001519
Michal Vasko3a41dff2020-07-15 14:30:28 +02001520 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001521 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001522
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001523 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001524 ly_in_free(in, 0);
1525
Michal Vasko3a41dff2020-07-15 14:30:28 +02001526 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001527}
1528
1529API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001530lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001531 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001532{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001533 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001534 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001535 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001536 char *wd, *wn = NULL;
1537 DIR *dir = NULL;
1538 struct dirent *file;
1539 char *match_name = NULL;
1540 LYS_INFORMAT format_aux, match_format = 0;
1541 struct ly_set *dirs;
1542 struct stat st;
1543
1544 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1545
1546 /* start to fill the dir fifo with the context's search path (if set)
1547 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001548 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001549
1550 len = strlen(name);
1551 if (cwd) {
1552 wd = get_current_dir_name();
1553 if (!wd) {
1554 LOGMEM(NULL);
1555 goto cleanup;
1556 } else {
1557 /* add implicit current working directory (./) to be searched,
1558 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001559 ret = ly_set_add(dirs, wd, 0, NULL);
1560 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001561 implicit_cwd = 1;
1562 }
1563 }
1564 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001565 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001566 /* check for duplicities with the implicit current working directory */
1567 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1568 implicit_cwd = 0;
1569 continue;
1570 }
1571 wd = strdup(searchpaths[i]);
1572 if (!wd) {
1573 LOGMEM(NULL);
1574 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001575 } else {
1576 ret = ly_set_add(dirs, wd, 0, NULL);
1577 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001578 }
1579 }
1580 }
1581 wd = NULL;
1582
1583 /* start searching */
1584 while (dirs->count) {
1585 free(wd);
1586 free(wn); wn = NULL;
1587
1588 dirs->count--;
1589 wd = (char *)dirs->objs[dirs->count];
1590 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001591 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001592
1593 if (dir) {
1594 closedir(dir);
1595 }
1596 dir = opendir(wd);
1597 dir_len = strlen(wd);
1598 if (!dir) {
1599 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1600 } else {
1601 while ((file = readdir(dir))) {
1602 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1603 /* skip . and .. */
1604 continue;
1605 }
1606 free(wn);
1607 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1608 LOGMEM(NULL);
1609 goto cleanup;
1610 }
1611 if (stat(wn, &st) == -1) {
1612 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001613 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001614 continue;
1615 }
1616 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1617 /* we have another subdirectory in searchpath to explore,
1618 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001619 ret = ly_set_add(dirs, wn, 0, NULL);
1620 LY_CHECK_GOTO(ret, cleanup);
1621
Radek Krejcid33273d2018-10-25 14:55:52 +02001622 /* continue with the next item in current directory */
1623 wn = NULL;
1624 continue;
1625 } else if (!S_ISREG(st.st_mode)) {
1626 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1627 continue;
1628 }
1629
1630 /* here we know that the item is a file which can contain a module */
1631 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001632 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001633 /* different filename than the module we search for */
1634 continue;
1635 }
1636
1637 /* get type according to filename suffix */
1638 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001639 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1640 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001641 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001642 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1643 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001644 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001645 } else {
1646 /* not supportde suffix/file format */
1647 continue;
1648 }
1649
1650 if (revision) {
1651 /* we look for the specific revision, try to get it from the filename */
1652 if (file->d_name[len] == '@') {
1653 /* check revision from the filename */
1654 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1655 /* another revision */
1656 continue;
1657 } else {
1658 /* exact revision */
1659 free(match_name);
1660 match_name = wn;
1661 wn = NULL;
1662 match_len = dir_len + 1 + len;
1663 match_format = format_aux;
1664 goto success;
1665 }
1666 } else {
1667 /* continue trying to find exact revision match, use this only if not found */
1668 free(match_name);
1669 match_name = wn;
1670 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001671 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001672 match_format = format_aux;
1673 continue;
1674 }
1675 } else {
1676 /* remember the revision and try to find the newest one */
1677 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001678 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001679 lysp_check_date(NULL, &file->d_name[len + 1],
1680 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001681 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001682 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001683 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1684 continue;
1685 }
1686 free(match_name);
1687 }
1688
1689 match_name = wn;
1690 wn = NULL;
1691 match_len = dir_len + 1 + len;
1692 match_format = format_aux;
1693 continue;
1694 }
1695 }
1696 }
1697 }
1698
1699success:
1700 (*localfile) = match_name;
1701 match_name = NULL;
1702 if (format) {
1703 (*format) = match_format;
1704 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001705 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001706
1707cleanup:
1708 free(wn);
1709 free(wd);
1710 if (dir) {
1711 closedir(dir);
1712 }
1713 free(match_name);
1714 ly_set_free(dirs, free);
1715
1716 return ret;
1717}