blob: b64665c36ce38937598451916b4a7e5f4973983c [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
132const char * const ly_devmod_list[] = {
133 [LYS_DEV_NOT_SUPPORTED] = "not-supported",
134 [LYS_DEV_ADD] = "add",
135 [LYS_DEV_DELETE] = "delete",
136 [LYS_DEV_REPLACE] = "replace",
137};
138
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200139API LY_ERR
140lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
141{
Michal Vasko1d972ca2020-11-03 17:16:56 +0100142 struct lysc_node *elem, *elem2;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100143 const struct lysc_node_action *action;
144 const struct lysc_node_notif *notif;
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200145
146 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
147
148 LYSC_TREE_DFS_BEGIN(root, elem) {
149 /* schema node */
150 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
151
Radek Krejci2a9fc652021-01-22 17:44:34 +0100152 LY_LIST_FOR(lysc_node_actions(elem), action) {
153 LYSC_TREE_DFS_BEGIN(action, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200154 /* action subtree */
155 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
156
Radek Krejci2a9fc652021-01-22 17:44:34 +0100157 LYSC_TREE_DFS_END(action, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200158 }
159 }
160
Radek Krejci2a9fc652021-01-22 17:44:34 +0100161 LY_LIST_FOR(lysc_node_notifs(elem), notif) {
162 LYSC_TREE_DFS_BEGIN(notif, elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200163 /* notification subtree */
164 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
165
Radek Krejci2a9fc652021-01-22 17:44:34 +0100166 LYSC_TREE_DFS_END(notif, elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200167 }
168 }
169
170 LYSC_TREE_DFS_END(root, elem);
171 }
172
173 return LY_SUCCESS;
174}
175
176API LY_ERR
177lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
178{
Michal Vasko2336cf52020-11-03 17:18:15 +0100179 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200180
181 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
182
183 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +0100184 LY_LIST_FOR(mod->compiled->data, root) {
185 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
186 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200187
188 /* RPCs */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100189 LY_LIST_FOR((const struct lysc_node *)mod->compiled->rpcs, root) {
190 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200191 }
192
193 /* notifications */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100194 LY_LIST_FOR((const struct lysc_node *)mod->compiled->notifs, root) {
195 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
Michal Vaskof1ab44f2020-10-22 08:58:32 +0200196 }
197
198 return LY_SUCCESS;
199}
200
Radek Krejcib93bd412020-11-02 13:23:11 +0100201static void
202lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
203{
Radek Krejcic5b54a02020-11-05 17:13:18 +0100204 for ( ; first_case; first_case = (const struct lysc_node_case *)first_case->next) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100205 if (first_case->child) {
206 /* there is something to return */
207 (*next) = first_case->child;
208 return;
209 }
210 }
211
212 /* no children in choice's cases, so go to the choice's sibling instead of into it */
213 (*last) = (*next);
214 (*next) = (*next)->next;
215}
216
Radek Krejci035dacf2021-02-12 18:25:53 +0100217/**
218 * @brief Generic getnext function for ::lys_getnext() and ::lys_getnext_ext().
219 *
220 * Gets next schema tree (sibling) node element that can be instantiated in a data tree. Returned node can
221 * be from an augment. If the @p ext is provided, the function is locked inside the schema tree defined in the
222 * extension instance.
223 *
224 * ::lys_getnext_() is supposed to be called sequentially. In the first call, the \p last parameter is usually NULL
225 * and function starts returning i) the first \p parent's child or ii) the first top level element specified in the
226 * given extension (if provided) or iii) the first top level element of the \p module.
227 * Consequent calls suppose to provide the previously returned node as the \p last parameter and still the same
228 * \p parent and \p module parameters.
229 *
230 * Without options, the function is used to traverse only the schema nodes that can be paired with corresponding
231 * data nodes in a data tree. By setting some \p options the behavior can be modified to the extent that
232 * all the schema nodes are iteratively returned.
233 *
234 * @param[in] last Previously returned schema tree node, or NULL in case of the first call.
235 * @param[in] parent Parent of the subtree where the function starts processing.
236 * @param[in] module In case of iterating on top level elements, the \p parent is NULL and
237 * module must be specified.
238 * @param[in] ext The extension instance to provide a separate schema tree. To consider the top level elements in the tree,
239 * the \p parent must be NULL. Anyway, at least one of @p parent, @p module and @p ext parameters must be specified.
240 * @param[in] options [ORed options](@ref sgetnextflags).
241 * @return Next schema tree node that can be instantiated in a data tree, NULL in case there is no such element.
242 */
243static const struct lysc_node *
244lys_getnext_(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module,
245 const struct lysc_ext_instance *ext, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100246{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100247 const struct lysc_node *next = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200248 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci035dacf2021-02-12 18:25:53 +0100249 struct lysc_node **data_p = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100250
Radek Krejci035dacf2021-02-12 18:25:53 +0100251 LY_CHECK_ARG_RET(NULL, parent || module || ext, NULL);
Radek Krejcia3045382018-11-22 14:30:31 +0100252
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200253next:
Radek Krejcia3045382018-11-22 14:30:31 +0100254 if (!last) {
255 /* first call */
256
257 /* get know where to start */
258 if (parent) {
259 /* schema subtree */
Michal Vasko544e58a2021-01-28 14:33:41 +0100260 next = last = lysc_node_child(parent);
Radek Krejcia3045382018-11-22 14:30:31 +0100261 } else {
262 /* top level data */
Radek Krejci035dacf2021-02-12 18:25:53 +0100263 if (ext) {
264 lysc_ext_substmt(ext, LY_STMT_CONTAINER /* matches all nodes */, (void **)&data_p, NULL);
265 next = last = data_p ? *data_p : NULL;
266 } else {
267 next = last = module->data;
268 }
Radek Krejcia3045382018-11-22 14:30:31 +0100269 }
270 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100271 /* try to get action or notification */
272 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100273 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100274 /* test if the next can be returned */
275 goto check;
276
Michal Vasko1bf09392020-03-27 12:38:10 +0100277 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100278 action_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100279 next = last->next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100280 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100281 action_flag = notif_flag = 1;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100282 next = last->next;
Michal Vasko20424b42020-08-31 12:29:38 +0200283 } else {
284 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100285 }
286
Radek Krejcia3045382018-11-22 14:30:31 +0100287repeat:
288 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100289 /* possibly go back to parent */
Radek Krejci035dacf2021-02-12 18:25:53 +0100290 data_p = NULL;
Michal Vasko69730152020-10-09 16:30:07 +0200291 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100292 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200293 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100294 } else if (!action_flag) {
295 action_flag = 1;
Radek Krejci035dacf2021-02-12 18:25:53 +0100296 if (ext) {
297 lysc_ext_substmt(ext, LY_STMT_RPC /* matches also actions */, (void **)&data_p, NULL);
298 next = data_p ? *data_p : NULL;
299 } else if (parent) {
300 next = (struct lysc_node *)lysc_node_actions(parent);
301 } else {
302 next = (struct lysc_node *)module->rpcs;
303 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100304 } else if (!notif_flag) {
305 notif_flag = 1;
Radek Krejci035dacf2021-02-12 18:25:53 +0100306 if (ext) {
307 lysc_ext_substmt(ext, LY_STMT_NOTIFICATION, (void **)&data_p, NULL);
308 next = data_p ? *data_p : NULL;
309 } else if (parent) {
310 next = (struct lysc_node *)lysc_node_notifs(parent);
311 } else {
312 next = (struct lysc_node *)module->notifs;
313 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100314 } else {
315 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100316 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100317 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100318 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100319check:
Radek Krejcia3045382018-11-22 14:30:31 +0100320 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100321 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100322 case LYS_ACTION:
323 case LYS_NOTIF:
324 case LYS_LEAF:
325 case LYS_ANYXML:
326 case LYS_ANYDATA:
327 case LYS_LIST:
328 case LYS_LEAFLIST:
329 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200330 case LYS_CASE:
331 if (options & LYS_GETNEXT_WITHCASE) {
332 break;
333 } else {
334 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100335 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200336 }
337 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100338 case LYS_CONTAINER:
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100339 if (!(next->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100340 if (lysc_node_child(next)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100341 /* go into */
Michal Vasko544e58a2021-01-28 14:33:41 +0100342 next = lysc_node_child(next);
Radek Krejcia3045382018-11-22 14:30:31 +0100343 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100344 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100345 next = next->next;
346 }
347 goto repeat;
348 }
349 break;
350 case LYS_CHOICE:
351 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200352 break;
Michal Vasko544e58a2021-01-28 14:33:41 +0100353 } else if ((options & LYS_GETNEXT_NOCHOICE) || !lysc_node_child(next)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100354 next = next->next;
355 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100356 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko544e58a2021-01-28 14:33:41 +0100357 next = lysc_node_child(next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100358 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100359 /* go into */
360 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100361 }
Radek Krejcia3045382018-11-22 14:30:31 +0100362 }
363 goto repeat;
Michal Vasko544e58a2021-01-28 14:33:41 +0100364 case LYS_INPUT:
365 if (options & LYS_GETNEXT_OUTPUT) {
366 /* skip */
367 next = next->next;
368 } else {
369 /* go into */
370 next = lysc_node_child(next);
371 }
372 goto repeat;
373 case LYS_OUTPUT:
374 if (!(options & LYS_GETNEXT_OUTPUT)) {
375 /* skip */
376 next = next->next;
377 } else {
378 /* go into */
379 next = lysc_node_child(next);
380 }
381 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100382 default:
383 /* we should not be here */
Radek Krejci035dacf2021-02-12 18:25:53 +0100384 LOGINT(module ? module->mod->ctx : parent ? parent->module->ctx : ext->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100385 return NULL;
386 }
387
Radek Krejcia3045382018-11-22 14:30:31 +0100388 return next;
389}
390
391API const struct lysc_node *
Radek Krejci035dacf2021-02-12 18:25:53 +0100392lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
393{
394 return lys_getnext_(last, parent, module, NULL, options);
395}
396
397API const struct lysc_node *
398lys_getnext_ext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_ext_instance *ext, uint32_t options)
399{
400 return lys_getnext_(last, parent, NULL, ext, options);
401}
402
Radek Krejcif16e2542021-02-17 15:39:23 +0100403const struct lysc_node *
404lys_find_ext_instance_node(const struct lysc_ext_instance *ext, const struct lys_module *module, const char *name, size_t name_len,
405 uint16_t nodetype, uint32_t options)
406{
407 const struct lysc_node *node = NULL;
408
409 LY_CHECK_ARG_RET(NULL, ext, name, NULL);
410 if (!nodetype) {
411 nodetype = LYS_NODETYPE_MASK;
412 }
413
414 if (module && (module != ext->module)) {
415 return NULL;
416 }
417
418 while ((node = lys_getnext_ext(node, NULL, ext, options))) {
419 if (!(node->nodetype & nodetype)) {
420 continue;
421 }
422
423 if (name_len) {
424 if (!ly_strncmp(node->name, name, name_len)) {
425 return node;
426 }
427 } else {
428 if (!strcmp(node->name, name)) {
429 return node;
430 }
431 }
432 }
433 return NULL;
434}
435
Radek Krejci035dacf2021-02-12 18:25:53 +0100436API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100437lys_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 +0200438 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100439{
440 const struct lysc_node *node = NULL;
441
442 LY_CHECK_ARG_RET(NULL, module, name, NULL);
443 if (!nodetype) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100444 nodetype = LYS_NODETYPE_MASK;
Radek Krejcia3045382018-11-22 14:30:31 +0100445 }
446
447 while ((node = lys_getnext(node, parent, module->compiled, options))) {
448 if (!(node->nodetype & nodetype)) {
449 continue;
450 }
451 if (node->module != module) {
452 continue;
453 }
454
455 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200456 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100457 return node;
458 }
459 } else {
460 if (!strcmp(node->name, name)) {
461 return node;
462 }
463 }
464 }
465 return NULL;
466}
467
Michal Vasko519fd602020-05-26 12:17:39 +0200468API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100469lys_find_xpath_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
470 struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200471{
472 LY_ERR ret = LY_SUCCESS;
473 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200474 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200475 uint32_t i;
476
Michal Vasko26512682021-01-11 11:35:40 +0100477 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko519fd602020-05-26 12:17:39 +0200478 if (!(options & LYXP_SCNODE_ALL)) {
479 options = LYXP_SCNODE;
480 }
Michal Vasko26512682021-01-11 11:35:40 +0100481 if (!ctx) {
482 ctx = ctx_node->module->ctx;
483 }
Michal Vasko519fd602020-05-26 12:17:39 +0200484
485 memset(&xp_set, 0, sizeof xp_set);
486
487 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100488 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200489 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200490
491 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100492 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200493 LY_CHECK_GOTO(ret, cleanup);
494
495 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200496 ret = ly_set_new(set);
497 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200498
499 /* transform into ly_set */
500 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100501 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200502 (*set)->size = xp_set.used;
503
504 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200505 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200506 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200507 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200508 }
509 }
510
511cleanup:
512 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100513 lyxp_expr_free(ctx, exp);
Michal Vasko519fd602020-05-26 12:17:39 +0200514 return ret;
515}
516
Michal Vasko072de482020-08-05 13:27:21 +0200517API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200518lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
519 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
520{
521 LY_ERR ret = LY_SUCCESS;
522 struct lyxp_set xp_set = {0};
523 uint32_t i;
524
525 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
526 if (!(options & LYXP_SCNODE_ALL)) {
527 options = LYXP_SCNODE;
528 }
529
530 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100531 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 +0200532 LY_CHECK_GOTO(ret, cleanup);
533
534 /* allocate return set */
535 ret = ly_set_new(set);
536 LY_CHECK_GOTO(ret, cleanup);
537
538 /* transform into ly_set */
539 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
540 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
541 (*set)->size = xp_set.used;
542
543 for (i = 0; i < xp_set.used; ++i) {
Michal Vaskod97959c2020-12-10 12:18:28 +0100544 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx >= LYXP_SET_SCNODE_ATOM)) {
545 assert((xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM) ||
546 (xp_set.val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX));
Michal Vasko40308e72020-10-20 16:38:40 +0200547 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
548 LY_CHECK_GOTO(ret, cleanup);
549 }
550 }
551
552cleanup:
553 lyxp_set_free_content(&xp_set);
554 if (ret) {
555 ly_set_free(*set, NULL);
556 *set = NULL;
557 }
558 return ret;
559}
560
561API LY_ERR
Michal Vasko26512682021-01-11 11:35:40 +0100562lys_find_xpath(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *xpath, uint32_t options,
563 struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200564{
565 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200566 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200567 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200568 uint32_t i;
569
Michal Vasko26512682021-01-11 11:35:40 +0100570 LY_CHECK_ARG_RET(NULL, ctx || ctx_node, xpath, set, LY_EINVAL);
Michal Vasko072de482020-08-05 13:27:21 +0200571 if (!(options & LYXP_SCNODE_ALL)) {
572 options = LYXP_SCNODE;
573 }
Michal Vasko26512682021-01-11 11:35:40 +0100574 if (!ctx) {
575 ctx = ctx_node->module->ctx;
576 }
Michal Vasko072de482020-08-05 13:27:21 +0200577
Michal Vasko072de482020-08-05 13:27:21 +0200578 /* compile expression */
Michal Vasko26512682021-01-11 11:35:40 +0100579 ret = lyxp_expr_parse(ctx, xpath, 0, 1, &exp);
Radek Krejcif03a9e22020-09-18 20:09:31 +0200580 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200581
582 /* atomize expression */
Michal Vasko400e9672021-01-11 13:39:17 +0100583 ret = lyxp_atomize(ctx, exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200584 LY_CHECK_GOTO(ret, cleanup);
585
586 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200587 ret = ly_set_new(set);
588 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200589
590 /* transform into ly_set */
591 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
Michal Vasko26512682021-01-11 11:35:40 +0100592 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200593 (*set)->size = xp_set.used;
594
595 for (i = 0; i < xp_set.used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100596 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 +0200597 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200598 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200599 }
600 }
601
602cleanup:
603 lyxp_set_free_content(&xp_set);
Michal Vasko26512682021-01-11 11:35:40 +0100604 lyxp_expr_free(ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200605 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200606 ly_set_free(*set, NULL);
607 *set = NULL;
608 }
Michal Vasko072de482020-08-05 13:27:21 +0200609 return ret;
610}
611
Radek Krejcibc5644c2020-10-27 14:53:17 +0100612API LY_ERR
613lys_find_lypath_atoms(const struct ly_path *path, struct ly_set **set)
614{
615 LY_ERR ret = LY_SUCCESS;
616 LY_ARRAY_COUNT_TYPE u, v;
617
618 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
619
620 /* allocate return set */
621 LY_CHECK_RET(ly_set_new(set));
622
623 LY_ARRAY_FOR(path, u) {
624 /* add nodes from the path */
625 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
626 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
627 LY_ARRAY_FOR(path[u].predicates, v) {
628 /* add all the keys in a predicate */
629 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
630 }
631 }
632 }
633
634cleanup:
635 if (ret) {
636 ly_set_free(*set, NULL);
637 *set = NULL;
638 }
639 return ret;
640}
641
642API LY_ERR
643lys_find_path_atoms(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output,
644 struct ly_set **set)
645{
646 LY_ERR ret = LY_SUCCESS;
647 uint8_t oper;
648 struct lyxp_expr *expr = NULL;
649 struct ly_path *p = NULL;
650
651 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, path, set, LY_EINVAL);
652
653 if (!ctx) {
654 ctx = ctx_node->module->ctx;
655 }
656
657 /* parse */
658 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &expr);
659 LY_CHECK_GOTO(ret, cleanup);
660
661 /* compile */
662 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
663 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 +0100664 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100665 LY_CHECK_GOTO(ret, cleanup);
666
667 /* resolve */
668 ret = lys_find_lypath_atoms(p, set);
669
670cleanup:
671 ly_path_free(ctx, p);
672 lyxp_expr_free(ctx, expr);
673 return ret;
674}
675
676API const struct lysc_node *
677lys_find_path(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, ly_bool output)
678{
679 const struct lysc_node *snode = NULL;
680 struct lyxp_expr *exp = NULL;
681 struct ly_path *p = NULL;
682 LY_ERR ret;
683 uint8_t oper;
684
685 LY_CHECK_ARG_RET(ctx, ctx || ctx_node, NULL);
686
687 if (!ctx) {
688 ctx = ctx_node->module->ctx;
689 }
690
691 /* parse */
692 ret = lyxp_expr_parse(ctx, path, strlen(path), 0, &exp);
693 LY_CHECK_GOTO(ret, cleanup);
694
695 /* compile */
696 oper = output ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
697 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 +0100698 LY_PREF_JSON, NULL, NULL, &p);
Radek Krejcibc5644c2020-10-27 14:53:17 +0100699 LY_CHECK_GOTO(ret, cleanup);
700
701 /* get last node */
702 snode = p[LY_ARRAY_COUNT(p) - 1].node;
703
704cleanup:
705 ly_path_free(ctx, p);
706 lyxp_expr_free(ctx, exp);
707 return snode;
708}
709
Michal Vasko14654712020-02-06 08:35:21 +0100710char *
711lysc_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 +0200712 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200713{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200715 char *path = NULL;
716 int len = 0;
717
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200718 if (buffer) {
719 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
Michal Vasko770d3fc2021-01-26 09:14:35 +0100720 buffer[0] = '\0';
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200721 }
722
Radek Krejci327de162019-06-14 12:52:07 +0200723 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200725 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100726 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200727 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100728 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200729
Michal Vasko721b6f62021-02-08 08:52:53 +0100730 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))) {
Michal Vasko65de0402020-08-03 16:34:19 +0200731 /* schema-only node */
732 continue;
733 }
734
Michal Vasko11deea12020-08-05 13:54:50 +0200735 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100737 if (parent && (iter->parent == parent)) {
738 slash = "";
739 } else {
740 slash = "/";
741 }
Michal Vasko69730152020-10-09 16:30:07 +0200742 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200743 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200744 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100745 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200746 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100747 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200748 }
Radek Krejci327de162019-06-14 12:52:07 +0200749 } else {
750 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200751 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100752 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200753 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100754 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200755 }
Radek Krejci327de162019-06-14 12:52:07 +0200756 }
757 free(s);
758 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200759
Michal Vasko69730152020-10-09 16:30:07 +0200760 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200761 /* not enough space in buffer */
762 break;
763 }
Radek Krejci327de162019-06-14 12:52:07 +0200764 }
765
766 if (len < 0) {
767 free(path);
768 path = NULL;
769 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200770 if (buffer) {
771 strcpy(buffer, "/");
772 } else {
773 path = strdup("/");
774 }
Radek Krejci327de162019-06-14 12:52:07 +0200775 }
776 break;
777 }
778
Radek Krejci1c0c3442019-07-23 16:08:47 +0200779 if (buffer) {
780 return buffer;
781 } else {
782 return path;
783 }
Radek Krejci327de162019-06-14 12:52:07 +0200784}
785
Michal Vasko14654712020-02-06 08:35:21 +0100786API char *
787lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
788{
789 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
790}
791
Michal Vasko405cc9e2020-12-01 12:01:27 +0100792LY_ERR
793lys_set_implemented_r(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200794{
795 struct lys_module *m;
796
Michal Vasko405cc9e2020-12-01 12:01:27 +0100797 assert(!mod->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200798
799 /* we have module from the current context */
800 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
801 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200802 assert(m != mod);
803
804 /* check collision with other implemented revision */
805 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
806 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
807 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200808 }
809
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100810 /* enable features */
811 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
812
Michal Vasko89b5c072020-10-06 13:52:44 +0200813 /* add the module into newly implemented module set */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100814 LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200815
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200816 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200817 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200818
819 /* compile the schema */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100820 return lys_compile(mod, 0, unres);
821}
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200822
Michal Vasko405cc9e2020-12-01 12:01:27 +0100823API LY_ERR
824lys_set_implemented(struct lys_module *mod, const char **features)
825{
826 LY_ERR ret = LY_SUCCESS, r;
827 struct lys_glob_unres unres = {0};
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200828
Michal Vasko405cc9e2020-12-01 12:01:27 +0100829 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
Michal Vasko916aefb2020-11-02 15:43:16 +0100830
Michal Vasko405cc9e2020-12-01 12:01:27 +0100831 if (mod->implemented) {
832 /* mod is already implemented, set the features */
833 r = lys_set_features(mod->parsed, features);
834 if (r == LY_EEXIST) {
835 /* no changes */
836 return LY_SUCCESS;
837 } else if (r) {
838 /* error */
839 return r;
Michal Vasko89b5c072020-10-06 13:52:44 +0200840 }
841
Michal Vasko405cc9e2020-12-01 12:01:27 +0100842 /* full recompilation */
843 return lys_recompile(mod->ctx, 1);
Michal Vasko89b5c072020-10-06 13:52:44 +0200844 }
Michal Vasko08c8b272020-11-24 18:11:30 +0100845
Michal Vasko405cc9e2020-12-01 12:01:27 +0100846 /* implement this module and any other required modules, recursively */
847 ret = lys_set_implemented_r(mod, features, &unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +0100848
849 /* the first module being implemented is finished, resolve global unres, consolidate the set */
850 if (!ret) {
851 ret = lys_compile_unres_glob(mod->ctx, &unres);
852 }
853 if (ret) {
854 /* failure, full compile revert */
855 lys_compile_unres_glob_revert(mod->ctx, &unres);
856 }
857
858 lys_compile_unres_glob_erase(mod->ctx, &unres);
Michal Vasko89b5c072020-10-06 13:52:44 +0200859 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200860}
861
Michal Vasko7c8439f2020-08-05 13:25:19 +0200862static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100863lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200864{
865 struct lysp_import *imp;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200866 LY_ARRAY_COUNT_TYPE u, v;
867
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100868 pmod->parsing = 1;
869 LY_ARRAY_FOR(pmod->imports, u) {
870 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200871 if (!imp->module) {
Michal Vasko405cc9e2020-12-01 12:01:27 +0100872 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, NULL,
873 pctx->unres, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200874 }
875 /* check for importing the same module twice */
876 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100877 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200878 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
879 }
880 }
881 }
Radek Krejci771928a2021-01-19 13:42:36 +0100882 LY_CHECK_RET(lysp_load_submodules(pctx, pmod));
883
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100884 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200885
886 return LY_SUCCESS;
887}
888
Michal Vasko3a41dff2020-07-15 14:30:28 +0200889LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200890lys_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 +0200891 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200892 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200893{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200894 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100895 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100896 struct lys_yang_parser_ctx *yangctx = NULL;
897 struct lys_yin_parser_ctx *yinctx = NULL;
898 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100899
Michal Vasko3a41dff2020-07-15 14:30:28 +0200900 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100901
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 switch (format) {
903 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200904 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100905 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100906 break;
907 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200908 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100909 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100910 break;
911 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200912 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200913 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100914 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200915 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200916 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200917 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100918
919 /* make sure that the newest revision is at position 0 */
920 lysp_sort_revisions(submod->revs);
921
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 /* decide the latest revision */
Michal Vasko8dc31992021-02-22 10:30:47 +0100923 latest_sp = (struct lysp_submodule *)ly_ctx_get_submodule2_latest(submod->mod, submod->name);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 if (latest_sp) {
925 if (submod->revs) {
926 if (!latest_sp->revs) {
927 /* latest has no revision, so mod is anyway newer */
928 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200929 /* the latest_sp is zeroed later when the new module is being inserted into the context */
930 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
931 submod->latest_revision = latest_sp->latest_revision;
932 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100933 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200934 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100935 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200936 } else {
937 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100938 }
939 } else {
940 submod->latest_revision = 1;
941 }
942
Radek Krejcib3289d62019-09-18 12:21:39 +0200943 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200944 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200945 }
946
947 if (latest_sp) {
948 latest_sp->latest_revision = 0;
949 }
950
Michal Vasko7a0b0762020-09-02 16:37:01 +0200951 lys_parser_fill_filepath(ctx, in, &submod->filepath);
952
Michal Vasko7c8439f2020-08-05 13:25:19 +0200953 /* resolve imports and includes */
954 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
955
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100956 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100957 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
958 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100959
David Sedlák1b623122019-08-05 15:27:49 +0200960 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100961 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200962 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100963 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200964 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200965 *submodule = submod;
966 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200967
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200969 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200970 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100971 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200972 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100973 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200974 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200975 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200976}
977
Michal Vasko45b521c2020-11-04 17:14:39 +0100978/**
979 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
980 *
981 * @param[in] mod Parsed module to add to.
982 * @return LY_SUCCESS on success.
983 * @return LY_ERR on error.
984 */
985static LY_ERR
986lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
987{
988 struct lysp_ext_instance *ext_p;
989 struct lysp_stmt *stmt;
990 struct lysp_import *imp;
991
992 /*
993 * 1) edit-config's operation
994 */
995 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
996 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
997 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
998 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +0100999 ext_p->format = LY_PREF_SCHEMA;
1000 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001001 ext_p->flags = LYS_INTERNAL;
1002 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1003 ext_p->insubstmt_index = 0;
1004
1005 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1006 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1007 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1008 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001009 stmt->format = LY_PREF_SCHEMA;
1010 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001011 stmt->kw = LY_STMT_TYPE;
1012
1013 stmt->child = calloc(1, sizeof *stmt->child);
1014 stmt = stmt->child;
1015 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1016 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1017 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001018 stmt->format = LY_PREF_SCHEMA;
1019 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001020 stmt->kw = LY_STMT_ENUM;
1021
1022 stmt->next = calloc(1, sizeof *stmt->child);
1023 stmt = stmt->next;
1024 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1025 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1026 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001027 stmt->format = LY_PREF_SCHEMA;
1028 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001029 stmt->kw = LY_STMT_ENUM;
1030
1031 stmt->next = calloc(1, sizeof *stmt->child);
1032 stmt = stmt->next;
1033 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1034 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1035 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001036 stmt->format = LY_PREF_SCHEMA;
1037 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001038 stmt->kw = LY_STMT_ENUM;
1039
1040 stmt->next = calloc(1, sizeof *stmt->child);
1041 stmt = stmt->next;
1042 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1043 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1044 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001045 stmt->format = LY_PREF_SCHEMA;
1046 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001047 stmt->kw = LY_STMT_ENUM;
1048
1049 stmt->next = calloc(1, sizeof *stmt->child);
1050 stmt = stmt->next;
1051 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1052 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1053 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001054 stmt->format = LY_PREF_SCHEMA;
1055 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001056 stmt->kw = LY_STMT_ENUM;
1057
1058 /*
1059 * 2) filter's type
1060 */
1061 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1062 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1063 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1064 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001065 ext_p->format = LY_PREF_SCHEMA;
1066 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001067 ext_p->flags = LYS_INTERNAL;
1068 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1069 ext_p->insubstmt_index = 0;
1070
1071 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1072 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1073 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1074 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001075 stmt->format = LY_PREF_SCHEMA;
1076 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001077 stmt->kw = LY_STMT_TYPE;
1078
1079 stmt->child = calloc(1, sizeof *stmt->child);
1080 stmt = stmt->child;
1081 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1082 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1083 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001084 stmt->format = LY_PREF_SCHEMA;
1085 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001086 stmt->kw = LY_STMT_ENUM;
1087
1088 stmt->next = calloc(1, sizeof *stmt->child);
1089 stmt = stmt->next;
1090 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1091 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
1092 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001093 stmt->format = LY_PREF_SCHEMA;
1094 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001095 stmt->kw = LY_STMT_ENUM;
1096
1097 /* if-feature for enum allowed only for YANG 1.1 modules */
1098 if (mod->version >= LYS_VERSION_1_1) {
1099 stmt->child = calloc(1, sizeof *stmt->child);
1100 stmt = stmt->child;
1101 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1102 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
1103 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001104 stmt->format = LY_PREF_SCHEMA;
1105 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001106 stmt->kw = LY_STMT_IF_FEATURE;
1107 }
1108
1109 /*
1110 * 3) filter's select
1111 */
1112 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1113 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1114 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1115 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001116 ext_p->format = LY_PREF_SCHEMA;
1117 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001118 ext_p->flags = LYS_INTERNAL;
1119 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1120 ext_p->insubstmt_index = 0;
1121
1122 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1123 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1124 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1125 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001126 stmt->format = LY_PREF_SCHEMA;
1127 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001128 stmt->kw = LY_STMT_TYPE;
1129
1130 /* create new imports for the used prefixes */
1131 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1132
1133 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1134 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1135 imp->flags = LYS_INTERNAL;
1136
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-types", 0, &imp->name));
1140 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
1141 imp->flags = LYS_INTERNAL;
1142
1143 return LY_SUCCESS;
1144}
1145
1146/**
1147 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
1148 *
1149 * @param[in] mod Parsed module to add to.
1150 * @return LY_SUCCESS on success.
1151 * @return LY_ERR on error.
1152 */
1153static LY_ERR
1154lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
1155{
1156 struct lysp_ext_instance *ext_p;
1157 struct lysp_stmt *stmt;
1158 struct lysp_import *imp;
1159
1160 /* add new extension instance */
1161 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
1162
1163 /* fill in the extension instance fields */
1164 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
1165 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
1166 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001167 ext_p->format = LY_PREF_SCHEMA;
1168 ext_p->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001169 ext_p->flags = LYS_INTERNAL;
1170 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
1171 ext_p->insubstmt_index = 0;
1172
1173 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
1174 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
1175 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
1176 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
Michal Vaskofc2cd072021-02-24 13:17:17 +01001177 stmt->format = LY_PREF_SCHEMA;
1178 stmt->prefix_data = mod;
Michal Vasko45b521c2020-11-04 17:14:39 +01001179 stmt->kw = LY_STMT_TYPE;
1180
1181 /* create new import for the used prefix */
1182 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
1183
1184 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
1185 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
1186 imp->flags = LYS_INTERNAL;
1187
1188 return LY_SUCCESS;
1189}
1190
Michal Vasko3a41dff2020-07-15 14:30:28 +02001191LY_ERR
Michal Vasko34e334d2021-01-25 16:12:31 +01001192lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool need_implemented,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001193 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 +01001194 void *check_data, const char **features, struct lys_glob_unres *unres, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001195{
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001196 struct lys_module *mod = NULL, *latest, *mod_dup, *mod_impl;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001197 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001198 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001199 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +01001200 struct lys_yang_parser_ctx *yangctx = NULL;
1201 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +02001202 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001203 char *filename, *rev, *dot;
1204 size_t len;
Michal Vasko34e334d2021-01-25 16:12:31 +01001205 ly_bool implement;
Radek Krejci86d106e2018-10-18 09:53:19 +02001206
Michal Vasko34e334d2021-01-25 16:12:31 +01001207 assert(ctx && in && (!features || need_implemented) && unres);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001208
Michal Vasko7a0b0762020-09-02 16:37:01 +02001209 if (module) {
1210 *module = NULL;
1211 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001212
Michal Vasko34e334d2021-01-25 16:12:31 +01001213 if (ctx->flags & LY_CTX_ALL_IMPLEMENTED) {
1214 implement = 1;
1215 } else {
1216 implement = need_implemented;
Radek Krejci00a3e8a2021-01-27 08:24:49 +01001217 }
Michal Vasko34e334d2021-01-25 16:12:31 +01001218
Radek Krejci86d106e2018-10-18 09:53:19 +02001219 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001220 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001221 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001222
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001223 /* parse */
Radek Krejci86d106e2018-10-18 09:53:19 +02001224 switch (format) {
1225 case LYS_IN_YIN:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001226 ret = yin_parse_module(&yinctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001227 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001228 break;
1229 case LYS_IN_YANG:
Michal Vasko405cc9e2020-12-01 12:01:27 +01001230 ret = yang_parse_module(&yangctx, in, mod, unres);
Michal Vaskob36053d2020-03-26 15:49:30 +01001231 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001232 break;
1233 default:
1234 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001235 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001236 break;
1237 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001238 LY_CHECK_GOTO(ret, free_mod_cleanup);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001239
1240 /* make sure that the newest revision is at position 0 */
1241 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001242 if (mod->parsed->revs) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001243 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 +01001244 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001245
Radek Krejcib3289d62019-09-18 12:21:39 +02001246 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001247 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001248 if (latest) {
1249 if (mod->revision) {
1250 if (!latest->revision) {
1251 /* latest has no revision, so mod is anyway newer */
1252 mod->latest_revision = latest->latest_revision;
1253 /* the latest is zeroed later when the new module is being inserted into the context */
1254 } else if (strcmp(mod->revision, latest->revision) > 0) {
1255 mod->latest_revision = latest->latest_revision;
1256 /* the latest is zeroed later when the new module is being inserted into the context */
1257 } else {
1258 latest = NULL;
1259 }
1260 } else {
1261 latest = NULL;
1262 }
1263 } else {
1264 mod->latest_revision = 1;
1265 }
1266
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001267 if (custom_check) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001268 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), free_mod_cleanup);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001269 }
1270
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001271 /* check whether it is not already in the context in the same revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001272 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001273 if (implement) {
1274 mod_impl = ly_ctx_get_module_implemented(ctx, mod->name);
1275 if (mod_impl && (mod_impl != mod_dup)) {
1276 LOGERR(ctx, LY_EDENIED, "Module \"%s@%s\" is already implemented in the context.", mod_impl->name,
1277 mod_impl->revision ? mod_impl->revision : "<none>");
1278 ret = LY_EDENIED;
1279 goto free_mod_cleanup;
Radek Krejcid33273d2018-10-25 14:55:52 +02001280 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001281 }
1282 if (mod_dup) {
1283 if (implement) {
1284 if (!mod_dup->implemented) {
1285 /* just implement it */
1286 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod_dup, features, unres), free_mod_cleanup);
1287 goto free_mod_cleanup;
1288 }
1289
1290 /* nothing to do */
1291 LOGVRB("Module \"%s@%s\" is already implemented in the context.", mod_dup->name,
1292 mod_dup->revision ? mod_dup->revision : "<none>");
1293 goto free_mod_cleanup;
1294 }
1295
1296 /* nothing to do */
1297 LOGVRB("Module \"%s@%s\" is already present in the context.", mod_dup->name,
1298 mod_dup->revision ? mod_dup->revision : "<none>");
1299 goto free_mod_cleanup;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001300 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001301
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001302 switch (in->type) {
1303 case LY_IN_FILEPATH:
1304 /* check that name and revision match filename */
1305 filename = strrchr(in->method.fpath.filepath, '/');
1306 if (!filename) {
1307 filename = in->method.fpath.filepath;
1308 } else {
1309 filename++;
1310 }
1311 rev = strchr(filename, '@');
1312 dot = strrchr(filename, '.');
1313
1314 /* name */
1315 len = strlen(mod->name);
1316 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001317 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001318 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1319 }
1320 if (rev) {
1321 len = dot - ++rev;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001322 if (!mod->parsed->revs || (len != LY_REV_SIZE - 1) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001323 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001324 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001325 }
1326 }
1327
1328 break;
1329 case LY_IN_FD:
1330 case LY_IN_FILE:
1331 case LY_IN_MEMORY:
1332 /* nothing special to do */
1333 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001334 case LY_IN_ERROR:
1335 LOGINT(ctx);
1336 ret = LY_EINT;
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001337 goto free_mod_cleanup;
Radek Krejci096235c2019-01-11 11:12:19 +01001338 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001339 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001340
Michal Vasko7a0b0762020-09-02 16:37:01 +02001341 if (latest) {
1342 latest->latest_revision = 0;
1343 }
1344
Michal Vasko45b521c2020-11-04 17:14:39 +01001345 /* add internal data in case specific modules were parsed */
1346 if (!strcmp(mod->name, "ietf-netconf")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001347 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001348 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001349 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), free_mod_cleanup);
Michal Vasko45b521c2020-11-04 17:14:39 +01001350 }
1351
Michal Vasko405cc9e2020-12-01 12:01:27 +01001352 /* add the module into newly created module set, will also be freed from there on any error */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001353 LY_CHECK_GOTO(ret = ly_set_add(&unres->creating, mod, 1, NULL), free_mod_cleanup);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001354
Michal Vasko7a0b0762020-09-02 16:37:01 +02001355 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001356 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko405cc9e2020-12-01 12:01:27 +01001357 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001358 ctx->module_set_id++;
1359
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001360 /* resolve includes and all imports */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001361 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001362
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001363 /* check name collisions */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001364 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001365 /* TODO groupings */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001366 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), cleanup);
1367 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001368
1369 /* compile features */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001370 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001371
Michal Vasko89b5c072020-10-06 13:52:44 +02001372 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001373 /* pre-compile identities of the module */
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001374 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001375
1376 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001377 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001378 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001379 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001380 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001381 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001382 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001383 /* implement (compile) */
Michal Vasko405cc9e2020-12-01 12:01:27 +01001384 LY_CHECK_GOTO(ret = lys_set_implemented_r(mod, features, unres), cleanup);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001385 }
1386
Michal Vasko405cc9e2020-12-01 12:01:27 +01001387 /* success */
1388 goto cleanup;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001389
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001390free_mod_cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001391 lys_module_free(mod, NULL);
Michal Vasko0e02e8e2021-02-26 15:01:55 +01001392 if (ret) {
1393 mod = NULL;
1394 } else {
1395 /* return the existing module */
1396 assert(mod_dup);
1397 mod = mod_dup;
1398 }
Michal Vasko8a69a1b2020-12-03 14:19:02 +01001399
Michal Vasko405cc9e2020-12-01 12:01:27 +01001400cleanup:
Michal Vasko7a0b0762020-09-02 16:37:01 +02001401 if (pctx) {
1402 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1403 }
1404 if (format == LYS_IN_YANG) {
1405 yang_parser_ctx_free(yangctx);
1406 } else {
1407 yin_parser_ctx_free(yinctx);
1408 }
1409
Michal Vasko405cc9e2020-12-01 12:01:27 +01001410 if (!ret && module) {
1411 *module = mod;
1412 }
Michal Vasko7a0b0762020-09-02 16:37:01 +02001413 return ret;
1414}
1415
Radek Krejci545b4872020-11-15 10:15:12 +01001416static LYS_INFORMAT
1417lys_parse_get_format(const struct ly_in *in, LYS_INFORMAT format)
1418{
1419 if (!format && (in->type == LY_IN_FILEPATH)) {
1420 /* unknown format - try to detect it from filename's suffix */
1421 const char *path = in->method.fpath.filepath;
1422 size_t len = strlen(path);
1423
1424 /* ignore trailing whitespaces */
1425 for ( ; len > 0 && isspace(path[len - 1]); len--) {}
1426
Radek Krejcif13b87b2020-12-01 22:02:17 +01001427 if ((len >= LY_YANG_SUFFIX_LEN + 1) &&
1428 !strncmp(&path[len - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX, LY_YANG_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001429 format = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001430 } else if ((len >= LY_YIN_SUFFIX_LEN + 1) &&
1431 !strncmp(&path[len - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX, LY_YIN_SUFFIX_LEN)) {
Radek Krejci545b4872020-11-15 10:15:12 +01001432 format = LYS_IN_YIN;
1433 } /* else still unknown */
1434 }
1435
1436 return format;
1437}
1438
Michal Vasko7a0b0762020-09-02 16:37:01 +02001439API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001440lys_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 +02001441{
Michal Vasko405cc9e2020-12-01 12:01:27 +01001442 LY_ERR ret;
1443 struct lys_glob_unres unres = {0};
1444
Michal Vasko7a0b0762020-09-02 16:37:01 +02001445 if (module) {
1446 *module = NULL;
1447 }
Radek Krejci545b4872020-11-15 10:15:12 +01001448 LY_CHECK_ARG_RET(NULL, ctx, in, LY_EINVAL);
1449
1450 format = lys_parse_get_format(in, format);
1451 LY_CHECK_ARG_RET(ctx, format, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001452
1453 /* remember input position */
1454 in->func_start = in->current;
1455
Michal Vasko405cc9e2020-12-01 12:01:27 +01001456 ret = lys_create_module(ctx, in, format, 1, NULL, NULL, features, &unres, (struct lys_module **)module);
1457 LY_CHECK_GOTO(ret, cleanup);
1458
1459 /* resolve global unres */
1460 ret = lys_compile_unres_glob(ctx, &unres);
1461 LY_CHECK_GOTO(ret, cleanup);
1462
1463cleanup:
1464 if (ret) {
1465 lys_compile_unres_glob_revert(ctx, &unres);
1466 }
1467 lys_compile_unres_glob_erase(ctx, &unres);
1468 if (ret && module) {
1469 *module = NULL;
1470 }
1471 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001472}
1473
Michal Vasko3a41dff2020-07-15 14:30:28 +02001474API LY_ERR
1475lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001476{
Radek Krejci0f969882020-08-21 16:56:47 +02001477 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001478 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001479
Michal Vasko3a41dff2020-07-15 14:30:28 +02001480 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001481
Michal Vasko3a41dff2020-07-15 14:30:28 +02001482 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 +02001483
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001484 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001485 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001486
Michal Vasko3a41dff2020-07-15 14:30:28 +02001487 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001488}
1489
Michal Vasko3a41dff2020-07-15 14:30:28 +02001490API LY_ERR
1491lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001492{
Radek Krejci0f969882020-08-21 16:56:47 +02001493 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001494 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001495
Michal Vasko3a41dff2020-07-15 14:30:28 +02001496 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001497
Michal Vasko3a41dff2020-07-15 14:30:28 +02001498 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 +02001499
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001500 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001501 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001502
Michal Vasko3a41dff2020-07-15 14:30:28 +02001503 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001504}
1505
Michal Vasko3a41dff2020-07-15 14:30:28 +02001506API LY_ERR
1507lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001508{
Radek Krejci0f969882020-08-21 16:56:47 +02001509 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001510 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001511
Michal Vasko3a41dff2020-07-15 14:30:28 +02001512 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001513
Michal Vasko3a41dff2020-07-15 14:30:28 +02001514 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001515 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001516
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001517 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001518 ly_in_free(in, 0);
1519
Michal Vasko3a41dff2020-07-15 14:30:28 +02001520 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001521}
1522
1523API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001524lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001525 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001526{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001527 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001528 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001529 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001530 char *wd, *wn = NULL;
1531 DIR *dir = NULL;
1532 struct dirent *file;
1533 char *match_name = NULL;
1534 LYS_INFORMAT format_aux, match_format = 0;
1535 struct ly_set *dirs;
1536 struct stat st;
1537
1538 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1539
1540 /* start to fill the dir fifo with the context's search path (if set)
1541 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001542 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001543
1544 len = strlen(name);
1545 if (cwd) {
1546 wd = get_current_dir_name();
1547 if (!wd) {
1548 LOGMEM(NULL);
1549 goto cleanup;
1550 } else {
1551 /* add implicit current working directory (./) to be searched,
1552 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001553 ret = ly_set_add(dirs, wd, 0, NULL);
1554 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001555 implicit_cwd = 1;
1556 }
1557 }
1558 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001559 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001560 /* check for duplicities with the implicit current working directory */
1561 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1562 implicit_cwd = 0;
1563 continue;
1564 }
1565 wd = strdup(searchpaths[i]);
1566 if (!wd) {
1567 LOGMEM(NULL);
1568 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001569 } else {
1570 ret = ly_set_add(dirs, wd, 0, NULL);
1571 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001572 }
1573 }
1574 }
1575 wd = NULL;
1576
1577 /* start searching */
1578 while (dirs->count) {
1579 free(wd);
1580 free(wn); wn = NULL;
1581
1582 dirs->count--;
1583 wd = (char *)dirs->objs[dirs->count];
1584 dirs->objs[dirs->count] = NULL;
Radek Krejcieeee95c2021-01-19 10:57:22 +01001585 LOGVRB("Searching for \"%s\" in \"%s\".", name, wd);
Radek Krejcid33273d2018-10-25 14:55:52 +02001586
1587 if (dir) {
1588 closedir(dir);
1589 }
1590 dir = opendir(wd);
1591 dir_len = strlen(wd);
1592 if (!dir) {
1593 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1594 } else {
1595 while ((file = readdir(dir))) {
1596 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1597 /* skip . and .. */
1598 continue;
1599 }
1600 free(wn);
1601 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1602 LOGMEM(NULL);
1603 goto cleanup;
1604 }
1605 if (stat(wn, &st) == -1) {
1606 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001607 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001608 continue;
1609 }
1610 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1611 /* we have another subdirectory in searchpath to explore,
1612 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001613 ret = ly_set_add(dirs, wn, 0, NULL);
1614 LY_CHECK_GOTO(ret, cleanup);
1615
Radek Krejcid33273d2018-10-25 14:55:52 +02001616 /* continue with the next item in current directory */
1617 wn = NULL;
1618 continue;
1619 } else if (!S_ISREG(st.st_mode)) {
1620 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1621 continue;
1622 }
1623
1624 /* here we know that the item is a file which can contain a module */
1625 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001626 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001627 /* different filename than the module we search for */
1628 continue;
1629 }
1630
1631 /* get type according to filename suffix */
1632 flen = strlen(file->d_name);
Radek Krejcif13b87b2020-12-01 22:02:17 +01001633 if ((flen >= LY_YANG_SUFFIX_LEN + 1) &&
1634 !strcmp(&file->d_name[flen - LY_YANG_SUFFIX_LEN], LY_YANG_SUFFIX)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001635 format_aux = LYS_IN_YANG;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001636 } else if ((flen >= LY_YIN_SUFFIX_LEN + 1) &&
1637 !strcmp(&file->d_name[flen - LY_YIN_SUFFIX_LEN], LY_YIN_SUFFIX)) {
Radek Krejci01a937f2020-11-15 10:14:12 +01001638 format_aux = LYS_IN_YIN;
Radek Krejcid33273d2018-10-25 14:55:52 +02001639 } else {
1640 /* not supportde suffix/file format */
1641 continue;
1642 }
1643
1644 if (revision) {
1645 /* we look for the specific revision, try to get it from the filename */
1646 if (file->d_name[len] == '@') {
1647 /* check revision from the filename */
1648 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1649 /* another revision */
1650 continue;
1651 } else {
1652 /* exact revision */
1653 free(match_name);
1654 match_name = wn;
1655 wn = NULL;
1656 match_len = dir_len + 1 + len;
1657 match_format = format_aux;
1658 goto success;
1659 }
1660 } else {
1661 /* continue trying to find exact revision match, use this only if not found */
1662 free(match_name);
1663 match_name = wn;
1664 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001665 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001666 match_format = format_aux;
1667 continue;
1668 }
1669 } else {
1670 /* remember the revision and try to find the newest one */
1671 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001672 if ((file->d_name[len] != '@') ||
Radek Krejcif13b87b2020-12-01 22:02:17 +01001673 lysp_check_date(NULL, &file->d_name[len + 1],
1674 flen - ((format_aux == LYS_IN_YANG) ? LY_YANG_SUFFIX_LEN : LY_YIN_SUFFIX_LEN) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001675 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001676 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001677 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1678 continue;
1679 }
1680 free(match_name);
1681 }
1682
1683 match_name = wn;
1684 wn = NULL;
1685 match_len = dir_len + 1 + len;
1686 match_format = format_aux;
1687 continue;
1688 }
1689 }
1690 }
1691 }
1692
1693success:
1694 (*localfile) = match_name;
1695 match_name = NULL;
1696 if (format) {
1697 (*format) = match_format;
1698 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001699 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001700
1701cleanup:
1702 free(wn);
1703 free(wd);
1704 if (dir) {
1705 closedir(dir);
1706 }
1707 free(match_name);
1708 ly_set_free(dirs, free);
1709
1710 return ret;
1711}