blob: 62e9c0b51b5392d48dbaad2f64bf0e96b3b6091a [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{
Radek Krejcif95b6292017-02-13 15:57:37 +0100393 struct lys_node *parent, *first, **pp;
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 Vasko6f929da2015-10-02 16:23:25 +0200486 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200487 continue;
488 }
489
490 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
491 if (!stop) {
492 stop = par_iter;
493 } else if (iter == stop) {
494 break;
495 }
496 if (iter->nodetype != LYS_GROUPING) {
497 continue;
498 }
499
Radek Krejcif8426a72015-10-31 23:14:03 +0100500 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200501 return (struct lys_node_grp *)iter;
502 }
503 }
504 }
505
Michal Vasko563ef092015-09-04 13:17:23 +0200506 return NULL;
507}
508
Radek Krejci10c760e2015-08-14 14:45:43 +0200509/*
510 * get next grouping in the root's subtree, in the
511 * first call, tha last is NULL
512 */
513static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200514lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200515{
Radek Krejci10c760e2015-08-14 14:45:43 +0200516 struct lys_node *last = (struct lys_node *)lastgrp;
517 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200518
Radek Krejci10c760e2015-08-14 14:45:43 +0200519 assert(root);
520
521 if (!last) {
522 last = root;
523 }
524
525 while (1) {
526 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
527 next = last->child;
528 } else {
529 next = NULL;
530 }
531 if (!next) {
532 if (last == root) {
533 /* we are done */
534 return NULL;
535 }
536
537 /* no children, go to siblings */
538 next = last->next;
539 }
540 while (!next) {
541 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100542 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200543 /* we are done */
544 return NULL;
545 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200546 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100547 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200548 }
549
550 if (next->nodetype == LYS_GROUPING) {
551 return (struct lys_node_grp *)next;
552 }
553
554 last = next;
555 }
556}
557
Michal Vasko0d343d12015-08-24 14:57:36 +0200558/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200559int
Radek Krejci07911992015-08-14 15:13:31 +0200560lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
561{
Michal Vasko563ef092015-09-04 13:17:23 +0200562 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200563 struct lys_node_grp *grp;
Radek Krejcif95b6292017-02-13 15:57:37 +0100564 int down, up;
Radek Krejci07911992015-08-14 15:13:31 +0200565
566 assert(node);
567
568 if (!parent) {
569 assert(module);
570 } else {
571 module = parent->module;
572 }
Radek Krejci115fa882017-03-01 16:15:07 +0100573 module = lys_main_module(module);
Radek Krejci07911992015-08-14 15:13:31 +0200574
575 switch (node->nodetype) {
576 case LYS_GROUPING:
577 /* 6.2.1, rule 6 */
578 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100579 start = *lys_child(parent, LYS_GROUPING);
580 if (!start) {
Radek Krejci07911992015-08-14 15:13:31 +0200581 down = 0;
582 start = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100583 } else {
584 down = 1;
585 }
586 if (parent->nodetype == LYS_EXT) {
587 up = 0;
588 } else {
589 up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200590 }
591 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100592 down = up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200593 start = module->data;
594 }
595 /* go up */
Radek Krejcif95b6292017-02-13 15:57:37 +0100596 if (up && lys_find_grouping_up(node->name, start)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100597 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200598 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200599 }
600 /* go down, because grouping can be defined after e.g. container in which is collision */
601 if (down) {
602 for (iter = start, stop = NULL; iter; iter = iter->prev) {
603 if (!stop) {
604 stop = start;
605 } else if (iter == stop) {
606 break;
607 }
608 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
609 continue;
610 }
611
612 grp = NULL;
613 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100614 if (ly_strequal(node->name, grp->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100615 LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200616 return EXIT_FAILURE;
617 }
618 }
619 }
620 }
621 break;
622 case LYS_LEAF:
623 case LYS_LEAFLIST:
624 case LYS_LIST:
625 case LYS_CONTAINER:
626 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200627 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200628 /* 6.2.1, rule 7 */
629 if (parent) {
630 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200631 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
632 if (iter->nodetype == LYS_AUGMENT) {
633 if (((struct lys_node_augment *)iter)->target) {
634 /* augment is resolved, go up */
635 iter = ((struct lys_node_augment *)iter)->target;
636 continue;
637 }
638 /* augment is not resolved, this is the final parent */
639 break;
640 }
Radek Krejci07911992015-08-14 15:13:31 +0200641 iter = iter->parent;
642 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200643
Radek Krejci07911992015-08-14 15:13:31 +0200644 if (!iter) {
645 stop = NULL;
646 iter = module->data;
Radek Krejcif95b6292017-02-13 15:57:37 +0100647 } else if (iter->nodetype == LYS_EXT) {
648 stop = iter;
PavolVican9d61d402018-02-05 15:52:48 +0100649 iter = (struct lys_node *)lys_child(iter, node->nodetype);
650 if (iter) {
651 iter = *(struct lys_node **)iter;
652 }
Radek Krejci07911992015-08-14 15:13:31 +0200653 } else {
654 stop = iter;
655 iter = iter->child;
656 }
657 } else {
658 stop = NULL;
659 iter = module->data;
660 }
661 while (iter) {
662 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
663 iter = iter->child;
664 continue;
665 }
666
Radek Krejcibf2abff2016-08-23 15:51:52 +0200667 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100668 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100669 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200670 return EXIT_FAILURE;
671 }
672 }
673
674 /* special case for choice - we must check the choice's name as
675 * well as the names of nodes under the choice
676 */
677 if (iter->nodetype == LYS_CHOICE) {
678 iter = iter->child;
679 continue;
680 }
681
682 /* go to siblings */
683 if (!iter->next) {
684 /* no sibling, go to parent's sibling */
685 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200686 /* for parent LYS_AUGMENT */
687 if (iter->parent == stop) {
688 iter = stop;
689 break;
690 }
691 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200692 if (iter && iter->next) {
693 break;
694 }
695 } while (iter != stop);
696
697 if (iter == stop) {
698 break;
699 }
700 }
701 iter = iter->next;
702 }
703 break;
704 case LYS_CASE:
705 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100706 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100707 start = *lys_child(parent, LYS_CASE);
Radek Krejcic071c542016-01-27 14:57:51 +0100708 } else {
709 start = module->data;
710 }
711
712 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200713 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200714 continue;
715 }
716
Radek Krejci749190d2016-02-18 16:26:25 +0100717 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100718 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200719 return EXIT_FAILURE;
720 }
721 }
722 break;
723 default:
724 /* no check needed */
725 break;
726 }
727
728 return EXIT_SUCCESS;
729}
730
Michal Vasko0d343d12015-08-24 14:57:36 +0200731/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200732int
Radek Krejci10c760e2015-08-14 14:45:43 +0200733lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
734{
Michal Vasko53b7da02018-02-13 15:28:42 +0100735 struct ly_ctx *ctx = child->module->ctx;
Radek Krejcic9d78692017-08-24 17:17:18 +0200736 struct lys_node *iter, **pchild;
Radek Krejci41a349b2016-10-24 19:21:59 +0200737 struct lys_node_inout *in, *out, *inout;
Radek Krejci744c2d42017-03-26 13:30:00 -0500738 struct lys_node_case *c;
739 int type, shortcase = 0;
Radek Krejcif95b6292017-02-13 15:57:37 +0100740 void *p;
741 struct lyext_substmt *info = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200742
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200743 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200744
Radek Krejci10c760e2015-08-14 14:45:43 +0200745 if (parent) {
746 type = parent->nodetype;
747 module = parent->module;
748 } else {
749 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100750 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200751 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200752 }
753
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200754 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200755 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200756 case LYS_CONTAINER:
757 case LYS_LIST:
758 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100759 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200760 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200761 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200762 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100763 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200764 return EXIT_FAILURE;
765 }
766 break;
Radek Krejci76512572015-08-04 09:47:08 +0200767 case LYS_INPUT:
768 case LYS_OUTPUT:
769 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200770 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200771 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200772 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100773 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200774 return EXIT_FAILURE;
775 }
776 break;
Radek Krejci76512572015-08-04 09:47:08 +0200777 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200779 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100780 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200781 return EXIT_FAILURE;
782 }
Radek Krejci744c2d42017-03-26 13:30:00 -0500783 if (child->nodetype != LYS_CASE) {
784 shortcase = 1;
785 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200786 break;
Radek Krejci76512572015-08-04 09:47:08 +0200787 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200788 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200789 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100790 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200791 return EXIT_FAILURE;
792 }
793 break;
Radek Krejci76512572015-08-04 09:47:08 +0200794 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200795 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200796 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100797 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200798 return EXIT_FAILURE;
799 }
800 break;
Radek Krejci76512572015-08-04 09:47:08 +0200801 case LYS_LEAF:
802 case LYS_LEAFLIST:
803 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200804 case LYS_ANYDATA:
Michal Vasko53b7da02018-02-13 15:28:42 +0100805 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
806 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100807 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200808 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200809 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200810 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200811 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskodb017262017-01-24 13:10:04 +0100812 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100813 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200814 return EXIT_FAILURE;
815 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200816 break;
817 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200818 /* top level */
819 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200820 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200821 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100822 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200823 return EXIT_FAILURE;
824 }
Radek Krejcif95b6292017-02-13 15:57:37 +0100825 break;
826 case LYS_EXT:
827 /* plugin-defined */
828 p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info);
829 if (!p) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100830 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype),
Radek Krejcif95b6292017-02-13 15:57:37 +0100831 ((struct lys_ext_instance_complex*)parent)->def->name);
832 return EXIT_FAILURE;
833 }
834 /* TODO check cardinality */
Radek Krejcic071c542016-01-27 14:57:51 +0100835 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200836 }
837
838 /* check identifier uniqueness */
Michal Vasko15a43372017-09-25 14:12:42 +0200839 if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && lys_check_id(child, parent, module)) {
Radek Krejci07911992015-08-14 15:13:31 +0200840 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200841 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200842
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200843 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200844 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200845 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200846
Radek Krejcif95b6292017-02-13 15:57:37 +0100847 if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200848 /* replace the implicit input/output node */
849 if (child->nodetype == LYS_OUTPUT) {
850 inout = (struct lys_node_inout *)parent->child->next;
851 } else { /* LYS_INPUT */
852 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200853 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200854 }
855 if (inout->next) {
856 child->next = inout->next;
857 inout->next->prev = child;
858 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200859 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200860 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200861 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200862 child->prev = inout->prev;
863 if (inout->prev->next) {
864 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200865 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200866 inout->prev = (struct lys_node *)inout;
867 child->parent = parent;
868 inout->parent = NULL;
869 lys_node_free((struct lys_node *)inout, NULL, 0);
870 } else {
Radek Krejci744c2d42017-03-26 13:30:00 -0500871 if (shortcase) {
872 /* create the implicit case to allow it to serve as a target of the augments,
873 * it won't be printed, but it will be present in the tree */
874 c = calloc(1, sizeof *c);
Michal Vasko53b7da02018-02-13 15:28:42 +0100875 LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE);
Radek Krejci744c2d42017-03-26 13:30:00 -0500876 c->name = lydict_insert(module->ctx, child->name, 0);
877 c->flags = LYS_IMPLICIT;
878 c->module = module;
879 c->nodetype = LYS_CASE;
880 c->prev = (struct lys_node*)c;
881 lys_node_addchild(parent, module, (struct lys_node*)c);
882 parent = (struct lys_node*)c;
883 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200884 /* connect the child correctly */
885 if (!parent) {
886 if (module->data) {
887 module->data->prev->next = child;
888 child->prev = module->data->prev;
889 module->data->prev = child;
890 } else {
891 module->data = child;
892 }
893 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100894 pchild = lys_child(parent, child->nodetype);
895 assert(pchild);
896
Radek Krejcic9d78692017-08-24 17:17:18 +0200897 child->parent = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100898 if (!(*pchild)) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200899 /* the only/first child of the parent */
Radek Krejcif95b6292017-02-13 15:57:37 +0100900 *pchild = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200901 iter = child;
902 } else {
903 /* add a new child at the end of parent's child list */
Radek Krejcif95b6292017-02-13 15:57:37 +0100904 iter = (*pchild)->prev;
Radek Krejci41a349b2016-10-24 19:21:59 +0200905 iter->next = child;
906 child->prev = iter;
907 }
908 while (iter->next) {
909 iter = iter->next;
910 iter->parent = parent;
911 }
Radek Krejcic9d78692017-08-24 17:17:18 +0200912 (*pchild)->prev = iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200913 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200914 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200915
Michal Vaskoe022a562016-09-27 14:24:15 +0200916 /* check config value (but ignore them in groupings and augments) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100917 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); iter = iter->parent);
Michal Vaskoe022a562016-09-27 14:24:15 +0200918 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200919 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
920 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100921 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config");
922 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200923 return EXIT_FAILURE;
924 }
925 }
926
Radek Krejci41771502016-04-14 17:52:32 +0200927 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200928 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200929 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200930 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200931 /* store it only into container or list - the only data inner nodes */
932 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
933 if (iter->flags & LYS_INCL_STATUS) {
934 /* done, someone else set it already from here */
935 break;
936 }
937 /* set flag about including status data */
938 iter->flags |= LYS_INCL_STATUS;
939 }
940 }
941 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200942
943 /* create implicit input/output nodes to have available them as possible target for augment */
Radek Krejci60251232017-08-24 17:13:08 +0200944 if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200945 in = calloc(1, sizeof *in);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200946 out = calloc(1, sizeof *out);
947 if (!in || !out) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100948 LOGMEM(ctx);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200949 free(in);
950 free(out);
951 return EXIT_FAILURE;
952 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200953 in->nodetype = LYS_INPUT;
954 in->name = lydict_insert(child->module->ctx, "input", 5);
Radek Krejci41a349b2016-10-24 19:21:59 +0200955 out->nodetype = LYS_OUTPUT;
Radek Krejcia8d111f2017-05-31 13:57:37 +0200956 out->name = lydict_insert(child->module->ctx, "output", 6);
Radek Krejci41a349b2016-10-24 19:21:59 +0200957 in->module = out->module = child->module;
958 in->parent = out->parent = child;
959 in->flags = out->flags = LYS_IMPLICIT;
960 in->next = (struct lys_node *)out;
961 in->prev = (struct lys_node *)out;
962 out->prev = (struct lys_node *)in;
963 child->child = (struct lys_node *)in;
964 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200965 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200966}
967
Michal Vasko29245662017-04-18 15:56:31 +0200968const struct lys_module *
Michal Vaskofb98dc42018-01-11 13:38:28 +0100969lys_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 +0200970{
Radek Krejcia1df1682016-04-11 14:56:59 +0200971 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200972 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200973 unsigned int len;
Radek Krejcif347abc2016-06-22 10:18:47 +0200974
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975 if (!ctx || !data) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100976 LOGARG;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200977 return NULL;
978 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200979
Radek Krejcia1df1682016-04-11 14:56:59 +0200980 if (!internal && format == LYS_IN_YANG) {
981 /* enlarge data by 2 bytes for flex */
982 len = strlen(data);
983 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Michal Vasko53b7da02018-02-13 15:28:42 +0100984 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL);
Radek Krejcia1df1682016-04-11 14:56:59 +0200985 memcpy(enlarged_data, data, len);
986 enlarged_data[len] = enlarged_data[len + 1] = '\0';
987 data = enlarged_data;
988 }
989
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200990 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200991 case LYS_IN_YIN:
Michal Vaskofb98dc42018-01-11 13:38:28 +0100992 mod = yin_read_module(ctx, data, revision, implement);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200993 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200994 case LYS_IN_YANG:
Michal Vaskofb98dc42018-01-11 13:38:28 +0100995 mod = yang_read_module(ctx, data, 0, revision, implement);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100996 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200997 default:
Michal Vasko53b7da02018-02-13 15:28:42 +0100998 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200999 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001000 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001001
Radek Krejcia1df1682016-04-11 14:56:59 +02001002 free(enlarged_data);
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001003
1004 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
1005 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
1006 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
1007 * the anotation definitions available in the internal schema structure. There is another hack in schema
1008 * printers to do not print this internally added annotation. */
1009 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci5b190662017-04-13 08:56:14 +02001010 if (lyp_add_ietf_netconf_annotations(mod)) {
Michal Vasko10681e82018-01-16 14:54:16 +01001011 lys_free(mod, NULL, 1, 1);
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001012 return NULL;
1013 }
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001014 }
1015
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001016 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017}
1018
Radek Krejcia1df1682016-04-11 14:56:59 +02001019API const struct lys_module *
1020lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
1021{
Michal Vaskofb98dc42018-01-11 13:38:28 +01001022 return lys_parse_mem_(ctx, data, format, NULL, 0, 1);
Radek Krejcia1df1682016-04-11 14:56:59 +02001023}
1024
Michal Vasko5a721fd2016-02-16 12:16:48 +01001025struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001026lys_sub_parse_mem(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001027{
Michal Vasko5b998712017-01-26 10:34:06 +01001028 char *enlarged_data = NULL;
Michal Vasko5a721fd2016-02-16 12:16:48 +01001029 struct lys_submodule *submod = NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001030 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001031
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001032 assert(module);
1033 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001034
Michal Vasko5b998712017-01-26 10:34:06 +01001035 if (format == LYS_IN_YANG) {
1036 /* enlarge data by 2 bytes for flex */
1037 len = strlen(data);
1038 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001039 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL);
Michal Vasko5b998712017-01-26 10:34:06 +01001040 memcpy(enlarged_data, data, len);
1041 enlarged_data[len] = enlarged_data[len + 1] = '\0';
1042 data = enlarged_data;
1043 }
1044
Radek Krejcic071c542016-01-27 14:57:51 +01001045 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +02001046 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001048 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001049 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +01001050 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001051 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001052 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001053 submod = yang_read_submodule(module, data, 0, unres);
1054 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001055 default:
Radek Krejci90a550a2016-04-13 16:00:58 +02001056 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001057 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001058 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001059
Michal Vasko5b998712017-01-26 10:34:06 +01001060 free(enlarged_data);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001061 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001062}
1063
Michal Vasko1e62a092015-12-01 12:27:20 +01001064API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +01001065lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1066{
1067 int fd;
1068 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +02001069 const char *rev, *dot, *filename;
1070 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +01001071
1072 if (!ctx || !path) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001073 LOGARG;
Michal Vasko662610a2015-12-07 11:25:45 +01001074 return NULL;
1075 }
1076
1077 fd = open(path, O_RDONLY);
1078 if (fd == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001079 LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
Michal Vasko662610a2015-12-07 11:25:45 +01001080 return NULL;
1081 }
1082
1083 ret = lys_parse_fd(ctx, fd, format);
1084 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +01001085
Radek Krejcid80c8602016-10-25 11:56:03 +02001086 if (!ret) {
1087 /* error */
1088 return NULL;
1089 }
1090
1091 /* check that name and revision match filename */
1092 filename = strrchr(path, '/');
1093 if (!filename) {
1094 filename = path;
1095 } else {
1096 filename++;
1097 }
1098 rev = strchr(filename, '@');
1099 dot = strrchr(filename, '.');
1100
1101 /* name */
1102 len = strlen(ret->name);
1103 if (strncmp(filename, ret->name, len) ||
1104 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001105 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +02001106 }
1107 if (rev) {
1108 len = dot - ++rev;
1109 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001110 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcid80c8602016-10-25 11:56:03 +02001111 ret->rev_size ? ret->rev[0].date : "none");
1112 }
1113 }
1114
1115 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +01001116 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +01001117 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +01001118 }
1119
Michal Vasko662610a2015-12-07 11:25:45 +01001120 return ret;
1121}
1122
1123API const struct lys_module *
1124lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001125{
Michal Vaskofb98dc42018-01-11 13:38:28 +01001126 return lys_parse_fd_(ctx, fd, format, NULL, 1);
1127}
1128
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001129static void
1130lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
1131{
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001132#ifdef __APPLE__
1133 char path[MAXPATHLEN];
1134#else
Michal Vasko25601f32018-02-16 09:41:46 +01001135 int len;
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001136 char path[PATH_MAX], proc_path[32];
1137#endif
1138
1139#ifdef __APPLE__
1140 if (fcntl(fd, F_GETPATH, path) != -1) {
1141 *filename = lydict_insert(ctx, path, 0);
1142 }
1143#else
1144 /* get URI if there is /proc */
1145 sprintf(proc_path, "/proc/self/fd/%d", fd);
1146 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
1147 *filename = lydict_insert(ctx, path, len);
1148 }
1149#endif
1150}
1151
Michal Vaskofb98dc42018-01-11 13:38:28 +01001152const struct lys_module *
1153lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement)
1154{
Michal Vasko1e62a092015-12-01 12:27:20 +01001155 const struct lys_module *module;
Radek Krejci0fb11502017-01-31 16:45:42 +01001156 size_t length;
Radek Krejci63a91a92015-07-29 13:31:04 +02001157 char *addr;
1158
1159 if (!ctx || fd < 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001160 LOGARG;
Radek Krejci63a91a92015-07-29 13:31:04 +02001161 return NULL;
1162 }
1163
Michal Vasko53b7da02018-02-13 15:28:42 +01001164 if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) {
1165 LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001166 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001167 } else if (!addr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001168 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
Pavol Vicane36ea262015-11-12 11:57:47 +01001169 return NULL;
1170 }
Radek Krejci0fb11502017-01-31 16:45:42 +01001171
Michal Vaskofb98dc42018-01-11 13:38:28 +01001172 module = lys_parse_mem_(ctx, addr, format, revision, 1, implement);
Radek Krejci0fb11502017-01-31 16:45:42 +01001173 lyp_munmap(addr, length);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001174
Radek Krejcia77904e2016-02-25 16:23:45 +01001175 if (module && !module->filepath) {
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001176 lys_parse_set_filename(ctx, (const char **)&module->filepath, fd);
Radek Krejcib051f722016-02-25 15:12:21 +01001177 }
1178
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001179 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001180}
1181
Michal Vasko5a721fd2016-02-16 12:16:48 +01001182struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001183lys_sub_parse_fd(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001184{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001185 struct lys_submodule *submodule;
Radek Krejci0fb11502017-01-31 16:45:42 +01001186 size_t length;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001187 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001188
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001189 assert(module);
1190 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001191
Michal Vasko53b7da02018-02-13 15:28:42 +01001192 if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) {
1193 LOGERR(module->ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001194 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001195 } else if (!addr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001196 LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file.");
Michal Vasko2e7241e2016-02-15 16:06:34 +01001197 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001198 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001199
Michal Vasko5b998712017-01-26 10:34:06 +01001200 /* get the main module */
1201 module = lys_main_module(module);
1202
1203 switch (format) {
1204 case LYS_IN_YIN:
1205 submodule = yin_read_submodule(module, addr, unres);
1206 break;
1207 case LYS_IN_YANG:
1208 submodule = yang_read_submodule(module, addr, 0, unres);
1209 break;
1210 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01001211 LOGINT(module->ctx);
Michal Vasko85d41522017-02-24 09:49:16 +01001212 return NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001213 }
1214
Radek Krejcic645a3a2017-01-31 16:59:00 +01001215 lyp_munmap(addr, length);
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001216
1217 if (submodule && !submodule->filepath) {
1218 lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd);
1219 }
1220
Michal Vasko5a721fd2016-02-16 12:16:48 +01001221 return submodule;
1222
Radek Krejciefaeba32015-05-27 14:30:57 +02001223}
1224
Radek Krejcibf285832017-01-26 16:05:41 +01001225int
1226lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt)
1227{
1228 unsigned int u;
1229
1230 for (u = start; u < ext_size; u++) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001231 if (ext[u]->insubstmt == substmt) {
Radek Krejcibf285832017-01-26 16:05:41 +01001232 return u;
1233 }
1234 }
1235
1236 return -1;
1237}
1238
Radek Krejcifdc0d702017-01-23 15:58:38 +01001239/*
1240 * duplicate extension instance
1241 */
1242int
Michal Vasko17e8ba32018-02-15 10:58:56 +01001243lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent,
1244 LYEXT_PAR parent_type, struct lys_ext_instance ***new, int shallow, struct unres_schema *unres)
Radek Krejcifdc0d702017-01-23 15:58:38 +01001245{
1246 int i;
1247 uint8_t u = 0;
1248 struct lys_ext_instance **result;
1249 struct unres_ext *info, *info_orig;
fanchanghu8d86f6b2017-06-10 12:49:54 +08001250 size_t len;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001251
1252 assert(new);
1253
1254 if (!size) {
1255 if (orig) {
Michal Vasko17e8ba32018-02-15 10:58:56 +01001256 LOGINT(ctx);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001257 return EXIT_FAILURE;
1258 }
1259 (*new) = NULL;
1260 return EXIT_SUCCESS;
1261 }
1262
1263 (*new) = result = calloc(size, sizeof *result);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001264 LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001265 for (u = 0; u < size; u++) {
1266 if (orig[u]) {
1267 /* resolved extension instance, just duplicate it */
Radek Krejci8de8f612017-02-16 15:03:32 +01001268 switch(orig[u]->ext_type) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001269 case LYEXT_FLAG:
1270 result[u] = malloc(sizeof(struct lys_ext_instance));
Michal Vasko17e8ba32018-02-15 10:58:56 +01001271 LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001272 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001273 case LYEXT_COMPLEX:
fanchanghu8d86f6b2017-06-10 12:49:54 +08001274 len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size;
1275 result[u] = calloc(1, len);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001276 LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001277
Radek Krejcifebdad72017-02-06 11:35:51 +01001278 ((struct lys_ext_instance_complex*)result[u])->substmt = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001279 /* TODO duplicate data in extension instance content */
fanchanghu8d86f6b2017-06-10 12:49:54 +08001280 memcpy((void*)result[u] + sizeof(**orig), (void*)orig[u] + sizeof(**orig), len - sizeof(**orig));
Radek Krejci8d6b7422017-02-03 14:42:13 +01001281 break;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001282 }
1283 /* generic part */
1284 result[u]->def = orig[u]->def;
fanchanghu8d86f6b2017-06-10 12:49:54 +08001285 result[u]->flags = LYEXT_OPT_CONTENT;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001286 result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001287 result[u]->parent = parent;
1288 result[u]->parent_type = parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01001289 result[u]->insubstmt = orig[u]->insubstmt;
1290 result[u]->insubstmt_index = orig[u]->insubstmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01001291 result[u]->ext_type = orig[u]->ext_type;
Radek Krejci7f1d47e2017-04-12 15:29:02 +02001292 result[u]->priv = NULL;
1293 result[u]->nodetype = LYS_EXT;
1294 result[u]->module = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001295
1296 /* extensions */
Radek Krejcifdc0d702017-01-23 15:58:38 +01001297 result[u]->ext_size = orig[u]->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001298 if (lys_ext_dup(ctx, mod, orig[u]->ext, orig[u]->ext_size, result[u],
Radek Krejci5138e9f2017-04-12 13:10:46 +02001299 LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001300 goto error;
1301 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001302
1303 /* in case of shallow copy (duplication for deviation), duplicate only the link to private data
1304 * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */
1305 if (shallow) {
1306 result[u]->priv = orig[u]->priv;
1307 }
Radek Krejcifdc0d702017-01-23 15:58:38 +01001308 } else {
1309 /* original extension is not yet resolved, so duplicate it in unres */
1310 i = unres_schema_find(unres, -1, &orig, UNRES_EXT);
1311 if (i == -1) {
1312 /* extension not found in unres */
Michal Vasko17e8ba32018-02-15 10:58:56 +01001313 LOGINT(ctx);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001314 goto error;
1315 }
1316 info_orig = unres->str_snode[i];
1317 info = malloc(sizeof *info);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001318 LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001319 info->datatype = info_orig->datatype;
1320 if (info->datatype == LYS_IN_YIN) {
Michal Vasko17e8ba32018-02-15 10:58:56 +01001321 info->data.yin = lyxml_dup_elem(ctx, info_orig->data.yin, NULL, 1);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001322 } /* else TODO YANG */
1323 info->parent = parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001324 info->mod = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001325 info->parent_type = parent_type;
1326 info->ext_index = u;
1327 if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) {
1328 goto error;
1329 }
1330 }
1331 }
1332
1333 return EXIT_SUCCESS;
1334
1335error:
1336 (*new) = NULL;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001337 lys_extension_instances_free(ctx, result, u, NULL);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001338 return EXIT_FAILURE;
1339}
1340
Radek Krejci1d82ef62015-08-07 14:44:40 +02001341static struct lys_restr *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001342lys_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 +02001343{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001344 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001345 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001346
Radek Krejci3733a802015-06-19 13:43:21 +02001347 if (!size) {
1348 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001349 }
Radek Krejci3733a802015-06-19 13:43:21 +02001350
1351 result = calloc(size, sizeof *result);
Michal Vasko53b7da02018-02-13 15:28:42 +01001352 LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001353
Radek Krejci3733a802015-06-19 13:43:21 +02001354 for (i = 0; i < size; i++) {
Radek Krejci77f22b22017-01-17 15:23:03 +01001355 result[i].ext_size = old[i].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001356 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 +01001357 result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0);
1358 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1359 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1360 result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0);
1361 result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001362 }
1363
1364 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001365}
1366
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001367void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001368lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr,
1369 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci0bd5db42015-06-19 13:30:07 +02001370{
1371 assert(ctx);
1372 if (!restr) {
1373 return;
1374 }
1375
Radek Krejci5138e9f2017-04-12 13:10:46 +02001376 lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001377 lydict_remove(ctx, restr->expr);
1378 lydict_remove(ctx, restr->dsc);
1379 lydict_remove(ctx, restr->ref);
1380 lydict_remove(ctx, restr->eapptag);
1381 lydict_remove(ctx, restr->emsg);
1382}
1383
Pavol Vican05810b62016-11-23 14:07:22 +01001384void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001385lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size,
Frank Rimplerc4db1c72017-09-12 12:56:39 +00001386 int shallow, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001387{
1388 uint8_t i;
1389
1390 for (i = 0; i < iffeature_size; ++i) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001391 lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor);
Michal Vasko15a43372017-09-25 14:12:42 +02001392 if (!shallow) {
Frank Rimpler2a503f52017-09-12 15:21:18 +00001393 free(iffeature[i].expr);
1394 free(iffeature[i].features);
1395 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001396 }
1397 free(iffeature);
1398}
1399
Michal Vaskob84f88a2015-09-24 13:16:10 +02001400static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001401type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001402 LY_DATA_TYPE base, int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001403{
1404 int i;
Radek Krejcidce5f972017-09-12 15:47:49 +02001405 unsigned int u;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001406
1407 switch (base) {
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001408 case LY_TYPE_BINARY:
1409 if (old->info.binary.length) {
1410 new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres);
1411 }
1412 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001413
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001414 case LY_TYPE_BITS:
1415 new->info.bits.count = old->info.bits.count;
1416 if (new->info.bits.count) {
1417 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
Michal Vasko53b7da02018-02-13 15:28:42 +01001418 LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001419
Radek Krejcidce5f972017-09-12 15:47:49 +02001420 for (u = 0; u < new->info.bits.count; u++) {
1421 new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0);
1422 new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0);
1423 new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0);
1424 new->info.bits.bit[u].flags = old->info.bits.bit[u].flags;
1425 new->info.bits.bit[u].pos = old->info.bits.bit[u].pos;
1426 new->info.bits.bit[u].ext_size = old->info.bits.bit[u].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001427 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 +02001428 &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT,
1429 &new->info.bits.bit[u].ext, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001430 return -1;
1431 }
1432 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001433 }
1434 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001435
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001436 case LY_TYPE_DEC64:
1437 new->info.dec64.dig = old->info.dec64.dig;
1438 new->info.dec64.div = old->info.dec64.div;
1439 if (old->info.dec64.range) {
1440 new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres);
1441 }
1442 break;
1443
1444 case LY_TYPE_ENUM:
1445 new->info.enums.count = old->info.enums.count;
1446 if (new->info.enums.count) {
1447 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
Michal Vasko53b7da02018-02-13 15:28:42 +01001448 LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001449
Radek Krejcidce5f972017-09-12 15:47:49 +02001450 for (u = 0; u < new->info.enums.count; u++) {
1451 new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0);
1452 new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0);
1453 new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0);
1454 new->info.enums.enm[u].flags = old->info.enums.enm[u].flags;
1455 new->info.enums.enm[u].value = old->info.enums.enm[u].value;
1456 new->info.enums.enm[u].ext_size = old->info.enums.enm[u].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001457 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 +02001458 &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM,
1459 &new->info.enums.enm[u].ext, shallow, unres)) {
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001460 return -1;
1461 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001462 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001463 }
1464 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001465
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001466 case LY_TYPE_IDENT:
1467 new->info.ident.count = old->info.ident.count;
1468 if (old->info.ident.count) {
1469 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
Michal Vasko53b7da02018-02-13 15:28:42 +01001470 LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001471 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1472 } else {
1473 /* there can be several unresolved base identities, duplicate them all */
1474 i = -1;
1475 do {
1476 i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF);
1477 if (i != -1) {
1478 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001479 return -1;
1480 }
1481 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001482 --i;
1483 } while (i > -1);
1484 }
1485 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001486
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001487 case LY_TYPE_INST:
1488 new->info.inst.req = old->info.inst.req;
1489 break;
1490
1491 case LY_TYPE_INT8:
1492 case LY_TYPE_INT16:
1493 case LY_TYPE_INT32:
1494 case LY_TYPE_INT64:
1495 case LY_TYPE_UINT8:
1496 case LY_TYPE_UINT16:
1497 case LY_TYPE_UINT32:
1498 case LY_TYPE_UINT64:
1499 if (old->info.num.range) {
1500 new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres);
1501 }
1502 break;
1503
1504 case LY_TYPE_LEAFREF:
1505 if (old->info.lref.path) {
1506 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
1507 if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
1508 return -1;
1509 }
1510 }
1511 break;
1512
1513 case LY_TYPE_STRING:
1514 if (old->info.str.length) {
1515 new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres);
1516 }
Radek Krejcib53154b2017-07-19 09:14:13 +02001517 if (old->info.str.pat_count) {
1518 new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres);
1519 new->info.str.pat_count = old->info.str.pat_count;
Michal Vaskofcd974b2017-08-22 10:17:49 +02001520#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02001521 if (!in_grp) {
1522 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 +01001523 LY_CHECK_ERR_RETURN(!new->info.str.patterns_pcre, LOGMEM(mod->ctx), -1);
Radek Krejcia4c107d2017-10-27 14:19:26 +02001524 for (u = 0; u < new->info.str.pat_count; u++) {
Michal Vaskoa26db302018-02-14 15:22:10 +01001525 if (lyp_precompile_pattern(mod->ctx, &new->info.str.patterns[u].expr[1],
Radek Krejcia4c107d2017-10-27 14:19:26 +02001526 (pcre**)&new->info.str.patterns_pcre[2 * u],
1527 (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) {
Radek Krejcib53154b2017-07-19 09:14:13 +02001528 free(new->info.str.patterns_pcre);
1529 new->info.str.patterns_pcre = NULL;
1530 return -1;
1531 }
1532 }
1533 }
Michal Vaskofcd974b2017-08-22 10:17:49 +02001534#endif
Radek Krejcib53154b2017-07-19 09:14:13 +02001535 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001536 break;
1537
1538 case LY_TYPE_UNION:
1539 new->info.uni.has_ptr_type = old->info.uni.has_ptr_type;
1540 new->info.uni.count = old->info.uni.count;
1541 if (new->info.uni.count) {
1542 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
Michal Vasko53b7da02018-02-13 15:28:42 +01001543 LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001544
Radek Krejcidce5f972017-09-12 15:47:49 +02001545 for (u = 0; u < new->info.uni.count; u++) {
1546 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 +02001547 shallow, unres)) {
1548 return -1;
1549 }
1550 }
1551 }
1552 break;
1553
1554 default:
1555 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1556 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001557 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001558
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001559 return EXIT_SUCCESS;
1560}
1561
1562struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001563lys_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 +02001564 int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001565{
1566 struct yang_type *new;
1567
1568 new = calloc(1, sizeof *new);
Michal Vasko53b7da02018-02-13 15:28:42 +01001569 LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001570 new->flags = old->flags;
1571 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001572 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001573 new->type = type;
1574 if (!new->name) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001575 LOGMEM(module->ctx);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001576 goto error;
1577 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001578 if (type_dup(module, parent, type, old->type, new->base, in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001579 new->type->base = new->base;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001580 lys_type_free(module->ctx, new->type, NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001581 memset(&new->type->info, 0, sizeof new->type->info);
1582 goto error;
1583 }
1584 return new;
1585
Michal Vasko53b7da02018-02-13 15:28:42 +01001586error:
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001587 free(new);
1588 return NULL;
1589}
1590
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001591int
1592lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new,
1593 struct unres_schema *unres)
1594{
1595 struct lys_type new;
Radek Krejcidce5f972017-09-12 15:47:49 +02001596 unsigned int i, top_type;
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001597 struct lys_ext_instance **ext;
1598 uint8_t ext_size;
1599 void *reloc;
1600
1601 if (!prev_new) {
1602 /* this is the "top-level" type, meaning it is a real type and no typedef directly above */
1603 top_type = 1;
1604
1605 memset(&new, 0, sizeof new);
1606
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001607 new.base = type->base;
1608 new.parent = (struct lys_tpdf *)parent;
1609
1610 prev_new = &new;
1611 } else {
1612 /* this is not top-level type, just a type of a typedef */
1613 top_type = 0;
1614 }
1615
Radek Krejci9c5cb6d2017-08-09 11:15:23 +02001616 assert(type->der);
1617 if (type->der->module) {
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001618 /* typedef, skip it, but keep the extensions */
1619 ext_size = type->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001620 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 +02001621 return -1;
1622 }
1623 if (prev_new->ext) {
1624 reloc = realloc(prev_new->ext, (prev_new->ext_size + ext_size) * sizeof *prev_new->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01001625 LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1);
Radek Krejci70379e22017-08-09 11:21:07 +02001626 prev_new->ext = reloc;
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001627
1628 memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext);
1629 free(ext);
1630
1631 prev_new->ext_size += ext_size;
1632 } else {
1633 prev_new->ext = ext;
1634 prev_new->ext_size = ext_size;
1635 }
1636
1637 if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) {
1638 return -1;
1639 }
1640 } else {
1641 /* type, just make a deep copy */
1642 switch (type->base) {
1643 case LY_TYPE_UNION:
1644 prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type;
1645 prev_new->info.uni.count = type->info.uni.count;
1646 /* this cannot be a typedef anymore */
1647 assert(prev_new->info.uni.count);
1648
1649 prev_new->info.uni.types = calloc(prev_new->info.uni.count, sizeof *prev_new->info.uni.types);
Michal Vasko53b7da02018-02-13 15:28:42 +01001650 LY_CHECK_ERR_RETURN(!prev_new->info.uni.types, LOGMEM(mod->ctx), -1);
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001651
1652 for (i = 0; i < prev_new->info.uni.count; i++) {
1653 if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) {
1654 return -1;
1655 }
1656 }
1657
1658 prev_new->der = type->der;
1659 break;
1660 default:
1661 if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) {
1662 return -1;
1663 }
1664 break;
1665 }
1666 }
1667
1668 if (top_type) {
1669 memcpy(type, prev_new, sizeof *type);
1670 }
1671 return EXIT_SUCCESS;
1672}
1673
Radek Krejci43ce4b72017-01-04 11:02:38 +01001674API const void *
1675lys_ext_instance_substmt(const struct lys_ext_instance *ext)
1676{
1677 if (!ext) {
1678 return NULL;
1679 }
1680
Radek Krejcifebdad72017-02-06 11:35:51 +01001681 switch (ext->insubstmt) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001682 case LYEXT_SUBSTMT_SELF:
1683 case LYEXT_SUBSTMT_MODIFIER:
1684 case LYEXT_SUBSTMT_VERSION:
1685 return NULL;
1686 case LYEXT_SUBSTMT_ARGUMENT:
1687 if (ext->parent_type == LYEXT_PAR_EXT) {
1688 return ((struct lys_ext_instance*)ext->parent)->arg_value;
1689 }
1690 break;
1691 case LYEXT_SUBSTMT_BASE:
1692 if (ext->parent_type == LYEXT_PAR_TYPE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001693 return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001694 } else if (ext->parent_type == LYEXT_PAR_IDENT) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001695 return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001696 }
1697 break;
1698 case LYEXT_SUBSTMT_BELONGSTO:
1699 if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) {
1700 return ((struct lys_submodule*)ext->parent)->belongsto;
1701 }
1702 break;
1703 case LYEXT_SUBSTMT_CONFIG:
1704 case LYEXT_SUBSTMT_MANDATORY:
1705 if (ext->parent_type == LYEXT_PAR_NODE) {
1706 return &((struct lys_node*)ext->parent)->flags;
1707 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1708 return &((struct lys_deviate*)ext->parent)->flags;
1709 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1710 return &((struct lys_refine*)ext->parent)->flags;
1711 }
1712 break;
1713 case LYEXT_SUBSTMT_CONTACT:
1714 if (ext->parent_type == LYEXT_PAR_MODULE) {
1715 return ((struct lys_module*)ext->parent)->contact;
1716 }
1717 break;
1718 case LYEXT_SUBSTMT_DEFAULT:
1719 if (ext->parent_type == LYEXT_PAR_NODE) {
1720 switch (((struct lys_node*)ext->parent)->nodetype) {
1721 case LYS_LEAF:
1722 case LYS_LEAFLIST:
1723 /* in case of leaf, the index is supposed to be 0, so it will return the
1724 * correct pointer despite the leaf structure does not have dflt as array */
Radek Krejcifebdad72017-02-06 11:35:51 +01001725 return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001726 case LYS_CHOICE:
1727 return ((struct lys_node_choice*)ext->parent)->dflt;
1728 default:
1729 /* internal error */
1730 break;
1731 }
1732 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1733 return ((struct lys_tpdf*)ext->parent)->dflt;
1734 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001735 return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001736 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001737 return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001738 }
1739 break;
1740 case LYEXT_SUBSTMT_DESCRIPTION:
1741 switch (ext->parent_type) {
1742 case LYEXT_PAR_NODE:
1743 return ((struct lys_node*)ext->parent)->dsc;
1744 case LYEXT_PAR_MODULE:
1745 return ((struct lys_module*)ext->parent)->dsc;
1746 case LYEXT_PAR_IMPORT:
1747 return ((struct lys_import*)ext->parent)->dsc;
1748 case LYEXT_PAR_INCLUDE:
1749 return ((struct lys_include*)ext->parent)->dsc;
1750 case LYEXT_PAR_EXT:
1751 return ((struct lys_ext*)ext->parent)->dsc;
1752 case LYEXT_PAR_FEATURE:
1753 return ((struct lys_feature*)ext->parent)->dsc;
1754 case LYEXT_PAR_TPDF:
1755 return ((struct lys_tpdf*)ext->parent)->dsc;
1756 case LYEXT_PAR_TYPE_BIT:
1757 return ((struct lys_type_bit*)ext->parent)->dsc;
1758 case LYEXT_PAR_TYPE_ENUM:
1759 return ((struct lys_type_enum*)ext->parent)->dsc;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001760 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001761 return ((struct lys_restr*)ext->parent)->dsc;
1762 case LYEXT_PAR_WHEN:
1763 return ((struct lys_when*)ext->parent)->dsc;
1764 case LYEXT_PAR_IDENT:
1765 return ((struct lys_ident*)ext->parent)->dsc;
1766 case LYEXT_PAR_DEVIATION:
1767 return ((struct lys_deviation*)ext->parent)->dsc;
1768 case LYEXT_PAR_REVISION:
1769 return ((struct lys_revision*)ext->parent)->dsc;
1770 case LYEXT_PAR_REFINE:
1771 return ((struct lys_refine*)ext->parent)->dsc;
1772 default:
1773 break;
1774 }
1775 break;
1776 case LYEXT_SUBSTMT_ERRTAG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001777 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001778 return ((struct lys_restr*)ext->parent)->eapptag;
1779 }
1780 break;
1781 case LYEXT_SUBSTMT_ERRMSG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001782 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001783 return ((struct lys_restr*)ext->parent)->emsg;
1784 }
1785 break;
1786 case LYEXT_SUBSTMT_DIGITS:
1787 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) {
1788 return &((struct lys_type*)ext->parent)->info.dec64.dig;
1789 }
1790 break;
1791 case LYEXT_SUBSTMT_KEY:
1792 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1793 return ((struct lys_node_list*)ext->parent)->keys;
1794 }
1795 break;
1796 case LYEXT_SUBSTMT_MAX:
1797 if (ext->parent_type == LYEXT_PAR_NODE) {
1798 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1799 return &((struct lys_node_list*)ext->parent)->max;
1800 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1801 return &((struct lys_node_leaflist*)ext->parent)->max;
1802 }
1803 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1804 return &((struct lys_refine*)ext->parent)->mod.list.max;
1805 }
1806 break;
1807 case LYEXT_SUBSTMT_MIN:
1808 if (ext->parent_type == LYEXT_PAR_NODE) {
1809 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1810 return &((struct lys_node_list*)ext->parent)->min;
1811 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1812 return &((struct lys_node_leaflist*)ext->parent)->min;
1813 }
1814 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1815 return &((struct lys_refine*)ext->parent)->mod.list.min;
1816 }
1817 break;
1818 case LYEXT_SUBSTMT_NAMESPACE:
1819 if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) {
1820 return ((struct lys_module*)ext->parent)->ns;
1821 }
1822 break;
1823 case LYEXT_SUBSTMT_ORDEREDBY:
1824 if (ext->parent_type == LYEXT_PAR_NODE &&
1825 (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1826 return &((struct lys_node_list*)ext->parent)->flags;
1827 }
1828 break;
1829 case LYEXT_SUBSTMT_ORGANIZATION:
1830 if (ext->parent_type == LYEXT_PAR_MODULE) {
1831 return ((struct lys_module*)ext->parent)->org;
1832 }
1833 break;
1834 case LYEXT_SUBSTMT_PATH:
1835 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1836 return ((struct lys_type*)ext->parent)->info.lref.path;
1837 }
1838 break;
1839 case LYEXT_SUBSTMT_POSITION:
1840 if (ext->parent_type == LYEXT_PAR_TYPE_BIT) {
1841 return &((struct lys_type_bit*)ext->parent)->pos;
1842 }
1843 break;
1844 case LYEXT_SUBSTMT_PREFIX:
1845 if (ext->parent_type == LYEXT_PAR_MODULE) {
1846 /* covers also lys_submodule */
1847 return ((struct lys_module*)ext->parent)->prefix;
1848 } else if (ext->parent_type == LYEXT_PAR_IMPORT) {
1849 return ((struct lys_import*)ext->parent)->prefix;
1850 }
1851 break;
1852 case LYEXT_SUBSTMT_PRESENCE:
1853 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) {
1854 return ((struct lys_node_container*)ext->parent)->presence;
1855 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1856 return ((struct lys_refine*)ext->parent)->mod.presence;
1857 }
1858 break;
1859 case LYEXT_SUBSTMT_REFERENCE:
1860 switch (ext->parent_type) {
1861 case LYEXT_PAR_NODE:
1862 return ((struct lys_node*)ext->parent)->ref;
1863 case LYEXT_PAR_MODULE:
1864 return ((struct lys_module*)ext->parent)->ref;
1865 case LYEXT_PAR_IMPORT:
1866 return ((struct lys_import*)ext->parent)->ref;
1867 case LYEXT_PAR_INCLUDE:
1868 return ((struct lys_include*)ext->parent)->ref;
1869 case LYEXT_PAR_EXT:
1870 return ((struct lys_ext*)ext->parent)->ref;
1871 case LYEXT_PAR_FEATURE:
1872 return ((struct lys_feature*)ext->parent)->ref;
1873 case LYEXT_PAR_TPDF:
1874 return ((struct lys_tpdf*)ext->parent)->ref;
1875 case LYEXT_PAR_TYPE_BIT:
1876 return ((struct lys_type_bit*)ext->parent)->ref;
1877 case LYEXT_PAR_TYPE_ENUM:
1878 return ((struct lys_type_enum*)ext->parent)->ref;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001879 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001880 return ((struct lys_restr*)ext->parent)->ref;
1881 case LYEXT_PAR_WHEN:
1882 return ((struct lys_when*)ext->parent)->ref;
1883 case LYEXT_PAR_IDENT:
1884 return ((struct lys_ident*)ext->parent)->ref;
1885 case LYEXT_PAR_DEVIATION:
1886 return ((struct lys_deviation*)ext->parent)->ref;
1887 case LYEXT_PAR_REVISION:
1888 return ((struct lys_revision*)ext->parent)->ref;
1889 case LYEXT_PAR_REFINE:
1890 return ((struct lys_refine*)ext->parent)->ref;
1891 default:
1892 break;
1893 }
1894 break;
Radek Krejcibe336392017-02-07 10:54:24 +01001895 case LYEXT_SUBSTMT_REQINSTANCE:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001896 if (ext->parent_type == LYEXT_PAR_TYPE) {
1897 if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1898 return &((struct lys_type*)ext->parent)->info.lref.req;
1899 } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) {
1900 return &((struct lys_type*)ext->parent)->info.inst.req;
1901 }
1902 }
1903 break;
1904 case LYEXT_SUBSTMT_REVISIONDATE:
1905 if (ext->parent_type == LYEXT_PAR_IMPORT) {
1906 return ((struct lys_import*)ext->parent)->rev;
1907 } else if (ext->parent_type == LYEXT_PAR_INCLUDE) {
1908 return ((struct lys_include*)ext->parent)->rev;
1909 }
1910 break;
1911 case LYEXT_SUBSTMT_STATUS:
1912 switch (ext->parent_type) {
1913 case LYEXT_PAR_NODE:
1914 case LYEXT_PAR_IDENT:
1915 case LYEXT_PAR_TPDF:
1916 case LYEXT_PAR_EXT:
1917 case LYEXT_PAR_FEATURE:
1918 case LYEXT_PAR_TYPE_ENUM:
1919 case LYEXT_PAR_TYPE_BIT:
1920 /* in all structures the flags member is at the same offset */
1921 return &((struct lys_node*)ext->parent)->flags;
1922 default:
1923 break;
1924 }
1925 break;
1926 case LYEXT_SUBSTMT_UNIQUE:
1927 if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001928 return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001929 } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001930 return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001931 }
1932 break;
1933 case LYEXT_SUBSTMT_UNITS:
1934 if (ext->parent_type == LYEXT_PAR_NODE &&
1935 (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1936 /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */
1937 return ((struct lys_node_leaf*)ext->parent)->units;
1938 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1939 return ((struct lys_tpdf*)ext->parent)->units;
1940 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1941 return ((struct lys_deviate*)ext->parent)->units;
1942 }
1943 break;
1944 case LYEXT_SUBSTMT_VALUE:
1945 if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) {
1946 return &((struct lys_type_enum*)ext->parent)->value;
1947 }
1948 break;
1949 case LYEXT_SUBSTMT_YINELEM:
1950 if (ext->parent_type == LYEXT_PAR_EXT) {
1951 return &((struct lys_ext*)ext->parent)->flags;
1952 }
1953 break;
1954 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001955 LOGINT(ext->module->ctx);
Radek Krejci43ce4b72017-01-04 11:02:38 +01001956 return NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01001957}
1958
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001959static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001960lys_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 +02001961 int in_grp, int shallow, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001962{
1963 int i;
1964
Radek Krejci3733a802015-06-19 13:43:21 +02001965 new->base = old->base;
1966 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001967 new->parent = (struct lys_tpdf *)parent;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001968 new->ext_size = old->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001969 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 +01001970 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01001971 }
Radek Krejci3733a802015-06-19 13:43:21 +02001972
Michal Vasko1c007172017-03-10 10:20:44 +01001973 i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001974 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001975 /* HACK (serious one) for unres */
1976 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001977 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001978 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp,
1979 shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001980 } else {
1981 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1982 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001983 /* all these unres additions can fail even though they did not before */
Michal Vasko1c007172017-03-10 10:20:44 +01001984 if (!new->der || (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent) == -1)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001985 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001986 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001987 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001988 }
1989
Radek Krejci5138e9f2017-04-12 13:10:46 +02001990 return type_dup(mod, parent, new, old, new->base, in_grp, shallow, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001991}
1992
1993void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001994lys_type_free(struct ly_ctx *ctx, struct lys_type *type,
1995 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02001996{
Radek Krejcidce5f972017-09-12 15:47:49 +02001997 unsigned int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001998
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001999 assert(ctx);
2000 if (!type) {
2001 return;
2002 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002003
Radek Krejci5138e9f2017-04-12 13:10:46 +02002004 lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002005
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002006 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02002007 case LY_TYPE_BINARY:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002008 lys_restr_free(ctx, type->info.binary.length, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02002009 free(type->info.binary.length);
2010 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02002011 case LY_TYPE_BITS:
2012 for (i = 0; i < type->info.bits.count; i++) {
2013 lydict_remove(ctx, type->info.bits.bit[i].name);
2014 lydict_remove(ctx, type->info.bits.bit[i].dsc);
2015 lydict_remove(ctx, type->info.bits.bit[i].ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002016 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 +02002017 private_destructor);
2018 lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size,
2019 private_destructor);
Radek Krejci994b6f62015-06-18 16:47:27 +02002020 }
2021 free(type->info.bits.bit);
2022 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02002023
2024 case LY_TYPE_DEC64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002025 lys_restr_free(ctx, type->info.dec64.range, private_destructor);
Radek Krejcif9401c32015-06-26 16:47:36 +02002026 free(type->info.dec64.range);
2027 break;
2028
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002029 case LY_TYPE_ENUM:
2030 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002031 lydict_remove(ctx, type->info.enums.enm[i].name);
2032 lydict_remove(ctx, type->info.enums.enm[i].dsc);
2033 lydict_remove(ctx, type->info.enums.enm[i].ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002034 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 +02002035 private_destructor);
2036 lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size,
2037 private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002038 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02002039 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002040 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02002041
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02002042 case LY_TYPE_INT8:
2043 case LY_TYPE_INT16:
2044 case LY_TYPE_INT32:
2045 case LY_TYPE_INT64:
2046 case LY_TYPE_UINT8:
2047 case LY_TYPE_UINT16:
2048 case LY_TYPE_UINT32:
2049 case LY_TYPE_UINT64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002050 lys_restr_free(ctx, type->info.num.range, private_destructor);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02002051 free(type->info.num.range);
2052 break;
2053
Radek Krejcidc4c1412015-06-19 15:39:54 +02002054 case LY_TYPE_LEAFREF:
2055 lydict_remove(ctx, type->info.lref.path);
2056 break;
2057
Radek Krejci3733a802015-06-19 13:43:21 +02002058 case LY_TYPE_STRING:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002059 lys_restr_free(ctx, type->info.str.length, private_destructor);
Radek Krejci3733a802015-06-19 13:43:21 +02002060 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02002061 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002062 lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002063#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02002064 if (type->info.str.patterns_pcre) {
2065 pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]);
2066 pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]);
2067 }
Michal Vaskofcd974b2017-08-22 10:17:49 +02002068#endif
Radek Krejci5fbc9162015-06-19 14:11:11 +02002069 }
2070 free(type->info.str.patterns);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002071#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02002072 free(type->info.str.patterns_pcre);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002073#endif
Radek Krejci3733a802015-06-19 13:43:21 +02002074 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02002075
Radek Krejcie4c366b2015-07-02 10:11:31 +02002076 case LY_TYPE_UNION:
2077 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002078 lys_type_free(ctx, &type->info.uni.types[i], private_destructor);
Radek Krejcie4c366b2015-07-02 10:11:31 +02002079 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02002080 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02002081 break;
2082
Michal Vaskod3282192016-09-05 11:27:57 +02002083 case LY_TYPE_IDENT:
2084 free(type->info.ident.ref);
2085 break;
2086
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002087 default:
Michal Vaskod3282192016-09-05 11:27:57 +02002088 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002089 break;
2090 }
Radek Krejci5a065542015-05-22 15:02:07 +02002091}
2092
Radek Krejci1d82ef62015-08-07 14:44:40 +02002093static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002094lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf,
2095 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002096{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002097 assert(ctx);
2098 if (!tpdf) {
2099 return;
2100 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002101
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002102 lydict_remove(ctx, tpdf->name);
2103 lydict_remove(ctx, tpdf->dsc);
2104 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002105
Radek Krejci5138e9f2017-04-12 13:10:46 +02002106 lys_type_free(ctx, &tpdf->type, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002107
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002108 lydict_remove(ctx, tpdf->units);
2109 lydict_remove(ctx, tpdf->dflt);
Radek Krejcie534c132016-11-23 13:32:31 +01002110
Radek Krejci5138e9f2017-04-12 13:10:46 +02002111 lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002112}
2113
Radek Krejci1d82ef62015-08-07 14:44:40 +02002114static struct lys_when *
Radek Krejci5138e9f2017-04-12 13:10:46 +02002115lys_when_dup(struct lys_module *mod, struct lys_when *old, int shallow, struct unres_schema *unres)
Radek Krejci00768f42015-06-18 17:04:04 +02002116{
Radek Krejci76512572015-08-04 09:47:08 +02002117 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02002118
2119 if (!old) {
2120 return NULL;
2121 }
2122
2123 new = calloc(1, sizeof *new);
Michal Vasko53b7da02018-02-13 15:28:42 +01002124 LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL);
Radek Krejci8d6b7422017-02-03 14:42:13 +01002125 new->cond = lydict_insert(mod->ctx, old->cond, 0);
2126 new->dsc = lydict_insert(mod->ctx, old->dsc, 0);
2127 new->ref = lydict_insert(mod->ctx, old->ref, 0);
Radek Krejcif0bb3602017-01-25 17:05:08 +01002128 new->ext_size = old->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002129 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 +02002130
2131 return new;
2132}
2133
Michal Vasko0308dd62015-10-07 09:14:40 +02002134void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002135lys_when_free(struct ly_ctx *ctx, struct lys_when *w,
2136 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002137{
2138 if (!w) {
2139 return;
2140 }
2141
Radek Krejci5138e9f2017-04-12 13:10:46 +02002142 lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002143 lydict_remove(ctx, w->cond);
2144 lydict_remove(ctx, w->dsc);
2145 lydict_remove(ctx, w->ref);
2146
2147 free(w);
2148}
2149
Radek Krejcib7f5e412015-08-13 10:15:51 +02002150static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002151lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug,
2152 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02002153{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002154 struct lys_node *next, *sub;
2155
Radek Krejcic071c542016-01-27 14:57:51 +01002156 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01002157 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002158 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002159 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002160 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002161 }
2162
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002163 lydict_remove(ctx, aug->target_name);
2164 lydict_remove(ctx, aug->dsc);
2165 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002166
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002167 lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002168 lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002169
Radek Krejci5138e9f2017-04-12 13:10:46 +02002170 lys_when_free(ctx, aug->when, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002171}
2172
Radek Krejci1d82ef62015-08-07 14:44:40 +02002173static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002174lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident,
2175 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci6793db02015-05-22 17:49:54 +02002176{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002177 assert(ctx);
2178 if (!ident) {
2179 return;
2180 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002181
Radek Krejci018f1f52016-08-03 16:01:20 +02002182 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02002183 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002184 lydict_remove(ctx, ident->name);
2185 lydict_remove(ctx, ident->dsc);
2186 lydict_remove(ctx, ident->ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002187 lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002188 lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor);
Radek Krejci6793db02015-05-22 17:49:54 +02002189
2190}
2191
Radek Krejci1d82ef62015-08-07 14:44:40 +02002192static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002193lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp,
2194 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002195{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002196 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002197
Radek Krejcid12f57b2015-08-06 10:43:39 +02002198 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002199 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002200 lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002201 }
2202 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002203}
2204
Radek Krejci1d82ef62015-08-07 14:44:40 +02002205static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002206lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io,
2207 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcid12f57b2015-08-06 10:43:39 +02002208{
2209 int i;
2210
2211 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
2212 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002213 lys_tpdf_free(ctx, &io->tpdf[i], private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002214 }
2215 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002216
2217 for (i = 0; i < io->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002218 lys_restr_free(ctx, &io->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002219 }
2220 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002221}
2222
Radek Krejci1d82ef62015-08-07 14:44:40 +02002223static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002224lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif,
2225 void (*private_destructor)(const struct lys_node *node, void *priv))
Pavol Vican7cff97e2016-08-09 14:56:08 +02002226{
2227 int i;
2228
2229 for (i = 0; i < notif->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002230 lys_restr_free(ctx, &notif->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002231 }
2232 free(notif->must);
2233
2234 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002235 lys_tpdf_free(ctx, &notif->tpdf[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002236 }
2237 free(notif->tpdf);
2238}
2239static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002240lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml,
2241 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci537cf382015-06-04 11:07:01 +02002242{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002243 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002244
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002245 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002246 lys_restr_free(ctx, &anyxml->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002247 }
2248 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002249
Radek Krejci5138e9f2017-04-12 13:10:46 +02002250 lys_when_free(ctx, anyxml->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002251}
2252
Radek Krejci1d82ef62015-08-07 14:44:40 +02002253static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002254lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf,
2255 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002256{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002257 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002258
Radek Krejci85a54be2016-10-20 12:39:56 +02002259 /* leafref backlinks */
2260 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002261
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002262 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002263 lys_restr_free(ctx, &leaf->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002264 }
2265 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002266
Radek Krejci5138e9f2017-04-12 13:10:46 +02002267 lys_when_free(ctx, leaf->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002268
Radek Krejci5138e9f2017-04-12 13:10:46 +02002269 lys_type_free(ctx, &leaf->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002270 lydict_remove(ctx, leaf->units);
2271 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02002272}
2273
Radek Krejci1d82ef62015-08-07 14:44:40 +02002274static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002275lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist,
2276 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002277{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002278 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002279
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002280 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002281 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002282 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002283 }
2284
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002285 for (i = 0; i < llist->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002286 lys_restr_free(ctx, &llist->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002287 }
2288 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002289
Pavol Vican38321d02016-08-16 14:56:02 +02002290 for (i = 0; i < llist->dflt_size; i++) {
2291 lydict_remove(ctx, llist->dflt[i]);
2292 }
2293 free(llist->dflt);
2294
Radek Krejci5138e9f2017-04-12 13:10:46 +02002295 lys_when_free(ctx, llist->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002296
Radek Krejci5138e9f2017-04-12 13:10:46 +02002297 lys_type_free(ctx, &llist->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002298 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02002299}
2300
Radek Krejci1d82ef62015-08-07 14:44:40 +02002301static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002302lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list,
2303 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002304{
Radek Krejci581ce772015-11-10 17:22:40 +01002305 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002306
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002307 /* handle only specific parts for LY_NODE_LIST */
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002308 lys_when_free(ctx, list->when, private_destructor);
Radek Krejci537cf382015-06-04 11:07:01 +02002309
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002310 for (i = 0; i < list->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002311 lys_restr_free(ctx, &list->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002312 }
2313 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002314
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002315 for (i = 0; i < list->tpdf_size; i++) {
2316 lys_tpdf_free(ctx, &list->tpdf[i], private_destructor);
2317 }
2318 free(list->tpdf);
2319
2320 free(list->keys);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002321
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002322 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02002323 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002324 lydict_remove(ctx, list->unique[i].expr[j]);
2325 }
2326 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002327 }
2328 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02002329
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002330 lydict_remove(ctx, list->keys_str);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002331}
2332
Radek Krejci1d82ef62015-08-07 14:44:40 +02002333static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002334lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont,
2335 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002336{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002337 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002338
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002339 /* handle only specific parts for LY_NODE_CONTAINER */
2340 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02002341
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002342 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002343 lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002344 }
2345 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02002346
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002347 for (i = 0; i < cont->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002348 lys_restr_free(ctx, &cont->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002349 }
2350 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002351
Radek Krejci5138e9f2017-04-12 13:10:46 +02002352 lys_when_free(ctx, cont->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002353}
2354
Radek Krejci1d82ef62015-08-07 14:44:40 +02002355static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002356lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f,
2357 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci3cf9e222015-06-18 11:37:50 +02002358{
2359 lydict_remove(ctx, f->name);
2360 lydict_remove(ctx, f->dsc);
2361 lydict_remove(ctx, f->ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002362 lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor);
Radek Krejci9de2c042016-10-19 16:53:06 +02002363 ly_set_free(f->depfeatures);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002364 lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002365}
2366
2367static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002368lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e,
2369 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie534c132016-11-23 13:32:31 +01002370{
2371 lydict_remove(ctx, e->name);
2372 lydict_remove(ctx, e->dsc);
2373 lydict_remove(ctx, e->ref);
2374 lydict_remove(ctx, e->argument);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002375 lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002376}
2377
Radek Krejci1d82ef62015-08-07 14:44:40 +02002378static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002379lys_deviation_free(struct lys_module *module, struct lys_deviation *dev,
2380 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcieb00f512015-07-01 16:44:58 +02002381{
Radek Krejci581ce772015-11-10 17:22:40 +01002382 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01002383 struct ly_ctx *ctx;
2384 struct lys_node *next, *elem;
2385
2386 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02002387
2388 lydict_remove(ctx, dev->target_name);
2389 lydict_remove(ctx, dev->dsc);
2390 lydict_remove(ctx, dev->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002391 lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002392
Pavol Vican64d0b762016-08-25 10:44:59 +02002393 if (!dev->deviate) {
2394 return ;
2395 }
2396
Michal Vaskoff006c12016-02-17 11:15:19 +01002397 /* the module was freed, but we only need the context from orig_node, use ours */
2398 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2399 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
2400 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
2401 elem->module = module;
2402
2403 LY_TREE_DFS_END(dev->orig_node, next, elem);
2404 }
2405 lys_node_free(dev->orig_node, NULL, 0);
2406 } else {
2407 /* it's just a shallow copy, freeing one node */
2408 dev->orig_node->module = module;
2409 lys_node_free(dev->orig_node, NULL, 1);
2410 }
2411
Radek Krejcieb00f512015-07-01 16:44:58 +02002412 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002413 lys_extension_instances_free(ctx, dev->deviate[i].ext, dev->deviate[i].ext_size, private_destructor);
Radek Krejci3c4b0ea2017-01-24 16:00:47 +01002414
Radek Krejcid5a5c282016-08-15 15:38:08 +02002415 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02002416 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02002417 }
2418 free(dev->deviate[i].dflt);
2419
Radek Krejcieb00f512015-07-01 16:44:58 +02002420 lydict_remove(ctx, dev->deviate[i].units);
2421
2422 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
2423 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002424 lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002425 }
2426 free(dev->deviate[i].must);
2427
2428 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002429 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
2430 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
2431 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01002432 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002433 }
2434 free(dev->deviate[i].unique);
2435 }
2436 }
2437 free(dev->deviate);
2438}
2439
Radek Krejci1d82ef62015-08-07 14:44:40 +02002440static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002441lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses,
2442 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02002443{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002444 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02002445
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002446 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002447 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002448 lydict_remove(ctx, uses->refine[i].dsc);
2449 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002450
Radek Krejcifde04bd2017-09-13 16:38:38 +02002451 lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor);
2452
Michal Vaskoa275c0a2015-09-24 09:55:42 +02002453 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002454 lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002455 }
2456 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002457
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002458 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02002459 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002460 }
2461 free(uses->refine[i].dflt);
2462
Radek Krejci5138e9f2017-04-12 13:10:46 +02002463 lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002464
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002465 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002466 lydict_remove(ctx, uses->refine[i].mod.presence);
2467 }
2468 }
2469 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002470
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002471 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002472 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002473 }
2474 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002475
Radek Krejci5138e9f2017-04-12 13:10:46 +02002476 lys_when_free(ctx, uses->when, private_destructor);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002477}
2478
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002479void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002480lys_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 +02002481{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002482 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02002483 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002484
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002485 if (!node) {
2486 return;
2487 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002488
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002489 assert(node->module);
2490 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02002491
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002492 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002493
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002494 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002495 if (node->priv && private_destructor) {
2496 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002497 }
2498
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002499 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002500 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002501 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002502 lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002503 lydict_remove(ctx, node->dsc);
2504 lydict_remove(ctx, node->ref);
2505 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002506
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002507 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002508 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002509 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002510 }
2511 }
2512
Radek Krejci5138e9f2017-04-12 13:10:46 +02002513 lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002514
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002515 /* specific part */
2516 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002517 case LYS_CONTAINER:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002518 lys_container_free(ctx, (struct lys_node_container *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002519 break;
Radek Krejci76512572015-08-04 09:47:08 +02002520 case LYS_CHOICE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002521 lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002522 break;
Radek Krejci76512572015-08-04 09:47:08 +02002523 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002524 lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002525 break;
Radek Krejci76512572015-08-04 09:47:08 +02002526 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002527 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002528 break;
Radek Krejci76512572015-08-04 09:47:08 +02002529 case LYS_LIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002530 lys_list_free(ctx, (struct lys_node_list *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002531 break;
Radek Krejci76512572015-08-04 09:47:08 +02002532 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002533 case LYS_ANYDATA:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002534 lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002535 break;
Radek Krejci76512572015-08-04 09:47:08 +02002536 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002537 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002538 break;
Radek Krejci76512572015-08-04 09:47:08 +02002539 case LYS_CASE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002540 lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002541 break;
Radek Krejci76512572015-08-04 09:47:08 +02002542 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002543 /* do nothing */
2544 break;
Radek Krejci76512572015-08-04 09:47:08 +02002545 case LYS_GROUPING:
2546 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002547 case LYS_ACTION:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002548 lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002549 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02002550 case LYS_NOTIF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002551 lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002552 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002553 case LYS_INPUT:
2554 case LYS_OUTPUT:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002555 lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002556 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01002557 case LYS_EXT:
Michal Vasko591e0b22015-08-13 13:53:43 +02002558 case LYS_UNKNOWN:
Michal Vasko53b7da02018-02-13 15:28:42 +01002559 LOGINT(ctx);
Michal Vasko591e0b22015-08-13 13:53:43 +02002560 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002561 }
Radek Krejci5a065542015-05-22 15:02:07 +02002562
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002563 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002564 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002565 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002566}
2567
Radek Krejci2eee5c02016-12-06 19:18:05 +01002568API struct lys_module *
2569lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02002570{
2571 struct ly_ctx *ctx;
2572 int i;
2573
2574 if (!mod || mod->implemented) {
2575 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002576 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002577 }
2578
2579 ctx = mod->ctx;
2580 for (i = 0; i < ctx->models.used; i++) {
2581 if (!ctx->models.list[i]->implemented) {
2582 continue;
2583 }
2584
2585 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2586 /* we have some revision of the module implemented */
2587 return ctx->models.list[i];
2588 }
2589 }
2590
2591 /* we have no revision of the module implemented, return the module itself,
2592 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002593 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002594}
2595
Michal Vasko13b15832015-08-19 11:04:48 +02002596/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002597static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002598module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002599{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002600 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002601 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002602 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002603
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002604 assert(module->ctx);
2605 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002606
Michal Vaskob746fff2016-02-11 11:37:50 +01002607 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002608 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002609 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002610 lydict_remove(ctx, module->imp[i].dsc);
2611 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002612 lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size, private_destructor);
Radek Krejci225376f2016-02-16 17:36:22 +01002613 }
Radek Krejcidce51452015-06-16 15:20:08 +02002614 free(module->imp);
2615
Radek Krejcic071c542016-01-27 14:57:51 +01002616 /* submodules don't have data tree, the data nodes
2617 * are placed in the main module altogether */
2618 if (!module->type) {
2619 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002620 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002621 }
Radek Krejci21181962015-06-30 14:11:00 +02002622 }
Radek Krejci5a065542015-05-22 15:02:07 +02002623
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002624 lydict_remove(ctx, module->dsc);
2625 lydict_remove(ctx, module->ref);
2626 lydict_remove(ctx, module->org);
2627 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002628 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002629
Radek Krejcieb00f512015-07-01 16:44:58 +02002630 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002631 for (i = 0; i < module->rev_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002632 lys_extension_instances_free(ctx, module->rev[i].ext, module->rev[i].ext_size, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002633 lydict_remove(ctx, module->rev[i].dsc);
2634 lydict_remove(ctx, module->rev[i].ref);
2635 }
2636 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002637
Radek Krejcieb00f512015-07-01 16:44:58 +02002638 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002639 for (i = 0; i < module->ident_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002640 lys_ident_free(ctx, &module->ident[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002641 }
2642 module->ident_size = 0;
2643 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002644
Radek Krejcieb00f512015-07-01 16:44:58 +02002645 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002646 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002647 lys_tpdf_free(ctx, &module->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002648 }
2649 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002650
Radek Krejcie534c132016-11-23 13:32:31 +01002651 /* extension instances */
Radek Krejci5138e9f2017-04-12 13:10:46 +02002652 lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002653
Radek Krejcieb00f512015-07-01 16:44:58 +02002654 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002655 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002656 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002657 }
2658 free(module->augment);
2659
Radek Krejcieb00f512015-07-01 16:44:58 +02002660 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002661 for (i = 0; i < module->features_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002662 lys_feature_free(ctx, &module->features[i], private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002663 }
2664 free(module->features);
2665
Radek Krejcieb00f512015-07-01 16:44:58 +02002666 /* deviations */
2667 for (i = 0; i < module->deviation_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002668 lys_deviation_free(module, &module->deviation[i], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002669 }
2670 free(module->deviation);
2671
Radek Krejcie534c132016-11-23 13:32:31 +01002672 /* extensions */
2673 for (i = 0; i < module->extensions_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002674 lys_extension_free(ctx, &module->extensions[i], private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002675 }
2676 free(module->extensions);
2677
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002678 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002679 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002680}
2681
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002682void
Michal Vaskob746fff2016-02-11 11:37:50 +01002683lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002684{
Michal Vasko10681e82018-01-16 14:54:16 +01002685 int i;
2686
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002687 if (!submodule) {
2688 return;
2689 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002690
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002691 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002692 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002693
Michal Vasko10681e82018-01-16 14:54:16 +01002694 /* include */
2695 for (i = 0; i < submodule->inc_size; i++) {
2696 lydict_remove(submodule->ctx, submodule->inc[i].dsc);
2697 lydict_remove(submodule->ctx, submodule->inc[i].ref);
2698 lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor);
2699 /* complete submodule free is done only from main module since
2700 * submodules propagate their includes to the main module */
2701 }
2702 free(submodule->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002703
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002704 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002705}
2706
Radek Krejcib53154b2017-07-19 09:14:13 +02002707int
2708lys_ingrouping(const struct lys_node *node)
Radek Krejci3a5501d2016-07-18 22:03:34 +02002709{
2710 const struct lys_node *iter = node;
2711 assert(node);
2712
2713 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2714 if (!iter) {
2715 return 0;
2716 } else {
2717 return 1;
2718 }
2719}
2720
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002721/*
2722 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2723 */
2724static struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01002725lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002726 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002727{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002728 struct lys_node *retval = NULL, *iter, *p;
Michal Vaskofe31cc02017-03-10 15:52:31 +01002729 struct lys_module *tmp_mod;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002730 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002731 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002732 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002733 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002734 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002735
Michal Vaskoc07187d2015-08-13 15:20:57 +02002736 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002737 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002738 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002739 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002740 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002741 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002742 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002743 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002744 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002745 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002746 struct lys_node_anydata *any = NULL;
2747 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002748 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002749 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002750 struct lys_node_rpc_action *rpc = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02002751 struct lys_node_inout *io = NULL;
Radek Krejcic43151c2017-03-12 07:10:52 +01002752 struct lys_node_notif *ntf = NULL;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002753 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002754 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002755
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002756 /* we cannot just duplicate memory since the strings are stored in
2757 * dictionary and we need to update dictionary counters.
2758 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002759
Radek Krejci1d82ef62015-08-07 14:44:40 +02002760 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002761 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002762 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002763 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002764 break;
2765
Radek Krejci76512572015-08-04 09:47:08 +02002766 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002767 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002768 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002769 break;
2770
Radek Krejci76512572015-08-04 09:47:08 +02002771 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002772 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002773 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002774 break;
2775
Radek Krejci76512572015-08-04 09:47:08 +02002776 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002777 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002778 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002779 break;
2780
Radek Krejci76512572015-08-04 09:47:08 +02002781 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002782 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002783 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002784 break;
2785
Radek Krejci76512572015-08-04 09:47:08 +02002786 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002787 case LYS_ANYDATA:
2788 any = calloc(1, sizeof *any);
2789 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002790 break;
2791
Radek Krejci76512572015-08-04 09:47:08 +02002792 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002793 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002794 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002795 break;
2796
Radek Krejci76512572015-08-04 09:47:08 +02002797 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002798 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002799 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002800 break;
2801
Radek Krejci76512572015-08-04 09:47:08 +02002802 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002803 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002804 rpc = calloc(1, sizeof *rpc);
2805 retval = (struct lys_node *)rpc;
2806 break;
2807
Radek Krejci76512572015-08-04 09:47:08 +02002808 case LYS_INPUT:
2809 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002810 io = calloc(1, sizeof *io);
2811 retval = (struct lys_node *)io;
2812 break;
2813
Radek Krejci76512572015-08-04 09:47:08 +02002814 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002815 ntf = calloc(1, sizeof *ntf);
2816 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002817 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002818
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002819 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01002820 LOGINT(ctx);
Michal Vasko49168a22015-08-17 16:35:41 +02002821 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002822 }
Michal Vasko53b7da02018-02-13 15:28:42 +01002823 LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL);
Michal Vasko253035f2015-12-17 16:58:13 +01002824
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002825 /*
2826 * duplicate generic part of the structure
2827 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002828 retval->name = lydict_insert(ctx, node->name, 0);
2829 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2830 retval->ref = lydict_insert(ctx, node->ref, 0);
2831 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002832
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002833 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002834 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002835
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002836 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002837
Radek Krejcif0bb3602017-01-25 17:05:08 +01002838 retval->ext_size = node->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002839 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 +01002840 goto error;
2841 }
2842
Radek Krejci06214042016-11-04 16:25:58 +01002843 if (node->iffeature_size) {
2844 retval->iffeature_size = node->iffeature_size;
2845 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01002846 LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error);
Michal Vasko253035f2015-12-17 16:58:13 +01002847 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002848
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002849 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002850 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002851 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2852 if (size1) {
2853 /* there is something to duplicate */
2854
2855 /* duplicate compiled expression */
2856 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2857 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01002858 LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002859 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2860
2861 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002862 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01002863 LY_CHECK_ERR_GOTO(!retval->iffeature[i].features, LOGMEM(ctx); free(retval->iffeature[i].expr), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002864
Radek Krejci9ff0a922016-07-14 13:08:05 +02002865 for (j = 0; (unsigned int)j < size2; j++) {
2866 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2867 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002868 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002869 /* feature is resolved in origin, so copy it
2870 * - duplication is used for instantiating groupings
2871 * and if-feature inside grouping is supposed to be
2872 * resolved inside the original grouping, so we want
2873 * to keep pointers to features from the grouping
2874 * context */
2875 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2876 } else if (rc == -1) {
2877 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002878 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002879 }
2880 }
Radek Krejcif0bb3602017-01-25 17:05:08 +01002881
2882 /* duplicate if-feature's extensions */
2883 retval->iffeature[i].ext_size = node->iffeature[i].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002884 if (lys_ext_dup(ctx, module, node->iffeature[i].ext, node->iffeature[i].ext_size,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002885 &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002886 goto error;
2887 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002888 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002889
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002890 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002891 p = parent;
2892 do {
2893 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2894 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002895 if (iter) {
2896 flags = iter->flags & LYS_CONFIG_MASK;
2897 } else {
2898 /* default */
2899 flags = LYS_CONFIG_W;
2900 }
2901
2902 switch (finalize) {
2903 case 1:
2904 /* inherit config flags */
2905 if (retval->flags & LYS_CONFIG_SET) {
2906 /* skip nodes with an explicit config value */
2907 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002908 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2909 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002910 goto error;
2911 }
2912 break;
2913 }
2914
2915 if (retval->nodetype != LYS_USES) {
2916 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2917 }
Radek Krejci2cc25322017-09-06 16:32:02 +02002918
2919 /* inherit status */
Radek Krejcie2807ea2017-09-07 10:52:21 +02002920 if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) {
2921 /* but do it only in case the parent has a stonger status */
2922 retval->flags &= ~LYS_STATUS_MASK;
2923 retval->flags |= (parent->flags & LYS_STATUS_MASK);
2924 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002925 break;
2926 case 2:
2927 /* erase config flags */
2928 retval->flags &= ~LYS_CONFIG_MASK;
2929 retval->flags &= ~LYS_CONFIG_SET;
2930 break;
2931 }
2932
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002933 /* connect it to the parent */
2934 if (lys_node_addchild(parent, retval->module, retval)) {
2935 goto error;
2936 }
Radek Krejcidce51452015-06-16 15:20:08 +02002937
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002938 /* go recursively */
2939 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002940 LY_TREE_FOR(node->child, iter) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002941 if (iter->nodetype & LYS_GROUPING) {
2942 /* do not instantiate groupings */
2943 continue;
2944 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01002945 if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002946 goto error;
2947 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002948 }
2949 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002950
2951 if (finalize == 1) {
2952 /* check that configuration lists have keys
2953 * - we really want to check keys_size in original node, because the keys are
2954 * not yet resolved here, it is done below in nodetype specific part */
2955 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2956 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002957 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002958 goto error;
2959 }
2960 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002961 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002962 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002963 }
2964
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002965 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002966 * duplicate specific part of the structure
2967 */
2968 switch (node->nodetype) {
2969 case LYS_CONTAINER:
2970 if (cont_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002971 cont->when = lys_when_dup(module, cont_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002972 LY_CHECK_GOTO(!cont->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002973 }
2974 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002975
Radek Krejcia8d111f2017-05-31 13:57:37 +02002976 if (cont_orig->must) {
2977 cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres);
2978 LY_CHECK_GOTO(!cont->must, error);
2979 cont->must_size = cont_orig->must_size;
2980 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002981
Radek Krejcif0bb3602017-01-25 17:05:08 +01002982 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
2983
2984 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002985 case LYS_CHOICE:
2986 if (choice_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002987 choice->when = lys_when_dup(module, choice_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002988 LY_CHECK_GOTO(!choice->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002989 }
2990
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002991 if (!shallow) {
2992 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002993 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2994 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2995 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002996 if (rc) {
2997 if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002998 LOGINT(ctx);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002999 }
3000 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02003001 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003002 } else {
3003 /* useless to check return value, we don't know whether
3004 * there really wasn't any default defined or it just hasn't
3005 * been resolved, we just hope for the best :)
3006 */
3007 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02003008 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003009 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003010 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003011 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003012 break;
3013
3014 case LYS_LEAF:
Radek Krejcib53154b2017-07-19 09:14:13 +02003015 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), lys_ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02003016 goto error;
3017 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003018 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
3019
3020 if (leaf_orig->dflt) {
3021 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejcib53154b2017-07-19 09:14:13 +02003022 if (!lys_ingrouping(retval) || (leaf->type.base != LY_TYPE_LEAFREF)) {
Radek Krejci968f6942017-03-25 15:55:01 -05003023 /* problem is when it is an identityref referencing an identity from a module
3024 * and we are using the grouping in a different module */
Radek Krejcid06f5a42017-03-25 16:03:42 -05003025 if (leaf->type.base == LY_TYPE_IDENT) {
Michal Vaskofe31cc02017-03-10 15:52:31 +01003026 tmp_mod = leaf_orig->module;
3027 } else {
3028 tmp_mod = module;
3029 }
3030 if (unres_schema_add_node(tmp_mod, unres, &leaf->type, UNRES_TYPE_DFLT,
3031 (struct lys_node *)(&leaf->dflt)) == -1) {
3032 goto error;
3033 }
Michal Vasko49168a22015-08-17 16:35:41 +02003034 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003035 }
3036
Radek Krejcia8d111f2017-05-31 13:57:37 +02003037 if (leaf_orig->must) {
3038 leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres);
3039 LY_CHECK_GOTO(!leaf->must, error);
3040 leaf->must_size = leaf_orig->must_size;
3041 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003042
3043 if (leaf_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003044 leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003045 LY_CHECK_GOTO(!leaf->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003046 }
3047 break;
3048
3049 case LYS_LEAFLIST:
Radek Krejcib53154b2017-07-19 09:14:13 +02003050 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), lys_ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02003051 goto error;
3052 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003053 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
3054
3055 llist->min = llist_orig->min;
3056 llist->max = llist_orig->max;
3057
Radek Krejcia8d111f2017-05-31 13:57:37 +02003058 if (llist_orig->must) {
3059 llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres);
3060 LY_CHECK_GOTO(!llist->must, error);
3061 llist->must_size = llist_orig->must_size;
3062 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003063
Radek Krejcia8d111f2017-05-31 13:57:37 +02003064 if (llist_orig->dflt) {
3065 llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01003066 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003067 llist->dflt_size = llist_orig->dflt_size;
Radek Krejcia8d111f2017-05-31 13:57:37 +02003068
Radek Krejcic699e422017-06-02 15:18:58 +02003069 for (i = 0; i < llist->dflt_size; i++) {
3070 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
Radek Krejcib53154b2017-07-19 09:14:13 +02003071 if (!lys_ingrouping(retval) || (llist->type.base != LY_TYPE_LEAFREF)) {
Radek Krejcic699e422017-06-02 15:18:58 +02003072 if ((llist->type.base == LY_TYPE_IDENT) && !strchr(llist->dflt[i], ':') && (module != llist_orig->module)) {
3073 tmp_mod = llist_orig->module;
3074 } else {
3075 tmp_mod = module;
3076 }
3077 if (unres_schema_add_node(tmp_mod, unres, &llist->type, UNRES_TYPE_DFLT,
3078 (struct lys_node *)(&llist->dflt[i])) == -1) {
3079 goto error;
3080 }
Michal Vaskofe31cc02017-03-10 15:52:31 +01003081 }
Radek Krejci51673202016-11-01 17:00:32 +01003082 }
3083 }
3084
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003085 if (llist_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003086 llist->when = lys_when_dup(module, llist_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003087 }
3088 break;
3089
3090 case LYS_LIST:
3091 list->min = list_orig->min;
3092 list->max = list_orig->max;
3093
Radek Krejcia8d111f2017-05-31 13:57:37 +02003094 if (list_orig->must) {
3095 list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres);
3096 LY_CHECK_GOTO(!list->must, error);
3097 list->must_size = list_orig->must_size;
3098 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003099
Radek Krejcif0bb3602017-01-25 17:05:08 +01003100 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Michal Vasko38d01f72015-06-15 09:41:06 +02003101
Radek Krejcia8d111f2017-05-31 13:57:37 +02003102 if (list_orig->keys_size) {
3103 list->keys = calloc(list_orig->keys_size, sizeof *list->keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01003104 LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error);
Radek Krejci5c08a992016-11-02 13:30:04 +01003105 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003106 list->keys_size = list_orig->keys_size;
Michal Vasko0ea41032015-06-16 08:53:55 +02003107
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003108 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01003109 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
3110 * in another grouping */
3111 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
3112 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
3113 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02003114 }
Michal Vasko0ea41032015-06-16 08:53:55 +02003115 } else {
Radek Krejcia8d111f2017-05-31 13:57:37 +02003116 memcpy(list->keys, list_orig->keys, list_orig->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02003117 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003118 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003119
Radek Krejcia8d111f2017-05-31 13:57:37 +02003120 if (list_orig->unique) {
3121 list->unique = malloc(list_orig->unique_size * sizeof *list->unique);
Michal Vasko53b7da02018-02-13 15:28:42 +01003122 LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003123 list->unique_size = list_orig->unique_size;
Radek Krejci581ce772015-11-10 17:22:40 +01003124
Radek Krejcic699e422017-06-02 15:18:58 +02003125 for (i = 0; i < list->unique_size; ++i) {
3126 list->unique[i].expr = malloc(list_orig->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01003127 LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error);
Radek Krejcic699e422017-06-02 15:18:58 +02003128 list->unique[i].expr_size = list_orig->unique[i].expr_size;
3129 for (j = 0; j < list->unique[i].expr_size; j++) {
3130 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
3131
3132 /* if it stays in unres list, duplicate it also there */
3133 unique_info = malloc(sizeof *unique_info);
Michal Vasko53b7da02018-02-13 15:28:42 +01003134 LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error);
Radek Krejcic699e422017-06-02 15:18:58 +02003135 unique_info->list = (struct lys_node *)list;
3136 unique_info->expr = list->unique[i].expr[j];
3137 unique_info->trg_type = &list->unique[i].trg_type;
3138 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
3139 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003140 }
3141 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003142
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003143 if (list_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003144 list->when = lys_when_dup(module, list_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003145 LY_CHECK_GOTO(!list->when, error);
Radek Krejciefaeba32015-05-27 14:30:57 +02003146 }
Radek Krejcidce51452015-06-16 15:20:08 +02003147 break;
3148
3149 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02003150 case LYS_ANYDATA:
Radek Krejcia8d111f2017-05-31 13:57:37 +02003151 if (any_orig->must) {
3152 any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres);
3153 LY_CHECK_GOTO(!any->must, error);
3154 any->must_size = any_orig->must_size;
3155 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003156
Radek Krejcibf2abff2016-08-23 15:51:52 +02003157 if (any_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003158 any->when = lys_when_dup(module, any_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003159 LY_CHECK_GOTO(!any->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003160 }
3161 break;
3162
3163 case LYS_USES:
3164 uses->grp = uses_orig->grp;
3165
3166 if (uses_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003167 uses->when = lys_when_dup(module, uses_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003168 LY_CHECK_GOTO(!uses->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003169 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01003170 /* it is not needed to duplicate refine, nor augment. They are already applied to the uses children */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003171 break;
3172
3173 case LYS_CASE:
3174 if (cs_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003175 cs->when = lys_when_dup(module, cs_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003176 LY_CHECK_GOTO(!cs->when, error);
Radek Krejcidce51452015-06-16 15:20:08 +02003177 }
3178 break;
3179
Radek Krejci96935402016-11-04 16:27:28 +01003180 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02003181 case LYS_RPC:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003182 case LYS_INPUT:
3183 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02003184 case LYS_NOTIF:
Radek Krejcif0bb3602017-01-25 17:05:08 +01003185 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003186 break;
3187
3188 default:
3189 /* LY_NODE_AUGMENT */
Michal Vasko53b7da02018-02-13 15:28:42 +01003190 LOGINT(ctx);
Michal Vasko49168a22015-08-17 16:35:41 +02003191 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003192 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003193
3194 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02003195
3196error:
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003197 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02003198 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003199}
3200
Radek Krejcib3142312016-11-09 11:04:12 +01003201int
3202lys_has_xpath(const struct lys_node *node)
3203{
3204 assert(node);
3205
3206 switch (node->nodetype) {
3207 case LYS_AUGMENT:
3208 if (((struct lys_node_augment *)node)->when) {
3209 return 1;
3210 }
3211 break;
3212 case LYS_CASE:
3213 if (((struct lys_node_case *)node)->when) {
3214 return 1;
3215 }
3216 break;
3217 case LYS_CHOICE:
3218 if (((struct lys_node_choice *)node)->when) {
3219 return 1;
3220 }
3221 break;
3222 case LYS_ANYDATA:
3223 if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) {
3224 return 1;
3225 }
3226 break;
3227 case LYS_LEAF:
3228 if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) {
3229 return 1;
3230 }
3231 break;
3232 case LYS_LEAFLIST:
3233 if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) {
3234 return 1;
3235 }
3236 break;
3237 case LYS_LIST:
3238 if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) {
3239 return 1;
3240 }
3241 break;
3242 case LYS_CONTAINER:
3243 if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) {
3244 return 1;
3245 }
3246 break;
3247 case LYS_INPUT:
3248 case LYS_OUTPUT:
3249 if (((struct lys_node_inout *)node)->must_size) {
3250 return 1;
3251 }
3252 break;
3253 case LYS_NOTIF:
3254 if (((struct lys_node_notif *)node)->must_size) {
3255 return 1;
3256 }
3257 break;
3258 case LYS_USES:
3259 if (((struct lys_node_uses *)node)->when) {
3260 return 1;
3261 }
3262 break;
3263 default:
3264 /* does not have XPath */
3265 break;
3266 }
3267
3268 return 0;
3269}
3270
Michal Vasko568b1952018-01-30 15:53:30 +01003271int
3272lys_type_is_local(const struct lys_type *type)
3273{
3274 if (!type->der->module) {
3275 /* build-in type */
3276 return 1;
3277 }
3278 /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */
3279 return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module));
3280}
3281
Radek Krejci2cc25322017-09-06 16:32:02 +02003282/*
3283 * shallow -
3284 * - do not inherit status from the parent
3285 */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003286struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01003287lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003288 struct unres_schema *unres, int shallow)
3289{
3290 struct lys_node *p = NULL;
Radek Krejcib3142312016-11-09 11:04:12 +01003291 int finalize = 0;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003292 struct lys_node *result, *iter, *next;
3293
3294 if (!shallow) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01003295 /* 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 +02003296 for (p = parent;
3297 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
3298 p = lys_parent(p));
3299 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
3300 }
3301
Radek Krejci6ff885d2017-01-03 14:06:22 +01003302 result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize);
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003303 if (finalize) {
3304 /* check xpath expressions in the instantiated tree */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003305 for (iter = next = result; iter; iter = next) {
Radek Krejcib3142312016-11-09 11:04:12 +01003306 if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
Radek Krejci3c48d042016-11-07 14:42:36 +01003307 /* invalid xpath */
3308 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003309 }
3310
3311 /* select next item */
3312 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
3313 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
3314 next = NULL;
3315 } else {
3316 next = iter->child;
3317 }
3318 if (!next) {
3319 /* no children, try siblings */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003320 if (iter == result) {
3321 /* we are done, no next element to process */
3322 break;
3323 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003324 next = iter->next;
3325 }
3326 while (!next) {
3327 /* parent is already processed, go to its sibling */
3328 iter = lys_parent(iter);
Radek Krejci7212e0a2017-03-08 15:58:22 +01003329 if (lys_parent(iter) == lys_parent(result)) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003330 /* we are done, no next element to process */
3331 break;
3332 }
3333 next = iter->next;
3334 }
3335 }
3336 }
3337
3338 return result;
3339}
3340
Michal Vasko13b15832015-08-19 11:04:48 +02003341void
Michal Vaskoff006c12016-02-17 11:15:19 +01003342lys_node_switch(struct lys_node *dst, struct lys_node *src)
3343{
3344 struct lys_node *child;
3345
Michal Vaskob42b6972016-06-06 14:21:30 +02003346 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01003347
3348 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02003349 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01003350 dst->prev->next = src;
3351 }
3352
3353 /* sibling prev */
3354 if (dst->next) {
3355 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02003356 } else {
3357 for (child = dst->prev; child->prev->next; child = child->prev);
3358 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003359 }
3360
3361 /* next */
3362 src->next = dst->next;
3363 dst->next = NULL;
3364
3365 /* prev */
3366 if (dst->prev != dst) {
3367 src->prev = dst->prev;
3368 }
3369 dst->prev = dst;
3370
3371 /* parent child */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003372 if (dst->parent) {
3373 if (dst->parent->child == dst) {
3374 dst->parent->child = src;
3375 }
Radek Krejci115fa882017-03-01 16:15:07 +01003376 } else if (lys_main_module(dst->module)->data == dst) {
3377 lys_main_module(dst->module)->data = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003378 }
3379
3380 /* parent */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003381 src->parent = dst->parent; dst->parent = NULL;
3382
Michal Vaskoff006c12016-02-17 11:15:19 +01003383
3384 /* child parent */
3385 LY_TREE_FOR(dst->child, child) {
3386 if (child->parent == dst) {
3387 child->parent = src;
3388 }
3389 }
3390
3391 /* child */
3392 src->child = dst->child;
3393 dst->child = NULL;
Radek Krejcif0bb3602017-01-25 17:05:08 +01003394
3395 /* node-specific data */
3396 switch (dst->nodetype) {
3397 case LYS_CONTAINER:
3398 ((struct lys_node_container *)src)->tpdf_size = ((struct lys_node_container *)dst)->tpdf_size;
3399 ((struct lys_node_container *)src)->tpdf = ((struct lys_node_container *)dst)->tpdf;
3400 ((struct lys_node_container *)dst)->tpdf_size = 0;
3401 ((struct lys_node_container *)dst)->tpdf = NULL;
3402 break;
3403 case LYS_LIST:
3404 ((struct lys_node_list *)src)->tpdf_size = ((struct lys_node_list *)dst)->tpdf_size;
3405 ((struct lys_node_list *)src)->tpdf = ((struct lys_node_list *)dst)->tpdf;
3406 ((struct lys_node_list *)dst)->tpdf_size = 0;
3407 ((struct lys_node_list *)dst)->tpdf = NULL;
3408 break;
3409 case LYS_RPC:
3410 case LYS_ACTION:
3411 ((struct lys_node_rpc_action *)src)->tpdf_size = ((struct lys_node_rpc_action *)dst)->tpdf_size;
3412 ((struct lys_node_rpc_action *)src)->tpdf = ((struct lys_node_rpc_action *)dst)->tpdf;
3413 ((struct lys_node_rpc_action *)dst)->tpdf_size = 0;
3414 ((struct lys_node_rpc_action *)dst)->tpdf = NULL;
3415 break;
3416 case LYS_NOTIF:
3417 ((struct lys_node_notif *)src)->tpdf_size = ((struct lys_node_notif *)dst)->tpdf_size;
3418 ((struct lys_node_notif *)src)->tpdf = ((struct lys_node_notif *)dst)->tpdf;
3419 ((struct lys_node_notif *)dst)->tpdf_size = 0;
3420 ((struct lys_node_notif *)dst)->tpdf = NULL;
3421 break;
3422 case LYS_INPUT:
3423 case LYS_OUTPUT:
3424 ((struct lys_node_inout *)src)->tpdf_size = ((struct lys_node_inout *)dst)->tpdf_size;
3425 ((struct lys_node_inout *)src)->tpdf = ((struct lys_node_inout *)dst)->tpdf;
3426 ((struct lys_node_inout *)dst)->tpdf_size = 0;
3427 ((struct lys_node_inout *)dst)->tpdf = NULL;
3428 break;
3429 default:
3430 /* nothing special */
3431 break;
3432 }
3433
Michal Vaskoff006c12016-02-17 11:15:19 +01003434}
3435
3436void
Michal Vasko10681e82018-01-16 14:54:16 +01003437lys_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 +02003438{
3439 struct ly_ctx *ctx;
3440 int i;
3441
3442 if (!module) {
3443 return;
3444 }
3445
3446 /* remove schema from the context */
3447 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01003448 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02003449 for (i = 0; i < ctx->models.used; i++) {
3450 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01003451 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003452 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01003453 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 +02003454 ctx->models.list[ctx->models.used] = NULL;
3455 /* we are done */
3456 break;
3457 }
3458 }
3459 }
3460
3461 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01003462 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003463
Michal Vasko10681e82018-01-16 14:54:16 +01003464 /* include */
3465 for (i = 0; i < module->inc_size; i++) {
3466 lydict_remove(ctx, module->inc[i].dsc);
3467 lydict_remove(ctx, module->inc[i].ref);
3468 lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor);
3469 /* complete submodule free is done only from main module since
3470 * submodules propagate their includes to the main module */
3471 if (free_subs) {
3472 lys_submodule_free(module->inc[i].submodule, private_destructor);
3473 }
3474 }
3475 free(module->inc);
3476
Radek Krejcida04f4a2015-05-21 12:54:09 +02003477 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01003478 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003479
3480 free(module);
3481}
Radek Krejci7e97c352015-06-19 16:26:34 +02003482
Radek Krejci9de2c042016-10-19 16:53:06 +02003483static void
3484lys_features_disable_recursive(struct lys_feature *f)
3485{
3486 unsigned int i;
3487 struct lys_feature *depf;
3488
3489 /* disable the feature */
3490 f->flags &= ~LYS_FENABLED;
3491
3492 /* by disabling feature we have to disable also all features that depends on this feature */
3493 if (f->depfeatures) {
3494 for (i = 0; i < f->depfeatures->number; i++) {
3495 depf = (struct lys_feature *)f->depfeatures->set.g[i];
3496 if (depf->flags & LYS_FENABLED) {
3497 lys_features_disable_recursive(depf);
3498 }
3499 }
3500 }
3501}
3502
3503
Radek Krejci7e97c352015-06-19 16:26:34 +02003504/*
3505 * op: 1 - enable, 0 - disable
3506 */
3507static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003508lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02003509{
3510 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003511 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02003512 int progress, faili, failj, failk;
3513
3514 uint8_t fsize;
3515 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02003516
3517 if (!module || !name || !strlen(name)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003518 LOGARG;
Radek Krejci7e97c352015-06-19 16:26:34 +02003519 return EXIT_FAILURE;
3520 }
3521
3522 if (!strcmp(name, "*")) {
3523 /* enable all */
3524 all = 1;
3525 }
3526
Radek Krejcia889c1f2016-10-19 15:50:11 +02003527 progress = failk = 1;
3528 while (progress && failk) {
3529 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
3530 if (i == -1) {
3531 fsize = module->features_size;
3532 f = module->features;
3533 } else {
3534 fsize = module->inc[i].submodule->features_size;
3535 f = module->inc[i].submodule->features;
3536 }
3537
3538 for (j = 0; j < fsize; j++) {
3539 if (all || !strcmp(f[j].name, name)) {
Michal Vasko34c3b552016-11-21 09:07:43 +01003540 if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) {
3541 if (all) {
3542 /* skip already set features */
3543 continue;
3544 } else {
3545 /* feature already set correctly */
3546 return EXIT_SUCCESS;
3547 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003548 }
3549
3550 if (op) {
3551 /* check referenced features if they are enabled */
3552 for (k = 0; k < f[j].iffeature_size; k++) {
3553 if (!resolve_iffeature(&f[j].iffeature[k])) {
3554 if (all) {
3555 faili = i;
3556 failj = j;
3557 failk = k + 1;
3558 break;
3559 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003560 LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
Radek Krejcia889c1f2016-10-19 15:50:11 +02003561 f[j].name, k + 1);
3562 return EXIT_FAILURE;
3563 }
3564 }
3565 }
3566
3567 if (k == f[j].iffeature_size) {
3568 /* the last check passed, do the change */
3569 f[j].flags |= LYS_FENABLED;
3570 progress++;
3571 }
3572 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02003573 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02003574 progress++;
3575 }
3576 if (!all) {
3577 /* stop in case changing a single feature */
3578 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003579 }
3580 }
Radek Krejci7e97c352015-06-19 16:26:34 +02003581 }
3582 }
3583 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003584 if (failk) {
3585 /* print info about the last failing feature */
Michal Vasko53b7da02018-02-13 15:28:42 +01003586 LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
Radek Krejcia889c1f2016-10-19 15:50:11 +02003587 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
3588 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02003589 }
3590
3591 if (all) {
3592 return EXIT_SUCCESS;
3593 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02003594 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02003595 return EXIT_FAILURE;
3596 }
3597}
3598
3599API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003600lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003601{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003602 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02003603}
3604
3605API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003606lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003607{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003608 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003609}
3610
3611API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003612lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003613{
3614 int i, j;
3615
3616 if (!module || !feature) {
3617 return -1;
3618 }
3619
3620 /* search for the specified feature */
3621 /* module itself */
3622 for (i = 0; i < module->features_size; i++) {
3623 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003624 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003625 return 1;
3626 } else {
3627 return 0;
3628 }
3629 }
3630 }
3631
3632 /* submodules */
3633 for (j = 0; j < module->inc_size; j++) {
3634 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
3635 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003636 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003637 return 1;
3638 } else {
3639 return 0;
3640 }
3641 }
3642 }
3643 }
3644
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003645 /* feature definition not found */
3646 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02003647}
Michal Vasko2367e7c2015-07-07 11:33:44 +02003648
Radek Krejci96a10da2015-07-30 11:00:14 +02003649API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01003650lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02003651{
Radek Krejci96a10da2015-07-30 11:00:14 +02003652 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003653 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003654 unsigned int count;
3655
3656 if (!module) {
3657 return NULL;
3658 }
3659
3660 count = module->features_size;
3661 for (i = 0; i < module->inc_size; i++) {
3662 count += module->inc[i].submodule->features_size;
3663 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003664 result = malloc((count + 1) * sizeof *result);
Michal Vasko53b7da02018-02-13 15:28:42 +01003665 LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003666
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003667 if (states) {
3668 *states = malloc((count + 1) * sizeof **states);
Michal Vasko53b7da02018-02-13 15:28:42 +01003669 LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL);
Michal Vasko2367e7c2015-07-07 11:33:44 +02003670 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003671 count = 0;
3672
3673 /* module itself */
3674 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003675 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003676 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003677 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003678 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003679 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003680 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003681 }
3682 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003683 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003684 }
3685
3686 /* submodules */
3687 for (j = 0; j < module->inc_size; j++) {
3688 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003689 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003690 if (states) {
3691 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3692 (*states)[count] = 1;
3693 } else {
3694 (*states)[count] = 0;
3695 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003696 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003697 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003698 }
3699 }
3700
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003701 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003702 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003703
3704 return result;
3705}
Michal Vaskobaefb032015-09-24 14:52:10 +02003706
Radek Krejci6910a032016-04-13 10:06:21 +02003707API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003708lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003709{
Michal Vaskof53187d2017-01-13 13:23:14 +01003710 if (!node) {
3711 return NULL;
3712 }
3713
Radek Krejcic071c542016-01-27 14:57:51 +01003714 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3715}
3716
Radek Krejci6910a032016-04-13 10:06:21 +02003717API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003718lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003719{
Michal Vaskof53187d2017-01-13 13:23:14 +01003720 if (!module) {
3721 return NULL;
3722 }
3723
Michal Vasko320e8532016-02-15 13:11:57 +01003724 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3725}
3726
Michal Vaskobaefb032015-09-24 14:52:10 +02003727API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003728lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003729{
Radek Krejcif95b6292017-02-13 15:57:37 +01003730 struct lys_node *parent;
3731
3732 if (!node) {
Michal Vaskobaefb032015-09-24 14:52:10 +02003733 return NULL;
3734 }
3735
Radek Krejcif95b6292017-02-13 15:57:37 +01003736 if (node->nodetype == LYS_EXT) {
3737 if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) {
3738 return NULL;
3739 }
3740 parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent;
3741 } else if (!node->parent) {
3742 return NULL;
3743 } else {
3744 parent = node->parent;
Michal Vaskobaefb032015-09-24 14:52:10 +02003745 }
3746
Radek Krejcif95b6292017-02-13 15:57:37 +01003747 if (parent->nodetype == LYS_AUGMENT) {
3748 return ((struct lys_node_augment *)parent)->target;
3749 } else {
3750 return parent;
3751 }
3752}
3753
3754struct lys_node **
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003755lys_child(const struct lys_node *node, LYS_NODE nodetype)
Radek Krejcif95b6292017-02-13 15:57:37 +01003756{
3757 void *pp;
3758 assert(node);
3759
3760 if (node->nodetype == LYS_EXT) {
3761 pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL);
3762 if (!pp) {
3763 return NULL;
3764 }
3765 return (struct lys_node **)pp;
Michal Vasko54d4c202017-08-09 14:09:18 +02003766 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Radek Krejcif7fe2cb2017-08-09 10:27:12 +02003767 return NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01003768 } else {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003769 return (struct lys_node **)&node->child;
Radek Krejcif95b6292017-02-13 15:57:37 +01003770 }
Michal Vaskobaefb032015-09-24 14:52:10 +02003771}
Michal Vasko1b229152016-01-13 11:28:38 +01003772
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003773API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003774lys_set_private(const struct lys_node *node, void *priv)
3775{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003776 void *prev;
3777
Michal Vasko1b229152016-01-13 11:28:38 +01003778 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003779 LOGARG;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003780 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003781 }
3782
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003783 prev = node->priv;
3784 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003785
3786 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003787}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003788
Michal Vasko01c6fd22016-05-20 11:43:05 +02003789int
3790lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3791{
Michal Vasko53b7da02018-02-13 15:28:42 +01003792 struct lys_node_leaf *iter;
3793 struct ly_ctx *ctx = leafref_target->module->ctx;
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003794
Michal Vasko48a573d2016-07-01 11:46:02 +02003795 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003796 LOGINT(ctx);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003797 return -1;
3798 }
3799
Pavol Vican93175152016-08-30 15:34:44 +02003800 /* check for config flag */
Radek Krejcic688ca02017-03-20 12:54:39 +01003801 if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 &&
3802 (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003803 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref,
Radek Krejcid831dd42017-03-16 12:59:30 +01003804 "The leafref %s is config but refers to a non-config %s.",
Pavol Vican93175152016-08-30 15:34:44 +02003805 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3806 return -1;
3807 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003808 /* check for cycles */
Michal Vasko53b7da02018-02-13 15:28:42 +01003809 for (iter = leafref_target; iter && iter->type.base == LY_TYPE_LEAFREF; iter = iter->type.info.lref.target) {
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003810 if ((void *)iter == (void *)leafref) {
3811 /* cycle detected */
Michal Vasko53b7da02018-02-13 15:28:42 +01003812 LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003813 return -1;
3814 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003815 }
3816
3817 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003818 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003819 if (!leafref_target->backlinks) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003820 leafref_target->backlinks = (void *)ly_set_new();
Radek Krejci85a54be2016-10-20 12:39:56 +02003821 if (!leafref_target->backlinks) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003822 LOGMEM(ctx);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003823 return -1;
3824 }
3825 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003826 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003827
3828 return 0;
3829}
3830
Michal Vasko8548e082016-07-22 12:00:18 +02003831/* not needed currently */
3832#if 0
3833
Michal Vasko5b3492c2016-07-20 09:37:40 +02003834static const char *
3835lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3836{
3837 struct lys_module *prev_mod;
3838 uint32_t str_len, mod_len, buf_idx;
3839
Radek Krejcibf2abff2016-08-23 15:51:52 +02003840 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003841 LOGINT;
3842 return NULL;
3843 }
3844
3845 buf_idx = buf_len - 1;
3846 buf[buf_idx] = '\0';
3847
3848 while (node) {
3849 if (lys_parent(node)) {
3850 prev_mod = lys_node_module(lys_parent(node));
3851 } else {
3852 prev_mod = NULL;
3853 }
3854
Radek Krejcibf2abff2016-08-23 15:51:52 +02003855 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003856 str_len = strlen(node->name);
3857
3858 if (prev_mod != node->module) {
3859 mod_len = strlen(node->module->name);
3860 } else {
3861 mod_len = 0;
3862 }
3863
3864 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3865 LOGINT;
3866 return NULL;
3867 }
3868
3869 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3870
3871 buf[buf_idx] = '/';
3872 if (mod_len) {
3873 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3874 buf[buf_idx + 1 + mod_len] = ':';
3875 }
3876 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3877 }
3878
3879 node = lys_parent(node);
3880 }
3881
3882 return buf + buf_idx;
3883}
3884
Michal Vasko8548e082016-07-22 12:00:18 +02003885#endif
3886
3887API struct ly_set *
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003888lys_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 +02003889{
Michal Vasko508a50d2016-09-07 14:50:33 +02003890 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003891 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003892 uint32_t i;
3893
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003894 if (!ctx_node || !expr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003895 LOGARG;
Michal Vasko8548e082016-07-22 12:00:18 +02003896 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003897 }
3898
Michal Vaskob94a5e42016-09-08 14:01:56 +02003899 /* adjust the root */
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003900 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
Michal Vaskob94a5e42016-09-08 14:01:56 +02003901 do {
Michal Vaskocb45f472018-02-12 10:47:42 +01003902 ctx_node = lys_getnext(NULL, NULL, lys_node_module(ctx_node), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003903 } while ((ctx_node_type == LYXP_NODE_ROOT_CONFIG) && (ctx_node->flags & LYS_CONFIG_R));
Michal Vaskob94a5e42016-09-08 14:01:56 +02003904 }
3905
Michal Vasko508a50d2016-09-07 14:50:33 +02003906 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003907
3908 if (options & LYXP_MUST) {
3909 options &= ~LYXP_MUST;
3910 options |= LYXP_SNODE_MUST;
3911 } else if (options & LYXP_WHEN) {
3912 options &= ~LYXP_WHEN;
3913 options |= LYXP_SNODE_WHEN;
3914 } else {
3915 options |= LYXP_SNODE;
3916 }
3917
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003918 if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003919 free(set.val.snodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01003920 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 +02003921 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003922 }
3923
Michal Vasko8548e082016-07-22 12:00:18 +02003924 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003925
Michal Vasko508a50d2016-09-07 14:50:33 +02003926 for (i = 0; i < set.used; ++i) {
3927 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003928 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003929 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003930 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003931 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003932 return NULL;
3933 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003934 break;
3935 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003936 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003937 break;
3938 }
3939 }
3940
Michal Vasko508a50d2016-09-07 14:50:33 +02003941 free(set.val.snodes);
3942 return ret_set;
3943}
3944
3945API struct ly_set *
3946lys_node_xpath_atomize(const struct lys_node *node, int options)
3947{
3948 const struct lys_node *next, *elem, *parent, *tmp;
3949 struct lyxp_set set;
3950 struct ly_set *ret_set;
3951 uint16_t i;
3952
3953 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003954 LOGARG;
Michal Vasko508a50d2016-09-07 14:50:33 +02003955 return NULL;
3956 }
3957
3958 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3959 if (!parent) {
3960 /* not in input, output, or notification */
3961 return NULL;
3962 }
3963
3964 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003965 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003966 return NULL;
3967 }
3968
3969 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoc04173b2018-03-09 10:43:22 +01003970 if ((options & LYXP_NO_LOCAL) && !(elem->flags & (LYS_XPCONF_DEP | LYS_XPSTATE_DEP))) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003971 /* elem has no dependencies from other subtrees and local nodes get discarded */
3972 goto next_iter;
3973 }
3974
Michal Vaskof96dfb62017-08-17 12:23:49 +02003975 if (lyxp_node_atomize(elem, &set, 0)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003976 ly_set_free(ret_set);
3977 free(set.val.snodes);
3978 return NULL;
3979 }
3980
3981 for (i = 0; i < set.used; ++i) {
3982 switch (set.val.snodes[i].type) {
3983 case LYXP_NODE_ELEM:
3984 if (options & LYXP_NO_LOCAL) {
3985 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3986 if (tmp) {
3987 /* in local subtree, discard */
3988 break;
3989 }
3990 }
3991 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3992 ly_set_free(ret_set);
3993 free(set.val.snodes);
3994 return NULL;
3995 }
3996 break;
3997 default:
3998 /* ignore roots, text and attr should not ever appear */
3999 break;
4000 }
4001 }
4002
4003 free(set.val.snodes);
4004 if (!(options & LYXP_RECURSIVE)) {
4005 break;
4006 }
4007next_iter:
4008 LY_TREE_DFS_END(node, next, elem);
4009 }
4010
Michal Vasko8548e082016-07-22 12:00:18 +02004011 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02004012}
4013
Michal Vasko44ab1462017-05-18 13:18:36 +02004014/* logs */
4015int
4016apply_aug(struct lys_node_augment *augment, struct unres_schema *unres)
Radek Krejci0ec51da2016-12-14 16:42:03 +01004017{
Michal Vasko44ab1462017-05-18 13:18:36 +02004018 struct lys_node *child, *parent;
4019 int clear_config;
4020 unsigned int u;
Michal Vaskod02e30e2018-01-22 13:35:48 +01004021 uint8_t *v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004022 struct lys_ext_instance *ext;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004023
4024 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
4025
Radek Krejcic9d78692017-08-24 17:17:18 +02004026 if (!augment->child) {
4027 /* nothing to apply */
4028 goto success;
4029 }
4030
Michal Vaskobb520442017-05-23 10:55:18 +02004031 /* check that all the modules are implemented */
4032 for (parent = augment->target; parent; parent = lys_parent(parent)) {
4033 if (!lys_node_module(parent)->implemented) {
4034 if (lys_set_implemented(lys_node_module(parent))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004035 LOGERR(augment->module->ctx, ly_errno, "Making the augment target module \"%s\" implemented failed.",
4036 lys_node_module(parent)->name);
Michal Vaskobb520442017-05-23 10:55:18 +02004037 return -1;
4038 }
Michal Vaskobb520442017-05-23 10:55:18 +02004039 }
4040 }
4041
Radek Krejci0ec51da2016-12-14 16:42:03 +01004042 /* reconnect augmenting data into the target - add them to the target child list */
4043 if (augment->target->child) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004044 child = augment->target->child->prev;
4045 child->next = augment->child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004046 augment->target->child->prev = augment->child->prev;
Michal Vasko44ab1462017-05-18 13:18:36 +02004047 augment->child->prev = child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004048 } else {
4049 augment->target->child = augment->child;
4050 }
4051
Michal Vasko44ab1462017-05-18 13:18:36 +02004052 /* inherit config information from actual parent */
4053 for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent));
4054 clear_config = (parent) ? 1 : 0;
4055 LY_TREE_FOR(augment->child, child) {
4056 if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) {
4057 return -1;
4058 }
4059 }
4060
4061 /* inherit extensions if any */
4062 for (u = 0; u < augment->target->ext_size; u++) {
4063 ext = augment->target->ext[u]; /* shortcut */
4064 if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) {
Michal Vaskod02e30e2018-01-22 13:35:48 +01004065 v = malloc(sizeof *v);
Michal Vasko53b7da02018-02-13 15:28:42 +01004066 LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1);
Michal Vaskod02e30e2018-01-22 13:35:48 +01004067 *v = u;
4068 if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext,
4069 UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004070 /* something really bad happend since the extension finalization is not actually
4071 * being resolved while adding into unres, so something more serious with the unres
4072 * list itself must happened */
4073 return -1;
4074 }
4075 }
4076 }
4077
Radek Krejcic9d78692017-08-24 17:17:18 +02004078success:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004079 /* remove the flag about not applicability */
4080 augment->flags &= ~LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004081 return EXIT_SUCCESS;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004082}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004083
Radek Krejcib2541a32016-12-12 16:45:57 +01004084static void
4085remove_aug(struct lys_node_augment *augment)
4086{
4087 struct lys_node *last, *elem;
4088
Michal Vaskof1aa47d2017-09-21 12:09:29 +02004089 if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004090 /* skip already not applied augment */
Radek Krejcib2541a32016-12-12 16:45:57 +01004091 return;
4092 }
4093
4094 elem = augment->child;
4095 if (elem) {
4096 LY_TREE_FOR(elem, last) {
4097 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
4098 break;
4099 }
4100 }
4101 /* elem is first augment child, last is the last child */
4102
4103 /* parent child ptr */
4104 if (augment->target->child == elem) {
4105 augment->target->child = last->next;
4106 }
4107
4108 /* parent child next ptr */
4109 if (elem->prev->next) {
4110 elem->prev->next = last->next;
4111 }
4112
4113 /* parent child prev ptr */
4114 if (last->next) {
4115 last->next->prev = elem->prev;
4116 } else if (augment->target->child) {
4117 augment->target->child->prev = elem->prev;
4118 }
4119
4120 /* update augment children themselves */
4121 elem->prev = last;
4122 last->next = NULL;
4123 }
4124
Radek Krejci0ec51da2016-12-14 16:42:03 +01004125 /* augment->target still keeps the resolved target, but for lys_augment_free()
4126 * we have to keep information that this augment is not applied to free its data */
4127 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01004128}
4129
Radek Krejci30bfcd22017-01-27 16:54:48 +01004130/*
4131 * @param[in] module - the module where the deviation is defined
4132 */
4133static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004134lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004135{
Radek Krejcic9d78692017-08-24 17:17:18 +02004136 int ret, reapply = 0;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004137 char *parent_path;
4138 struct lys_node *target = NULL, *parent;
Michal Vasko50576712017-07-28 12:28:33 +02004139 struct ly_set *set;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004140
4141 if (!dev->deviate) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004142 return;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004143 }
4144
4145 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
4146 if (dev->orig_node) {
4147 /* removing not-supported deviation ... */
4148 if (strrchr(dev->target_name, '/') != dev->target_name) {
4149 /* ... from a parent */
4150
4151 /* reconnect to its previous position */
4152 parent = dev->orig_node->parent;
Michal Vaskoa1074a52018-01-03 12:18:53 +01004153 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejcic9d78692017-08-24 17:17:18 +02004154 dev->orig_node->parent = NULL;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004155 /* the original node was actually from augment, we have to get know if the augment is
4156 * applied (its module is enabled and implemented). If yes, the node will be connected
4157 * to the augment and the linkage with the target will be fixed if needed, otherwise
4158 * it will be connected only to the augment */
Radek Krejcic9d78692017-08-24 17:17:18 +02004159 if (!(parent->flags & LYS_NOTAPPLIED)) {
4160 /* start with removing augment if applied before adding nodes, we have to make sure
4161 * that everything will be connect correctly */
4162 remove_aug((struct lys_node_augment *)parent);
4163 reapply = 1;
4164 }
4165 /* connect the deviated node back into the augment */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004166 lys_node_addchild(parent, NULL, dev->orig_node);
Radek Krejcic9d78692017-08-24 17:17:18 +02004167 if (reapply) {
Radek Krejci30bfcd22017-01-27 16:54:48 +01004168 /* 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 +01004169 parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */
4170 apply_aug((struct lys_node_augment *)parent, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004171 }
Michal Vaskoa1074a52018-01-03 12:18:53 +01004172 } else if (parent && (parent->nodetype == LYS_USES)) {
4173 /* uses child */
4174 lys_node_addchild(parent, NULL, dev->orig_node);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004175 } else {
4176 /* non-augment, non-toplevel */
4177 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
Michal Vasko50576712017-07-28 12:28:33 +02004178 ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004179 free(parent_path);
Michal Vasko50576712017-07-28 12:28:33 +02004180 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004181 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004182 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004183 return;
4184 }
Michal Vasko50576712017-07-28 12:28:33 +02004185 target = set->set.s[0];
4186 ly_set_free(set);
4187
Radek Krejci30bfcd22017-01-27 16:54:48 +01004188 lys_node_addchild(target, NULL, dev->orig_node);
4189 }
4190 } else {
4191 /* ... from top-level data */
4192 lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node);
4193 }
4194
4195 dev->orig_node = NULL;
4196 } else {
4197 /* adding not-supported deviation */
Michal Vasko50576712017-07-28 12:28:33 +02004198 ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
4199 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004200 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004201 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004202 return;
4203 }
Michal Vasko50576712017-07-28 12:28:33 +02004204 target = set->set.s[0];
4205 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004206
4207 /* unlink and store the original node */
4208 parent = target->parent;
4209 lys_node_unlink(target);
Michal Vaskoa1074a52018-01-03 12:18:53 +01004210 if (parent && (parent->nodetype & (LYS_AUGMENT | LYS_USES))) {
Radek Krejci30bfcd22017-01-27 16:54:48 +01004211 /* hack for augment, because when the original will be sometime reconnected back, we actually need
4212 * to reconnect it to both - the augment and its target (which is deduced from the deviations target
4213 * path), so we need to remember the augment as an addition */
Michal Vaskoa1074a52018-01-03 12:18:53 +01004214 /* 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 +01004215 target->parent = parent;
4216 }
4217 dev->orig_node = target;
4218 }
4219 } else {
Michal Vasko50576712017-07-28 12:28:33 +02004220 ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
4221 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004222 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004223 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004224 return;
4225 }
Michal Vasko50576712017-07-28 12:28:33 +02004226 target = set->set.s[0];
4227 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004228
4229 lys_node_switch(target, dev->orig_node);
4230 dev->orig_node = target;
4231 }
4232}
4233
4234/* temporarily removes or applies deviations, updates module deviation flag accordingly */
4235void
4236lys_switch_deviations(struct lys_module *module)
4237{
4238 uint32_t i = 0, j;
4239 const struct lys_module *mod;
4240 const char *ptr;
Michal Vasko44ab1462017-05-18 13:18:36 +02004241 struct unres_schema *unres;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004242
4243 if (module->deviated) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004244 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004245 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Michal Vasko44ab1462017-05-18 13:18:36 +02004246
Radek Krejci30bfcd22017-01-27 16:54:48 +01004247 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
4248 if (mod == module) {
4249 continue;
4250 }
4251
4252 for (j = 0; j < mod->deviation_size; ++j) {
4253 ptr = strstr(mod->deviation[j].target_name, module->name);
4254 if (ptr && ptr[strlen(module->name)] == ':') {
Michal Vasko44ab1462017-05-18 13:18:36 +02004255 lys_switch_deviation(&mod->deviation[j], mod, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004256 }
4257 }
4258 }
4259
4260 if (module->deviated == 2) {
4261 module->deviated = 1;
4262 } else {
4263 module->deviated = 2;
4264 }
Radek Krejci29eac3d2017-06-01 16:50:02 +02004265 for (j = 0; j < module->inc_size; j++) {
4266 if (module->inc[j].submodule->deviated) {
4267 module->inc[j].submodule->deviated = module->deviated;
4268 }
4269 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004270
4271 if (unres->count) {
4272 resolve_unres_schema(module, unres);
4273 }
4274 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004275 }
4276}
4277
4278static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004279apply_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004280{
Michal Vasko44ab1462017-05-18 13:18:36 +02004281 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004282
4283 assert(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004284 lys_node_module(dev->orig_node)->deviated = 1; /* main module */
4285 dev->orig_node->module->deviated = 1; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004286}
4287
4288static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004289remove_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004290{
4291 uint32_t idx = 0, j;
4292 const struct lys_module *mod;
Radek Krejci29eac3d2017-06-01 16:50:02 +02004293 struct lys_module *target_mod, *target_submod;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004294 const char *ptr;
4295
4296 if (dev->orig_node) {
4297 target_mod = lys_node_module(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004298 target_submod = dev->orig_node->module;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004299 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004300 LOGINT(module->ctx);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004301 return;
4302 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004303 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004304
4305 /* clear the deviation flag if possible */
4306 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
4307 if ((mod == module) || (mod == target_mod)) {
4308 continue;
4309 }
4310
4311 for (j = 0; j < mod->deviation_size; ++j) {
4312 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
4313 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
4314 /* some other module deviation targets the inspected module, flag remains */
4315 break;
4316 }
4317 }
4318
4319 if (j < mod->deviation_size) {
4320 break;
4321 }
4322 }
4323
4324 if (!mod) {
Radek Krejci29eac3d2017-06-01 16:50:02 +02004325 target_mod->deviated = 0; /* main module */
4326 target_submod->deviated = 0; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004327 }
4328}
4329
4330void
4331lys_sub_module_apply_devs_augs(struct lys_module *module)
4332{
4333 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004334 struct unres_schema *unres;
4335
4336 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004337 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Radek Krejci30bfcd22017-01-27 16:54:48 +01004338
4339 /* remove applied deviations */
4340 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004341 apply_dev(&module->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004342 }
4343 /* remove applied augments */
4344 for (u = 0; u < module->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004345 apply_aug(&module->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004346 }
4347
4348 /* remove deviation and augments defined in submodules */
4349 for (v = 0; v < module->inc_size; ++v) {
4350 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004351 apply_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004352 }
4353
4354 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004355 apply_aug(&module->inc[v].submodule->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004356 }
4357 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004358
4359 if (unres->count) {
4360 resolve_unres_schema(module, unres);
4361 }
4362 /* nothing else left to do even if something is not resolved */
4363 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004364}
4365
Radek Krejcib2541a32016-12-12 16:45:57 +01004366void
4367lys_sub_module_remove_devs_augs(struct lys_module *module)
4368{
4369 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004370 struct unres_schema *unres;
4371
4372 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004373 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Radek Krejcib2541a32016-12-12 16:45:57 +01004374
4375 /* remove applied deviations */
4376 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004377 remove_dev(&module->deviation[u], module, unres);
Radek Krejcib2541a32016-12-12 16:45:57 +01004378 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004379 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01004380 for (u = 0; u < module->augment_size; ++u) {
4381 remove_aug(&module->augment[u]);
4382 }
4383
4384 /* remove deviation and augments defined in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004385 for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) {
Radek Krejcib2541a32016-12-12 16:45:57 +01004386 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004387 remove_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejcidbc15262016-06-16 14:58:29 +02004388 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004389
Radek Krejcib2541a32016-12-12 16:45:57 +01004390 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4391 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004392 }
4393 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004394
4395 if (unres->count) {
4396 resolve_unres_schema(module, unres);
4397 }
4398 /* nothing else left to do even if something is not resolved */
4399 unres_schema_free(module, &unres, 1);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004400}
4401
Radek Krejci27fe55e2016-09-13 17:13:35 +02004402static int
4403lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
4404{
4405 struct lys_node *root, *next, *node;
Radek Krejci9e6af732017-04-27 14:40:25 +02004406 uint16_t i, j;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004407
4408 for (i = 0; i < module->augment_size; i++) {
4409 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004410 assert(module->augment[i].target);
4411 if (apply_aug(&module->augment[i], unres)) {
4412 return EXIT_FAILURE;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004413 }
4414 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004415
4416 /* identities */
4417 for (i = 0; i < module->ident_size; i++) {
4418 for (j = 0; j < module->ident[i].base_size; j++) {
4419 resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]);
4420 }
4421 }
4422
Radek Krejci27fe55e2016-09-13 17:13:35 +02004423 LY_TREE_FOR(module->data, root) {
4424 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
4425 LY_TREE_DFS_BEGIN(root, next, node) {
4426 if (node->nodetype == LYS_GROUPING) {
4427 goto nextsibling;
4428 }
4429 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4430 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
4431 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
4432 UNRES_TYPE_LEAFREF, node) == -1) {
4433 return EXIT_FAILURE;
4434 }
4435 }
4436 }
4437
4438 /* modified LY_TREE_DFS_END */
4439 next = node->child;
4440 /* child exception for leafs, leaflists and anyxml without children */
4441 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4442 next = NULL;
4443 }
4444 if (!next) {
4445nextsibling:
4446 /* no children */
4447 if (node == root) {
4448 /* we are done, root has no children */
4449 break;
4450 }
4451 /* try siblings */
4452 next = node->next;
4453 }
4454 while (!next) {
4455 /* parent is already processed, go to its sibling */
4456 node = lys_parent(node);
4457 /* no siblings, go back through parents */
4458 if (lys_parent(node) == lys_parent(root)) {
4459 /* we are done, no next element to process */
4460 break;
4461 }
4462 next = node->next;
4463 }
4464 }
4465 }
4466
4467 return EXIT_SUCCESS;
4468}
4469
4470API int
4471lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02004472{
4473 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004474 struct unres_schema *unres;
Radek Krejci9e6af732017-04-27 14:40:25 +02004475 int i, j, k, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02004476
Radek Krejci27fe55e2016-09-13 17:13:35 +02004477 if (!module) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004478 LOGARG;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004479 return EXIT_FAILURE;
4480 }
4481
4482 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004483
4484 if (module->disabled) {
4485 disabled = 1;
4486 lys_set_enabled(module);
4487 }
4488
Michal Vasko26055752016-05-03 11:36:31 +02004489 if (module->implemented) {
4490 return EXIT_SUCCESS;
4491 }
4492
4493 ctx = module->ctx;
4494
4495 for (i = 0; i < ctx->models.used; ++i) {
4496 if (module == ctx->models.list[i]) {
4497 continue;
4498 }
4499
4500 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004501 LOGERR(ctx, LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004502 if (disabled) {
4503 /* set it back disabled */
4504 lys_set_disabled(module);
4505 }
Michal Vasko26055752016-05-03 11:36:31 +02004506 return EXIT_FAILURE;
4507 }
4508 }
4509
Radek Krejci27fe55e2016-09-13 17:13:35 +02004510 unres = calloc(1, sizeof *unres);
4511 if (!unres) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004512 LOGMEM(ctx);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004513 if (disabled) {
4514 /* set it back disabled */
4515 lys_set_disabled(module);
4516 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004517 return EXIT_FAILURE;
4518 }
4519 /* recursively make the module implemented */
4520 ((struct lys_module *)module)->implemented = 1;
4521 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
4522 goto error;
4523 }
4524 /* process augments in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004525 for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004526 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
4527 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004528 assert(module->inc[i].submodule->augment[j].target);
4529 if (apply_aug(&module->inc[i].submodule->augment[j], unres)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004530 goto error;
4531 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004532 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004533
4534 /* identities */
4535 for (j = 0; j < module->inc[i].submodule->ident_size; j++) {
4536 for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) {
4537 resolve_identity_backlink_update(&module->inc[i].submodule->ident[j],
4538 module->inc[i].submodule->ident[j].base[k]);
4539 }
4540 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004541 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004542 /* try again resolve augments in other modules possibly augmenting this one,
4543 * since we have just enabled it
4544 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004545 /* resolve rest of unres items */
4546 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
4547 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004548 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004549 unres_schema_free(NULL, &unres, 0);
Michal Vasko26055752016-05-03 11:36:31 +02004550
Radek Krejci29eac3d2017-06-01 16:50:02 +02004551 /* reflect implemented flag in submodules */
4552 for (i = 0; i < module->inc_size; i++) {
4553 module->inc[i].submodule->implemented = 1;
4554 }
4555
Michal Vaskobe136f62017-09-21 12:08:39 +02004556 LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""),
4557 (module->rev_size ? module->rev[0].date : ""));
Michal Vasko26055752016-05-03 11:36:31 +02004558 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004559
4560error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004561 if (disabled) {
4562 /* set it back disabled */
4563 lys_set_disabled(module);
4564 }
4565
Radek Krejci27fe55e2016-09-13 17:13:35 +02004566 ((struct lys_module *)module)->implemented = 0;
Michal Vasko44ab1462017-05-18 13:18:36 +02004567 unres_schema_free((struct lys_module *)module, &unres, 1);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004568 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004569}
4570
4571void
4572lys_submodule_module_data_free(struct lys_submodule *submodule)
4573{
4574 struct lys_node *next, *elem;
4575
4576 /* remove parsed data */
4577 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
4578 if (elem->module == (struct lys_module *)submodule) {
4579 lys_node_free(elem, NULL, 0);
4580 }
4581 }
4582}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02004583
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004584API char *
Michal Vasko395b0a02018-01-22 09:36:20 +01004585lys_path(const struct lys_node *node, int options)
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004586{
Michal Vasko53b7da02018-02-13 15:28:42 +01004587 char *buf = NULL;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004588
4589 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004590 LOGARG;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004591 return NULL;
4592 }
4593
Michal Vasko53b7da02018-02-13 15:28:42 +01004594 if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options)) {
Michal Vasko59631dd2017-10-02 11:56:11 +02004595 return NULL;
4596 }
4597
Michal Vasko53b7da02018-02-13 15:28:42 +01004598 return buf;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004599}
Radek Krejci9ad23f42016-10-31 15:46:52 +01004600
Michal Vasko50576712017-07-28 12:28:33 +02004601API char *
4602lys_data_path(const struct lys_node *node)
4603{
Michal Vasko53b7da02018-02-13 15:28:42 +01004604 char *result = NULL, buf[1024];
PavolVicanb28bbff2018-02-21 00:44:02 +01004605 const char *separator, *name;
Michal Vasko50576712017-07-28 12:28:33 +02004606 int i, used;
4607 struct ly_set *set;
4608 const struct lys_module *prev_mod;
4609
4610 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004611 LOGARG;
Michal Vasko50576712017-07-28 12:28:33 +02004612 return NULL;
4613 }
4614
Michal Vasko50576712017-07-28 12:28:33 +02004615 set = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01004616 LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup);
Michal Vasko50576712017-07-28 12:28:33 +02004617
4618 while (node) {
4619 ly_set_add(set, (void *)node, 0);
4620 do {
4621 node = lys_parent(node);
4622 } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
4623 }
4624
4625 prev_mod = NULL;
4626 used = 0;
4627 for (i = set->number - 1; i > -1; --i) {
4628 node = set->set.s[i];
PavolVicanb28bbff2018-02-21 00:44:02 +01004629 if (node->nodetype == LYS_EXT) {
4630 if (strcmp(((struct lys_ext_instance *)node)->def->name, "yang-data")) {
4631 continue;
4632 }
4633 name = ((struct lys_ext_instance *)node)->arg_value;
4634 separator = ":#";
4635 } else {
4636 name = node->name;
4637 separator = ":";
4638 }
Michal Vasko50576712017-07-28 12:28:33 +02004639 used += sprintf(buf + used, "/%s%s%s", (lys_node_module(node) == prev_mod ? "" : lys_node_module(node)->name),
PavolVicanb28bbff2018-02-21 00:44:02 +01004640 (lys_node_module(node) == prev_mod ? "" : separator), name);
Michal Vasko50576712017-07-28 12:28:33 +02004641 prev_mod = lys_node_module(node);
4642 }
4643
4644 result = strdup(buf);
Michal Vasko53b7da02018-02-13 15:28:42 +01004645 LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup);
Michal Vasko50576712017-07-28 12:28:33 +02004646
Michal Vasko53b7da02018-02-13 15:28:42 +01004647cleanup:
Michal Vasko50576712017-07-28 12:28:33 +02004648 ly_set_free(set);
Michal Vasko50576712017-07-28 12:28:33 +02004649 return result;
4650}
4651
Michal Vaskobb520442017-05-23 10:55:18 +02004652struct lys_node_augment *
4653lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target)
4654{
4655 int i, j, last_found;
4656
4657 if (!last) {
4658 last_found = 1;
4659 } else {
4660 last_found = 0;
4661 }
4662
4663 /* search module augments */
4664 for (i = 0; i < mod->augment_size; ++i) {
4665 if (!mod->augment[i].target) {
4666 /* still unresolved, skip */
4667 continue;
4668 }
4669
4670 if (mod->augment[i].target == aug_target) {
4671 if (last_found) {
4672 /* next match after last */
4673 return &mod->augment[i];
4674 }
4675
4676 if (&mod->augment[i] == last) {
4677 last_found = 1;
4678 }
4679 }
4680 }
4681
4682 /* search submodule augments */
4683 for (i = 0; i < mod->inc_size; ++i) {
4684 for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) {
4685 if (!mod->inc[i].submodule->augment[j].target) {
4686 continue;
4687 }
4688
4689 if (mod->inc[i].submodule->augment[j].target == aug_target) {
4690 if (last_found) {
4691 /* next match after last */
4692 return &mod->inc[i].submodule->augment[j];
4693 }
4694
4695 if (&mod->inc[i].submodule->augment[j] == last) {
4696 last_found = 1;
4697 }
4698 }
4699 }
4700 }
4701
4702 return NULL;
4703}
4704
Michal Vasko50576712017-07-28 12:28:33 +02004705API struct ly_set *
4706lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path)
4707{
4708 struct ly_set *ret;
4709 int rc;
4710
4711 if ((!cur_module && !cur_node) || !path) {
4712 return NULL;
4713 }
4714
4715 rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1);
4716 if (rc == -1) {
4717 return NULL;
4718 }
4719
4720 return ret;
4721}
4722
Radek Krejci8d6b7422017-02-03 14:42:13 +01004723static void
4724lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt)
4725{
4726 struct lyext_substmt *info;
Radek Krejcib71243e2017-02-08 16:20:08 +01004727 const char **str, ***a;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004728 int c;
4729
4730 str = lys_ext_complex_get_substmt(stmt, ext, &info);
4731 if (!str || !(*str)) {
4732 return;
4733 }
4734 if (info->cardinality >= LY_STMT_CARD_SOME) {
4735 /* we have array */
Radek Krejcib71243e2017-02-08 16:20:08 +01004736 a = (const char ***)str;
Radek Krejci56c80412017-02-09 10:44:16 +01004737 for (str = (*(const char ***)str), c = 0; str[c]; c++) {
4738 lydict_remove(ctx, str[c]);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004739 }
Radek Krejci56c80412017-02-09 10:44:16 +01004740 free(a[0]);
4741 if (stmt == LY_STMT_BELONGSTO) {
4742 for (str = a[1], c = 0; str[c]; c++) {
4743 lydict_remove(ctx, str[c]);
4744 }
4745 free(a[1]);
PavolVican99c70722017-02-18 17:25:52 +01004746 } else if (stmt == LY_STMT_ARGUMENT) {
4747 free(a[1]);
Radek Krejci56c80412017-02-09 10:44:16 +01004748 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004749 } else {
Radek Krejci56c80412017-02-09 10:44:16 +01004750 lydict_remove(ctx, str[0]);
4751 if (stmt == LY_STMT_BELONGSTO) {
4752 lydict_remove(ctx, str[1]);
4753 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004754 }
4755}
4756void
Radek Krejci5138e9f2017-04-12 13:10:46 +02004757lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size,
4758 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci8d6b7422017-02-03 14:42:13 +01004759{
Radek Krejcif8d05c22017-02-10 15:33:35 +01004760 unsigned int i, j, k;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004761 struct lyext_substmt *substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004762 void **pp, **start;
Radek Krejcif95b6292017-02-13 15:57:37 +01004763 struct lys_node *siter, *snext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004764
4765#define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \
Radek Krejcib84686f2017-02-09 16:04:55 +01004766 pp = lys_ext_complex_get_substmt(STMT, (struct lys_ext_instance_complex *)e[i], NULL); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004767 if (!pp || !(*pp)) { break; } \
Radek Krejcib84686f2017-02-09 16:04:55 +01004768 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004769 for (start = pp = *pp; *pp; pp++) { \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004770 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004771 if (FREE) { free(*pp); } \
4772 } \
4773 free(start); \
4774 } else { /* single item */ \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004775 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004776 if (FREE) { free(*pp); } \
4777 }
4778
PavolVican9d61d402018-02-05 15:52:48 +01004779 if (!size || !e) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01004780 return;
4781 }
4782
4783 for (i = 0; i < size; i++) {
4784 if (!e[i]) {
4785 continue;
4786 }
4787
4788 if (e[i]->flags & (LYEXT_OPT_INHERIT)) {
4789 /* no free, this is just a shadow copy of the original extension instance */
4790 } else {
4791 if (e[i]->flags & (LYEXT_OPT_YANG)) {
4792 free(e[i]->def); /* remove name of instance extension */
PavolVican19dc6152017-02-06 12:04:15 +01004793 e[i]->def = NULL;
PavolVicandb0e8172017-02-20 00:46:09 +01004794 yang_free_ext_data((struct yang_ext_substmt *)e[i]->parent); /* remove backup part of yang file */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004795 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02004796 /* remove private object */
4797 if (e[i]->priv && private_destructor) {
4798 private_destructor((struct lys_node*)e[i], e[i]->priv);
4799 }
4800 lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size, private_destructor);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004801 lydict_remove(ctx, e[i]->arg_value);
4802 }
4803
fanchanghu8d86f6b2017-06-10 12:49:54 +08004804 if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX
4805 && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) {
Radek Krejcifebdad72017-02-06 11:35:51 +01004806 substmt = ((struct lys_ext_instance_complex *)e[i])->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004807 for (j = 0; substmt[j].stmt; j++) {
4808 switch(substmt[j].stmt) {
4809 case LY_STMT_DESCRIPTION:
4810 case LY_STMT_REFERENCE:
4811 case LY_STMT_UNITS:
Radek Krejcib71243e2017-02-08 16:20:08 +01004812 case LY_STMT_ARGUMENT:
4813 case LY_STMT_DEFAULT:
4814 case LY_STMT_ERRTAG:
4815 case LY_STMT_ERRMSG:
4816 case LY_STMT_PREFIX:
4817 case LY_STMT_NAMESPACE:
4818 case LY_STMT_PRESENCE:
4819 case LY_STMT_REVISIONDATE:
4820 case LY_STMT_KEY:
4821 case LY_STMT_BASE:
4822 case LY_STMT_BELONGSTO:
4823 case LY_STMT_CONTACT:
4824 case LY_STMT_ORGANIZATION:
4825 case LY_STMT_PATH:
Radek Krejci8d6b7422017-02-03 14:42:13 +01004826 lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt);
4827 break;
4828 case LY_STMT_TYPE:
4829 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1);
4830 break;
Radek Krejci63fc0962017-02-15 13:20:18 +01004831 case LY_STMT_TYPEDEF:
4832 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1);
4833 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004834 case LY_STMT_IFFEATURE:
Frank Rimplerc4db1c72017-09-12 12:56:39 +00004835 EXTCOMPLEX_FREE_STRUCT(LY_STMT_IFFEATURE, struct lys_iffeature, lys_iffeature_free, 0, 1, 0);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004836 break;
Radek Krejci5496fae2017-02-10 13:26:48 +01004837 case LY_STMT_MAX:
4838 case LY_STMT_MIN:
4839 case LY_STMT_POSITION:
PavolVican2ed9f4e2017-02-16 00:08:45 +01004840 case LY_STMT_VALUE:
Radek Krejcif8d05c22017-02-10 15:33:35 +01004841 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
Radek Krejci716cd7a2017-02-15 12:23:41 +01004842 if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) {
Radek Krejcif8d05c22017-02-10 15:33:35 +01004843 for(k = 0; ((uint32_t**)(*pp))[k]; k++) {
4844 free(((uint32_t**)(*pp))[k]);
4845 }
4846 }
4847 free(*pp);
4848 break;
4849 case LY_STMT_DIGITS:
Radek Krejcib84686f2017-02-09 16:04:55 +01004850 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4851 /* free the array */
4852 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4853 free(*pp);
4854 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004855 break;
Radek Krejci37f9ba32017-02-10 16:50:35 +01004856 case LY_STMT_MODULE:
4857 /* modules are part of the context, so they will be freed there */
4858 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4859 /* free the array */
4860 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4861 free(*pp);
4862 }
4863 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01004864 case LY_STMT_ACTION:
Radek Krejcib31762b2017-02-15 10:48:42 +01004865 case LY_STMT_ANYDATA:
4866 case LY_STMT_ANYXML:
4867 case LY_STMT_CASE:
4868 case LY_STMT_CHOICE:
4869 case LY_STMT_CONTAINER:
4870 case LY_STMT_GROUPING:
4871 case LY_STMT_INPUT:
4872 case LY_STMT_LEAF:
4873 case LY_STMT_LEAFLIST:
4874 case LY_STMT_LIST:
4875 case LY_STMT_NOTIFICATION:
4876 case LY_STMT_OUTPUT:
4877 case LY_STMT_RPC:
4878 case LY_STMT_USES:
Radek Krejcif95b6292017-02-13 15:57:37 +01004879 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4880 LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) {
4881 lys_node_free(siter, NULL, 0);
4882 }
Radek Krejcib31762b2017-02-15 10:48:42 +01004883 *pp = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01004884 break;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01004885 case LY_STMT_UNIQUE:
4886 pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL);
4887 if (!pp || !(*pp)) {
4888 break;
4889 }
4890 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4891 for (start = pp = *pp; *pp; pp++) {
4892 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4893 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4894 }
4895 free((*(struct lys_unique**)pp)->expr);
4896 free(*pp);
4897 }
4898 free(start);
4899 } else { /* single item */
4900 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4901 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4902 }
4903 free((*(struct lys_unique**)pp)->expr);
4904 free(*pp);
4905 }
4906 break;
Radek Krejciaa9c5202017-02-15 16:10:14 +01004907 case LY_STMT_LENGTH:
4908 case LY_STMT_MUST:
4909 case LY_STMT_PATTERN:
4910 case LY_STMT_RANGE:
4911 EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1);
4912 break;
Radek Krejcic5cc5302017-02-16 10:07:46 +01004913 case LY_STMT_WHEN:
4914 EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0);
4915 break;
Radek Krejci7417a082017-02-16 11:07:59 +01004916 case LY_STMT_REVISION:
4917 pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL);
4918 if (!pp || !(*pp)) {
4919 break;
4920 }
4921 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4922 for (start = pp = *pp; *pp; pp++) {
4923 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4924 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4925 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004926 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004927 free(*pp);
4928 }
4929 free(start);
4930 } else { /* single item */
4931 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4932 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4933 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004934 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004935 free(*pp);
4936 }
4937 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004938 default:
Radek Krejcib84686f2017-02-09 16:04:55 +01004939 /* nothing to free */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004940 break;
4941 }
4942 }
4943 }
4944
4945 free(e[i]);
4946 }
4947 free(e);
4948
4949#undef EXTCOMPLEX_FREE_STRUCT
4950}