blob: a840121d3c2abfc9cdbd75d231456672032e24f6 [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>
Radek Krejcid33273d2018-10-25 14:55:52 +020020#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020021#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020023#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include <stdlib.h>
25#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020026#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020028
Radek Krejcica376bd2020-06-11 16:04:06 +020029#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020030#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020031#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "dict.h"
33#include "log.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020034#include "parser.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020035#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020036#include "parser_schema.h"
37#include "set.h"
38#include "tree.h"
39#include "tree_schema_internal.h"
40#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020041
Radek Krejcia3045382018-11-22 14:30:31 +010042API const struct lysc_node *
43lys_getnext(const struct lysc_node *last, const struct lysc_node *parent, const struct lysc_module *module, int options)
44{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010045 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010046 struct lysc_node **snode;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010047 int action_flag = 0, notif_flag = 0;
48 const struct lysc_action *actions;
49 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020050 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +010051
52 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
53
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020054next:
Radek Krejcia3045382018-11-22 14:30:31 +010055 if (!last) {
56 /* first call */
57
58 /* get know where to start */
59 if (parent) {
60 /* schema subtree */
Radek Krejci056d0a82018-12-06 16:57:25 +010061 if (parent->nodetype == LYS_CHOICE && (options & LYS_GETNEXT_WITHCASE)) {
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020062 if (((struct lysc_node_choice*)parent)->cases) {
63 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010064 }
Radek Krejci056d0a82018-12-06 16:57:25 +010065 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010066 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010067 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020068 if (snode && *snode) {
69 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010070 }
Radek Krejcia3045382018-11-22 14:30:31 +010071 }
Radek Krejcia3045382018-11-22 14:30:31 +010072 } else {
73 /* top level data */
74 next = last = module->data;
75 }
76 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010077 /* try to get action or notification */
78 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010079 }
Radek Krejci05b774b2019-02-25 13:26:18 +010080 /* test if the next can be returned */
81 goto check;
82
Michal Vasko1bf09392020-03-27 12:38:10 +010083 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010084 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010085 if (last->parent) {
86 actions = lysc_node_actions(last->parent);
87 } else {
88 actions = module->rpcs;
89 }
90 LY_ARRAY_FOR(actions, u) {
91 if (&actions[u] == (struct lysc_action*)last) {
92 break;
93 }
94 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +020095 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010096 next = (struct lysc_node*)(&actions[u + 1]);
97 }
98 goto repeat;
99 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100100 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100101 if (last->parent) {
102 notifs = lysc_node_notifs(last->parent);
103 } else {
104 notifs = module->notifs;
105 }
106 LY_ARRAY_FOR(notifs, u) {
107 if (&notifs[u] == (struct lysc_notif*)last) {
108 break;
109 }
110 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100112 next = (struct lysc_node*)(&notifs[u + 1]);
113 }
114 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100115 }
116
117 next = last->next;
118repeat:
Radek Krejci01342af2019-01-03 15:18:08 +0100119 if (next && parent && parent->nodetype == LYS_CASE && next->parent != parent) {
120 /* inside case (as an explicit parent, not when diving into it from choice),
121 * limit the list of children only to the specific case */
122 next = NULL;
123 }
Radek Krejcia3045382018-11-22 14:30:31 +0100124 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100125 /* possibly go back to parent */
Radek Krejci05b774b2019-02-25 13:26:18 +0100126 if (last && last->parent != parent) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100127 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200128 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100129 } else if (!action_flag) {
130 action_flag = 1;
131 next = parent ? (struct lysc_node*)lysc_node_actions(parent) : (struct lysc_node*)module->rpcs;
132 } else if (!notif_flag) {
133 notif_flag = 1;
134 next = parent ? (struct lysc_node*)lysc_node_notifs(parent) : (struct lysc_node*)module->notifs;
135 } else {
136 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100137 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100138 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100139 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100140check:
Radek Krejcia3045382018-11-22 14:30:31 +0100141 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100142 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100143 case LYS_ACTION:
144 case LYS_NOTIF:
145 case LYS_LEAF:
146 case LYS_ANYXML:
147 case LYS_ANYDATA:
148 case LYS_LIST:
149 case LYS_LEAFLIST:
Radek Krejcia9026eb2018-12-12 16:04:47 +0100150 case LYS_CASE:
Radek Krejcia3045382018-11-22 14:30:31 +0100151 break;
152 case LYS_CONTAINER:
153 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
154 if (((struct lysc_node_container *)next)->child) {
155 /* go into */
156 next = ((struct lysc_node_container *)next)->child;
157 } else {
158 next = next->next;
159 }
160 goto repeat;
161 }
162 break;
163 case LYS_CHOICE:
164 if (options & LYS_GETNEXT_WITHCHOICE) {
165 return next;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100166 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
167 next = next->next;
168 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100169 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100170 if (options & LYS_GETNEXT_WITHCASE) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100171 next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100172 } else {
173 next = ((struct lysc_node_choice *)next)->cases->child;
174 }
Radek Krejcia3045382018-11-22 14:30:31 +0100175 }
176 goto repeat;
177 default:
178 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200179 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100180 return NULL;
181 }
182
183 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
184 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200185 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100186 next = next->next;
187 goto repeat;
188 }
189 }
190 return next;
191}
192
193API const struct lysc_node *
Radek Krejci09e8d0a2019-11-17 12:14:15 +0800194lys_find_node(struct ly_ctx *ctx, const struct lysc_node *context_node, const char *qpath)
195{
196 const char *id = qpath;
197 const char *prefix, *name;
198 size_t prefix_len, name_len;
199 unsigned int u;
200 const struct lysc_node *node = context_node;
201 struct lys_module *mod = NULL;
202
203 LY_CHECK_ARG_RET(ctx, qpath, NULL);
204
205 while(*id) {
206 if (id[0] == '/') {
207 ++id;
208 }
209 /* precess ".." in relative paths */
210 while (!strncmp("../", id, 3)) {
211 id += 3;
212 if (!node) {
213 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - too many \"..\" in the path.", qpath);
214 return NULL;
215 }
216 node = node->parent;
217 }
218
219 if (ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len) != LY_SUCCESS) {
220 LOGERR(ctx, LY_EINVAL, "Invalid qpath \"%s\" - invalid nodeid \"%.*s\".", qpath, id- qpath, qpath);
221 return NULL;
222 }
223 if (prefix) {
224 if (context_node) {
225 mod = lys_module_find_prefix(context_node->module, prefix, prefix_len);
226 } else {
227 for (u = 0; u < ctx->list.count; ++u) {
228 if (!ly_strncmp(((struct lys_module *)ctx->list.objs[u])->name, prefix, prefix_len)) {
229 struct lys_module *m = (struct lys_module *)ctx->list.objs[u];
230 if (mod) {
231 if (m->implemented) {
232 mod = m;
233 break;
234 } else if (m->latest_revision) {
235 mod = m;
236 }
237 } else {
238 mod = m;
239 }
240 }
241 }
242 }
243 }
244 if (!mod) {
245 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find module connected with the prefix of the node \"%.*s\".",
246 id - qpath, qpath);
247 return NULL;
248 }
249
250 node = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
251 if (!node) {
252 LOGERR(ctx, LY_EINVAL, "Invalid qpath - unable to find \"%.*s\".", id - qpath, qpath);
253 return NULL;
254 }
255 }
256
257 return node;
258}
259
260API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100261lys_find_child(const struct lysc_node *parent, const struct lys_module *module, const char *name, size_t name_len,
262 uint16_t nodetype, int options)
Radek Krejcia3045382018-11-22 14:30:31 +0100263{
264 const struct lysc_node *node = NULL;
265
266 LY_CHECK_ARG_RET(NULL, module, name, NULL);
267 if (!nodetype) {
268 nodetype = 0xffff;
269 }
270
271 while ((node = lys_getnext(node, parent, module->compiled, options))) {
272 if (!(node->nodetype & nodetype)) {
273 continue;
274 }
275 if (node->module != module) {
276 continue;
277 }
278
279 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200280 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100281 return node;
282 }
283 } else {
284 if (!strcmp(node->name, name)) {
285 return node;
286 }
287 }
288 }
289 return NULL;
290}
291
Michal Vasko519fd602020-05-26 12:17:39 +0200292API LY_ERR
293lys_atomize_xpath(const struct lysc_node *ctx_node, const char *xpath, int options, struct ly_set **set)
294{
295 LY_ERR ret = LY_SUCCESS;
296 struct lyxp_set xp_set;
297 struct lyxp_expr *exp;
298 uint32_t i;
299
300 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
301 if (!(options & LYXP_SCNODE_ALL)) {
302 options = LYXP_SCNODE;
303 }
304
305 memset(&xp_set, 0, sizeof xp_set);
306
307 /* compile expression */
Michal Vasko004d3152020-06-11 19:59:22 +0200308 exp = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1);
Michal Vasko519fd602020-05-26 12:17:39 +0200309 LY_CHECK_ERR_GOTO(!exp, ret = LY_EINVAL, cleanup);
310
311 /* atomize expression */
312 ret = lyxp_atomize(exp, LYD_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &xp_set, options);
313 LY_CHECK_GOTO(ret, cleanup);
314
315 /* allocate return set */
316 *set = ly_set_new();
317 LY_CHECK_ERR_GOTO(!*set, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
318
319 /* transform into ly_set */
320 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
321 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
322 (*set)->size = xp_set.used;
323
324 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200325 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko519fd602020-05-26 12:17:39 +0200326 ly_set_add(*set, xp_set.val.scnodes[i].scnode, LY_SET_OPT_USEASLIST);
327 }
328 }
329
330cleanup:
331 lyxp_set_free_content(&xp_set);
332 lyxp_expr_free(ctx_node->module->ctx, exp);
333 return ret;
334}
335
Michal Vasko14654712020-02-06 08:35:21 +0100336char *
337lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LYSC_PATH_TYPE pathtype, char *buffer,
338 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200339{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200341 char *path = NULL;
342 int len = 0;
343
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200344 LY_CHECK_ARG_RET(NULL, node, NULL);
345 if (buffer) {
346 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
347 }
348
Radek Krejci327de162019-06-14 12:52:07 +0200349 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 case LYSC_PATH_LOG:
Michal Vasko90932a92020-02-12 14:33:03 +0100351 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200352 char *s = buffer ? strdup(buffer) : path;
Radek Krejci327de162019-06-14 12:52:07 +0200353 char *id;
Michal Vasko14654712020-02-06 08:35:21 +0100354 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200355
Michal Vasko03ff5a72019-09-11 13:49:33 +0200356 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100357 if (parent && (iter->parent == parent)) {
358 slash = "";
359 } else {
360 slash = "/";
361 }
Radek Krejci327de162019-06-14 12:52:07 +0200362 if (!iter->parent || iter->parent->module != iter->module) {
363 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200364 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100365 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200366 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100367 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200368 }
Radek Krejci327de162019-06-14 12:52:07 +0200369 } else {
370 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200371 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100372 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200373 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100374 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200375 }
Radek Krejci327de162019-06-14 12:52:07 +0200376 }
377 free(s);
378 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200379
380 if (buffer && buflen <= (size_t)len) {
381 /* not enough space in buffer */
382 break;
383 }
Radek Krejci327de162019-06-14 12:52:07 +0200384 }
385
386 if (len < 0) {
387 free(path);
388 path = NULL;
389 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200390 if (buffer) {
391 strcpy(buffer, "/");
392 } else {
393 path = strdup("/");
394 }
Radek Krejci327de162019-06-14 12:52:07 +0200395 }
396 break;
397 }
398
Radek Krejci1c0c3442019-07-23 16:08:47 +0200399 if (buffer) {
400 return buffer;
401 } else {
402 return path;
403 }
Radek Krejci327de162019-06-14 12:52:07 +0200404}
405
Michal Vasko14654712020-02-06 08:35:21 +0100406API char *
407lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
408{
409 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
410}
411
Michal Vasko28d78432020-05-26 13:10:53 +0200412API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100413lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200414{
Michal Vasko28d78432020-05-26 13:10:53 +0200415 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
416 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200417}
418
Radek Krejci693262f2019-04-29 15:23:20 +0200419uint8_t
420lysc_iff_getop(uint8_t *list, int pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200421{
422 uint8_t *item;
423 uint8_t mask = 3, result;
424
425 assert(pos >= 0);
426
427 item = &list[pos / 4];
428 result = (*item) & (mask << 2 * (pos % 4));
429 return result >> 2 * (pos % 4);
430}
431
Michal Vasko28d78432020-05-26 13:10:53 +0200432static LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200433lysc_iffeature_value_(const struct lysc_iffeature *iff, int *index_e, int *index_f)
434{
435 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200436 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200437
Radek Krejci693262f2019-04-29 15:23:20 +0200438 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200439 (*index_e)++;
440
441 switch (op) {
442 case LYS_IFF_F:
443 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200444 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200445 case LYS_IFF_NOT:
446 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200447 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200448 case LYS_IFF_AND:
449 case LYS_IFF_OR:
450 a = lysc_iffeature_value_(iff, index_e, index_f);
451 b = lysc_iffeature_value_(iff, index_e, index_f);
452 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200453 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
454 return LY_SUCCESS;
455 } else {
456 return LY_ENOT;
457 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200458 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200459 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
460 return LY_SUCCESS;
461 } else {
462 return LY_ENOT;
463 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200464 }
465 }
466
467 return 0;
468}
469
Michal Vasko28d78432020-05-26 13:10:53 +0200470API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200471lysc_iffeature_value(const struct lysc_iffeature *iff)
472{
473 int index_e = 0, index_f = 0;
474
475 LY_CHECK_ARG_RET(NULL, iff, -1);
476
477 if (iff->expr) {
478 return lysc_iffeature_value_(iff, &index_e, &index_f);
479 }
480 return 0;
481}
482
Radek Krejci151a5b72018-10-19 14:21:44 +0200483/**
484 * @brief Enable/Disable the specified feature in the module.
485 *
486 * If the feature is already set to the desired value, LY_SUCCESS is returned.
487 * By changing the feature, also all the feature which depends on it via their
488 * if-feature statements are again evaluated (disabled if a if-feature statemen
489 * evaluates to false).
490 *
Radek Krejci0af46292019-01-11 16:02:31 +0100491 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200492 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
493 * set all the features in the module.
494 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
495 * @return LY_ERR value.
496 */
497static LY_ERR
Michal Vasko82c31e62020-07-17 15:30:40 +0200498lys_feature_change(const struct lys_module *mod, const char *name, int value, int skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200499{
500 int all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200501 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200502 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200503 struct lysc_feature *f, **df;
504 struct lysc_iffeature *iff;
505 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100506 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200507
Radek Krejci6e67c402019-05-02 09:55:39 +0200508 if (!strcmp(name, "*")) {
509 /* enable all */
510 all = 1;
511 }
512
Radek Krejci0af46292019-01-11 16:02:31 +0100513 if (!mod->compiled) {
514 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
515 mod->name);
516 return LY_EINVAL;
517 }
518 if (!mod->compiled->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200519 if (all) {
520 /* no feature to enable */
521 return LY_SUCCESS;
522 }
Radek Krejci0af46292019-01-11 16:02:31 +0100523 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200524 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200525 }
526
Radek Krejci151a5b72018-10-19 14:21:44 +0200527 changed = ly_set_new();
Radek Krejcica3db002018-11-01 10:31:01 +0100528 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200529
Radek Krejcica3db002018-11-01 10:31:01 +0100530run:
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200531 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->compiled->features); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +0100532 f = &mod->compiled->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200533 if (all || !strcmp(f->name, name)) {
534 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
535 if (all) {
536 /* skip already set features */
537 continue;
538 } else {
539 /* feature already set correctly */
540 ly_set_free(changed, NULL);
541 return LY_SUCCESS;
542 }
543 }
544
545 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200546 if (!skip_checks) {
547 /* check referenced features if they are enabled */
548 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
549 if (lysc_iffeature_value(iff) == LY_ENOT) {
550 if (all) {
551 ++disabled_count;
552 goto next;
553 } else {
554 LOGERR(ctx, LY_EDENIED,
555 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
556 f->name);
557 ly_set_free(changed, NULL);
558 return LY_EDENIED;
559 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200560 }
561 }
562 }
563 /* enable the feature */
564 f->flags |= LYS_FENABLED;
565 } else { /* disable */
566 /* disable the feature */
567 f->flags &= ~LYS_FENABLED;
568 }
569
570 /* remember the changed feature */
571 ly_set_add(changed, f, LY_SET_OPT_USEASLIST);
572
573 if (!all) {
574 /* stop in case changing a single feature */
575 break;
576 }
577 }
578next:
579 ;
580 }
581
582 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100583 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Radek Krejci151a5b72018-10-19 14:21:44 +0200584 ly_set_free(changed, NULL);
Michal Vasko82c31e62020-07-17 15:30:40 +0200585 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200586 }
587
Radek Krejcica3db002018-11-01 10:31:01 +0100588 if (value && all && disabled_count) {
589 if (changed_count == changed->count) {
590 /* no change in last run -> not able to enable all ... */
591 /* ... print errors */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200592 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->compiled->features); ++u) {
Radek Krejci0af46292019-01-11 16:02:31 +0100593 if (!(mod->compiled->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100594 LOGERR(ctx, LY_EDENIED,
Radek Krejcica3db002018-11-01 10:31:01 +0100595 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
Radek Krejci0af46292019-01-11 16:02:31 +0100596 mod->compiled->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100597 --disabled_count;
598 }
599 }
600 /* ... restore the original state */
601 for (u = 0; u < changed->count; ++u) {
602 f = changed->objs[u];
603 /* re-disable the feature */
604 f->flags &= ~LYS_FENABLED;
605 }
606
607 ly_set_free(changed, NULL);
608 return LY_EDENIED;
609 } else {
610 /* we did some change in last run, try it again */
611 changed_count = changed->count;
612 goto run;
613 }
614 }
615
Radek Krejci151a5b72018-10-19 14:21:44 +0200616 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200617 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200618 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
619 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
620 * is not done - by default, features are disabled and must be explicitely enabled. */
621 f = changed->objs[u];
622 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature*, df) {
623 if (!((*df)->flags & LYS_FENABLED)) {
624 /* not enabled, nothing to do */
625 continue;
626 }
627 /* check the feature's if-features which could change by the previous change of our feature */
628 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200629 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200630 /* the feature must be disabled now */
631 (*df)->flags &= ~LYS_FENABLED;
632 /* add the feature into the list of changed features */
633 ly_set_add(changed, *df, LY_SET_OPT_USEASLIST);
634 break;
635 }
636 }
637 }
638 }
639
640 ly_set_free(changed, NULL);
641 return LY_SUCCESS;
642}
643
644API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200645lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200646{
Radek Krejci0af46292019-01-11 16:02:31 +0100647 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200648
Michal Vasko82c31e62020-07-17 15:30:40 +0200649 return lys_feature_change((struct lys_module*)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200650}
651
652API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200653lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200654{
Radek Krejci0af46292019-01-11 16:02:31 +0100655 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200656
Michal Vasko82c31e62020-07-17 15:30:40 +0200657 return lys_feature_change((struct lys_module*)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200658}
659
Michal Vasko82c31e62020-07-17 15:30:40 +0200660API LY_ERR
661lys_feature_enable_force(const struct lys_module *module, const char *feature)
662{
663 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
664
665 return lys_feature_change((struct lys_module*)module, feature, 1, 1);
666}
667
668API LY_ERR
669lys_feature_disable_force(const struct lys_module *module, const char *feature)
670{
671 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
672
673 return lys_feature_change((struct lys_module*)module, feature, 0, 1);
674}
675
676API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200677lys_feature_value(const struct lys_module *module, const char *feature)
678{
Michal Vasko82c31e62020-07-17 15:30:40 +0200679 struct lysc_feature *f = NULL;
Radek Krejci151a5b72018-10-19 14:21:44 +0200680 struct lysc_module *mod;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200681 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200682
683 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
684 mod = module->compiled;
685
686 /* search for the specified feature */
Michal Vasko82c31e62020-07-17 15:30:40 +0200687 LY_ARRAY_FOR(mod->features, u) {
Radek Krejci2c4e7172018-10-19 15:56:26 +0200688 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200689 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200690 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200691 }
692 }
693
694 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200695 if (!f) {
696 return LY_ENOTFOUND;
697 }
698
699 /* feature disabled */
700 if (!(f->flags & LYS_FENABLED)) {
701 return LY_ENOT;
702 }
703
704 /* check referenced features if they are enabled */
705 LY_ARRAY_FOR(f->iffeatures, u) {
706 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
707 /* if-feature disabled */
708 return LY_ENOT;
709 }
710 }
711
712 /* feature enabled */
713 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200714}
715
Michal Vaskoc193ce92020-03-06 11:04:48 +0100716API const struct lysc_node *
Radek Krejcifab954b2019-09-11 11:25:14 +0200717lysc_node_is_disabled(const struct lysc_node *node, int recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100718{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200719 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100720
721 LY_CHECK_ARG_RET(NULL, node, NULL);
722
Michal Vaskoc193ce92020-03-06 11:04:48 +0100723 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100724 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100725 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100726 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200727 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100728 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100729 }
730 }
731 }
732
733 if (!recursive) {
734 return NULL;
735 }
736
Michal Vaskoc193ce92020-03-06 11:04:48 +0100737 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100738 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100739 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
740
Radek Krejcia3045382018-11-22 14:30:31 +0100741 return NULL;
742}
743
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200744LY_ERR
745lys_set_implemented_internal(struct lys_module *mod, uint8_t value)
746{
747 struct lys_module *m;
748
749 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
750
751 if (mod->implemented) {
752 return LY_SUCCESS;
753 }
754
755 /* we have module from the current context */
756 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
757 if (m) {
758 if (m != mod) {
759 /* check collision with other implemented revision */
760 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s\" is present in the context in other implemented revision (%s).",
761 mod->name, mod->revision ? mod->revision : "module without revision");
762 return LY_EDENIED;
763 } else {
764 /* mod is already implemented */
765 return LY_SUCCESS;
766 }
767 }
768
769 /* mark the module implemented, check for collision was already done */
770 mod->implemented = value;
771
772 /* compile the schema */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200773 LY_CHECK_RET(lys_compile(&mod, LYSC_OPT_INTERNAL));
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200774
775 return LY_SUCCESS;
776}
777
778API LY_ERR
779lys_set_implemented(struct lys_module *mod)
780{
781 return lys_set_implemented_internal(mod, 1);
782}
783
Michal Vasko3a41dff2020-07-15 14:30:28 +0200784LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200785lys_parse_mem_submodule(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, struct lys_parser_ctx *main_ctx,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200786 LY_ERR (*custom_check)(const struct ly_ctx*, struct lysp_module*, struct lysp_submodule*, void*),
787 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200788{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200789 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100791 struct lys_yang_parser_ctx *yangctx = NULL;
792 struct lys_yin_parser_ctx *yinctx = NULL;
793 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100794
Michal Vasko3a41dff2020-07-15 14:30:28 +0200795 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100796
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100797 switch (format) {
798 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200799 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100800 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100801 break;
802 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100804 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100805 break;
806 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200807 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200808 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100809 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200810 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200811 LY_CHECK_GOTO(ret, error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100812
813 /* make sure that the newest revision is at position 0 */
814 lysp_sort_revisions(submod->revs);
815
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100816 /* decide the latest revision */
Michal Vaskob36053d2020-03-26 15:49:30 +0100817 latest_sp = ly_ctx_get_submodule(PARSER_CTX(pctx), submod->belongsto, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100818 if (latest_sp) {
819 if (submod->revs) {
820 if (!latest_sp->revs) {
821 /* latest has no revision, so mod is anyway newer */
822 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200823 /* the latest_sp is zeroed later when the new module is being inserted into the context */
824 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
825 submod->latest_revision = latest_sp->latest_revision;
826 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100827 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200828 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100829 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200830 } else {
831 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100832 }
833 } else {
834 submod->latest_revision = 1;
835 }
836
Radek Krejcib3289d62019-09-18 12:21:39 +0200837 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200838 LY_CHECK_GOTO(ret = custom_check(PARSER_CTX(pctx), NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200839 }
840
841 if (latest_sp) {
842 latest_sp->latest_revision = 0;
843 }
844
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100846 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
847 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100848
David Sedlák1b623122019-08-05 15:27:49 +0200849 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100850 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200851 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100852 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200853 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200854 *submodule = submod;
855 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200856
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100857error:
858 lysp_submodule_free(ctx, submod);
David Sedlák1b623122019-08-05 15:27:49 +0200859 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100860 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200861 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100862 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200863 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200864 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200865}
866
Michal Vasko3a41dff2020-07-15 14:30:28 +0200867LY_ERR
Michal Vasko63f3d842020-07-08 10:10:14 +0200868lys_parse_mem_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, int implement,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200869 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod,
870 struct lysp_submodule *submod, void *data), void *check_data,
871 struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200872{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100873 struct lys_module *mod = NULL, *latest, *mod_dup;
Radek Krejci086c7132018-10-26 15:29:04 +0200874 struct lysp_import *imp;
875 struct lysp_include *inc;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200876 LY_ERR ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200877 LY_ARRAY_COUNT_TYPE u, v;
Michal Vaskob36053d2020-03-26 15:49:30 +0100878 struct lys_yang_parser_ctx *yangctx = NULL;
879 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200880 struct lys_parser_ctx *pctx = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200881
Michal Vasko3a41dff2020-07-15 14:30:28 +0200882 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +0200883
884 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200885 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100886 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200887
888 switch (format) {
889 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200890 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100891 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200892 break;
893 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200894 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100895 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200896 break;
897 default:
898 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200899 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200900 break;
901 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200902 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200903
904 /* make sure that the newest revision is at position 0 */
905 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +0100906 if (mod->parsed->revs) {
907 mod->revision = lydict_insert(ctx, mod->parsed->revs[0].date, 0);
908 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200909
Radek Krejcib3289d62019-09-18 12:21:39 +0200910 /* decide the latest revision */
911 latest = (struct lys_module*)ly_ctx_get_module_latest(ctx, mod->name);
912 if (latest) {
913 if (mod->revision) {
914 if (!latest->revision) {
915 /* latest has no revision, so mod is anyway newer */
916 mod->latest_revision = latest->latest_revision;
917 /* the latest is zeroed later when the new module is being inserted into the context */
918 } else if (strcmp(mod->revision, latest->revision) > 0) {
919 mod->latest_revision = latest->latest_revision;
920 /* the latest is zeroed later when the new module is being inserted into the context */
921 } else {
922 latest = NULL;
923 }
924 } else {
925 latest = NULL;
926 }
927 } else {
928 mod->latest_revision = 1;
929 }
930
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100931 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200932 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100933 }
934
Radek Krejci86d106e2018-10-18 09:53:19 +0200935 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200936 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
938 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200939 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100940 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +0200941 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100942 mod->implemented = 1;
Radek Krejci86d106e2018-10-18 09:53:19 +0200943 }
944
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100945 /* check for duplicity in the context */
Radek Krejci0af46292019-01-11 16:02:31 +0100946 mod_dup = (struct lys_module*)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100947 if (mod_dup) {
948 if (mod_dup->parsed) {
949 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +0200950 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100951 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
952 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +0200953 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100954 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
955 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +0200956 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200957 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100958 goto error;
959 } else {
960 /* add the parsed data to the currently compiled-only module in the context */
961 mod_dup->parsed = mod->parsed;
962 mod_dup->parsed->mod = mod_dup;
963 mod->parsed = NULL;
964 lys_module_free(mod, NULL);
965 mod = mod_dup;
966 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +0200967 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200969
Radek Krejci0af46292019-01-11 16:02:31 +0100970 if (!mod->implemented) {
Michal Vasko33ff9422020-07-03 09:50:39 +0200971 /* pre-compile features and identities of the module */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200972 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->dis_features), error);
973 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->identities, &mod->dis_identities), error);
Radek Krejci0af46292019-01-11 16:02:31 +0100974 }
975
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100976 if (latest) {
Radek Krejcib3289d62019-09-18 12:21:39 +0200977 latest->latest_revision = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100978 }
Radek Krejcid33273d2018-10-25 14:55:52 +0200979
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100980 /* add into context */
981 ly_set_add(&ctx->list, mod, LY_SET_OPT_USEASLIST);
Radek Krejcid33273d2018-10-25 14:55:52 +0200982
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100983finish_parsing:
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100984 /* resolve imports */
985 mod->parsed->parsing = 1;
986 LY_ARRAY_FOR(mod->parsed->imports, u) {
987 imp = &mod->parsed->imports[u];
Michal Vasko3a41dff2020-07-15 14:30:28 +0200988 if (!imp->module) {
989 LY_CHECK_GOTO(ret = lysp_load_module(ctx, imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module),
990 error_ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200991 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100992 /* check for importing the same module twice */
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200993 for (v = 0; v < u; ++v) {
994 if (imp->module == mod->parsed->imports[v].module) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200995 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Single revision of the module \"%s\" referred twice.",
996 imp->name);
997 ret = LY_EVALID;
Radek Krejcibbe09a92018-11-08 09:36:54 +0100998 goto error_ctx;
Radek Krejci086c7132018-10-26 15:29:04 +0200999 }
1000 }
Radek Krejcid33273d2018-10-25 14:55:52 +02001001 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001002 LY_ARRAY_FOR(mod->parsed->includes, u) {
1003 inc = &mod->parsed->includes[u];
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004 if (!inc->submodule) {
1005 LY_CHECK_GOTO(ret = lysp_load_submodule(pctx, mod->parsed, inc), error_ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001006 }
Radek Krejci0af46292019-01-11 16:02:31 +01001007 if (!mod->implemented) {
Michal Vasko33ff9422020-07-03 09:50:39 +02001008 /* pre-compile features and identities of the submodule */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001009 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, inc->submodule->features, &mod->dis_features), error);
1010 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, inc->submodule->identities, &mod->dis_identities),
1011 error);
Radek Krejci0af46292019-01-11 16:02:31 +01001012 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001013 }
1014 mod->parsed->parsing = 0;
1015
Radek Krejci7fc68292019-06-12 13:51:09 +02001016 /* check name collisions - typedefs and TODO groupings */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001017 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
Radek Krejcid33273d2018-10-25 14:55:52 +02001018
David Sedlák1b623122019-08-05 15:27:49 +02001019 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001020 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001021 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001022 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001023 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001024 *module = mod;
1025 return LY_SUCCESS;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001026
1027error_ctx:
1028 ly_set_rm(&ctx->list, mod, NULL);
1029error:
1030 lys_module_free(mod, NULL);
Radek Krejcif6923e82020-07-02 16:36:53 +02001031 if (pctx) {
1032 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1033 }
David Sedlák1b623122019-08-05 15:27:49 +02001034 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +01001035 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +02001036 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +01001037 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +02001038 }
1039
Michal Vasko3a41dff2020-07-15 14:30:28 +02001040 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001041}
1042
Michal Vasko3a41dff2020-07-15 14:30:28 +02001043API LY_ERR
1044lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001045{
Radek Krejci096235c2019-01-11 11:12:19 +01001046 struct lys_module *mod;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001047 char *filename, *rev, *dot;
1048 size_t len;
Radek Krejci096235c2019-01-11 11:12:19 +01001049
Michal Vasko3a41dff2020-07-15 14:30:28 +02001050 if (module) {
1051 *module = NULL;
1052 }
1053 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001054
Michal Vasko63f3d842020-07-08 10:10:14 +02001055 /* remember input position */
1056 in->func_start = in->current;
1057
Michal Vasko3a41dff2020-07-15 14:30:28 +02001058 LY_CHECK_RET(lys_parse_mem_module(ctx, in, format, 1, NULL, NULL, &mod));
Radek Krejci096235c2019-01-11 11:12:19 +01001059
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001060 switch (in->type) {
1061 case LY_IN_FILEPATH:
1062 /* check that name and revision match filename */
1063 filename = strrchr(in->method.fpath.filepath, '/');
1064 if (!filename) {
1065 filename = in->method.fpath.filepath;
1066 } else {
1067 filename++;
1068 }
1069 rev = strchr(filename, '@');
1070 dot = strrchr(filename, '.');
1071
1072 /* name */
1073 len = strlen(mod->name);
1074 if (strncmp(filename, mod->name, len) ||
1075 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
1076 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1077 }
1078 if (rev) {
1079 len = dot - ++rev;
1080 if (!mod->parsed->revs || len != 10 || strncmp(mod->parsed->revs[0].date, rev, len)) {
1081 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
1082 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
1083 }
1084 }
1085
1086 break;
1087 case LY_IN_FD:
1088 case LY_IN_FILE:
1089 case LY_IN_MEMORY:
1090 /* nothing special to do */
1091 break;
1092 default:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001093 LOGINT_RET(ctx);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001094 break;
Radek Krejci096235c2019-01-11 11:12:19 +01001095 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001096
1097 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001098 LY_CHECK_RET(lys_compile(&mod, 0));
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001099
Michal Vasko3a41dff2020-07-15 14:30:28 +02001100 if (module) {
1101 *module = mod;
1102 }
1103 return LY_SUCCESS;
Radek Krejci86d106e2018-10-18 09:53:19 +02001104}
1105
Michal Vasko3a41dff2020-07-15 14:30:28 +02001106API LY_ERR
1107lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001108{
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001109 LY_ERR ret;
1110 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001111
Michal Vasko3a41dff2020-07-15 14:30:28 +02001112 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001113
Michal Vasko3a41dff2020-07-15 14:30:28 +02001114 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 +02001115
Michal Vasko3a41dff2020-07-15 14:30:28 +02001116 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001117 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001118
Michal Vasko3a41dff2020-07-15 14:30:28 +02001119 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001120}
1121
Michal Vasko3a41dff2020-07-15 14:30:28 +02001122API LY_ERR
1123lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001124{
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001125 LY_ERR ret;
1126 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001127
Michal Vasko3a41dff2020-07-15 14:30:28 +02001128 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001129
Michal Vasko3a41dff2020-07-15 14:30:28 +02001130 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 +02001131
Michal Vasko3a41dff2020-07-15 14:30:28 +02001132 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001133 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001134
Michal Vasko3a41dff2020-07-15 14:30:28 +02001135 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001136}
1137
Michal Vasko3a41dff2020-07-15 14:30:28 +02001138API LY_ERR
1139lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001140{
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001141 LY_ERR ret;
1142 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001143
Michal Vasko3a41dff2020-07-15 14:30:28 +02001144 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001145
Michal Vasko3a41dff2020-07-15 14:30:28 +02001146 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
1147 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001148
Michal Vasko3a41dff2020-07-15 14:30:28 +02001149 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001150 ly_in_free(in, 0);
1151
Michal Vasko3a41dff2020-07-15 14:30:28 +02001152 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001153}
1154
1155API LY_ERR
1156lys_search_localfile(const char * const *searchpaths, int cwd, const char *name, const char *revision,
1157 char **localfile, LYS_INFORMAT *format)
1158{
1159 size_t len, flen, match_len = 0, dir_len;
1160 int i, implicit_cwd = 0, ret = EXIT_FAILURE;
1161 char *wd, *wn = NULL;
1162 DIR *dir = NULL;
1163 struct dirent *file;
1164 char *match_name = NULL;
1165 LYS_INFORMAT format_aux, match_format = 0;
1166 struct ly_set *dirs;
1167 struct stat st;
1168
1169 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1170
1171 /* start to fill the dir fifo with the context's search path (if set)
1172 * and the current working directory */
1173 dirs = ly_set_new();
1174 if (!dirs) {
1175 LOGMEM(NULL);
1176 return EXIT_FAILURE;
1177 }
1178
1179 len = strlen(name);
1180 if (cwd) {
1181 wd = get_current_dir_name();
1182 if (!wd) {
1183 LOGMEM(NULL);
1184 goto cleanup;
1185 } else {
1186 /* add implicit current working directory (./) to be searched,
1187 * this directory is not searched recursively */
1188 if (ly_set_add(dirs, wd, 0) == -1) {
1189 goto cleanup;
1190 }
1191 implicit_cwd = 1;
1192 }
1193 }
1194 if (searchpaths) {
1195 for (i = 0; searchpaths[i]; i++) {
1196 /* check for duplicities with the implicit current working directory */
1197 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1198 implicit_cwd = 0;
1199 continue;
1200 }
1201 wd = strdup(searchpaths[i]);
1202 if (!wd) {
1203 LOGMEM(NULL);
1204 goto cleanup;
1205 } else if (ly_set_add(dirs, wd, 0) == -1) {
1206 goto cleanup;
1207 }
1208 }
1209 }
1210 wd = NULL;
1211
1212 /* start searching */
1213 while (dirs->count) {
1214 free(wd);
1215 free(wn); wn = NULL;
1216
1217 dirs->count--;
1218 wd = (char *)dirs->objs[dirs->count];
1219 dirs->objs[dirs->count] = NULL;
1220 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1221
1222 if (dir) {
1223 closedir(dir);
1224 }
1225 dir = opendir(wd);
1226 dir_len = strlen(wd);
1227 if (!dir) {
1228 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1229 } else {
1230 while ((file = readdir(dir))) {
1231 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1232 /* skip . and .. */
1233 continue;
1234 }
1235 free(wn);
1236 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1237 LOGMEM(NULL);
1238 goto cleanup;
1239 }
1240 if (stat(wn, &st) == -1) {
1241 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
1242 file->d_name, wd, strerror(errno));
1243 continue;
1244 }
1245 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1246 /* we have another subdirectory in searchpath to explore,
1247 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
1248 if (ly_set_add(dirs, wn, 0) == -1) {
1249 goto cleanup;
1250 }
1251 /* continue with the next item in current directory */
1252 wn = NULL;
1253 continue;
1254 } else if (!S_ISREG(st.st_mode)) {
1255 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1256 continue;
1257 }
1258
1259 /* here we know that the item is a file which can contain a module */
1260 if (strncmp(name, file->d_name, len) ||
1261 (file->d_name[len] != '.' && file->d_name[len] != '@')) {
1262 /* different filename than the module we search for */
1263 continue;
1264 }
1265
1266 /* get type according to filename suffix */
1267 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001268 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001269 format_aux = LYS_IN_YANG;
Radek Krejcied5acc52019-04-25 15:57:04 +02001270 /* TODO YIN parser
1271 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1272 format_aux = LYS_IN_YIN;
1273 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001274 } else {
1275 /* not supportde suffix/file format */
1276 continue;
1277 }
1278
1279 if (revision) {
1280 /* we look for the specific revision, try to get it from the filename */
1281 if (file->d_name[len] == '@') {
1282 /* check revision from the filename */
1283 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1284 /* another revision */
1285 continue;
1286 } else {
1287 /* exact revision */
1288 free(match_name);
1289 match_name = wn;
1290 wn = NULL;
1291 match_len = dir_len + 1 + len;
1292 match_format = format_aux;
1293 goto success;
1294 }
1295 } else {
1296 /* continue trying to find exact revision match, use this only if not found */
1297 free(match_name);
1298 match_name = wn;
1299 wn = NULL;
1300 match_len = dir_len + 1 +len;
1301 match_format = format_aux;
1302 continue;
1303 }
1304 } else {
1305 /* remember the revision and try to find the newest one */
1306 if (match_name) {
1307 if (file->d_name[len] != '@' ||
1308 lysp_check_date(NULL, &file->d_name[len + 1], flen - (format_aux == LYS_IN_YANG ? 5 : 4) - len - 1, NULL)) {
1309 continue;
1310 } else if (match_name[match_len] == '@' &&
1311 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1312 continue;
1313 }
1314 free(match_name);
1315 }
1316
1317 match_name = wn;
1318 wn = NULL;
1319 match_len = dir_len + 1 + len;
1320 match_format = format_aux;
1321 continue;
1322 }
1323 }
1324 }
1325 }
1326
1327success:
1328 (*localfile) = match_name;
1329 match_name = NULL;
1330 if (format) {
1331 (*format) = match_format;
1332 }
1333 ret = EXIT_SUCCESS;
1334
1335cleanup:
1336 free(wn);
1337 free(wd);
1338 if (dir) {
1339 closedir(dir);
1340 }
1341 free(match_name);
1342 ly_set_free(dirs, free);
1343
1344 return ret;
1345}
1346