blob: e933995eb2387189f68c97bf3a4dd7bddbc282b7 [file] [log] [blame]
Radek Krejci3f5e3db2018-10-11 15:57:47 +02001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib7db73a2018-10-24 14:18:40 +020014
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#define _GNU_SOURCE
16
Radek Krejcica376bd2020-06-11 16:04:06 +020017#include "tree_schema.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020018
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <assert.h>
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010020#include <ctype.h>
Radek Krejcid33273d2018-10-25 14:55:52 +020021#include <dirent.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020022#include <errno.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020024#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdlib.h>
26#include <string.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020027#include <sys/stat.h>
Radek Krejci86d106e2018-10-18 09:53:19 +020028#include <unistd.h>
Radek Krejci3f5e3db2018-10-11 15:57:47 +020029
Radek Krejcica376bd2020-06-11 16:04:06 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci86d106e2018-10-18 09:53:19 +020032#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
34#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020035#include "in_internal.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020036#include "parser_internal.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020037#include "parser_schema.h"
Michal Vasko40308e72020-10-20 16:38:40 +020038#include "path.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020039#include "schema_compile.h"
40#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010041#include "schema_features.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020042#include "set.h"
43#include "tree.h"
44#include "tree_schema_internal.h"
45#include "xpath.h"
Radek Krejci3f5e3db2018-10-11 15:57:47 +020046
Michal Vaskof1ab44f2020-10-22 08:58:32 +020047API LY_ERR
48lysc_tree_dfs_full(const struct lysc_node *root, lysc_dfs_clb dfs_clb, void *data)
49{
Michal Vasko1d972ca2020-11-03 17:16:56 +010050 struct lysc_node *elem, *elem2;
51 const struct lysc_action *acts;
52 const struct lysc_notif *notifs;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020053 LY_ARRAY_COUNT_TYPE u;
54
55 LY_CHECK_ARG_RET(NULL, root, dfs_clb, LY_EINVAL);
56
57 LYSC_TREE_DFS_BEGIN(root, elem) {
58 /* schema node */
59 LY_CHECK_RET(dfs_clb(elem, data, &LYSC_TREE_DFS_continue));
60
Michal Vasko1d972ca2020-11-03 17:16:56 +010061 acts = lysc_node_actions(elem);
62 LY_ARRAY_FOR(acts, u) {
63 LYSC_TREE_DFS_BEGIN(&acts[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020064 /* action subtree */
65 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
66
Michal Vasko1d972ca2020-11-03 17:16:56 +010067 LYSC_TREE_DFS_END(&acts[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020068 }
69 }
70
Michal Vasko1d972ca2020-11-03 17:16:56 +010071 notifs = lysc_node_notifs(elem);
72 LY_ARRAY_FOR(notifs, u) {
73 LYSC_TREE_DFS_BEGIN(&notifs[u], elem2) {
Michal Vaskof1ab44f2020-10-22 08:58:32 +020074 /* notification subtree */
75 LY_CHECK_RET(dfs_clb(elem2, data, &LYSC_TREE_DFS_continue));
76
Michal Vasko1d972ca2020-11-03 17:16:56 +010077 LYSC_TREE_DFS_END(&notifs[u], elem2);
Michal Vaskof1ab44f2020-10-22 08:58:32 +020078 }
79 }
80
81 LYSC_TREE_DFS_END(root, elem);
82 }
83
84 return LY_SUCCESS;
85}
86
87API LY_ERR
88lysc_module_dfs_full(const struct lys_module *mod, lysc_dfs_clb dfs_clb, void *data)
89{
90 LY_ARRAY_COUNT_TYPE u;
Michal Vasko2336cf52020-11-03 17:18:15 +010091 const struct lysc_node *root;
Michal Vaskof1ab44f2020-10-22 08:58:32 +020092
93 LY_CHECK_ARG_RET(NULL, mod, mod->compiled, dfs_clb, LY_EINVAL);
94
95 /* schema nodes */
Michal Vasko2336cf52020-11-03 17:18:15 +010096 LY_LIST_FOR(mod->compiled->data, root) {
97 LY_CHECK_RET(lysc_tree_dfs_full(root, dfs_clb, data));
98 }
Michal Vaskof1ab44f2020-10-22 08:58:32 +020099
100 /* RPCs */
101 LY_ARRAY_FOR(mod->compiled->rpcs, u) {
102 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->rpcs[u], dfs_clb, data));
103 }
104
105 /* notifications */
106 LY_ARRAY_FOR(mod->compiled->notifs, u) {
107 LY_CHECK_RET(lysc_tree_dfs_full((struct lysc_node *)&mod->compiled->notifs[u], dfs_clb, data));
108 }
109
110 return LY_SUCCESS;
111}
112
Radek Krejcib93bd412020-11-02 13:23:11 +0100113static void
114lys_getnext_into_case(const struct lysc_node_case *first_case, const struct lysc_node **last, const struct lysc_node **next)
115{
116 for (; first_case; first_case = (const struct lysc_node_case*)first_case->next) {
117 if (first_case->child) {
118 /* there is something to return */
119 (*next) = first_case->child;
120 return;
121 }
122 }
123
124 /* no children in choice's cases, so go to the choice's sibling instead of into it */
125 (*last) = (*next);
126 (*next) = (*next)->next;
127}
128
Radek Krejcia3045382018-11-22 14:30:31 +0100129API const struct lysc_node *
Radek Krejci1deb5be2020-08-26 16:43:36 +0200130lys_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 +0100131{
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100132 const struct lysc_node *next = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +0100133 struct lysc_node **snode;
Radek Krejci857189e2020-09-01 13:26:36 +0200134 ly_bool action_flag = 0, notif_flag = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100135 const struct lysc_action *actions;
136 const struct lysc_notif *notifs;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200137 LY_ARRAY_COUNT_TYPE u;
Radek Krejcia3045382018-11-22 14:30:31 +0100138
139 LY_CHECK_ARG_RET(NULL, parent || module, NULL);
140
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200141next:
Radek Krejcia3045382018-11-22 14:30:31 +0100142 if (!last) {
143 /* first call */
144
145 /* get know where to start */
146 if (parent) {
147 /* schema subtree */
Michal Vasko69730152020-10-09 16:30:07 +0200148 if ((parent->nodetype == LYS_CHOICE) && (options & LYS_GETNEXT_WITHCASE)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200149 if (((struct lysc_node_choice *)parent)->cases) {
Radek Krejcib93bd412020-11-02 13:23:11 +0100150 next = last = (const struct lysc_node *)((struct lysc_node_choice *)parent)->cases;
Radek Krejci056d0a82018-12-06 16:57:25 +0100151 }
Radek Krejci056d0a82018-12-06 16:57:25 +0100152 } else {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100153 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
Radek Krejci05b774b2019-02-25 13:26:18 +0100154 /* do not return anything if the node does not have any children */
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200155 if (snode && *snode) {
156 next = last = *snode;
Radek Krejci056d0a82018-12-06 16:57:25 +0100157 }
Radek Krejcia3045382018-11-22 14:30:31 +0100158 }
Radek Krejcia3045382018-11-22 14:30:31 +0100159 } else {
160 /* top level data */
161 next = last = module->data;
162 }
163 if (!next) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100164 /* try to get action or notification */
165 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100166 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100167 /* test if the next can be returned */
168 goto check;
169
Michal Vasko1bf09392020-03-27 12:38:10 +0100170 } else if (last->nodetype & (LYS_RPC | LYS_ACTION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100171 action_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100172 if (last->parent) {
173 actions = lysc_node_actions(last->parent);
174 } else {
175 actions = module->rpcs;
176 }
177 LY_ARRAY_FOR(actions, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200178 if (&actions[u] == (struct lysc_action *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100179 break;
180 }
181 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200182 if (u + 1 < LY_ARRAY_COUNT(actions)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200183 next = (struct lysc_node *)(&actions[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100184 }
185 goto repeat;
186 } else if (last->nodetype == LYS_NOTIF) {
Radek Krejci05b774b2019-02-25 13:26:18 +0100187 action_flag = notif_flag = 1;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100188 if (last->parent) {
189 notifs = lysc_node_notifs(last->parent);
190 } else {
191 notifs = module->notifs;
192 }
193 LY_ARRAY_FOR(notifs, u) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200194 if (&notifs[u] == (struct lysc_notif *)last) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100195 break;
196 }
197 }
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200198 if (u + 1 < LY_ARRAY_COUNT(notifs)) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200199 next = (struct lysc_node *)(&notifs[u + 1]);
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100200 }
201 goto repeat;
Michal Vasko20424b42020-08-31 12:29:38 +0200202 } else {
203 next = last->next;
Radek Krejcia3045382018-11-22 14:30:31 +0100204 }
205
Radek Krejcia3045382018-11-22 14:30:31 +0100206repeat:
207 if (!next) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100208 /* possibly go back to parent */
Michal Vasko69730152020-10-09 16:30:07 +0200209 if (last && (last->parent != parent)) {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100210 last = last->parent;
Radek Krejcid5a2b9d2019-04-12 10:39:30 +0200211 goto next;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100212 } else if (!action_flag) {
213 action_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200214 next = parent ? (struct lysc_node *)lysc_node_actions(parent) : (struct lysc_node *)module->rpcs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100215 } else if (!notif_flag) {
216 notif_flag = 1;
Michal Vasko22df3f02020-08-24 13:29:22 +0200217 next = parent ? (struct lysc_node *)lysc_node_notifs(parent) : (struct lysc_node *)module->notifs;
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100218 } else {
219 return NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100220 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +0100221 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100222 }
Radek Krejci05b774b2019-02-25 13:26:18 +0100223check:
Radek Krejcia3045382018-11-22 14:30:31 +0100224 switch (next->nodetype) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100225 case LYS_RPC:
Radek Krejcia3045382018-11-22 14:30:31 +0100226 case LYS_ACTION:
227 case LYS_NOTIF:
228 case LYS_LEAF:
229 case LYS_ANYXML:
230 case LYS_ANYDATA:
231 case LYS_LIST:
232 case LYS_LEAFLIST:
233 break;
Michal Vasko20424b42020-08-31 12:29:38 +0200234 case LYS_CASE:
235 if (options & LYS_GETNEXT_WITHCASE) {
236 break;
237 } else {
238 /* go into */
Radek Krejcib93bd412020-11-02 13:23:11 +0100239 lys_getnext_into_case((const struct lysc_node_case *)next, &last, &next);
Michal Vasko20424b42020-08-31 12:29:38 +0200240 }
241 goto repeat;
Radek Krejcia3045382018-11-22 14:30:31 +0100242 case LYS_CONTAINER:
243 if (!(((struct lysc_node_container *)next)->flags & LYS_PRESENCE) && (options & LYS_GETNEXT_INTONPCONT)) {
244 if (((struct lysc_node_container *)next)->child) {
245 /* go into */
246 next = ((struct lysc_node_container *)next)->child;
247 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100248 last = next;
Radek Krejcia3045382018-11-22 14:30:31 +0100249 next = next->next;
250 }
251 goto repeat;
252 }
253 break;
254 case LYS_CHOICE:
255 if (options & LYS_GETNEXT_WITHCHOICE) {
Michal Vasko20424b42020-08-31 12:29:38 +0200256 break;
Radek Krejci9bb94eb2018-12-04 16:48:35 +0100257 } else if ((options & LYS_GETNEXT_NOCHOICE) || !((struct lysc_node_choice *)next)->cases) {
258 next = next->next;
259 } else {
Radek Krejcia9026eb2018-12-12 16:04:47 +0100260 if (options & LYS_GETNEXT_WITHCASE) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200261 next = (struct lysc_node *)((struct lysc_node_choice *)next)->cases;
Radek Krejcia9026eb2018-12-12 16:04:47 +0100262 } else {
Radek Krejcib93bd412020-11-02 13:23:11 +0100263 /* go into */
264 lys_getnext_into_case(((struct lysc_node_choice *)next)->cases, &last, &next);
Radek Krejcia9026eb2018-12-12 16:04:47 +0100265 }
Radek Krejcia3045382018-11-22 14:30:31 +0100266 }
267 goto repeat;
268 default:
269 /* we should not be here */
Radek Krejcib07b5c92019-04-08 10:56:37 +0200270 LOGINT(module ? module->mod->ctx : parent->module->ctx);
Radek Krejcia3045382018-11-22 14:30:31 +0100271 return NULL;
272 }
273
Radek Krejcia3045382018-11-22 14:30:31 +0100274 return next;
275}
276
277API const struct lysc_node *
Michal Vaskoe444f752020-02-10 12:20:06 +0100278lys_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 +0200279 uint16_t nodetype, uint32_t options)
Radek Krejcia3045382018-11-22 14:30:31 +0100280{
281 const struct lysc_node *node = NULL;
282
283 LY_CHECK_ARG_RET(NULL, module, name, NULL);
284 if (!nodetype) {
285 nodetype = 0xffff;
286 }
287
288 while ((node = lys_getnext(node, parent, module->compiled, options))) {
289 if (!(node->nodetype & nodetype)) {
290 continue;
291 }
292 if (node->module != module) {
293 continue;
294 }
295
296 if (name_len) {
Radek Krejci7f9b6512019-09-18 13:11:09 +0200297 if (!ly_strncmp(node->name, name, name_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +0100298 return node;
299 }
300 } else {
301 if (!strcmp(node->name, name)) {
302 return node;
303 }
304 }
305 }
306 return NULL;
307}
308
Michal Vasko519fd602020-05-26 12:17:39 +0200309API LY_ERR
Radek Krejcibed13942020-10-19 16:06:28 +0200310lys_find_xpath_atoms(const struct lysc_node *ctx_node, const char *xpath, uint32_t options, struct ly_set **set)
Michal Vasko519fd602020-05-26 12:17:39 +0200311{
312 LY_ERR ret = LY_SUCCESS;
313 struct lyxp_set xp_set;
Radek Krejcif03a9e22020-09-18 20:09:31 +0200314 struct lyxp_expr *exp = NULL;
Michal Vasko519fd602020-05-26 12:17:39 +0200315 uint32_t i;
316
317 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
318 if (!(options & LYXP_SCNODE_ALL)) {
319 options = LYXP_SCNODE;
320 }
321
322 memset(&xp_set, 0, sizeof xp_set);
323
324 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200325 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
326 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200327
328 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200329 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko519fd602020-05-26 12:17:39 +0200330 LY_CHECK_GOTO(ret, cleanup);
331
332 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200333 ret = ly_set_new(set);
334 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200335
336 /* transform into ly_set */
337 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
338 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
339 (*set)->size = xp_set.used;
340
341 for (i = 0; i < xp_set.used; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +0200342 if (xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200343 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200344 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko519fd602020-05-26 12:17:39 +0200345 }
346 }
347
348cleanup:
349 lyxp_set_free_content(&xp_set);
350 lyxp_expr_free(ctx_node->module->ctx, exp);
351 return ret;
352}
353
Michal Vasko072de482020-08-05 13:27:21 +0200354API LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +0200355lys_find_path_atoms(const struct ly_path *path, struct ly_set **set)
356{
357 LY_ERR ret = LY_SUCCESS;
358 LY_ARRAY_COUNT_TYPE u, v;
359
360 LY_CHECK_ARG_RET(NULL, path, set, LY_EINVAL);
361
362 /* allocate return set */
363 LY_CHECK_RET(ly_set_new(set));
364
365 LY_ARRAY_FOR(path, u) {
366 /* add nodes from the path */
367 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].node, 0, NULL), cleanup);
368 if (path[u].pred_type == LY_PATH_PREDTYPE_LIST) {
369 LY_ARRAY_FOR(path[u].predicates, v) {
370 /* add all the keys in a predicate */
371 LY_CHECK_GOTO(ret = ly_set_add(*set, (void *)path[u].predicates[v].key, 0, NULL), cleanup);
372 }
373 }
374 }
375
376cleanup:
377 if (ret) {
378 ly_set_free(*set, NULL);
379 *set = NULL;
380 }
381 return ret;
382}
383
384API LY_ERR
385lys_find_expr_atoms(const struct lysc_node *ctx_node, const struct lys_module *cur_mod, const struct lyxp_expr *expr,
386 const struct lysc_prefix *prefixes, uint32_t options, struct ly_set **set)
387{
388 LY_ERR ret = LY_SUCCESS;
389 struct lyxp_set xp_set = {0};
390 uint32_t i;
391
392 LY_CHECK_ARG_RET(NULL, cur_mod, expr, prefixes, set, LY_EINVAL);
393 if (!(options & LYXP_SCNODE_ALL)) {
394 options = LYXP_SCNODE;
395 }
396
397 /* atomize expression */
398 ret = lyxp_atomize(expr, cur_mod, LY_PREF_SCHEMA_RESOLVED, (void *)prefixes, ctx_node, &xp_set, options);
399 LY_CHECK_GOTO(ret, cleanup);
400
401 /* allocate return set */
402 ret = ly_set_new(set);
403 LY_CHECK_GOTO(ret, cleanup);
404
405 /* transform into ly_set */
406 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
407 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(cur_mod->ctx); ret = LY_EMEM, cleanup);
408 (*set)->size = xp_set.used;
409
410 for (i = 0; i < xp_set.used; ++i) {
411 if ((xp_set.val.scnodes[i].type == LYXP_NODE_ELEM) && (xp_set.val.scnodes[i].in_ctx == 1)) {
412 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
413 LY_CHECK_GOTO(ret, cleanup);
414 }
415 }
416
417cleanup:
418 lyxp_set_free_content(&xp_set);
419 if (ret) {
420 ly_set_free(*set, NULL);
421 *set = NULL;
422 }
423 return ret;
424}
425
426API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200427lys_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 +0200428{
429 LY_ERR ret = LY_SUCCESS;
Michal Vasko40308e72020-10-20 16:38:40 +0200430 struct lyxp_set xp_set = {0};
Radek Krejcif03a9e22020-09-18 20:09:31 +0200431 struct lyxp_expr *exp = NULL;
Michal Vasko072de482020-08-05 13:27:21 +0200432 uint32_t i;
433
434 LY_CHECK_ARG_RET(NULL, ctx_node, xpath, set, LY_EINVAL);
435 if (!(options & LYXP_SCNODE_ALL)) {
436 options = LYXP_SCNODE;
437 }
438
Michal Vasko072de482020-08-05 13:27:21 +0200439 /* compile expression */
Radek Krejcif03a9e22020-09-18 20:09:31 +0200440 ret = lyxp_expr_parse(ctx_node->module->ctx, xpath, 0, 1, &exp);
441 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200442
443 /* atomize expression */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200444 ret = lyxp_atomize(exp, NULL, LY_PREF_JSON, NULL, ctx_node, &xp_set, options);
Michal Vasko072de482020-08-05 13:27:21 +0200445 LY_CHECK_GOTO(ret, cleanup);
446
447 /* allocate return set */
Radek Krejciba03a5a2020-08-27 14:40:41 +0200448 ret = ly_set_new(set);
449 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200450
451 /* transform into ly_set */
452 (*set)->objs = malloc(xp_set.used * sizeof *(*set)->objs);
453 LY_CHECK_ERR_GOTO(!(*set)->objs, LOGMEM(ctx_node->module->ctx); ret = LY_EMEM, cleanup);
454 (*set)->size = xp_set.used;
455
456 for (i = 0; i < xp_set.used; ++i) {
457 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 +0200458 ret = ly_set_add(*set, xp_set.val.scnodes[i].scnode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200459 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko072de482020-08-05 13:27:21 +0200460 }
461 }
462
463cleanup:
464 lyxp_set_free_content(&xp_set);
465 lyxp_expr_free(ctx_node->module->ctx, exp);
Michal Vaskoae159662020-10-21 11:57:24 +0200466 if (ret) {
Michal Vasko40308e72020-10-20 16:38:40 +0200467 ly_set_free(*set, NULL);
468 *set = NULL;
469 }
Michal Vasko072de482020-08-05 13:27:21 +0200470 return ret;
471}
472
Michal Vasko14654712020-02-06 08:35:21 +0100473char *
474lysc_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 +0200475 size_t buflen)
Radek Krejci327de162019-06-14 12:52:07 +0200476{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 const struct lysc_node *iter;
Radek Krejci327de162019-06-14 12:52:07 +0200478 char *path = NULL;
479 int len = 0;
480
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200481 LY_CHECK_ARG_RET(NULL, node, NULL);
482 if (buffer) {
483 LY_CHECK_ARG_RET(node->module->ctx, buflen > 1, NULL);
484 }
485
Radek Krejci327de162019-06-14 12:52:07 +0200486 switch (pathtype) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 case LYSC_PATH_LOG:
Michal Vasko65de0402020-08-03 16:34:19 +0200488 case LYSC_PATH_DATA:
Michal Vasko90932a92020-02-12 14:33:03 +0100489 for (iter = node; iter && (iter != parent) && (len >= 0); iter = iter->parent) {
Michal Vasko11deea12020-08-05 13:54:50 +0200490 char *s, *id;
Michal Vasko14654712020-02-06 08:35:21 +0100491 const char *slash;
Radek Krejci327de162019-06-14 12:52:07 +0200492
Michal Vasko65de0402020-08-03 16:34:19 +0200493 if ((pathtype == LYSC_PATH_DATA) && (iter->nodetype & (LYS_CHOICE | LYS_CASE))) {
494 /* schema-only node */
495 continue;
496 }
497
Michal Vasko11deea12020-08-05 13:54:50 +0200498 s = buffer ? strdup(buffer) : path;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 id = strdup(iter->name);
Michal Vasko14654712020-02-06 08:35:21 +0100500 if (parent && (iter->parent == parent)) {
501 slash = "";
502 } else {
503 slash = "/";
504 }
Michal Vasko69730152020-10-09 16:30:07 +0200505 if (!iter->parent || (iter->parent->module != iter->module)) {
Radek Krejci327de162019-06-14 12:52:07 +0200506 /* print prefix */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200507 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100508 len = snprintf(buffer, buflen, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200509 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100510 len = asprintf(&path, "%s%s:%s%s", slash, iter->module->name, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200511 }
Radek Krejci327de162019-06-14 12:52:07 +0200512 } else {
513 /* prefix is the same as in parent */
Radek Krejci1c0c3442019-07-23 16:08:47 +0200514 if (buffer) {
Michal Vasko14654712020-02-06 08:35:21 +0100515 len = snprintf(buffer, buflen, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200516 } else {
Michal Vasko14654712020-02-06 08:35:21 +0100517 len = asprintf(&path, "%s%s%s", slash, id, s ? s : "");
Radek Krejci1c0c3442019-07-23 16:08:47 +0200518 }
Radek Krejci327de162019-06-14 12:52:07 +0200519 }
520 free(s);
521 free(id);
Radek Krejci1c0c3442019-07-23 16:08:47 +0200522
Michal Vasko69730152020-10-09 16:30:07 +0200523 if (buffer && (buflen <= (size_t)len)) {
Radek Krejci1c0c3442019-07-23 16:08:47 +0200524 /* not enough space in buffer */
525 break;
526 }
Radek Krejci327de162019-06-14 12:52:07 +0200527 }
528
529 if (len < 0) {
530 free(path);
531 path = NULL;
532 } else if (len == 0) {
Radek Krejci3bbd93e2019-07-24 09:57:23 +0200533 if (buffer) {
534 strcpy(buffer, "/");
535 } else {
536 path = strdup("/");
537 }
Radek Krejci327de162019-06-14 12:52:07 +0200538 }
539 break;
540 }
541
Radek Krejci1c0c3442019-07-23 16:08:47 +0200542 if (buffer) {
543 return buffer;
544 } else {
545 return path;
546 }
Radek Krejci327de162019-06-14 12:52:07 +0200547}
548
Michal Vasko14654712020-02-06 08:35:21 +0100549API char *
550lysc_path(const struct lysc_node *node, LYSC_PATH_TYPE pathtype, char *buffer, size_t buflen)
551{
552 return lysc_path_until(node, NULL, pathtype, buffer, buflen);
553}
554
Michal Vasko28d78432020-05-26 13:10:53 +0200555API LY_ERR
Radek Krejciaf9cd802020-10-06 21:59:47 +0200556lysc_set_private(const struct lysc_node *node, void *priv, void **prev_priv_p)
Radek Krejci19cf8052020-08-18 15:02:38 +0200557{
Radek Krejciaf9cd802020-10-06 21:59:47 +0200558 struct lysc_action *act;
559 struct lysc_notif *notif;
560
Radek Krejci19cf8052020-08-18 15:02:38 +0200561 LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
562
Radek Krejciaf9cd802020-10-06 21:59:47 +0200563 switch (node->nodetype) {
564 case LYS_CONTAINER:
565 case LYS_CHOICE:
566 case LYS_CASE:
567 case LYS_LEAF:
568 case LYS_LEAFLIST:
569 case LYS_LIST:
570 case LYS_ANYXML:
571 case LYS_ANYDATA:
572 if (prev_priv_p) {
573 *prev_priv_p = node->priv;
574 }
575 ((struct lysc_node *)node)->priv = priv;
576 break;
577 case LYS_RPC:
578 case LYS_ACTION:
579 act = (struct lysc_action *)node;
580 if (prev_priv_p) {
581 *prev_priv_p = act->priv;
582 }
583 act->priv = priv;
584 break;
585 case LYS_NOTIF:
586 notif = (struct lysc_notif *)node;
587 if (prev_priv_p) {
588 *prev_priv_p = notif->priv;
589 }
590 notif->priv = priv;
591 break;
592 default:
593 return LY_EINVAL;
Radek Krejci19cf8052020-08-18 15:02:38 +0200594 }
Radek Krejci19cf8052020-08-18 15:02:38 +0200595
596 return LY_SUCCESS;
597}
598
Michal Vasko89b5c072020-10-06 13:52:44 +0200599API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100600lys_set_implemented(struct lys_module *mod, const char **features)
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200601{
Michal Vasko916aefb2020-11-02 15:43:16 +0100602 LY_ERR ret = LY_SUCCESS;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200603 struct lys_module *m;
Michal Vasko89b5c072020-10-06 13:52:44 +0200604 uint32_t i, idx;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200605
606 LY_CHECK_ARG_RET(NULL, mod, LY_EINVAL);
607
608 if (mod->implemented) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200609 /* mod is already implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200610 return LY_SUCCESS;
611 }
612
613 /* we have module from the current context */
614 m = ly_ctx_get_module_implemented(mod->ctx, mod->name);
615 if (m) {
Michal Vasko89b5c072020-10-06 13:52:44 +0200616 assert(m != mod);
617
618 /* check collision with other implemented revision */
619 LOGERR(mod->ctx, LY_EDENIED, "Module \"%s%s%s\" is present in the context in other implemented revision (%s).",
620 mod->name, mod->revision ? "@" : "", mod->revision ? mod->revision : "", m->revision ? m->revision : "none");
621 return LY_EDENIED;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200622 }
623
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100624 /* enable features */
625 LY_CHECK_RET(lys_enable_features(mod->parsed, features));
626
Michal Vasko89b5c072020-10-06 13:52:44 +0200627 /* add the module into newly implemented module set */
Radek Krejci3d92e442020-10-12 12:48:13 +0200628 LY_CHECK_RET(ly_set_add(&mod->ctx->implementing, mod, 1, NULL));
Michal Vasko89b5c072020-10-06 13:52:44 +0200629
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200630 /* mark the module implemented, check for collision was already done */
Michal Vasko89b5c072020-10-06 13:52:44 +0200631 mod->implemented = 1;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200632
633 /* compile the schema */
Michal Vasko89b5c072020-10-06 13:52:44 +0200634 ret = lys_compile(mod, 0);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200635
Michal Vasko89b5c072020-10-06 13:52:44 +0200636 if (mod == mod->ctx->implementing.objs[0]) {
637 /* the first module being implemented, consolidate the set */
638 if (ret) {
639 /* failure, full compile revert */
640 for (i = 0; i < mod->ctx->list.count; ++i) {
641 m = mod->ctx->list.objs[i];
642 if (ly_set_contains(&mod->ctx->implementing, m, &idx)) {
643 assert(m->implemented);
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200644
Michal Vasko89b5c072020-10-06 13:52:44 +0200645 /* make the module correctly non-implemented again */
646 m->implemented = 0;
647 ly_set_rm_index(&mod->ctx->implementing, idx, NULL);
648 lys_precompile_augments_deviations_revert(mod->ctx, m);
649 }
Michal Vasko89b5c072020-10-06 13:52:44 +0200650 }
Michal Vasko916aefb2020-11-02 15:43:16 +0100651
652 /* recompile, do not overwrite return value */
653 lys_recompile(mod->ctx, NULL);
Michal Vasko89b5c072020-10-06 13:52:44 +0200654 }
655
656 ly_set_erase(&mod->ctx->implementing, NULL);
657 }
658 return ret;
Radek Krejci77a8bcd2019-09-11 11:20:02 +0200659}
660
Michal Vasko7c8439f2020-08-05 13:25:19 +0200661static LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100662lys_resolve_import_include(struct lys_parser_ctx *pctx, struct lysp_module *pmod)
Michal Vasko7c8439f2020-08-05 13:25:19 +0200663{
664 struct lysp_import *imp;
665 struct lysp_include *inc;
666 LY_ARRAY_COUNT_TYPE u, v;
667
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100668 pmod->parsing = 1;
669 LY_ARRAY_FOR(pmod->imports, u) {
670 imp = &pmod->imports[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200671 if (!imp->module) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100672 LY_CHECK_RET(lysp_load_module(PARSER_CTX(pctx), imp->name, imp->rev[0] ? imp->rev : NULL, 0, 0, NULL, &imp->module));
Michal Vasko7c8439f2020-08-05 13:25:19 +0200673 }
674 /* check for importing the same module twice */
675 for (v = 0; v < u; ++v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100676 if (imp->module == pmod->imports[v].module) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200677 LOGWRN(PARSER_CTX(pctx), "Single revision of the module \"%s\" imported twice.", imp->name);
678 }
679 }
680 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100681 LY_ARRAY_FOR(pmod->includes, u) {
682 inc = &pmod->includes[u];
Michal Vasko7c8439f2020-08-05 13:25:19 +0200683 if (!inc->submodule) {
684 LY_CHECK_RET(lysp_load_submodule(pctx, inc));
685 }
686 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100687 pmod->parsing = 0;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200688
689 return LY_SUCCESS;
690}
691
Michal Vasko3a41dff2020-07-15 14:30:28 +0200692LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200693lys_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 +0200694 LY_ERR (*custom_check)(const struct ly_ctx *, struct lysp_module *, struct lysp_submodule *, void *),
Radek Krejci0f969882020-08-21 16:56:47 +0200695 void *check_data, struct lysp_submodule **submodule)
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200696{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200697 LY_ERR ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100698 struct lysp_submodule *submod = NULL, *latest_sp;
Michal Vaskob36053d2020-03-26 15:49:30 +0100699 struct lys_yang_parser_ctx *yangctx = NULL;
700 struct lys_yin_parser_ctx *yinctx = NULL;
701 struct lys_parser_ctx *pctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100702
Michal Vasko3a41dff2020-07-15 14:30:28 +0200703 LY_CHECK_ARG_RET(ctx, ctx, in, LY_EINVAL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100704
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100705 switch (format) {
706 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200707 ret = yin_parse_submodule(&yinctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100708 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100709 break;
710 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200711 ret = yang_parse_submodule(&yangctx, ctx, main_ctx, in, &submod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100712 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100713 break;
714 default:
David Sedlák4f2f5ba2019-08-15 13:18:48 +0200715 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Radek Krejci82fa8d42020-07-11 22:00:59 +0200716 ret = LY_EINVAL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 break;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200718 }
Radek Krejcif6923e82020-07-02 16:36:53 +0200719 LY_CHECK_GOTO(ret, error);
Radek Krejcif027df72020-09-15 13:00:28 +0200720 assert(submod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100721
722 /* make sure that the newest revision is at position 0 */
723 lysp_sort_revisions(submod->revs);
724
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 /* decide the latest revision */
Michal Vaskoc3781c32020-10-06 14:04:08 +0200726 latest_sp = ly_ctx_get_submodule(NULL, submod->mod, submod->name, NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100727 if (latest_sp) {
728 if (submod->revs) {
729 if (!latest_sp->revs) {
730 /* latest has no revision, so mod is anyway newer */
731 submod->latest_revision = latest_sp->latest_revision;
Radek Krejcib3289d62019-09-18 12:21:39 +0200732 /* the latest_sp is zeroed later when the new module is being inserted into the context */
733 } else if (strcmp(submod->revs[0].date, latest_sp->revs[0].date) > 0) {
734 submod->latest_revision = latest_sp->latest_revision;
735 /* the latest_sp is zeroed later when the new module is being inserted into the context */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736 } else {
Radek Krejcib3289d62019-09-18 12:21:39 +0200737 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 }
Radek Krejcib3289d62019-09-18 12:21:39 +0200739 } else {
740 latest_sp = NULL;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741 }
742 } else {
743 submod->latest_revision = 1;
744 }
745
Radek Krejcib3289d62019-09-18 12:21:39 +0200746 if (custom_check) {
Michal Vasko7c8439f2020-08-05 13:25:19 +0200747 LY_CHECK_GOTO(ret = custom_check(ctx, NULL, submod, check_data), error);
Radek Krejcib3289d62019-09-18 12:21:39 +0200748 }
749
750 if (latest_sp) {
751 latest_sp->latest_revision = 0;
752 }
753
Michal Vasko7a0b0762020-09-02 16:37:01 +0200754 lys_parser_fill_filepath(ctx, in, &submod->filepath);
755
Michal Vasko7c8439f2020-08-05 13:25:19 +0200756 /* resolve imports and includes */
757 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, (struct lysp_module *)submod), error);
758
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100759 /* remap possibly changed and reallocated typedefs and groupings list back to the main context */
Michal Vaskob36053d2020-03-26 15:49:30 +0100760 memcpy(&main_ctx->tpdfs_nodes, &pctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
761 memcpy(&main_ctx->grps_nodes, &pctx->grps_nodes, sizeof main_ctx->grps_nodes);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762
David Sedlák1b623122019-08-05 15:27:49 +0200763 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100764 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200765 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100766 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200767 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200768 *submodule = submod;
769 return LY_SUCCESS;
David Sedlák1b623122019-08-05 15:27:49 +0200770
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771error:
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200772 lysp_module_free((struct lysp_module *)submod);
David Sedlák1b623122019-08-05 15:27:49 +0200773 if (format == LYS_IN_YANG) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100774 yang_parser_ctx_free(yangctx);
David Sedlák1b623122019-08-05 15:27:49 +0200775 } else {
Michal Vaskob36053d2020-03-26 15:49:30 +0100776 yin_parser_ctx_free(yinctx);
David Sedlák1b623122019-08-05 15:27:49 +0200777 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200778 return ret;
Radek Krejci9f5e6fb2018-10-25 09:26:12 +0200779}
780
Michal Vasko45b521c2020-11-04 17:14:39 +0100781/**
782 * @brief Add ietf-netconf metadata to the parsed module. Operation, filter, and select are added.
783 *
784 * @param[in] mod Parsed module to add to.
785 * @return LY_SUCCESS on success.
786 * @return LY_ERR on error.
787 */
788static LY_ERR
789lys_parsed_add_internal_ietf_netconf(struct lysp_module *mod)
790{
791 struct lysp_ext_instance *ext_p;
792 struct lysp_stmt *stmt;
793 struct lysp_import *imp;
794
795 /*
796 * 1) edit-config's operation
797 */
798 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
799 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
800 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
801 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "operation", 0, &ext_p->argument));
802 ext_p->flags = LYS_INTERNAL;
803 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
804 ext_p->insubstmt_index = 0;
805
806 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
807 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
808 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
809 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
810 stmt->kw = LY_STMT_TYPE;
811
812 stmt->child = calloc(1, sizeof *stmt->child);
813 stmt = stmt->child;
814 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
815 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
816 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "merge", 0, &stmt->arg));
817 stmt->kw = LY_STMT_ENUM;
818
819 stmt->next = calloc(1, sizeof *stmt->child);
820 stmt = stmt->next;
821 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
822 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
823 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "replace", 0, &stmt->arg));
824 stmt->kw = LY_STMT_ENUM;
825
826 stmt->next = calloc(1, sizeof *stmt->child);
827 stmt = stmt->next;
828 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
829 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
830 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "create", 0, &stmt->arg));
831 stmt->kw = LY_STMT_ENUM;
832
833 stmt->next = calloc(1, sizeof *stmt->child);
834 stmt = stmt->next;
835 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
836 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
837 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "delete", 0, &stmt->arg));
838 stmt->kw = LY_STMT_ENUM;
839
840 stmt->next = calloc(1, sizeof *stmt->child);
841 stmt = stmt->next;
842 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
843 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
844 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "remove", 0, &stmt->arg));
845 stmt->kw = LY_STMT_ENUM;
846
847 /*
848 * 2) filter's type
849 */
850 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
851 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
852 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
853 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &ext_p->argument));
854 ext_p->flags = LYS_INTERNAL;
855 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
856 ext_p->insubstmt_index = 0;
857
858 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
859 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
860 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
861 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enumeration", 0, &stmt->arg));
862 stmt->kw = LY_STMT_TYPE;
863
864 stmt->child = calloc(1, sizeof *stmt->child);
865 stmt = stmt->child;
866 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
867 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
868 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "subtree", 0, &stmt->arg));
869 stmt->kw = LY_STMT_ENUM;
870
871 stmt->next = calloc(1, sizeof *stmt->child);
872 stmt = stmt->next;
873 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
874 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "enum", 0, &stmt->stmt));
875 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
876 stmt->kw = LY_STMT_ENUM;
877
878 /* if-feature for enum allowed only for YANG 1.1 modules */
879 if (mod->version >= LYS_VERSION_1_1) {
880 stmt->child = calloc(1, sizeof *stmt->child);
881 stmt = stmt->child;
882 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
883 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "if-feature", 0, &stmt->stmt));
884 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "xpath", 0, &stmt->arg));
885 stmt->kw = LY_STMT_IF_FEATURE;
886 }
887
888 /*
889 * 3) filter's select
890 */
891 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
892 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
893 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
894 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "select", 0, &ext_p->argument));
895 ext_p->flags = LYS_INTERNAL;
896 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
897 ext_p->insubstmt_index = 0;
898
899 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
900 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
901 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
902 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_:xpath1.0", 0, &stmt->arg));
903 stmt->kw = LY_STMT_TYPE;
904
905 /* create new imports for the used prefixes */
906 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
907
908 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
909 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
910 imp->flags = LYS_INTERNAL;
911
912 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
913
914 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-types", 0, &imp->name));
915 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "yang_", 0, &imp->prefix));
916 imp->flags = LYS_INTERNAL;
917
918 return LY_SUCCESS;
919}
920
921/**
922 * @brief Add ietf-netconf-with-defaults "default" metadata to the parsed module.
923 *
924 * @param[in] mod Parsed module to add to.
925 * @return LY_SUCCESS on success.
926 * @return LY_ERR on error.
927 */
928static LY_ERR
929lys_parsed_add_internal_ietf_netconf_with_defaults(struct lysp_module *mod)
930{
931 struct lysp_ext_instance *ext_p;
932 struct lysp_stmt *stmt;
933 struct lysp_import *imp;
934
935 /* add new extension instance */
936 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->exts, ext_p, LY_EMEM);
937
938 /* fill in the extension instance fields */
939 LY_CHECK_ERR_RET(!ext_p, LOGMEM(mod->mod->ctx), LY_EMEM);
940 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_:annotation", 0, &ext_p->name));
941 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "default", 0, &ext_p->argument));
942 ext_p->flags = LYS_INTERNAL;
943 ext_p->insubstmt = LYEXT_SUBSTMT_SELF;
944 ext_p->insubstmt_index = 0;
945
946 ext_p->child = stmt = calloc(1, sizeof *ext_p->child);
947 LY_CHECK_ERR_RET(!stmt, LOGMEM(mod->mod->ctx), LY_EMEM);
948 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "type", 0, &stmt->stmt));
949 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "boolean", 0, &stmt->arg));
950 stmt->kw = LY_STMT_TYPE;
951
952 /* create new import for the used prefix */
953 LY_ARRAY_NEW_RET(mod->mod->ctx, mod->imports, imp, LY_EMEM);
954
955 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "ietf-yang-metadata", 0, &imp->name));
956 LY_CHECK_RET(lydict_insert(mod->mod->ctx, "md_", 0, &imp->prefix));
957 imp->flags = LYS_INTERNAL;
958
959 return LY_SUCCESS;
960}
961
Michal Vasko3a41dff2020-07-15 14:30:28 +0200962LY_ERR
Michal Vasko7a0b0762020-09-02 16:37:01 +0200963lys_create_module(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, ly_bool implement,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200964 LY_ERR (*custom_check)(const struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_submodule *submod, void *data),
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100965 void *check_data, const char **features, struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +0200966{
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100967 struct lys_module *mod = NULL, *latest, *mod_dup;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200968 struct lysp_submodule *submod;
Michal Vasko3a41dff2020-07-15 14:30:28 +0200969 LY_ERR ret;
Michal Vasko7c8439f2020-08-05 13:25:19 +0200970 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob36053d2020-03-26 15:49:30 +0100971 struct lys_yang_parser_ctx *yangctx = NULL;
972 struct lys_yin_parser_ctx *yinctx = NULL;
Radek Krejcif6923e82020-07-02 16:36:53 +0200973 struct lys_parser_ctx *pctx = NULL;
Michal Vasko7a0b0762020-09-02 16:37:01 +0200974 char *filename, *rev, *dot;
975 size_t len;
Radek Krejci86d106e2018-10-18 09:53:19 +0200976
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100977 LY_CHECK_ARG_RET(ctx, ctx, in, !features || implement, LY_EINVAL);
Michal Vasko7a0b0762020-09-02 16:37:01 +0200978 if (module) {
979 *module = NULL;
980 }
Radek Krejci86d106e2018-10-18 09:53:19 +0200981
982 mod = calloc(1, sizeof *mod);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200983 LY_CHECK_ERR_RET(!mod, LOGMEM(ctx), LY_EMEM);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100984 mod->ctx = ctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200985
986 switch (format) {
987 case LYS_IN_YIN:
Michal Vasko63f3d842020-07-08 10:10:14 +0200988 ret = yin_parse_module(&yinctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100989 pctx = (struct lys_parser_ctx *)yinctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200990 break;
991 case LYS_IN_YANG:
Michal Vasko63f3d842020-07-08 10:10:14 +0200992 ret = yang_parse_module(&yangctx, in, mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100993 pctx = (struct lys_parser_ctx *)yangctx;
Radek Krejci86d106e2018-10-18 09:53:19 +0200994 break;
995 default:
996 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vasko3a41dff2020-07-15 14:30:28 +0200997 ret = LY_EINVAL;
Radek Krejci86d106e2018-10-18 09:53:19 +0200998 break;
999 }
Radek Krejcif6923e82020-07-02 16:36:53 +02001000 LY_CHECK_GOTO(ret, error);
Radek Krejci9f5e6fb2018-10-25 09:26:12 +02001001
1002 /* make sure that the newest revision is at position 0 */
1003 lysp_sort_revisions(mod->parsed->revs);
Radek Krejci0af46292019-01-11 16:02:31 +01001004 if (mod->parsed->revs) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001005 LY_CHECK_GOTO(ret = lydict_insert(ctx, mod->parsed->revs[0].date, 0, &mod->revision), error);
Radek Krejci0af46292019-01-11 16:02:31 +01001006 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001007
Radek Krejcib3289d62019-09-18 12:21:39 +02001008 /* decide the latest revision */
Michal Vasko22df3f02020-08-24 13:29:22 +02001009 latest = (struct lys_module *)ly_ctx_get_module_latest(ctx, mod->name);
Radek Krejcib3289d62019-09-18 12:21:39 +02001010 if (latest) {
1011 if (mod->revision) {
1012 if (!latest->revision) {
1013 /* latest has no revision, so mod is anyway newer */
1014 mod->latest_revision = latest->latest_revision;
1015 /* the latest is zeroed later when the new module is being inserted into the context */
1016 } else if (strcmp(mod->revision, latest->revision) > 0) {
1017 mod->latest_revision = latest->latest_revision;
1018 /* the latest is zeroed later when the new module is being inserted into the context */
1019 } else {
1020 latest = NULL;
1021 }
1022 } else {
1023 latest = NULL;
1024 }
1025 } else {
1026 mod->latest_revision = 1;
1027 }
1028
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001029 if (custom_check) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001030 LY_CHECK_GOTO(ret = custom_check(ctx, mod->parsed, NULL, check_data), error);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001031 }
1032
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001033 /* check for duplicity in the context */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001034 if (implement && ly_ctx_get_module_implemented(ctx, mod->name)) {
1035 LOGERR(ctx, LY_EDENIED, "Module \"%s\" is already implemented in the context.", mod->name);
1036 ret = LY_EDENIED;
1037 goto error;
1038 }
Michal Vasko22df3f02020-08-24 13:29:22 +02001039 mod_dup = (struct lys_module *)ly_ctx_get_module(ctx, mod->name, mod->revision);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001040 if (mod_dup) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001041 if (mod->parsed->revs) {
1042 LOGERR(ctx, LY_EEXIST, "Module \"%s\" of revision \"%s\" is already present in the context.",
1043 mod->name, mod->parsed->revs[0].date);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 } else {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001045 LOGERR(ctx, LY_EEXIST, "Module \"%s\" with no revision is already present in the context.",
1046 mod->name);
Radek Krejcid33273d2018-10-25 14:55:52 +02001047 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001048 ret = LY_EEXIST;
1049 goto error;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001050 }
Radek Krejci86d106e2018-10-18 09:53:19 +02001051
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001052 switch (in->type) {
1053 case LY_IN_FILEPATH:
1054 /* check that name and revision match filename */
1055 filename = strrchr(in->method.fpath.filepath, '/');
1056 if (!filename) {
1057 filename = in->method.fpath.filepath;
1058 } else {
1059 filename++;
1060 }
1061 rev = strchr(filename, '@');
1062 dot = strrchr(filename, '.');
1063
1064 /* name */
1065 len = strlen(mod->name);
1066 if (strncmp(filename, mod->name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001067 ((rev && (rev != &filename[len])) || (!rev && (dot != &filename[len])))) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001068 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, mod->name);
1069 }
1070 if (rev) {
1071 len = dot - ++rev;
Michal Vasko69730152020-10-09 16:30:07 +02001072 if (!mod->parsed->revs || (len != 10) || strncmp(mod->parsed->revs[0].date, rev, len)) {
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001073 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Michal Vasko69730152020-10-09 16:30:07 +02001074 mod->parsed->revs ? mod->parsed->revs[0].date : "none");
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001075 }
1076 }
1077
1078 break;
1079 case LY_IN_FD:
1080 case LY_IN_FILE:
1081 case LY_IN_MEMORY:
1082 /* nothing special to do */
1083 break;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001084 case LY_IN_ERROR:
1085 LOGINT(ctx);
1086 ret = LY_EINT;
1087 goto error;
Radek Krejci096235c2019-01-11 11:12:19 +01001088 }
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001089 lys_parser_fill_filepath(ctx, in, &mod->filepath);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001090
Michal Vasko7a0b0762020-09-02 16:37:01 +02001091 if (latest) {
1092 latest->latest_revision = 0;
1093 }
1094
Michal Vasko45b521c2020-11-04 17:14:39 +01001095 /* add internal data in case specific modules were parsed */
1096 if (!strcmp(mod->name, "ietf-netconf")) {
1097 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf(mod->parsed), error);
1098 } else if (!strcmp(mod->name, "ietf-netconf-with-defaults")) {
1099 LY_CHECK_GOTO(ret = lys_parsed_add_internal_ietf_netconf_with_defaults(mod->parsed), error);
1100 }
1101
Michal Vasko7a0b0762020-09-02 16:37:01 +02001102 /* add into context */
Radek Krejci3d92e442020-10-12 12:48:13 +02001103 ret = ly_set_add(&ctx->list, mod, 1, NULL);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001104 LY_CHECK_GOTO(ret, error);
1105 ctx->module_set_id++;
1106
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001107 /* resolve includes and all imports */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001108 LY_CHECK_GOTO(ret = lys_resolve_import_include(pctx, mod->parsed), error_ctx);
1109
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001110 /* check name collisions */
1111 LY_CHECK_GOTO(ret = lysp_check_dup_typedefs(pctx, mod->parsed), error_ctx);
1112 /* TODO groupings */
1113 LY_CHECK_GOTO(ret = lysp_check_dup_features(pctx, mod->parsed), error_ctx);
1114 LY_CHECK_GOTO(ret = lysp_check_dup_identities(pctx, mod->parsed), error_ctx);
1115
1116 /* compile features */
1117 LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), error_ctx);
1118
Michal Vasko89b5c072020-10-06 13:52:44 +02001119 if (!implement) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001120 /* pre-compile identities of the module */
1121 LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), error);
1122
1123 /* pre-compile identities of any submodules */
Michal Vasko7a0b0762020-09-02 16:37:01 +02001124 LY_ARRAY_FOR(mod->parsed->includes, u) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001125 submod = mod->parsed->includes[u].submodule;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001126 ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
1127 LY_CHECK_GOTO(ret, error);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001128 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001129 } else {
Michal Vasko89b5c072020-10-06 13:52:44 +02001130 /* implement (compile) */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001131 LY_CHECK_GOTO(ret = lys_set_implemented(mod, features), error_ctx);
Michal Vasko7a0b0762020-09-02 16:37:01 +02001132 }
1133
1134 if (format == LYS_IN_YANG) {
1135 yang_parser_ctx_free(yangctx);
1136 } else {
1137 yin_parser_ctx_free(yinctx);
1138 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001139 if (module) {
1140 *module = mod;
1141 }
1142 return LY_SUCCESS;
Michal Vasko7a0b0762020-09-02 16:37:01 +02001143
1144error_ctx:
1145 ly_set_rm(&ctx->list, mod, NULL);
1146error:
1147 lys_module_free(mod, NULL);
1148 if (pctx) {
1149 ly_set_erase(&pctx->tpdfs_nodes, NULL);
1150 }
1151 if (format == LYS_IN_YANG) {
1152 yang_parser_ctx_free(yangctx);
1153 } else {
1154 yin_parser_ctx_free(yinctx);
1155 }
1156
1157 return ret;
1158}
1159
1160API LY_ERR
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001161lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, const struct lys_module **module)
Michal Vasko7a0b0762020-09-02 16:37:01 +02001162{
1163 if (module) {
1164 *module = NULL;
1165 }
1166 LY_CHECK_ARG_RET(NULL, ctx, in, format > LYS_IN_UNKNOWN, LY_EINVAL);
1167
1168 /* remember input position */
1169 in->func_start = in->current;
1170
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001171 return lys_create_module(ctx, in, format, 1, NULL, NULL, features, (struct lys_module **)module);
Radek Krejci86d106e2018-10-18 09:53:19 +02001172}
1173
Michal Vasko3a41dff2020-07-15 14:30:28 +02001174API LY_ERR
1175lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001176{
Radek Krejci0f969882020-08-21 16:56:47 +02001177 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001178 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001179
Michal Vasko3a41dff2020-07-15 14:30:28 +02001180 LY_CHECK_ARG_RET(ctx, data, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci65639b92018-11-27 10:51:37 +01001181
Michal Vasko3a41dff2020-07-15 14:30:28 +02001182 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 +02001183
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001184 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001185 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001186
Michal Vasko3a41dff2020-07-15 14:30:28 +02001187 return ret;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001188}
1189
Michal Vasko3a41dff2020-07-15 14:30:28 +02001190API LY_ERR
1191lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejci86d106e2018-10-18 09:53:19 +02001192{
Radek Krejci0f969882020-08-21 16:56:47 +02001193 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001194 struct ly_in *in = NULL;
Radek Krejci86d106e2018-10-18 09:53:19 +02001195
Michal Vasko3a41dff2020-07-15 14:30:28 +02001196 LY_CHECK_ARG_RET(ctx, fd > -1, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejci86d106e2018-10-18 09:53:19 +02001197
Michal Vasko3a41dff2020-07-15 14:30:28 +02001198 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 +02001199
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001200 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001201 ly_in_free(in, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +02001202
Michal Vasko3a41dff2020-07-15 14:30:28 +02001203 return ret;
Radek Krejci86d106e2018-10-18 09:53:19 +02001204}
1205
Michal Vasko3a41dff2020-07-15 14:30:28 +02001206API LY_ERR
1207lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const struct lys_module **module)
Radek Krejcid33273d2018-10-25 14:55:52 +02001208{
Radek Krejci0f969882020-08-21 16:56:47 +02001209 LY_ERR ret;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001210 struct ly_in *in = NULL;
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001211
Michal Vasko3a41dff2020-07-15 14:30:28 +02001212 LY_CHECK_ARG_RET(ctx, path, format != LYS_IN_UNKNOWN, LY_EINVAL);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001213
Michal Vasko3a41dff2020-07-15 14:30:28 +02001214 LY_CHECK_ERR_RET(ret = ly_in_new_filepath(path, 0, &in),
Michal Vasko69730152020-10-09 16:30:07 +02001215 LOGERR(ctx, ret, "Unable to create input handler for filepath %s.", path), ret);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001216
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001217 ret = lys_parse(ctx, in, format, NULL, module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001218 ly_in_free(in, 0);
1219
Michal Vasko3a41dff2020-07-15 14:30:28 +02001220 return ret;
Radek Krejcid33273d2018-10-25 14:55:52 +02001221}
1222
1223API LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02001224lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +02001225 char **localfile, LYS_INFORMAT *format)
Radek Krejcid33273d2018-10-25 14:55:52 +02001226{
Radek Krejci1deb5be2020-08-26 16:43:36 +02001227 LY_ERR ret = LY_EMEM;
Radek Krejcid33273d2018-10-25 14:55:52 +02001228 size_t len, flen, match_len = 0, dir_len;
Radek Krejci857189e2020-09-01 13:26:36 +02001229 ly_bool implicit_cwd = 0;
Radek Krejcid33273d2018-10-25 14:55:52 +02001230 char *wd, *wn = NULL;
1231 DIR *dir = NULL;
1232 struct dirent *file;
1233 char *match_name = NULL;
1234 LYS_INFORMAT format_aux, match_format = 0;
1235 struct ly_set *dirs;
1236 struct stat st;
1237
1238 LY_CHECK_ARG_RET(NULL, localfile, LY_EINVAL);
1239
1240 /* start to fill the dir fifo with the context's search path (if set)
1241 * and the current working directory */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001242 LY_CHECK_RET(ly_set_new(&dirs));
Radek Krejcid33273d2018-10-25 14:55:52 +02001243
1244 len = strlen(name);
1245 if (cwd) {
1246 wd = get_current_dir_name();
1247 if (!wd) {
1248 LOGMEM(NULL);
1249 goto cleanup;
1250 } else {
1251 /* add implicit current working directory (./) to be searched,
1252 * this directory is not searched recursively */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001253 ret = ly_set_add(dirs, wd, 0, NULL);
1254 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001255 implicit_cwd = 1;
1256 }
1257 }
1258 if (searchpaths) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001259 for (uint64_t i = 0; searchpaths[i]; i++) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001260 /* check for duplicities with the implicit current working directory */
1261 if (implicit_cwd && !strcmp(dirs->objs[0], searchpaths[i])) {
1262 implicit_cwd = 0;
1263 continue;
1264 }
1265 wd = strdup(searchpaths[i]);
1266 if (!wd) {
1267 LOGMEM(NULL);
1268 goto cleanup;
Radek Krejciba03a5a2020-08-27 14:40:41 +02001269 } else {
1270 ret = ly_set_add(dirs, wd, 0, NULL);
1271 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcid33273d2018-10-25 14:55:52 +02001272 }
1273 }
1274 }
1275 wd = NULL;
1276
1277 /* start searching */
1278 while (dirs->count) {
1279 free(wd);
1280 free(wn); wn = NULL;
1281
1282 dirs->count--;
1283 wd = (char *)dirs->objs[dirs->count];
1284 dirs->objs[dirs->count] = NULL;
1285 LOGVRB("Searching for \"%s\" in %s.", name, wd);
1286
1287 if (dir) {
1288 closedir(dir);
1289 }
1290 dir = opendir(wd);
1291 dir_len = strlen(wd);
1292 if (!dir) {
1293 LOGWRN(NULL, "Unable to open directory \"%s\" for searching (sub)modules (%s).", wd, strerror(errno));
1294 } else {
1295 while ((file = readdir(dir))) {
1296 if (!strcmp(".", file->d_name) || !strcmp("..", file->d_name)) {
1297 /* skip . and .. */
1298 continue;
1299 }
1300 free(wn);
1301 if (asprintf(&wn, "%s/%s", wd, file->d_name) == -1) {
1302 LOGMEM(NULL);
1303 goto cleanup;
1304 }
1305 if (stat(wn, &st) == -1) {
1306 LOGWRN(NULL, "Unable to get information about \"%s\" file in \"%s\" when searching for (sub)modules (%s)",
Michal Vasko69730152020-10-09 16:30:07 +02001307 file->d_name, wd, strerror(errno));
Radek Krejcid33273d2018-10-25 14:55:52 +02001308 continue;
1309 }
1310 if (S_ISDIR(st.st_mode) && (dirs->count || !implicit_cwd)) {
1311 /* we have another subdirectory in searchpath to explore,
1312 * subdirectories are not taken into account in current working dir (dirs->set.g[0]) */
Radek Krejciba03a5a2020-08-27 14:40:41 +02001313 ret = ly_set_add(dirs, wn, 0, NULL);
1314 LY_CHECK_GOTO(ret, cleanup);
1315
Radek Krejcid33273d2018-10-25 14:55:52 +02001316 /* continue with the next item in current directory */
1317 wn = NULL;
1318 continue;
1319 } else if (!S_ISREG(st.st_mode)) {
1320 /* not a regular file (note that we see the target of symlinks instead of symlinks */
1321 continue;
1322 }
1323
1324 /* here we know that the item is a file which can contain a module */
1325 if (strncmp(name, file->d_name, len) ||
Michal Vasko69730152020-10-09 16:30:07 +02001326 ((file->d_name[len] != '.') && (file->d_name[len] != '@'))) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001327 /* different filename than the module we search for */
1328 continue;
1329 }
1330
1331 /* get type according to filename suffix */
1332 flen = strlen(file->d_name);
Radek Krejcied5acc52019-04-25 15:57:04 +02001333 if (!strcmp(&file->d_name[flen - 5], ".yang")) {
Radek Krejcid33273d2018-10-25 14:55:52 +02001334 format_aux = LYS_IN_YANG;
Radek Krejci0f969882020-08-21 16:56:47 +02001335 /* TODO YIN parser
1336 } else if (!strcmp(&file->d_name[flen - 4], ".yin")) {
1337 format_aux = LYS_IN_YIN;
1338 */
Radek Krejcid33273d2018-10-25 14:55:52 +02001339 } else {
1340 /* not supportde suffix/file format */
1341 continue;
1342 }
1343
1344 if (revision) {
1345 /* we look for the specific revision, try to get it from the filename */
1346 if (file->d_name[len] == '@') {
1347 /* check revision from the filename */
1348 if (strncmp(revision, &file->d_name[len + 1], strlen(revision))) {
1349 /* another revision */
1350 continue;
1351 } else {
1352 /* exact revision */
1353 free(match_name);
1354 match_name = wn;
1355 wn = NULL;
1356 match_len = dir_len + 1 + len;
1357 match_format = format_aux;
1358 goto success;
1359 }
1360 } else {
1361 /* continue trying to find exact revision match, use this only if not found */
1362 free(match_name);
1363 match_name = wn;
1364 wn = NULL;
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001365 match_len = dir_len + 1 + len;
Radek Krejcid33273d2018-10-25 14:55:52 +02001366 match_format = format_aux;
1367 continue;
1368 }
1369 } else {
1370 /* remember the revision and try to find the newest one */
1371 if (match_name) {
Michal Vasko69730152020-10-09 16:30:07 +02001372 if ((file->d_name[len] != '@') ||
1373 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 +02001374 continue;
Michal Vasko69730152020-10-09 16:30:07 +02001375 } else if ((match_name[match_len] == '@') &&
Radek Krejcid33273d2018-10-25 14:55:52 +02001376 (strncmp(&match_name[match_len + 1], &file->d_name[len + 1], LY_REV_SIZE - 1) >= 0)) {
1377 continue;
1378 }
1379 free(match_name);
1380 }
1381
1382 match_name = wn;
1383 wn = NULL;
1384 match_len = dir_len + 1 + len;
1385 match_format = format_aux;
1386 continue;
1387 }
1388 }
1389 }
1390 }
1391
1392success:
1393 (*localfile) = match_name;
1394 match_name = NULL;
1395 if (format) {
1396 (*format) = match_format;
1397 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02001398 ret = LY_SUCCESS;
Radek Krejcid33273d2018-10-25 14:55:52 +02001399
1400cleanup:
1401 free(wn);
1402 free(wd);
1403 if (dir) {
1404 closedir(dir);
1405 }
1406 free(match_name);
1407 ly_set_free(dirs, free);
1408
1409 return ret;
1410}