blob: e81e77da44cbfeeb766353ef17ef6be9bd289fbb [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 *
Radek Krejci1deb5be2020-08-26 16:43:36 +020043lys_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 +010044{
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 Krejci857189e2020-09-01 13:26:36 +020047 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010048 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 */
Michal Vasko69730152020-10-09 16:30:07 +020061 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +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) {
Michal Vasko22df3f02020-08-24 13:29:22 +020091 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010092 break;
93 }
94 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +020095 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020096 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +010097 }
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) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200107 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100108 break;
109 }
110 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200111 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200112 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100113 }
114 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200115 } else {
116 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100117 }
118
Radek Krejcia3045382018-11-22 14:30:31 +0100119repeat:
120 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100121 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200122 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100123 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200124 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100125 } else if (!action_flag) {
126 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200127 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100128 } else if (!notif_flag) {
129 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200130 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100131 } else {
132 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100133 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100134 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100135 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100136check:
Radek Krejcia3045382018-11-22 14:30:31 +0100137 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100138 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100139 case LYS_ACTION:
140 case LYS_NOTIF:
141 case LYS_LEAF:
142 case LYS_ANYXML:
143 case LYS_ANYDATA:
144 case LYS_LIST:
145 case LYS_LEAFLIST:
146 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200147 case LYS_CASE:
148 if (options & LYS_GETNEXT_WITHCASE) {
149 break;
150 } else {
151 /* go into */
152 next = ((struct lysc_node_case *)next)->child;
153 }
154 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100155 case LYS_CONTAINER:
156 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
157 if (((struct lysc_node_container *)next)->child) {
158 /* go into */
159 next = ((struct lysc_node_container *)next)->child;
160 } else {
161 next = next->next;
162 }
163 goto repeat;
164 }
165 break;
166 case LYS_CHOICE:
167 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200168 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100169 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
170 next = next->next;
171 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100172 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100173 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200174 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100175 } else {
176 next = ((struct lysc_node_choice *)next)->cases->child;
177 }
Radek Krejcia3045382018-11-22 14:30:31 +0100178 }
179 goto repeat;
180 default:
181 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200182 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100183 return NULL;
184 }
185
186 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
187 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200188 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100189 next = next->next;
190 goto repeat;
191 }
192 }
193 return next;
194}
195
196API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100197lys_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 +0200198 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100199{
200 const struct lysc_node *node = NULL;
201
202 LY_CHECK_ARG_RET(NULL, module, name, NULL);
203 if (!nodetype) {
204 nodetype = 0xffff;
205 }
206
207 while ((node = lys_getnext(node, parent, module->compiled, options))) {
208 if (!(node->nodetype & nodetype)) {
209 continue;
210 }
211 if (node->module != module) {
212 continue;
213 }
214
215 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200216 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100217 return node;
218 }
219 } else {
220 if (!strcmp(node->name, name)) {
221 return node;
222 }
223 }
224 }
225 return NULL;
226}
227
Michal Vasko519fd602020-05-26 12:17:39 +0200228API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200229lys_atomize_xpath(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200230{
231 LY_ERR ret = LY_SUCCESS;
232 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200233 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200234 uint32_t i;
235
236 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
237 if (!(options & LYXP_SCNODE_ALL)) {
238 options = LYXP_SCNODE;
239 }
240
241 memset(&xp_set, 0, sizeof xp_set);
242
243 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200244 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
245 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200246
247 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200248 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200249 LY_CHECK_GOTO(ret, cleanup);
250
251 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200252 ret = ly_set_new(set);
253 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200254
255 /* transform into ly_set */
256 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
257 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
258 (*set)->size = xp_set.used;
259
260 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200261 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200262 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200263 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200264 }
265 }
266
267cleanup:
268 lyxp_set_free_content(&xp_set);
269 lyxp_expr_free(ctx_node->module->ctx, exp);
270 return ret;
271}
272
Michal Vasko072de482020-08-05 13:27:21 +0200273API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200274lys_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 +0200275{
276 LY_ERR ret = LY_SUCCESS;
277 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200278 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200279 uint32_t i;
280
281 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
282 if (!(options & LYXP_SCNODE_ALL)) {
283 options = LYXP_SCNODE;
284 }
285
286 memset(&xp_set, 0, sizeof xp_set);
287
288 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200289 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
290 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200291
292 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200293 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200294 LY_CHECK_GOTO(ret, cleanup);
295
296 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200297 ret = ly_set_new(set);
298 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200299
300 /* transform into ly_set */
301 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
302 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
303 (*set)->size = xp_set.used;
304
305 for (i = 0; i < xp_set.used; ++i) {
306 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 +0200307 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200308 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200309 }
310 }
311
312cleanup:
313 lyxp_set_free_content(&xp_set);
314 lyxp_expr_free(ctx_node->module->ctx, exp);
315 return ret;
316}
317
Michal Vasko14654712020-02-06 08:35:21 +0100318char *
319lysc_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 +0200320 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200321{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200322 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200323 char *path = NULL;
324 int len = 0;
325
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200326 LY_CHECK_ARG_RET(NULL, node, NULL);
327 if (buffer) {
328 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
329 }
330
Radek Krejci327de162019-06-14 12:52:07 +0200331 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200333 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100334 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200335 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100336 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200337
Michal Vasko65de0402020-08-03 16:34:19 +0200338 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
339 /* schema-only node */
340 continue;
341 }
342
Michal Vasko11deea12020-08-05 13:54:50 +0200343 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200344 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100345 if (parent && (iter->parent == parent)) {
346 slash = "";
347 } else {
348 slash = "/";
349 }
Michal Vasko69730152020-10-09 16:30:07 +0200350 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200351 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200352 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100353 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200354 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100355 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200356 }
Radek Krejci327de162019-06-14 12:52:07 +0200357 } else {
358 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200359 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100360 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200361 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100362 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200363 }
Radek Krejci327de162019-06-14 12:52:07 +0200364 }
365 free(s);
366 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200367
Michal Vasko69730152020-10-09 16:30:07 +0200368 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200369 /* not enough space in buffer */
370 break;
371 }
Radek Krejci327de162019-06-14 12:52:07 +0200372 }
373
374 if (len < 0) {
375 free(path);
376 path = NULL;
377 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200378 if (buffer) {
379 strcpy(buffer, "/");
380 } else {
381 path = strdup("/");
382 }
Radek Krejci327de162019-06-14 12:52:07 +0200383 }
384 break;
385 }
386
Radek Krejci1c0c3442019-07-23 16:08:47 +0200387 if (buffer) {
388 return buffer;
389 } else {
390 return path;
391 }
Radek Krejci327de162019-06-14 12:52:07 +0200392}
393
Michal Vasko14654712020-02-06 08:35:21 +0100394API char *
395lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
396{
397 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
398}
399
Michal Vasko28d78432020-05-26 13:10:53 +0200400API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100401lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200402{
Michal Vasko28d78432020-05-26 13:10:53 +0200403 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
404 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200405}
406
Radek Krejci693262f2019-04-29 15:23:20 +0200407uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200408lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200409{
410 uint8_t *item;
411 uint8_t mask = 3, result;
412
Radek Krejci151a5b72018-10-19 14:21:44 +0200413 item = &list[pos / 4];
414 result = (*item) & (mask << 2 * (pos % 4));
415 return result >> 2 * (pos % 4);
416}
417
Michal Vasko28d78432020-05-26 13:10:53 +0200418static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200419lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200420{
421 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200422 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200423
Radek Krejci693262f2019-04-29 15:23:20 +0200424 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200425 (*index_e)++;
426
427 switch (op) {
428 case LYS_IFF_F:
429 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200430 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200431 case LYS_IFF_NOT:
432 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200433 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200434 case LYS_IFF_AND:
435 case LYS_IFF_OR:
436 a = lysc_iffeature_value_(iff, index_e, index_f);
437 b = lysc_iffeature_value_(iff, index_e, index_f);
438 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200439 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
440 return LY_SUCCESS;
441 } else {
442 return LY_ENOT;
443 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200444 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200445 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
446 return LY_SUCCESS;
447 } else {
448 return LY_ENOT;
449 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200450 }
451 }
452
453 return 0;
454}
455
Michal Vasko28d78432020-05-26 13:10:53 +0200456API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200457lysc_iffeature_value(const struct lysc_iffeature *iff)
458{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200459 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200460
461 LY_CHECK_ARG_RET(NULL, iff, -1);
462
463 if (iff->expr) {
464 return lysc_iffeature_value_(iff, &index_e, &index_f);
465 }
466 return 0;
467}
468
Radek Krejci151a5b72018-10-19 14:21:44 +0200469/**
470 * @brief Enable/Disable the specified feature in the module.
471 *
472 * If the feature is already set to the desired value, LY_SUCCESS is returned.
473 * By changing the feature, also all the feature which depends on it via their
474 * if-feature statements are again evaluated (disabled if a if-feature statemen
475 * evaluates to false).
476 *
Radek Krejci0af46292019-01-11 16:02:31 +0100477 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200478 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
479 * set all the features in the module.
480 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
Radek Krejci857189e2020-09-01 13:26:36 +0200481 * @param[in] skip_checks Flag to skip checking of if-features and just set @p value of the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200482 * @return LY_ERR value.
483 */
484static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200485lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200486{
Michal Vaskob0099a92020-08-31 14:55:23 +0200487 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200488 ly_bool all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200489 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200490 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200491 struct lysc_feature *f, **df;
492 struct lysc_iffeature *iff;
493 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100494 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200495
Radek Krejci6e67c402019-05-02 09:55:39 +0200496 if (!strcmp(name, "*")) {
497 /* enable all */
498 all = 1;
499 }
500
Radek Krejci0af46292019-01-11 16:02:31 +0100501 if (!mod->compiled) {
502 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled without a chance to change it.",
Michal Vasko69730152020-10-09 16:30:07 +0200503 mod->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100504 return LY_EINVAL;
505 }
Radek Krejci14915cc2020-09-14 17:28:13 +0200506 if (!mod->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200507 if (all) {
508 /* no feature to enable */
509 return LY_SUCCESS;
510 }
Radek Krejci0af46292019-01-11 16:02:31 +0100511 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200512 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200513 }
514
Radek Krejciba03a5a2020-08-27 14:40:41 +0200515 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100516 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200517
Radek Krejcica3db002018-11-01 10:31:01 +0100518run:
Radek Krejci14915cc2020-09-14 17:28:13 +0200519 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) {
520 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200521 if (all || !strcmp(f->name, name)) {
522 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
523 if (all) {
524 /* skip already set features */
525 continue;
526 } else {
527 /* feature already set correctly */
Michal Vaskob0099a92020-08-31 14:55:23 +0200528 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200529 }
530 }
531
532 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200533 if (!skip_checks) {
534 /* check referenced features if they are enabled */
535 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
536 if (lysc_iffeature_value(iff) == LY_ENOT) {
537 if (all) {
538 ++disabled_count;
539 goto next;
540 } else {
541 LOGERR(ctx, LY_EDENIED,
Michal Vasko69730152020-10-09 16:30:07 +0200542 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
543 f->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200544 ret = LY_EDENIED;
545 goto cleanup;
Michal Vasko82c31e62020-07-17 15:30:40 +0200546 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200547 }
548 }
549 }
550 /* enable the feature */
551 f->flags |= LYS_FENABLED;
552 } else { /* disable */
553 /* disable the feature */
554 f->flags &= ~LYS_FENABLED;
555 }
556
557 /* remember the changed feature */
Radek Krejci3d92e442020-10-12 12:48:13 +0200558 ret = ly_set_add(changed, f, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200559 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200560
561 if (!all) {
562 /* stop in case changing a single feature */
563 break;
564 }
565 }
566next:
567 ;
568 }
569
570 if (!all && !changed->count) {
Radek Krejci0af46292019-01-11 16:02:31 +0100571 LOGERR(ctx, LY_EINVAL, "Feature \"%s\" not found in module \"%s\".", name, mod->name);
Michal Vaskob0099a92020-08-31 14:55:23 +0200572 ret = LY_ENOTFOUND;
573 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200574 }
575
Radek Krejcica3db002018-11-01 10:31:01 +0100576 if (value && all && disabled_count) {
577 if (changed_count == changed->count) {
578 /* no change in last run -> not able to enable all ... */
579 /* ... print errors */
Radek Krejci14915cc2020-09-14 17:28:13 +0200580 for (u = 0; disabled_count && u < LY_ARRAY_COUNT(mod->features); ++u) {
581 if (!(mod->features[u].flags & LYS_FENABLED)) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100582 LOGERR(ctx, LY_EDENIED,
Michal Vasko69730152020-10-09 16:30:07 +0200583 "Feature \"%s\" cannot be enabled since it is disabled by its if-feature condition(s).",
584 mod->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100585 --disabled_count;
586 }
587 }
588 /* ... restore the original state */
589 for (u = 0; u < changed->count; ++u) {
590 f = changed->objs[u];
591 /* re-disable the feature */
592 f->flags &= ~LYS_FENABLED;
593 }
594
Michal Vaskob0099a92020-08-31 14:55:23 +0200595 ret = LY_EDENIED;
596 goto cleanup;
Radek Krejcica3db002018-11-01 10:31:01 +0100597 } else {
598 /* we did some change in last run, try it again */
599 changed_count = changed->count;
600 goto run;
601 }
602 }
603
Radek Krejci151a5b72018-10-19 14:21:44 +0200604 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200605 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200606 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
607 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
608 * is not done - by default, features are disabled and must be explicitely enabled. */
609 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200610 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200611 if (!((*df)->flags & LYS_FENABLED)) {
612 /* not enabled, nothing to do */
613 continue;
614 }
615 /* check the feature's if-features which could change by the previous change of our feature */
616 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200617 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200618 /* the feature must be disabled now */
619 (*df)->flags &= ~LYS_FENABLED;
620 /* add the feature into the list of changed features */
Radek Krejci3d92e442020-10-12 12:48:13 +0200621 ret = ly_set_add(changed, *df, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200622 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200623 break;
624 }
625 }
626 }
627 }
628
Michal Vasko87a97f42020-10-06 10:30:28 +0200629 /* success */
630 ++mod->ctx->module_set_id;
631
Michal Vaskob0099a92020-08-31 14:55:23 +0200632cleanup:
Radek Krejci151a5b72018-10-19 14:21:44 +0200633 ly_set_free(changed, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200634 return ret;
Radek Krejci151a5b72018-10-19 14:21:44 +0200635}
636
637API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200638lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200639{
Radek Krejci0af46292019-01-11 16:02:31 +0100640 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200641
Michal Vasko22df3f02020-08-24 13:29:22 +0200642 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200643}
644
645API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200646lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200647{
Radek Krejci0af46292019-01-11 16:02:31 +0100648 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200649
Michal Vasko22df3f02020-08-24 13:29:22 +0200650 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200651}
652
Michal Vasko82c31e62020-07-17 15:30:40 +0200653API LY_ERR
654lys_feature_enable_force(const struct lys_module *module, const char *feature)
655{
656 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
657
Michal Vasko22df3f02020-08-24 13:29:22 +0200658 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200659}
660
661API LY_ERR
662lys_feature_disable_force(const struct lys_module *module, const char *feature)
663{
664 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
665
Michal Vasko22df3f02020-08-24 13:29:22 +0200666 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200667}
668
669API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200670lys_feature_value(const struct lys_module *module, const char *feature)
671{
Michal Vasko82c31e62020-07-17 15:30:40 +0200672 struct lysc_feature *f = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200673 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200674
675 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200676
677 /* search for the specified feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200678 LY_ARRAY_FOR(module->features, u) {
679 f = &module->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200680 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200681 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200682 }
683 }
684
685 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200686 if (!f) {
687 return LY_ENOTFOUND;
688 }
689
690 /* feature disabled */
691 if (!(f->flags & LYS_FENABLED)) {
692 return LY_ENOT;
693 }
694
695 /* check referenced features if they are enabled */
696 LY_ARRAY_FOR(f->iffeatures, u) {
697 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
698 /* if-feature disabled */
699 return LY_ENOT;
700 }
701 }
702
703 /* feature enabled */
704 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200705}
706
Michal Vaskoc193ce92020-03-06 11:04:48 +0100707API const struct lysc_node *
Radek Krejci857189e2020-09-01 13:26:36 +0200708lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100709{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200710 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100711
712 LY_CHECK_ARG_RET(NULL, node, NULL);
713
Michal Vaskoc193ce92020-03-06 11:04:48 +0100714 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100715 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100716 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100717 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200718 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100719 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100720 }
721 }
722 }
723
724 if (!recursive) {
725 return NULL;
726 }
727
Michal Vaskoc193ce92020-03-06 11:04:48 +0100728 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100729 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100730 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
731
Radek Krejcia3045382018-11-22 14:30:31 +0100732 return NULL;
733}
734
Radek Krejci19cf8052020-08-18 15:02:38 +0200735API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200736lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200737{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200738 struct lysc_action *act;
739 struct lysc_notif *notif;
740
Radek Krejci19cf8052020-08-18 15:02:38 +0200741 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
742
Radek Krejciaf9cd802020-10-06 21:59:47 +0200743 switch (node->nodetype) {
744 case LYS_CONTAINER:
745 case LYS_CHOICE:
746 case LYS_CASE:
747 case LYS_LEAF:
748 case LYS_LEAFLIST:
749 case LYS_LIST:
750 case LYS_ANYXML:
751 case LYS_ANYDATA:
752 if (prev_priv_p) {
753 *prev_priv_p = node->priv;
754 }
755 ((struct lysc_node *)node)->priv = priv;
756 break;
757 case LYS_RPC:
758 case LYS_ACTION:
759 act = (struct lysc_action *)node;
760 if (prev_priv_p) {
761 *prev_priv_p = act->priv;
762 }
763 act->priv = priv;
764 break;
765 case LYS_NOTIF:
766 notif = (struct lysc_notif *)node;
767 if (prev_priv_p) {
768 *prev_priv_p = notif->priv;
769 }
770 notif->priv = priv;
771 break;
772 default:
773 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200774 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200775
776 return LY_SUCCESS;
777}
778
Michal Vasko89b5c072020-10-06 13:52:44 +0200779API LY_ERR
780lys_set_implemented(struct lys_module *mod)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200781{
Michal Vasko89b5c072020-10-06 13:52:44 +0200782 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200783 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200784 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200785
786 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
787
788 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200789 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200790 return LY_SUCCESS;
791 }
792
793 /* we have module from the current context */
794 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
795 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200796 assert(m != mod);
797
798 /* check collision with other implemented revision */
799 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
800 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
801 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200802 }
803
Michal Vasko89b5c072020-10-06 13:52:44 +0200804 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200805 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200806
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200807 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200808 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200809
810 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200811 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200812
Michal Vasko89b5c072020-10-06 13:52:44 +0200813 if (mod == mod->ctx->implementing.objs[0]) {
814 /* the first module being implemented, consolidate the set */
815 if (ret) {
816 /* failure, full compile revert */
817 for (i = 0; i < mod->ctx->list.count; ++i) {
818 m = mod->ctx->list.objs[i];
819 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
820 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200821
Michal Vasko89b5c072020-10-06 13:52:44 +0200822 /* make the module correctly non-implemented again */
823 m->implemented = 0;
824 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
825 lys_precompile_augments_deviations_revert(mod->ctx, m);
826 }
827
828 /* free the compiled version of the module, if any */
829 lysc_module_free(m->compiled, NULL);
830 m->compiled = NULL;
831
832 if (m->implemented) {
833 /* recompile, must succeed because it was already compiled; hide messages because any
834 * warnings were already printed, are not really relevant, and would hide the real error */
835 uint32_t prev_lo = ly_log_options(0);
836 r = lys_compile(m, 0);
837 ly_log_options(prev_lo);
838 if (r) {
839 LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name);
840 }
841 }
842 }
843 }
844
845 ly_set_erase(&mod->ctx->implementing, NULL);
846 }
847 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200848}
849
Michal Vasko7c8439f2020-08-05 13:25:19 +0200850static LY_ERR
851lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
852{
853 struct lysp_import *imp;
854 struct lysp_include *inc;
855 LY_ARRAY_COUNT_TYPE u, v;
856
857 modp->parsing = 1;
858 LY_ARRAY_FOR(modp->imports, u) {
859 imp = &modp->imports[u];
860 if (!imp->module) {
861 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
862 }
863 /* check for importing the same module twice */
864 for (v = 0; v < u; ++v) {
865 if (imp->module == modp->imports[v].module) {
866 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
867 }
868 }
869 }
870 LY_ARRAY_FOR(modp->includes, u) {
871 inc = &modp->includes[u];
872 if (!inc->submodule) {
873 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
874 }
875 }
876 modp->parsing = 0;
877
878 return LY_SUCCESS;
879}
880
Michal Vasko3a41dff2020-07-15 14:30:28 +0200881LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200882lys_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 +0200883 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200884 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200885{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200886 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100887 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100888 struct lys_yang_parser_ctx *yangctx = NULL;
889 struct lys_yin_parser_ctx *yinctx = NULL;
890 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891
Michal Vasko3a41dff2020-07-15 14:30:28 +0200892 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100894 switch (format) {
895 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200896 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100897 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 break;
899 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200900 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100901 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100902 break;
903 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200904 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200905 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100906 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200907 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200908 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200909 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100910
911 /* make sure that the newest revision is at position 0 */
912 lysp_sort_revisions(submod->revs);
913
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100914 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200915 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 if (latest_sp) {
917 if (submod->revs) {
918 if (!latest_sp->revs) {
919 /* latest has no revision, so mod is anyway newer */
920 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200921 /* the latest_sp is zeroed later when the new module is being inserted into the context */
922 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
923 submod->latest_revision = latest_sp->latest_revision;
924 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200926 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200928 } else {
929 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100930 }
931 } else {
932 submod->latest_revision = 1;
933 }
934
Radek Krejcib3289d62019-09-18 12:21:39 +0200935 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200936 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200937 }
938
939 if (latest_sp) {
940 latest_sp->latest_revision = 0;
941 }
942
Michal Vasko7a0b0762020-09-02 16:37:01 +0200943 lys_parser_fill_filepath(ctx, in, &submod->filepath);
944
Michal Vasko7c8439f2020-08-05 13:25:19 +0200945 /* resolve imports and includes */
946 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
947
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100948 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100949 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
950 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100951
David Sedlák1b623122019-08-05 15:27:49 +0200952 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100953 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200954 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100955 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200956 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200957 *submodule = submod;
958 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200959
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100960error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200961 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200962 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100963 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200964 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100965 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200966 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200967 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200968}
969
Michal Vasko3a41dff2020-07-15 14:30:28 +0200970LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200971lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200972 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
973 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200974{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100975 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200976 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200977 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200978 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100979 struct lys_yang_parser_ctx *yangctx = NULL;
980 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200981 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200982 char *filename, *rev, *dot;
983 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +0200984
Michal Vasko3a41dff2020-07-15 14:30:28 +0200985 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200986 if (module) {
987 *module = NULL;
988 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200989
990 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200991 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100992 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200993
994 switch (format) {
995 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200996 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100997 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200998 break;
999 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +02001000 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001001 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001002 break;
1003 default:
1004 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001005 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001006 break;
1007 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001008 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001009
1010 /* make sure that the newest revision is at position 0 */
1011 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001012 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001013 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001014 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001015
Radek Krejcib3289d62019-09-18 12:21:39 +02001016 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001017 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001018 if (latest) {
1019 if (mod->revision) {
1020 if (!latest->revision) {
1021 /* latest has no revision, so mod is anyway newer */
1022 mod->latest_revision = latest->latest_revision;
1023 /* the latest is zeroed later when the new module is being inserted into the context */
1024 } else if (strcmp(mod->revision, latest->revision) > 0) {
1025 mod->latest_revision = latest->latest_revision;
1026 /* the latest is zeroed later when the new module is being inserted into the context */
1027 } else {
1028 latest = NULL;
1029 }
1030 } else {
1031 latest = NULL;
1032 }
1033 } else {
1034 mod->latest_revision = 1;
1035 }
1036
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001037 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001038 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001039 }
1040
Radek Krejci86d106e2018-10-18 09:53:19 +02001041 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001042 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001043 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1044 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001045 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001046 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001047 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001048 }
1049
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001050 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001051 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001052 if (mod_dup) {
1053 if (mod_dup->parsed) {
1054 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001055 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001056 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001057 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001058 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001059 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001060 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001061 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001062 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 goto error;
1064 } else {
1065 /* add the parsed data to the currently compiled-only module in the context */
1066 mod_dup->parsed = mod->parsed;
1067 mod_dup->parsed->mod = mod_dup;
1068 mod->parsed = NULL;
1069 lys_module_free(mod, NULL);
1070 mod = mod_dup;
1071 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001072 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001073 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001074
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001075 switch (in->type) {
1076 case LY_IN_FILEPATH:
1077 /* check that name and revision match filename */
1078 filename = strrchr(in->method.fpath.filepath, '/');
1079 if (!filename) {
1080 filename = in->method.fpath.filepath;
1081 } else {
1082 filename++;
1083 }
1084 rev = strchr(filename, '@');
1085 dot = strrchr(filename, '.');
1086
1087 /* name */
1088 len = strlen(mod->name);
1089 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001090 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001091 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1092 }
1093 if (rev) {
1094 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001095 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001096 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001097 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001098 }
1099 }
1100
1101 break;
1102 case LY_IN_FD:
1103 case LY_IN_FILE:
1104 case LY_IN_MEMORY:
1105 /* nothing special to do */
1106 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001107 case LY_IN_ERROR:
1108 LOGINT(ctx);
1109 ret = LY_EINT;
1110 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001111 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001112
1113 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001114
Michal Vasko89b5c072020-10-06 13:52:44 +02001115 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001116 /* pre-compile features and identities of the module */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001117 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod->parsed, mod->parsed->features, &mod->features), error);
1118 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001119 }
1120
1121 if (latest) {
1122 latest->latest_revision = 0;
1123 }
1124
1125 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001126 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001127 LY_CHECK_GOTO(ret, error);
1128 ctx->module_set_id++;
1129
1130finish_parsing:
1131 /* resolve imports and includes */
1132 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1133
Michal Vasko89b5c072020-10-06 13:52:44 +02001134 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001135 /* pre-compile features and identities of any submodules */
1136 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001137 submod = mod->parsed->includes[u].submodule;
1138 ret = lys_feature_precompile(NULL, ctx, (struct lysp_module *)submod, submod->features, &mod->features);
1139 LY_CHECK_GOTO(ret, error);
1140 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1141 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001142 }
1143 }
1144
1145 /* check name collisions - typedefs and TODO groupings */
1146 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1147
Michal Vasko89b5c072020-10-06 13:52:44 +02001148 if (implement) {
1149 /* implement (compile) */
1150 LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001151 }
1152
1153 if (format == LYS_IN_YANG) {
1154 yang_parser_ctx_free(yangctx);
1155 } else {
1156 yin_parser_ctx_free(yinctx);
1157 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001158 if (module) {
1159 *module = mod;
1160 }
1161 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001162
1163error_ctx:
1164 ly_set_rm(&ctx->list, mod, NULL);
1165error:
1166 lys_module_free(mod, NULL);
1167 if (pctx) {
1168 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1169 }
1170 if (format == LYS_IN_YANG) {
1171 yang_parser_ctx_free(yangctx);
1172 } else {
1173 yin_parser_ctx_free(yinctx);
1174 }
1175
1176 return ret;
1177}
1178
1179API LY_ERR
1180lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1181{
1182 if (module) {
1183 *module = NULL;
1184 }
1185 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1186
1187 /* remember input position */
1188 in->func_start = in->current;
1189
1190 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001191}
1192
Michal Vasko3a41dff2020-07-15 14:30:28 +02001193API LY_ERR
1194lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001195{
Radek Krejci0f969882020-08-21 16:56:47 +02001196 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001197 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001198
Michal Vasko3a41dff2020-07-15 14:30:28 +02001199 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001200
Michal Vasko3a41dff2020-07-15 14:30:28 +02001201 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 +02001202
Michal Vasko3a41dff2020-07-15 14:30:28 +02001203 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001204 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001205
Michal Vasko3a41dff2020-07-15 14:30:28 +02001206 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001207}
1208
Michal Vasko3a41dff2020-07-15 14:30:28 +02001209API LY_ERR
1210lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001211{
Radek Krejci0f969882020-08-21 16:56:47 +02001212 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001213 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001214
Michal Vasko3a41dff2020-07-15 14:30:28 +02001215 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001216
Michal Vasko3a41dff2020-07-15 14:30:28 +02001217 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 +02001218
Michal Vasko3a41dff2020-07-15 14:30:28 +02001219 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001220 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001221
Michal Vasko3a41dff2020-07-15 14:30:28 +02001222 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001223}
1224
Michal Vasko3a41dff2020-07-15 14:30:28 +02001225API LY_ERR
1226lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001227{
Radek Krejci0f969882020-08-21 16:56:47 +02001228 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001229 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001230
Michal Vasko3a41dff2020-07-15 14:30:28 +02001231 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001232
Michal Vasko3a41dff2020-07-15 14:30:28 +02001233 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001234 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001235
Michal Vasko3a41dff2020-07-15 14:30:28 +02001236 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001237 ly_in_free(in, 0);
1238
Michal Vasko3a41dff2020-07-15 14:30:28 +02001239 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001240}
1241
1242API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001243lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001244 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001245{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001246 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001247 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001248 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001249 char *wd, *wn = NULL;
1250 DIR *dir = NULL;
1251 struct dirent *file;
1252 char *match_name = NULL;
1253 LYS_INFORMAT format_aux, match_format = 0;
1254 struct ly_set *dirs;
1255 struct stat st;
1256
1257 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1258
1259 /* start to fill the dir fifo with the context's search path (if set)
1260 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001261 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001262
1263 len = strlen(name);
1264 if (cwd) {
1265 wd = get_current_dir_name();
1266 if (!wd) {
1267 LOGMEM(NULL);
1268 goto cleanup;
1269 } else {
1270 /* add implicit current working directory (./) to be searched,
1271 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001272 ret = ly_set_add(dirs, wd, 0, NULL);
1273 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001274 implicit_cwd = 1;
1275 }
1276 }
1277 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001278 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001279 /* check for duplicities with the implicit current working directory */
1280 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1281 implicit_cwd = 0;
1282 continue;
1283 }
1284 wd = strdup(searchpaths[i]);
1285 if (!wd) {
1286 LOGMEM(NULL);
1287 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001288 } else {
1289 ret = ly_set_add(dirs, wd, 0, NULL);
1290 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001291 }
1292 }
1293 }
1294 wd = NULL;
1295
1296 /* start searching */
1297 while (dirs->count) {
1298 free(wd);
1299 free(wn); wn = NULL;
1300
1301 dirs->count--;
1302 wd = (char *)dirs->objs[dirs->count];
1303 dirs->objs[dirs->count] = NULL;
1304 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1305
1306 if (dir) {
1307 closedir(dir);
1308 }
1309 dir = opendir(wd);
1310 dir_len = strlen(wd);
1311 if (!dir) {
1312 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1313 } else {
1314 while ((file = readdir(dir))) {
1315 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1316 /* skip . and .. */
1317 continue;
1318 }
1319 free(wn);
1320 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1321 LOGMEM(NULL);
1322 goto cleanup;
1323 }
1324 if (stat(wn, &st) == -1) {
1325 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001326 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001327 continue;
1328 }
1329 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1330 /* we have another subdirectory in searchpath to explore,
1331 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001332 ret = ly_set_add(dirs, wn, 0, NULL);
1333 LY_CHECK_GOTO(ret, cleanup);
1334
Radek Krejcid33273d2018-10-25 14:55:52 +02001335 /* continue with the next item in current directory */
1336 wn = NULL;
1337 continue;
1338 } else if (!S_ISREG(st.st_mode)) {
1339 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1340 continue;
1341 }
1342
1343 /* here we know that the item is a file which can contain a module */
1344 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001345 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001346 /* different filename than the module we search for */
1347 continue;
1348 }
1349
1350 /* get type according to filename suffix */
1351 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001352 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001353 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001354 /* TODO YIN parser
1355 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1356 format_aux = LYS_IN_YIN;
1357 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001358 } else {
1359 /* not supportde suffix/file format */
1360 continue;
1361 }
1362
1363 if (revision) {
1364 /* we look for the specific revision, try to get it from the filename */
1365 if (file->d_name[len] == '@') {
1366 /* check revision from the filename */
1367 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1368 /* another revision */
1369 continue;
1370 } else {
1371 /* exact revision */
1372 free(match_name);
1373 match_name = wn;
1374 wn = NULL;
1375 match_len = dir_len + 1 + len;
1376 match_format = format_aux;
1377 goto success;
1378 }
1379 } else {
1380 /* continue trying to find exact revision match, use this only if not found */
1381 free(match_name);
1382 match_name = wn;
1383 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001384 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001385 match_format = format_aux;
1386 continue;
1387 }
1388 } else {
1389 /* remember the revision and try to find the newest one */
1390 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001391 if ((file->d_name[len] != '@') ||
1392 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 +02001393 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001394 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001395 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1396 continue;
1397 }
1398 free(match_name);
1399 }
1400
1401 match_name = wn;
1402 wn = NULL;
1403 match_len = dir_len + 1 + len;
1404 match_format = format_aux;
1405 continue;
1406 }
1407 }
1408 }
1409 }
1410
1411success:
1412 (*localfile) = match_name;
1413 match_name = NULL;
1414 if (format) {
1415 (*format) = match_format;
1416 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001417 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001418
1419cleanup:
1420 free(wn);
1421 free(wd);
1422 if (dir) {
1423 closedir(dir);
1424 }
1425 free(match_name);
1426 ly_set_free(dirs, free);
1427
1428 return ret;
1429}