blob: f1a170f243c09abe68eec518d75ab5a1887c3b68 [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"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile.h"
38#include "schema_compile_amend.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020039#include "set.h"
40#include "tree.h"
41#include "tree_schema_internal.h"
42#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020043
Radek Krejcia3045382018-11-22 14:30:31 +010044API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +020045lys_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 +010046{
Radek Krejci6eeb58f2019-02-22 16:29:37 +010047 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +010048 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +020049 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010050 const struct lysc_action *actions;
51 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +020052 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +010053
54 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
55
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020056next:
Radek Krejcia3045382018-11-22 14:30:31 +010057 if (!last) {
58 /* first call */
59
60 /* get know where to start */
61 if (parent) {
62 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +020063 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020064 if (((struct lysc_node_choice *)parent)->cases) {
65 next = last = (const struct lysc_node *)&((struct lysc_node_choice *)parent)->cases[0];
Radek Krejci056d0a82018-12-06 16:57:25 +010066 }
Radek Krejci056d0a82018-12-06 16:57:25 +010067 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010068 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +010069 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +020070 if (snode && *snode) {
71 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +010072 }
Radek Krejcia3045382018-11-22 14:30:31 +010073 }
Radek Krejcia3045382018-11-22 14:30:31 +010074 } else {
75 /* top level data */
76 next = last = module->data;
77 }
78 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010079 /* try to get action or notification */
80 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +010081 }
Radek Krejci05b774b2019-02-25 13:26:18 +010082 /* test if the next can be returned */
83 goto check;
84
Michal Vasko1bf09392020-03-27 12:38:10 +010085 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +010086 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +010087 if (last->parent) {
88 actions = lysc_node_actions(last->parent);
89 } else {
90 actions = module->rpcs;
91 }
92 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +020093 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +010094 break;
95 }
96 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +020097 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +020098 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +010099 }
100 goto repeat;
101 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100102 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100103 if (last->parent) {
104 notifs = lysc_node_notifs(last->parent);
105 } else {
106 notifs = module->notifs;
107 }
108 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200109 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100110 break;
111 }
112 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200113 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200114 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100115 }
116 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200117 } else {
118 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100119 }
120
Radek Krejcia3045382018-11-22 14:30:31 +0100121repeat:
122 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100123 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200124 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100125 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200126 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100127 } else if (!action_flag) {
128 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200129 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100130 } else if (!notif_flag) {
131 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200132 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100133 } else {
134 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100135 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100136 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100137 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100138check:
Radek Krejcia3045382018-11-22 14:30:31 +0100139 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100140 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100141 case LYS_ACTION:
142 case LYS_NOTIF:
143 case LYS_LEAF:
144 case LYS_ANYXML:
145 case LYS_ANYDATA:
146 case LYS_LIST:
147 case LYS_LEAFLIST:
148 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200149 case LYS_CASE:
150 if (options & LYS_GETNEXT_WITHCASE) {
151 break;
152 } else {
153 /* go into */
154 next = ((struct lysc_node_case *)next)->child;
155 }
156 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100157 case LYS_CONTAINER:
158 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
159 if (((struct lysc_node_container *)next)->child) {
160 /* go into */
161 next = ((struct lysc_node_container *)next)->child;
162 } else {
163 next = next->next;
164 }
165 goto repeat;
166 }
167 break;
168 case LYS_CHOICE:
169 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200170 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100171 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
172 next = next->next;
173 } else {
Radek Krejcia3045382018-11-22 14:30:31 +0100174 /* go into */
Radek Krejcia9026eb2018-12-12 16:04:47 +0100175 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200176 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100177 } else {
178 next = ((struct lysc_node_choice *)next)->cases->child;
179 }
Radek Krejcia3045382018-11-22 14:30:31 +0100180 }
181 goto repeat;
182 default:
183 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200184 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100185 return NULL;
186 }
187
188 if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
189 /* check if the node is disabled by if-feature */
Radek Krejcifab954b2019-09-11 11:25:14 +0200190 if (lysc_node_is_disabled(next, 0)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100191 next = next->next;
192 goto repeat;
193 }
194 }
195 return next;
196}
197
198API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100199lys_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 +0200200 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100201{
202 const struct lysc_node *node = NULL;
203
204 LY_CHECK_ARG_RET(NULL, module, name, NULL);
205 if (!nodetype) {
206 nodetype = 0xffff;
207 }
208
209 while ((node = lys_getnext(node, parent, module->compiled, options))) {
210 if (!(node->nodetype & nodetype)) {
211 continue;
212 }
213 if (node->module != module) {
214 continue;
215 }
216
217 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200218 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100219 return node;
220 }
221 } else {
222 if (!strcmp(node->name, name)) {
223 return node;
224 }
225 }
226 }
227 return NULL;
228}
229
Michal Vasko519fd602020-05-26 12:17:39 +0200230API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200231lys_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 +0200232{
233 LY_ERR ret = LY_SUCCESS;
234 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200235 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200236 uint32_t i;
237
238 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
239 if (!(options & LYXP_SCNODE_ALL)) {
240 options = LYXP_SCNODE;
241 }
242
243 memset(&xp_set, 0, sizeof xp_set);
244
245 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200246 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
247 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200248
249 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200250 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200251 LY_CHECK_GOTO(ret, cleanup);
252
253 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200254 ret = ly_set_new(set);
255 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200256
257 /* transform into ly_set */
258 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
259 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
260 (*set)->size = xp_set.used;
261
262 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200263 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200264 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200265 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200266 }
267 }
268
269cleanup:
270 lyxp_set_free_content(&xp_set);
271 lyxp_expr_free(ctx_node->module->ctx, exp);
272 return ret;
273}
274
Michal Vasko072de482020-08-05 13:27:21 +0200275API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200276lys_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 +0200277{
278 LY_ERR ret = LY_SUCCESS;
279 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200280 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200281 uint32_t i;
282
283 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
284 if (!(options & LYXP_SCNODE_ALL)) {
285 options = LYXP_SCNODE;
286 }
287
288 memset(&xp_set, 0, sizeof xp_set);
289
290 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200291 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
292 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200293
294 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200295 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200296 LY_CHECK_GOTO(ret, cleanup);
297
298 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200299 ret = ly_set_new(set);
300 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200301
302 /* transform into ly_set */
303 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
304 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
305 (*set)->size = xp_set.used;
306
307 for (i = 0; i < xp_set.used; ++i) {
308 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 +0200309 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200310 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200311 }
312 }
313
314cleanup:
315 lyxp_set_free_content(&xp_set);
316 lyxp_expr_free(ctx_node->module->ctx, exp);
317 return ret;
318}
319
Michal Vasko14654712020-02-06 08:35:21 +0100320char *
321lysc_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 +0200322 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200323{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200324 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200325 char *path = NULL;
326 int len = 0;
327
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200328 LY_CHECK_ARG_RET(NULL, node, NULL);
329 if (buffer) {
330 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
331 }
332
Radek Krejci327de162019-06-14 12:52:07 +0200333 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200334 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200335 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100336 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200337 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100338 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200339
Michal Vasko65de0402020-08-03 16:34:19 +0200340 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
341 /* schema-only node */
342 continue;
343 }
344
Michal Vasko11deea12020-08-05 13:54:50 +0200345 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200346 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100347 if (parent && (iter->parent == parent)) {
348 slash = "";
349 } else {
350 slash = "/";
351 }
Michal Vasko69730152020-10-09 16:30:07 +0200352 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200353 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200354 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100355 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200356 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100357 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200358 }
Radek Krejci327de162019-06-14 12:52:07 +0200359 } else {
360 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200361 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100362 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200363 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100364 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200365 }
Radek Krejci327de162019-06-14 12:52:07 +0200366 }
367 free(s);
368 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200369
Michal Vasko69730152020-10-09 16:30:07 +0200370 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200371 /* not enough space in buffer */
372 break;
373 }
Radek Krejci327de162019-06-14 12:52:07 +0200374 }
375
376 if (len < 0) {
377 free(path);
378 path = NULL;
379 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200380 if (buffer) {
381 strcpy(buffer, "/");
382 } else {
383 path = strdup("/");
384 }
Radek Krejci327de162019-06-14 12:52:07 +0200385 }
386 break;
387 }
388
Radek Krejci1c0c3442019-07-23 16:08:47 +0200389 if (buffer) {
390 return buffer;
391 } else {
392 return path;
393 }
Radek Krejci327de162019-06-14 12:52:07 +0200394}
395
Michal Vasko14654712020-02-06 08:35:21 +0100396API char *
397lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
398{
399 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
400}
401
Michal Vasko28d78432020-05-26 13:10:53 +0200402API LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +0100403lysc_feature_value(const struct lysc_feature *feature)
Radek Krejci6f7feb62018-10-12 15:23:02 +0200404{
Michal Vasko28d78432020-05-26 13:10:53 +0200405 LY_CHECK_ARG_RET(NULL, feature, LY_EINVAL);
406 return feature->flags & LYS_FENABLED ? LY_SUCCESS : LY_ENOT;
Radek Krejci151a5b72018-10-19 14:21:44 +0200407}
408
Radek Krejci693262f2019-04-29 15:23:20 +0200409uint8_t
Radek Krejci1deb5be2020-08-26 16:43:36 +0200410lysc_iff_getop(uint8_t *list, size_t pos)
Radek Krejci151a5b72018-10-19 14:21:44 +0200411{
412 uint8_t *item;
413 uint8_t mask = 3, result;
414
Radek Krejci151a5b72018-10-19 14:21:44 +0200415 item = &list[pos / 4];
416 result = (*item) & (mask << 2 * (pos % 4));
417 return result >> 2 * (pos % 4);
418}
419
Michal Vasko28d78432020-05-26 13:10:53 +0200420static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200421lysc_iffeature_value_(const struct lysc_iffeature *iff, size_t *index_e, size_t *index_f)
Radek Krejci151a5b72018-10-19 14:21:44 +0200422{
423 uint8_t op;
Michal Vasko28d78432020-05-26 13:10:53 +0200424 LY_ERR a, b;
Radek Krejci151a5b72018-10-19 14:21:44 +0200425
Radek Krejci693262f2019-04-29 15:23:20 +0200426 op = lysc_iff_getop(iff->expr, *index_e);
Radek Krejci151a5b72018-10-19 14:21:44 +0200427 (*index_e)++;
428
429 switch (op) {
430 case LYS_IFF_F:
431 /* resolve feature */
Radek Krejci2c4e7172018-10-19 15:56:26 +0200432 return lysc_feature_value(iff->features[(*index_f)++]);
Radek Krejci151a5b72018-10-19 14:21:44 +0200433 case LYS_IFF_NOT:
434 /* invert result */
Michal Vasko28d78432020-05-26 13:10:53 +0200435 return lysc_iffeature_value_(iff, index_e, index_f) == LY_SUCCESS ? LY_ENOT : LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200436 case LYS_IFF_AND:
437 case LYS_IFF_OR:
438 a = lysc_iffeature_value_(iff, index_e, index_f);
439 b = lysc_iffeature_value_(iff, index_e, index_f);
440 if (op == LYS_IFF_AND) {
Michal Vasko28d78432020-05-26 13:10:53 +0200441 if ((a == LY_SUCCESS) && (b == LY_SUCCESS)) {
442 return LY_SUCCESS;
443 } else {
444 return LY_ENOT;
445 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200446 } else { /* LYS_IFF_OR */
Michal Vasko28d78432020-05-26 13:10:53 +0200447 if ((a == LY_SUCCESS) || (b == LY_SUCCESS)) {
448 return LY_SUCCESS;
449 } else {
450 return LY_ENOT;
451 }
Radek Krejci151a5b72018-10-19 14:21:44 +0200452 }
453 }
454
455 return 0;
456}
457
Michal Vasko28d78432020-05-26 13:10:53 +0200458API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200459lysc_iffeature_value(const struct lysc_iffeature *iff)
460{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200461 size_t index_e = 0, index_f = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200462
463 LY_CHECK_ARG_RET(NULL, iff, -1);
464
465 if (iff->expr) {
466 return lysc_iffeature_value_(iff, &index_e, &index_f);
467 }
468 return 0;
469}
470
Radek Krejci151a5b72018-10-19 14:21:44 +0200471/**
472 * @brief Enable/Disable the specified feature in the module.
473 *
474 * If the feature is already set to the desired value, LY_SUCCESS is returned.
475 * By changing the feature, also all the feature which depends on it via their
476 * if-feature statements are again evaluated (disabled if a if-feature statemen
477 * evaluates to false).
478 *
Radek Krejci0af46292019-01-11 16:02:31 +0100479 * @param[in] mod Module where to set (search for) the feature.
Radek Krejci151a5b72018-10-19 14:21:44 +0200480 * @param[in] name Name of the feature to set. Asterisk ('*') can be used to
481 * set all the features in the module.
482 * @param[in] value Desired value of the feature: 1 (enable) or 0 (disable).
Radek Krejci857189e2020-09-01 13:26:36 +0200483 * @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 +0200484 * @return LY_ERR value.
485 */
486static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200487lys_feature_change(const struct lys_module *mod, const char *name, ly_bool value, ly_bool skip_checks)
Radek Krejci151a5b72018-10-19 14:21:44 +0200488{
Michal Vaskob0099a92020-08-31 14:55:23 +0200489 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200490 ly_bool all = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200491 LY_ARRAY_COUNT_TYPE u, disabled_count;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200492 uint32_t changed_count;
Radek Krejci151a5b72018-10-19 14:21:44 +0200493 struct lysc_feature *f, **df;
494 struct lysc_iffeature *iff;
495 struct ly_set *changed;
Radek Krejci0af46292019-01-11 16:02:31 +0100496 struct ly_ctx *ctx = mod->ctx; /* shortcut */
Radek Krejci151a5b72018-10-19 14:21:44 +0200497
Radek Krejci6e67c402019-05-02 09:55:39 +0200498 if (!strcmp(name, "*")) {
499 /* enable all */
500 all = 1;
501 }
502
Michal Vasko997f9822020-10-14 13:09:05 +0200503 if (!mod->implemented) {
504 LOGERR(ctx, LY_EINVAL, "Module \"%s\" is not implemented so all its features are permanently disabled.", mod->name);
Radek Krejci0af46292019-01-11 16:02:31 +0100505 return LY_EINVAL;
506 }
Radek Krejci14915cc2020-09-14 17:28:13 +0200507 if (!mod->features) {
Radek Krejci6e67c402019-05-02 09:55:39 +0200508 if (all) {
509 /* no feature to enable */
510 return LY_SUCCESS;
511 }
Radek Krejci0af46292019-01-11 16:02:31 +0100512 LOGERR(ctx, LY_EINVAL, "Unable to switch feature since the module \"%s\" has no features.", mod->name);
Michal Vasko82c31e62020-07-17 15:30:40 +0200513 return LY_ENOTFOUND;
Radek Krejci151a5b72018-10-19 14:21:44 +0200514 }
515
Radek Krejciba03a5a2020-08-27 14:40:41 +0200516 LY_CHECK_RET(ly_set_new(&changed));
Radek Krejcica3db002018-11-01 10:31:01 +0100517 changed_count = 0;
Radek Krejci151a5b72018-10-19 14:21:44 +0200518
Radek Krejcica3db002018-11-01 10:31:01 +0100519run:
Radek Krejci14915cc2020-09-14 17:28:13 +0200520 for (disabled_count = u = 0; u < LY_ARRAY_COUNT(mod->features); ++u) {
521 f = &mod->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200522 if (all || !strcmp(f->name, name)) {
523 if ((value && (f->flags & LYS_FENABLED)) || (!value && !(f->flags & LYS_FENABLED))) {
524 if (all) {
525 /* skip already set features */
526 continue;
527 } else {
528 /* feature already set correctly */
Michal Vaskob0099a92020-08-31 14:55:23 +0200529 goto cleanup;
Radek Krejci151a5b72018-10-19 14:21:44 +0200530 }
531 }
532
533 if (value) { /* enable */
Michal Vasko82c31e62020-07-17 15:30:40 +0200534 if (!skip_checks) {
535 /* check referenced features if they are enabled */
536 LY_ARRAY_FOR(f->iffeatures, struct lysc_iffeature, iff) {
537 if (lysc_iffeature_value(iff) == LY_ENOT) {
538 if (all) {
539 ++disabled_count;
540 goto next;
541 } else {
Michal Vasko997f9822020-10-14 13:09:05 +0200542 LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by "
543 "its if-feature condition(s).", 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)) {
Michal Vasko997f9822020-10-14 13:09:05 +0200582 LOGERR(ctx, LY_EDENIED, "Feature \"%s\" cannot be enabled since it is disabled by its if-feature "
583 "condition(s).", mod->features[u].name);
Radek Krejcica3db002018-11-01 10:31:01 +0100584 --disabled_count;
585 }
586 }
587 /* ... restore the original state */
588 for (u = 0; u < changed->count; ++u) {
589 f = changed->objs[u];
590 /* re-disable the feature */
591 f->flags &= ~LYS_FENABLED;
592 }
593
Michal Vaskob0099a92020-08-31 14:55:23 +0200594 ret = LY_EDENIED;
595 goto cleanup;
Radek Krejcica3db002018-11-01 10:31:01 +0100596 } else {
597 /* we did some change in last run, try it again */
598 changed_count = changed->count;
599 goto run;
600 }
601 }
602
Radek Krejci151a5b72018-10-19 14:21:44 +0200603 /* reflect change(s) in the dependent features */
Michal Vasko82c31e62020-07-17 15:30:40 +0200604 for (u = 0; !skip_checks && (u < changed->count); ++u) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200605 /* If a dependent feature is enabled, it can be now changed by the change (to false) of the value of
606 * its if-feature statements. The reverse logic, automatically enable feature when its feature is enabled
607 * is not done - by default, features are disabled and must be explicitely enabled. */
608 f = changed->objs[u];
Michal Vasko22df3f02020-08-24 13:29:22 +0200609 LY_ARRAY_FOR(f->depfeatures, struct lysc_feature *, df) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200610 if (!((*df)->flags & LYS_FENABLED)) {
611 /* not enabled, nothing to do */
612 continue;
613 }
614 /* check the feature's if-features which could change by the previous change of our feature */
615 LY_ARRAY_FOR((*df)->iffeatures, struct lysc_iffeature, iff) {
Michal Vasko28d78432020-05-26 13:10:53 +0200616 if (lysc_iffeature_value(iff) == LY_ENOT) {
Radek Krejci151a5b72018-10-19 14:21:44 +0200617 /* the feature must be disabled now */
618 (*df)->flags &= ~LYS_FENABLED;
619 /* add the feature into the list of changed features */
Radek Krejci3d92e442020-10-12 12:48:13 +0200620 ret = ly_set_add(changed, *df, 1, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200621 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci151a5b72018-10-19 14:21:44 +0200622 break;
623 }
624 }
625 }
626 }
627
Michal Vasko87a97f42020-10-06 10:30:28 +0200628 /* success */
629 ++mod->ctx->module_set_id;
630
Michal Vaskob0099a92020-08-31 14:55:23 +0200631cleanup:
Radek Krejci151a5b72018-10-19 14:21:44 +0200632 ly_set_free(changed, NULL);
Michal Vaskob0099a92020-08-31 14:55:23 +0200633 return ret;
Radek Krejci151a5b72018-10-19 14:21:44 +0200634}
635
636API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200637lys_feature_enable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200638{
Radek Krejci0af46292019-01-11 16:02:31 +0100639 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200640
Michal Vasko22df3f02020-08-24 13:29:22 +0200641 return lys_feature_change((struct lys_module *)module, feature, 1, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200642}
643
644API LY_ERR
Radek Krejcied5acc52019-04-25 15:57:04 +0200645lys_feature_disable(const struct lys_module *module, const char *feature)
Radek Krejci151a5b72018-10-19 14:21:44 +0200646{
Radek Krejci0af46292019-01-11 16:02:31 +0100647 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
Radek Krejci151a5b72018-10-19 14:21:44 +0200648
Michal Vasko22df3f02020-08-24 13:29:22 +0200649 return lys_feature_change((struct lys_module *)module, feature, 0, 0);
Radek Krejci151a5b72018-10-19 14:21:44 +0200650}
651
Michal Vasko82c31e62020-07-17 15:30:40 +0200652API LY_ERR
653lys_feature_enable_force(const struct lys_module *module, const char *feature)
654{
655 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
656
Michal Vasko22df3f02020-08-24 13:29:22 +0200657 return lys_feature_change((struct lys_module *)module, feature, 1, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200658}
659
660API LY_ERR
661lys_feature_disable_force(const struct lys_module *module, const char *feature)
662{
663 LY_CHECK_ARG_RET(NULL, module, feature, LY_EINVAL);
664
Michal Vasko22df3f02020-08-24 13:29:22 +0200665 return lys_feature_change((struct lys_module *)module, feature, 0, 1);
Michal Vasko82c31e62020-07-17 15:30:40 +0200666}
667
668API LY_ERR
Radek Krejci151a5b72018-10-19 14:21:44 +0200669lys_feature_value(const struct lys_module *module, const char *feature)
670{
Michal Vasko82c31e62020-07-17 15:30:40 +0200671 struct lysc_feature *f = NULL;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200672 LY_ARRAY_COUNT_TYPE u;
Radek Krejci151a5b72018-10-19 14:21:44 +0200673
674 LY_CHECK_ARG_RET(NULL, module, module->compiled, feature, -1);
Radek Krejci151a5b72018-10-19 14:21:44 +0200675
676 /* search for the specified feature */
Radek Krejci14915cc2020-09-14 17:28:13 +0200677 LY_ARRAY_FOR(module->features, u) {
678 f = &module->features[u];
Radek Krejci151a5b72018-10-19 14:21:44 +0200679 if (!strcmp(f->name, feature)) {
Michal Vasko82c31e62020-07-17 15:30:40 +0200680 break;
Radek Krejci151a5b72018-10-19 14:21:44 +0200681 }
682 }
683
684 /* feature definition not found */
Michal Vasko82c31e62020-07-17 15:30:40 +0200685 if (!f) {
686 return LY_ENOTFOUND;
687 }
688
689 /* feature disabled */
690 if (!(f->flags & LYS_FENABLED)) {
691 return LY_ENOT;
692 }
693
694 /* check referenced features if they are enabled */
695 LY_ARRAY_FOR(f->iffeatures, u) {
696 if (lysc_iffeature_value(&f->iffeatures[u]) == LY_ENOT) {
697 /* if-feature disabled */
698 return LY_ENOT;
699 }
700 }
701
702 /* feature enabled */
703 return LY_SUCCESS;
Radek Krejci151a5b72018-10-19 14:21:44 +0200704}
705
Michal Vaskoc193ce92020-03-06 11:04:48 +0100706API const struct lysc_node *
Radek Krejci857189e2020-09-01 13:26:36 +0200707lysc_node_is_disabled(const struct lysc_node *node, ly_bool recursive)
Radek Krejcia3045382018-11-22 14:30:31 +0100708{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200709 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100710
711 LY_CHECK_ARG_RET(NULL, node, NULL);
712
Michal Vaskoc193ce92020-03-06 11:04:48 +0100713 do {
Radek Krejci056d0a82018-12-06 16:57:25 +0100714 if (node->iffeatures) {
Radek Krejcia3045382018-11-22 14:30:31 +0100715 /* check local if-features */
Radek Krejci056d0a82018-12-06 16:57:25 +0100716 LY_ARRAY_FOR(node->iffeatures, u) {
Michal Vasko28d78432020-05-26 13:10:53 +0200717 if (lysc_iffeature_value(&node->iffeatures[u]) == LY_ENOT) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100718 return node;
Radek Krejcia3045382018-11-22 14:30:31 +0100719 }
720 }
721 }
722
723 if (!recursive) {
724 return NULL;
725 }
726
Michal Vaskoc193ce92020-03-06 11:04:48 +0100727 /* go through schema-only parents */
Radek Krejcia3045382018-11-22 14:30:31 +0100728 node = node->parent;
Michal Vaskoc193ce92020-03-06 11:04:48 +0100729 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
730
Radek Krejcia3045382018-11-22 14:30:31 +0100731 return NULL;
732}
733
Radek Krejci19cf8052020-08-18 15:02:38 +0200734API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200735lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200736{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200737 struct lysc_action *act;
738 struct lysc_notif *notif;
739
Radek Krejci19cf8052020-08-18 15:02:38 +0200740 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
741
Radek Krejciaf9cd802020-10-06 21:59:47 +0200742 switch (node->nodetype) {
743 case LYS_CONTAINER:
744 case LYS_CHOICE:
745 case LYS_CASE:
746 case LYS_LEAF:
747 case LYS_LEAFLIST:
748 case LYS_LIST:
749 case LYS_ANYXML:
750 case LYS_ANYDATA:
751 if (prev_priv_p) {
752 *prev_priv_p = node->priv;
753 }
754 ((struct lysc_node *)node)->priv = priv;
755 break;
756 case LYS_RPC:
757 case LYS_ACTION:
758 act = (struct lysc_action *)node;
759 if (prev_priv_p) {
760 *prev_priv_p = act->priv;
761 }
762 act->priv = priv;
763 break;
764 case LYS_NOTIF:
765 notif = (struct lysc_notif *)node;
766 if (prev_priv_p) {
767 *prev_priv_p = notif->priv;
768 }
769 notif->priv = priv;
770 break;
771 default:
772 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200773 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200774
775 return LY_SUCCESS;
776}
777
Michal Vasko89b5c072020-10-06 13:52:44 +0200778API LY_ERR
779lys_set_implemented(struct lys_module *mod)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200780{
Michal Vasko89b5c072020-10-06 13:52:44 +0200781 LY_ERR ret = LY_SUCCESS, r;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200782 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200783 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200784
785 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
786
787 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200788 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200789 return LY_SUCCESS;
790 }
791
792 /* we have module from the current context */
793 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
794 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200795 assert(m != mod);
796
797 /* check collision with other implemented revision */
798 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
799 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
800 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200801 }
802
Michal Vasko89b5c072020-10-06 13:52:44 +0200803 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200804 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200805
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200806 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200807 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200808
809 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200810 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200811
Michal Vasko89b5c072020-10-06 13:52:44 +0200812 if (mod == mod->ctx->implementing.objs[0]) {
813 /* the first module being implemented, consolidate the set */
814 if (ret) {
815 /* failure, full compile revert */
816 for (i = 0; i < mod->ctx->list.count; ++i) {
817 m = mod->ctx->list.objs[i];
818 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
819 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200820
Michal Vasko89b5c072020-10-06 13:52:44 +0200821 /* make the module correctly non-implemented again */
822 m->implemented = 0;
823 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
824 lys_precompile_augments_deviations_revert(mod->ctx, m);
825 }
826
827 /* free the compiled version of the module, if any */
828 lysc_module_free(m->compiled, NULL);
829 m->compiled = NULL;
830
831 if (m->implemented) {
832 /* recompile, must succeed because it was already compiled; hide messages because any
833 * warnings were already printed, are not really relevant, and would hide the real error */
834 uint32_t prev_lo = ly_log_options(0);
835 r = lys_compile(m, 0);
836 ly_log_options(prev_lo);
837 if (r) {
838 LOGERR(mod->ctx, r, "Recompilation of module \"%s\" failed.", m->name);
839 }
840 }
841 }
842 }
843
844 ly_set_erase(&mod->ctx->implementing, NULL);
845 }
846 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200847}
848
Michal Vasko7c8439f2020-08-05 13:25:19 +0200849static LY_ERR
850lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *modp)
851{
852 struct lysp_import *imp;
853 struct lysp_include *inc;
854 LY_ARRAY_COUNT_TYPE u, v;
855
856 modp->parsing = 1;
857 LY_ARRAY_FOR(modp->imports, u) {
858 imp = &modp->imports[u];
859 if (!imp->module) {
860 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, &imp->module));
861 }
862 /* check for importing the same module twice */
863 for (v = 0; v < u; ++v) {
864 if (imp->module == modp->imports[v].module) {
865 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
866 }
867 }
868 }
869 LY_ARRAY_FOR(modp->includes, u) {
870 inc = &modp->includes[u];
871 if (!inc->submodule) {
872 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
873 }
874 }
875 modp->parsing = 0;
876
877 return LY_SUCCESS;
878}
879
Michal Vasko3a41dff2020-07-15 14:30:28 +0200880LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200881lys_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 +0200882 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200883 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200884{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200885 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100886 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100887 struct lys_yang_parser_ctx *yangctx = NULL;
888 struct lys_yin_parser_ctx *yinctx = NULL;
889 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100890
Michal Vasko3a41dff2020-07-15 14:30:28 +0200891 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893 switch (format) {
894 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200895 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100896 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 break;
898 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200899 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100900 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100901 break;
902 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200903 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200904 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200906 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200907 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200908 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100909
910 /* make sure that the newest revision is at position 0 */
911 lysp_sort_revisions(submod->revs);
912
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200914 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100915 if (latest_sp) {
916 if (submod->revs) {
917 if (!latest_sp->revs) {
918 /* latest has no revision, so mod is anyway newer */
919 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200920 /* the latest_sp is zeroed later when the new module is being inserted into the context */
921 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
922 submod->latest_revision = latest_sp->latest_revision;
923 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200925 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200927 } else {
928 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100929 }
930 } else {
931 submod->latest_revision = 1;
932 }
933
Radek Krejcib3289d62019-09-18 12:21:39 +0200934 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200935 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200936 }
937
938 if (latest_sp) {
939 latest_sp->latest_revision = 0;
940 }
941
Michal Vasko7a0b0762020-09-02 16:37:01 +0200942 lys_parser_fill_filepath(ctx, in, &submod->filepath);
943
Michal Vasko7c8439f2020-08-05 13:25:19 +0200944 /* resolve imports and includes */
945 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
946
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100947 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100948 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
949 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100950
David Sedlák1b623122019-08-05 15:27:49 +0200951 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100952 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200953 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100954 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200955 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200956 *submodule = submod;
957 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200958
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100959error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200960 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200961 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100962 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200963 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100964 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200965 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200966 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200967}
968
Michal Vasko3a41dff2020-07-15 14:30:28 +0200969LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200970lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200971 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
972 void *check_data, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200973{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100974 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200975 struct lysp_submodule *submod;
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 */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001116 LY_CHECK_GOTO(ret = lys_feature_precompile(NULL, ctx, mod->parsed, mod->parsed->features, &mod->features), error);
1117 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, 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) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001136 submod = mod->parsed->includes[u].submodule;
1137 ret = lys_feature_precompile(NULL, ctx, (struct lysp_module *)submod, submod->features, &mod->features);
1138 LY_CHECK_GOTO(ret, error);
1139 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1140 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001141 }
1142 }
1143
1144 /* check name collisions - typedefs and TODO groupings */
1145 LY_CHECK_GOTO(ret = lysp_check_typedefs(pctx, mod->parsed), error_ctx);
1146
Michal Vasko89b5c072020-10-06 13:52:44 +02001147 if (implement) {
1148 /* implement (compile) */
1149 LY_CHECK_GOTO(ret = lys_set_implemented(mod), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001150 }
1151
1152 if (format == LYS_IN_YANG) {
1153 yang_parser_ctx_free(yangctx);
1154 } else {
1155 yin_parser_ctx_free(yinctx);
1156 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001157 if (module) {
1158 *module = mod;
1159 }
1160 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001161
1162error_ctx:
1163 ly_set_rm(&ctx->list, mod, NULL);
1164error:
1165 lys_module_free(mod, NULL);
1166 if (pctx) {
1167 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1168 }
1169 if (format == LYS_IN_YANG) {
1170 yang_parser_ctx_free(yangctx);
1171 } else {
1172 yin_parser_ctx_free(yinctx);
1173 }
1174
1175 return ret;
1176}
1177
1178API LY_ERR
1179lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const struct lys_module **module)
1180{
1181 if (module) {
1182 *module = NULL;
1183 }
1184 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1185
1186 /* remember input position */
1187 in->func_start = in->current;
1188
1189 return lys_create_module(ctx, in, format, 1, NULL, NULL, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001190}
1191
Michal Vasko3a41dff2020-07-15 14:30:28 +02001192API LY_ERR
1193lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001194{
Radek Krejci0f969882020-08-21 16:56:47 +02001195 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001196 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001197
Michal Vasko3a41dff2020-07-15 14:30:28 +02001198 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001199
Michal Vasko3a41dff2020-07-15 14:30:28 +02001200 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 +02001201
Michal Vasko3a41dff2020-07-15 14:30:28 +02001202 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001203 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001204
Michal Vasko3a41dff2020-07-15 14:30:28 +02001205 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001206}
1207
Michal Vasko3a41dff2020-07-15 14:30:28 +02001208API LY_ERR
1209lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001210{
Radek Krejci0f969882020-08-21 16:56:47 +02001211 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001212 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001213
Michal Vasko3a41dff2020-07-15 14:30:28 +02001214 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001215
Michal Vasko3a41dff2020-07-15 14:30:28 +02001216 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 +02001217
Michal Vasko3a41dff2020-07-15 14:30:28 +02001218 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001219 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001220
Michal Vasko3a41dff2020-07-15 14:30:28 +02001221 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001222}
1223
Michal Vasko3a41dff2020-07-15 14:30:28 +02001224API LY_ERR
1225lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001226{
Radek Krejci0f969882020-08-21 16:56:47 +02001227 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001228 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001229
Michal Vasko3a41dff2020-07-15 14:30:28 +02001230 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001231
Michal Vasko3a41dff2020-07-15 14:30:28 +02001232 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001233 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001234
Michal Vasko3a41dff2020-07-15 14:30:28 +02001235 ret = lys_parse(ctx, in, format, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001236 ly_in_free(in, 0);
1237
Michal Vasko3a41dff2020-07-15 14:30:28 +02001238 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001239}
1240
1241API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001242lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001243 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001244{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001245 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001246 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001247 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001248 char *wd, *wn = NULL;
1249 DIR *dir = NULL;
1250 struct dirent *file;
1251 char *match_name = NULL;
1252 LYS_INFORMAT format_aux, match_format = 0;
1253 struct ly_set *dirs;
1254 struct stat st;
1255
1256 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1257
1258 /* start to fill the dir fifo with the context's search path (if set)
1259 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001260 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001261
1262 len = strlen(name);
1263 if (cwd) {
1264 wd = get_current_dir_name();
1265 if (!wd) {
1266 LOGMEM(NULL);
1267 goto cleanup;
1268 } else {
1269 /* add implicit current working directory (./) to be searched,
1270 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001271 ret = ly_set_add(dirs, wd, 0, NULL);
1272 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001273 implicit_cwd = 1;
1274 }
1275 }
1276 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001277 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001278 /* check for duplicities with the implicit current working directory */
1279 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1280 implicit_cwd = 0;
1281 continue;
1282 }
1283 wd = strdup(searchpaths[i]);
1284 if (!wd) {
1285 LOGMEM(NULL);
1286 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001287 } else {
1288 ret = ly_set_add(dirs, wd, 0, NULL);
1289 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001290 }
1291 }
1292 }
1293 wd = NULL;
1294
1295 /* start searching */
1296 while (dirs->count) {
1297 free(wd);
1298 free(wn); wn = NULL;
1299
1300 dirs->count--;
1301 wd = (char *)dirs->objs[dirs->count];
1302 dirs->objs[dirs->count] = NULL;
1303 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1304
1305 if (dir) {
1306 closedir(dir);
1307 }
1308 dir = opendir(wd);
1309 dir_len = strlen(wd);
1310 if (!dir) {
1311 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1312 } else {
1313 while ((file = readdir(dir))) {
1314 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1315 /* skip . and .. */
1316 continue;
1317 }
1318 free(wn);
1319 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1320 LOGMEM(NULL);
1321 goto cleanup;
1322 }
1323 if (stat(wn, &st) == -1) {
1324 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001325 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001326 continue;
1327 }
1328 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1329 /* we have another subdirectory in searchpath to explore,
1330 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001331 ret = ly_set_add(dirs, wn, 0, NULL);
1332 LY_CHECK_GOTO(ret, cleanup);
1333
Radek Krejcid33273d2018-10-25 14:55:52 +02001334 /* continue with the next item in current directory */
1335 wn = NULL;
1336 continue;
1337 } else if (!S_ISREG(st.st_mode)) {
1338 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1339 continue;
1340 }
1341
1342 /* here we know that the item is a file which can contain a module */
1343 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001344 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001345 /* different filename than the module we search for */
1346 continue;
1347 }
1348
1349 /* get type according to filename suffix */
1350 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001351 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001352 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001353 /* TODO YIN parser
1354 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1355 format_aux = LYS_IN_YIN;
1356 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001357 } else {
1358 /* not supportde suffix/file format */
1359 continue;
1360 }
1361
1362 if (revision) {
1363 /* we look for the specific revision, try to get it from the filename */
1364 if (file->d_name[len] == '@') {
1365 /* check revision from the filename */
1366 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1367 /* another revision */
1368 continue;
1369 } else {
1370 /* exact revision */
1371 free(match_name);
1372 match_name = wn;
1373 wn = NULL;
1374 match_len = dir_len + 1 + len;
1375 match_format = format_aux;
1376 goto success;
1377 }
1378 } else {
1379 /* continue trying to find exact revision match, use this only if not found */
1380 free(match_name);
1381 match_name = wn;
1382 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001383 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001384 match_format = format_aux;
1385 continue;
1386 }
1387 } else {
1388 /* remember the revision and try to find the newest one */
1389 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001390 if ((file->d_name[len] != '@') ||
1391 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 +02001392 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001393 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001394 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1395 continue;
1396 }
1397 free(match_name);
1398 }
1399
1400 match_name = wn;
1401 wn = NULL;
1402 match_len = dir_len + 1 + len;
1403 match_format = format_aux;
1404 continue;
1405 }
1406 }
1407 }
1408 }
1409
1410success:
1411 (*localfile) = match_name;
1412 match_name = NULL;
1413 if (format) {
1414 (*format) = match_format;
1415 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001416 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001417
1418cleanup:
1419 free(wn);
1420 free(wd);
1421 if (dir) {
1422 closedir(dir);
1423 }
1424 free(match_name);
1425 ly_set_free(dirs, free);
1426
1427 return ret;
1428}