blob: ace1ee8809bf12f1ea8e0bbee18a61cb756d8057 [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 Vaskoc8a230d2020-08-14 12:17:10 +0200248 ret = lyxp_atomize(exp, LY_PREF_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &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 Vaskoc8a230d2020-08-14 12:17:10 +0200293 ret = lyxp_atomize(exp, LY_PREF_JSON, ctx_node->module, ctx_node, LYXP_NODE_ELEM, &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:
961 lysp_submodule_free(ctx, 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 Vasko3a41dff2020-07-15 14:30:28 +0200976 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200977 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100978 struct lys_yang_parser_ctx *yangctx = NULL;
979 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200980 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200981 char *filename, *rev, *dot;
982 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +0200983
Michal Vasko3a41dff2020-07-15 14:30:28 +0200984 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200985 if (module) {
986 *module = NULL;
987 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200988
989 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200990 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100991 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200992
993 switch (format) {
994 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200995 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100996 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200997 break;
998 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200999 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +01001000 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +02001001 break;
1002 default:
1003 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001005 break;
1006 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001007 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001008
1009 /* make sure that the newest revision is at position 0 */
1010 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001011 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001012 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001013 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001014
Radek Krejcib3289d62019-09-18 12:21:39 +02001015 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001016 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001017 if (latest) {
1018 if (mod->revision) {
1019 if (!latest->revision) {
1020 /* latest has no revision, so mod is anyway newer */
1021 mod->latest_revision = latest->latest_revision;
1022 /* the latest is zeroed later when the new module is being inserted into the context */
1023 } else if (strcmp(mod->revision, latest->revision) > 0) {
1024 mod->latest_revision = latest->latest_revision;
1025 /* the latest is zeroed later when the new module is being inserted into the context */
1026 } else {
1027 latest = NULL;
1028 }
1029 } else {
1030 latest = NULL;
1031 }
1032 } else {
1033 mod->latest_revision = 1;
1034 }
1035
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001036 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001037 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001038 }
1039
Radek Krejci86d106e2018-10-18 09:53:19 +02001040 if (implement) {
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001041 /* mark the loaded module implemented */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001042 if (ly_ctx_get_module_implemented(ctx, mod->name)) {
1043 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001044 ret = LY_EDENIED;
Radek Krejcibbe09a92018-11-08 09:36:54 +01001045 goto error;
Radek Krejcib7db73a2018-10-24 14:18:40 +02001046 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001047 }
1048
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001049 /* check for duplicity in the context */
Michal Vasko22df3f02020-08-24 13:29:22 +02001050 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001051 if (mod_dup) {
1052 if (mod_dup->parsed) {
1053 /* error */
Radek Krejcid33273d2018-10-25 14:55:52 +02001054 if (mod->parsed->revs) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001055 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001056 mod->name, mod->parsed->revs[0].date);
Radek Krejcid33273d2018-10-25 14:55:52 +02001057 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001058 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
Michal Vasko69730152020-10-09 16:30:07 +02001059 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001060 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001061 ret = LY_EEXIST;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 goto error;
1063 } else {
1064 /* add the parsed data to the currently compiled-only module in the context */
1065 mod_dup->parsed = mod->parsed;
1066 mod_dup->parsed->mod = mod_dup;
1067 mod->parsed = NULL;
1068 lys_module_free(mod, NULL);
1069 mod = mod_dup;
1070 goto finish_parsing;
Radek Krejcid33273d2018-10-25 14:55:52 +02001071 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001072 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001073
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001074 switch (in->type) {
1075 case LY_IN_FILEPATH:
1076 /* check that name and revision match filename */
1077 filename = strrchr(in->method.fpath.filepath, '/');
1078 if (!filename) {
1079 filename = in->method.fpath.filepath;
1080 } else {
1081 filename++;
1082 }
1083 rev = strchr(filename, '@');
1084 dot = strrchr(filename, '.');
1085
1086 /* name */
1087 len = strlen(mod->name);
1088 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001089 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001090 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1091 }
1092 if (rev) {
1093 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001094 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001095 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001096 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001097 }
1098 }
1099
1100 break;
1101 case LY_IN_FD:
1102 case LY_IN_FILE:
1103 case LY_IN_MEMORY:
1104 /* nothing special to do */
1105 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001106 case LY_IN_ERROR:
1107 LOGINT(ctx);
1108 ret = LY_EINT;
1109 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001110 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001111
1112 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001113
Michal Vasko89b5c072020-10-06 13:52:44 +02001114 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001115 /* pre-compile features and identities of the module */
Radek Krejci14915cc2020-09-14 17:28:13 +02001116 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->features, &mod->features), error);
Radek Krejci80d281e2020-09-14 17:42:54 +02001117 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->identities, &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001118 }
1119
1120 if (latest) {
1121 latest->latest_revision = 0;
1122 }
1123
1124 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001125 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001126 LY_CHECK_GOTO(ret, error);
1127 ctx->module_set_id++;
1128
1129finish_parsing:
1130 /* resolve imports and includes */
1131 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1132
Michal Vasko89b5c072020-10-06 13:52:44 +02001133 if (!implement) {
Michal Vasko7a0b0762020-09-02 16:37:01 +02001134 /* pre-compile features and identities of any submodules */
1135 LY_ARRAY_FOR(mod->parsed->includes, u) {
1136 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->features,
Michal Vasko69730152020-10-09 16:30:07 +02001137 &mod->features), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001138 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod, mod->parsed->includes[u].submodule->identities,
Michal Vasko69730152020-10-09 16:30:07 +02001139 &mod->identities), error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001140 }
1141 }
1142
1143 /* check name collisions - typedefs and TODO groupings */
1144 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1145
Michal Vasko89b5c072020-10-06 13:52:44 +02001146 if (implement) {
1147 /* implement (compile) */
1148 LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001149 }
1150
1151 if (format == LYS_IN_YANG) {
1152 yang_parser_ctx_free(yangctx);
1153 } else {
1154 yin_parser_ctx_free(yinctx);
1155 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001156 if (module) {
1157 *module = mod;
1158 }
1159 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001160
1161error_ctx:
1162 ly_set_rm(&ctx->list, mod, NULL);
1163error:
1164 lys_module_free(mod, NULL);
1165 if (pctx) {
1166 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1167 }
1168 if (format == LYS_IN_YANG) {
1169 yang_parser_ctx_free(yangctx);
1170 } else {
1171 yin_parser_ctx_free(yinctx);
1172 }
1173
1174 return ret;
1175}
1176
1177API LY_ERR
1178lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1179{
1180 if (module) {
1181 *module = NULL;
1182 }
1183 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1184
1185 /* remember input position */
1186 in->func_start = in->current;
1187
1188 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001189}
1190
Michal Vasko3a41dff2020-07-15 14:30:28 +02001191API LY_ERR
1192lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001193{
Radek Krejci0f969882020-08-21 16:56:47 +02001194 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001195 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001196
Michal Vasko3a41dff2020-07-15 14:30:28 +02001197 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001198
Michal Vasko3a41dff2020-07-15 14:30:28 +02001199 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 +02001200
Michal Vasko3a41dff2020-07-15 14:30:28 +02001201 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001202 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001203
Michal Vasko3a41dff2020-07-15 14:30:28 +02001204 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001205}
1206
Michal Vasko3a41dff2020-07-15 14:30:28 +02001207API LY_ERR
1208lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001209{
Radek Krejci0f969882020-08-21 16:56:47 +02001210 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001211 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001212
Michal Vasko3a41dff2020-07-15 14:30:28 +02001213 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001214
Michal Vasko3a41dff2020-07-15 14:30:28 +02001215 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 +02001216
Michal Vasko3a41dff2020-07-15 14:30:28 +02001217 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001218 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001219
Michal Vasko3a41dff2020-07-15 14:30:28 +02001220 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001221}
1222
Michal Vasko3a41dff2020-07-15 14:30:28 +02001223API LY_ERR
1224lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001225{
Radek Krejci0f969882020-08-21 16:56:47 +02001226 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001227 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001228
Michal Vasko3a41dff2020-07-15 14:30:28 +02001229 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001230
Michal Vasko3a41dff2020-07-15 14:30:28 +02001231 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001232 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001233
Michal Vasko3a41dff2020-07-15 14:30:28 +02001234 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001235 ly_in_free(in, 0);
1236
Michal Vasko3a41dff2020-07-15 14:30:28 +02001237 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001238}
1239
1240API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001241lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001242 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001243{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001244 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001245 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001246 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001247 char *wd, *wn = NULL;
1248 DIR *dir = NULL;
1249 struct dirent *file;
1250 char *match_name = NULL;
1251 LYS_INFORMAT format_aux, match_format = 0;
1252 struct ly_set *dirs;
1253 struct stat st;
1254
1255 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1256
1257 /* start to fill the dir fifo with the context's search path (if set)
1258 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001259 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001260
1261 len = strlen(name);
1262 if (cwd) {
1263 wd = get_current_dir_name();
1264 if (!wd) {
1265 LOGMEM(NULL);
1266 goto cleanup;
1267 } else {
1268 /* add implicit current working directory (./) to be searched,
1269 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001270 ret = ly_set_add(dirs, wd, 0, NULL);
1271 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001272 implicit_cwd = 1;
1273 }
1274 }
1275 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001276 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001277 /* check for duplicities with the implicit current working directory */
1278 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1279 implicit_cwd = 0;
1280 continue;
1281 }
1282 wd = strdup(searchpaths[i]);
1283 if (!wd) {
1284 LOGMEM(NULL);
1285 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001286 } else {
1287 ret = ly_set_add(dirs, wd, 0, NULL);
1288 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001289 }
1290 }
1291 }
1292 wd = NULL;
1293
1294 /* start searching */
1295 while (dirs->count) {
1296 free(wd);
1297 free(wn); wn = NULL;
1298
1299 dirs->count--;
1300 wd = (char *)dirs->objs[dirs->count];
1301 dirs->objs[dirs->count] = NULL;
1302 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1303
1304 if (dir) {
1305 closedir(dir);
1306 }
1307 dir = opendir(wd);
1308 dir_len = strlen(wd);
1309 if (!dir) {
1310 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1311 } else {
1312 while ((file = readdir(dir))) {
1313 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1314 /* skip . and .. */
1315 continue;
1316 }
1317 free(wn);
1318 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1319 LOGMEM(NULL);
1320 goto cleanup;
1321 }
1322 if (stat(wn, &st) == -1) {
1323 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001324 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001325 continue;
1326 }
1327 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1328 /* we have another subdirectory in searchpath to explore,
1329 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001330 ret = ly_set_add(dirs, wn, 0, NULL);
1331 LY_CHECK_GOTO(ret, cleanup);
1332
Radek Krejcid33273d2018-10-25 14:55:52 +02001333 /* continue with the next item in current directory */
1334 wn = NULL;
1335 continue;
1336 } else if (!S_ISREG(st.st_mode)) {
1337 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1338 continue;
1339 }
1340
1341 /* here we know that the item is a file which can contain a module */
1342 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001343 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001344 /* different filename than the module we search for */
1345 continue;
1346 }
1347
1348 /* get type according to filename suffix */
1349 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001350 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001351 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001352 /* TODO YIN parser
1353 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1354 format_aux = LYS_IN_YIN;
1355 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001356 } else {
1357 /* not supportde suffix/file format */
1358 continue;
1359 }
1360
1361 if (revision) {
1362 /* we look for the specific revision, try to get it from the filename */
1363 if (file->d_name[len] == '@') {
1364 /* check revision from the filename */
1365 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1366 /* another revision */
1367 continue;
1368 } else {
1369 /* exact revision */
1370 free(match_name);
1371 match_name = wn;
1372 wn = NULL;
1373 match_len = dir_len + 1 + len;
1374 match_format = format_aux;
1375 goto success;
1376 }
1377 } else {
1378 /* continue trying to find exact revision match, use this only if not found */
1379 free(match_name);
1380 match_name = wn;
1381 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001382 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001383 match_format = format_aux;
1384 continue;
1385 }
1386 } else {
1387 /* remember the revision and try to find the newest one */
1388 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001389 if ((file->d_name[len] != '@') ||
1390 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 +02001391 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001392 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001393 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1394 continue;
1395 }
1396 free(match_name);
1397 }
1398
1399 match_name = wn;
1400 wn = NULL;
1401 match_len = dir_len + 1 + len;
1402 match_format = format_aux;
1403 continue;
1404 }
1405 }
1406 }
1407 }
1408
1409success:
1410 (*localfile) = match_name;
1411 match_name = NULL;
1412 if (format) {
1413 (*format) = match_format;
1414 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001415 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001416
1417cleanup:
1418 free(wn);
1419 free(wd);
1420 if (dir) {
1421 closedir(dir);
1422 }
1423 free(match_name);
1424 ly_set_free(dirs, free);
1425
1426 return ret;
1427}