blob: fcccb6422abd87e13ef594bf1ebd434b69d67b98 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
Michal Vasko2d162e12015-09-24 14:33:29 +02002 * @file tree_schema.c
Radek Krejcida04f4a2015-05-21 12:54:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko2d162e12015-09-24 14:33:29 +02004 * @brief Manipulation with libyang schema data structures
Radek Krejcida04f4a2015-05-21 12:54:09 +02005 *
Michal Vasko53b7da02018-02-13 15:28:42 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejcida04f4a2015-05-21 12:54:09 +02007 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
Radek Krejci54f6fb32016-02-24 12:56:39 +010014
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
Radek Krejcida04f4a2015-05-21 12:54:09 +020016
Michal Vaskob0bbf5f2018-02-16 09:35:59 +010017#ifdef __APPLE__
18# include <sys/param.h>
19#endif
Radek Krejci812b10a2015-05-28 16:48:25 +020020#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020021#include <ctype.h>
Radek Krejcib051f722016-02-25 15:12:21 +010022#include <limits.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <stdlib.h>
24#include <sys/mman.h>
Michal Vasko662610a2015-12-07 11:25:45 +010025#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020026#include <sys/stat.h>
Michal Vasko662610a2015-12-07 11:25:45 +010027#include <fcntl.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020028#include <string.h>
Michal Vasko662610a2015-12-07 11:25:45 +010029#include <unistd.h>
30#include <errno.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020031
32#include "common.h"
33#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020034#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020035#include "resolve.h"
Michal Vasko88c29542015-11-27 14:57:53 +010036#include "xml.h"
Michal Vasko5b3492c2016-07-20 09:37:40 +020037#include "xpath.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020038#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020039#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020040#include "validation.h"
Pavol Vicanf7cc2852016-03-22 23:27:35 +010041#include "parser_yang.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020042
Radek Krejci8d6b7422017-02-03 14:42:13 +010043static int lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci5138e9f2017-04-12 13:10:46 +020044 int in_grp, int shallow, struct unres_schema *unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020045
Radek Krejci65aca412018-01-24 11:23:06 +010046API const struct lys_node_list *
47lys_is_key(const struct lys_node_leaf *node, uint8_t *index)
48{
49 struct lys_node *parent = (struct lys_node *)node;
50 struct lys_node_list *list;
51 uint8_t i;
52
53 if (!node || node->nodetype != LYS_LEAF) {
54 return NULL;
55 }
56
57 do {
58 parent = lys_parent(parent);
59 } while (parent && parent->nodetype == LYS_USES);
60
61 if (!parent || parent->nodetype != LYS_LIST) {
62 return NULL;
63 }
64
65 list = (struct lys_node_list*)parent;
66 for (i = 0; i < list->keys_size; i++) {
67 if (list->keys[i] == node) {
68 if (index) {
69 (*index) = i;
70 }
71 return list;
72 }
73 }
74 return NULL;
75}
76
Radek Krejci9ff0a922016-07-14 13:08:05 +020077API const struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +010078lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020079{
Radek Krejci9ff0a922016-07-14 13:08:05 +020080 int i;
Radek Krejci48061fb2015-08-05 15:41:07 +020081
Radek Krejci27fe55e2016-09-13 17:13:35 +020082 if (!node) {
83 return NULL;
84 }
85
Radek Krejci48061fb2015-08-05 15:41:07 +020086check:
87 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
88 /* input/output does not have if-feature, so skip them */
89
90 /* check local if-features */
Michal Vaskoc5c26b02016-06-29 11:10:29 +020091 for (i = 0; i < node->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +020092 if (!resolve_iffeature(&node->iffeature[i])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +020093 return node;
Radek Krejci48061fb2015-08-05 15:41:07 +020094 }
95 }
96 }
97
98 if (!recursive) {
99 return NULL;
100 }
101
102 /* go through parents */
103 if (node->nodetype == LYS_AUGMENT) {
104 /* go to parent actually means go to the target node */
105 node = ((struct lys_node_augment *)node)->target;
Michal Vasko17549192017-03-13 10:19:33 +0100106 if (!node) {
107 /* unresolved augment, let's say it's enabled */
108 return NULL;
109 }
Radek Krejci48061fb2015-08-05 15:41:07 +0200110 } else if (node->parent) {
111 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +0200112 } else {
113 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +0200114 }
115
Radek Krejci074bf852015-08-19 14:22:16 +0200116 if (recursive == 2) {
117 /* continue only if the node cannot have a data instance */
118 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
119 return NULL;
120 }
121 }
122 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +0200123}
124
Michal Vasko1dca6882015-10-22 14:29:42 +0200125int
Michal Vasko36cbaa42015-12-14 13:15:48 +0100126lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
127 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +0200128{
Radek Krejcic071c542016-01-27 14:57:51 +0100129 const struct lys_node *node, *parent = NULL;
130 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +0100131 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +0200132
Michal Vasko36cbaa42015-12-14 13:15:48 +0100133 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +0200134 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +0200135
Michal Vasko36cbaa42015-12-14 13:15:48 +0100136 /* fill the lengths in case the caller is so indifferent */
137 if (!mod_name_len) {
138 mod_name_len = strlen(mod_name);
139 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200140 if (!nam_len) {
141 nam_len = strlen(name);
142 }
143
Michal Vasko9e635ac2016-10-17 11:44:09 +0200144 while (siblings && (siblings->nodetype == LYS_USES)) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200145 siblings = siblings->child;
146 }
Michal Vasko9e635ac2016-10-17 11:44:09 +0200147 if (!siblings) {
148 /* unresolved uses */
149 return EXIT_FAILURE;
150 }
151
Michal Vasko680f8b42016-10-17 10:27:37 +0200152 if (siblings->nodetype == LYS_GROUPING) {
153 for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev);
154 if (node->nodetype == LYS_GROUPING) {
155 /* we went through all the siblings, only groupings there - no valid sibling */
156 return EXIT_FAILURE;
157 }
158 /* update siblings to be valid */
159 siblings = node;
160 }
161
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200162 /* set parent correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100163 parent = lys_parent(siblings);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200164
Michal Vasko680f8b42016-10-17 10:27:37 +0200165 /* go up all uses */
166 while (parent && (parent->nodetype == LYS_USES)) {
167 parent = lys_parent(parent);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200168 }
169
Radek Krejcic071c542016-01-27 14:57:51 +0100170 if (!parent) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200171 /* handle situation when there is a top-level uses referencing a foreign grouping */
172 for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node));
173 mod = lys_node_module(node);
Michal Vasko1dca6882015-10-22 14:29:42 +0200174 }
175
Radek Krejcic071c542016-01-27 14:57:51 +0100176 /* try to find the node */
177 node = NULL;
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100178 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Radek Krejcic071c542016-01-27 14:57:51 +0100179 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100180 /* module name comparison */
181 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200182 if (!ly_strequal(node_mod_name, mod_name, 1) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100183 continue;
184 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200185
Radek Krejcic071c542016-01-27 14:57:51 +0100186 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200187 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100188 if (ret) {
189 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200190 }
Radek Krejcic071c542016-01-27 14:57:51 +0100191 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200192 }
193 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200194 }
195
196 return EXIT_FAILURE;
197}
198
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200199int
Michal Vaskobb520442017-05-23 10:55:18 +0200200lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len,
201 LYS_NODE type, const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200202{
Michal Vaskobb520442017-05-23 10:55:18 +0200203 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200204
Michal Vaskobb520442017-05-23 10:55:18 +0200205 assert((mod || parent) && name);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200206 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
207
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200208 if (!mod) {
Michal Vaskobb520442017-05-23 10:55:18 +0200209 mod = lys_node_module(parent);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200210 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200211
Michal Vasko4f0dad02016-02-15 14:08:23 +0100212 /* try to find the node */
213 node = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +0100214 while ((node = lys_getnext(node, parent, mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100215 if (!type || (node->nodetype & type)) {
216 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200217 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100218 continue;
219 }
220
Michal Vasko4f0dad02016-02-15 14:08:23 +0100221 /* direct name check */
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100222 if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100223 if (ret) {
224 *ret = node;
225 }
226 return EXIT_SUCCESS;
227 }
Radek Krejcic071c542016-01-27 14:57:51 +0100228 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200229 }
230
231 return EXIT_FAILURE;
232}
233
Michal Vasko1e62a092015-12-01 12:27:20 +0100234API const struct lys_node *
235lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200236{
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100237 const struct lys_node *next, *aug_parent;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100238 struct lys_node **snode;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200239
Michal Vasko75702cd2018-02-12 11:27:09 +0100240 if ((!parent && !module) || (module && module->type) || (parent && (parent->nodetype == LYS_USES) && !(options & LYS_GETNEXT_PARENTUSES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100241 LOGARG;
Michal Vasko24476fa2017-03-08 12:33:48 +0100242 return NULL;
243 }
244
Radek Krejci8bc87f62015-09-02 16:19:05 +0200245 if (!last) {
246 /* first call */
247
248 /* get know where to start */
249 if (parent) {
250 /* schema subtree */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100251 snode = lys_child(parent, LYS_UNKNOWN);
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100252 /* do not return anything if the augment does not have any children */
Radek Krejcibb08db32017-07-03 11:29:17 +0200253 if (!snode || !(*snode) || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100254 return NULL;
255 }
256 next = last = *snode;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200257 } else {
258 /* top level data */
Michal Vaskocb45f472018-02-12 10:47:42 +0100259 if (!(options & LYS_GETNEXT_NOSTATECHECK) && (module->disabled || !module->implemented)) {
260 /* nothing to return from a disabled/imported module */
261 return NULL;
262 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200263 next = last = module->data;
264 }
Radek Krejci972724f2016-08-12 15:24:40 +0200265 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
266 /* continue with uses content */
267 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200268 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200269 /* continue after the last returned value */
270 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200271 }
272
273repeat:
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100274 if (parent && (parent->nodetype == LYS_AUGMENT) && next) {
275 /* do not return anything outside the parent augment */
276 aug_parent = next->parent;
277 do {
278 while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) {
279 aug_parent = aug_parent->parent;
280 }
281 if (aug_parent) {
282 if (aug_parent == parent) {
283 break;
284 }
285 aug_parent = ((struct lys_node_augment *)aug_parent)->target;
286 }
287
288 } while (aug_parent);
289 if (!aug_parent) {
290 return NULL;
291 }
292 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200293 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200294 if (options & LYS_GETNEXT_WITHGROUPING) {
295 return next;
296 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200297 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200298 }
299
Radek Krejci972724f2016-08-12 15:24:40 +0200300 if (!next) { /* cover case when parent is augment */
301 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200302 /* no next element */
303 return NULL;
304 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200305 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200306 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200307 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200308 } else {
309 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200310 }
311
Michal Vaskocb45f472018-02-12 10:47:42 +0100312 if (!(options & LYS_GETNEXT_NOSTATECHECK) && lys_is_disabled(next, 0)) {
313 next = next->next;
314 goto repeat;
315 }
316
Radek Krejci7f40ce32015-08-12 20:38:46 +0200317 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200318 case LYS_INPUT:
319 case LYS_OUTPUT:
320 if (options & LYS_GETNEXT_WITHINOUT) {
321 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200322 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200323 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200324 } else {
325 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200326 }
Radek Krejci972724f2016-08-12 15:24:40 +0200327 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200328
Michal Vaskoa5835e92015-10-20 15:07:39 +0200329 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200330 if (options & LYS_GETNEXT_WITHCASE) {
331 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200332 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200333 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200334 } else {
335 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200336 }
Radek Krejci972724f2016-08-12 15:24:40 +0200337 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200338
Michal Vasko1dca6882015-10-22 14:29:42 +0200339 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200340 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200341 if (options & LYS_GETNEXT_WITHUSES) {
342 return next;
343 } else if (next->child) {
344 next = next->child;
345 } else {
346 next = next->next;
347 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200348 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200349
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200350 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200351 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200352 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200353 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200354 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200355 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200356 case LYS_LIST:
357 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200358 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200359
Radek Krejci972724f2016-08-12 15:24:40 +0200360 case LYS_CONTAINER:
361 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
362 if (next->child) {
363 /* go into */
364 next = next->child;
365 } else {
366 next = next->next;
367 }
368 goto repeat;
369 } else {
370 return next;
371 }
372
Radek Krejci8bc87f62015-09-02 16:19:05 +0200373 case LYS_CHOICE:
374 if (options & LYS_GETNEXT_WITHCHOICE) {
375 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200376 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200377 /* go into */
378 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200379 } else {
380 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200381 }
Radek Krejci972724f2016-08-12 15:24:40 +0200382 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200383
Radek Krejci7f40ce32015-08-12 20:38:46 +0200384 default:
385 /* we should not be here */
386 return NULL;
387 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200388}
389
Radek Krejcibf285832017-01-26 16:05:41 +0100390void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200391lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200392{
Michal Vaskof75b2772018-03-14 09:55:33 +0100393 struct lys_node *parent, *first, **pp = NULL;
Radek Krejcic071c542016-01-27 14:57:51 +0100394 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200395
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200396 if (!node) {
397 return;
398 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200399
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200400 /* unlink from data model if necessary */
401 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100402 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100403 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100404 if (main_module->data == node) {
405 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200406 }
407 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200408
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200409 /* store pointers to important nodes */
410 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200411 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200412 /* handle augments - first, unlink it from the augment parent ... */
413 if (parent->child == node) {
Radek Krejcic9d78692017-08-24 17:17:18 +0200414 parent->child = (node->next && node->next->parent == parent) ? node->next : NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200415 }
Radek Krejcifec2e142017-01-05 15:19:03 +0100416
417 if (parent->flags & LYS_NOTAPPLIED) {
418 /* data are not connected in the target, so we cannot continue with the target as a parent */
419 parent = NULL;
420 } else {
421 /* data are connected in target, so we will continue with the target as a parent */
422 parent = ((struct lys_node_augment *)parent)->target;
423 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200424 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200425
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200426 /* unlink from parent */
427 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100428 if (parent->nodetype == LYS_EXT) {
429 pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype),
430 (struct lys_ext_instance_complex*)parent, NULL);
431 if (*pp == node) {
432 *pp = node->next;
433 }
434 } else if (parent->child == node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200435 parent->child = node->next;
436 }
437 node->parent = NULL;
438 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200439
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200440 /* unlink from siblings */
441 if (node->prev == node) {
442 /* there are no more siblings */
443 return;
444 }
445 if (node->next) {
446 node->next->prev = node->prev;
447 } else {
448 /* unlinking the last element */
449 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100450 if (parent->nodetype == LYS_EXT) {
451 first = *(struct lys_node **)pp;
452 } else {
453 first = parent->child;
454 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200455 } else {
456 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200457 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200458 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200459 }
460 }
461 first->prev = node->prev;
462 }
463 if (node->prev->next) {
464 node->prev->next = node->next;
465 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200466
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200467 /* clean up the unlinked element */
468 node->next = NULL;
469 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200470}
471
Michal Vasko563ef092015-09-04 13:17:23 +0200472struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100473lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200474{
475 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200476
477 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200478 /* top-level augment, look into module (uses augment is handled correctly below) */
479 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
Radek Krejci115fa882017-03-01 16:15:07 +0100480 par_iter = lys_main_module(par_iter->parent->module)->data;
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200481 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200482 break;
483 }
484 }
485
Michal Vasko38a00412018-03-14 10:01:50 +0100486 if (par_iter->nodetype == LYS_EXT) {
487 /* we are in a top-level extension, search grouping in top-level groupings */
488 par_iter = lys_main_module(par_iter->module)->data;
489 if (!par_iter) {
490 /* not connected yet, wait */
491 return NULL;
492 }
493 } else if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200494 continue;
495 }
496
497 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
498 if (!stop) {
499 stop = par_iter;
500 } else if (iter == stop) {
501 break;
502 }
503 if (iter->nodetype != LYS_GROUPING) {
504 continue;
505 }
506
Radek Krejcif8426a72015-10-31 23:14:03 +0100507 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200508 return (struct lys_node_grp *)iter;
509 }
510 }
511 }
512
Michal Vasko563ef092015-09-04 13:17:23 +0200513 return NULL;
514}
515
Radek Krejci10c760e2015-08-14 14:45:43 +0200516/*
517 * get next grouping in the root's subtree, in the
518 * first call, tha last is NULL
519 */
520static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200521lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200522{
Radek Krejci10c760e2015-08-14 14:45:43 +0200523 struct lys_node *last = (struct lys_node *)lastgrp;
524 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200525
Radek Krejci10c760e2015-08-14 14:45:43 +0200526 assert(root);
527
528 if (!last) {
529 last = root;
530 }
531
532 while (1) {
533 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
534 next = last->child;
535 } else {
536 next = NULL;
537 }
538 if (!next) {
539 if (last == root) {
540 /* we are done */
541 return NULL;
542 }
543
544 /* no children, go to siblings */
545 next = last->next;
546 }
547 while (!next) {
548 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100549 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200550 /* we are done */
551 return NULL;
552 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200553 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100554 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200555 }
556
557 if (next->nodetype == LYS_GROUPING) {
558 return (struct lys_node_grp *)next;
559 }
560
561 last = next;
562 }
563}
564
Michal Vasko0d343d12015-08-24 14:57:36 +0200565/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200566int
Radek Krejci07911992015-08-14 15:13:31 +0200567lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
568{
Michal Vasko563ef092015-09-04 13:17:23 +0200569 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200570 struct lys_node_grp *grp;
Radek Krejcif95b6292017-02-13 15:57:37 +0100571 int down, up;
Radek Krejci07911992015-08-14 15:13:31 +0200572
573 assert(node);
574
575 if (!parent) {
576 assert(module);
577 } else {
578 module = parent->module;
579 }
Radek Krejci115fa882017-03-01 16:15:07 +0100580 module = lys_main_module(module);
Radek Krejci07911992015-08-14 15:13:31 +0200581
582 switch (node->nodetype) {
583 case LYS_GROUPING:
584 /* 6.2.1, rule 6 */
585 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100586 start = *lys_child(parent, LYS_GROUPING);
587 if (!start) {
Radek Krejci07911992015-08-14 15:13:31 +0200588 down = 0;
589 start = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100590 } else {
591 down = 1;
592 }
593 if (parent->nodetype == LYS_EXT) {
594 up = 0;
595 } else {
596 up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200597 }
598 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100599 down = up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200600 start = module->data;
601 }
602 /* go up */
Radek Krejcif95b6292017-02-13 15:57:37 +0100603 if (up && lys_find_grouping_up(node->name, start)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100604 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200605 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200606 }
607 /* go down, because grouping can be defined after e.g. container in which is collision */
608 if (down) {
609 for (iter = start, stop = NULL; iter; iter = iter->prev) {
610 if (!stop) {
611 stop = start;
612 } else if (iter == stop) {
613 break;
614 }
615 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
616 continue;
617 }
618
619 grp = NULL;
620 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100621 if (ly_strequal(node->name, grp->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100622 LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200623 return EXIT_FAILURE;
624 }
625 }
626 }
627 }
628 break;
629 case LYS_LEAF:
630 case LYS_LEAFLIST:
631 case LYS_LIST:
632 case LYS_CONTAINER:
633 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200634 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200635 /* 6.2.1, rule 7 */
636 if (parent) {
637 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200638 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
639 if (iter->nodetype == LYS_AUGMENT) {
640 if (((struct lys_node_augment *)iter)->target) {
641 /* augment is resolved, go up */
642 iter = ((struct lys_node_augment *)iter)->target;
643 continue;
644 }
645 /* augment is not resolved, this is the final parent */
646 break;
647 }
Radek Krejci07911992015-08-14 15:13:31 +0200648 iter = iter->parent;
649 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200650
Radek Krejci07911992015-08-14 15:13:31 +0200651 if (!iter) {
652 stop = NULL;
653 iter = module->data;
Radek Krejcif95b6292017-02-13 15:57:37 +0100654 } else if (iter->nodetype == LYS_EXT) {
655 stop = iter;
PavolVican9d61d402018-02-05 15:52:48 +0100656 iter = (struct lys_node *)lys_child(iter, node->nodetype);
657 if (iter) {
658 iter = *(struct lys_node **)iter;
659 }
Radek Krejci07911992015-08-14 15:13:31 +0200660 } else {
661 stop = iter;
662 iter = iter->child;
663 }
664 } else {
665 stop = NULL;
666 iter = module->data;
667 }
668 while (iter) {
669 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
670 iter = iter->child;
671 continue;
672 }
673
Radek Krejcibf2abff2016-08-23 15:51:52 +0200674 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100675 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100676 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200677 return EXIT_FAILURE;
678 }
679 }
680
681 /* special case for choice - we must check the choice's name as
682 * well as the names of nodes under the choice
683 */
684 if (iter->nodetype == LYS_CHOICE) {
685 iter = iter->child;
686 continue;
687 }
688
689 /* go to siblings */
690 if (!iter->next) {
691 /* no sibling, go to parent's sibling */
692 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200693 /* for parent LYS_AUGMENT */
694 if (iter->parent == stop) {
695 iter = stop;
696 break;
697 }
698 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200699 if (iter && iter->next) {
700 break;
701 }
702 } while (iter != stop);
703
704 if (iter == stop) {
705 break;
706 }
707 }
708 iter = iter->next;
709 }
710 break;
711 case LYS_CASE:
712 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100713 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100714 start = *lys_child(parent, LYS_CASE);
Radek Krejcic071c542016-01-27 14:57:51 +0100715 } else {
716 start = module->data;
717 }
718
719 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200720 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200721 continue;
722 }
723
Radek Krejci749190d2016-02-18 16:26:25 +0100724 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100725 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200726 return EXIT_FAILURE;
727 }
728 }
729 break;
730 default:
731 /* no check needed */
732 break;
733 }
734
735 return EXIT_SUCCESS;
736}
737
Michal Vasko0d343d12015-08-24 14:57:36 +0200738/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200739int
Radek Krejci10c760e2015-08-14 14:45:43 +0200740lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
741{
Michal Vasko53b7da02018-02-13 15:28:42 +0100742 struct ly_ctx *ctx = child->module->ctx;
Radek Krejcic9d78692017-08-24 17:17:18 +0200743 struct lys_node *iter, **pchild;
Radek Krejci41a349b2016-10-24 19:21:59 +0200744 struct lys_node_inout *in, *out, *inout;
Radek Krejci744c2d42017-03-26 13:30:00 -0500745 struct lys_node_case *c;
746 int type, shortcase = 0;
Radek Krejcif95b6292017-02-13 15:57:37 +0100747 void *p;
748 struct lyext_substmt *info = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200749
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200750 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200751
Radek Krejci10c760e2015-08-14 14:45:43 +0200752 if (parent) {
753 type = parent->nodetype;
754 module = parent->module;
755 } else {
756 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100757 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200758 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200759 }
760
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200761 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200762 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200763 case LYS_CONTAINER:
764 case LYS_LIST:
765 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100766 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200767 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200768 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200769 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100770 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200771 return EXIT_FAILURE;
772 }
773 break;
Radek Krejci76512572015-08-04 09:47:08 +0200774 case LYS_INPUT:
775 case LYS_OUTPUT:
776 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200777 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200778 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200779 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100780 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200781 return EXIT_FAILURE;
782 }
783 break;
Radek Krejci76512572015-08-04 09:47:08 +0200784 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200785 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200786 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100787 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200788 return EXIT_FAILURE;
789 }
Radek Krejci744c2d42017-03-26 13:30:00 -0500790 if (child->nodetype != LYS_CASE) {
791 shortcase = 1;
792 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200793 break;
Radek Krejci76512572015-08-04 09:47:08 +0200794 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200795 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200796 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100797 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200798 return EXIT_FAILURE;
799 }
800 break;
Radek Krejci76512572015-08-04 09:47:08 +0200801 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200802 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200803 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100804 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200805 return EXIT_FAILURE;
806 }
807 break;
Radek Krejci76512572015-08-04 09:47:08 +0200808 case LYS_LEAF:
809 case LYS_LEAFLIST:
810 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200811 case LYS_ANYDATA:
Michal Vasko53b7da02018-02-13 15:28:42 +0100812 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
813 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100814 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200815 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200816 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200817 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200818 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskodb017262017-01-24 13:10:04 +0100819 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100820 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200821 return EXIT_FAILURE;
822 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200823 break;
824 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200825 /* top level */
826 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200827 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200828 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100829 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200830 return EXIT_FAILURE;
831 }
Radek Krejcif95b6292017-02-13 15:57:37 +0100832 break;
833 case LYS_EXT:
834 /* plugin-defined */
835 p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info);
836 if (!p) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100837 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype),
Radek Krejcif95b6292017-02-13 15:57:37 +0100838 ((struct lys_ext_instance_complex*)parent)->def->name);
839 return EXIT_FAILURE;
840 }
841 /* TODO check cardinality */
Radek Krejcic071c542016-01-27 14:57:51 +0100842 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200843 }
844
845 /* check identifier uniqueness */
Michal Vasko15a43372017-09-25 14:12:42 +0200846 if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && lys_check_id(child, parent, module)) {
Radek Krejci07911992015-08-14 15:13:31 +0200847 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200848 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200849
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200850 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200851 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200852 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200853
Radek Krejcif95b6292017-02-13 15:57:37 +0100854 if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200855 /* replace the implicit input/output node */
856 if (child->nodetype == LYS_OUTPUT) {
857 inout = (struct lys_node_inout *)parent->child->next;
858 } else { /* LYS_INPUT */
859 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200860 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200861 }
862 if (inout->next) {
863 child->next = inout->next;
864 inout->next->prev = child;
865 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200866 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200867 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200868 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200869 child->prev = inout->prev;
870 if (inout->prev->next) {
871 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200872 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200873 inout->prev = (struct lys_node *)inout;
874 child->parent = parent;
875 inout->parent = NULL;
876 lys_node_free((struct lys_node *)inout, NULL, 0);
877 } else {
Radek Krejci744c2d42017-03-26 13:30:00 -0500878 if (shortcase) {
879 /* create the implicit case to allow it to serve as a target of the augments,
880 * it won't be printed, but it will be present in the tree */
881 c = calloc(1, sizeof *c);
Michal Vasko53b7da02018-02-13 15:28:42 +0100882 LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE);
Radek Krejci744c2d42017-03-26 13:30:00 -0500883 c->name = lydict_insert(module->ctx, child->name, 0);
884 c->flags = LYS_IMPLICIT;
885 c->module = module;
886 c->nodetype = LYS_CASE;
887 c->prev = (struct lys_node*)c;
888 lys_node_addchild(parent, module, (struct lys_node*)c);
889 parent = (struct lys_node*)c;
890 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200891 /* connect the child correctly */
892 if (!parent) {
893 if (module->data) {
894 module->data->prev->next = child;
895 child->prev = module->data->prev;
896 module->data->prev = child;
897 } else {
898 module->data = child;
899 }
900 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100901 pchild = lys_child(parent, child->nodetype);
902 assert(pchild);
903
Radek Krejcic9d78692017-08-24 17:17:18 +0200904 child->parent = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100905 if (!(*pchild)) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200906 /* the only/first child of the parent */
Radek Krejcif95b6292017-02-13 15:57:37 +0100907 *pchild = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200908 iter = child;
909 } else {
910 /* add a new child at the end of parent's child list */
Radek Krejcif95b6292017-02-13 15:57:37 +0100911 iter = (*pchild)->prev;
Radek Krejci41a349b2016-10-24 19:21:59 +0200912 iter->next = child;
913 child->prev = iter;
914 }
915 while (iter->next) {
916 iter = iter->next;
917 iter->parent = parent;
918 }
Radek Krejcic9d78692017-08-24 17:17:18 +0200919 (*pchild)->prev = iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200920 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200921 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200922
Michal Vaskoe022a562016-09-27 14:24:15 +0200923 /* check config value (but ignore them in groupings and augments) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100924 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); iter = iter->parent);
Michal Vaskoe022a562016-09-27 14:24:15 +0200925 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200926 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
927 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100928 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config");
929 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200930 return EXIT_FAILURE;
931 }
932 }
933
Radek Krejci41771502016-04-14 17:52:32 +0200934 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200935 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200936 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200937 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200938 /* store it only into container or list - the only data inner nodes */
939 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
940 if (iter->flags & LYS_INCL_STATUS) {
941 /* done, someone else set it already from here */
942 break;
943 }
944 /* set flag about including status data */
945 iter->flags |= LYS_INCL_STATUS;
946 }
947 }
948 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200949
950 /* create implicit input/output nodes to have available them as possible target for augment */
Radek Krejci60251232017-08-24 17:13:08 +0200951 if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200952 in = calloc(1, sizeof *in);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200953 out = calloc(1, sizeof *out);
954 if (!in || !out) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100955 LOGMEM(ctx);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200956 free(in);
957 free(out);
958 return EXIT_FAILURE;
959 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200960 in->nodetype = LYS_INPUT;
961 in->name = lydict_insert(child->module->ctx, "input", 5);
Radek Krejci41a349b2016-10-24 19:21:59 +0200962 out->nodetype = LYS_OUTPUT;
Radek Krejcia8d111f2017-05-31 13:57:37 +0200963 out->name = lydict_insert(child->module->ctx, "output", 6);
Radek Krejci41a349b2016-10-24 19:21:59 +0200964 in->module = out->module = child->module;
965 in->parent = out->parent = child;
966 in->flags = out->flags = LYS_IMPLICIT;
967 in->next = (struct lys_node *)out;
968 in->prev = (struct lys_node *)out;
969 out->prev = (struct lys_node *)in;
970 child->child = (struct lys_node *)in;
971 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200972 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200973}
974
Michal Vasko29245662017-04-18 15:56:31 +0200975const struct lys_module *
Michal Vaskofb98dc42018-01-11 13:38:28 +0100976lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const char *revision, int internal, int implement)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200977{
Radek Krejcia1df1682016-04-11 14:56:59 +0200978 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200979 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200980 unsigned int len;
Radek Krejcif347abc2016-06-22 10:18:47 +0200981
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200982 if (!ctx || !data) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100983 LOGARG;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200984 return NULL;
985 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200986
Radek Krejcia1df1682016-04-11 14:56:59 +0200987 if (!internal && format == LYS_IN_YANG) {
988 /* enlarge data by 2 bytes for flex */
989 len = strlen(data);
990 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Michal Vasko53b7da02018-02-13 15:28:42 +0100991 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL);
Radek Krejcia1df1682016-04-11 14:56:59 +0200992 memcpy(enlarged_data, data, len);
993 enlarged_data[len] = enlarged_data[len + 1] = '\0';
994 data = enlarged_data;
995 }
996
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200998 case LYS_IN_YIN:
Michal Vaskofb98dc42018-01-11 13:38:28 +0100999 mod = yin_read_module(ctx, data, revision, implement);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001000 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001001 case LYS_IN_YANG:
Michal Vaskofb98dc42018-01-11 13:38:28 +01001002 mod = yang_read_module(ctx, data, 0, revision, implement);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001003 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001004 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01001005 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001006 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001007 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001008
Radek Krejcia1df1682016-04-11 14:56:59 +02001009 free(enlarged_data);
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001010
1011 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
1012 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
1013 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
1014 * the anotation definitions available in the internal schema structure. There is another hack in schema
1015 * printers to do not print this internally added annotation. */
1016 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci5b190662017-04-13 08:56:14 +02001017 if (lyp_add_ietf_netconf_annotations(mod)) {
Michal Vasko10681e82018-01-16 14:54:16 +01001018 lys_free(mod, NULL, 1, 1);
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001019 return NULL;
1020 }
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001021 }
1022
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001023 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001024}
1025
Radek Krejcia1df1682016-04-11 14:56:59 +02001026API const struct lys_module *
1027lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
1028{
Michal Vaskofb98dc42018-01-11 13:38:28 +01001029 return lys_parse_mem_(ctx, data, format, NULL, 0, 1);
Radek Krejcia1df1682016-04-11 14:56:59 +02001030}
1031
Michal Vasko5a721fd2016-02-16 12:16:48 +01001032struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001033lys_sub_parse_mem(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001034{
Michal Vasko5b998712017-01-26 10:34:06 +01001035 char *enlarged_data = NULL;
Michal Vasko5a721fd2016-02-16 12:16:48 +01001036 struct lys_submodule *submod = NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001037 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001038
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001039 assert(module);
1040 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001041
Michal Vasko5b998712017-01-26 10:34:06 +01001042 if (format == LYS_IN_YANG) {
1043 /* enlarge data by 2 bytes for flex */
1044 len = strlen(data);
1045 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001046 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL);
Michal Vasko5b998712017-01-26 10:34:06 +01001047 memcpy(enlarged_data, data, len);
1048 enlarged_data[len] = enlarged_data[len + 1] = '\0';
1049 data = enlarged_data;
1050 }
1051
Radek Krejcic071c542016-01-27 14:57:51 +01001052 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +02001053 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001054
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001055 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001056 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +01001057 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001058 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001059 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001060 submod = yang_read_submodule(module, data, 0, unres);
1061 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001062 default:
Radek Krejci90a550a2016-04-13 16:00:58 +02001063 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001064 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001065 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001066
Michal Vasko5b998712017-01-26 10:34:06 +01001067 free(enlarged_data);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001068 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001069}
1070
Michal Vasko1e62a092015-12-01 12:27:20 +01001071API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +01001072lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1073{
1074 int fd;
1075 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +02001076 const char *rev, *dot, *filename;
1077 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +01001078
1079 if (!ctx || !path) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001080 LOGARG;
Michal Vasko662610a2015-12-07 11:25:45 +01001081 return NULL;
1082 }
1083
1084 fd = open(path, O_RDONLY);
1085 if (fd == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001086 LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
Michal Vasko662610a2015-12-07 11:25:45 +01001087 return NULL;
1088 }
1089
1090 ret = lys_parse_fd(ctx, fd, format);
1091 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +01001092
Radek Krejcid80c8602016-10-25 11:56:03 +02001093 if (!ret) {
1094 /* error */
1095 return NULL;
1096 }
1097
1098 /* check that name and revision match filename */
1099 filename = strrchr(path, '/');
1100 if (!filename) {
1101 filename = path;
1102 } else {
1103 filename++;
1104 }
1105 rev = strchr(filename, '@');
1106 dot = strrchr(filename, '.');
1107
1108 /* name */
1109 len = strlen(ret->name);
1110 if (strncmp(filename, ret->name, len) ||
1111 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001112 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +02001113 }
1114 if (rev) {
1115 len = dot - ++rev;
1116 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001117 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcid80c8602016-10-25 11:56:03 +02001118 ret->rev_size ? ret->rev[0].date : "none");
1119 }
1120 }
1121
1122 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +01001123 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +01001124 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +01001125 }
1126
Michal Vasko662610a2015-12-07 11:25:45 +01001127 return ret;
1128}
1129
1130API const struct lys_module *
1131lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001132{
Michal Vaskofb98dc42018-01-11 13:38:28 +01001133 return lys_parse_fd_(ctx, fd, format, NULL, 1);
1134}
1135
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001136static void
1137lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
1138{
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001139#ifdef __APPLE__
1140 char path[MAXPATHLEN];
1141#else
Michal Vasko25601f32018-02-16 09:41:46 +01001142 int len;
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001143 char path[PATH_MAX], proc_path[32];
1144#endif
1145
1146#ifdef __APPLE__
1147 if (fcntl(fd, F_GETPATH, path) != -1) {
1148 *filename = lydict_insert(ctx, path, 0);
1149 }
1150#else
1151 /* get URI if there is /proc */
1152 sprintf(proc_path, "/proc/self/fd/%d", fd);
1153 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
1154 *filename = lydict_insert(ctx, path, len);
1155 }
1156#endif
1157}
1158
Michal Vaskofb98dc42018-01-11 13:38:28 +01001159const struct lys_module *
1160lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement)
1161{
Michal Vasko1e62a092015-12-01 12:27:20 +01001162 const struct lys_module *module;
Radek Krejci0fb11502017-01-31 16:45:42 +01001163 size_t length;
Radek Krejci63a91a92015-07-29 13:31:04 +02001164 char *addr;
1165
1166 if (!ctx || fd < 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001167 LOGARG;
Radek Krejci63a91a92015-07-29 13:31:04 +02001168 return NULL;
1169 }
1170
Michal Vasko53b7da02018-02-13 15:28:42 +01001171 if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) {
1172 LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001173 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001174 } else if (!addr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001175 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
Pavol Vicane36ea262015-11-12 11:57:47 +01001176 return NULL;
1177 }
Radek Krejci0fb11502017-01-31 16:45:42 +01001178
Michal Vaskofb98dc42018-01-11 13:38:28 +01001179 module = lys_parse_mem_(ctx, addr, format, revision, 1, implement);
Radek Krejci0fb11502017-01-31 16:45:42 +01001180 lyp_munmap(addr, length);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001181
Radek Krejcia77904e2016-02-25 16:23:45 +01001182 if (module && !module->filepath) {
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001183 lys_parse_set_filename(ctx, (const char **)&module->filepath, fd);
Radek Krejcib051f722016-02-25 15:12:21 +01001184 }
1185
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001186 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001187}
1188
Michal Vasko5a721fd2016-02-16 12:16:48 +01001189struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001190lys_sub_parse_fd(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001191{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001192 struct lys_submodule *submodule;
Radek Krejci0fb11502017-01-31 16:45:42 +01001193 size_t length;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001194 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001195
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001196 assert(module);
1197 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001198
Michal Vasko53b7da02018-02-13 15:28:42 +01001199 if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) {
1200 LOGERR(module->ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001201 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001202 } else if (!addr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001203 LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file.");
Michal Vasko2e7241e2016-02-15 16:06:34 +01001204 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001205 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001206
Michal Vasko5b998712017-01-26 10:34:06 +01001207 /* get the main module */
1208 module = lys_main_module(module);
1209
1210 switch (format) {
1211 case LYS_IN_YIN:
1212 submodule = yin_read_submodule(module, addr, unres);
1213 break;
1214 case LYS_IN_YANG:
1215 submodule = yang_read_submodule(module, addr, 0, unres);
1216 break;
1217 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01001218 LOGINT(module->ctx);
Michal Vasko85d41522017-02-24 09:49:16 +01001219 return NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001220 }
1221
Radek Krejcic645a3a2017-01-31 16:59:00 +01001222 lyp_munmap(addr, length);
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001223
1224 if (submodule && !submodule->filepath) {
1225 lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd);
1226 }
1227
Michal Vasko5a721fd2016-02-16 12:16:48 +01001228 return submodule;
1229
Radek Krejciefaeba32015-05-27 14:30:57 +02001230}
1231
Radek Krejcibf285832017-01-26 16:05:41 +01001232int
1233lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt)
1234{
1235 unsigned int u;
1236
1237 for (u = start; u < ext_size; u++) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001238 if (ext[u]->insubstmt == substmt) {
Radek Krejcibf285832017-01-26 16:05:41 +01001239 return u;
1240 }
1241 }
1242
1243 return -1;
1244}
1245
Radek Krejcifdc0d702017-01-23 15:58:38 +01001246/*
1247 * duplicate extension instance
1248 */
1249int
Michal Vasko17e8ba32018-02-15 10:58:56 +01001250lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent,
1251 LYEXT_PAR parent_type, struct lys_ext_instance ***new, int shallow, struct unres_schema *unres)
Radek Krejcifdc0d702017-01-23 15:58:38 +01001252{
1253 int i;
1254 uint8_t u = 0;
1255 struct lys_ext_instance **result;
1256 struct unres_ext *info, *info_orig;
fanchanghu8d86f6b2017-06-10 12:49:54 +08001257 size_t len;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001258
1259 assert(new);
1260
1261 if (!size) {
1262 if (orig) {
Michal Vasko17e8ba32018-02-15 10:58:56 +01001263 LOGINT(ctx);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001264 return EXIT_FAILURE;
1265 }
1266 (*new) = NULL;
1267 return EXIT_SUCCESS;
1268 }
1269
1270 (*new) = result = calloc(size, sizeof *result);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001271 LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001272 for (u = 0; u < size; u++) {
1273 if (orig[u]) {
1274 /* resolved extension instance, just duplicate it */
Radek Krejci8de8f612017-02-16 15:03:32 +01001275 switch(orig[u]->ext_type) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001276 case LYEXT_FLAG:
1277 result[u] = malloc(sizeof(struct lys_ext_instance));
Michal Vasko17e8ba32018-02-15 10:58:56 +01001278 LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001279 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001280 case LYEXT_COMPLEX:
fanchanghu8d86f6b2017-06-10 12:49:54 +08001281 len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size;
1282 result[u] = calloc(1, len);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001283 LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001284
Radek Krejcifebdad72017-02-06 11:35:51 +01001285 ((struct lys_ext_instance_complex*)result[u])->substmt = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001286 /* TODO duplicate data in extension instance content */
fanchanghu8d86f6b2017-06-10 12:49:54 +08001287 memcpy((void*)result[u] + sizeof(**orig), (void*)orig[u] + sizeof(**orig), len - sizeof(**orig));
Radek Krejci8d6b7422017-02-03 14:42:13 +01001288 break;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001289 }
1290 /* generic part */
1291 result[u]->def = orig[u]->def;
fanchanghu8d86f6b2017-06-10 12:49:54 +08001292 result[u]->flags = LYEXT_OPT_CONTENT;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001293 result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001294 result[u]->parent = parent;
1295 result[u]->parent_type = parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01001296 result[u]->insubstmt = orig[u]->insubstmt;
1297 result[u]->insubstmt_index = orig[u]->insubstmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01001298 result[u]->ext_type = orig[u]->ext_type;
Radek Krejci7f1d47e2017-04-12 15:29:02 +02001299 result[u]->priv = NULL;
1300 result[u]->nodetype = LYS_EXT;
1301 result[u]->module = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001302
1303 /* extensions */
Radek Krejcifdc0d702017-01-23 15:58:38 +01001304 result[u]->ext_size = orig[u]->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001305 if (lys_ext_dup(ctx, mod, orig[u]->ext, orig[u]->ext_size, result[u],
Radek Krejci5138e9f2017-04-12 13:10:46 +02001306 LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001307 goto error;
1308 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001309
1310 /* in case of shallow copy (duplication for deviation), duplicate only the link to private data
1311 * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */
1312 if (shallow) {
1313 result[u]->priv = orig[u]->priv;
1314 }
Radek Krejcifdc0d702017-01-23 15:58:38 +01001315 } else {
1316 /* original extension is not yet resolved, so duplicate it in unres */
1317 i = unres_schema_find(unres, -1, &orig, UNRES_EXT);
1318 if (i == -1) {
1319 /* extension not found in unres */
Michal Vasko17e8ba32018-02-15 10:58:56 +01001320 LOGINT(ctx);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001321 goto error;
1322 }
1323 info_orig = unres->str_snode[i];
1324 info = malloc(sizeof *info);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001325 LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001326 info->datatype = info_orig->datatype;
1327 if (info->datatype == LYS_IN_YIN) {
Michal Vasko17e8ba32018-02-15 10:58:56 +01001328 info->data.yin = lyxml_dup_elem(ctx, info_orig->data.yin, NULL, 1);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001329 } /* else TODO YANG */
1330 info->parent = parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001331 info->mod = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001332 info->parent_type = parent_type;
1333 info->ext_index = u;
1334 if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) {
1335 goto error;
1336 }
1337 }
1338 }
1339
1340 return EXIT_SUCCESS;
1341
1342error:
1343 (*new) = NULL;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001344 lys_extension_instances_free(ctx, result, u, NULL);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001345 return EXIT_FAILURE;
1346}
1347
Radek Krejci1d82ef62015-08-07 14:44:40 +02001348static struct lys_restr *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001349lys_restr_dup(struct lys_module *mod, struct lys_restr *old, int size, int shallow, struct unres_schema *unres)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001350{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001351 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001352 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001353
Radek Krejci3733a802015-06-19 13:43:21 +02001354 if (!size) {
1355 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001356 }
Radek Krejci3733a802015-06-19 13:43:21 +02001357
1358 result = calloc(size, sizeof *result);
Michal Vasko53b7da02018-02-13 15:28:42 +01001359 LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001360
Radek Krejci3733a802015-06-19 13:43:21 +02001361 for (i = 0; i < size; i++) {
Radek Krejci77f22b22017-01-17 15:23:03 +01001362 result[i].ext_size = old[i].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001363 lys_ext_dup(mod->ctx, mod, old[i].ext, old[i].ext_size, &result[i], LYEXT_PAR_RESTR, &result[i].ext, shallow, unres);
Radek Krejci8d6b7422017-02-03 14:42:13 +01001364 result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0);
1365 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1366 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1367 result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0);
1368 result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001369 }
1370
1371 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001372}
1373
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001374void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001375lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr,
1376 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci0bd5db42015-06-19 13:30:07 +02001377{
1378 assert(ctx);
1379 if (!restr) {
1380 return;
1381 }
1382
Radek Krejci5138e9f2017-04-12 13:10:46 +02001383 lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001384 lydict_remove(ctx, restr->expr);
1385 lydict_remove(ctx, restr->dsc);
1386 lydict_remove(ctx, restr->ref);
1387 lydict_remove(ctx, restr->eapptag);
1388 lydict_remove(ctx, restr->emsg);
1389}
1390
Pavol Vican05810b62016-11-23 14:07:22 +01001391void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001392lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size,
Frank Rimplerc4db1c72017-09-12 12:56:39 +00001393 int shallow, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001394{
1395 uint8_t i;
1396
1397 for (i = 0; i < iffeature_size; ++i) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001398 lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor);
Michal Vasko15a43372017-09-25 14:12:42 +02001399 if (!shallow) {
Frank Rimpler2a503f52017-09-12 15:21:18 +00001400 free(iffeature[i].expr);
1401 free(iffeature[i].features);
1402 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001403 }
1404 free(iffeature);
1405}
1406
Michal Vaskob84f88a2015-09-24 13:16:10 +02001407static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001408type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001409 LY_DATA_TYPE base, int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001410{
1411 int i;
Radek Krejcidce5f972017-09-12 15:47:49 +02001412 unsigned int u;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001413
1414 switch (base) {
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001415 case LY_TYPE_BINARY:
1416 if (old->info.binary.length) {
1417 new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres);
1418 }
1419 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001420
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001421 case LY_TYPE_BITS:
1422 new->info.bits.count = old->info.bits.count;
1423 if (new->info.bits.count) {
1424 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
Michal Vasko53b7da02018-02-13 15:28:42 +01001425 LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001426
Radek Krejcidce5f972017-09-12 15:47:49 +02001427 for (u = 0; u < new->info.bits.count; u++) {
1428 new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0);
1429 new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0);
1430 new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0);
1431 new->info.bits.bit[u].flags = old->info.bits.bit[u].flags;
1432 new->info.bits.bit[u].pos = old->info.bits.bit[u].pos;
1433 new->info.bits.bit[u].ext_size = old->info.bits.bit[u].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001434 if (lys_ext_dup(mod->ctx, mod, old->info.bits.bit[u].ext, old->info.bits.bit[u].ext_size,
Radek Krejcidce5f972017-09-12 15:47:49 +02001435 &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT,
1436 &new->info.bits.bit[u].ext, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001437 return -1;
1438 }
1439 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001440 }
1441 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001442
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001443 case LY_TYPE_DEC64:
1444 new->info.dec64.dig = old->info.dec64.dig;
1445 new->info.dec64.div = old->info.dec64.div;
1446 if (old->info.dec64.range) {
1447 new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres);
1448 }
1449 break;
1450
1451 case LY_TYPE_ENUM:
1452 new->info.enums.count = old->info.enums.count;
1453 if (new->info.enums.count) {
1454 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
Michal Vasko53b7da02018-02-13 15:28:42 +01001455 LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001456
Radek Krejcidce5f972017-09-12 15:47:49 +02001457 for (u = 0; u < new->info.enums.count; u++) {
1458 new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0);
1459 new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0);
1460 new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0);
1461 new->info.enums.enm[u].flags = old->info.enums.enm[u].flags;
1462 new->info.enums.enm[u].value = old->info.enums.enm[u].value;
1463 new->info.enums.enm[u].ext_size = old->info.enums.enm[u].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001464 if (lys_ext_dup(mod->ctx, mod, old->info.enums.enm[u].ext, old->info.enums.enm[u].ext_size,
Radek Krejcidce5f972017-09-12 15:47:49 +02001465 &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM,
1466 &new->info.enums.enm[u].ext, shallow, unres)) {
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001467 return -1;
1468 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001469 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001470 }
1471 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001472
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001473 case LY_TYPE_IDENT:
1474 new->info.ident.count = old->info.ident.count;
1475 if (old->info.ident.count) {
1476 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
Michal Vasko53b7da02018-02-13 15:28:42 +01001477 LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001478 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1479 } else {
1480 /* there can be several unresolved base identities, duplicate them all */
1481 i = -1;
1482 do {
1483 i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF);
1484 if (i != -1) {
1485 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001486 return -1;
1487 }
1488 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001489 --i;
1490 } while (i > -1);
1491 }
1492 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001493
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001494 case LY_TYPE_INST:
1495 new->info.inst.req = old->info.inst.req;
1496 break;
1497
1498 case LY_TYPE_INT8:
1499 case LY_TYPE_INT16:
1500 case LY_TYPE_INT32:
1501 case LY_TYPE_INT64:
1502 case LY_TYPE_UINT8:
1503 case LY_TYPE_UINT16:
1504 case LY_TYPE_UINT32:
1505 case LY_TYPE_UINT64:
1506 if (old->info.num.range) {
1507 new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres);
1508 }
1509 break;
1510
1511 case LY_TYPE_LEAFREF:
1512 if (old->info.lref.path) {
1513 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
1514 if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
1515 return -1;
1516 }
1517 }
1518 break;
1519
1520 case LY_TYPE_STRING:
1521 if (old->info.str.length) {
1522 new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres);
1523 }
Radek Krejcib53154b2017-07-19 09:14:13 +02001524 if (old->info.str.pat_count) {
1525 new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres);
1526 new->info.str.pat_count = old->info.str.pat_count;
Michal Vaskofcd974b2017-08-22 10:17:49 +02001527#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02001528 if (!in_grp) {
1529 new->info.str.patterns_pcre = malloc(new->info.str.pat_count * 2 * sizeof *new->info.str.patterns_pcre);
Michal Vasko53b7da02018-02-13 15:28:42 +01001530 LY_CHECK_ERR_RETURN(!new->info.str.patterns_pcre, LOGMEM(mod->ctx), -1);
Radek Krejcia4c107d2017-10-27 14:19:26 +02001531 for (u = 0; u < new->info.str.pat_count; u++) {
Michal Vaskoa26db302018-02-14 15:22:10 +01001532 if (lyp_precompile_pattern(mod->ctx, &new->info.str.patterns[u].expr[1],
Radek Krejcia4c107d2017-10-27 14:19:26 +02001533 (pcre**)&new->info.str.patterns_pcre[2 * u],
1534 (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) {
Radek Krejcib53154b2017-07-19 09:14:13 +02001535 free(new->info.str.patterns_pcre);
1536 new->info.str.patterns_pcre = NULL;
1537 return -1;
1538 }
1539 }
1540 }
Michal Vaskofcd974b2017-08-22 10:17:49 +02001541#endif
Radek Krejcib53154b2017-07-19 09:14:13 +02001542 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001543 break;
1544
1545 case LY_TYPE_UNION:
1546 new->info.uni.has_ptr_type = old->info.uni.has_ptr_type;
1547 new->info.uni.count = old->info.uni.count;
1548 if (new->info.uni.count) {
1549 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
Michal Vasko53b7da02018-02-13 15:28:42 +01001550 LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001551
Radek Krejcidce5f972017-09-12 15:47:49 +02001552 for (u = 0; u < new->info.uni.count; u++) {
1553 if (lys_type_dup(mod, parent, &(new->info.uni.types[u]), &(old->info.uni.types[u]), in_grp,
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001554 shallow, unres)) {
1555 return -1;
1556 }
1557 }
1558 }
1559 break;
1560
1561 default:
1562 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1563 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001564 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001565
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001566 return EXIT_SUCCESS;
1567}
1568
1569struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001570lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001571 int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001572{
1573 struct yang_type *new;
1574
1575 new = calloc(1, sizeof *new);
Michal Vasko53b7da02018-02-13 15:28:42 +01001576 LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001577 new->flags = old->flags;
1578 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001579 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001580 new->type = type;
1581 if (!new->name) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001582 LOGMEM(module->ctx);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001583 goto error;
1584 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001585 if (type_dup(module, parent, type, old->type, new->base, in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001586 new->type->base = new->base;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001587 lys_type_free(module->ctx, new->type, NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001588 memset(&new->type->info, 0, sizeof new->type->info);
1589 goto error;
1590 }
1591 return new;
1592
Michal Vasko53b7da02018-02-13 15:28:42 +01001593error:
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001594 free(new);
1595 return NULL;
1596}
1597
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001598int
1599lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new,
1600 struct unres_schema *unres)
1601{
1602 struct lys_type new;
Radek Krejcidce5f972017-09-12 15:47:49 +02001603 unsigned int i, top_type;
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001604 struct lys_ext_instance **ext;
1605 uint8_t ext_size;
1606 void *reloc;
1607
1608 if (!prev_new) {
1609 /* this is the "top-level" type, meaning it is a real type and no typedef directly above */
1610 top_type = 1;
1611
1612 memset(&new, 0, sizeof new);
1613
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001614 new.base = type->base;
1615 new.parent = (struct lys_tpdf *)parent;
1616
1617 prev_new = &new;
1618 } else {
1619 /* this is not top-level type, just a type of a typedef */
1620 top_type = 0;
1621 }
1622
Radek Krejci9c5cb6d2017-08-09 11:15:23 +02001623 assert(type->der);
1624 if (type->der->module) {
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001625 /* typedef, skip it, but keep the extensions */
1626 ext_size = type->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001627 if (lys_ext_dup(mod->ctx, mod, type->ext, type->ext_size, prev_new, LYEXT_PAR_TYPE, &ext, 0, unres)) {
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001628 return -1;
1629 }
1630 if (prev_new->ext) {
1631 reloc = realloc(prev_new->ext, (prev_new->ext_size + ext_size) * sizeof *prev_new->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01001632 LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1);
Radek Krejci70379e22017-08-09 11:21:07 +02001633 prev_new->ext = reloc;
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001634
1635 memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext);
1636 free(ext);
1637
1638 prev_new->ext_size += ext_size;
1639 } else {
1640 prev_new->ext = ext;
1641 prev_new->ext_size = ext_size;
1642 }
1643
1644 if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) {
1645 return -1;
1646 }
1647 } else {
1648 /* type, just make a deep copy */
1649 switch (type->base) {
1650 case LY_TYPE_UNION:
1651 prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type;
1652 prev_new->info.uni.count = type->info.uni.count;
1653 /* this cannot be a typedef anymore */
1654 assert(prev_new->info.uni.count);
1655
1656 prev_new->info.uni.types = calloc(prev_new->info.uni.count, sizeof *prev_new->info.uni.types);
Michal Vasko53b7da02018-02-13 15:28:42 +01001657 LY_CHECK_ERR_RETURN(!prev_new->info.uni.types, LOGMEM(mod->ctx), -1);
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001658
1659 for (i = 0; i < prev_new->info.uni.count; i++) {
1660 if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) {
1661 return -1;
1662 }
1663 }
1664
1665 prev_new->der = type->der;
1666 break;
1667 default:
1668 if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) {
1669 return -1;
1670 }
1671 break;
1672 }
1673 }
1674
1675 if (top_type) {
1676 memcpy(type, prev_new, sizeof *type);
1677 }
1678 return EXIT_SUCCESS;
1679}
1680
Radek Krejci43ce4b72017-01-04 11:02:38 +01001681API const void *
1682lys_ext_instance_substmt(const struct lys_ext_instance *ext)
1683{
1684 if (!ext) {
1685 return NULL;
1686 }
1687
Radek Krejcifebdad72017-02-06 11:35:51 +01001688 switch (ext->insubstmt) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001689 case LYEXT_SUBSTMT_SELF:
1690 case LYEXT_SUBSTMT_MODIFIER:
1691 case LYEXT_SUBSTMT_VERSION:
1692 return NULL;
1693 case LYEXT_SUBSTMT_ARGUMENT:
1694 if (ext->parent_type == LYEXT_PAR_EXT) {
1695 return ((struct lys_ext_instance*)ext->parent)->arg_value;
1696 }
1697 break;
1698 case LYEXT_SUBSTMT_BASE:
1699 if (ext->parent_type == LYEXT_PAR_TYPE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001700 return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001701 } else if (ext->parent_type == LYEXT_PAR_IDENT) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001702 return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001703 }
1704 break;
1705 case LYEXT_SUBSTMT_BELONGSTO:
1706 if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) {
1707 return ((struct lys_submodule*)ext->parent)->belongsto;
1708 }
1709 break;
1710 case LYEXT_SUBSTMT_CONFIG:
1711 case LYEXT_SUBSTMT_MANDATORY:
1712 if (ext->parent_type == LYEXT_PAR_NODE) {
1713 return &((struct lys_node*)ext->parent)->flags;
1714 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1715 return &((struct lys_deviate*)ext->parent)->flags;
1716 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1717 return &((struct lys_refine*)ext->parent)->flags;
1718 }
1719 break;
1720 case LYEXT_SUBSTMT_CONTACT:
1721 if (ext->parent_type == LYEXT_PAR_MODULE) {
1722 return ((struct lys_module*)ext->parent)->contact;
1723 }
1724 break;
1725 case LYEXT_SUBSTMT_DEFAULT:
1726 if (ext->parent_type == LYEXT_PAR_NODE) {
1727 switch (((struct lys_node*)ext->parent)->nodetype) {
1728 case LYS_LEAF:
1729 case LYS_LEAFLIST:
1730 /* in case of leaf, the index is supposed to be 0, so it will return the
1731 * correct pointer despite the leaf structure does not have dflt as array */
Radek Krejcifebdad72017-02-06 11:35:51 +01001732 return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001733 case LYS_CHOICE:
1734 return ((struct lys_node_choice*)ext->parent)->dflt;
1735 default:
1736 /* internal error */
1737 break;
1738 }
1739 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1740 return ((struct lys_tpdf*)ext->parent)->dflt;
1741 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001742 return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001743 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001744 return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001745 }
1746 break;
1747 case LYEXT_SUBSTMT_DESCRIPTION:
1748 switch (ext->parent_type) {
1749 case LYEXT_PAR_NODE:
1750 return ((struct lys_node*)ext->parent)->dsc;
1751 case LYEXT_PAR_MODULE:
1752 return ((struct lys_module*)ext->parent)->dsc;
1753 case LYEXT_PAR_IMPORT:
1754 return ((struct lys_import*)ext->parent)->dsc;
1755 case LYEXT_PAR_INCLUDE:
1756 return ((struct lys_include*)ext->parent)->dsc;
1757 case LYEXT_PAR_EXT:
1758 return ((struct lys_ext*)ext->parent)->dsc;
1759 case LYEXT_PAR_FEATURE:
1760 return ((struct lys_feature*)ext->parent)->dsc;
1761 case LYEXT_PAR_TPDF:
1762 return ((struct lys_tpdf*)ext->parent)->dsc;
1763 case LYEXT_PAR_TYPE_BIT:
1764 return ((struct lys_type_bit*)ext->parent)->dsc;
1765 case LYEXT_PAR_TYPE_ENUM:
1766 return ((struct lys_type_enum*)ext->parent)->dsc;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001767 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001768 return ((struct lys_restr*)ext->parent)->dsc;
1769 case LYEXT_PAR_WHEN:
1770 return ((struct lys_when*)ext->parent)->dsc;
1771 case LYEXT_PAR_IDENT:
1772 return ((struct lys_ident*)ext->parent)->dsc;
1773 case LYEXT_PAR_DEVIATION:
1774 return ((struct lys_deviation*)ext->parent)->dsc;
1775 case LYEXT_PAR_REVISION:
1776 return ((struct lys_revision*)ext->parent)->dsc;
1777 case LYEXT_PAR_REFINE:
1778 return ((struct lys_refine*)ext->parent)->dsc;
1779 default:
1780 break;
1781 }
1782 break;
1783 case LYEXT_SUBSTMT_ERRTAG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001784 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001785 return ((struct lys_restr*)ext->parent)->eapptag;
1786 }
1787 break;
1788 case LYEXT_SUBSTMT_ERRMSG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001789 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001790 return ((struct lys_restr*)ext->parent)->emsg;
1791 }
1792 break;
1793 case LYEXT_SUBSTMT_DIGITS:
1794 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) {
1795 return &((struct lys_type*)ext->parent)->info.dec64.dig;
1796 }
1797 break;
1798 case LYEXT_SUBSTMT_KEY:
1799 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1800 return ((struct lys_node_list*)ext->parent)->keys;
1801 }
1802 break;
1803 case LYEXT_SUBSTMT_MAX:
1804 if (ext->parent_type == LYEXT_PAR_NODE) {
1805 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1806 return &((struct lys_node_list*)ext->parent)->max;
1807 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1808 return &((struct lys_node_leaflist*)ext->parent)->max;
1809 }
1810 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1811 return &((struct lys_refine*)ext->parent)->mod.list.max;
1812 }
1813 break;
1814 case LYEXT_SUBSTMT_MIN:
1815 if (ext->parent_type == LYEXT_PAR_NODE) {
1816 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1817 return &((struct lys_node_list*)ext->parent)->min;
1818 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1819 return &((struct lys_node_leaflist*)ext->parent)->min;
1820 }
1821 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1822 return &((struct lys_refine*)ext->parent)->mod.list.min;
1823 }
1824 break;
1825 case LYEXT_SUBSTMT_NAMESPACE:
1826 if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) {
1827 return ((struct lys_module*)ext->parent)->ns;
1828 }
1829 break;
1830 case LYEXT_SUBSTMT_ORDEREDBY:
1831 if (ext->parent_type == LYEXT_PAR_NODE &&
1832 (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1833 return &((struct lys_node_list*)ext->parent)->flags;
1834 }
1835 break;
1836 case LYEXT_SUBSTMT_ORGANIZATION:
1837 if (ext->parent_type == LYEXT_PAR_MODULE) {
1838 return ((struct lys_module*)ext->parent)->org;
1839 }
1840 break;
1841 case LYEXT_SUBSTMT_PATH:
1842 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1843 return ((struct lys_type*)ext->parent)->info.lref.path;
1844 }
1845 break;
1846 case LYEXT_SUBSTMT_POSITION:
1847 if (ext->parent_type == LYEXT_PAR_TYPE_BIT) {
1848 return &((struct lys_type_bit*)ext->parent)->pos;
1849 }
1850 break;
1851 case LYEXT_SUBSTMT_PREFIX:
1852 if (ext->parent_type == LYEXT_PAR_MODULE) {
1853 /* covers also lys_submodule */
1854 return ((struct lys_module*)ext->parent)->prefix;
1855 } else if (ext->parent_type == LYEXT_PAR_IMPORT) {
1856 return ((struct lys_import*)ext->parent)->prefix;
1857 }
1858 break;
1859 case LYEXT_SUBSTMT_PRESENCE:
1860 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) {
1861 return ((struct lys_node_container*)ext->parent)->presence;
1862 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1863 return ((struct lys_refine*)ext->parent)->mod.presence;
1864 }
1865 break;
1866 case LYEXT_SUBSTMT_REFERENCE:
1867 switch (ext->parent_type) {
1868 case LYEXT_PAR_NODE:
1869 return ((struct lys_node*)ext->parent)->ref;
1870 case LYEXT_PAR_MODULE:
1871 return ((struct lys_module*)ext->parent)->ref;
1872 case LYEXT_PAR_IMPORT:
1873 return ((struct lys_import*)ext->parent)->ref;
1874 case LYEXT_PAR_INCLUDE:
1875 return ((struct lys_include*)ext->parent)->ref;
1876 case LYEXT_PAR_EXT:
1877 return ((struct lys_ext*)ext->parent)->ref;
1878 case LYEXT_PAR_FEATURE:
1879 return ((struct lys_feature*)ext->parent)->ref;
1880 case LYEXT_PAR_TPDF:
1881 return ((struct lys_tpdf*)ext->parent)->ref;
1882 case LYEXT_PAR_TYPE_BIT:
1883 return ((struct lys_type_bit*)ext->parent)->ref;
1884 case LYEXT_PAR_TYPE_ENUM:
1885 return ((struct lys_type_enum*)ext->parent)->ref;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001886 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001887 return ((struct lys_restr*)ext->parent)->ref;
1888 case LYEXT_PAR_WHEN:
1889 return ((struct lys_when*)ext->parent)->ref;
1890 case LYEXT_PAR_IDENT:
1891 return ((struct lys_ident*)ext->parent)->ref;
1892 case LYEXT_PAR_DEVIATION:
1893 return ((struct lys_deviation*)ext->parent)->ref;
1894 case LYEXT_PAR_REVISION:
1895 return ((struct lys_revision*)ext->parent)->ref;
1896 case LYEXT_PAR_REFINE:
1897 return ((struct lys_refine*)ext->parent)->ref;
1898 default:
1899 break;
1900 }
1901 break;
Radek Krejcibe336392017-02-07 10:54:24 +01001902 case LYEXT_SUBSTMT_REQINSTANCE:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001903 if (ext->parent_type == LYEXT_PAR_TYPE) {
1904 if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1905 return &((struct lys_type*)ext->parent)->info.lref.req;
1906 } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) {
1907 return &((struct lys_type*)ext->parent)->info.inst.req;
1908 }
1909 }
1910 break;
1911 case LYEXT_SUBSTMT_REVISIONDATE:
1912 if (ext->parent_type == LYEXT_PAR_IMPORT) {
1913 return ((struct lys_import*)ext->parent)->rev;
1914 } else if (ext->parent_type == LYEXT_PAR_INCLUDE) {
1915 return ((struct lys_include*)ext->parent)->rev;
1916 }
1917 break;
1918 case LYEXT_SUBSTMT_STATUS:
1919 switch (ext->parent_type) {
1920 case LYEXT_PAR_NODE:
1921 case LYEXT_PAR_IDENT:
1922 case LYEXT_PAR_TPDF:
1923 case LYEXT_PAR_EXT:
1924 case LYEXT_PAR_FEATURE:
1925 case LYEXT_PAR_TYPE_ENUM:
1926 case LYEXT_PAR_TYPE_BIT:
1927 /* in all structures the flags member is at the same offset */
1928 return &((struct lys_node*)ext->parent)->flags;
1929 default:
1930 break;
1931 }
1932 break;
1933 case LYEXT_SUBSTMT_UNIQUE:
1934 if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001935 return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001936 } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001937 return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001938 }
1939 break;
1940 case LYEXT_SUBSTMT_UNITS:
1941 if (ext->parent_type == LYEXT_PAR_NODE &&
1942 (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1943 /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */
1944 return ((struct lys_node_leaf*)ext->parent)->units;
1945 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1946 return ((struct lys_tpdf*)ext->parent)->units;
1947 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1948 return ((struct lys_deviate*)ext->parent)->units;
1949 }
1950 break;
1951 case LYEXT_SUBSTMT_VALUE:
1952 if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) {
1953 return &((struct lys_type_enum*)ext->parent)->value;
1954 }
1955 break;
1956 case LYEXT_SUBSTMT_YINELEM:
1957 if (ext->parent_type == LYEXT_PAR_EXT) {
1958 return &((struct lys_ext*)ext->parent)->flags;
1959 }
1960 break;
1961 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001962 LOGINT(ext->module->ctx);
Radek Krejci43ce4b72017-01-04 11:02:38 +01001963 return NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01001964}
1965
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001966static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001967lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001968 int in_grp, int shallow, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001969{
1970 int i;
1971
Radek Krejci3733a802015-06-19 13:43:21 +02001972 new->base = old->base;
1973 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001974 new->parent = (struct lys_tpdf *)parent;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001975 new->ext_size = old->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001976 if (lys_ext_dup(mod->ctx, mod, old->ext, old->ext_size, new, LYEXT_PAR_TYPE, &new->ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001977 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01001978 }
Radek Krejci3733a802015-06-19 13:43:21 +02001979
Michal Vasko1c007172017-03-10 10:20:44 +01001980 i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001981 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001982 /* HACK (serious one) for unres */
1983 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001984 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001985 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp,
1986 shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001987 } else {
1988 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1989 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001990 /* all these unres additions can fail even though they did not before */
Michal Vasko1c007172017-03-10 10:20:44 +01001991 if (!new->der || (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent) == -1)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001992 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001993 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001994 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001995 }
1996
Radek Krejci5138e9f2017-04-12 13:10:46 +02001997 return type_dup(mod, parent, new, old, new->base, in_grp, shallow, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001998}
1999
2000void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002001lys_type_free(struct ly_ctx *ctx, struct lys_type *type,
2002 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002003{
Radek Krejcidce5f972017-09-12 15:47:49 +02002004 unsigned int i;
Radek Krejci5a065542015-05-22 15:02:07 +02002005
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002006 assert(ctx);
2007 if (!type) {
2008 return;
2009 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002010
Radek Krejci5138e9f2017-04-12 13:10:46 +02002011 lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002012
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002013 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02002014 case LY_TYPE_BINARY:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002015 lys_restr_free(ctx, type->info.binary.length, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02002016 free(type->info.binary.length);
2017 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02002018 case LY_TYPE_BITS:
2019 for (i = 0; i < type->info.bits.count; i++) {
2020 lydict_remove(ctx, type->info.bits.bit[i].name);
2021 lydict_remove(ctx, type->info.bits.bit[i].dsc);
2022 lydict_remove(ctx, type->info.bits.bit[i].ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002023 lys_iffeature_free(ctx, type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size, 0,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002024 private_destructor);
2025 lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size,
2026 private_destructor);
Radek Krejci994b6f62015-06-18 16:47:27 +02002027 }
2028 free(type->info.bits.bit);
2029 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02002030
2031 case LY_TYPE_DEC64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002032 lys_restr_free(ctx, type->info.dec64.range, private_destructor);
Radek Krejcif9401c32015-06-26 16:47:36 +02002033 free(type->info.dec64.range);
2034 break;
2035
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002036 case LY_TYPE_ENUM:
2037 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002038 lydict_remove(ctx, type->info.enums.enm[i].name);
2039 lydict_remove(ctx, type->info.enums.enm[i].dsc);
2040 lydict_remove(ctx, type->info.enums.enm[i].ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002041 lys_iffeature_free(ctx, type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size, 0,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002042 private_destructor);
2043 lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size,
2044 private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002045 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02002046 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002047 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02002048
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02002049 case LY_TYPE_INT8:
2050 case LY_TYPE_INT16:
2051 case LY_TYPE_INT32:
2052 case LY_TYPE_INT64:
2053 case LY_TYPE_UINT8:
2054 case LY_TYPE_UINT16:
2055 case LY_TYPE_UINT32:
2056 case LY_TYPE_UINT64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002057 lys_restr_free(ctx, type->info.num.range, private_destructor);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02002058 free(type->info.num.range);
2059 break;
2060
Radek Krejcidc4c1412015-06-19 15:39:54 +02002061 case LY_TYPE_LEAFREF:
2062 lydict_remove(ctx, type->info.lref.path);
2063 break;
2064
Radek Krejci3733a802015-06-19 13:43:21 +02002065 case LY_TYPE_STRING:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002066 lys_restr_free(ctx, type->info.str.length, private_destructor);
Radek Krejci3733a802015-06-19 13:43:21 +02002067 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02002068 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002069 lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002070#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02002071 if (type->info.str.patterns_pcre) {
2072 pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]);
2073 pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]);
2074 }
Michal Vaskofcd974b2017-08-22 10:17:49 +02002075#endif
Radek Krejci5fbc9162015-06-19 14:11:11 +02002076 }
2077 free(type->info.str.patterns);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002078#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02002079 free(type->info.str.patterns_pcre);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002080#endif
Radek Krejci3733a802015-06-19 13:43:21 +02002081 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02002082
Radek Krejcie4c366b2015-07-02 10:11:31 +02002083 case LY_TYPE_UNION:
2084 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002085 lys_type_free(ctx, &type->info.uni.types[i], private_destructor);
Radek Krejcie4c366b2015-07-02 10:11:31 +02002086 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02002087 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02002088 break;
2089
Michal Vaskod3282192016-09-05 11:27:57 +02002090 case LY_TYPE_IDENT:
2091 free(type->info.ident.ref);
2092 break;
2093
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002094 default:
Michal Vaskod3282192016-09-05 11:27:57 +02002095 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002096 break;
2097 }
Radek Krejci5a065542015-05-22 15:02:07 +02002098}
2099
Radek Krejci1d82ef62015-08-07 14:44:40 +02002100static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002101lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf,
2102 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002103{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002104 assert(ctx);
2105 if (!tpdf) {
2106 return;
2107 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002108
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002109 lydict_remove(ctx, tpdf->name);
2110 lydict_remove(ctx, tpdf->dsc);
2111 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002112
Radek Krejci5138e9f2017-04-12 13:10:46 +02002113 lys_type_free(ctx, &tpdf->type, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002114
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002115 lydict_remove(ctx, tpdf->units);
2116 lydict_remove(ctx, tpdf->dflt);
Radek Krejcie534c132016-11-23 13:32:31 +01002117
Radek Krejci5138e9f2017-04-12 13:10:46 +02002118 lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002119}
2120
Radek Krejci1d82ef62015-08-07 14:44:40 +02002121static struct lys_when *
Radek Krejci5138e9f2017-04-12 13:10:46 +02002122lys_when_dup(struct lys_module *mod, struct lys_when *old, int shallow, struct unres_schema *unres)
Radek Krejci00768f42015-06-18 17:04:04 +02002123{
Radek Krejci76512572015-08-04 09:47:08 +02002124 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02002125
2126 if (!old) {
2127 return NULL;
2128 }
2129
2130 new = calloc(1, sizeof *new);
Michal Vasko53b7da02018-02-13 15:28:42 +01002131 LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL);
Radek Krejci8d6b7422017-02-03 14:42:13 +01002132 new->cond = lydict_insert(mod->ctx, old->cond, 0);
2133 new->dsc = lydict_insert(mod->ctx, old->dsc, 0);
2134 new->ref = lydict_insert(mod->ctx, old->ref, 0);
Radek Krejcif0bb3602017-01-25 17:05:08 +01002135 new->ext_size = old->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002136 lys_ext_dup(mod->ctx, mod, old->ext, old->ext_size, new, LYEXT_PAR_WHEN, &new->ext, shallow, unres);
Radek Krejci00768f42015-06-18 17:04:04 +02002137
2138 return new;
2139}
2140
Michal Vasko0308dd62015-10-07 09:14:40 +02002141void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002142lys_when_free(struct ly_ctx *ctx, struct lys_when *w,
2143 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002144{
2145 if (!w) {
2146 return;
2147 }
2148
Radek Krejci5138e9f2017-04-12 13:10:46 +02002149 lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002150 lydict_remove(ctx, w->cond);
2151 lydict_remove(ctx, w->dsc);
2152 lydict_remove(ctx, w->ref);
2153
2154 free(w);
2155}
2156
Radek Krejcib7f5e412015-08-13 10:15:51 +02002157static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002158lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug,
2159 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02002160{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002161 struct lys_node *next, *sub;
2162
Radek Krejcic071c542016-01-27 14:57:51 +01002163 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01002164 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002165 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002166 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002167 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002168 }
2169
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002170 lydict_remove(ctx, aug->target_name);
2171 lydict_remove(ctx, aug->dsc);
2172 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002173
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002174 lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002175 lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002176
Radek Krejci5138e9f2017-04-12 13:10:46 +02002177 lys_when_free(ctx, aug->when, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002178}
2179
Radek Krejci1d82ef62015-08-07 14:44:40 +02002180static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002181lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident,
2182 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci6793db02015-05-22 17:49:54 +02002183{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002184 assert(ctx);
2185 if (!ident) {
2186 return;
2187 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002188
Radek Krejci018f1f52016-08-03 16:01:20 +02002189 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02002190 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002191 lydict_remove(ctx, ident->name);
2192 lydict_remove(ctx, ident->dsc);
2193 lydict_remove(ctx, ident->ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002194 lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002195 lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor);
Radek Krejci6793db02015-05-22 17:49:54 +02002196
2197}
2198
Radek Krejci1d82ef62015-08-07 14:44:40 +02002199static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002200lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp,
2201 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002202{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002203 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002204
Radek Krejcid12f57b2015-08-06 10:43:39 +02002205 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002206 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002207 lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002208 }
2209 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002210}
2211
Radek Krejci1d82ef62015-08-07 14:44:40 +02002212static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002213lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io,
2214 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcid12f57b2015-08-06 10:43:39 +02002215{
2216 int i;
2217
2218 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
2219 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002220 lys_tpdf_free(ctx, &io->tpdf[i], private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002221 }
2222 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002223
2224 for (i = 0; i < io->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002225 lys_restr_free(ctx, &io->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002226 }
2227 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002228}
2229
Radek Krejci1d82ef62015-08-07 14:44:40 +02002230static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002231lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif,
2232 void (*private_destructor)(const struct lys_node *node, void *priv))
Pavol Vican7cff97e2016-08-09 14:56:08 +02002233{
2234 int i;
2235
2236 for (i = 0; i < notif->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002237 lys_restr_free(ctx, &notif->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002238 }
2239 free(notif->must);
2240
2241 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002242 lys_tpdf_free(ctx, &notif->tpdf[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002243 }
2244 free(notif->tpdf);
2245}
2246static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002247lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml,
2248 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci537cf382015-06-04 11:07:01 +02002249{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002250 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002251
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002252 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002253 lys_restr_free(ctx, &anyxml->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002254 }
2255 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002256
Radek Krejci5138e9f2017-04-12 13:10:46 +02002257 lys_when_free(ctx, anyxml->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002258}
2259
Radek Krejci1d82ef62015-08-07 14:44:40 +02002260static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002261lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf,
2262 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002263{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002264 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002265
Radek Krejci85a54be2016-10-20 12:39:56 +02002266 /* leafref backlinks */
2267 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002268
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002269 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002270 lys_restr_free(ctx, &leaf->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002271 }
2272 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002273
Radek Krejci5138e9f2017-04-12 13:10:46 +02002274 lys_when_free(ctx, leaf->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002275
Radek Krejci5138e9f2017-04-12 13:10:46 +02002276 lys_type_free(ctx, &leaf->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002277 lydict_remove(ctx, leaf->units);
2278 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02002279}
2280
Radek Krejci1d82ef62015-08-07 14:44:40 +02002281static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002282lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist,
2283 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002284{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002285 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002286
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002287 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002288 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002289 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002290 }
2291
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002292 for (i = 0; i < llist->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002293 lys_restr_free(ctx, &llist->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002294 }
2295 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002296
Pavol Vican38321d02016-08-16 14:56:02 +02002297 for (i = 0; i < llist->dflt_size; i++) {
2298 lydict_remove(ctx, llist->dflt[i]);
2299 }
2300 free(llist->dflt);
2301
Radek Krejci5138e9f2017-04-12 13:10:46 +02002302 lys_when_free(ctx, llist->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002303
Radek Krejci5138e9f2017-04-12 13:10:46 +02002304 lys_type_free(ctx, &llist->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002305 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02002306}
2307
Radek Krejci1d82ef62015-08-07 14:44:40 +02002308static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002309lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list,
2310 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002311{
Radek Krejci581ce772015-11-10 17:22:40 +01002312 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002313
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002314 /* handle only specific parts for LY_NODE_LIST */
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002315 lys_when_free(ctx, list->when, private_destructor);
Radek Krejci537cf382015-06-04 11:07:01 +02002316
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002317 for (i = 0; i < list->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002318 lys_restr_free(ctx, &list->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002319 }
2320 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002321
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002322 for (i = 0; i < list->tpdf_size; i++) {
2323 lys_tpdf_free(ctx, &list->tpdf[i], private_destructor);
2324 }
2325 free(list->tpdf);
2326
2327 free(list->keys);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002328
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002329 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02002330 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002331 lydict_remove(ctx, list->unique[i].expr[j]);
2332 }
2333 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002334 }
2335 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02002336
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002337 lydict_remove(ctx, list->keys_str);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002338}
2339
Radek Krejci1d82ef62015-08-07 14:44:40 +02002340static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002341lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont,
2342 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002343{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002344 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002345
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002346 /* handle only specific parts for LY_NODE_CONTAINER */
2347 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02002348
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002349 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002350 lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002351 }
2352 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02002353
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002354 for (i = 0; i < cont->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002355 lys_restr_free(ctx, &cont->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002356 }
2357 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002358
Radek Krejci5138e9f2017-04-12 13:10:46 +02002359 lys_when_free(ctx, cont->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002360}
2361
Radek Krejci1d82ef62015-08-07 14:44:40 +02002362static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002363lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f,
2364 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci3cf9e222015-06-18 11:37:50 +02002365{
2366 lydict_remove(ctx, f->name);
2367 lydict_remove(ctx, f->dsc);
2368 lydict_remove(ctx, f->ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002369 lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor);
Radek Krejci9de2c042016-10-19 16:53:06 +02002370 ly_set_free(f->depfeatures);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002371 lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002372}
2373
2374static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002375lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e,
2376 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie534c132016-11-23 13:32:31 +01002377{
2378 lydict_remove(ctx, e->name);
2379 lydict_remove(ctx, e->dsc);
2380 lydict_remove(ctx, e->ref);
2381 lydict_remove(ctx, e->argument);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002382 lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002383}
2384
Radek Krejci1d82ef62015-08-07 14:44:40 +02002385static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002386lys_deviation_free(struct lys_module *module, struct lys_deviation *dev,
2387 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcieb00f512015-07-01 16:44:58 +02002388{
Radek Krejci581ce772015-11-10 17:22:40 +01002389 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01002390 struct ly_ctx *ctx;
2391 struct lys_node *next, *elem;
2392
2393 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02002394
2395 lydict_remove(ctx, dev->target_name);
2396 lydict_remove(ctx, dev->dsc);
2397 lydict_remove(ctx, dev->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002398 lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002399
Pavol Vican64d0b762016-08-25 10:44:59 +02002400 if (!dev->deviate) {
2401 return ;
2402 }
2403
Michal Vaskoff006c12016-02-17 11:15:19 +01002404 /* the module was freed, but we only need the context from orig_node, use ours */
2405 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2406 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
2407 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
2408 elem->module = module;
2409
2410 LY_TREE_DFS_END(dev->orig_node, next, elem);
2411 }
2412 lys_node_free(dev->orig_node, NULL, 0);
2413 } else {
2414 /* it's just a shallow copy, freeing one node */
2415 dev->orig_node->module = module;
2416 lys_node_free(dev->orig_node, NULL, 1);
2417 }
2418
Radek Krejcieb00f512015-07-01 16:44:58 +02002419 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002420 lys_extension_instances_free(ctx, dev->deviate[i].ext, dev->deviate[i].ext_size, private_destructor);
Radek Krejci3c4b0ea2017-01-24 16:00:47 +01002421
Radek Krejcid5a5c282016-08-15 15:38:08 +02002422 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02002423 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02002424 }
2425 free(dev->deviate[i].dflt);
2426
Radek Krejcieb00f512015-07-01 16:44:58 +02002427 lydict_remove(ctx, dev->deviate[i].units);
2428
2429 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
2430 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002431 lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002432 }
2433 free(dev->deviate[i].must);
2434
2435 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002436 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
2437 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
2438 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01002439 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002440 }
2441 free(dev->deviate[i].unique);
2442 }
2443 }
2444 free(dev->deviate);
2445}
2446
Radek Krejci1d82ef62015-08-07 14:44:40 +02002447static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002448lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses,
2449 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02002450{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002451 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02002452
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002453 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002454 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002455 lydict_remove(ctx, uses->refine[i].dsc);
2456 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002457
Radek Krejcifde04bd2017-09-13 16:38:38 +02002458 lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor);
2459
Michal Vaskoa275c0a2015-09-24 09:55:42 +02002460 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002461 lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002462 }
2463 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002464
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002465 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02002466 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002467 }
2468 free(uses->refine[i].dflt);
2469
Radek Krejci5138e9f2017-04-12 13:10:46 +02002470 lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002471
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002472 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002473 lydict_remove(ctx, uses->refine[i].mod.presence);
2474 }
2475 }
2476 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002477
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002478 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002479 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002480 }
2481 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002482
Radek Krejci5138e9f2017-04-12 13:10:46 +02002483 lys_when_free(ctx, uses->when, private_destructor);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002484}
2485
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002486void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002487lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv), int shallow)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002488{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002489 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02002490 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002491
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002492 if (!node) {
2493 return;
2494 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002495
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002496 assert(node->module);
2497 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02002498
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002499 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002500
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002501 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002502 if (node->priv && private_destructor) {
2503 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002504 }
2505
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002506 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002507 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002508 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002509 lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002510 lydict_remove(ctx, node->dsc);
2511 lydict_remove(ctx, node->ref);
2512 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002513
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002514 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002515 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002516 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002517 }
2518 }
2519
Radek Krejci5138e9f2017-04-12 13:10:46 +02002520 lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002521
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002522 /* specific part */
2523 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002524 case LYS_CONTAINER:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002525 lys_container_free(ctx, (struct lys_node_container *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002526 break;
Radek Krejci76512572015-08-04 09:47:08 +02002527 case LYS_CHOICE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002528 lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002529 break;
Radek Krejci76512572015-08-04 09:47:08 +02002530 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002531 lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002532 break;
Radek Krejci76512572015-08-04 09:47:08 +02002533 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002534 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002535 break;
Radek Krejci76512572015-08-04 09:47:08 +02002536 case LYS_LIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002537 lys_list_free(ctx, (struct lys_node_list *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002538 break;
Radek Krejci76512572015-08-04 09:47:08 +02002539 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002540 case LYS_ANYDATA:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002541 lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002542 break;
Radek Krejci76512572015-08-04 09:47:08 +02002543 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002544 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002545 break;
Radek Krejci76512572015-08-04 09:47:08 +02002546 case LYS_CASE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002547 lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002548 break;
Radek Krejci76512572015-08-04 09:47:08 +02002549 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002550 /* do nothing */
2551 break;
Radek Krejci76512572015-08-04 09:47:08 +02002552 case LYS_GROUPING:
2553 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002554 case LYS_ACTION:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002555 lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002556 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02002557 case LYS_NOTIF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002558 lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002559 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002560 case LYS_INPUT:
2561 case LYS_OUTPUT:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002562 lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002563 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01002564 case LYS_EXT:
Michal Vasko591e0b22015-08-13 13:53:43 +02002565 case LYS_UNKNOWN:
Michal Vasko53b7da02018-02-13 15:28:42 +01002566 LOGINT(ctx);
Michal Vasko591e0b22015-08-13 13:53:43 +02002567 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002568 }
Radek Krejci5a065542015-05-22 15:02:07 +02002569
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002570 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002571 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002572 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002573}
2574
Radek Krejci2eee5c02016-12-06 19:18:05 +01002575API struct lys_module *
2576lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02002577{
2578 struct ly_ctx *ctx;
2579 int i;
2580
2581 if (!mod || mod->implemented) {
2582 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002583 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002584 }
2585
2586 ctx = mod->ctx;
2587 for (i = 0; i < ctx->models.used; i++) {
2588 if (!ctx->models.list[i]->implemented) {
2589 continue;
2590 }
2591
2592 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2593 /* we have some revision of the module implemented */
2594 return ctx->models.list[i];
2595 }
2596 }
2597
2598 /* we have no revision of the module implemented, return the module itself,
2599 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002600 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002601}
2602
Michal Vasko13b15832015-08-19 11:04:48 +02002603/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002604static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002605module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002606{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002607 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002608 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002609 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002610
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002611 assert(module->ctx);
2612 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002613
Michal Vaskob746fff2016-02-11 11:37:50 +01002614 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002615 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002616 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002617 lydict_remove(ctx, module->imp[i].dsc);
2618 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002619 lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size, private_destructor);
Radek Krejci225376f2016-02-16 17:36:22 +01002620 }
Radek Krejcidce51452015-06-16 15:20:08 +02002621 free(module->imp);
2622
Radek Krejcic071c542016-01-27 14:57:51 +01002623 /* submodules don't have data tree, the data nodes
2624 * are placed in the main module altogether */
2625 if (!module->type) {
2626 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002627 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002628 }
Radek Krejci21181962015-06-30 14:11:00 +02002629 }
Radek Krejci5a065542015-05-22 15:02:07 +02002630
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002631 lydict_remove(ctx, module->dsc);
2632 lydict_remove(ctx, module->ref);
2633 lydict_remove(ctx, module->org);
2634 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002635 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002636
Radek Krejcieb00f512015-07-01 16:44:58 +02002637 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002638 for (i = 0; i < module->rev_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002639 lys_extension_instances_free(ctx, module->rev[i].ext, module->rev[i].ext_size, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002640 lydict_remove(ctx, module->rev[i].dsc);
2641 lydict_remove(ctx, module->rev[i].ref);
2642 }
2643 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002644
Radek Krejcieb00f512015-07-01 16:44:58 +02002645 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002646 for (i = 0; i < module->ident_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002647 lys_ident_free(ctx, &module->ident[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002648 }
2649 module->ident_size = 0;
2650 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002651
Radek Krejcieb00f512015-07-01 16:44:58 +02002652 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002653 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002654 lys_tpdf_free(ctx, &module->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002655 }
2656 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002657
Radek Krejcie534c132016-11-23 13:32:31 +01002658 /* extension instances */
Radek Krejci5138e9f2017-04-12 13:10:46 +02002659 lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002660
Radek Krejcieb00f512015-07-01 16:44:58 +02002661 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002662 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002663 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002664 }
2665 free(module->augment);
2666
Radek Krejcieb00f512015-07-01 16:44:58 +02002667 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002668 for (i = 0; i < module->features_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002669 lys_feature_free(ctx, &module->features[i], private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002670 }
2671 free(module->features);
2672
Radek Krejcieb00f512015-07-01 16:44:58 +02002673 /* deviations */
2674 for (i = 0; i < module->deviation_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002675 lys_deviation_free(module, &module->deviation[i], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002676 }
2677 free(module->deviation);
2678
Radek Krejcie534c132016-11-23 13:32:31 +01002679 /* extensions */
2680 for (i = 0; i < module->extensions_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002681 lys_extension_free(ctx, &module->extensions[i], private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002682 }
2683 free(module->extensions);
2684
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002685 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002686 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002687}
2688
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002689void
Michal Vaskob746fff2016-02-11 11:37:50 +01002690lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002691{
Michal Vasko10681e82018-01-16 14:54:16 +01002692 int i;
2693
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002694 if (!submodule) {
2695 return;
2696 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002697
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002698 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002699 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002700
Michal Vasko10681e82018-01-16 14:54:16 +01002701 /* include */
2702 for (i = 0; i < submodule->inc_size; i++) {
2703 lydict_remove(submodule->ctx, submodule->inc[i].dsc);
2704 lydict_remove(submodule->ctx, submodule->inc[i].ref);
2705 lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor);
2706 /* complete submodule free is done only from main module since
2707 * submodules propagate their includes to the main module */
2708 }
2709 free(submodule->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002710
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002711 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002712}
2713
Radek Krejcib53154b2017-07-19 09:14:13 +02002714int
2715lys_ingrouping(const struct lys_node *node)
Radek Krejci3a5501d2016-07-18 22:03:34 +02002716{
2717 const struct lys_node *iter = node;
2718 assert(node);
2719
2720 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2721 if (!iter) {
2722 return 0;
2723 } else {
2724 return 1;
2725 }
2726}
2727
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002728/*
2729 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2730 */
2731static struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01002732lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002733 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002734{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002735 struct lys_node *retval = NULL, *iter, *p;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002736 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002737 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002738 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002739 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002740 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002741
Michal Vaskoc07187d2015-08-13 15:20:57 +02002742 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002743 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002744 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002745 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002746 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002747 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002748 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002749 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002750 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002751 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002752 struct lys_node_anydata *any = NULL;
2753 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002754 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002755 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002756 struct lys_node_rpc_action *rpc = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02002757 struct lys_node_inout *io = NULL;
Radek Krejcic43151c2017-03-12 07:10:52 +01002758 struct lys_node_notif *ntf = NULL;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002759 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002760 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002761
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002762 /* we cannot just duplicate memory since the strings are stored in
2763 * dictionary and we need to update dictionary counters.
2764 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002765
Radek Krejci1d82ef62015-08-07 14:44:40 +02002766 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002767 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002768 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002769 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002770 break;
2771
Radek Krejci76512572015-08-04 09:47:08 +02002772 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002773 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002774 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002775 break;
2776
Radek Krejci76512572015-08-04 09:47:08 +02002777 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002778 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002779 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002780 break;
2781
Radek Krejci76512572015-08-04 09:47:08 +02002782 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002783 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002784 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002785 break;
2786
Radek Krejci76512572015-08-04 09:47:08 +02002787 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002788 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002789 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002790 break;
2791
Radek Krejci76512572015-08-04 09:47:08 +02002792 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002793 case LYS_ANYDATA:
2794 any = calloc(1, sizeof *any);
2795 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002796 break;
2797
Radek Krejci76512572015-08-04 09:47:08 +02002798 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002799 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002800 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002801 break;
2802
Radek Krejci76512572015-08-04 09:47:08 +02002803 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002804 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002805 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002806 break;
2807
Radek Krejci76512572015-08-04 09:47:08 +02002808 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002809 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002810 rpc = calloc(1, sizeof *rpc);
2811 retval = (struct lys_node *)rpc;
2812 break;
2813
Radek Krejci76512572015-08-04 09:47:08 +02002814 case LYS_INPUT:
2815 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002816 io = calloc(1, sizeof *io);
2817 retval = (struct lys_node *)io;
2818 break;
2819
Radek Krejci76512572015-08-04 09:47:08 +02002820 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002821 ntf = calloc(1, sizeof *ntf);
2822 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002823 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002824
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002825 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01002826 LOGINT(ctx);
Michal Vasko49168a22015-08-17 16:35:41 +02002827 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002828 }
Michal Vasko53b7da02018-02-13 15:28:42 +01002829 LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL);
Michal Vasko253035f2015-12-17 16:58:13 +01002830
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002831 /*
2832 * duplicate generic part of the structure
2833 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002834 retval->name = lydict_insert(ctx, node->name, 0);
2835 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2836 retval->ref = lydict_insert(ctx, node->ref, 0);
2837 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002838
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002839 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002840 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002841
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002842 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002843
Radek Krejcif0bb3602017-01-25 17:05:08 +01002844 retval->ext_size = node->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002845 if (lys_ext_dup(ctx, module, node->ext, node->ext_size, retval, LYEXT_PAR_NODE, &retval->ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002846 goto error;
2847 }
2848
Radek Krejci06214042016-11-04 16:25:58 +01002849 if (node->iffeature_size) {
2850 retval->iffeature_size = node->iffeature_size;
2851 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01002852 LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error);
Michal Vasko253035f2015-12-17 16:58:13 +01002853 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002854
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002855 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002856 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002857 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2858 if (size1) {
2859 /* there is something to duplicate */
2860
2861 /* duplicate compiled expression */
2862 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2863 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01002864 LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002865 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2866
2867 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002868 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01002869 LY_CHECK_ERR_GOTO(!retval->iffeature[i].features, LOGMEM(ctx); free(retval->iffeature[i].expr), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002870
Radek Krejci9ff0a922016-07-14 13:08:05 +02002871 for (j = 0; (unsigned int)j < size2; j++) {
2872 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2873 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002874 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002875 /* feature is resolved in origin, so copy it
2876 * - duplication is used for instantiating groupings
2877 * and if-feature inside grouping is supposed to be
2878 * resolved inside the original grouping, so we want
2879 * to keep pointers to features from the grouping
2880 * context */
2881 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2882 } else if (rc == -1) {
2883 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002884 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002885 }
2886 }
Radek Krejcif0bb3602017-01-25 17:05:08 +01002887
2888 /* duplicate if-feature's extensions */
2889 retval->iffeature[i].ext_size = node->iffeature[i].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002890 if (lys_ext_dup(ctx, module, node->iffeature[i].ext, node->iffeature[i].ext_size,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002891 &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002892 goto error;
2893 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002894 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002895
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002896 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002897 p = parent;
2898 do {
2899 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2900 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002901 if (iter) {
2902 flags = iter->flags & LYS_CONFIG_MASK;
2903 } else {
2904 /* default */
2905 flags = LYS_CONFIG_W;
2906 }
2907
2908 switch (finalize) {
2909 case 1:
2910 /* inherit config flags */
2911 if (retval->flags & LYS_CONFIG_SET) {
2912 /* skip nodes with an explicit config value */
2913 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002914 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2915 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002916 goto error;
2917 }
2918 break;
2919 }
2920
2921 if (retval->nodetype != LYS_USES) {
2922 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2923 }
Radek Krejci2cc25322017-09-06 16:32:02 +02002924
2925 /* inherit status */
Radek Krejcie2807ea2017-09-07 10:52:21 +02002926 if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) {
2927 /* but do it only in case the parent has a stonger status */
2928 retval->flags &= ~LYS_STATUS_MASK;
2929 retval->flags |= (parent->flags & LYS_STATUS_MASK);
2930 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002931 break;
2932 case 2:
2933 /* erase config flags */
2934 retval->flags &= ~LYS_CONFIG_MASK;
2935 retval->flags &= ~LYS_CONFIG_SET;
2936 break;
2937 }
2938
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002939 /* connect it to the parent */
2940 if (lys_node_addchild(parent, retval->module, retval)) {
2941 goto error;
2942 }
Radek Krejcidce51452015-06-16 15:20:08 +02002943
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002944 /* go recursively */
2945 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002946 LY_TREE_FOR(node->child, iter) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002947 if (iter->nodetype & LYS_GROUPING) {
2948 /* do not instantiate groupings */
2949 continue;
2950 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01002951 if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002952 goto error;
2953 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002954 }
2955 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002956
2957 if (finalize == 1) {
2958 /* check that configuration lists have keys
2959 * - we really want to check keys_size in original node, because the keys are
2960 * not yet resolved here, it is done below in nodetype specific part */
2961 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2962 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002963 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002964 goto error;
2965 }
2966 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002967 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002968 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002969 }
2970
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002971 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002972 * duplicate specific part of the structure
2973 */
2974 switch (node->nodetype) {
2975 case LYS_CONTAINER:
2976 if (cont_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002977 cont->when = lys_when_dup(module, cont_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002978 LY_CHECK_GOTO(!cont->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002979 }
2980 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002981
Radek Krejcia8d111f2017-05-31 13:57:37 +02002982 if (cont_orig->must) {
2983 cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres);
2984 LY_CHECK_GOTO(!cont->must, error);
2985 cont->must_size = cont_orig->must_size;
2986 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002987
Radek Krejcif0bb3602017-01-25 17:05:08 +01002988 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
2989
2990 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002991 case LYS_CHOICE:
2992 if (choice_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002993 choice->when = lys_when_dup(module, choice_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002994 LY_CHECK_GOTO(!choice->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002995 }
2996
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002997 if (!shallow) {
2998 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002999 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
3000 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
3001 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003002 if (rc) {
3003 if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003004 LOGINT(ctx);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003005 }
3006 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02003007 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003008 } else {
3009 /* useless to check return value, we don't know whether
3010 * there really wasn't any default defined or it just hasn't
3011 * been resolved, we just hope for the best :)
3012 */
3013 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02003014 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003015 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003016 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003017 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003018 break;
3019
3020 case LYS_LEAF:
Radek Krejcib53154b2017-07-19 09:14:13 +02003021 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), lys_ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02003022 goto error;
3023 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003024 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
3025
3026 if (leaf_orig->dflt) {
3027 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003028 }
3029
Radek Krejcia8d111f2017-05-31 13:57:37 +02003030 if (leaf_orig->must) {
3031 leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres);
3032 LY_CHECK_GOTO(!leaf->must, error);
3033 leaf->must_size = leaf_orig->must_size;
3034 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003035
3036 if (leaf_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003037 leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003038 LY_CHECK_GOTO(!leaf->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003039 }
3040 break;
3041
3042 case LYS_LEAFLIST:
Radek Krejcib53154b2017-07-19 09:14:13 +02003043 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), lys_ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02003044 goto error;
3045 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003046 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
3047
3048 llist->min = llist_orig->min;
3049 llist->max = llist_orig->max;
3050
Radek Krejcia8d111f2017-05-31 13:57:37 +02003051 if (llist_orig->must) {
3052 llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres);
3053 LY_CHECK_GOTO(!llist->must, error);
3054 llist->must_size = llist_orig->must_size;
3055 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003056
Radek Krejcia8d111f2017-05-31 13:57:37 +02003057 if (llist_orig->dflt) {
3058 llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01003059 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003060 llist->dflt_size = llist_orig->dflt_size;
Radek Krejcia8d111f2017-05-31 13:57:37 +02003061
Radek Krejcic699e422017-06-02 15:18:58 +02003062 for (i = 0; i < llist->dflt_size; i++) {
3063 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
Radek Krejci51673202016-11-01 17:00:32 +01003064 }
3065 }
3066
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003067 if (llist_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003068 llist->when = lys_when_dup(module, llist_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003069 }
3070 break;
3071
3072 case LYS_LIST:
3073 list->min = list_orig->min;
3074 list->max = list_orig->max;
3075
Radek Krejcia8d111f2017-05-31 13:57:37 +02003076 if (list_orig->must) {
3077 list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres);
3078 LY_CHECK_GOTO(!list->must, error);
3079 list->must_size = list_orig->must_size;
3080 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003081
Radek Krejcif0bb3602017-01-25 17:05:08 +01003082 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Michal Vasko38d01f72015-06-15 09:41:06 +02003083
Radek Krejcia8d111f2017-05-31 13:57:37 +02003084 if (list_orig->keys_size) {
3085 list->keys = calloc(list_orig->keys_size, sizeof *list->keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01003086 LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error);
Radek Krejci5c08a992016-11-02 13:30:04 +01003087 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003088 list->keys_size = list_orig->keys_size;
Michal Vasko0ea41032015-06-16 08:53:55 +02003089
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003090 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01003091 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
3092 * in another grouping */
3093 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
3094 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
3095 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02003096 }
Michal Vasko0ea41032015-06-16 08:53:55 +02003097 } else {
Radek Krejcia8d111f2017-05-31 13:57:37 +02003098 memcpy(list->keys, list_orig->keys, list_orig->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02003099 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003100 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003101
Radek Krejcia8d111f2017-05-31 13:57:37 +02003102 if (list_orig->unique) {
3103 list->unique = malloc(list_orig->unique_size * sizeof *list->unique);
Michal Vasko53b7da02018-02-13 15:28:42 +01003104 LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003105 list->unique_size = list_orig->unique_size;
Radek Krejci581ce772015-11-10 17:22:40 +01003106
Radek Krejcic699e422017-06-02 15:18:58 +02003107 for (i = 0; i < list->unique_size; ++i) {
3108 list->unique[i].expr = malloc(list_orig->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01003109 LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error);
Radek Krejcic699e422017-06-02 15:18:58 +02003110 list->unique[i].expr_size = list_orig->unique[i].expr_size;
3111 for (j = 0; j < list->unique[i].expr_size; j++) {
3112 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
3113
3114 /* if it stays in unres list, duplicate it also there */
3115 unique_info = malloc(sizeof *unique_info);
Michal Vasko53b7da02018-02-13 15:28:42 +01003116 LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error);
Radek Krejcic699e422017-06-02 15:18:58 +02003117 unique_info->list = (struct lys_node *)list;
3118 unique_info->expr = list->unique[i].expr[j];
3119 unique_info->trg_type = &list->unique[i].trg_type;
3120 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
3121 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003122 }
3123 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003124
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003125 if (list_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003126 list->when = lys_when_dup(module, list_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003127 LY_CHECK_GOTO(!list->when, error);
Radek Krejciefaeba32015-05-27 14:30:57 +02003128 }
Radek Krejcidce51452015-06-16 15:20:08 +02003129 break;
3130
3131 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02003132 case LYS_ANYDATA:
Radek Krejcia8d111f2017-05-31 13:57:37 +02003133 if (any_orig->must) {
3134 any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres);
3135 LY_CHECK_GOTO(!any->must, error);
3136 any->must_size = any_orig->must_size;
3137 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003138
Radek Krejcibf2abff2016-08-23 15:51:52 +02003139 if (any_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003140 any->when = lys_when_dup(module, any_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003141 LY_CHECK_GOTO(!any->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003142 }
3143 break;
3144
3145 case LYS_USES:
3146 uses->grp = uses_orig->grp;
3147
3148 if (uses_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003149 uses->when = lys_when_dup(module, uses_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003150 LY_CHECK_GOTO(!uses->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003151 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01003152 /* it is not needed to duplicate refine, nor augment. They are already applied to the uses children */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003153 break;
3154
3155 case LYS_CASE:
3156 if (cs_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003157 cs->when = lys_when_dup(module, cs_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003158 LY_CHECK_GOTO(!cs->when, error);
Radek Krejcidce51452015-06-16 15:20:08 +02003159 }
3160 break;
3161
Radek Krejci96935402016-11-04 16:27:28 +01003162 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02003163 case LYS_RPC:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003164 case LYS_INPUT:
3165 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02003166 case LYS_NOTIF:
Radek Krejcif0bb3602017-01-25 17:05:08 +01003167 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003168 break;
3169
3170 default:
3171 /* LY_NODE_AUGMENT */
Michal Vasko53b7da02018-02-13 15:28:42 +01003172 LOGINT(ctx);
Michal Vasko49168a22015-08-17 16:35:41 +02003173 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003174 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003175
3176 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02003177
3178error:
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003179 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02003180 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003181}
3182
Radek Krejcib3142312016-11-09 11:04:12 +01003183int
3184lys_has_xpath(const struct lys_node *node)
3185{
3186 assert(node);
3187
3188 switch (node->nodetype) {
3189 case LYS_AUGMENT:
3190 if (((struct lys_node_augment *)node)->when) {
3191 return 1;
3192 }
3193 break;
3194 case LYS_CASE:
3195 if (((struct lys_node_case *)node)->when) {
3196 return 1;
3197 }
3198 break;
3199 case LYS_CHOICE:
3200 if (((struct lys_node_choice *)node)->when) {
3201 return 1;
3202 }
3203 break;
3204 case LYS_ANYDATA:
3205 if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) {
3206 return 1;
3207 }
3208 break;
3209 case LYS_LEAF:
3210 if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) {
3211 return 1;
3212 }
3213 break;
3214 case LYS_LEAFLIST:
3215 if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) {
3216 return 1;
3217 }
3218 break;
3219 case LYS_LIST:
3220 if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) {
3221 return 1;
3222 }
3223 break;
3224 case LYS_CONTAINER:
3225 if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) {
3226 return 1;
3227 }
3228 break;
3229 case LYS_INPUT:
3230 case LYS_OUTPUT:
3231 if (((struct lys_node_inout *)node)->must_size) {
3232 return 1;
3233 }
3234 break;
3235 case LYS_NOTIF:
3236 if (((struct lys_node_notif *)node)->must_size) {
3237 return 1;
3238 }
3239 break;
3240 case LYS_USES:
3241 if (((struct lys_node_uses *)node)->when) {
3242 return 1;
3243 }
3244 break;
3245 default:
3246 /* does not have XPath */
3247 break;
3248 }
3249
3250 return 0;
3251}
3252
Michal Vasko568b1952018-01-30 15:53:30 +01003253int
3254lys_type_is_local(const struct lys_type *type)
3255{
3256 if (!type->der->module) {
3257 /* build-in type */
3258 return 1;
3259 }
3260 /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */
3261 return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module));
3262}
3263
Radek Krejci2cc25322017-09-06 16:32:02 +02003264/*
3265 * shallow -
3266 * - do not inherit status from the parent
3267 */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003268struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01003269lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003270 struct unres_schema *unres, int shallow)
3271{
3272 struct lys_node *p = NULL;
Radek Krejcib3142312016-11-09 11:04:12 +01003273 int finalize = 0;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003274 struct lys_node *result, *iter, *next;
3275
3276 if (!shallow) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01003277 /* get know where in schema tree we are to know what should be done during instantiation of the grouping */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003278 for (p = parent;
3279 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
3280 p = lys_parent(p));
3281 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
3282 }
3283
Radek Krejci6ff885d2017-01-03 14:06:22 +01003284 result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize);
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003285 if (finalize) {
3286 /* check xpath expressions in the instantiated tree */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003287 for (iter = next = result; iter; iter = next) {
Radek Krejcib3142312016-11-09 11:04:12 +01003288 if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
Radek Krejci3c48d042016-11-07 14:42:36 +01003289 /* invalid xpath */
3290 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003291 }
3292
3293 /* select next item */
3294 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
3295 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
3296 next = NULL;
3297 } else {
3298 next = iter->child;
3299 }
3300 if (!next) {
3301 /* no children, try siblings */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003302 if (iter == result) {
3303 /* we are done, no next element to process */
3304 break;
3305 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003306 next = iter->next;
3307 }
3308 while (!next) {
3309 /* parent is already processed, go to its sibling */
3310 iter = lys_parent(iter);
Radek Krejci7212e0a2017-03-08 15:58:22 +01003311 if (lys_parent(iter) == lys_parent(result)) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003312 /* we are done, no next element to process */
3313 break;
3314 }
3315 next = iter->next;
3316 }
3317 }
3318 }
3319
3320 return result;
3321}
3322
Michal Vasko13b15832015-08-19 11:04:48 +02003323void
Michal Vaskoff006c12016-02-17 11:15:19 +01003324lys_node_switch(struct lys_node *dst, struct lys_node *src)
3325{
3326 struct lys_node *child;
3327
Michal Vaskob42b6972016-06-06 14:21:30 +02003328 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01003329
3330 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02003331 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01003332 dst->prev->next = src;
3333 }
3334
3335 /* sibling prev */
3336 if (dst->next) {
3337 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02003338 } else {
3339 for (child = dst->prev; child->prev->next; child = child->prev);
3340 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003341 }
3342
3343 /* next */
3344 src->next = dst->next;
3345 dst->next = NULL;
3346
3347 /* prev */
3348 if (dst->prev != dst) {
3349 src->prev = dst->prev;
3350 }
3351 dst->prev = dst;
3352
3353 /* parent child */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003354 if (dst->parent) {
3355 if (dst->parent->child == dst) {
3356 dst->parent->child = src;
3357 }
Radek Krejci115fa882017-03-01 16:15:07 +01003358 } else if (lys_main_module(dst->module)->data == dst) {
3359 lys_main_module(dst->module)->data = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003360 }
3361
3362 /* parent */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003363 src->parent = dst->parent; dst->parent = NULL;
3364
Michal Vaskoff006c12016-02-17 11:15:19 +01003365
3366 /* child parent */
3367 LY_TREE_FOR(dst->child, child) {
3368 if (child->parent == dst) {
3369 child->parent = src;
3370 }
3371 }
3372
3373 /* child */
3374 src->child = dst->child;
3375 dst->child = NULL;
Radek Krejcif0bb3602017-01-25 17:05:08 +01003376
3377 /* node-specific data */
3378 switch (dst->nodetype) {
3379 case LYS_CONTAINER:
3380 ((struct lys_node_container *)src)->tpdf_size = ((struct lys_node_container *)dst)->tpdf_size;
3381 ((struct lys_node_container *)src)->tpdf = ((struct lys_node_container *)dst)->tpdf;
3382 ((struct lys_node_container *)dst)->tpdf_size = 0;
3383 ((struct lys_node_container *)dst)->tpdf = NULL;
3384 break;
3385 case LYS_LIST:
3386 ((struct lys_node_list *)src)->tpdf_size = ((struct lys_node_list *)dst)->tpdf_size;
3387 ((struct lys_node_list *)src)->tpdf = ((struct lys_node_list *)dst)->tpdf;
3388 ((struct lys_node_list *)dst)->tpdf_size = 0;
3389 ((struct lys_node_list *)dst)->tpdf = NULL;
3390 break;
3391 case LYS_RPC:
3392 case LYS_ACTION:
3393 ((struct lys_node_rpc_action *)src)->tpdf_size = ((struct lys_node_rpc_action *)dst)->tpdf_size;
3394 ((struct lys_node_rpc_action *)src)->tpdf = ((struct lys_node_rpc_action *)dst)->tpdf;
3395 ((struct lys_node_rpc_action *)dst)->tpdf_size = 0;
3396 ((struct lys_node_rpc_action *)dst)->tpdf = NULL;
3397 break;
3398 case LYS_NOTIF:
3399 ((struct lys_node_notif *)src)->tpdf_size = ((struct lys_node_notif *)dst)->tpdf_size;
3400 ((struct lys_node_notif *)src)->tpdf = ((struct lys_node_notif *)dst)->tpdf;
3401 ((struct lys_node_notif *)dst)->tpdf_size = 0;
3402 ((struct lys_node_notif *)dst)->tpdf = NULL;
3403 break;
3404 case LYS_INPUT:
3405 case LYS_OUTPUT:
3406 ((struct lys_node_inout *)src)->tpdf_size = ((struct lys_node_inout *)dst)->tpdf_size;
3407 ((struct lys_node_inout *)src)->tpdf = ((struct lys_node_inout *)dst)->tpdf;
3408 ((struct lys_node_inout *)dst)->tpdf_size = 0;
3409 ((struct lys_node_inout *)dst)->tpdf = NULL;
3410 break;
3411 default:
3412 /* nothing special */
3413 break;
3414 }
3415
Michal Vaskoff006c12016-02-17 11:15:19 +01003416}
3417
3418void
Michal Vasko10681e82018-01-16 14:54:16 +01003419lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int free_subs, int remove_from_ctx)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003420{
3421 struct ly_ctx *ctx;
3422 int i;
3423
3424 if (!module) {
3425 return;
3426 }
3427
3428 /* remove schema from the context */
3429 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01003430 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02003431 for (i = 0; i < ctx->models.used; i++) {
3432 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01003433 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003434 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01003435 memmove(&ctx->models.list[i], ctx->models.list[i + 1], (ctx->models.used - i) * sizeof *ctx->models.list);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003436 ctx->models.list[ctx->models.used] = NULL;
3437 /* we are done */
3438 break;
3439 }
3440 }
3441 }
3442
3443 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01003444 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003445
Michal Vasko10681e82018-01-16 14:54:16 +01003446 /* include */
3447 for (i = 0; i < module->inc_size; i++) {
3448 lydict_remove(ctx, module->inc[i].dsc);
3449 lydict_remove(ctx, module->inc[i].ref);
3450 lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor);
3451 /* complete submodule free is done only from main module since
3452 * submodules propagate their includes to the main module */
3453 if (free_subs) {
3454 lys_submodule_free(module->inc[i].submodule, private_destructor);
3455 }
3456 }
3457 free(module->inc);
3458
Radek Krejcida04f4a2015-05-21 12:54:09 +02003459 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01003460 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003461
3462 free(module);
3463}
Radek Krejci7e97c352015-06-19 16:26:34 +02003464
Radek Krejci9de2c042016-10-19 16:53:06 +02003465static void
3466lys_features_disable_recursive(struct lys_feature *f)
3467{
3468 unsigned int i;
3469 struct lys_feature *depf;
3470
3471 /* disable the feature */
3472 f->flags &= ~LYS_FENABLED;
3473
3474 /* by disabling feature we have to disable also all features that depends on this feature */
3475 if (f->depfeatures) {
3476 for (i = 0; i < f->depfeatures->number; i++) {
3477 depf = (struct lys_feature *)f->depfeatures->set.g[i];
3478 if (depf->flags & LYS_FENABLED) {
3479 lys_features_disable_recursive(depf);
3480 }
3481 }
3482 }
3483}
3484
3485
Radek Krejci7e97c352015-06-19 16:26:34 +02003486/*
3487 * op: 1 - enable, 0 - disable
3488 */
3489static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003490lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02003491{
3492 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003493 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02003494 int progress, faili, failj, failk;
3495
3496 uint8_t fsize;
3497 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02003498
3499 if (!module || !name || !strlen(name)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003500 LOGARG;
Radek Krejci7e97c352015-06-19 16:26:34 +02003501 return EXIT_FAILURE;
3502 }
3503
3504 if (!strcmp(name, "*")) {
3505 /* enable all */
3506 all = 1;
3507 }
3508
Radek Krejcia889c1f2016-10-19 15:50:11 +02003509 progress = failk = 1;
3510 while (progress && failk) {
3511 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
3512 if (i == -1) {
3513 fsize = module->features_size;
3514 f = module->features;
3515 } else {
3516 fsize = module->inc[i].submodule->features_size;
3517 f = module->inc[i].submodule->features;
3518 }
3519
3520 for (j = 0; j < fsize; j++) {
3521 if (all || !strcmp(f[j].name, name)) {
Michal Vasko34c3b552016-11-21 09:07:43 +01003522 if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) {
3523 if (all) {
3524 /* skip already set features */
3525 continue;
3526 } else {
3527 /* feature already set correctly */
3528 return EXIT_SUCCESS;
3529 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003530 }
3531
3532 if (op) {
3533 /* check referenced features if they are enabled */
3534 for (k = 0; k < f[j].iffeature_size; k++) {
3535 if (!resolve_iffeature(&f[j].iffeature[k])) {
3536 if (all) {
3537 faili = i;
3538 failj = j;
3539 failk = k + 1;
3540 break;
3541 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003542 LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
Radek Krejcia889c1f2016-10-19 15:50:11 +02003543 f[j].name, k + 1);
3544 return EXIT_FAILURE;
3545 }
3546 }
3547 }
3548
3549 if (k == f[j].iffeature_size) {
3550 /* the last check passed, do the change */
3551 f[j].flags |= LYS_FENABLED;
3552 progress++;
3553 }
3554 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02003555 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02003556 progress++;
3557 }
3558 if (!all) {
3559 /* stop in case changing a single feature */
3560 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003561 }
3562 }
Radek Krejci7e97c352015-06-19 16:26:34 +02003563 }
3564 }
3565 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003566 if (failk) {
3567 /* print info about the last failing feature */
Michal Vasko53b7da02018-02-13 15:28:42 +01003568 LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
Radek Krejcia889c1f2016-10-19 15:50:11 +02003569 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
3570 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02003571 }
3572
3573 if (all) {
3574 return EXIT_SUCCESS;
3575 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02003576 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02003577 return EXIT_FAILURE;
3578 }
3579}
3580
3581API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003582lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003583{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003584 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02003585}
3586
3587API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003588lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003589{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003590 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003591}
3592
3593API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003594lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003595{
3596 int i, j;
3597
3598 if (!module || !feature) {
3599 return -1;
3600 }
3601
3602 /* search for the specified feature */
3603 /* module itself */
3604 for (i = 0; i < module->features_size; i++) {
3605 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003606 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003607 return 1;
3608 } else {
3609 return 0;
3610 }
3611 }
3612 }
3613
3614 /* submodules */
3615 for (j = 0; j < module->inc_size; j++) {
3616 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
3617 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003618 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003619 return 1;
3620 } else {
3621 return 0;
3622 }
3623 }
3624 }
3625 }
3626
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003627 /* feature definition not found */
3628 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02003629}
Michal Vasko2367e7c2015-07-07 11:33:44 +02003630
Radek Krejci96a10da2015-07-30 11:00:14 +02003631API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01003632lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02003633{
Radek Krejci96a10da2015-07-30 11:00:14 +02003634 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003635 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003636 unsigned int count;
3637
3638 if (!module) {
3639 return NULL;
3640 }
3641
3642 count = module->features_size;
3643 for (i = 0; i < module->inc_size; i++) {
3644 count += module->inc[i].submodule->features_size;
3645 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003646 result = malloc((count + 1) * sizeof *result);
Michal Vasko53b7da02018-02-13 15:28:42 +01003647 LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003648
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003649 if (states) {
3650 *states = malloc((count + 1) * sizeof **states);
Michal Vasko53b7da02018-02-13 15:28:42 +01003651 LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL);
Michal Vasko2367e7c2015-07-07 11:33:44 +02003652 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003653 count = 0;
3654
3655 /* module itself */
3656 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003657 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003658 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003659 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003660 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003661 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003662 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003663 }
3664 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003665 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003666 }
3667
3668 /* submodules */
3669 for (j = 0; j < module->inc_size; j++) {
3670 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003671 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003672 if (states) {
3673 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3674 (*states)[count] = 1;
3675 } else {
3676 (*states)[count] = 0;
3677 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003678 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003679 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003680 }
3681 }
3682
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003683 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003684 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003685
3686 return result;
3687}
Michal Vaskobaefb032015-09-24 14:52:10 +02003688
Radek Krejci6910a032016-04-13 10:06:21 +02003689API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003690lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003691{
Michal Vaskof53187d2017-01-13 13:23:14 +01003692 if (!node) {
3693 return NULL;
3694 }
3695
Radek Krejcic071c542016-01-27 14:57:51 +01003696 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3697}
3698
Radek Krejci6910a032016-04-13 10:06:21 +02003699API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003700lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003701{
Michal Vaskof53187d2017-01-13 13:23:14 +01003702 if (!module) {
3703 return NULL;
3704 }
3705
Michal Vasko320e8532016-02-15 13:11:57 +01003706 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3707}
3708
Michal Vaskobaefb032015-09-24 14:52:10 +02003709API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003710lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003711{
Radek Krejcif95b6292017-02-13 15:57:37 +01003712 struct lys_node *parent;
3713
3714 if (!node) {
Michal Vaskobaefb032015-09-24 14:52:10 +02003715 return NULL;
3716 }
3717
Radek Krejcif95b6292017-02-13 15:57:37 +01003718 if (node->nodetype == LYS_EXT) {
3719 if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) {
3720 return NULL;
3721 }
3722 parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent;
3723 } else if (!node->parent) {
3724 return NULL;
3725 } else {
3726 parent = node->parent;
Michal Vaskobaefb032015-09-24 14:52:10 +02003727 }
3728
Radek Krejcif95b6292017-02-13 15:57:37 +01003729 if (parent->nodetype == LYS_AUGMENT) {
3730 return ((struct lys_node_augment *)parent)->target;
3731 } else {
3732 return parent;
3733 }
3734}
3735
3736struct lys_node **
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003737lys_child(const struct lys_node *node, LYS_NODE nodetype)
Radek Krejcif95b6292017-02-13 15:57:37 +01003738{
3739 void *pp;
3740 assert(node);
3741
3742 if (node->nodetype == LYS_EXT) {
3743 pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL);
3744 if (!pp) {
3745 return NULL;
3746 }
3747 return (struct lys_node **)pp;
Michal Vasko54d4c202017-08-09 14:09:18 +02003748 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Radek Krejcif7fe2cb2017-08-09 10:27:12 +02003749 return NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01003750 } else {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003751 return (struct lys_node **)&node->child;
Radek Krejcif95b6292017-02-13 15:57:37 +01003752 }
Michal Vaskobaefb032015-09-24 14:52:10 +02003753}
Michal Vasko1b229152016-01-13 11:28:38 +01003754
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003755API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003756lys_set_private(const struct lys_node *node, void *priv)
3757{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003758 void *prev;
3759
Michal Vasko1b229152016-01-13 11:28:38 +01003760 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003761 LOGARG;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003762 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003763 }
3764
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003765 prev = node->priv;
3766 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003767
3768 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003769}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003770
Michal Vasko01c6fd22016-05-20 11:43:05 +02003771int
3772lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3773{
Michal Vasko53b7da02018-02-13 15:28:42 +01003774 struct lys_node_leaf *iter;
3775 struct ly_ctx *ctx = leafref_target->module->ctx;
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003776
Michal Vasko48a573d2016-07-01 11:46:02 +02003777 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003778 LOGINT(ctx);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003779 return -1;
3780 }
3781
Pavol Vican93175152016-08-30 15:34:44 +02003782 /* check for config flag */
Radek Krejcic688ca02017-03-20 12:54:39 +01003783 if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 &&
3784 (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003785 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref,
Radek Krejcid831dd42017-03-16 12:59:30 +01003786 "The leafref %s is config but refers to a non-config %s.",
Pavol Vican93175152016-08-30 15:34:44 +02003787 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3788 return -1;
3789 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003790 /* check for cycles */
Michal Vasko53b7da02018-02-13 15:28:42 +01003791 for (iter = leafref_target; iter && iter->type.base == LY_TYPE_LEAFREF; iter = iter->type.info.lref.target) {
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003792 if ((void *)iter == (void *)leafref) {
3793 /* cycle detected */
Michal Vasko53b7da02018-02-13 15:28:42 +01003794 LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003795 return -1;
3796 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003797 }
3798
3799 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003800 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003801 if (!leafref_target->backlinks) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003802 leafref_target->backlinks = (void *)ly_set_new();
Radek Krejci85a54be2016-10-20 12:39:56 +02003803 if (!leafref_target->backlinks) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003804 LOGMEM(ctx);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003805 return -1;
3806 }
3807 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003808 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003809
3810 return 0;
3811}
3812
Michal Vasko8548e082016-07-22 12:00:18 +02003813/* not needed currently */
3814#if 0
3815
Michal Vasko5b3492c2016-07-20 09:37:40 +02003816static const char *
3817lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3818{
3819 struct lys_module *prev_mod;
3820 uint32_t str_len, mod_len, buf_idx;
3821
Radek Krejcibf2abff2016-08-23 15:51:52 +02003822 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003823 LOGINT;
3824 return NULL;
3825 }
3826
3827 buf_idx = buf_len - 1;
3828 buf[buf_idx] = '\0';
3829
3830 while (node) {
3831 if (lys_parent(node)) {
3832 prev_mod = lys_node_module(lys_parent(node));
3833 } else {
3834 prev_mod = NULL;
3835 }
3836
Radek Krejcibf2abff2016-08-23 15:51:52 +02003837 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003838 str_len = strlen(node->name);
3839
3840 if (prev_mod != node->module) {
3841 mod_len = strlen(node->module->name);
3842 } else {
3843 mod_len = 0;
3844 }
3845
3846 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3847 LOGINT;
3848 return NULL;
3849 }
3850
3851 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3852
3853 buf[buf_idx] = '/';
3854 if (mod_len) {
3855 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3856 buf[buf_idx + 1 + mod_len] = ':';
3857 }
3858 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3859 }
3860
3861 node = lys_parent(node);
3862 }
3863
3864 return buf + buf_idx;
3865}
3866
Michal Vasko8548e082016-07-22 12:00:18 +02003867#endif
3868
3869API struct ly_set *
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003870lys_xpath_atomize(const struct lys_node *ctx_node, enum lyxp_node_type ctx_node_type, const char *expr, int options)
Michal Vasko5b3492c2016-07-20 09:37:40 +02003871{
Michal Vasko508a50d2016-09-07 14:50:33 +02003872 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003873 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003874 uint32_t i;
3875
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003876 if (!ctx_node || !expr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003877 LOGARG;
Michal Vasko8548e082016-07-22 12:00:18 +02003878 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003879 }
3880
Michal Vaskob94a5e42016-09-08 14:01:56 +02003881 /* adjust the root */
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003882 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
Michal Vaskob94a5e42016-09-08 14:01:56 +02003883 do {
Michal Vaskocb45f472018-02-12 10:47:42 +01003884 ctx_node = lys_getnext(NULL, NULL, lys_node_module(ctx_node), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003885 } while ((ctx_node_type == LYXP_NODE_ROOT_CONFIG) && (ctx_node->flags & LYS_CONFIG_R));
Michal Vaskob94a5e42016-09-08 14:01:56 +02003886 }
3887
Michal Vasko508a50d2016-09-07 14:50:33 +02003888 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003889
3890 if (options & LYXP_MUST) {
3891 options &= ~LYXP_MUST;
3892 options |= LYXP_SNODE_MUST;
3893 } else if (options & LYXP_WHEN) {
3894 options &= ~LYXP_WHEN;
3895 options |= LYXP_SNODE_WHEN;
3896 } else {
3897 options |= LYXP_SNODE;
3898 }
3899
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003900 if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003901 free(set.val.snodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01003902 LOGVAL(ctx_node->module->ctx, LYE_SPEC, LY_VLOG_LYS, ctx_node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003903 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003904 }
3905
Michal Vasko8548e082016-07-22 12:00:18 +02003906 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003907
Michal Vasko508a50d2016-09-07 14:50:33 +02003908 for (i = 0; i < set.used; ++i) {
3909 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003910 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003911 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003912 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003913 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003914 return NULL;
3915 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003916 break;
3917 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003918 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003919 break;
3920 }
3921 }
3922
Michal Vasko508a50d2016-09-07 14:50:33 +02003923 free(set.val.snodes);
3924 return ret_set;
3925}
3926
3927API struct ly_set *
3928lys_node_xpath_atomize(const struct lys_node *node, int options)
3929{
3930 const struct lys_node *next, *elem, *parent, *tmp;
3931 struct lyxp_set set;
3932 struct ly_set *ret_set;
3933 uint16_t i;
3934
3935 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003936 LOGARG;
Michal Vasko508a50d2016-09-07 14:50:33 +02003937 return NULL;
3938 }
3939
3940 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3941 if (!parent) {
3942 /* not in input, output, or notification */
3943 return NULL;
3944 }
3945
3946 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003947 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003948 return NULL;
3949 }
3950
3951 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoc04173b2018-03-09 10:43:22 +01003952 if ((options & LYXP_NO_LOCAL) && !(elem->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003953 /* elem has no dependencies from other subtrees and local nodes get discarded */
3954 goto next_iter;
3955 }
3956
Michal Vaskof96dfb62017-08-17 12:23:49 +02003957 if (lyxp_node_atomize(elem, &set, 0)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003958 ly_set_free(ret_set);
3959 free(set.val.snodes);
3960 return NULL;
3961 }
3962
3963 for (i = 0; i < set.used; ++i) {
3964 switch (set.val.snodes[i].type) {
3965 case LYXP_NODE_ELEM:
3966 if (options & LYXP_NO_LOCAL) {
3967 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3968 if (tmp) {
3969 /* in local subtree, discard */
3970 break;
3971 }
3972 }
3973 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3974 ly_set_free(ret_set);
3975 free(set.val.snodes);
3976 return NULL;
3977 }
3978 break;
3979 default:
3980 /* ignore roots, text and attr should not ever appear */
3981 break;
3982 }
3983 }
3984
3985 free(set.val.snodes);
3986 if (!(options & LYXP_RECURSIVE)) {
3987 break;
3988 }
3989next_iter:
3990 LY_TREE_DFS_END(node, next, elem);
3991 }
3992
Michal Vasko8548e082016-07-22 12:00:18 +02003993 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003994}
3995
Michal Vasko44ab1462017-05-18 13:18:36 +02003996/* logs */
3997int
3998apply_aug(struct lys_node_augment *augment, struct unres_schema *unres)
Radek Krejci0ec51da2016-12-14 16:42:03 +01003999{
Michal Vasko44ab1462017-05-18 13:18:36 +02004000 struct lys_node *child, *parent;
4001 int clear_config;
4002 unsigned int u;
Michal Vaskod02e30e2018-01-22 13:35:48 +01004003 uint8_t *v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004004 struct lys_ext_instance *ext;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004005
4006 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
4007
Radek Krejcic9d78692017-08-24 17:17:18 +02004008 if (!augment->child) {
4009 /* nothing to apply */
4010 goto success;
4011 }
4012
Michal Vaskobb520442017-05-23 10:55:18 +02004013 /* check that all the modules are implemented */
4014 for (parent = augment->target; parent; parent = lys_parent(parent)) {
4015 if (!lys_node_module(parent)->implemented) {
4016 if (lys_set_implemented(lys_node_module(parent))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004017 LOGERR(augment->module->ctx, ly_errno, "Making the augment target module \"%s\" implemented failed.",
4018 lys_node_module(parent)->name);
Michal Vaskobb520442017-05-23 10:55:18 +02004019 return -1;
4020 }
Michal Vaskobb520442017-05-23 10:55:18 +02004021 }
4022 }
4023
Radek Krejci0ec51da2016-12-14 16:42:03 +01004024 /* reconnect augmenting data into the target - add them to the target child list */
4025 if (augment->target->child) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004026 child = augment->target->child->prev;
4027 child->next = augment->child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004028 augment->target->child->prev = augment->child->prev;
Michal Vasko44ab1462017-05-18 13:18:36 +02004029 augment->child->prev = child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004030 } else {
4031 augment->target->child = augment->child;
4032 }
4033
Michal Vasko44ab1462017-05-18 13:18:36 +02004034 /* inherit config information from actual parent */
4035 for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent));
4036 clear_config = (parent) ? 1 : 0;
4037 LY_TREE_FOR(augment->child, child) {
4038 if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) {
4039 return -1;
4040 }
4041 }
4042
4043 /* inherit extensions if any */
4044 for (u = 0; u < augment->target->ext_size; u++) {
4045 ext = augment->target->ext[u]; /* shortcut */
4046 if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) {
Michal Vaskod02e30e2018-01-22 13:35:48 +01004047 v = malloc(sizeof *v);
Michal Vasko53b7da02018-02-13 15:28:42 +01004048 LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1);
Michal Vaskod02e30e2018-01-22 13:35:48 +01004049 *v = u;
4050 if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext,
4051 UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004052 /* something really bad happend since the extension finalization is not actually
4053 * being resolved while adding into unres, so something more serious with the unres
4054 * list itself must happened */
4055 return -1;
4056 }
4057 }
4058 }
4059
Radek Krejcic9d78692017-08-24 17:17:18 +02004060success:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004061 /* remove the flag about not applicability */
4062 augment->flags &= ~LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004063 return EXIT_SUCCESS;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004064}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004065
Radek Krejcib2541a32016-12-12 16:45:57 +01004066static void
4067remove_aug(struct lys_node_augment *augment)
4068{
4069 struct lys_node *last, *elem;
4070
Michal Vaskof1aa47d2017-09-21 12:09:29 +02004071 if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004072 /* skip already not applied augment */
Radek Krejcib2541a32016-12-12 16:45:57 +01004073 return;
4074 }
4075
4076 elem = augment->child;
4077 if (elem) {
4078 LY_TREE_FOR(elem, last) {
4079 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
4080 break;
4081 }
4082 }
4083 /* elem is first augment child, last is the last child */
4084
4085 /* parent child ptr */
4086 if (augment->target->child == elem) {
4087 augment->target->child = last->next;
4088 }
4089
4090 /* parent child next ptr */
4091 if (elem->prev->next) {
4092 elem->prev->next = last->next;
4093 }
4094
4095 /* parent child prev ptr */
4096 if (last->next) {
4097 last->next->prev = elem->prev;
4098 } else if (augment->target->child) {
4099 augment->target->child->prev = elem->prev;
4100 }
4101
4102 /* update augment children themselves */
4103 elem->prev = last;
4104 last->next = NULL;
4105 }
4106
Radek Krejci0ec51da2016-12-14 16:42:03 +01004107 /* augment->target still keeps the resolved target, but for lys_augment_free()
4108 * we have to keep information that this augment is not applied to free its data */
4109 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01004110}
4111
Radek Krejci30bfcd22017-01-27 16:54:48 +01004112/*
4113 * @param[in] module - the module where the deviation is defined
4114 */
4115static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004116lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004117{
Radek Krejcic9d78692017-08-24 17:17:18 +02004118 int ret, reapply = 0;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004119 char *parent_path;
4120 struct lys_node *target = NULL, *parent;
Michal Vasko50576712017-07-28 12:28:33 +02004121 struct ly_set *set;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004122
4123 if (!dev->deviate) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004124 return;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004125 }
4126
4127 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
4128 if (dev->orig_node) {
4129 /* removing not-supported deviation ... */
4130 if (strrchr(dev->target_name, '/') != dev->target_name) {
4131 /* ... from a parent */
4132
4133 /* reconnect to its previous position */
4134 parent = dev->orig_node->parent;
Michal Vaskoa1074a52018-01-03 12:18:53 +01004135 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejcic9d78692017-08-24 17:17:18 +02004136 dev->orig_node->parent = NULL;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004137 /* the original node was actually from augment, we have to get know if the augment is
4138 * applied (its module is enabled and implemented). If yes, the node will be connected
4139 * to the augment and the linkage with the target will be fixed if needed, otherwise
4140 * it will be connected only to the augment */
Radek Krejcic9d78692017-08-24 17:17:18 +02004141 if (!(parent->flags & LYS_NOTAPPLIED)) {
4142 /* start with removing augment if applied before adding nodes, we have to make sure
4143 * that everything will be connect correctly */
4144 remove_aug((struct lys_node_augment *)parent);
4145 reapply = 1;
4146 }
4147 /* connect the deviated node back into the augment */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004148 lys_node_addchild(parent, NULL, dev->orig_node);
Radek Krejcic9d78692017-08-24 17:17:18 +02004149 if (reapply) {
Radek Krejci30bfcd22017-01-27 16:54:48 +01004150 /* augment is supposed to be applied, so fix pointers in target and the status of the original node */
Michal Vasko823fbe02017-12-14 11:01:40 +01004151 parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */
4152 apply_aug((struct lys_node_augment *)parent, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004153 }
Michal Vaskoa1074a52018-01-03 12:18:53 +01004154 } else if (parent && (parent->nodetype == LYS_USES)) {
4155 /* uses child */
4156 lys_node_addchild(parent, NULL, dev->orig_node);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004157 } else {
4158 /* non-augment, non-toplevel */
4159 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
Michal Vasko50576712017-07-28 12:28:33 +02004160 ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004161 free(parent_path);
Michal Vasko50576712017-07-28 12:28:33 +02004162 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004163 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004164 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004165 return;
4166 }
Michal Vasko50576712017-07-28 12:28:33 +02004167 target = set->set.s[0];
4168 ly_set_free(set);
4169
Radek Krejci30bfcd22017-01-27 16:54:48 +01004170 lys_node_addchild(target, NULL, dev->orig_node);
4171 }
4172 } else {
4173 /* ... from top-level data */
4174 lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node);
4175 }
4176
4177 dev->orig_node = NULL;
4178 } else {
4179 /* adding not-supported deviation */
Michal Vasko50576712017-07-28 12:28:33 +02004180 ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
4181 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004182 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004183 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004184 return;
4185 }
Michal Vasko50576712017-07-28 12:28:33 +02004186 target = set->set.s[0];
4187 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004188
4189 /* unlink and store the original node */
4190 parent = target->parent;
4191 lys_node_unlink(target);
Michal Vaskoa1074a52018-01-03 12:18:53 +01004192 if (parent && (parent->nodetype & (LYS_AUGMENT | LYS_USES))) {
Radek Krejci30bfcd22017-01-27 16:54:48 +01004193 /* hack for augment, because when the original will be sometime reconnected back, we actually need
4194 * to reconnect it to both - the augment and its target (which is deduced from the deviations target
4195 * path), so we need to remember the augment as an addition */
Michal Vaskoa1074a52018-01-03 12:18:53 +01004196 /* we also need to remember the parent uses so that we connect it back to it when switching deviation state */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004197 target->parent = parent;
4198 }
4199 dev->orig_node = target;
4200 }
4201 } else {
Michal Vasko50576712017-07-28 12:28:33 +02004202 ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
4203 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004204 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004205 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004206 return;
4207 }
Michal Vasko50576712017-07-28 12:28:33 +02004208 target = set->set.s[0];
4209 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004210
4211 lys_node_switch(target, dev->orig_node);
4212 dev->orig_node = target;
4213 }
4214}
4215
4216/* temporarily removes or applies deviations, updates module deviation flag accordingly */
4217void
4218lys_switch_deviations(struct lys_module *module)
4219{
4220 uint32_t i = 0, j;
4221 const struct lys_module *mod;
4222 const char *ptr;
Michal Vasko44ab1462017-05-18 13:18:36 +02004223 struct unres_schema *unres;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004224
4225 if (module->deviated) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004226 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004227 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Michal Vasko44ab1462017-05-18 13:18:36 +02004228
Radek Krejci30bfcd22017-01-27 16:54:48 +01004229 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
4230 if (mod == module) {
4231 continue;
4232 }
4233
4234 for (j = 0; j < mod->deviation_size; ++j) {
4235 ptr = strstr(mod->deviation[j].target_name, module->name);
4236 if (ptr && ptr[strlen(module->name)] == ':') {
Michal Vasko44ab1462017-05-18 13:18:36 +02004237 lys_switch_deviation(&mod->deviation[j], mod, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004238 }
4239 }
4240 }
4241
4242 if (module->deviated == 2) {
4243 module->deviated = 1;
4244 } else {
4245 module->deviated = 2;
4246 }
Radek Krejci29eac3d2017-06-01 16:50:02 +02004247 for (j = 0; j < module->inc_size; j++) {
4248 if (module->inc[j].submodule->deviated) {
4249 module->inc[j].submodule->deviated = module->deviated;
4250 }
4251 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004252
4253 if (unres->count) {
4254 resolve_unres_schema(module, unres);
4255 }
4256 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004257 }
4258}
4259
4260static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004261apply_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004262{
Michal Vasko44ab1462017-05-18 13:18:36 +02004263 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004264
4265 assert(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004266 lys_node_module(dev->orig_node)->deviated = 1; /* main module */
4267 dev->orig_node->module->deviated = 1; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004268}
4269
4270static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004271remove_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004272{
4273 uint32_t idx = 0, j;
4274 const struct lys_module *mod;
Radek Krejci29eac3d2017-06-01 16:50:02 +02004275 struct lys_module *target_mod, *target_submod;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004276 const char *ptr;
4277
4278 if (dev->orig_node) {
4279 target_mod = lys_node_module(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004280 target_submod = dev->orig_node->module;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004281 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004282 LOGINT(module->ctx);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004283 return;
4284 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004285 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004286
4287 /* clear the deviation flag if possible */
4288 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
4289 if ((mod == module) || (mod == target_mod)) {
4290 continue;
4291 }
4292
4293 for (j = 0; j < mod->deviation_size; ++j) {
4294 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
4295 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
4296 /* some other module deviation targets the inspected module, flag remains */
4297 break;
4298 }
4299 }
4300
4301 if (j < mod->deviation_size) {
4302 break;
4303 }
4304 }
4305
4306 if (!mod) {
Radek Krejci29eac3d2017-06-01 16:50:02 +02004307 target_mod->deviated = 0; /* main module */
4308 target_submod->deviated = 0; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004309 }
4310}
4311
4312void
4313lys_sub_module_apply_devs_augs(struct lys_module *module)
4314{
4315 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004316 struct unres_schema *unres;
4317
4318 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004319 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Radek Krejci30bfcd22017-01-27 16:54:48 +01004320
4321 /* remove applied deviations */
4322 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004323 apply_dev(&module->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004324 }
4325 /* remove applied augments */
4326 for (u = 0; u < module->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004327 apply_aug(&module->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004328 }
4329
4330 /* remove deviation and augments defined in submodules */
4331 for (v = 0; v < module->inc_size; ++v) {
4332 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004333 apply_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004334 }
4335
4336 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004337 apply_aug(&module->inc[v].submodule->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004338 }
4339 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004340
4341 if (unres->count) {
4342 resolve_unres_schema(module, unres);
4343 }
4344 /* nothing else left to do even if something is not resolved */
4345 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004346}
4347
Radek Krejcib2541a32016-12-12 16:45:57 +01004348void
4349lys_sub_module_remove_devs_augs(struct lys_module *module)
4350{
4351 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004352 struct unres_schema *unres;
4353
4354 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004355 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Radek Krejcib2541a32016-12-12 16:45:57 +01004356
4357 /* remove applied deviations */
4358 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004359 remove_dev(&module->deviation[u], module, unres);
Radek Krejcib2541a32016-12-12 16:45:57 +01004360 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004361 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01004362 for (u = 0; u < module->augment_size; ++u) {
4363 remove_aug(&module->augment[u]);
4364 }
4365
4366 /* remove deviation and augments defined in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004367 for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) {
Radek Krejcib2541a32016-12-12 16:45:57 +01004368 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004369 remove_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejcidbc15262016-06-16 14:58:29 +02004370 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004371
Radek Krejcib2541a32016-12-12 16:45:57 +01004372 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4373 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004374 }
4375 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004376
4377 if (unres->count) {
4378 resolve_unres_schema(module, unres);
4379 }
4380 /* nothing else left to do even if something is not resolved */
4381 unres_schema_free(module, &unres, 1);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004382}
4383
Radek Krejci27fe55e2016-09-13 17:13:35 +02004384static int
4385lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
4386{
4387 struct lys_node *root, *next, *node;
Radek Krejci9e6af732017-04-27 14:40:25 +02004388 uint16_t i, j;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004389
4390 for (i = 0; i < module->augment_size; i++) {
4391 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004392 assert(module->augment[i].target);
4393 if (apply_aug(&module->augment[i], unres)) {
4394 return EXIT_FAILURE;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004395 }
4396 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004397
4398 /* identities */
4399 for (i = 0; i < module->ident_size; i++) {
4400 for (j = 0; j < module->ident[i].base_size; j++) {
4401 resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]);
4402 }
4403 }
4404
Radek Krejci27fe55e2016-09-13 17:13:35 +02004405 LY_TREE_FOR(module->data, root) {
4406 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
4407 LY_TREE_DFS_BEGIN(root, next, node) {
4408 if (node->nodetype == LYS_GROUPING) {
4409 goto nextsibling;
4410 }
4411 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4412 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
4413 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
4414 UNRES_TYPE_LEAFREF, node) == -1) {
4415 return EXIT_FAILURE;
4416 }
4417 }
4418 }
4419
4420 /* modified LY_TREE_DFS_END */
4421 next = node->child;
4422 /* child exception for leafs, leaflists and anyxml without children */
4423 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4424 next = NULL;
4425 }
4426 if (!next) {
4427nextsibling:
4428 /* no children */
4429 if (node == root) {
4430 /* we are done, root has no children */
4431 break;
4432 }
4433 /* try siblings */
4434 next = node->next;
4435 }
4436 while (!next) {
4437 /* parent is already processed, go to its sibling */
4438 node = lys_parent(node);
4439 /* no siblings, go back through parents */
4440 if (lys_parent(node) == lys_parent(root)) {
4441 /* we are done, no next element to process */
4442 break;
4443 }
4444 next = node->next;
4445 }
4446 }
4447 }
4448
4449 return EXIT_SUCCESS;
4450}
4451
4452API int
4453lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02004454{
4455 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004456 struct unres_schema *unres;
Radek Krejci9e6af732017-04-27 14:40:25 +02004457 int i, j, k, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02004458
Radek Krejci27fe55e2016-09-13 17:13:35 +02004459 if (!module) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004460 LOGARG;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004461 return EXIT_FAILURE;
4462 }
4463
4464 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004465
4466 if (module->disabled) {
4467 disabled = 1;
4468 lys_set_enabled(module);
4469 }
4470
Michal Vasko26055752016-05-03 11:36:31 +02004471 if (module->implemented) {
4472 return EXIT_SUCCESS;
4473 }
4474
4475 ctx = module->ctx;
4476
4477 for (i = 0; i < ctx->models.used; ++i) {
4478 if (module == ctx->models.list[i]) {
4479 continue;
4480 }
4481
4482 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004483 LOGERR(ctx, LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004484 if (disabled) {
4485 /* set it back disabled */
4486 lys_set_disabled(module);
4487 }
Michal Vasko26055752016-05-03 11:36:31 +02004488 return EXIT_FAILURE;
4489 }
4490 }
4491
Radek Krejci27fe55e2016-09-13 17:13:35 +02004492 unres = calloc(1, sizeof *unres);
4493 if (!unres) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004494 LOGMEM(ctx);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004495 if (disabled) {
4496 /* set it back disabled */
4497 lys_set_disabled(module);
4498 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004499 return EXIT_FAILURE;
4500 }
4501 /* recursively make the module implemented */
4502 ((struct lys_module *)module)->implemented = 1;
4503 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
4504 goto error;
4505 }
4506 /* process augments in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004507 for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004508 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
4509 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004510 assert(module->inc[i].submodule->augment[j].target);
4511 if (apply_aug(&module->inc[i].submodule->augment[j], unres)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004512 goto error;
4513 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004514 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004515
4516 /* identities */
4517 for (j = 0; j < module->inc[i].submodule->ident_size; j++) {
4518 for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) {
4519 resolve_identity_backlink_update(&module->inc[i].submodule->ident[j],
4520 module->inc[i].submodule->ident[j].base[k]);
4521 }
4522 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004523 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004524 /* try again resolve augments in other modules possibly augmenting this one,
4525 * since we have just enabled it
4526 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004527 /* resolve rest of unres items */
4528 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
4529 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004530 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004531 unres_schema_free(NULL, &unres, 0);
Michal Vasko26055752016-05-03 11:36:31 +02004532
Radek Krejci29eac3d2017-06-01 16:50:02 +02004533 /* reflect implemented flag in submodules */
4534 for (i = 0; i < module->inc_size; i++) {
4535 module->inc[i].submodule->implemented = 1;
4536 }
4537
Michal Vaskobe136f62017-09-21 12:08:39 +02004538 LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""),
4539 (module->rev_size ? module->rev[0].date : ""));
Michal Vasko26055752016-05-03 11:36:31 +02004540 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004541
4542error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004543 if (disabled) {
4544 /* set it back disabled */
4545 lys_set_disabled(module);
4546 }
4547
Radek Krejci27fe55e2016-09-13 17:13:35 +02004548 ((struct lys_module *)module)->implemented = 0;
Michal Vasko44ab1462017-05-18 13:18:36 +02004549 unres_schema_free((struct lys_module *)module, &unres, 1);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004550 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004551}
4552
4553void
4554lys_submodule_module_data_free(struct lys_submodule *submodule)
4555{
4556 struct lys_node *next, *elem;
4557
4558 /* remove parsed data */
4559 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
4560 if (elem->module == (struct lys_module *)submodule) {
4561 lys_node_free(elem, NULL, 0);
4562 }
4563 }
4564}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02004565
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004566API char *
Michal Vasko395b0a02018-01-22 09:36:20 +01004567lys_path(const struct lys_node *node, int options)
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004568{
Michal Vasko53b7da02018-02-13 15:28:42 +01004569 char *buf = NULL;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004570
4571 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004572 LOGARG;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004573 return NULL;
4574 }
4575
Michal Vasko53b7da02018-02-13 15:28:42 +01004576 if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options)) {
Michal Vasko59631dd2017-10-02 11:56:11 +02004577 return NULL;
4578 }
4579
Michal Vasko53b7da02018-02-13 15:28:42 +01004580 return buf;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004581}
Radek Krejci9ad23f42016-10-31 15:46:52 +01004582
Michal Vasko50576712017-07-28 12:28:33 +02004583API char *
4584lys_data_path(const struct lys_node *node)
4585{
Michal Vasko53b7da02018-02-13 15:28:42 +01004586 char *result = NULL, buf[1024];
PavolVicanb28bbff2018-02-21 00:44:02 +01004587 const char *separator, *name;
Michal Vasko50576712017-07-28 12:28:33 +02004588 int i, used;
4589 struct ly_set *set;
4590 const struct lys_module *prev_mod;
4591
4592 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004593 LOGARG;
Michal Vasko50576712017-07-28 12:28:33 +02004594 return NULL;
4595 }
4596
Michal Vasko50576712017-07-28 12:28:33 +02004597 set = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01004598 LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup);
Michal Vasko50576712017-07-28 12:28:33 +02004599
4600 while (node) {
4601 ly_set_add(set, (void *)node, 0);
4602 do {
4603 node = lys_parent(node);
4604 } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
4605 }
4606
4607 prev_mod = NULL;
4608 used = 0;
4609 for (i = set->number - 1; i > -1; --i) {
4610 node = set->set.s[i];
PavolVicanb28bbff2018-02-21 00:44:02 +01004611 if (node->nodetype == LYS_EXT) {
4612 if (strcmp(((struct lys_ext_instance *)node)->def->name, "yang-data")) {
4613 continue;
4614 }
4615 name = ((struct lys_ext_instance *)node)->arg_value;
4616 separator = ":#";
4617 } else {
4618 name = node->name;
4619 separator = ":";
4620 }
Michal Vasko50576712017-07-28 12:28:33 +02004621 used += sprintf(buf + used, "/%s%s%s", (lys_node_module(node) == prev_mod ? "" : lys_node_module(node)->name),
PavolVicanb28bbff2018-02-21 00:44:02 +01004622 (lys_node_module(node) == prev_mod ? "" : separator), name);
Michal Vasko50576712017-07-28 12:28:33 +02004623 prev_mod = lys_node_module(node);
4624 }
4625
4626 result = strdup(buf);
Michal Vasko53b7da02018-02-13 15:28:42 +01004627 LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup);
Michal Vasko50576712017-07-28 12:28:33 +02004628
Michal Vasko53b7da02018-02-13 15:28:42 +01004629cleanup:
Michal Vasko50576712017-07-28 12:28:33 +02004630 ly_set_free(set);
Michal Vasko50576712017-07-28 12:28:33 +02004631 return result;
4632}
4633
Michal Vaskobb520442017-05-23 10:55:18 +02004634struct lys_node_augment *
4635lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target)
4636{
4637 int i, j, last_found;
4638
Michal Vaskob6906872018-03-12 11:35:09 +01004639 assert(mod && aug_target);
4640
Michal Vaskobb520442017-05-23 10:55:18 +02004641 if (!last) {
4642 last_found = 1;
4643 } else {
4644 last_found = 0;
4645 }
4646
4647 /* search module augments */
4648 for (i = 0; i < mod->augment_size; ++i) {
4649 if (!mod->augment[i].target) {
4650 /* still unresolved, skip */
4651 continue;
4652 }
4653
4654 if (mod->augment[i].target == aug_target) {
4655 if (last_found) {
4656 /* next match after last */
4657 return &mod->augment[i];
4658 }
4659
4660 if (&mod->augment[i] == last) {
4661 last_found = 1;
4662 }
4663 }
4664 }
4665
4666 /* search submodule augments */
4667 for (i = 0; i < mod->inc_size; ++i) {
4668 for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) {
4669 if (!mod->inc[i].submodule->augment[j].target) {
4670 continue;
4671 }
4672
4673 if (mod->inc[i].submodule->augment[j].target == aug_target) {
4674 if (last_found) {
4675 /* next match after last */
4676 return &mod->inc[i].submodule->augment[j];
4677 }
4678
4679 if (&mod->inc[i].submodule->augment[j] == last) {
4680 last_found = 1;
4681 }
4682 }
4683 }
4684 }
4685
4686 return NULL;
4687}
4688
Michal Vasko50576712017-07-28 12:28:33 +02004689API struct ly_set *
4690lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path)
4691{
4692 struct ly_set *ret;
4693 int rc;
4694
4695 if ((!cur_module && !cur_node) || !path) {
4696 return NULL;
4697 }
4698
4699 rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1);
4700 if (rc == -1) {
4701 return NULL;
4702 }
4703
4704 return ret;
4705}
4706
Radek Krejci8d6b7422017-02-03 14:42:13 +01004707static void
4708lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt)
4709{
4710 struct lyext_substmt *info;
Radek Krejcib71243e2017-02-08 16:20:08 +01004711 const char **str, ***a;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004712 int c;
4713
4714 str = lys_ext_complex_get_substmt(stmt, ext, &info);
4715 if (!str || !(*str)) {
4716 return;
4717 }
4718 if (info->cardinality >= LY_STMT_CARD_SOME) {
4719 /* we have array */
Radek Krejcib71243e2017-02-08 16:20:08 +01004720 a = (const char ***)str;
Radek Krejci56c80412017-02-09 10:44:16 +01004721 for (str = (*(const char ***)str), c = 0; str[c]; c++) {
4722 lydict_remove(ctx, str[c]);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004723 }
Radek Krejci56c80412017-02-09 10:44:16 +01004724 free(a[0]);
4725 if (stmt == LY_STMT_BELONGSTO) {
4726 for (str = a[1], c = 0; str[c]; c++) {
4727 lydict_remove(ctx, str[c]);
4728 }
4729 free(a[1]);
PavolVican99c70722017-02-18 17:25:52 +01004730 } else if (stmt == LY_STMT_ARGUMENT) {
4731 free(a[1]);
Radek Krejci56c80412017-02-09 10:44:16 +01004732 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004733 } else {
Radek Krejci56c80412017-02-09 10:44:16 +01004734 lydict_remove(ctx, str[0]);
4735 if (stmt == LY_STMT_BELONGSTO) {
4736 lydict_remove(ctx, str[1]);
4737 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004738 }
4739}
4740void
Radek Krejci5138e9f2017-04-12 13:10:46 +02004741lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size,
4742 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci8d6b7422017-02-03 14:42:13 +01004743{
Radek Krejcif8d05c22017-02-10 15:33:35 +01004744 unsigned int i, j, k;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004745 struct lyext_substmt *substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004746 void **pp, **start;
Radek Krejcif95b6292017-02-13 15:57:37 +01004747 struct lys_node *siter, *snext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004748
4749#define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \
Radek Krejcib84686f2017-02-09 16:04:55 +01004750 pp = lys_ext_complex_get_substmt(STMT, (struct lys_ext_instance_complex *)e[i], NULL); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004751 if (!pp || !(*pp)) { break; } \
Radek Krejcib84686f2017-02-09 16:04:55 +01004752 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004753 for (start = pp = *pp; *pp; pp++) { \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004754 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004755 if (FREE) { free(*pp); } \
4756 } \
4757 free(start); \
4758 } else { /* single item */ \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004759 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004760 if (FREE) { free(*pp); } \
4761 }
4762
PavolVican9d61d402018-02-05 15:52:48 +01004763 if (!size || !e) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004764 return;
4765 }
4766
4767 for (i = 0; i < size; i++) {
4768 if (!e[i]) {
4769 continue;
4770 }
4771
4772 if (e[i]->flags & (LYEXT_OPT_INHERIT)) {
4773 /* no free, this is just a shadow copy of the original extension instance */
4774 } else {
4775 if (e[i]->flags & (LYEXT_OPT_YANG)) {
4776 free(e[i]->def); /* remove name of instance extension */
PavolVican19dc6152017-02-06 12:04:15 +01004777 e[i]->def = NULL;
PavolVicandb0e8172017-02-20 00:46:09 +01004778 yang_free_ext_data((struct yang_ext_substmt *)e[i]->parent); /* remove backup part of yang file */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004779 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02004780 /* remove private object */
4781 if (e[i]->priv && private_destructor) {
4782 private_destructor((struct lys_node*)e[i], e[i]->priv);
4783 }
4784 lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size, private_destructor);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004785 lydict_remove(ctx, e[i]->arg_value);
4786 }
4787
fanchanghu8d86f6b2017-06-10 12:49:54 +08004788 if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX
4789 && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) {
Radek Krejcifebdad72017-02-06 11:35:51 +01004790 substmt = ((struct lys_ext_instance_complex *)e[i])->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004791 for (j = 0; substmt[j].stmt; j++) {
4792 switch(substmt[j].stmt) {
4793 case LY_STMT_DESCRIPTION:
4794 case LY_STMT_REFERENCE:
4795 case LY_STMT_UNITS:
Radek Krejcib71243e2017-02-08 16:20:08 +01004796 case LY_STMT_ARGUMENT:
4797 case LY_STMT_DEFAULT:
4798 case LY_STMT_ERRTAG:
4799 case LY_STMT_ERRMSG:
4800 case LY_STMT_PREFIX:
4801 case LY_STMT_NAMESPACE:
4802 case LY_STMT_PRESENCE:
4803 case LY_STMT_REVISIONDATE:
4804 case LY_STMT_KEY:
4805 case LY_STMT_BASE:
4806 case LY_STMT_BELONGSTO:
4807 case LY_STMT_CONTACT:
4808 case LY_STMT_ORGANIZATION:
4809 case LY_STMT_PATH:
Radek Krejci8d6b7422017-02-03 14:42:13 +01004810 lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt);
4811 break;
4812 case LY_STMT_TYPE:
4813 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1);
4814 break;
Radek Krejci63fc0962017-02-15 13:20:18 +01004815 case LY_STMT_TYPEDEF:
4816 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1);
4817 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004818 case LY_STMT_IFFEATURE:
Frank Rimplerc4db1c72017-09-12 12:56:39 +00004819 EXTCOMPLEX_FREE_STRUCT(LY_STMT_IFFEATURE, struct lys_iffeature, lys_iffeature_free, 0, 1, 0);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004820 break;
Radek Krejci5496fae2017-02-10 13:26:48 +01004821 case LY_STMT_MAX:
4822 case LY_STMT_MIN:
4823 case LY_STMT_POSITION:
PavolVican2ed9f4e2017-02-16 00:08:45 +01004824 case LY_STMT_VALUE:
Radek Krejcif8d05c22017-02-10 15:33:35 +01004825 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
Radek Krejci716cd7a2017-02-15 12:23:41 +01004826 if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) {
Radek Krejcif8d05c22017-02-10 15:33:35 +01004827 for(k = 0; ((uint32_t**)(*pp))[k]; k++) {
4828 free(((uint32_t**)(*pp))[k]);
4829 }
4830 }
4831 free(*pp);
4832 break;
4833 case LY_STMT_DIGITS:
Radek Krejcib84686f2017-02-09 16:04:55 +01004834 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4835 /* free the array */
4836 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4837 free(*pp);
4838 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004839 break;
Radek Krejci37f9ba32017-02-10 16:50:35 +01004840 case LY_STMT_MODULE:
4841 /* modules are part of the context, so they will be freed there */
4842 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4843 /* free the array */
4844 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4845 free(*pp);
4846 }
4847 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01004848 case LY_STMT_ACTION:
Radek Krejcib31762b2017-02-15 10:48:42 +01004849 case LY_STMT_ANYDATA:
4850 case LY_STMT_ANYXML:
4851 case LY_STMT_CASE:
4852 case LY_STMT_CHOICE:
4853 case LY_STMT_CONTAINER:
4854 case LY_STMT_GROUPING:
4855 case LY_STMT_INPUT:
4856 case LY_STMT_LEAF:
4857 case LY_STMT_LEAFLIST:
4858 case LY_STMT_LIST:
4859 case LY_STMT_NOTIFICATION:
4860 case LY_STMT_OUTPUT:
4861 case LY_STMT_RPC:
4862 case LY_STMT_USES:
Radek Krejcif95b6292017-02-13 15:57:37 +01004863 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4864 LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) {
4865 lys_node_free(siter, NULL, 0);
4866 }
Radek Krejcib31762b2017-02-15 10:48:42 +01004867 *pp = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01004868 break;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01004869 case LY_STMT_UNIQUE:
4870 pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL);
4871 if (!pp || !(*pp)) {
4872 break;
4873 }
4874 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4875 for (start = pp = *pp; *pp; pp++) {
4876 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4877 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4878 }
4879 free((*(struct lys_unique**)pp)->expr);
4880 free(*pp);
4881 }
4882 free(start);
4883 } else { /* single item */
4884 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4885 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4886 }
4887 free((*(struct lys_unique**)pp)->expr);
4888 free(*pp);
4889 }
4890 break;
Radek Krejciaa9c5202017-02-15 16:10:14 +01004891 case LY_STMT_LENGTH:
4892 case LY_STMT_MUST:
4893 case LY_STMT_PATTERN:
4894 case LY_STMT_RANGE:
4895 EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1);
4896 break;
Radek Krejcic5cc5302017-02-16 10:07:46 +01004897 case LY_STMT_WHEN:
4898 EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0);
4899 break;
Radek Krejci7417a082017-02-16 11:07:59 +01004900 case LY_STMT_REVISION:
4901 pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL);
4902 if (!pp || !(*pp)) {
4903 break;
4904 }
4905 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4906 for (start = pp = *pp; *pp; pp++) {
4907 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4908 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4909 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004910 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004911 free(*pp);
4912 }
4913 free(start);
4914 } else { /* single item */
4915 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4916 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4917 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004918 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004919 free(*pp);
4920 }
4921 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004922 default:
Radek Krejcib84686f2017-02-09 16:04:55 +01004923 /* nothing to free */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004924 break;
4925 }
4926 }
4927 }
4928
4929 free(e[i]);
4930 }
4931 free(e);
4932
4933#undef EXTCOMPLEX_FREE_STRUCT
4934}