blob: 3eb40b7e8450b1e791ec87c05fadf0684b3a37c3 [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
16
Radek Krejcica376bd2020-06-11 16:04:06 +020017#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020018
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <assert.h>
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010020#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020021#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdlib.h>
26#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020029
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020032#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
34#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020036#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020037#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020038#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020039#include "schema_compile.h"
40#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010041#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020042#include "set.h"
43#include "tree.h"
44#include "tree_schema_internal.h"
45#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020046
Michal Vaskof1ab44f2020-10-22 08:58:32 +020047API LY_ERR
48lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
49{
Michal Vasko1d972ca2020-11-03 17:16:56 +010050 struct lysc_node *elem, *elem2;
51 const struct lysc_action *acts;
52 const struct lysc_notif *notifs;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020053 LY_ARRAY_COUNT_TYPE u;
54
55 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
56
57 LYSC_TREE_DFS_BEGIN(root, elem) {
58 /* schema node */
59 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
60
Michal Vasko1d972ca2020-11-03 17:16:56 +010061 acts = lysc_node_actions(elem);
62 LY_ARRAY_FOR(acts, u) {
63 LYSC_TREE_DFS_BEGIN(&acts[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020064 /* action subtree */
65 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
66
Michal Vasko1d972ca2020-11-03 17:16:56 +010067 LYSC_TREE_DFS_END(&acts[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020068 }
69 }
70
Michal Vasko1d972ca2020-11-03 17:16:56 +010071 notifs = lysc_node_notifs(elem);
72 LY_ARRAY_FOR(notifs, u) {
73 LYSC_TREE_DFS_BEGIN(&notifs[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020074 /* notification subtree */
75 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
76
Michal Vasko1d972ca2020-11-03 17:16:56 +010077 LYSC_TREE_DFS_END(&notifs[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020078 }
79 }
80
81 LYSC_TREE_DFS_END(root, elem);
82 }
83
84 return LY_SUCCESS;
85}
86
87API LY_ERR
88lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
89{
90 LY_ARRAY_COUNT_TYPE u;
91
92 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
93
94 /* schema nodes */
95 LY_CHECK_RET(lysc_tree_dfs_full(mod->compiled->data, dfs_clb, data));
96
97 /* RPCs */
98 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
99 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
100 }
101
102 /* notifications */
103 LY_ARRAY_FOR(mod->compiled->notifs, u) {
104 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
105 }
106
107 return LY_SUCCESS;
108}
109
Radek Krejcib93bd412020-11-02 13:23:11 +0100110static void
111lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
112{
113 for (; first_case; first_case = (const struct lysc_node_case*)first_case->next) {
114 if (first_case->child) {
115 /* there is something to return */
116 (*next) = first_case->child;
117 return;
118 }
119 }
120
121 /* no children in choice's cases, so go to the choice's sibling instead of into it */
122 (*last) = (*next);
123 (*next) = (*next)->next;
124}
125
Radek Krejcia3045382018-11-22 14:30:31 +0100126API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200127lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100128{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100129 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100130 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200131 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100132 const struct lysc_action *actions;
133 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200134 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100135
136 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
137
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200138next:
Radek Krejcia3045382018-11-22 14:30:31 +0100139 if (!last) {
140 /* first call */
141
142 /* get know where to start */
143 if (parent) {
144 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200145 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200146 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100147 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100148 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100149 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100150 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100151 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200152 if (snode && *snode) {
153 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100154 }
Radek Krejcia3045382018-11-22 14:30:31 +0100155 }
Radek Krejcia3045382018-11-22 14:30:31 +0100156 } else {
157 /* top level data */
158 next = last = module->data;
159 }
160 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100161 /* try to get action or notification */
162 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100163 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100164 /* test if the next can be returned */
165 goto check;
166
Michal Vasko1bf09392020-03-27 12:38:10 +0100167 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100168 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100169 if (last->parent) {
170 actions = lysc_node_actions(last->parent);
171 } else {
172 actions = module->rpcs;
173 }
174 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200175 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100176 break;
177 }
178 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200179 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200180 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100181 }
182 goto repeat;
183 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100184 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100185 if (last->parent) {
186 notifs = lysc_node_notifs(last->parent);
187 } else {
188 notifs = module->notifs;
189 }
190 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200191 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100192 break;
193 }
194 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200195 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200196 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100197 }
198 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200199 } else {
200 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100201 }
202
Radek Krejcia3045382018-11-22 14:30:31 +0100203repeat:
204 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100205 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200206 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100207 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200208 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100209 } else if (!action_flag) {
210 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200211 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100212 } else if (!notif_flag) {
213 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200214 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100215 } else {
216 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100217 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100218 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100219 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100220check:
Radek Krejcia3045382018-11-22 14:30:31 +0100221 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100222 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100223 case LYS_ACTION:
224 case LYS_NOTIF:
225 case LYS_LEAF:
226 case LYS_ANYXML:
227 case LYS_ANYDATA:
228 case LYS_LIST:
229 case LYS_LEAFLIST:
230 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200231 case LYS_CASE:
232 if (options & LYS_GETNEXT_WITHCASE) {
233 break;
234 } else {
235 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100236 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200237 }
238 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100239 case LYS_CONTAINER:
240 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
241 if (((struct lysc_node_container *)next)->child) {
242 /* go into */
243 next = ((struct lysc_node_container *)next)->child;
244 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100245 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100246 next = next->next;
247 }
248 goto repeat;
249 }
250 break;
251 case LYS_CHOICE:
252 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200253 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100254 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
255 next = next->next;
256 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100257 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200258 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100259 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100260 /* go into */
261 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100262 }
Radek Krejcia3045382018-11-22 14:30:31 +0100263 }
264 goto repeat;
265 default:
266 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200267 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100268 return NULL;
269 }
270
Radek Krejcia3045382018-11-22 14:30:31 +0100271 return next;
272}
273
274API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100275lys_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 +0200276 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100277{
278 const struct lysc_node *node = NULL;
279
280 LY_CHECK_ARG_RET(NULL, module, name, NULL);
281 if (!nodetype) {
282 nodetype = 0xffff;
283 }
284
285 while ((node = lys_getnext(node, parent, module->compiled, options))) {
286 if (!(node->nodetype & nodetype)) {
287 continue;
288 }
289 if (node->module != module) {
290 continue;
291 }
292
293 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200294 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100295 return node;
296 }
297 } else {
298 if (!strcmp(node->name, name)) {
299 return node;
300 }
301 }
302 }
303 return NULL;
304}
305
Michal Vasko519fd602020-05-26 12:17:39 +0200306API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200307lys_find_xpath_atoms(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200308{
309 LY_ERR ret = LY_SUCCESS;
310 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200311 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200312 uint32_t i;
313
314 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
315 if (!(options & LYXP_SCNODE_ALL)) {
316 options = LYXP_SCNODE;
317 }
318
319 memset(&xp_set, 0, sizeof xp_set);
320
321 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200322 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
323 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200324
325 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200326 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200327 LY_CHECK_GOTO(ret, cleanup);
328
329 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200330 ret = ly_set_new(set);
331 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200332
333 /* transform into ly_set */
334 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
335 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
336 (*set)->size = xp_set.used;
337
338 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200339 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200340 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200341 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200342 }
343 }
344
345cleanup:
346 lyxp_set_free_content(&xp_set);
347 lyxp_expr_free(ctx_node->module->ctx, exp);
348 return ret;
349}
350
Michal Vasko072de482020-08-05 13:27:21 +0200351API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200352lys_find_path_atoms(const struct ly_path *path, struct ly_set **set)
353{
354 LY_ERR ret = LY_SUCCESS;
355 LY_ARRAY_COUNT_TYPE u, v;
356
357 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
358
359 /* allocate return set */
360 LY_CHECK_RET(ly_set_new(set));
361
362 LY_ARRAY_FOR(path, u) {
363 /* add nodes from the path */
364 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
365 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
366 LY_ARRAY_FOR(path[u].predicates, v) {
367 /* add all the keys in a predicate */
368 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
369 }
370 }
371 }
372
373cleanup:
374 if (ret) {
375 ly_set_free(*set, NULL);
376 *set = NULL;
377 }
378 return ret;
379}
380
381API LY_ERR
382lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
383 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
384{
385 LY_ERR ret = LY_SUCCESS;
386 struct lyxp_set xp_set = {0};
387 uint32_t i;
388
389 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
390 if (!(options & LYXP_SCNODE_ALL)) {
391 options = LYXP_SCNODE;
392 }
393
394 /* atomize expression */
395 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
396 LY_CHECK_GOTO(ret, cleanup);
397
398 /* allocate return set */
399 ret = ly_set_new(set);
400 LY_CHECK_GOTO(ret, cleanup);
401
402 /* transform into ly_set */
403 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
404 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
405 (*set)->size = xp_set.used;
406
407 for (i = 0; i < xp_set.used; ++i) {
408 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
409 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
410 LY_CHECK_GOTO(ret, cleanup);
411 }
412 }
413
414cleanup:
415 lyxp_set_free_content(&xp_set);
416 if (ret) {
417 ly_set_free(*set, NULL);
418 *set = NULL;
419 }
420 return ret;
421}
422
423API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200424lys_find_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko072de482020-08-05 13:27:21 +0200425{
426 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200427 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200428 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200429 uint32_t i;
430
431 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
432 if (!(options & LYXP_SCNODE_ALL)) {
433 options = LYXP_SCNODE;
434 }
435
Michal Vasko072de482020-08-05 13:27:21 +0200436 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200437 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
438 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200439
440 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200441 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200442 LY_CHECK_GOTO(ret, cleanup);
443
444 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200445 ret = ly_set_new(set);
446 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200447
448 /* transform into ly_set */
449 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
450 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
451 (*set)->size = xp_set.used;
452
453 for (i = 0; i < xp_set.used; ++i) {
454 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200455 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200456 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200457 }
458 }
459
460cleanup:
461 lyxp_set_free_content(&xp_set);
462 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200463 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200464 ly_set_free(*set, NULL);
465 *set = NULL;
466 }
Michal Vasko072de482020-08-05 13:27:21 +0200467 return ret;
468}
469
Michal Vasko14654712020-02-06 08:35:21 +0100470char *
471lysc_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 +0200472 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200473{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200475 char *path = NULL;
476 int len = 0;
477
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200478 LY_CHECK_ARG_RET(NULL, node, NULL);
479 if (buffer) {
480 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
481 }
482
Radek Krejci327de162019-06-14 12:52:07 +0200483 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200484 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200485 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100486 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200487 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100488 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200489
Michal Vasko65de0402020-08-03 16:34:19 +0200490 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
491 /* schema-only node */
492 continue;
493 }
494
Michal Vasko11deea12020-08-05 13:54:50 +0200495 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100497 if (parent && (iter->parent == parent)) {
498 slash = "";
499 } else {
500 slash = "/";
501 }
Michal Vasko69730152020-10-09 16:30:07 +0200502 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200503 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200504 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100505 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200506 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100507 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200508 }
Radek Krejci327de162019-06-14 12:52:07 +0200509 } else {
510 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200511 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100512 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200513 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100514 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200515 }
Radek Krejci327de162019-06-14 12:52:07 +0200516 }
517 free(s);
518 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200519
Michal Vasko69730152020-10-09 16:30:07 +0200520 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200521 /* not enough space in buffer */
522 break;
523 }
Radek Krejci327de162019-06-14 12:52:07 +0200524 }
525
526 if (len < 0) {
527 free(path);
528 path = NULL;
529 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200530 if (buffer) {
531 strcpy(buffer, "/");
532 } else {
533 path = strdup("/");
534 }
Radek Krejci327de162019-06-14 12:52:07 +0200535 }
536 break;
537 }
538
Radek Krejci1c0c3442019-07-23 16:08:47 +0200539 if (buffer) {
540 return buffer;
541 } else {
542 return path;
543 }
Radek Krejci327de162019-06-14 12:52:07 +0200544}
545
Michal Vasko14654712020-02-06 08:35:21 +0100546API char *
547lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
548{
549 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
550}
551
Michal Vasko28d78432020-05-26 13:10:53 +0200552API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200553lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200554{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200555 struct lysc_action *act;
556 struct lysc_notif *notif;
557
Radek Krejci19cf8052020-08-18 15:02:38 +0200558 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
559
Radek Krejciaf9cd802020-10-06 21:59:47 +0200560 switch (node->nodetype) {
561 case LYS_CONTAINER:
562 case LYS_CHOICE:
563 case LYS_CASE:
564 case LYS_LEAF:
565 case LYS_LEAFLIST:
566 case LYS_LIST:
567 case LYS_ANYXML:
568 case LYS_ANYDATA:
569 if (prev_priv_p) {
570 *prev_priv_p = node->priv;
571 }
572 ((struct lysc_node *)node)->priv = priv;
573 break;
574 case LYS_RPC:
575 case LYS_ACTION:
576 act = (struct lysc_action *)node;
577 if (prev_priv_p) {
578 *prev_priv_p = act->priv;
579 }
580 act->priv = priv;
581 break;
582 case LYS_NOTIF:
583 notif = (struct lysc_notif *)node;
584 if (prev_priv_p) {
585 *prev_priv_p = notif->priv;
586 }
587 notif->priv = priv;
588 break;
589 default:
590 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200591 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200592
593 return LY_SUCCESS;
594}
595
Michal Vasko89b5c072020-10-06 13:52:44 +0200596API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100597lys_set_implemented(struct lys_module *mod, const char **features)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200598{
Michal Vasko916aefb2020-11-02 15:43:16 +0100599 LY_ERR ret = LY_SUCCESS;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200600 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200601 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200602
603 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
604
605 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200606 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200607 return LY_SUCCESS;
608 }
609
610 /* we have module from the current context */
611 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
612 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200613 assert(m != mod);
614
615 /* check collision with other implemented revision */
616 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
617 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
618 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200619 }
620
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100621 /* enable features */
622 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
623
Michal Vasko89b5c072020-10-06 13:52:44 +0200624 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200625 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200626
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200627 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200628 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200629
630 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200631 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200632
Michal Vasko89b5c072020-10-06 13:52:44 +0200633 if (mod == mod->ctx->implementing.objs[0]) {
634 /* the first module being implemented, consolidate the set */
635 if (ret) {
636 /* failure, full compile revert */
637 for (i = 0; i < mod->ctx->list.count; ++i) {
638 m = mod->ctx->list.objs[i];
639 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
640 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200641
Michal Vasko89b5c072020-10-06 13:52:44 +0200642 /* make the module correctly non-implemented again */
643 m->implemented = 0;
644 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
645 lys_precompile_augments_deviations_revert(mod->ctx, m);
646 }
Michal Vasko89b5c072020-10-06 13:52:44 +0200647 }
Michal Vasko916aefb2020-11-02 15:43:16 +0100648
649 /* recompile, do not overwrite return value */
650 lys_recompile(mod->ctx, NULL);
Michal Vasko89b5c072020-10-06 13:52:44 +0200651 }
652
653 ly_set_erase(&mod->ctx->implementing, NULL);
654 }
655 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200656}
657
Michal Vasko7c8439f2020-08-05 13:25:19 +0200658static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100659lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200660{
661 struct lysp_import *imp;
662 struct lysp_include *inc;
663 LY_ARRAY_COUNT_TYPE u, v;
664
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100665 pmod->parsing = 1;
666 LY_ARRAY_FOR(pmod->imports, u) {
667 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200668 if (!imp->module) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100669 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, NULL, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200670 }
671 /* check for importing the same module twice */
672 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100673 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200674 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
675 }
676 }
677 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100678 LY_ARRAY_FOR(pmod->includes, u) {
679 inc = &pmod->includes[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200680 if (!inc->submodule) {
681 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
682 }
683 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100684 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200685
686 return LY_SUCCESS;
687}
688
Michal Vasko3a41dff2020-07-15 14:30:28 +0200689LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200690lys_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 +0200691 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200692 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200693{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200694 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100695 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100696 struct lys_yang_parser_ctx *yangctx = NULL;
697 struct lys_yin_parser_ctx *yinctx = NULL;
698 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100699
Michal Vasko3a41dff2020-07-15 14:30:28 +0200700 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100701
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100702 switch (format) {
703 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200704 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100705 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100706 break;
707 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200708 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100709 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100710 break;
711 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200712 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200713 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100714 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200715 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200716 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200717 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100718
719 /* make sure that the newest revision is at position 0 */
720 lysp_sort_revisions(submod->revs);
721
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100722 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200723 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100724 if (latest_sp) {
725 if (submod->revs) {
726 if (!latest_sp->revs) {
727 /* latest has no revision, so mod is anyway newer */
728 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200729 /* the latest_sp is zeroed later when the new module is being inserted into the context */
730 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
731 submod->latest_revision = latest_sp->latest_revision;
732 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200734 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100735 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200736 } else {
737 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 }
739 } else {
740 submod->latest_revision = 1;
741 }
742
Radek Krejcib3289d62019-09-18 12:21:39 +0200743 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200744 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200745 }
746
747 if (latest_sp) {
748 latest_sp->latest_revision = 0;
749 }
750
Michal Vasko7a0b0762020-09-02 16:37:01 +0200751 lys_parser_fill_filepath(ctx, in, &submod->filepath);
752
Michal Vasko7c8439f2020-08-05 13:25:19 +0200753 /* resolve imports and includes */
754 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
755
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100756 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100757 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
758 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100759
David Sedlák1b623122019-08-05 15:27:49 +0200760 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100761 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200762 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100763 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200764 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200765 *submodule = submod;
766 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200767
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100768error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200769 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200770 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100771 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200772 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100773 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200774 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200775 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200776}
777
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200779lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200780 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100781 void *check_data, const char **features, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200782{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100783 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200784 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200785 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200786 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100787 struct lys_yang_parser_ctx *yangctx = NULL;
788 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200789 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200790 char *filename, *rev, *dot;
791 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +0200792
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100793 LY_CHECK_ARG_RET(ctx, ctx, in, !features || implement, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200794 if (module) {
795 *module = NULL;
796 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200797
798 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200799 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100800 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200801
802 switch (format) {
803 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200804 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100805 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200806 break;
807 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200808 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100809 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200810 break;
811 default:
812 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200813 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200814 break;
815 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200816 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200817
818 /* make sure that the newest revision is at position 0 */
819 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100820 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200821 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100822 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200823
Radek Krejcib3289d62019-09-18 12:21:39 +0200824 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +0200825 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +0200826 if (latest) {
827 if (mod->revision) {
828 if (!latest->revision) {
829 /* latest has no revision, so mod is anyway newer */
830 mod->latest_revision = latest->latest_revision;
831 /* the latest is zeroed later when the new module is being inserted into the context */
832 } else if (strcmp(mod->revision, latest->revision) > 0) {
833 mod->latest_revision = latest->latest_revision;
834 /* the latest is zeroed later when the new module is being inserted into the context */
835 } else {
836 latest = NULL;
837 }
838 } else {
839 latest = NULL;
840 }
841 } else {
842 mod->latest_revision = 1;
843 }
844
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200846 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100847 }
848
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100849 /* check for duplicity in the context */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100850 if (implement && ly_ctx_get_module_implemented(ctx, mod->name)) {
851 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
852 ret = LY_EDENIED;
853 goto error;
854 }
Michal Vasko22df3f02020-08-24 13:29:22 +0200855 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100856 if (mod_dup) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100857 if (mod->parsed->revs) {
858 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
859 mod->name, mod->parsed->revs[0].date);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100860 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100861 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
862 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200863 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100864 ret = LY_EEXIST;
865 goto error;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100866 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200867
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200868 switch (in->type) {
869 case LY_IN_FILEPATH:
870 /* check that name and revision match filename */
871 filename = strrchr(in->method.fpath.filepath, '/');
872 if (!filename) {
873 filename = in->method.fpath.filepath;
874 } else {
875 filename++;
876 }
877 rev = strchr(filename, '@');
878 dot = strrchr(filename, '.');
879
880 /* name */
881 len = strlen(mod->name);
882 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +0200883 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200884 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
885 }
886 if (rev) {
887 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +0200888 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200889 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +0200890 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200891 }
892 }
893
894 break;
895 case LY_IN_FD:
896 case LY_IN_FILE:
897 case LY_IN_MEMORY:
898 /* nothing special to do */
899 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200900 case LY_IN_ERROR:
901 LOGINT(ctx);
902 ret = LY_EINT;
903 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +0100904 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200905 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200906
Michal Vasko7a0b0762020-09-02 16:37:01 +0200907 if (latest) {
908 latest->latest_revision = 0;
909 }
910
911 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +0200912 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200913 LY_CHECK_GOTO(ret, error);
914 ctx->module_set_id++;
915
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100916 /* resolve includes and all imports */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200917 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
918
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100919 /* check name collisions */
920 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), error_ctx);
921 /* TODO groupings */
922 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), error_ctx);
923 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), error_ctx);
924
925 /* compile features */
926 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), error_ctx);
927
Michal Vasko89b5c072020-10-06 13:52:44 +0200928 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100929 /* pre-compile identities of the module */
930 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
931
932 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +0200933 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200934 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200935 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
936 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200937 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100938 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +0200939 /* implement (compile) */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100940 LY_CHECK_GOTO(ret = lys_set_implemented(mod, features), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200941 }
942
943 if (format == LYS_IN_YANG) {
944 yang_parser_ctx_free(yangctx);
945 } else {
946 yin_parser_ctx_free(yinctx);
947 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200948 if (module) {
949 *module = mod;
950 }
951 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200952
953error_ctx:
954 ly_set_rm(&ctx->list, mod, NULL);
955error:
956 lys_module_free(mod, NULL);
957 if (pctx) {
958 ly_set_erase(&pctx->tpdfs_nodes, NULL);
959 }
960 if (format == LYS_IN_YANG) {
961 yang_parser_ctx_free(yangctx);
962 } else {
963 yin_parser_ctx_free(yinctx);
964 }
965
966 return ret;
967}
968
969API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100970lys_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 +0200971{
972 if (module) {
973 *module = NULL;
974 }
975 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
976
977 /* remember input position */
978 in->func_start = in->current;
979
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100980 return lys_create_module(ctx, in, format, 1, NULL, NULL, features, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +0200981}
982
Michal Vasko3a41dff2020-07-15 14:30:28 +0200983API LY_ERR
984lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200985{
Radek Krejci0f969882020-08-21 16:56:47 +0200986 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200987 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200988
Michal Vasko3a41dff2020-07-15 14:30:28 +0200989 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +0100990
Michal Vasko3a41dff2020-07-15 14:30:28 +0200991 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 +0200992
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100993 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200994 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200995
Michal Vasko3a41dff2020-07-15 14:30:28 +0200996 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100997}
998
Michal Vasko3a41dff2020-07-15 14:30:28 +0200999API LY_ERR
1000lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001001{
Radek Krejci0f969882020-08-21 16:56:47 +02001002 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001003 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001004
Michal Vasko3a41dff2020-07-15 14:30:28 +02001005 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001006
Michal Vasko3a41dff2020-07-15 14:30:28 +02001007 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 +02001008
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001009 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001010 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001011
Michal Vasko3a41dff2020-07-15 14:30:28 +02001012 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001013}
1014
Michal Vasko3a41dff2020-07-15 14:30:28 +02001015API LY_ERR
1016lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001017{
Radek Krejci0f969882020-08-21 16:56:47 +02001018 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001019 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001020
Michal Vasko3a41dff2020-07-15 14:30:28 +02001021 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001022
Michal Vasko3a41dff2020-07-15 14:30:28 +02001023 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001024 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001025
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001026 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001027 ly_in_free(in, 0);
1028
Michal Vasko3a41dff2020-07-15 14:30:28 +02001029 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001030}
1031
1032API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001033lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001034 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001035{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001036 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001037 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001038 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001039 char *wd, *wn = NULL;
1040 DIR *dir = NULL;
1041 struct dirent *file;
1042 char *match_name = NULL;
1043 LYS_INFORMAT format_aux, match_format = 0;
1044 struct ly_set *dirs;
1045 struct stat st;
1046
1047 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1048
1049 /* start to fill the dir fifo with the context's search path (if set)
1050 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001051 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001052
1053 len = strlen(name);
1054 if (cwd) {
1055 wd = get_current_dir_name();
1056 if (!wd) {
1057 LOGMEM(NULL);
1058 goto cleanup;
1059 } else {
1060 /* add implicit current working directory (./) to be searched,
1061 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001062 ret = ly_set_add(dirs, wd, 0, NULL);
1063 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001064 implicit_cwd = 1;
1065 }
1066 }
1067 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001068 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001069 /* check for duplicities with the implicit current working directory */
1070 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1071 implicit_cwd = 0;
1072 continue;
1073 }
1074 wd = strdup(searchpaths[i]);
1075 if (!wd) {
1076 LOGMEM(NULL);
1077 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001078 } else {
1079 ret = ly_set_add(dirs, wd, 0, NULL);
1080 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001081 }
1082 }
1083 }
1084 wd = NULL;
1085
1086 /* start searching */
1087 while (dirs->count) {
1088 free(wd);
1089 free(wn); wn = NULL;
1090
1091 dirs->count--;
1092 wd = (char *)dirs->objs[dirs->count];
1093 dirs->objs[dirs->count] = NULL;
1094 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1095
1096 if (dir) {
1097 closedir(dir);
1098 }
1099 dir = opendir(wd);
1100 dir_len = strlen(wd);
1101 if (!dir) {
1102 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1103 } else {
1104 while ((file = readdir(dir))) {
1105 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1106 /* skip . and .. */
1107 continue;
1108 }
1109 free(wn);
1110 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1111 LOGMEM(NULL);
1112 goto cleanup;
1113 }
1114 if (stat(wn, &st) == -1) {
1115 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001116 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001117 continue;
1118 }
1119 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1120 /* we have another subdirectory in searchpath to explore,
1121 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001122 ret = ly_set_add(dirs, wn, 0, NULL);
1123 LY_CHECK_GOTO(ret, cleanup);
1124
Radek Krejcid33273d2018-10-25 14:55:52 +02001125 /* continue with the next item in current directory */
1126 wn = NULL;
1127 continue;
1128 } else if (!S_ISREG(st.st_mode)) {
1129 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1130 continue;
1131 }
1132
1133 /* here we know that the item is a file which can contain a module */
1134 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001135 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001136 /* different filename than the module we search for */
1137 continue;
1138 }
1139
1140 /* get type according to filename suffix */
1141 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001142 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001143 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001144 /* TODO YIN parser
1145 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1146 format_aux = LYS_IN_YIN;
1147 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001148 } else {
1149 /* not supportde suffix/file format */
1150 continue;
1151 }
1152
1153 if (revision) {
1154 /* we look for the specific revision, try to get it from the filename */
1155 if (file->d_name[len] == '@') {
1156 /* check revision from the filename */
1157 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1158 /* another revision */
1159 continue;
1160 } else {
1161 /* exact revision */
1162 free(match_name);
1163 match_name = wn;
1164 wn = NULL;
1165 match_len = dir_len + 1 + len;
1166 match_format = format_aux;
1167 goto success;
1168 }
1169 } else {
1170 /* continue trying to find exact revision match, use this only if not found */
1171 free(match_name);
1172 match_name = wn;
1173 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001174 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001175 match_format = format_aux;
1176 continue;
1177 }
1178 } else {
1179 /* remember the revision and try to find the newest one */
1180 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001181 if ((file->d_name[len] != '@') ||
1182 lysp_check_date(NULL, &file->d_name[len + 1], flen - ((format_aux == LYS_IN_YANG) ? 5 : 4) - len - 1, NULL)) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001183 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001184 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001185 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1186 continue;
1187 }
1188 free(match_name);
1189 }
1190
1191 match_name = wn;
1192 wn = NULL;
1193 match_len = dir_len + 1 + len;
1194 match_format = format_aux;
1195 continue;
1196 }
1197 }
1198 }
1199 }
1200
1201success:
1202 (*localfile) = match_name;
1203 match_name = NULL;
1204 if (format) {
1205 (*format) = match_format;
1206 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001207 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001208
1209cleanup:
1210 free(wn);
1211 free(wd);
1212 if (dir) {
1213 closedir(dir);
1214 }
1215 free(match_name);
1216 ly_set_free(dirs, free);
1217
1218 return ret;
1219}