blob: 0cd17321db91ceab96664bf986cd34b5ae69faa1 [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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
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
Radek Krejci812b10a2015-05-28 16:48:25 +020017#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020018#include <ctype.h>
Radek Krejcib051f722016-02-25 15:12:21 +010019#include <limits.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020020#include <stdlib.h>
21#include <sys/mman.h>
Michal Vasko662610a2015-12-07 11:25:45 +010022#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <sys/stat.h>
Michal Vasko662610a2015-12-07 11:25:45 +010024#include <fcntl.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020025#include <string.h>
Michal Vasko662610a2015-12-07 11:25:45 +010026#include <unistd.h>
27#include <errno.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020028
29#include "common.h"
30#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020031#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020032#include "resolve.h"
Michal Vasko88c29542015-11-27 14:57:53 +010033#include "xml.h"
Michal Vasko5b3492c2016-07-20 09:37:40 +020034#include "xpath.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020035#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020036#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020037#include "validation.h"
Pavol Vicanf7cc2852016-03-22 23:27:35 +010038#include "parser_yang.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020039
Radek Krejci8d6b7422017-02-03 14:42:13 +010040static 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 +020041 int in_grp, int shallow, struct unres_schema *unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020042
Radek Krejci9ff0a922016-07-14 13:08:05 +020043API const struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +010044lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020045{
Radek Krejci9ff0a922016-07-14 13:08:05 +020046 int i;
Radek Krejci48061fb2015-08-05 15:41:07 +020047
Radek Krejci27fe55e2016-09-13 17:13:35 +020048 if (!node) {
49 return NULL;
50 }
51
Radek Krejci48061fb2015-08-05 15:41:07 +020052check:
53 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
54 /* input/output does not have if-feature, so skip them */
55
56 /* check local if-features */
Michal Vaskoc5c26b02016-06-29 11:10:29 +020057 for (i = 0; i < node->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +020058 if (!resolve_iffeature(&node->iffeature[i])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +020059 return node;
Radek Krejci48061fb2015-08-05 15:41:07 +020060 }
61 }
62 }
63
64 if (!recursive) {
65 return NULL;
66 }
67
68 /* go through parents */
69 if (node->nodetype == LYS_AUGMENT) {
70 /* go to parent actually means go to the target node */
71 node = ((struct lys_node_augment *)node)->target;
Michal Vasko17549192017-03-13 10:19:33 +010072 if (!node) {
73 /* unresolved augment, let's say it's enabled */
74 return NULL;
75 }
Radek Krejci48061fb2015-08-05 15:41:07 +020076 } else if (node->parent) {
77 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +020078 } else {
79 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +020080 }
81
Radek Krejci074bf852015-08-19 14:22:16 +020082 if (recursive == 2) {
83 /* continue only if the node cannot have a data instance */
84 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
85 return NULL;
86 }
87 }
88 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +020089}
90
Michal Vasko1dca6882015-10-22 14:29:42 +020091int
Michal Vasko36cbaa42015-12-14 13:15:48 +010092lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
93 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +020094{
Radek Krejcic071c542016-01-27 14:57:51 +010095 const struct lys_node *node, *parent = NULL;
96 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +010097 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +020098
Michal Vasko36cbaa42015-12-14 13:15:48 +010099 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +0200100 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +0200101
Michal Vasko36cbaa42015-12-14 13:15:48 +0100102 /* fill the lengths in case the caller is so indifferent */
103 if (!mod_name_len) {
104 mod_name_len = strlen(mod_name);
105 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200106 if (!nam_len) {
107 nam_len = strlen(name);
108 }
109
Michal Vasko9e635ac2016-10-17 11:44:09 +0200110 while (siblings && (siblings->nodetype == LYS_USES)) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200111 siblings = siblings->child;
112 }
Michal Vasko9e635ac2016-10-17 11:44:09 +0200113 if (!siblings) {
114 /* unresolved uses */
115 return EXIT_FAILURE;
116 }
117
Michal Vasko680f8b42016-10-17 10:27:37 +0200118 if (siblings->nodetype == LYS_GROUPING) {
119 for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev);
120 if (node->nodetype == LYS_GROUPING) {
121 /* we went through all the siblings, only groupings there - no valid sibling */
122 return EXIT_FAILURE;
123 }
124 /* update siblings to be valid */
125 siblings = node;
126 }
127
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200128 /* set parent correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100129 parent = lys_parent(siblings);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200130
Michal Vasko680f8b42016-10-17 10:27:37 +0200131 /* go up all uses */
132 while (parent && (parent->nodetype == LYS_USES)) {
133 parent = lys_parent(parent);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200134 }
135
Radek Krejcic071c542016-01-27 14:57:51 +0100136 if (!parent) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200137 /* handle situation when there is a top-level uses referencing a foreign grouping */
138 for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node));
139 mod = lys_node_module(node);
Michal Vasko1dca6882015-10-22 14:29:42 +0200140 }
141
Radek Krejcic071c542016-01-27 14:57:51 +0100142 /* try to find the node */
143 node = NULL;
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100144 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Radek Krejcic071c542016-01-27 14:57:51 +0100145 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100146 /* module name comparison */
147 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200148 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 +0100149 continue;
150 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200151
Radek Krejcic071c542016-01-27 14:57:51 +0100152 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200153 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100154 if (ret) {
155 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200156 }
Radek Krejcic071c542016-01-27 14:57:51 +0100157 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200158 }
159 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200160 }
161
162 return EXIT_FAILURE;
163}
164
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200165int
Michal Vaskobb520442017-05-23 10:55:18 +0200166lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len,
167 LYS_NODE type, const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200168{
Michal Vaskobb520442017-05-23 10:55:18 +0200169 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200170
Michal Vaskobb520442017-05-23 10:55:18 +0200171 assert((mod || parent) && name);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200172 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
173
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200174 if (!mod) {
Michal Vaskobb520442017-05-23 10:55:18 +0200175 mod = lys_node_module(parent);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200176 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200177
Michal Vasko4f0dad02016-02-15 14:08:23 +0100178 /* try to find the node */
179 node = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +0100180 while ((node = lys_getnext(node, parent, mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100181 if (!type || (node->nodetype & type)) {
182 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200183 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100184 continue;
185 }
186
Michal Vasko4f0dad02016-02-15 14:08:23 +0100187 /* direct name check */
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100188 if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100189 if (ret) {
190 *ret = node;
191 }
192 return EXIT_SUCCESS;
193 }
Radek Krejcic071c542016-01-27 14:57:51 +0100194 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200195 }
196
197 return EXIT_FAILURE;
198}
199
Michal Vasko1e62a092015-12-01 12:27:20 +0100200API const struct lys_node *
201lys_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 +0200202{
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100203 const struct lys_node *next, *aug_parent;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100204 struct lys_node **snode;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200205
Michal Vasko24476fa2017-03-08 12:33:48 +0100206 if ((!parent && !module) || (parent && (parent->nodetype == LYS_USES) && !(options & LYS_GETNEXT_PARENTUSES))) {
207 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
208 return NULL;
209 }
210
Radek Krejci8bc87f62015-09-02 16:19:05 +0200211 if (!last) {
212 /* first call */
213
214 /* get know where to start */
215 if (parent) {
216 /* schema subtree */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100217 snode = lys_child(parent, LYS_UNKNOWN);
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100218 /* do not return anything if the augment does not have any children */
219 if (!snode || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100220 return NULL;
221 }
222 next = last = *snode;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200223 } else {
224 /* top level data */
225 assert(module);
226 next = last = module->data;
227 }
Radek Krejci972724f2016-08-12 15:24:40 +0200228 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
229 /* continue with uses content */
230 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200231 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200232 /* continue after the last returned value */
233 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200234 }
235
236repeat:
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100237 if (parent && (parent->nodetype == LYS_AUGMENT) && next) {
238 /* do not return anything outside the parent augment */
239 aug_parent = next->parent;
240 do {
241 while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) {
242 aug_parent = aug_parent->parent;
243 }
244 if (aug_parent) {
245 if (aug_parent == parent) {
246 break;
247 }
248 aug_parent = ((struct lys_node_augment *)aug_parent)->target;
249 }
250
251 } while (aug_parent);
252 if (!aug_parent) {
253 return NULL;
254 }
255 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200256 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200257 if (options & LYS_GETNEXT_WITHGROUPING) {
258 return next;
259 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200260 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200261 }
262
Radek Krejci972724f2016-08-12 15:24:40 +0200263 if (!next) { /* cover case when parent is augment */
264 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200265 /* no next element */
266 return NULL;
267 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200268 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200269 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200270 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200271 } else {
272 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200273 }
274
275 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200276 case LYS_INPUT:
277 case LYS_OUTPUT:
278 if (options & LYS_GETNEXT_WITHINOUT) {
279 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200280 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200281 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200282 } else {
283 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200284 }
Radek Krejci972724f2016-08-12 15:24:40 +0200285 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200286
Michal Vaskoa5835e92015-10-20 15:07:39 +0200287 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200288 if (options & LYS_GETNEXT_WITHCASE) {
289 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200290 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200291 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200292 } else {
293 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200294 }
Radek Krejci972724f2016-08-12 15:24:40 +0200295 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200296
Michal Vasko1dca6882015-10-22 14:29:42 +0200297 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200298 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200299 if (options & LYS_GETNEXT_WITHUSES) {
300 return next;
301 } else if (next->child) {
302 next = next->child;
303 } else {
304 next = next->next;
305 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200306 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200307
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200308 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200309 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200310 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200311 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200312 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200313 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200314 case LYS_LIST:
315 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200316 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200317
Radek Krejci972724f2016-08-12 15:24:40 +0200318 case LYS_CONTAINER:
319 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
320 if (next->child) {
321 /* go into */
322 next = next->child;
323 } else {
324 next = next->next;
325 }
326 goto repeat;
327 } else {
328 return next;
329 }
330
Radek Krejci8bc87f62015-09-02 16:19:05 +0200331 case LYS_CHOICE:
332 if (options & LYS_GETNEXT_WITHCHOICE) {
333 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200334 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200335 /* go into */
336 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200337 } else {
338 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200339 }
Radek Krejci972724f2016-08-12 15:24:40 +0200340 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200341
Radek Krejci7f40ce32015-08-12 20:38:46 +0200342 default:
343 /* we should not be here */
344 return NULL;
345 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200346}
347
Radek Krejcibf285832017-01-26 16:05:41 +0100348void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200349lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200350{
Radek Krejcif95b6292017-02-13 15:57:37 +0100351 struct lys_node *parent, *first, **pp;
Radek Krejcic071c542016-01-27 14:57:51 +0100352 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200353
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200354 if (!node) {
355 return;
356 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200357
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200358 /* unlink from data model if necessary */
359 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100360 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100361 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100362 if (main_module->data == node) {
363 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200364 }
365 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200366
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200367 /* store pointers to important nodes */
368 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200369 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200370 /* handle augments - first, unlink it from the augment parent ... */
371 if (parent->child == node) {
372 parent->child = node->next;
373 }
Radek Krejcifec2e142017-01-05 15:19:03 +0100374
375 if (parent->flags & LYS_NOTAPPLIED) {
376 /* data are not connected in the target, so we cannot continue with the target as a parent */
377 parent = NULL;
378 } else {
379 /* data are connected in target, so we will continue with the target as a parent */
380 parent = ((struct lys_node_augment *)parent)->target;
381 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200382 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200383
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200384 /* unlink from parent */
385 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100386 if (parent->nodetype == LYS_EXT) {
387 pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype),
388 (struct lys_ext_instance_complex*)parent, NULL);
389 if (*pp == node) {
390 *pp = node->next;
391 }
392 } else if (parent->child == node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200393 parent->child = node->next;
394 }
395 node->parent = NULL;
396 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200397
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200398 /* unlink from siblings */
399 if (node->prev == node) {
400 /* there are no more siblings */
401 return;
402 }
403 if (node->next) {
404 node->next->prev = node->prev;
405 } else {
406 /* unlinking the last element */
407 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100408 if (parent->nodetype == LYS_EXT) {
409 first = *(struct lys_node **)pp;
410 } else {
411 first = parent->child;
412 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200413 } else {
414 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200415 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200416 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200417 }
418 }
419 first->prev = node->prev;
420 }
421 if (node->prev->next) {
422 node->prev->next = node->next;
423 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200424
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200425 /* clean up the unlinked element */
426 node->next = NULL;
427 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200428}
429
Michal Vasko563ef092015-09-04 13:17:23 +0200430struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100431lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200432{
433 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200434
435 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200436 /* top-level augment, look into module (uses augment is handled correctly below) */
437 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
Radek Krejci115fa882017-03-01 16:15:07 +0100438 par_iter = lys_main_module(par_iter->parent->module)->data;
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200439 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200440 break;
441 }
442 }
443
Michal Vasko6f929da2015-10-02 16:23:25 +0200444 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200445 continue;
446 }
447
448 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
449 if (!stop) {
450 stop = par_iter;
451 } else if (iter == stop) {
452 break;
453 }
454 if (iter->nodetype != LYS_GROUPING) {
455 continue;
456 }
457
Radek Krejcif8426a72015-10-31 23:14:03 +0100458 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200459 return (struct lys_node_grp *)iter;
460 }
461 }
462 }
463
Michal Vasko563ef092015-09-04 13:17:23 +0200464 return NULL;
465}
466
Radek Krejci10c760e2015-08-14 14:45:43 +0200467/*
468 * get next grouping in the root's subtree, in the
469 * first call, tha last is NULL
470 */
471static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200472lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200473{
Radek Krejci10c760e2015-08-14 14:45:43 +0200474 struct lys_node *last = (struct lys_node *)lastgrp;
475 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200476
Radek Krejci10c760e2015-08-14 14:45:43 +0200477 assert(root);
478
479 if (!last) {
480 last = root;
481 }
482
483 while (1) {
484 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
485 next = last->child;
486 } else {
487 next = NULL;
488 }
489 if (!next) {
490 if (last == root) {
491 /* we are done */
492 return NULL;
493 }
494
495 /* no children, go to siblings */
496 next = last->next;
497 }
498 while (!next) {
499 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100500 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200501 /* we are done */
502 return NULL;
503 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200504 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100505 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200506 }
507
508 if (next->nodetype == LYS_GROUPING) {
509 return (struct lys_node_grp *)next;
510 }
511
512 last = next;
513 }
514}
515
Michal Vasko0d343d12015-08-24 14:57:36 +0200516/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200517int
Radek Krejci07911992015-08-14 15:13:31 +0200518lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
519{
Michal Vasko563ef092015-09-04 13:17:23 +0200520 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200521 struct lys_node_grp *grp;
Radek Krejcif95b6292017-02-13 15:57:37 +0100522 int down, up;
Radek Krejci07911992015-08-14 15:13:31 +0200523
524 assert(node);
525
526 if (!parent) {
527 assert(module);
528 } else {
529 module = parent->module;
530 }
Radek Krejci115fa882017-03-01 16:15:07 +0100531 module = lys_main_module(module);
Radek Krejci07911992015-08-14 15:13:31 +0200532
533 switch (node->nodetype) {
534 case LYS_GROUPING:
535 /* 6.2.1, rule 6 */
536 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100537 start = *lys_child(parent, LYS_GROUPING);
538 if (!start) {
Radek Krejci07911992015-08-14 15:13:31 +0200539 down = 0;
540 start = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100541 } else {
542 down = 1;
543 }
544 if (parent->nodetype == LYS_EXT) {
545 up = 0;
546 } else {
547 up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200548 }
549 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100550 down = up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200551 start = module->data;
552 }
553 /* go up */
Radek Krejcif95b6292017-02-13 15:57:37 +0100554 if (up && lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100555 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200556 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200557 }
558 /* go down, because grouping can be defined after e.g. container in which is collision */
559 if (down) {
560 for (iter = start, stop = NULL; iter; iter = iter->prev) {
561 if (!stop) {
562 stop = start;
563 } else if (iter == stop) {
564 break;
565 }
566 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
567 continue;
568 }
569
570 grp = NULL;
571 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100572 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100573 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200574 return EXIT_FAILURE;
575 }
576 }
577 }
578 }
579 break;
580 case LYS_LEAF:
581 case LYS_LEAFLIST:
582 case LYS_LIST:
583 case LYS_CONTAINER:
584 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200585 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200586 /* 6.2.1, rule 7 */
587 if (parent) {
588 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200589 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
590 if (iter->nodetype == LYS_AUGMENT) {
591 if (((struct lys_node_augment *)iter)->target) {
592 /* augment is resolved, go up */
593 iter = ((struct lys_node_augment *)iter)->target;
594 continue;
595 }
596 /* augment is not resolved, this is the final parent */
597 break;
598 }
Radek Krejci07911992015-08-14 15:13:31 +0200599 iter = iter->parent;
600 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200601
Radek Krejci07911992015-08-14 15:13:31 +0200602 if (!iter) {
603 stop = NULL;
604 iter = module->data;
Radek Krejcif95b6292017-02-13 15:57:37 +0100605 } else if (iter->nodetype == LYS_EXT) {
606 stop = iter;
607 iter = *lys_child(iter, node->nodetype);
Radek Krejci07911992015-08-14 15:13:31 +0200608 } else {
609 stop = iter;
610 iter = iter->child;
611 }
612 } else {
613 stop = NULL;
614 iter = module->data;
615 }
616 while (iter) {
617 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
618 iter = iter->child;
619 continue;
620 }
621
Radek Krejcibf2abff2016-08-23 15:51:52 +0200622 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100623 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100624 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200625 return EXIT_FAILURE;
626 }
627 }
628
629 /* special case for choice - we must check the choice's name as
630 * well as the names of nodes under the choice
631 */
632 if (iter->nodetype == LYS_CHOICE) {
633 iter = iter->child;
634 continue;
635 }
636
637 /* go to siblings */
638 if (!iter->next) {
639 /* no sibling, go to parent's sibling */
640 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200641 /* for parent LYS_AUGMENT */
642 if (iter->parent == stop) {
643 iter = stop;
644 break;
645 }
646 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200647 if (iter && iter->next) {
648 break;
649 }
650 } while (iter != stop);
651
652 if (iter == stop) {
653 break;
654 }
655 }
656 iter = iter->next;
657 }
658 break;
659 case LYS_CASE:
660 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100661 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100662 start = *lys_child(parent, LYS_CASE);
Radek Krejcic071c542016-01-27 14:57:51 +0100663 } else {
664 start = module->data;
665 }
666
667 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200668 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200669 continue;
670 }
671
Radek Krejci749190d2016-02-18 16:26:25 +0100672 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100673 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200674 return EXIT_FAILURE;
675 }
676 }
677 break;
678 default:
679 /* no check needed */
680 break;
681 }
682
683 return EXIT_SUCCESS;
684}
685
Michal Vasko0d343d12015-08-24 14:57:36 +0200686/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200687int
Radek Krejci10c760e2015-08-14 14:45:43 +0200688lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
689{
Radek Krejcif95b6292017-02-13 15:57:37 +0100690 struct lys_node *iter, *next, **pchild;
Radek Krejci41a349b2016-10-24 19:21:59 +0200691 struct lys_node_inout *in, *out, *inout;
Radek Krejci744c2d42017-03-26 13:30:00 -0500692 struct lys_node_case *c;
693 int type, shortcase = 0;
Radek Krejcif95b6292017-02-13 15:57:37 +0100694 void *p;
695 struct lyext_substmt *info = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200696
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200697 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200698
Radek Krejci10c760e2015-08-14 14:45:43 +0200699 if (parent) {
700 type = parent->nodetype;
701 module = parent->module;
702 } else {
703 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100704 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200705 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200706 }
707
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200708 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200709 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200710 case LYS_CONTAINER:
711 case LYS_LIST:
712 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100713 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200714 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200715 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200716 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200717 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
718 return EXIT_FAILURE;
719 }
720 break;
Radek Krejci76512572015-08-04 09:47:08 +0200721 case LYS_INPUT:
722 case LYS_OUTPUT:
723 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200724 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200725 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200726 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100727 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200728 return EXIT_FAILURE;
729 }
730 break;
Radek Krejci76512572015-08-04 09:47:08 +0200731 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200732 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200733 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100734 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200735 return EXIT_FAILURE;
736 }
Radek Krejci744c2d42017-03-26 13:30:00 -0500737 if (child->nodetype != LYS_CASE) {
738 shortcase = 1;
739 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200740 break;
Radek Krejci76512572015-08-04 09:47:08 +0200741 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200742 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200743 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100744 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200745 return EXIT_FAILURE;
746 }
747 break;
Radek Krejci76512572015-08-04 09:47:08 +0200748 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200749 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200750 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100751 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200752 return EXIT_FAILURE;
753 }
754 break;
Radek Krejci76512572015-08-04 09:47:08 +0200755 case LYS_LEAF:
756 case LYS_LEAFLIST:
757 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200758 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100759 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vasko51e5c582017-01-19 14:16:39 +0100760 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100761 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200762 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200763 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200764 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200765 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskodb017262017-01-24 13:10:04 +0100766 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100767 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200768 return EXIT_FAILURE;
769 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200770 break;
771 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200772 /* top level */
773 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200774 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200775 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100776 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200777 return EXIT_FAILURE;
778 }
Radek Krejcif95b6292017-02-13 15:57:37 +0100779 break;
780 case LYS_EXT:
781 /* plugin-defined */
782 p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info);
783 if (!p) {
784 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype),
785 ((struct lys_ext_instance_complex*)parent)->def->name);
786 return EXIT_FAILURE;
787 }
788 /* TODO check cardinality */
Radek Krejcic071c542016-01-27 14:57:51 +0100789 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200790 }
791
792 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200793 if (lys_check_id(child, parent, module)) {
794 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200795 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200796
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200797 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200798 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200799 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200800
Radek Krejcif95b6292017-02-13 15:57:37 +0100801 if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200802 /* replace the implicit input/output node */
803 if (child->nodetype == LYS_OUTPUT) {
804 inout = (struct lys_node_inout *)parent->child->next;
805 } else { /* LYS_INPUT */
806 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200807 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200808 }
809 if (inout->next) {
810 child->next = inout->next;
811 inout->next->prev = child;
812 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200813 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200814 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200815 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200816 child->prev = inout->prev;
817 if (inout->prev->next) {
818 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200819 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200820 inout->prev = (struct lys_node *)inout;
821 child->parent = parent;
822 inout->parent = NULL;
823 lys_node_free((struct lys_node *)inout, NULL, 0);
824 } else {
Radek Krejci744c2d42017-03-26 13:30:00 -0500825 if (shortcase) {
826 /* create the implicit case to allow it to serve as a target of the augments,
827 * it won't be printed, but it will be present in the tree */
828 c = calloc(1, sizeof *c);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200829 LY_CHECK_ERR_RETURN(!c, LOGMEM, EXIT_FAILURE);
Radek Krejci744c2d42017-03-26 13:30:00 -0500830 c->name = lydict_insert(module->ctx, child->name, 0);
831 c->flags = LYS_IMPLICIT;
832 c->module = module;
833 c->nodetype = LYS_CASE;
834 c->prev = (struct lys_node*)c;
835 lys_node_addchild(parent, module, (struct lys_node*)c);
836 parent = (struct lys_node*)c;
837 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200838 /* connect the child correctly */
839 if (!parent) {
840 if (module->data) {
841 module->data->prev->next = child;
842 child->prev = module->data->prev;
843 module->data->prev = child;
844 } else {
845 module->data = child;
846 }
847 } else {
Radek Krejci30bfcd22017-01-27 16:54:48 +0100848 next = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +0100849 pchild = lys_child(parent, child->nodetype);
850 assert(pchild);
851
852 if (!(*pchild)) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200853 /* the only/first child of the parent */
Radek Krejcif95b6292017-02-13 15:57:37 +0100854 *pchild = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200855 child->parent = parent;
856 iter = child;
Radek Krejci30bfcd22017-01-27 16:54:48 +0100857 } else if (type == LYS_AUGMENT) {
858 /* add a new child as a last child of the augment (no matter if applied or not) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100859 for (iter = (*pchild)->prev; iter->parent != parent; iter = iter->prev);
Radek Krejci30bfcd22017-01-27 16:54:48 +0100860 next = iter->next;
861 iter->next = child;
862 child->prev = iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200863 } else {
864 /* add a new child at the end of parent's child list */
Radek Krejcif95b6292017-02-13 15:57:37 +0100865 iter = (*pchild)->prev;
Radek Krejci41a349b2016-10-24 19:21:59 +0200866 iter->next = child;
867 child->prev = iter;
868 }
869 while (iter->next) {
870 iter = iter->next;
871 iter->parent = parent;
872 }
Radek Krejci30bfcd22017-01-27 16:54:48 +0100873 if (next) {
874 /* we are in applied augment, its target has some additional nodes after the nodes from this augment */
875 iter->next = next;
876 next->prev = iter;
877 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100878 (*pchild)->prev = iter;
Radek Krejci30bfcd22017-01-27 16:54:48 +0100879 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200880 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200881 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200882
Michal Vaskoe022a562016-09-27 14:24:15 +0200883 /* check config value (but ignore them in groupings and augments) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100884 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); iter = iter->parent);
Michal Vaskoe022a562016-09-27 14:24:15 +0200885 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200886 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
887 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
888 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +0100889 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200890 return EXIT_FAILURE;
891 }
892 }
893
Radek Krejci41771502016-04-14 17:52:32 +0200894 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200895 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200896 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200897 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200898 /* store it only into container or list - the only data inner nodes */
899 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
900 if (iter->flags & LYS_INCL_STATUS) {
901 /* done, someone else set it already from here */
902 break;
903 }
904 /* set flag about including status data */
905 iter->flags |= LYS_INCL_STATUS;
906 }
907 }
908 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200909
910 /* create implicit input/output nodes to have available them as possible target for augment */
911 if (child->nodetype & (LYS_RPC | LYS_ACTION)) {
912 in = calloc(1, sizeof *in);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200913 out = calloc(1, sizeof *out);
914 if (!in || !out) {
915 LOGMEM;
916 free(in);
917 free(out);
918 return EXIT_FAILURE;
919 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200920 in->nodetype = LYS_INPUT;
921 in->name = lydict_insert(child->module->ctx, "input", 5);
Radek Krejci41a349b2016-10-24 19:21:59 +0200922 out->nodetype = LYS_OUTPUT;
Radek Krejcia8d111f2017-05-31 13:57:37 +0200923 out->name = lydict_insert(child->module->ctx, "output", 6);
Radek Krejci41a349b2016-10-24 19:21:59 +0200924 in->module = out->module = child->module;
925 in->parent = out->parent = child;
926 in->flags = out->flags = LYS_IMPLICIT;
927 in->next = (struct lys_node *)out;
928 in->prev = (struct lys_node *)out;
929 out->prev = (struct lys_node *)in;
930 child->child = (struct lys_node *)in;
931 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200932 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200933}
934
Michal Vasko29245662017-04-18 15:56:31 +0200935const struct lys_module *
936lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal, int implement)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200937{
Radek Krejcia1df1682016-04-11 14:56:59 +0200938 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200939 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200940 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200941
Radek Krejci00a0e712016-10-26 10:24:46 +0200942 ly_err_clean(1);
Radek Krejcif347abc2016-06-22 10:18:47 +0200943
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200944 if (!ctx || !data) {
945 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
946 return NULL;
947 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200948
Radek Krejcia1df1682016-04-11 14:56:59 +0200949 if (!internal && format == LYS_IN_YANG) {
950 /* enlarge data by 2 bytes for flex */
951 len = strlen(data);
952 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200953 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM, NULL);
Radek Krejcia1df1682016-04-11 14:56:59 +0200954 memcpy(enlarged_data, data, len);
955 enlarged_data[len] = enlarged_data[len + 1] = '\0';
956 data = enlarged_data;
957 }
958
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200959 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200960 case LYS_IN_YIN:
Michal Vasko29245662017-04-18 15:56:31 +0200961 mod = yin_read_module(ctx, data, NULL, implement);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200962 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200963 case LYS_IN_YANG:
Michal Vasko29245662017-04-18 15:56:31 +0200964 mod = yang_read_module(ctx, data, 0, NULL, implement);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100965 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200966 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200967 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200968 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200969 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200970
Radek Krejcia1df1682016-04-11 14:56:59 +0200971 free(enlarged_data);
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100972
973 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
974 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
975 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
976 * the anotation definitions available in the internal schema structure. There is another hack in schema
977 * printers to do not print this internally added annotation. */
978 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci5b190662017-04-13 08:56:14 +0200979 if (lyp_add_ietf_netconf_annotations(mod)) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100980 lys_free(mod, NULL, 1);
981 return NULL;
982 }
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100983 }
984
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200985 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200986}
987
Radek Krejcia1df1682016-04-11 14:56:59 +0200988API const struct lys_module *
989lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
990{
Michal Vasko29245662017-04-18 15:56:31 +0200991 return lys_parse_mem_(ctx, data, format, 0, 1);
Radek Krejcia1df1682016-04-11 14:56:59 +0200992}
993
Michal Vasko5a721fd2016-02-16 12:16:48 +0100994struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +0100995lys_sub_parse_mem(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200996{
Michal Vasko5b998712017-01-26 10:34:06 +0100997 char *enlarged_data = NULL;
Michal Vasko5a721fd2016-02-16 12:16:48 +0100998 struct lys_submodule *submod = NULL;
Michal Vasko5b998712017-01-26 10:34:06 +0100999 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001000
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001001 assert(module);
1002 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001003
Michal Vasko5b998712017-01-26 10:34:06 +01001004 if (format == LYS_IN_YANG) {
1005 /* enlarge data by 2 bytes for flex */
1006 len = strlen(data);
1007 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001008 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM, NULL);
Michal Vasko5b998712017-01-26 10:34:06 +01001009 memcpy(enlarged_data, data, len);
1010 enlarged_data[len] = enlarged_data[len + 1] = '\0';
1011 data = enlarged_data;
1012 }
1013
Radek Krejcic071c542016-01-27 14:57:51 +01001014 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +02001015 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001016
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001017 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001018 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +01001019 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001020 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001021 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001022 submod = yang_read_submodule(module, data, 0, unres);
1023 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001024 default:
Radek Krejci90a550a2016-04-13 16:00:58 +02001025 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001026 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001027 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001028
Michal Vasko5b998712017-01-26 10:34:06 +01001029 free(enlarged_data);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001030 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001031}
1032
Michal Vasko1e62a092015-12-01 12:27:20 +01001033API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +01001034lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1035{
1036 int fd;
1037 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +02001038 const char *rev, *dot, *filename;
1039 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +01001040
1041 if (!ctx || !path) {
1042 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1043 return NULL;
1044 }
1045
1046 fd = open(path, O_RDONLY);
1047 if (fd == -1) {
1048 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
1049 return NULL;
1050 }
1051
1052 ret = lys_parse_fd(ctx, fd, format);
1053 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +01001054
Radek Krejcid80c8602016-10-25 11:56:03 +02001055 if (!ret) {
1056 /* error */
1057 return NULL;
1058 }
1059
1060 /* check that name and revision match filename */
1061 filename = strrchr(path, '/');
1062 if (!filename) {
1063 filename = path;
1064 } else {
1065 filename++;
1066 }
1067 rev = strchr(filename, '@');
1068 dot = strrchr(filename, '.');
1069
1070 /* name */
1071 len = strlen(ret->name);
1072 if (strncmp(filename, ret->name, len) ||
1073 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejcic0c82302016-10-25 14:21:33 +02001074 LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +02001075 }
1076 if (rev) {
1077 len = dot - ++rev;
1078 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
1079 LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename,
1080 ret->rev_size ? ret->rev[0].date : "none");
1081 }
1082 }
1083
1084 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +01001085 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +01001086 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +01001087 }
1088
Michal Vasko662610a2015-12-07 11:25:45 +01001089 return ret;
1090}
1091
1092API const struct lys_module *
1093lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001094{
Michal Vasko1e62a092015-12-01 12:27:20 +01001095 const struct lys_module *module;
Radek Krejci0fb11502017-01-31 16:45:42 +01001096 size_t length;
Radek Krejci63a91a92015-07-29 13:31:04 +02001097 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +01001098 char buf[PATH_MAX];
1099 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +02001100
1101 if (!ctx || fd < 0) {
1102 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1103 return NULL;
1104 }
1105
Radek Krejci0fb11502017-01-31 16:45:42 +01001106 addr = lyp_mmap(fd, format == LYS_IN_YANG ? 1 : 0, &length);
Pavol Vicane36ea262015-11-12 11:57:47 +01001107 if (addr == MAP_FAILED) {
Radek Krejci0fb11502017-01-31 16:45:42 +01001108 LOGERR(LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001109 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001110 } else if (!addr) {
1111 LOGERR(LY_EINVAL, "Empty schema file.");
Pavol Vicane36ea262015-11-12 11:57:47 +01001112 return NULL;
1113 }
Radek Krejci0fb11502017-01-31 16:45:42 +01001114
Michal Vasko29245662017-04-18 15:56:31 +02001115 module = lys_parse_mem_(ctx, addr, format, 1, 1);
Radek Krejci0fb11502017-01-31 16:45:42 +01001116 lyp_munmap(addr, length);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001117
Radek Krejcia77904e2016-02-25 16:23:45 +01001118 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001119 /* get URI if there is /proc */
1120 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001121 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1122 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1123 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1124 }
1125 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001126 }
Radek Krejcib051f722016-02-25 15:12:21 +01001127 }
1128
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001129 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001130}
1131
Michal Vasko5a721fd2016-02-16 12:16:48 +01001132struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001133lys_sub_parse_fd(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001134{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001135 struct lys_submodule *submodule;
Radek Krejci0fb11502017-01-31 16:45:42 +01001136 size_t length;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001137 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001138
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001139 assert(module);
1140 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001141
Radek Krejci0fb11502017-01-31 16:45:42 +01001142 addr = lyp_mmap(fd, format == LYS_IN_YANG ? 1 : 0, &length);
Pavol Vicane36ea262015-11-12 11:57:47 +01001143 if (addr == MAP_FAILED) {
Radek Krejci0fb11502017-01-31 16:45:42 +01001144 LOGERR(LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001145 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001146 } else if (!addr) {
1147 LOGERR(LY_EINVAL, "Empty submodule schema file.");
Michal Vasko2e7241e2016-02-15 16:06:34 +01001148 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001149 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001150
Michal Vasko5b998712017-01-26 10:34:06 +01001151 /* get the main module */
1152 module = lys_main_module(module);
1153
1154 switch (format) {
1155 case LYS_IN_YIN:
1156 submodule = yin_read_submodule(module, addr, unres);
1157 break;
1158 case LYS_IN_YANG:
1159 submodule = yang_read_submodule(module, addr, 0, unres);
1160 break;
1161 default:
Michal Vasko85d41522017-02-24 09:49:16 +01001162 LOGINT;
1163 return NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001164 }
1165
Radek Krejcic645a3a2017-01-31 16:59:00 +01001166 lyp_munmap(addr, length);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001167 return submodule;
1168
Radek Krejciefaeba32015-05-27 14:30:57 +02001169}
1170
Radek Krejcibf285832017-01-26 16:05:41 +01001171int
1172lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt)
1173{
1174 unsigned int u;
1175
1176 for (u = start; u < ext_size; u++) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001177 if (ext[u]->insubstmt == substmt) {
Radek Krejcibf285832017-01-26 16:05:41 +01001178 return u;
1179 }
1180 }
1181
1182 return -1;
1183}
1184
Radek Krejcifdc0d702017-01-23 15:58:38 +01001185/*
1186 * duplicate extension instance
1187 */
1188int
Radek Krejci5138e9f2017-04-12 13:10:46 +02001189lys_ext_dup(struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent, LYEXT_PAR parent_type,
1190 struct lys_ext_instance ***new, int shallow, struct unres_schema *unres)
Radek Krejcifdc0d702017-01-23 15:58:38 +01001191{
1192 int i;
1193 uint8_t u = 0;
1194 struct lys_ext_instance **result;
1195 struct unres_ext *info, *info_orig;
1196
1197 assert(new);
1198
1199 if (!size) {
1200 if (orig) {
1201 LOGINT;
1202 return EXIT_FAILURE;
1203 }
1204 (*new) = NULL;
1205 return EXIT_SUCCESS;
1206 }
1207
1208 (*new) = result = calloc(size, sizeof *result);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001209 LY_CHECK_ERR_RETURN(!result, LOGMEM, EXIT_FAILURE);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001210 for (u = 0; u < size; u++) {
1211 if (orig[u]) {
1212 /* resolved extension instance, just duplicate it */
Radek Krejci8de8f612017-02-16 15:03:32 +01001213 switch(orig[u]->ext_type) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001214 case LYEXT_FLAG:
1215 result[u] = malloc(sizeof(struct lys_ext_instance));
Radek Krejcia8d111f2017-05-31 13:57:37 +02001216 LY_CHECK_ERR_GOTO(!result[u], LOGMEM, error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001217 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001218 case LYEXT_COMPLEX:
1219 result[u] = calloc(1, ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001220 LY_CHECK_ERR_GOTO(!result[u], LOGMEM, error);
1221
Radek Krejcifebdad72017-02-06 11:35:51 +01001222 ((struct lys_ext_instance_complex*)result[u])->substmt = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001223 /* TODO duplicate data in extension instance content */
1224 break;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001225 }
1226 /* generic part */
1227 result[u]->def = orig[u]->def;
1228 result[u]->flags = 0;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001229 result[u]->arg_value = lydict_insert(mod->ctx, orig[u]->arg_value, 0);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001230 result[u]->parent = parent;
1231 result[u]->parent_type = parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01001232 result[u]->insubstmt = orig[u]->insubstmt;
1233 result[u]->insubstmt_index = orig[u]->insubstmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01001234 result[u]->ext_type = orig[u]->ext_type;
Radek Krejci7f1d47e2017-04-12 15:29:02 +02001235 result[u]->priv = NULL;
1236 result[u]->nodetype = LYS_EXT;
1237 result[u]->module = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001238
1239 /* extensions */
1240 orig[u]->ext = NULL;
1241 result[u]->ext_size = orig[u]->ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001242 if (lys_ext_dup(mod, orig[u]->ext, orig[u]->ext_size, result[u],
Radek Krejci5138e9f2017-04-12 13:10:46 +02001243 LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001244 goto error;
1245 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001246
1247 /* in case of shallow copy (duplication for deviation), duplicate only the link to private data
1248 * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */
1249 if (shallow) {
1250 result[u]->priv = orig[u]->priv;
1251 }
Radek Krejcifdc0d702017-01-23 15:58:38 +01001252 } else {
1253 /* original extension is not yet resolved, so duplicate it in unres */
1254 i = unres_schema_find(unres, -1, &orig, UNRES_EXT);
1255 if (i == -1) {
1256 /* extension not found in unres */
1257 LOGINT;
1258 goto error;
1259 }
1260 info_orig = unres->str_snode[i];
1261 info = malloc(sizeof *info);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001262 LY_CHECK_ERR_GOTO(!info, LOGMEM, error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001263 info->datatype = info_orig->datatype;
1264 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01001265 info->data.yin = lyxml_dup_elem(mod->ctx, info_orig->data.yin, NULL, 1);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001266 } /* else TODO YANG */
1267 info->parent = parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001268 info->mod = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001269 info->parent_type = parent_type;
1270 info->ext_index = u;
1271 if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) {
1272 goto error;
1273 }
1274 }
1275 }
1276
1277 return EXIT_SUCCESS;
1278
1279error:
1280 (*new) = NULL;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001281 lys_extension_instances_free(mod->ctx, result, u, NULL);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001282 return EXIT_FAILURE;
1283}
1284
Radek Krejci1d82ef62015-08-07 14:44:40 +02001285static struct lys_restr *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001286lys_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 +02001287{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001288 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001289 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001290
Radek Krejci3733a802015-06-19 13:43:21 +02001291 if (!size) {
1292 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001293 }
Radek Krejci3733a802015-06-19 13:43:21 +02001294
1295 result = calloc(size, sizeof *result);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001296 LY_CHECK_ERR_RETURN(!result, LOGMEM, NULL);
1297
Radek Krejci3733a802015-06-19 13:43:21 +02001298 for (i = 0; i < size; i++) {
Radek Krejci77f22b22017-01-17 15:23:03 +01001299 result[i].ext_size = old[i].ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001300 lys_ext_dup(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 +01001301 result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0);
1302 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1303 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1304 result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0);
1305 result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001306 }
1307
1308 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001309}
1310
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001311void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001312lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr,
1313 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci0bd5db42015-06-19 13:30:07 +02001314{
1315 assert(ctx);
1316 if (!restr) {
1317 return;
1318 }
1319
Radek Krejci5138e9f2017-04-12 13:10:46 +02001320 lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001321 lydict_remove(ctx, restr->expr);
1322 lydict_remove(ctx, restr->dsc);
1323 lydict_remove(ctx, restr->ref);
1324 lydict_remove(ctx, restr->eapptag);
1325 lydict_remove(ctx, restr->emsg);
1326}
1327
Pavol Vican05810b62016-11-23 14:07:22 +01001328void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001329lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size,
1330 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001331{
1332 uint8_t i;
1333
1334 for (i = 0; i < iffeature_size; ++i) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001335 lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001336 free(iffeature[i].expr);
1337 free(iffeature[i].features);
1338 }
1339 free(iffeature);
1340}
1341
Michal Vaskob84f88a2015-09-24 13:16:10 +02001342static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001343type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001344 LY_DATA_TYPE base, int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001345{
1346 int i;
1347
1348 switch (base) {
1349 case LY_TYPE_BINARY:
1350 if (old->info.binary.length) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001351 new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001352 }
1353 break;
1354
1355 case LY_TYPE_BITS:
1356 new->info.bits.count = old->info.bits.count;
1357 if (new->info.bits.count) {
1358 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001359 LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM, -1);
1360
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001361 for (i = 0; i < new->info.bits.count; i++) {
1362 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1363 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1364 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1365 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1366 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001367 new->info.bits.bit[i].ext_size = old->info.bits.bit[i].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001368 if (lys_ext_dup(mod, old->info.bits.bit[i].ext, old->info.bits.bit[i].ext_size,
Radek Krejcif0bb3602017-01-25 17:05:08 +01001369 &new->info.bits.bit[i], LYEXT_PAR_TYPE_BIT,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001370 &new->info.bits.bit[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001371 return -1;
1372 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001373 }
1374 }
1375 break;
1376
1377 case LY_TYPE_DEC64:
1378 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001379 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001380 if (old->info.dec64.range) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001381 new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001382 }
1383 break;
1384
1385 case LY_TYPE_ENUM:
1386 new->info.enums.count = old->info.enums.count;
1387 if (new->info.enums.count) {
1388 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001389 LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM, -1);
1390
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001391 for (i = 0; i < new->info.enums.count; i++) {
1392 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1393 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1394 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1395 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1396 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001397 new->info.enums.enm[i].ext_size = old->info.enums.enm[i].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001398 if (lys_ext_dup(mod, old->info.enums.enm[i].ext, old->info.enums.enm[i].ext_size,
Radek Krejcif0bb3602017-01-25 17:05:08 +01001399 &new->info.enums.enm[i], LYEXT_PAR_TYPE_ENUM,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001400 &new->info.enums.enm[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001401 return -1;
1402 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001403 }
1404 }
1405 break;
1406
1407 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001408 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001409 if (old->info.ident.count) {
1410 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001411 LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM, -1);
Michal Vasko878e38d2016-09-05 12:17:53 +02001412 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1413 } else {
1414 /* there can be several unresolved base identities, duplicate them all */
1415 i = -1;
Radek Krejciddddd0d2017-01-20 15:20:46 +01001416 do {
1417 i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF);
1418 if (i != -1) {
1419 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1420 return -1;
1421 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001422 }
1423 --i;
Radek Krejciddddd0d2017-01-20 15:20:46 +01001424 } while (i > -1);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001425 }
1426 break;
1427
1428 case LY_TYPE_INST:
1429 new->info.inst.req = old->info.inst.req;
1430 break;
1431
1432 case LY_TYPE_INT8:
1433 case LY_TYPE_INT16:
1434 case LY_TYPE_INT32:
1435 case LY_TYPE_INT64:
1436 case LY_TYPE_UINT8:
1437 case LY_TYPE_UINT16:
1438 case LY_TYPE_UINT32:
1439 case LY_TYPE_UINT64:
1440 if (old->info.num.range) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001441 new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001442 }
1443 break;
1444
1445 case LY_TYPE_LEAFREF:
1446 if (old->info.lref.path) {
1447 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko1c007172017-03-10 10:20:44 +01001448 if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001449 return -1;
1450 }
1451 }
1452 break;
1453
1454 case LY_TYPE_STRING:
1455 if (old->info.str.length) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001456 new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001457 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001458 new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001459 new->info.str.pat_count = old->info.str.pat_count;
1460 break;
1461
1462 case LY_TYPE_UNION:
1463 new->info.uni.count = old->info.uni.count;
1464 if (new->info.uni.count) {
1465 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001466 LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM, -1);
1467
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001468 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001469 if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]),
1470 in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001471 return -1;
1472 }
1473 }
1474 }
1475 break;
1476
1477 default:
1478 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1479 break;
1480 }
1481 return EXIT_SUCCESS;
1482}
1483
1484struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001485lys_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 +02001486 int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001487{
1488 struct yang_type *new;
1489
1490 new = calloc(1, sizeof *new);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001491 LY_CHECK_ERR_RETURN(!new, LOGMEM, NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001492 new->flags = old->flags;
1493 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001494 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001495 new->type = type;
1496 if (!new->name) {
1497 LOGMEM;
1498 goto error;
1499 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001500 if (type_dup(module, parent, type, old->type, new->base, in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001501 new->type->base = new->base;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001502 lys_type_free(module->ctx, new->type, NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001503 memset(&new->type->info, 0, sizeof new->type->info);
1504 goto error;
1505 }
1506 return new;
1507
1508 error:
1509 free(new);
1510 return NULL;
1511}
1512
Radek Krejci43ce4b72017-01-04 11:02:38 +01001513API const void *
1514lys_ext_instance_substmt(const struct lys_ext_instance *ext)
1515{
1516 if (!ext) {
1517 return NULL;
1518 }
1519
Radek Krejcifebdad72017-02-06 11:35:51 +01001520 switch (ext->insubstmt) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001521 case LYEXT_SUBSTMT_SELF:
1522 case LYEXT_SUBSTMT_MODIFIER:
1523 case LYEXT_SUBSTMT_VERSION:
1524 return NULL;
1525 case LYEXT_SUBSTMT_ARGUMENT:
1526 if (ext->parent_type == LYEXT_PAR_EXT) {
1527 return ((struct lys_ext_instance*)ext->parent)->arg_value;
1528 }
1529 break;
1530 case LYEXT_SUBSTMT_BASE:
1531 if (ext->parent_type == LYEXT_PAR_TYPE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001532 return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001533 } else if (ext->parent_type == LYEXT_PAR_IDENT) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001534 return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001535 }
1536 break;
1537 case LYEXT_SUBSTMT_BELONGSTO:
1538 if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) {
1539 return ((struct lys_submodule*)ext->parent)->belongsto;
1540 }
1541 break;
1542 case LYEXT_SUBSTMT_CONFIG:
1543 case LYEXT_SUBSTMT_MANDATORY:
1544 if (ext->parent_type == LYEXT_PAR_NODE) {
1545 return &((struct lys_node*)ext->parent)->flags;
1546 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1547 return &((struct lys_deviate*)ext->parent)->flags;
1548 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1549 return &((struct lys_refine*)ext->parent)->flags;
1550 }
1551 break;
1552 case LYEXT_SUBSTMT_CONTACT:
1553 if (ext->parent_type == LYEXT_PAR_MODULE) {
1554 return ((struct lys_module*)ext->parent)->contact;
1555 }
1556 break;
1557 case LYEXT_SUBSTMT_DEFAULT:
1558 if (ext->parent_type == LYEXT_PAR_NODE) {
1559 switch (((struct lys_node*)ext->parent)->nodetype) {
1560 case LYS_LEAF:
1561 case LYS_LEAFLIST:
1562 /* in case of leaf, the index is supposed to be 0, so it will return the
1563 * correct pointer despite the leaf structure does not have dflt as array */
Radek Krejcifebdad72017-02-06 11:35:51 +01001564 return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001565 case LYS_CHOICE:
1566 return ((struct lys_node_choice*)ext->parent)->dflt;
1567 default:
1568 /* internal error */
1569 break;
1570 }
1571 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1572 return ((struct lys_tpdf*)ext->parent)->dflt;
1573 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001574 return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001575 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001576 return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001577 }
1578 break;
1579 case LYEXT_SUBSTMT_DESCRIPTION:
1580 switch (ext->parent_type) {
1581 case LYEXT_PAR_NODE:
1582 return ((struct lys_node*)ext->parent)->dsc;
1583 case LYEXT_PAR_MODULE:
1584 return ((struct lys_module*)ext->parent)->dsc;
1585 case LYEXT_PAR_IMPORT:
1586 return ((struct lys_import*)ext->parent)->dsc;
1587 case LYEXT_PAR_INCLUDE:
1588 return ((struct lys_include*)ext->parent)->dsc;
1589 case LYEXT_PAR_EXT:
1590 return ((struct lys_ext*)ext->parent)->dsc;
1591 case LYEXT_PAR_FEATURE:
1592 return ((struct lys_feature*)ext->parent)->dsc;
1593 case LYEXT_PAR_TPDF:
1594 return ((struct lys_tpdf*)ext->parent)->dsc;
1595 case LYEXT_PAR_TYPE_BIT:
1596 return ((struct lys_type_bit*)ext->parent)->dsc;
1597 case LYEXT_PAR_TYPE_ENUM:
1598 return ((struct lys_type_enum*)ext->parent)->dsc;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001599 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001600 return ((struct lys_restr*)ext->parent)->dsc;
1601 case LYEXT_PAR_WHEN:
1602 return ((struct lys_when*)ext->parent)->dsc;
1603 case LYEXT_PAR_IDENT:
1604 return ((struct lys_ident*)ext->parent)->dsc;
1605 case LYEXT_PAR_DEVIATION:
1606 return ((struct lys_deviation*)ext->parent)->dsc;
1607 case LYEXT_PAR_REVISION:
1608 return ((struct lys_revision*)ext->parent)->dsc;
1609 case LYEXT_PAR_REFINE:
1610 return ((struct lys_refine*)ext->parent)->dsc;
1611 default:
1612 break;
1613 }
1614 break;
1615 case LYEXT_SUBSTMT_ERRTAG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001616 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001617 return ((struct lys_restr*)ext->parent)->eapptag;
1618 }
1619 break;
1620 case LYEXT_SUBSTMT_ERRMSG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001621 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001622 return ((struct lys_restr*)ext->parent)->emsg;
1623 }
1624 break;
1625 case LYEXT_SUBSTMT_DIGITS:
1626 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) {
1627 return &((struct lys_type*)ext->parent)->info.dec64.dig;
1628 }
1629 break;
1630 case LYEXT_SUBSTMT_KEY:
1631 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1632 return ((struct lys_node_list*)ext->parent)->keys;
1633 }
1634 break;
1635 case LYEXT_SUBSTMT_MAX:
1636 if (ext->parent_type == LYEXT_PAR_NODE) {
1637 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1638 return &((struct lys_node_list*)ext->parent)->max;
1639 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1640 return &((struct lys_node_leaflist*)ext->parent)->max;
1641 }
1642 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1643 return &((struct lys_refine*)ext->parent)->mod.list.max;
1644 }
1645 break;
1646 case LYEXT_SUBSTMT_MIN:
1647 if (ext->parent_type == LYEXT_PAR_NODE) {
1648 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1649 return &((struct lys_node_list*)ext->parent)->min;
1650 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1651 return &((struct lys_node_leaflist*)ext->parent)->min;
1652 }
1653 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1654 return &((struct lys_refine*)ext->parent)->mod.list.min;
1655 }
1656 break;
1657 case LYEXT_SUBSTMT_NAMESPACE:
1658 if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) {
1659 return ((struct lys_module*)ext->parent)->ns;
1660 }
1661 break;
1662 case LYEXT_SUBSTMT_ORDEREDBY:
1663 if (ext->parent_type == LYEXT_PAR_NODE &&
1664 (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1665 return &((struct lys_node_list*)ext->parent)->flags;
1666 }
1667 break;
1668 case LYEXT_SUBSTMT_ORGANIZATION:
1669 if (ext->parent_type == LYEXT_PAR_MODULE) {
1670 return ((struct lys_module*)ext->parent)->org;
1671 }
1672 break;
1673 case LYEXT_SUBSTMT_PATH:
1674 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1675 return ((struct lys_type*)ext->parent)->info.lref.path;
1676 }
1677 break;
1678 case LYEXT_SUBSTMT_POSITION:
1679 if (ext->parent_type == LYEXT_PAR_TYPE_BIT) {
1680 return &((struct lys_type_bit*)ext->parent)->pos;
1681 }
1682 break;
1683 case LYEXT_SUBSTMT_PREFIX:
1684 if (ext->parent_type == LYEXT_PAR_MODULE) {
1685 /* covers also lys_submodule */
1686 return ((struct lys_module*)ext->parent)->prefix;
1687 } else if (ext->parent_type == LYEXT_PAR_IMPORT) {
1688 return ((struct lys_import*)ext->parent)->prefix;
1689 }
1690 break;
1691 case LYEXT_SUBSTMT_PRESENCE:
1692 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) {
1693 return ((struct lys_node_container*)ext->parent)->presence;
1694 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1695 return ((struct lys_refine*)ext->parent)->mod.presence;
1696 }
1697 break;
1698 case LYEXT_SUBSTMT_REFERENCE:
1699 switch (ext->parent_type) {
1700 case LYEXT_PAR_NODE:
1701 return ((struct lys_node*)ext->parent)->ref;
1702 case LYEXT_PAR_MODULE:
1703 return ((struct lys_module*)ext->parent)->ref;
1704 case LYEXT_PAR_IMPORT:
1705 return ((struct lys_import*)ext->parent)->ref;
1706 case LYEXT_PAR_INCLUDE:
1707 return ((struct lys_include*)ext->parent)->ref;
1708 case LYEXT_PAR_EXT:
1709 return ((struct lys_ext*)ext->parent)->ref;
1710 case LYEXT_PAR_FEATURE:
1711 return ((struct lys_feature*)ext->parent)->ref;
1712 case LYEXT_PAR_TPDF:
1713 return ((struct lys_tpdf*)ext->parent)->ref;
1714 case LYEXT_PAR_TYPE_BIT:
1715 return ((struct lys_type_bit*)ext->parent)->ref;
1716 case LYEXT_PAR_TYPE_ENUM:
1717 return ((struct lys_type_enum*)ext->parent)->ref;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001718 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001719 return ((struct lys_restr*)ext->parent)->ref;
1720 case LYEXT_PAR_WHEN:
1721 return ((struct lys_when*)ext->parent)->ref;
1722 case LYEXT_PAR_IDENT:
1723 return ((struct lys_ident*)ext->parent)->ref;
1724 case LYEXT_PAR_DEVIATION:
1725 return ((struct lys_deviation*)ext->parent)->ref;
1726 case LYEXT_PAR_REVISION:
1727 return ((struct lys_revision*)ext->parent)->ref;
1728 case LYEXT_PAR_REFINE:
1729 return ((struct lys_refine*)ext->parent)->ref;
1730 default:
1731 break;
1732 }
1733 break;
Radek Krejcibe336392017-02-07 10:54:24 +01001734 case LYEXT_SUBSTMT_REQINSTANCE:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001735 if (ext->parent_type == LYEXT_PAR_TYPE) {
1736 if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1737 return &((struct lys_type*)ext->parent)->info.lref.req;
1738 } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) {
1739 return &((struct lys_type*)ext->parent)->info.inst.req;
1740 }
1741 }
1742 break;
1743 case LYEXT_SUBSTMT_REVISIONDATE:
1744 if (ext->parent_type == LYEXT_PAR_IMPORT) {
1745 return ((struct lys_import*)ext->parent)->rev;
1746 } else if (ext->parent_type == LYEXT_PAR_INCLUDE) {
1747 return ((struct lys_include*)ext->parent)->rev;
1748 }
1749 break;
1750 case LYEXT_SUBSTMT_STATUS:
1751 switch (ext->parent_type) {
1752 case LYEXT_PAR_NODE:
1753 case LYEXT_PAR_IDENT:
1754 case LYEXT_PAR_TPDF:
1755 case LYEXT_PAR_EXT:
1756 case LYEXT_PAR_FEATURE:
1757 case LYEXT_PAR_TYPE_ENUM:
1758 case LYEXT_PAR_TYPE_BIT:
1759 /* in all structures the flags member is at the same offset */
1760 return &((struct lys_node*)ext->parent)->flags;
1761 default:
1762 break;
1763 }
1764 break;
1765 case LYEXT_SUBSTMT_UNIQUE:
1766 if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001767 return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001768 } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001769 return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001770 }
1771 break;
1772 case LYEXT_SUBSTMT_UNITS:
1773 if (ext->parent_type == LYEXT_PAR_NODE &&
1774 (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1775 /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */
1776 return ((struct lys_node_leaf*)ext->parent)->units;
1777 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1778 return ((struct lys_tpdf*)ext->parent)->units;
1779 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1780 return ((struct lys_deviate*)ext->parent)->units;
1781 }
1782 break;
1783 case LYEXT_SUBSTMT_VALUE:
1784 if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) {
1785 return &((struct lys_type_enum*)ext->parent)->value;
1786 }
1787 break;
1788 case LYEXT_SUBSTMT_YINELEM:
1789 if (ext->parent_type == LYEXT_PAR_EXT) {
1790 return &((struct lys_ext*)ext->parent)->flags;
1791 }
1792 break;
1793 }
1794 LOGINT;
1795 return NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01001796}
1797
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001798static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001799lys_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 +02001800 int in_grp, int shallow, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001801{
1802 int i;
1803
Michal Vasko1dca6882015-10-22 14:29:42 +02001804 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001805 new->base = old->base;
1806 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001807 new->parent = (struct lys_tpdf *)parent;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001808 new->ext_size = old->ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001809 if (lys_ext_dup(mod, old->ext, old->ext_size, new, LYEXT_PAR_TYPE, &new->ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001810 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01001811 }
Radek Krejci3733a802015-06-19 13:43:21 +02001812
Michal Vasko1c007172017-03-10 10:20:44 +01001813 i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001814 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001815 /* HACK (serious one) for unres */
1816 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001817 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001818 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp,
1819 shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001820 } else {
1821 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1822 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001823 /* all these unres additions can fail even though they did not before */
Michal Vasko1c007172017-03-10 10:20:44 +01001824 if (!new->der || (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent) == -1)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001825 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001826 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001827 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001828 }
1829
Radek Krejci5138e9f2017-04-12 13:10:46 +02001830 return type_dup(mod, parent, new, old, new->base, in_grp, shallow, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001831}
1832
1833void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001834lys_type_free(struct ly_ctx *ctx, struct lys_type *type,
1835 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02001836{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001837 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001838
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001839 assert(ctx);
1840 if (!type) {
1841 return;
1842 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001843
Michal Vasko1dca6882015-10-22 14:29:42 +02001844 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001845
Radek Krejci5138e9f2017-04-12 13:10:46 +02001846 lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01001847
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001848 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001849 case LY_TYPE_BINARY:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001850 lys_restr_free(ctx, type->info.binary.length, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001851 free(type->info.binary.length);
1852 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001853 case LY_TYPE_BITS:
1854 for (i = 0; i < type->info.bits.count; i++) {
1855 lydict_remove(ctx, type->info.bits.bit[i].name);
1856 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1857 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02001858 lys_iffeature_free(ctx, type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size,
1859 private_destructor);
1860 lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size,
1861 private_destructor);
Radek Krejci994b6f62015-06-18 16:47:27 +02001862 }
1863 free(type->info.bits.bit);
1864 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001865
1866 case LY_TYPE_DEC64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001867 lys_restr_free(ctx, type->info.dec64.range, private_destructor);
Radek Krejcif9401c32015-06-26 16:47:36 +02001868 free(type->info.dec64.range);
1869 break;
1870
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001871 case LY_TYPE_ENUM:
1872 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001873 lydict_remove(ctx, type->info.enums.enm[i].name);
1874 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1875 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02001876 lys_iffeature_free(ctx, type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size,
1877 private_destructor);
1878 lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size,
1879 private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001880 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001881 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001882 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001883
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001884 case LY_TYPE_INT8:
1885 case LY_TYPE_INT16:
1886 case LY_TYPE_INT32:
1887 case LY_TYPE_INT64:
1888 case LY_TYPE_UINT8:
1889 case LY_TYPE_UINT16:
1890 case LY_TYPE_UINT32:
1891 case LY_TYPE_UINT64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001892 lys_restr_free(ctx, type->info.num.range, private_destructor);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001893 free(type->info.num.range);
1894 break;
1895
Radek Krejcidc4c1412015-06-19 15:39:54 +02001896 case LY_TYPE_LEAFREF:
1897 lydict_remove(ctx, type->info.lref.path);
1898 break;
1899
Radek Krejci3733a802015-06-19 13:43:21 +02001900 case LY_TYPE_STRING:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001901 lys_restr_free(ctx, type->info.str.length, private_destructor);
Radek Krejci3733a802015-06-19 13:43:21 +02001902 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001903 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001904 lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001905 }
1906 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001907 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001908
Radek Krejcie4c366b2015-07-02 10:11:31 +02001909 case LY_TYPE_UNION:
1910 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001911 lys_type_free(ctx, &type->info.uni.types[i], private_destructor);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001912 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001913 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001914 break;
1915
Michal Vaskod3282192016-09-05 11:27:57 +02001916 case LY_TYPE_IDENT:
1917 free(type->info.ident.ref);
1918 break;
1919
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001920 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001921 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001922 break;
1923 }
Radek Krejci5a065542015-05-22 15:02:07 +02001924}
1925
Radek Krejci1d82ef62015-08-07 14:44:40 +02001926static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001927lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf,
1928 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02001929{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001930 assert(ctx);
1931 if (!tpdf) {
1932 return;
1933 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001934
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001935 lydict_remove(ctx, tpdf->name);
1936 lydict_remove(ctx, tpdf->dsc);
1937 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001938
Radek Krejci5138e9f2017-04-12 13:10:46 +02001939 lys_type_free(ctx, &tpdf->type, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001940
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001941 lydict_remove(ctx, tpdf->units);
1942 lydict_remove(ctx, tpdf->dflt);
Radek Krejcie534c132016-11-23 13:32:31 +01001943
Radek Krejci5138e9f2017-04-12 13:10:46 +02001944 lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001945}
1946
Radek Krejci1d82ef62015-08-07 14:44:40 +02001947static struct lys_when *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001948lys_when_dup(struct lys_module *mod, struct lys_when *old, int shallow, struct unres_schema *unres)
Radek Krejci00768f42015-06-18 17:04:04 +02001949{
Radek Krejci76512572015-08-04 09:47:08 +02001950 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001951
1952 if (!old) {
1953 return NULL;
1954 }
1955
1956 new = calloc(1, sizeof *new);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001957 LY_CHECK_ERR_RETURN(!new, LOGMEM, NULL);
Radek Krejci8d6b7422017-02-03 14:42:13 +01001958 new->cond = lydict_insert(mod->ctx, old->cond, 0);
1959 new->dsc = lydict_insert(mod->ctx, old->dsc, 0);
1960 new->ref = lydict_insert(mod->ctx, old->ref, 0);
Radek Krejcif0bb3602017-01-25 17:05:08 +01001961 new->ext_size = old->ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001962 lys_ext_dup(mod, old->ext, old->ext_size, new, LYEXT_PAR_WHEN, &new->ext, shallow, unres);
Radek Krejci00768f42015-06-18 17:04:04 +02001963
1964 return new;
1965}
1966
Michal Vasko0308dd62015-10-07 09:14:40 +02001967void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001968lys_when_free(struct ly_ctx *ctx, struct lys_when *w,
1969 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001970{
1971 if (!w) {
1972 return;
1973 }
1974
Radek Krejci5138e9f2017-04-12 13:10:46 +02001975 lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001976 lydict_remove(ctx, w->cond);
1977 lydict_remove(ctx, w->dsc);
1978 lydict_remove(ctx, w->ref);
1979
1980 free(w);
1981}
1982
Radek Krejcib7f5e412015-08-13 10:15:51 +02001983static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001984lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug,
1985 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02001986{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001987 struct lys_node *next, *sub;
1988
Radek Krejcic071c542016-01-27 14:57:51 +01001989 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01001990 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001991 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001992 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001993 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001994 }
1995
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001996 lydict_remove(ctx, aug->target_name);
1997 lydict_remove(ctx, aug->dsc);
1998 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001999
Radek Krejci5138e9f2017-04-12 13:10:46 +02002000 lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, private_destructor);
2001 lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002002
Radek Krejci5138e9f2017-04-12 13:10:46 +02002003 lys_when_free(ctx, aug->when, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002004}
2005
Radek Krejci1d82ef62015-08-07 14:44:40 +02002006static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002007lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident,
2008 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci6793db02015-05-22 17:49:54 +02002009{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002010 assert(ctx);
2011 if (!ident) {
2012 return;
2013 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002014
Radek Krejci018f1f52016-08-03 16:01:20 +02002015 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02002016 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002017 lydict_remove(ctx, ident->name);
2018 lydict_remove(ctx, ident->dsc);
2019 lydict_remove(ctx, ident->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002020 lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, private_destructor);
2021 lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor);
Radek Krejci6793db02015-05-22 17:49:54 +02002022
2023}
2024
Radek Krejci1d82ef62015-08-07 14:44:40 +02002025static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002026lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp,
2027 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002028{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002029 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002030
Radek Krejcid12f57b2015-08-06 10:43:39 +02002031 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002032 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002033 lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002034 }
2035 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002036}
2037
Radek Krejci1d82ef62015-08-07 14:44:40 +02002038static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002039lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io,
2040 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcid12f57b2015-08-06 10:43:39 +02002041{
2042 int i;
2043
2044 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
2045 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002046 lys_tpdf_free(ctx, &io->tpdf[i], private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002047 }
2048 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002049
2050 for (i = 0; i < io->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002051 lys_restr_free(ctx, &io->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002052 }
2053 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002054}
2055
Radek Krejci1d82ef62015-08-07 14:44:40 +02002056static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002057lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif,
2058 void (*private_destructor)(const struct lys_node *node, void *priv))
Pavol Vican7cff97e2016-08-09 14:56:08 +02002059{
2060 int i;
2061
2062 for (i = 0; i < notif->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002063 lys_restr_free(ctx, &notif->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002064 }
2065 free(notif->must);
2066
2067 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002068 lys_tpdf_free(ctx, &notif->tpdf[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002069 }
2070 free(notif->tpdf);
2071}
2072static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002073lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml,
2074 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci537cf382015-06-04 11:07:01 +02002075{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002076 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002077
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002078 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002079 lys_restr_free(ctx, &anyxml->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002080 }
2081 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002082
Radek Krejci5138e9f2017-04-12 13:10:46 +02002083 lys_when_free(ctx, anyxml->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002084}
2085
Radek Krejci1d82ef62015-08-07 14:44:40 +02002086static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002087lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf,
2088 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002089{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002090 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002091
Radek Krejci85a54be2016-10-20 12:39:56 +02002092 /* leafref backlinks */
2093 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002094
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002095 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002096 lys_restr_free(ctx, &leaf->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002097 }
2098 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002099
Radek Krejci5138e9f2017-04-12 13:10:46 +02002100 lys_when_free(ctx, leaf->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002101
Radek Krejci5138e9f2017-04-12 13:10:46 +02002102 lys_type_free(ctx, &leaf->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002103 lydict_remove(ctx, leaf->units);
2104 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02002105}
2106
Radek Krejci1d82ef62015-08-07 14:44:40 +02002107static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002108lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist,
2109 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002110{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002111 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002112
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002113 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002114 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002115 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002116 }
2117
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002118 for (i = 0; i < llist->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002119 lys_restr_free(ctx, &llist->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002120 }
2121 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002122
Pavol Vican38321d02016-08-16 14:56:02 +02002123 for (i = 0; i < llist->dflt_size; i++) {
2124 lydict_remove(ctx, llist->dflt[i]);
2125 }
2126 free(llist->dflt);
2127
Radek Krejci5138e9f2017-04-12 13:10:46 +02002128 lys_when_free(ctx, llist->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002129
Radek Krejci5138e9f2017-04-12 13:10:46 +02002130 lys_type_free(ctx, &llist->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002131 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02002132}
2133
Radek Krejci1d82ef62015-08-07 14:44:40 +02002134static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002135lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list,
2136 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002137{
Radek Krejci581ce772015-11-10 17:22:40 +01002138 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002139
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002140 /* handle only specific parts for LY_NODE_LIST */
2141 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002142 lys_tpdf_free(ctx, &list->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002143 }
2144 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002145
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002146 for (i = 0; i < list->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002147 lys_restr_free(ctx, &list->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002148 }
2149 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002150
Radek Krejci5138e9f2017-04-12 13:10:46 +02002151 lys_when_free(ctx, list->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002152
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002153 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02002154 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002155 lydict_remove(ctx, list->unique[i].expr[j]);
2156 }
2157 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002158 }
2159 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02002160
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002161 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002162}
2163
Radek Krejci1d82ef62015-08-07 14:44:40 +02002164static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002165lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont,
2166 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002167{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002168 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002169
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002170 /* handle only specific parts for LY_NODE_CONTAINER */
2171 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02002172
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002173 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002174 lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002175 }
2176 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02002177
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002178 for (i = 0; i < cont->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002179 lys_restr_free(ctx, &cont->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002180 }
2181 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002182
Radek Krejci5138e9f2017-04-12 13:10:46 +02002183 lys_when_free(ctx, cont->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002184}
2185
Radek Krejci1d82ef62015-08-07 14:44:40 +02002186static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002187lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f,
2188 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci3cf9e222015-06-18 11:37:50 +02002189{
2190 lydict_remove(ctx, f->name);
2191 lydict_remove(ctx, f->dsc);
2192 lydict_remove(ctx, f->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002193 lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, private_destructor);
Radek Krejci9de2c042016-10-19 16:53:06 +02002194 ly_set_free(f->depfeatures);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002195 lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002196}
2197
2198static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002199lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e,
2200 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie534c132016-11-23 13:32:31 +01002201{
2202 lydict_remove(ctx, e->name);
2203 lydict_remove(ctx, e->dsc);
2204 lydict_remove(ctx, e->ref);
2205 lydict_remove(ctx, e->argument);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002206 lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002207}
2208
Radek Krejci1d82ef62015-08-07 14:44:40 +02002209static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002210lys_deviation_free(struct lys_module *module, struct lys_deviation *dev,
2211 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcieb00f512015-07-01 16:44:58 +02002212{
Radek Krejci581ce772015-11-10 17:22:40 +01002213 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01002214 struct ly_ctx *ctx;
2215 struct lys_node *next, *elem;
2216
2217 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02002218
2219 lydict_remove(ctx, dev->target_name);
2220 lydict_remove(ctx, dev->dsc);
2221 lydict_remove(ctx, dev->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002222 lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002223
Pavol Vican64d0b762016-08-25 10:44:59 +02002224 if (!dev->deviate) {
2225 return ;
2226 }
2227
Michal Vaskoff006c12016-02-17 11:15:19 +01002228 /* the module was freed, but we only need the context from orig_node, use ours */
2229 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2230 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
2231 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
2232 elem->module = module;
2233
2234 LY_TREE_DFS_END(dev->orig_node, next, elem);
2235 }
2236 lys_node_free(dev->orig_node, NULL, 0);
2237 } else {
2238 /* it's just a shallow copy, freeing one node */
2239 dev->orig_node->module = module;
2240 lys_node_free(dev->orig_node, NULL, 1);
2241 }
2242
Radek Krejcieb00f512015-07-01 16:44:58 +02002243 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002244 lys_extension_instances_free(ctx, dev->deviate[i].ext, dev->deviate[i].ext_size, private_destructor);
Radek Krejci3c4b0ea2017-01-24 16:00:47 +01002245
Radek Krejcid5a5c282016-08-15 15:38:08 +02002246 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02002247 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02002248 }
2249 free(dev->deviate[i].dflt);
2250
Radek Krejcieb00f512015-07-01 16:44:58 +02002251 lydict_remove(ctx, dev->deviate[i].units);
2252
2253 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
2254 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002255 lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002256 }
2257 free(dev->deviate[i].must);
2258
2259 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002260 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
2261 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
2262 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01002263 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002264 }
2265 free(dev->deviate[i].unique);
2266 }
2267 }
2268 free(dev->deviate);
2269}
2270
Radek Krejci1d82ef62015-08-07 14:44:40 +02002271static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002272lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses,
2273 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02002274{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002275 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02002276
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002277 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002278 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002279 lydict_remove(ctx, uses->refine[i].dsc);
2280 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002281
Michal Vaskoa275c0a2015-09-24 09:55:42 +02002282 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002283 lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002284 }
2285 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002286
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002287 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02002288 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002289 }
2290 free(uses->refine[i].dflt);
2291
Radek Krejci5138e9f2017-04-12 13:10:46 +02002292 lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002293
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002294 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002295 lydict_remove(ctx, uses->refine[i].mod.presence);
2296 }
2297 }
2298 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002299
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002300 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002301 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002302 }
2303 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002304
Radek Krejci5138e9f2017-04-12 13:10:46 +02002305 lys_when_free(ctx, uses->when, private_destructor);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002306}
2307
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002308void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002309lys_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 +02002310{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002311 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02002312 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002313
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002314 if (!node) {
2315 return;
2316 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002317
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002318 assert(node->module);
2319 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02002320
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002321 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002322
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002323 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002324 if (node->priv && private_destructor) {
2325 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002326 }
2327
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002328 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002329 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002330 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002331 lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002332 lydict_remove(ctx, node->dsc);
2333 lydict_remove(ctx, node->ref);
2334 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002335
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002336 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002337 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002338 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002339 }
2340 }
2341
Radek Krejci5138e9f2017-04-12 13:10:46 +02002342 lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002343
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002344 /* specific part */
2345 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002346 case LYS_CONTAINER:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002347 lys_container_free(ctx, (struct lys_node_container *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002348 break;
Radek Krejci76512572015-08-04 09:47:08 +02002349 case LYS_CHOICE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002350 lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002351 break;
Radek Krejci76512572015-08-04 09:47:08 +02002352 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002353 lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002354 break;
Radek Krejci76512572015-08-04 09:47:08 +02002355 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002356 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002357 break;
Radek Krejci76512572015-08-04 09:47:08 +02002358 case LYS_LIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002359 lys_list_free(ctx, (struct lys_node_list *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002360 break;
Radek Krejci76512572015-08-04 09:47:08 +02002361 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002362 case LYS_ANYDATA:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002363 lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002364 break;
Radek Krejci76512572015-08-04 09:47:08 +02002365 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002366 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002367 break;
Radek Krejci76512572015-08-04 09:47:08 +02002368 case LYS_CASE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002369 lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002370 break;
Radek Krejci76512572015-08-04 09:47:08 +02002371 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002372 /* do nothing */
2373 break;
Radek Krejci76512572015-08-04 09:47:08 +02002374 case LYS_GROUPING:
2375 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002376 case LYS_ACTION:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002377 lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002378 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02002379 case LYS_NOTIF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002380 lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002381 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002382 case LYS_INPUT:
2383 case LYS_OUTPUT:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002384 lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002385 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01002386 case LYS_EXT:
Michal Vasko591e0b22015-08-13 13:53:43 +02002387 case LYS_UNKNOWN:
2388 LOGINT;
2389 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002390 }
Radek Krejci5a065542015-05-22 15:02:07 +02002391
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002392 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002393 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002394 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002395}
2396
Radek Krejci2eee5c02016-12-06 19:18:05 +01002397API struct lys_module *
2398lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02002399{
2400 struct ly_ctx *ctx;
2401 int i;
2402
2403 if (!mod || mod->implemented) {
2404 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002405 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002406 }
2407
2408 ctx = mod->ctx;
2409 for (i = 0; i < ctx->models.used; i++) {
2410 if (!ctx->models.list[i]->implemented) {
2411 continue;
2412 }
2413
2414 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2415 /* we have some revision of the module implemented */
2416 return ctx->models.list[i];
2417 }
2418 }
2419
2420 /* we have no revision of the module implemented, return the module itself,
2421 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002422 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002423}
2424
2425const struct lys_module *
Radek Krejcie534c132016-11-23 13:32:31 +01002426lys_get_import_module_ns(const struct lys_module *module, const char *ns)
2427{
2428 int i;
2429
2430 assert(module && ns);
2431
Radek Krejcic789d692017-01-11 11:31:14 +01002432 if (module->type) {
2433 /* the module is actually submodule and to get the namespace, we need the main module */
2434 if (ly_strequal(((struct lys_submodule *)module)->belongsto->ns, ns, 0)) {
2435 return ((struct lys_submodule *)module)->belongsto;
2436 }
2437 } else {
2438 /* modul's own namespace */
2439 if (ly_strequal(module->ns, ns, 0)) {
2440 return module;
2441 }
Radek Krejcie534c132016-11-23 13:32:31 +01002442 }
2443
2444 /* imported modules */
2445 for (i = 0; i < module->imp_size; ++i) {
2446 if (ly_strequal(module->imp[i].module->ns, ns, 0)) {
2447 return module->imp[i].module;
2448 }
2449 }
2450
2451 return NULL;
2452}
2453
2454const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01002455lys_get_import_module(const struct lys_module *module, const char *prefix, int pref_len, const char *name, int name_len)
Michal Vasko8ce24d72015-10-21 11:27:26 +02002456{
Radek Krejcic071c542016-01-27 14:57:51 +01002457 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02002458 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002459 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002460
Michal Vaskoa7789a82016-02-11 15:42:55 +01002461 assert(!prefix || !name);
2462
Michal Vaskob6729c62015-10-21 12:09:47 +02002463 if (prefix && !pref_len) {
2464 pref_len = strlen(prefix);
2465 }
2466 if (name && !name_len) {
2467 name_len = strlen(name);
2468 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002469
Radek Krejcic4283442016-04-22 09:19:27 +02002470 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002471
2472 /* module own prefix, submodule own prefix, (sub)module own name */
2473 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2474 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002475 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002476 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002477 }
2478
Michal Vasko89563fc2016-07-28 16:19:35 +02002479 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002480 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002481 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2482 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002483 return module->imp[i].module;
2484 }
2485 }
2486
Michal Vasko89563fc2016-07-28 16:19:35 +02002487 /* module required by a foreign grouping, deviation, or submodule */
2488 if (name) {
2489 str = strndup(name, name_len);
2490 if (!str) {
2491 LOGMEM;
2492 return NULL;
2493 }
2494 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2495 free(str);
2496 return main_module;
2497 }
2498
Michal Vasko8ce24d72015-10-21 11:27:26 +02002499 return NULL;
2500}
2501
Michal Vasko13b15832015-08-19 11:04:48 +02002502/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002503static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002504module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002505{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002506 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002507 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002508 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002509
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002510 assert(module->ctx);
2511 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002512
Michal Vaskob746fff2016-02-11 11:37:50 +01002513 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002514 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002515 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002516 lydict_remove(ctx, module->imp[i].dsc);
2517 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002518 lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size, private_destructor);
Radek Krejci225376f2016-02-16 17:36:22 +01002519 }
Radek Krejcidce51452015-06-16 15:20:08 +02002520 free(module->imp);
2521
Radek Krejcic071c542016-01-27 14:57:51 +01002522 /* submodules don't have data tree, the data nodes
2523 * are placed in the main module altogether */
2524 if (!module->type) {
2525 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002526 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002527 }
Radek Krejci21181962015-06-30 14:11:00 +02002528 }
Radek Krejci5a065542015-05-22 15:02:07 +02002529
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002530 lydict_remove(ctx, module->dsc);
2531 lydict_remove(ctx, module->ref);
2532 lydict_remove(ctx, module->org);
2533 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002534 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002535
Radek Krejcieb00f512015-07-01 16:44:58 +02002536 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002537 for (i = 0; i < module->rev_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002538 lys_extension_instances_free(ctx, module->rev[i].ext, module->rev[i].ext_size, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002539 lydict_remove(ctx, module->rev[i].dsc);
2540 lydict_remove(ctx, module->rev[i].ref);
2541 }
2542 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002543
Radek Krejcieb00f512015-07-01 16:44:58 +02002544 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002545 for (i = 0; i < module->ident_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002546 lys_ident_free(ctx, &module->ident[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002547 }
2548 module->ident_size = 0;
2549 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002550
Radek Krejcieb00f512015-07-01 16:44:58 +02002551 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002552 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002553 lys_tpdf_free(ctx, &module->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002554 }
2555 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002556
Radek Krejcie534c132016-11-23 13:32:31 +01002557 /* extension instances */
Radek Krejci5138e9f2017-04-12 13:10:46 +02002558 lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002559
Radek Krejcieb00f512015-07-01 16:44:58 +02002560 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002561 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002562 lydict_remove(ctx, module->inc[i].dsc);
2563 lydict_remove(ctx, module->inc[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002564 lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002565 /* complete submodule free is done only from main module since
2566 * submodules propagate their includes to the main module */
2567 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002568 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002569 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002570 }
2571 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002572
Radek Krejcieb00f512015-07-01 16:44:58 +02002573 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002574 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002575 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002576 }
2577 free(module->augment);
2578
Radek Krejcieb00f512015-07-01 16:44:58 +02002579 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002580 for (i = 0; i < module->features_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002581 lys_feature_free(ctx, &module->features[i], private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002582 }
2583 free(module->features);
2584
Radek Krejcieb00f512015-07-01 16:44:58 +02002585 /* deviations */
2586 for (i = 0; i < module->deviation_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002587 lys_deviation_free(module, &module->deviation[i], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002588 }
2589 free(module->deviation);
2590
Radek Krejcie534c132016-11-23 13:32:31 +01002591 /* extensions */
2592 for (i = 0; i < module->extensions_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002593 lys_extension_free(ctx, &module->extensions[i], private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002594 }
2595 free(module->extensions);
2596
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002597 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002598 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002599}
2600
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002601void
Michal Vaskob746fff2016-02-11 11:37:50 +01002602lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002603{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002604 if (!submodule) {
2605 return;
2606 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002607
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002608 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002609 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002610
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002611 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002612
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002613 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002614}
2615
Radek Krejci3a5501d2016-07-18 22:03:34 +02002616static int
2617ingrouping(const struct lys_node *node)
2618{
2619 const struct lys_node *iter = node;
2620 assert(node);
2621
2622 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2623 if (!iter) {
2624 return 0;
2625 } else {
2626 return 1;
2627 }
2628}
2629
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002630/*
2631 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2632 */
2633static struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01002634lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002635 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002636{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002637 struct lys_node *retval = NULL, *iter, *p;
Michal Vaskofe31cc02017-03-10 15:52:31 +01002638 struct lys_module *tmp_mod;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002639 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002640 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002641 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002642 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002643 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002644
Michal Vaskoc07187d2015-08-13 15:20:57 +02002645 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002646 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002647 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002648 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002649 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002650 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002651 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002652 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002653 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002654 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002655 struct lys_node_anydata *any = NULL;
2656 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002657 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002658 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002659 struct lys_node_rpc_action *rpc = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02002660 struct lys_node_inout *io = NULL;
Radek Krejcic43151c2017-03-12 07:10:52 +01002661 struct lys_node_notif *ntf = NULL;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002662 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002663 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002664
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002665 /* we cannot just duplicate memory since the strings are stored in
2666 * dictionary and we need to update dictionary counters.
2667 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002668
Radek Krejci1d82ef62015-08-07 14:44:40 +02002669 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002670 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002671 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002672 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002673 break;
2674
Radek Krejci76512572015-08-04 09:47:08 +02002675 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002676 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002677 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002678 break;
2679
Radek Krejci76512572015-08-04 09:47:08 +02002680 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002681 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002682 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002683 break;
2684
Radek Krejci76512572015-08-04 09:47:08 +02002685 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002686 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002687 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002688 break;
2689
Radek Krejci76512572015-08-04 09:47:08 +02002690 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002691 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002692 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002693 break;
2694
Radek Krejci76512572015-08-04 09:47:08 +02002695 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002696 case LYS_ANYDATA:
2697 any = calloc(1, sizeof *any);
2698 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002699 break;
2700
Radek Krejci76512572015-08-04 09:47:08 +02002701 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002702 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002703 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002704 break;
2705
Radek Krejci76512572015-08-04 09:47:08 +02002706 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002707 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002708 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002709 break;
2710
Radek Krejci76512572015-08-04 09:47:08 +02002711 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002712 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002713 rpc = calloc(1, sizeof *rpc);
2714 retval = (struct lys_node *)rpc;
2715 break;
2716
Radek Krejci76512572015-08-04 09:47:08 +02002717 case LYS_INPUT:
2718 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002719 io = calloc(1, sizeof *io);
2720 retval = (struct lys_node *)io;
2721 break;
2722
Radek Krejci76512572015-08-04 09:47:08 +02002723 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002724 ntf = calloc(1, sizeof *ntf);
2725 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002726 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002727
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002728 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002729 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002730 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002731 }
Radek Krejcia8d111f2017-05-31 13:57:37 +02002732 LY_CHECK_ERR_RETURN(!retval, LOGMEM, NULL);
Michal Vasko253035f2015-12-17 16:58:13 +01002733
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002734 /*
2735 * duplicate generic part of the structure
2736 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002737 retval->name = lydict_insert(ctx, node->name, 0);
2738 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2739 retval->ref = lydict_insert(ctx, node->ref, 0);
2740 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002741
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002742 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002743 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002744
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002745 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002746
Radek Krejcif0bb3602017-01-25 17:05:08 +01002747 retval->ext_size = node->ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02002748 if (lys_ext_dup(module, node->ext, node->ext_size, retval, LYEXT_PAR_NODE, &retval->ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002749 goto error;
2750 }
2751
Radek Krejci06214042016-11-04 16:25:58 +01002752 if (node->iffeature_size) {
2753 retval->iffeature_size = node->iffeature_size;
2754 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002755 LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM, error);
Michal Vasko253035f2015-12-17 16:58:13 +01002756 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002757
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002758 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002759 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002760 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2761 if (size1) {
2762 /* there is something to duplicate */
2763
2764 /* duplicate compiled expression */
2765 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2766 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002767 LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM, error);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002768 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2769
2770 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002771 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002772 LY_CHECK_ERR_GOTO(!retval->iffeature[i].features, LOGMEM; free(retval->iffeature[i].expr), error);
2773
Radek Krejci9ff0a922016-07-14 13:08:05 +02002774 for (j = 0; (unsigned int)j < size2; j++) {
2775 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2776 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002777 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002778 /* feature is resolved in origin, so copy it
2779 * - duplication is used for instantiating groupings
2780 * and if-feature inside grouping is supposed to be
2781 * resolved inside the original grouping, so we want
2782 * to keep pointers to features from the grouping
2783 * context */
2784 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2785 } else if (rc == -1) {
2786 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002787 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002788 }
2789 }
Radek Krejcif0bb3602017-01-25 17:05:08 +01002790
2791 /* duplicate if-feature's extensions */
2792 retval->iffeature[i].ext_size = node->iffeature[i].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01002793 if (lys_ext_dup(module, node->iffeature[i].ext, node->iffeature[i].ext_size,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002794 &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002795 goto error;
2796 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002797 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002798
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002799 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002800 p = parent;
2801 do {
2802 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2803 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002804 if (iter) {
2805 flags = iter->flags & LYS_CONFIG_MASK;
2806 } else {
2807 /* default */
2808 flags = LYS_CONFIG_W;
2809 }
2810
2811 switch (finalize) {
2812 case 1:
2813 /* inherit config flags */
2814 if (retval->flags & LYS_CONFIG_SET) {
2815 /* skip nodes with an explicit config value */
2816 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2817 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +01002818 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002819 goto error;
2820 }
2821 break;
2822 }
2823
2824 if (retval->nodetype != LYS_USES) {
2825 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2826 }
2827 break;
2828 case 2:
2829 /* erase config flags */
2830 retval->flags &= ~LYS_CONFIG_MASK;
2831 retval->flags &= ~LYS_CONFIG_SET;
2832 break;
2833 }
2834
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002835 /* connect it to the parent */
2836 if (lys_node_addchild(parent, retval->module, retval)) {
2837 goto error;
2838 }
Radek Krejcidce51452015-06-16 15:20:08 +02002839
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002840 /* go recursively */
2841 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002842 LY_TREE_FOR(node->child, iter) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002843 if (iter->nodetype & LYS_GROUPING) {
2844 /* do not instantiate groupings */
2845 continue;
2846 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01002847 if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002848 goto error;
2849 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002850 }
2851 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002852
2853 if (finalize == 1) {
2854 /* check that configuration lists have keys
2855 * - we really want to check keys_size in original node, because the keys are
2856 * not yet resolved here, it is done below in nodetype specific part */
2857 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2858 && !((struct lys_node_list *)node)->keys_size) {
2859 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2860 goto error;
2861 }
2862 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002863 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002864 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002865 }
2866
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002867 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002868 * duplicate specific part of the structure
2869 */
2870 switch (node->nodetype) {
2871 case LYS_CONTAINER:
2872 if (cont_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002873 cont->when = lys_when_dup(module, cont_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002874 LY_CHECK_GOTO(!cont->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002875 }
2876 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002877
Radek Krejcia8d111f2017-05-31 13:57:37 +02002878 if (cont_orig->must) {
2879 cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres);
2880 LY_CHECK_GOTO(!cont->must, error);
2881 cont->must_size = cont_orig->must_size;
2882 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002883
Radek Krejcif0bb3602017-01-25 17:05:08 +01002884 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
2885
2886 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002887 case LYS_CHOICE:
2888 if (choice_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002889 choice->when = lys_when_dup(module, choice_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002890 LY_CHECK_GOTO(!choice->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002891 }
2892
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002893 if (!shallow) {
2894 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002895 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2896 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2897 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002898 if (rc) {
2899 if (rc == EXIT_FAILURE) {
2900 LOGINT;
2901 }
2902 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002903 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002904 } else {
2905 /* useless to check return value, we don't know whether
2906 * there really wasn't any default defined or it just hasn't
2907 * been resolved, we just hope for the best :)
2908 */
2909 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002910 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002911 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002912 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002913 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002914 break;
2915
2916 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002917 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002918 goto error;
2919 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002920 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2921
2922 if (leaf_orig->dflt) {
2923 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Michal Vaskofe31cc02017-03-10 15:52:31 +01002924 if (!ingrouping(retval) || (leaf->type.base != LY_TYPE_LEAFREF)) {
Radek Krejci968f6942017-03-25 15:55:01 -05002925 /* problem is when it is an identityref referencing an identity from a module
2926 * and we are using the grouping in a different module */
Radek Krejcid06f5a42017-03-25 16:03:42 -05002927 if (leaf->type.base == LY_TYPE_IDENT) {
Michal Vaskofe31cc02017-03-10 15:52:31 +01002928 tmp_mod = leaf_orig->module;
2929 } else {
2930 tmp_mod = module;
2931 }
2932 if (unres_schema_add_node(tmp_mod, unres, &leaf->type, UNRES_TYPE_DFLT,
2933 (struct lys_node *)(&leaf->dflt)) == -1) {
2934 goto error;
2935 }
Michal Vasko49168a22015-08-17 16:35:41 +02002936 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002937 }
2938
Radek Krejcia8d111f2017-05-31 13:57:37 +02002939 if (leaf_orig->must) {
2940 leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres);
2941 LY_CHECK_GOTO(!leaf->must, error);
2942 leaf->must_size = leaf_orig->must_size;
2943 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002944
2945 if (leaf_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002946 leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002947 LY_CHECK_GOTO(!leaf->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002948 }
2949 break;
2950
2951 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002952 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002953 goto error;
2954 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002955 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2956
2957 llist->min = llist_orig->min;
2958 llist->max = llist_orig->max;
2959
Radek Krejcia8d111f2017-05-31 13:57:37 +02002960 if (llist_orig->must) {
2961 llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres);
2962 LY_CHECK_GOTO(!llist->must, error);
2963 llist->must_size = llist_orig->must_size;
2964 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002965
Radek Krejcia8d111f2017-05-31 13:57:37 +02002966 if (llist_orig->dflt) {
2967 llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt);
2968 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM, error);
2969 llist->dflt_size = llist_orig->dflt_size;
2970 }
2971
Radek Krejci51673202016-11-01 17:00:32 +01002972 for (i = 0; i < llist->dflt_size; i++) {
2973 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
Michal Vaskofe31cc02017-03-10 15:52:31 +01002974 if (!ingrouping(retval) || (llist->type.base != LY_TYPE_LEAFREF)) {
2975 if ((llist->type.base == LY_TYPE_IDENT) && !strchr(llist->dflt[i], ':') && (module != llist_orig->module)) {
2976 tmp_mod = llist_orig->module;
2977 } else {
2978 tmp_mod = module;
2979 }
2980 if (unres_schema_add_node(tmp_mod, unres, &llist->type, UNRES_TYPE_DFLT,
2981 (struct lys_node *)(&llist->dflt[i])) == -1) {
2982 goto error;
2983 }
Radek Krejci51673202016-11-01 17:00:32 +01002984 }
2985 }
2986
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002987 if (llist_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002988 llist->when = lys_when_dup(module, llist_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002989 }
2990 break;
2991
2992 case LYS_LIST:
2993 list->min = list_orig->min;
2994 list->max = list_orig->max;
2995
Radek Krejcia8d111f2017-05-31 13:57:37 +02002996 if (list_orig->must) {
2997 list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres);
2998 LY_CHECK_GOTO(!list->must, error);
2999 list->must_size = list_orig->must_size;
3000 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003001
Radek Krejcif0bb3602017-01-25 17:05:08 +01003002 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Michal Vasko38d01f72015-06-15 09:41:06 +02003003
Radek Krejcia8d111f2017-05-31 13:57:37 +02003004 if (list_orig->keys_size) {
3005 list->keys = calloc(list_orig->keys_size, sizeof *list->keys);
3006 LY_CHECK_ERR_GOTO(!list->keys, LOGMEM, error);
Radek Krejci5c08a992016-11-02 13:30:04 +01003007 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003008 list->keys_size = list_orig->keys_size;
Michal Vasko0ea41032015-06-16 08:53:55 +02003009
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003010 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01003011 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
3012 * in another grouping */
3013 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
3014 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
3015 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02003016 }
Michal Vasko0ea41032015-06-16 08:53:55 +02003017 } else {
Radek Krejcia8d111f2017-05-31 13:57:37 +02003018 memcpy(list->keys, list_orig->keys, list_orig->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02003019 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003020 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003021
Radek Krejcia8d111f2017-05-31 13:57:37 +02003022 if (list_orig->unique) {
3023 list->unique = malloc(list_orig->unique_size * sizeof *list->unique);
3024 LY_CHECK_ERR_GOTO(!list->unique, LOGMEM, error);
3025 list->unique_size = list_orig->unique_size;
Michal Vasko253035f2015-12-17 16:58:13 +01003026 }
Radek Krejci581ce772015-11-10 17:22:40 +01003027 for (i = 0; i < list->unique_size; ++i) {
Radek Krejcia8d111f2017-05-31 13:57:37 +02003028 list->unique[i].expr = malloc(list_orig->unique[i].expr_size * sizeof *list->unique[i].expr);
3029 LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM, error);
Radek Krejci581ce772015-11-10 17:22:40 +01003030 list->unique[i].expr_size = list_orig->unique[i].expr_size;
Radek Krejci581ce772015-11-10 17:22:40 +01003031 for (j = 0; j < list->unique[i].expr_size; j++) {
3032 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
3033
3034 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02003035 unique_info = malloc(sizeof *unique_info);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003036 LY_CHECK_ERR_GOTO(!unique_info, LOGMEM, error);
Radek Krejcid09d1a52016-08-11 14:05:45 +02003037 unique_info->list = (struct lys_node *)list;
3038 unique_info->expr = list->unique[i].expr[j];
3039 unique_info->trg_type = &list->unique[i].trg_type;
3040 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003041 }
3042 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003043
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003044 if (list_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003045 list->when = lys_when_dup(module, list_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003046 LY_CHECK_GOTO(!list->when, error);
Radek Krejciefaeba32015-05-27 14:30:57 +02003047 }
Radek Krejcidce51452015-06-16 15:20:08 +02003048 break;
3049
3050 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02003051 case LYS_ANYDATA:
Radek Krejcia8d111f2017-05-31 13:57:37 +02003052 if (any_orig->must) {
3053 any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres);
3054 LY_CHECK_GOTO(!any->must, error);
3055 any->must_size = any_orig->must_size;
3056 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003057
Radek Krejcibf2abff2016-08-23 15:51:52 +02003058 if (any_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003059 any->when = lys_when_dup(module, any_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003060 LY_CHECK_GOTO(!any->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003061 }
3062 break;
3063
3064 case LYS_USES:
3065 uses->grp = uses_orig->grp;
3066
3067 if (uses_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003068 uses->when = lys_when_dup(module, uses_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003069 LY_CHECK_GOTO(!uses->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003070 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01003071 /* it is not needed to duplicate refine, nor augment. They are already applied to the uses children */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003072 break;
3073
3074 case LYS_CASE:
3075 if (cs_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003076 cs->when = lys_when_dup(module, cs_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003077 LY_CHECK_GOTO(!cs->when, error);
Radek Krejcidce51452015-06-16 15:20:08 +02003078 }
3079 break;
3080
Radek Krejci96935402016-11-04 16:27:28 +01003081 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02003082 case LYS_RPC:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003083 case LYS_INPUT:
3084 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02003085 case LYS_NOTIF:
Radek Krejcif0bb3602017-01-25 17:05:08 +01003086 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003087 break;
3088
3089 default:
3090 /* LY_NODE_AUGMENT */
3091 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02003092 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003093 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003094
3095 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02003096
3097error:
3098
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003099 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02003100 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003101}
3102
Radek Krejcib3142312016-11-09 11:04:12 +01003103int
3104lys_has_xpath(const struct lys_node *node)
3105{
3106 assert(node);
3107
3108 switch (node->nodetype) {
3109 case LYS_AUGMENT:
3110 if (((struct lys_node_augment *)node)->when) {
3111 return 1;
3112 }
3113 break;
3114 case LYS_CASE:
3115 if (((struct lys_node_case *)node)->when) {
3116 return 1;
3117 }
3118 break;
3119 case LYS_CHOICE:
3120 if (((struct lys_node_choice *)node)->when) {
3121 return 1;
3122 }
3123 break;
3124 case LYS_ANYDATA:
3125 if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) {
3126 return 1;
3127 }
3128 break;
3129 case LYS_LEAF:
3130 if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) {
3131 return 1;
3132 }
3133 break;
3134 case LYS_LEAFLIST:
3135 if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) {
3136 return 1;
3137 }
3138 break;
3139 case LYS_LIST:
3140 if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) {
3141 return 1;
3142 }
3143 break;
3144 case LYS_CONTAINER:
3145 if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) {
3146 return 1;
3147 }
3148 break;
3149 case LYS_INPUT:
3150 case LYS_OUTPUT:
3151 if (((struct lys_node_inout *)node)->must_size) {
3152 return 1;
3153 }
3154 break;
3155 case LYS_NOTIF:
3156 if (((struct lys_node_notif *)node)->must_size) {
3157 return 1;
3158 }
3159 break;
3160 case LYS_USES:
3161 if (((struct lys_node_uses *)node)->when) {
3162 return 1;
3163 }
3164 break;
3165 default:
3166 /* does not have XPath */
3167 break;
3168 }
3169
3170 return 0;
3171}
3172
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003173struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01003174lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003175 struct unres_schema *unres, int shallow)
3176{
3177 struct lys_node *p = NULL;
Radek Krejcib3142312016-11-09 11:04:12 +01003178 int finalize = 0;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003179 struct lys_node *result, *iter, *next;
3180
3181 if (!shallow) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01003182 /* 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 +02003183 for (p = parent;
3184 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
3185 p = lys_parent(p));
3186 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
3187 }
3188
Radek Krejci6ff885d2017-01-03 14:06:22 +01003189 result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize);
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003190 if (finalize) {
3191 /* check xpath expressions in the instantiated tree */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003192 for (iter = next = result; iter; iter = next) {
Radek Krejcib3142312016-11-09 11:04:12 +01003193 if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
Radek Krejci3c48d042016-11-07 14:42:36 +01003194 /* invalid xpath */
3195 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003196 }
3197
3198 /* select next item */
3199 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
3200 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
3201 next = NULL;
3202 } else {
3203 next = iter->child;
3204 }
3205 if (!next) {
3206 /* no children, try siblings */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003207 if (iter == result) {
3208 /* we are done, no next element to process */
3209 break;
3210 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003211 next = iter->next;
3212 }
3213 while (!next) {
3214 /* parent is already processed, go to its sibling */
3215 iter = lys_parent(iter);
Radek Krejci7212e0a2017-03-08 15:58:22 +01003216 if (lys_parent(iter) == lys_parent(result)) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003217 /* we are done, no next element to process */
3218 break;
3219 }
3220 next = iter->next;
3221 }
3222 }
3223 }
3224
3225 return result;
3226}
3227
Michal Vasko13b15832015-08-19 11:04:48 +02003228void
Michal Vaskoff006c12016-02-17 11:15:19 +01003229lys_node_switch(struct lys_node *dst, struct lys_node *src)
3230{
3231 struct lys_node *child;
3232
Michal Vaskob42b6972016-06-06 14:21:30 +02003233 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01003234
3235 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02003236 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01003237 dst->prev->next = src;
3238 }
3239
3240 /* sibling prev */
3241 if (dst->next) {
3242 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02003243 } else {
3244 for (child = dst->prev; child->prev->next; child = child->prev);
3245 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003246 }
3247
3248 /* next */
3249 src->next = dst->next;
3250 dst->next = NULL;
3251
3252 /* prev */
3253 if (dst->prev != dst) {
3254 src->prev = dst->prev;
3255 }
3256 dst->prev = dst;
3257
3258 /* parent child */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003259 if (dst->parent) {
3260 if (dst->parent->child == dst) {
3261 dst->parent->child = src;
3262 }
Radek Krejci115fa882017-03-01 16:15:07 +01003263 } else if (lys_main_module(dst->module)->data == dst) {
3264 lys_main_module(dst->module)->data = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003265 }
3266
3267 /* parent */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003268 src->parent = dst->parent; dst->parent = NULL;
3269
Michal Vaskoff006c12016-02-17 11:15:19 +01003270
3271 /* child parent */
3272 LY_TREE_FOR(dst->child, child) {
3273 if (child->parent == dst) {
3274 child->parent = src;
3275 }
3276 }
3277
3278 /* child */
3279 src->child = dst->child;
3280 dst->child = NULL;
Radek Krejcif0bb3602017-01-25 17:05:08 +01003281
3282 /* node-specific data */
3283 switch (dst->nodetype) {
3284 case LYS_CONTAINER:
3285 ((struct lys_node_container *)src)->tpdf_size = ((struct lys_node_container *)dst)->tpdf_size;
3286 ((struct lys_node_container *)src)->tpdf = ((struct lys_node_container *)dst)->tpdf;
3287 ((struct lys_node_container *)dst)->tpdf_size = 0;
3288 ((struct lys_node_container *)dst)->tpdf = NULL;
3289 break;
3290 case LYS_LIST:
3291 ((struct lys_node_list *)src)->tpdf_size = ((struct lys_node_list *)dst)->tpdf_size;
3292 ((struct lys_node_list *)src)->tpdf = ((struct lys_node_list *)dst)->tpdf;
3293 ((struct lys_node_list *)dst)->tpdf_size = 0;
3294 ((struct lys_node_list *)dst)->tpdf = NULL;
3295 break;
3296 case LYS_RPC:
3297 case LYS_ACTION:
3298 ((struct lys_node_rpc_action *)src)->tpdf_size = ((struct lys_node_rpc_action *)dst)->tpdf_size;
3299 ((struct lys_node_rpc_action *)src)->tpdf = ((struct lys_node_rpc_action *)dst)->tpdf;
3300 ((struct lys_node_rpc_action *)dst)->tpdf_size = 0;
3301 ((struct lys_node_rpc_action *)dst)->tpdf = NULL;
3302 break;
3303 case LYS_NOTIF:
3304 ((struct lys_node_notif *)src)->tpdf_size = ((struct lys_node_notif *)dst)->tpdf_size;
3305 ((struct lys_node_notif *)src)->tpdf = ((struct lys_node_notif *)dst)->tpdf;
3306 ((struct lys_node_notif *)dst)->tpdf_size = 0;
3307 ((struct lys_node_notif *)dst)->tpdf = NULL;
3308 break;
3309 case LYS_INPUT:
3310 case LYS_OUTPUT:
3311 ((struct lys_node_inout *)src)->tpdf_size = ((struct lys_node_inout *)dst)->tpdf_size;
3312 ((struct lys_node_inout *)src)->tpdf = ((struct lys_node_inout *)dst)->tpdf;
3313 ((struct lys_node_inout *)dst)->tpdf_size = 0;
3314 ((struct lys_node_inout *)dst)->tpdf = NULL;
3315 break;
3316 default:
3317 /* nothing special */
3318 break;
3319 }
3320
Michal Vaskoff006c12016-02-17 11:15:19 +01003321}
3322
3323void
Michal Vasko627975a2016-02-11 11:39:03 +01003324lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int remove_from_ctx)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003325{
3326 struct ly_ctx *ctx;
3327 int i;
3328
3329 if (!module) {
3330 return;
3331 }
3332
3333 /* remove schema from the context */
3334 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01003335 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02003336 for (i = 0; i < ctx->models.used; i++) {
3337 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01003338 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003339 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01003340 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 +02003341 ctx->models.list[ctx->models.used] = NULL;
3342 /* we are done */
3343 break;
3344 }
3345 }
3346 }
3347
3348 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01003349 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003350
3351 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01003352 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003353
3354 free(module);
3355}
Radek Krejci7e97c352015-06-19 16:26:34 +02003356
Radek Krejci9de2c042016-10-19 16:53:06 +02003357static void
3358lys_features_disable_recursive(struct lys_feature *f)
3359{
3360 unsigned int i;
3361 struct lys_feature *depf;
3362
3363 /* disable the feature */
3364 f->flags &= ~LYS_FENABLED;
3365
3366 /* by disabling feature we have to disable also all features that depends on this feature */
3367 if (f->depfeatures) {
3368 for (i = 0; i < f->depfeatures->number; i++) {
3369 depf = (struct lys_feature *)f->depfeatures->set.g[i];
3370 if (depf->flags & LYS_FENABLED) {
3371 lys_features_disable_recursive(depf);
3372 }
3373 }
3374 }
3375}
3376
3377
Radek Krejci7e97c352015-06-19 16:26:34 +02003378/*
3379 * op: 1 - enable, 0 - disable
3380 */
3381static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003382lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02003383{
3384 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003385 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02003386 int progress, faili, failj, failk;
3387
3388 uint8_t fsize;
3389 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02003390
3391 if (!module || !name || !strlen(name)) {
3392 return EXIT_FAILURE;
3393 }
3394
3395 if (!strcmp(name, "*")) {
3396 /* enable all */
3397 all = 1;
3398 }
3399
Radek Krejcia889c1f2016-10-19 15:50:11 +02003400 progress = failk = 1;
3401 while (progress && failk) {
3402 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
3403 if (i == -1) {
3404 fsize = module->features_size;
3405 f = module->features;
3406 } else {
3407 fsize = module->inc[i].submodule->features_size;
3408 f = module->inc[i].submodule->features;
3409 }
3410
3411 for (j = 0; j < fsize; j++) {
3412 if (all || !strcmp(f[j].name, name)) {
Michal Vasko34c3b552016-11-21 09:07:43 +01003413 if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) {
3414 if (all) {
3415 /* skip already set features */
3416 continue;
3417 } else {
3418 /* feature already set correctly */
3419 return EXIT_SUCCESS;
3420 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003421 }
3422
3423 if (op) {
3424 /* check referenced features if they are enabled */
3425 for (k = 0; k < f[j].iffeature_size; k++) {
3426 if (!resolve_iffeature(&f[j].iffeature[k])) {
3427 if (all) {
3428 faili = i;
3429 failj = j;
3430 failk = k + 1;
3431 break;
3432 } else {
3433 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
3434 f[j].name, k + 1);
3435 return EXIT_FAILURE;
3436 }
3437 }
3438 }
3439
3440 if (k == f[j].iffeature_size) {
3441 /* the last check passed, do the change */
3442 f[j].flags |= LYS_FENABLED;
3443 progress++;
3444 }
3445 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02003446 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02003447 progress++;
3448 }
3449 if (!all) {
3450 /* stop in case changing a single feature */
3451 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003452 }
3453 }
Radek Krejci7e97c352015-06-19 16:26:34 +02003454 }
3455 }
3456 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003457 if (failk) {
3458 /* print info about the last failing feature */
3459 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
3460 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
3461 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02003462 }
3463
3464 if (all) {
3465 return EXIT_SUCCESS;
3466 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02003467 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02003468 return EXIT_FAILURE;
3469 }
3470}
3471
3472API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003473lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003474{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003475 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02003476}
3477
3478API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003479lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003480{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003481 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003482}
3483
3484API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003485lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003486{
3487 int i, j;
3488
3489 if (!module || !feature) {
3490 return -1;
3491 }
3492
3493 /* search for the specified feature */
3494 /* module itself */
3495 for (i = 0; i < module->features_size; i++) {
3496 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003497 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003498 return 1;
3499 } else {
3500 return 0;
3501 }
3502 }
3503 }
3504
3505 /* submodules */
3506 for (j = 0; j < module->inc_size; j++) {
3507 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
3508 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003509 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003510 return 1;
3511 } else {
3512 return 0;
3513 }
3514 }
3515 }
3516 }
3517
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003518 /* feature definition not found */
3519 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02003520}
Michal Vasko2367e7c2015-07-07 11:33:44 +02003521
Radek Krejci96a10da2015-07-30 11:00:14 +02003522API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01003523lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02003524{
Radek Krejci96a10da2015-07-30 11:00:14 +02003525 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003526 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003527 unsigned int count;
3528
3529 if (!module) {
3530 return NULL;
3531 }
3532
3533 count = module->features_size;
3534 for (i = 0; i < module->inc_size; i++) {
3535 count += module->inc[i].submodule->features_size;
3536 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003537 result = malloc((count + 1) * sizeof *result);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003538 LY_CHECK_ERR_RETURN(!result, LOGMEM, NULL);
3539
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003540 if (states) {
3541 *states = malloc((count + 1) * sizeof **states);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003542 LY_CHECK_ERR_RETURN(!(*states), LOGMEM; free(result), NULL);
Michal Vasko2367e7c2015-07-07 11:33:44 +02003543 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003544 count = 0;
3545
3546 /* module itself */
3547 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003548 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003549 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003550 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003551 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003552 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003553 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003554 }
3555 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003556 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003557 }
3558
3559 /* submodules */
3560 for (j = 0; j < module->inc_size; j++) {
3561 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003562 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003563 if (states) {
3564 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3565 (*states)[count] = 1;
3566 } else {
3567 (*states)[count] = 0;
3568 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003569 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003570 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003571 }
3572 }
3573
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003574 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003575 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003576
3577 return result;
3578}
Michal Vaskobaefb032015-09-24 14:52:10 +02003579
Radek Krejci6910a032016-04-13 10:06:21 +02003580API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003581lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003582{
Michal Vaskof53187d2017-01-13 13:23:14 +01003583 if (!node) {
3584 return NULL;
3585 }
3586
Radek Krejcic071c542016-01-27 14:57:51 +01003587 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3588}
3589
Radek Krejci6910a032016-04-13 10:06:21 +02003590API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003591lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003592{
Michal Vaskof53187d2017-01-13 13:23:14 +01003593 if (!module) {
3594 return NULL;
3595 }
3596
Michal Vasko320e8532016-02-15 13:11:57 +01003597 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3598}
3599
Michal Vaskobaefb032015-09-24 14:52:10 +02003600API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003601lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003602{
Radek Krejcif95b6292017-02-13 15:57:37 +01003603 struct lys_node *parent;
3604
3605 if (!node) {
Michal Vaskobaefb032015-09-24 14:52:10 +02003606 return NULL;
3607 }
3608
Radek Krejcif95b6292017-02-13 15:57:37 +01003609 if (node->nodetype == LYS_EXT) {
3610 if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) {
3611 return NULL;
3612 }
3613 parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent;
3614 } else if (!node->parent) {
3615 return NULL;
3616 } else {
3617 parent = node->parent;
Michal Vaskobaefb032015-09-24 14:52:10 +02003618 }
3619
Radek Krejcif95b6292017-02-13 15:57:37 +01003620 if (parent->nodetype == LYS_AUGMENT) {
3621 return ((struct lys_node_augment *)parent)->target;
3622 } else {
3623 return parent;
3624 }
3625}
3626
3627struct lys_node **
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003628lys_child(const struct lys_node *node, LYS_NODE nodetype)
Radek Krejcif95b6292017-02-13 15:57:37 +01003629{
3630 void *pp;
3631 assert(node);
3632
3633 if (node->nodetype == LYS_EXT) {
3634 pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL);
3635 if (!pp) {
3636 return NULL;
3637 }
3638 return (struct lys_node **)pp;
3639 } else {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003640 return (struct lys_node **)&node->child;
Radek Krejcif95b6292017-02-13 15:57:37 +01003641 }
Michal Vaskobaefb032015-09-24 14:52:10 +02003642}
Michal Vasko1b229152016-01-13 11:28:38 +01003643
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003644API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003645lys_set_private(const struct lys_node *node, void *priv)
3646{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003647 void *prev;
3648
Michal Vasko1b229152016-01-13 11:28:38 +01003649 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003650 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
3651 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003652 }
3653
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003654 prev = node->priv;
3655 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003656
3657 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003658}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003659
Michal Vasko01c6fd22016-05-20 11:43:05 +02003660int
3661lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3662{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003663 struct lys_node_leaf *iter = leafref_target;
3664
Michal Vasko48a573d2016-07-01 11:46:02 +02003665 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003666 LOGINT;
3667 return -1;
3668 }
3669
Pavol Vican93175152016-08-30 15:34:44 +02003670 /* check for config flag */
Radek Krejcic688ca02017-03-20 12:54:39 +01003671 if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 &&
3672 (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
Pavol Vican93175152016-08-30 15:34:44 +02003673 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
Radek Krejcid831dd42017-03-16 12:59:30 +01003674 "The leafref %s is config but refers to a non-config %s.",
Pavol Vican93175152016-08-30 15:34:44 +02003675 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3676 return -1;
3677 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003678 /* check for cycles */
3679 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3680 if ((void *)iter == (void *)leafref) {
3681 /* cycle detected */
3682 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3683 return -1;
3684 }
3685 iter = iter->type.info.lref.target;
3686 }
3687
3688 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003689 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003690 if (!leafref_target->backlinks) {
3691 leafref_target->backlinks = (void*)ly_set_new();
3692 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003693 LOGMEM;
3694 return -1;
3695 }
3696 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003697 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003698
3699 return 0;
3700}
3701
Michal Vasko8548e082016-07-22 12:00:18 +02003702/* not needed currently */
3703#if 0
3704
Michal Vasko5b3492c2016-07-20 09:37:40 +02003705static const char *
3706lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3707{
3708 struct lys_module *prev_mod;
3709 uint32_t str_len, mod_len, buf_idx;
3710
Radek Krejcibf2abff2016-08-23 15:51:52 +02003711 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003712 LOGINT;
3713 return NULL;
3714 }
3715
3716 buf_idx = buf_len - 1;
3717 buf[buf_idx] = '\0';
3718
3719 while (node) {
3720 if (lys_parent(node)) {
3721 prev_mod = lys_node_module(lys_parent(node));
3722 } else {
3723 prev_mod = NULL;
3724 }
3725
Radek Krejcibf2abff2016-08-23 15:51:52 +02003726 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003727 str_len = strlen(node->name);
3728
3729 if (prev_mod != node->module) {
3730 mod_len = strlen(node->module->name);
3731 } else {
3732 mod_len = 0;
3733 }
3734
3735 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3736 LOGINT;
3737 return NULL;
3738 }
3739
3740 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3741
3742 buf[buf_idx] = '/';
3743 if (mod_len) {
3744 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3745 buf[buf_idx + 1 + mod_len] = ':';
3746 }
3747 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3748 }
3749
3750 node = lys_parent(node);
3751 }
3752
3753 return buf + buf_idx;
3754}
3755
Michal Vasko8548e082016-07-22 12:00:18 +02003756#endif
3757
3758API struct ly_set *
Michal Vasko2611e192017-01-23 10:33:21 +01003759lys_find_xpath(struct ly_ctx *ctx, const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003760{
3761 struct lyxp_set set;
3762 struct ly_set *ret_set;
3763 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003764 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003765
Michal Vasko2611e192017-01-23 10:33:21 +01003766 if ((!ctx && !node) || !expr) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003767 ly_errno = LY_EINVAL;
3768 return NULL;
3769 }
3770
Michal Vasko2611e192017-01-23 10:33:21 +01003771 if (!node) {
3772 node = ly_ctx_get_node(ctx, NULL, "/ietf-yang-library:modules-state");
3773 if (!node) {
3774 ly_errno = LY_EINT;
3775 return NULL;
3776 }
3777 }
3778
Michal Vasko46a4bf92016-09-08 08:23:49 +02003779 memset(&set, 0, sizeof set);
3780
Michal Vaskodb1da032016-09-08 10:07:38 +02003781 opts = LYXP_SNODE;
3782 if (options & LYS_FIND_OUTPUT) {
3783 opts |= LYXP_SNODE_OUTPUT;
3784 }
3785
Michal Vaskodb1da032016-09-08 10:07:38 +02003786 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vaskoc90f3ff2017-01-23 13:51:58 +01003787 /* just find a relevant node to put in path, if it fails, use the original one */
3788 for (i = 0; i < set.used; ++i) {
3789 if (set.val.snodes[i].in_ctx == 1) {
3790 node = set.val.snodes[i].snode;
3791 break;
3792 }
3793 }
Michal Vasko46a4bf92016-09-08 08:23:49 +02003794 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003795 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko46a4bf92016-09-08 08:23:49 +02003796 return NULL;
3797 }
3798
3799 ret_set = ly_set_new();
3800
3801 for (i = 0; i < set.used; ++i) {
3802 if (!set.val.snodes[i].in_ctx) {
3803 continue;
3804 }
3805 assert(set.val.snodes[i].in_ctx == 1);
3806
3807 switch (set.val.snodes[i].type) {
3808 case LYXP_NODE_ELEM:
3809 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3810 ly_set_free(ret_set);
3811 free(set.val.snodes);
3812 return NULL;
3813 }
3814 break;
3815 default:
3816 /* ignore roots, text and attr should not ever appear */
3817 break;
3818 }
3819 }
3820
3821 free(set.val.snodes);
3822 return ret_set;
3823}
3824
3825API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003826lys_xpath_atomize(const struct lys_node *cur_snode, enum lyxp_node_type cur_snode_type, const char *expr, int options)
Michal Vasko5b3492c2016-07-20 09:37:40 +02003827{
Michal Vasko508a50d2016-09-07 14:50:33 +02003828 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003829 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003830 uint32_t i;
3831
Michal Vaskob94a5e42016-09-08 14:01:56 +02003832 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003833 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003834 }
3835
Michal Vaskob94a5e42016-09-08 14:01:56 +02003836 /* adjust the root */
3837 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3838 do {
3839 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3840 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3841 }
3842
Michal Vasko508a50d2016-09-07 14:50:33 +02003843 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003844
3845 if (options & LYXP_MUST) {
3846 options &= ~LYXP_MUST;
3847 options |= LYXP_SNODE_MUST;
3848 } else if (options & LYXP_WHEN) {
3849 options &= ~LYXP_WHEN;
3850 options |= LYXP_SNODE_WHEN;
3851 } else {
3852 options |= LYXP_SNODE;
3853 }
3854
Michal Vasko508a50d2016-09-07 14:50:33 +02003855 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3856 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003857 LOGVAL(LYE_SPEC, LY_VLOG_LYS, cur_snode, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003858 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003859 }
3860
Michal Vasko8548e082016-07-22 12:00:18 +02003861 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003862
Michal Vasko508a50d2016-09-07 14:50:33 +02003863 for (i = 0; i < set.used; ++i) {
3864 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003865 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003866 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003867 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003868 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003869 return NULL;
3870 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003871 break;
3872 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003873 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003874 break;
3875 }
3876 }
3877
Michal Vasko508a50d2016-09-07 14:50:33 +02003878 free(set.val.snodes);
3879 return ret_set;
3880}
3881
3882API struct ly_set *
3883lys_node_xpath_atomize(const struct lys_node *node, int options)
3884{
3885 const struct lys_node *next, *elem, *parent, *tmp;
3886 struct lyxp_set set;
3887 struct ly_set *ret_set;
3888 uint16_t i;
3889
3890 if (!node) {
3891 return NULL;
3892 }
3893
3894 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3895 if (!parent) {
3896 /* not in input, output, or notification */
3897 return NULL;
3898 }
3899
3900 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003901 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003902 return NULL;
3903 }
3904
3905 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003906 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003907 /* elem has no dependencies from other subtrees and local nodes get discarded */
3908 goto next_iter;
3909 }
3910
Michal Vasko769f8032017-01-24 13:11:55 +01003911 if (lyxp_node_atomize(elem, &set, 0)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003912 ly_set_free(ret_set);
3913 free(set.val.snodes);
3914 return NULL;
3915 }
3916
3917 for (i = 0; i < set.used; ++i) {
3918 switch (set.val.snodes[i].type) {
3919 case LYXP_NODE_ELEM:
3920 if (options & LYXP_NO_LOCAL) {
3921 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3922 if (tmp) {
3923 /* in local subtree, discard */
3924 break;
3925 }
3926 }
3927 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3928 ly_set_free(ret_set);
3929 free(set.val.snodes);
3930 return NULL;
3931 }
3932 break;
3933 default:
3934 /* ignore roots, text and attr should not ever appear */
3935 break;
3936 }
3937 }
3938
3939 free(set.val.snodes);
3940 if (!(options & LYXP_RECURSIVE)) {
3941 break;
3942 }
3943next_iter:
3944 LY_TREE_DFS_END(node, next, elem);
3945 }
3946
Michal Vasko8548e082016-07-22 12:00:18 +02003947 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003948}
3949
Michal Vasko44ab1462017-05-18 13:18:36 +02003950/* logs */
3951int
3952apply_aug(struct lys_node_augment *augment, struct unres_schema *unres)
Radek Krejci0ec51da2016-12-14 16:42:03 +01003953{
Michal Vasko44ab1462017-05-18 13:18:36 +02003954 struct lys_node *child, *parent;
3955 int clear_config;
3956 unsigned int u;
3957 struct lys_ext_instance *ext;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003958
3959 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
3960
Michal Vaskobb520442017-05-23 10:55:18 +02003961 /* check that all the modules are implemented */
3962 for (parent = augment->target; parent; parent = lys_parent(parent)) {
3963 if (!lys_node_module(parent)->implemented) {
3964 if (lys_set_implemented(lys_node_module(parent))) {
3965 LOGERR(ly_errno, "Making the augment target module \"%s\" implemented failed.", lys_node_module(parent)->name);
3966 return -1;
3967 }
3968 LOGVRB("Augment target module \"%s\" now implemented.", lys_node_module(parent)->name);
3969 }
3970 }
3971
Radek Krejci0ec51da2016-12-14 16:42:03 +01003972 /* reconnect augmenting data into the target - add them to the target child list */
3973 if (augment->target->child) {
Michal Vasko44ab1462017-05-18 13:18:36 +02003974 child = augment->target->child->prev;
3975 child->next = augment->child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003976 augment->target->child->prev = augment->child->prev;
Michal Vasko44ab1462017-05-18 13:18:36 +02003977 augment->child->prev = child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003978 } else {
3979 augment->target->child = augment->child;
3980 }
3981
Michal Vasko44ab1462017-05-18 13:18:36 +02003982 /* inherit config information from actual parent */
3983 for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent));
3984 clear_config = (parent) ? 1 : 0;
3985 LY_TREE_FOR(augment->child, child) {
3986 if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) {
3987 return -1;
3988 }
3989 }
3990
3991 /* inherit extensions if any */
3992 for (u = 0; u < augment->target->ext_size; u++) {
3993 ext = augment->target->ext[u]; /* shortcut */
3994 if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) {
3995 if (unres_schema_add_node(lys_main_module(augment->module), unres, &ext, UNRES_EXT_FINALIZE, NULL) == -1) {
3996 /* something really bad happend since the extension finalization is not actually
3997 * being resolved while adding into unres, so something more serious with the unres
3998 * list itself must happened */
3999 return -1;
4000 }
4001 }
4002 }
4003
Radek Krejci0ec51da2016-12-14 16:42:03 +01004004 /* remove the flag about not applicability */
4005 augment->flags &= ~LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004006 return EXIT_SUCCESS;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004007}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004008
Radek Krejcib2541a32016-12-12 16:45:57 +01004009static void
4010remove_aug(struct lys_node_augment *augment)
4011{
4012 struct lys_node *last, *elem;
4013
Michal Vasko44ab1462017-05-18 13:18:36 +02004014 if (augment->flags & LYS_NOTAPPLIED) {
4015 /* skip already not applied augment */
Radek Krejcib2541a32016-12-12 16:45:57 +01004016 return;
4017 }
4018
4019 elem = augment->child;
4020 if (elem) {
4021 LY_TREE_FOR(elem, last) {
4022 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
4023 break;
4024 }
4025 }
4026 /* elem is first augment child, last is the last child */
4027
4028 /* parent child ptr */
4029 if (augment->target->child == elem) {
4030 augment->target->child = last->next;
4031 }
4032
4033 /* parent child next ptr */
4034 if (elem->prev->next) {
4035 elem->prev->next = last->next;
4036 }
4037
4038 /* parent child prev ptr */
4039 if (last->next) {
4040 last->next->prev = elem->prev;
4041 } else if (augment->target->child) {
4042 augment->target->child->prev = elem->prev;
4043 }
4044
4045 /* update augment children themselves */
4046 elem->prev = last;
4047 last->next = NULL;
4048 }
4049
Radek Krejci0ec51da2016-12-14 16:42:03 +01004050 /* augment->target still keeps the resolved target, but for lys_augment_free()
4051 * we have to keep information that this augment is not applied to free its data */
4052 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01004053}
4054
Radek Krejci30bfcd22017-01-27 16:54:48 +01004055/*
4056 * @param[in] module - the module where the deviation is defined
4057 */
4058static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004059lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004060{
4061 int ret;
4062 char *parent_path;
4063 struct lys_node *target = NULL, *parent;
4064
4065 if (!dev->deviate) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004066 return;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004067 }
4068
4069 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
4070 if (dev->orig_node) {
4071 /* removing not-supported deviation ... */
4072 if (strrchr(dev->target_name, '/') != dev->target_name) {
4073 /* ... from a parent */
4074
4075 /* reconnect to its previous position */
4076 parent = dev->orig_node->parent;
4077 if (parent) {
4078 /* the original node was actually from augment, we have to get know if the augment is
4079 * applied (its module is enabled and implemented). If yes, the node will be connected
4080 * to the augment and the linkage with the target will be fixed if needed, otherwise
4081 * it will be connected only to the augment */
4082 /* first, connect it into the augment */
4083 lys_node_addchild(parent, NULL, dev->orig_node);
4084 if (!parent->module->disabled && parent->module->implemented) {
4085 /* augment is supposed to be applied, so fix pointers in target and the status of the original node */
4086 if (parent->child == dev->orig_node) {
4087 /* the only node in augment */
4088 dev->orig_node->flags |= LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004089 apply_aug((struct lys_node_augment *)parent, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004090 } else {
4091 /* other nodes from augment applied, nothing more needed in target, everything was done
4092 * by lys_node_addchild() */
4093 dev->orig_node->flags |= parent->child->flags & LYS_NOTAPPLIED;
4094 }
4095 } else {
4096 /* augment is not supposed to be applied */
4097 dev->orig_node->flags |= LYS_NOTAPPLIED;
4098 }
4099 } else {
4100 /* non-augment, non-toplevel */
4101 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
Michal Vaskobb520442017-05-23 10:55:18 +02004102 ret = resolve_augment_schema_nodeid(parent_path, NULL, module, (const struct lys_node **)&target);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004103 free(parent_path);
4104 if (ret || !target) {
4105 LOGINT;
4106 return;
4107 }
4108 lys_node_addchild(target, NULL, dev->orig_node);
4109 }
4110 } else {
4111 /* ... from top-level data */
4112 lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node);
4113 }
4114
4115 dev->orig_node = NULL;
4116 } else {
4117 /* adding not-supported deviation */
Michal Vaskobb520442017-05-23 10:55:18 +02004118 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&target);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004119 if (ret || !target) {
4120 LOGINT;
4121 return;
4122 }
4123
4124 /* unlink and store the original node */
4125 parent = target->parent;
4126 lys_node_unlink(target);
4127 if (parent && parent->nodetype == LYS_AUGMENT) {
4128 /* hack for augment, because when the original will be sometime reconnected back, we actually need
4129 * to reconnect it to both - the augment and its target (which is deduced from the deviations target
4130 * path), so we need to remember the augment as an addition */
4131 target->parent = parent;
4132 }
4133 dev->orig_node = target;
4134 }
4135 } else {
Michal Vaskobb520442017-05-23 10:55:18 +02004136 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, module, (const struct lys_node **)&target);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004137 if (ret || !target) {
4138 LOGINT;
4139 return;
4140 }
4141
4142 lys_node_switch(target, dev->orig_node);
4143 dev->orig_node = target;
4144 }
4145}
4146
4147/* temporarily removes or applies deviations, updates module deviation flag accordingly */
4148void
4149lys_switch_deviations(struct lys_module *module)
4150{
4151 uint32_t i = 0, j;
4152 const struct lys_module *mod;
4153 const char *ptr;
Michal Vasko44ab1462017-05-18 13:18:36 +02004154 struct unres_schema *unres;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004155
4156 if (module->deviated) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004157 unres = calloc(1, sizeof *unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004158 LY_CHECK_ERR_RETURN(!unres, LOGMEM, );
Michal Vasko44ab1462017-05-18 13:18:36 +02004159
Radek Krejci30bfcd22017-01-27 16:54:48 +01004160 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
4161 if (mod == module) {
4162 continue;
4163 }
4164
4165 for (j = 0; j < mod->deviation_size; ++j) {
4166 ptr = strstr(mod->deviation[j].target_name, module->name);
4167 if (ptr && ptr[strlen(module->name)] == ':') {
Michal Vasko44ab1462017-05-18 13:18:36 +02004168 lys_switch_deviation(&mod->deviation[j], mod, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004169 }
4170 }
4171 }
4172
4173 if (module->deviated == 2) {
4174 module->deviated = 1;
4175 } else {
4176 module->deviated = 2;
4177 }
Radek Krejci29eac3d2017-06-01 16:50:02 +02004178 for (j = 0; j < module->inc_size; j++) {
4179 if (module->inc[j].submodule->deviated) {
4180 module->inc[j].submodule->deviated = module->deviated;
4181 }
4182 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004183
4184 if (unres->count) {
4185 resolve_unres_schema(module, unres);
4186 }
4187 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004188 }
4189}
4190
4191static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004192apply_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004193{
Michal Vasko44ab1462017-05-18 13:18:36 +02004194 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004195
4196 assert(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004197 lys_node_module(dev->orig_node)->deviated = 1; /* main module */
4198 dev->orig_node->module->deviated = 1; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004199}
4200
4201static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004202remove_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004203{
4204 uint32_t idx = 0, j;
4205 const struct lys_module *mod;
Radek Krejci29eac3d2017-06-01 16:50:02 +02004206 struct lys_module *target_mod, *target_submod;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004207 const char *ptr;
4208
4209 if (dev->orig_node) {
4210 target_mod = lys_node_module(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004211 target_submod = dev->orig_node->module;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004212 } else {
4213 LOGINT;
4214 return;
4215 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004216 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004217
4218 /* clear the deviation flag if possible */
4219 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
4220 if ((mod == module) || (mod == target_mod)) {
4221 continue;
4222 }
4223
4224 for (j = 0; j < mod->deviation_size; ++j) {
4225 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
4226 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
4227 /* some other module deviation targets the inspected module, flag remains */
4228 break;
4229 }
4230 }
4231
4232 if (j < mod->deviation_size) {
4233 break;
4234 }
4235 }
4236
4237 if (!mod) {
Radek Krejci29eac3d2017-06-01 16:50:02 +02004238 target_mod->deviated = 0; /* main module */
4239 target_submod->deviated = 0; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004240 }
4241}
4242
4243void
4244lys_sub_module_apply_devs_augs(struct lys_module *module)
4245{
4246 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004247 struct unres_schema *unres;
4248
4249 unres = calloc(1, sizeof *unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004250 LY_CHECK_ERR_RETURN(!unres, LOGMEM, );
Radek Krejci30bfcd22017-01-27 16:54:48 +01004251
4252 /* remove applied deviations */
4253 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004254 apply_dev(&module->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004255 }
4256 /* remove applied augments */
4257 for (u = 0; u < module->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004258 apply_aug(&module->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004259 }
4260
4261 /* remove deviation and augments defined in submodules */
4262 for (v = 0; v < module->inc_size; ++v) {
4263 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004264 apply_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004265 }
4266
4267 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004268 apply_aug(&module->inc[v].submodule->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004269 }
4270 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004271
4272 if (unres->count) {
4273 resolve_unres_schema(module, unres);
4274 }
4275 /* nothing else left to do even if something is not resolved */
4276 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004277}
4278
Radek Krejcib2541a32016-12-12 16:45:57 +01004279void
4280lys_sub_module_remove_devs_augs(struct lys_module *module)
4281{
4282 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004283 struct unres_schema *unres;
4284
4285 unres = calloc(1, sizeof *unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004286 LY_CHECK_ERR_RETURN(!unres, LOGMEM, );
Radek Krejcib2541a32016-12-12 16:45:57 +01004287
4288 /* remove applied deviations */
4289 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004290 remove_dev(&module->deviation[u], module, unres);
Radek Krejcib2541a32016-12-12 16:45:57 +01004291 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004292 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01004293 for (u = 0; u < module->augment_size; ++u) {
4294 remove_aug(&module->augment[u]);
4295 }
4296
4297 /* remove deviation and augments defined in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004298 for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) {
Radek Krejcib2541a32016-12-12 16:45:57 +01004299 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004300 remove_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejcidbc15262016-06-16 14:58:29 +02004301 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004302
Radek Krejcib2541a32016-12-12 16:45:57 +01004303 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4304 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004305 }
4306 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004307
4308 if (unres->count) {
4309 resolve_unres_schema(module, unres);
4310 }
4311 /* nothing else left to do even if something is not resolved */
4312 unres_schema_free(module, &unres, 1);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004313}
4314
Radek Krejci27fe55e2016-09-13 17:13:35 +02004315static int
4316lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
4317{
4318 struct lys_node *root, *next, *node;
Radek Krejci9e6af732017-04-27 14:40:25 +02004319 uint16_t i, j;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004320
4321 for (i = 0; i < module->augment_size; i++) {
4322 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004323 assert(module->augment[i].target);
4324 if (apply_aug(&module->augment[i], unres)) {
4325 return EXIT_FAILURE;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004326 }
4327 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004328
4329 /* identities */
4330 for (i = 0; i < module->ident_size; i++) {
4331 for (j = 0; j < module->ident[i].base_size; j++) {
4332 resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]);
4333 }
4334 }
4335
Radek Krejci27fe55e2016-09-13 17:13:35 +02004336 LY_TREE_FOR(module->data, root) {
4337 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
4338 LY_TREE_DFS_BEGIN(root, next, node) {
4339 if (node->nodetype == LYS_GROUPING) {
4340 goto nextsibling;
4341 }
4342 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4343 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
4344 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
4345 UNRES_TYPE_LEAFREF, node) == -1) {
4346 return EXIT_FAILURE;
4347 }
4348 }
4349 }
4350
4351 /* modified LY_TREE_DFS_END */
4352 next = node->child;
4353 /* child exception for leafs, leaflists and anyxml without children */
4354 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4355 next = NULL;
4356 }
4357 if (!next) {
4358nextsibling:
4359 /* no children */
4360 if (node == root) {
4361 /* we are done, root has no children */
4362 break;
4363 }
4364 /* try siblings */
4365 next = node->next;
4366 }
4367 while (!next) {
4368 /* parent is already processed, go to its sibling */
4369 node = lys_parent(node);
4370 /* no siblings, go back through parents */
4371 if (lys_parent(node) == lys_parent(root)) {
4372 /* we are done, no next element to process */
4373 break;
4374 }
4375 next = node->next;
4376 }
4377 }
4378 }
4379
4380 return EXIT_SUCCESS;
4381}
4382
4383API int
4384lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02004385{
4386 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004387 struct unres_schema *unres;
Radek Krejci9e6af732017-04-27 14:40:25 +02004388 int i, j, k, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02004389
Radek Krejci27fe55e2016-09-13 17:13:35 +02004390 if (!module) {
4391 ly_errno = LY_EINVAL;
4392 return EXIT_FAILURE;
4393 }
4394
4395 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004396
4397 if (module->disabled) {
4398 disabled = 1;
4399 lys_set_enabled(module);
4400 }
4401
Michal Vasko26055752016-05-03 11:36:31 +02004402 if (module->implemented) {
4403 return EXIT_SUCCESS;
4404 }
4405
4406 ctx = module->ctx;
4407
4408 for (i = 0; i < ctx->models.used; ++i) {
4409 if (module == ctx->models.list[i]) {
4410 continue;
4411 }
4412
4413 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
4414 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004415 if (disabled) {
4416 /* set it back disabled */
4417 lys_set_disabled(module);
4418 }
Michal Vasko26055752016-05-03 11:36:31 +02004419 return EXIT_FAILURE;
4420 }
4421 }
4422
Radek Krejci27fe55e2016-09-13 17:13:35 +02004423 unres = calloc(1, sizeof *unres);
4424 if (!unres) {
4425 LOGMEM;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004426 if (disabled) {
4427 /* set it back disabled */
4428 lys_set_disabled(module);
4429 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004430 return EXIT_FAILURE;
4431 }
4432 /* recursively make the module implemented */
4433 ((struct lys_module *)module)->implemented = 1;
4434 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
4435 goto error;
4436 }
4437 /* process augments in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004438 for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004439 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
4440 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004441 assert(module->inc[i].submodule->augment[j].target);
4442 if (apply_aug(&module->inc[i].submodule->augment[j], unres)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004443 goto error;
4444 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004445 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004446
4447 /* identities */
4448 for (j = 0; j < module->inc[i].submodule->ident_size; j++) {
4449 for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) {
4450 resolve_identity_backlink_update(&module->inc[i].submodule->ident[j],
4451 module->inc[i].submodule->ident[j].base[k]);
4452 }
4453 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004454 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004455 /* try again resolve augments in other modules possibly augmenting this one,
4456 * since we have just enabled it
4457 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004458 /* resolve rest of unres items */
4459 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
4460 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004461 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004462 unres_schema_free(NULL, &unres, 0);
Michal Vasko26055752016-05-03 11:36:31 +02004463
Radek Krejci29eac3d2017-06-01 16:50:02 +02004464 /* reflect implemented flag in submodules */
4465 for (i = 0; i < module->inc_size; i++) {
4466 module->inc[i].submodule->implemented = 1;
4467 }
4468
Michal Vasko26055752016-05-03 11:36:31 +02004469 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004470
4471error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004472 if (disabled) {
4473 /* set it back disabled */
4474 lys_set_disabled(module);
4475 }
4476
Radek Krejci27fe55e2016-09-13 17:13:35 +02004477 ((struct lys_module *)module)->implemented = 0;
Michal Vasko44ab1462017-05-18 13:18:36 +02004478 unres_schema_free((struct lys_module *)module, &unres, 1);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004479 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004480}
4481
4482void
4483lys_submodule_module_data_free(struct lys_submodule *submodule)
4484{
4485 struct lys_node *next, *elem;
4486
4487 /* remove parsed data */
4488 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
4489 if (elem->module == (struct lys_module *)submodule) {
4490 lys_node_free(elem, NULL, 0);
4491 }
4492 }
4493}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02004494
4495int
4496lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
4497{
4498 uint8_t i;
4499
4500 for (i = 0; i < list->keys_size; i++) {
4501 if (list->keys[i] == leaf) {
4502 return i + 1;
4503 }
4504 }
4505
4506 return 0;
4507}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004508
4509API char *
4510lys_path(const struct lys_node *node)
4511{
4512 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
4513 uint16_t index = LY_BUF_SIZE - 1;
4514
4515 if (!node) {
4516 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
4517 return NULL;
4518 }
4519
4520 /* backup the shared internal buffer */
4521 if (ly_buf_used && buf[0]) {
4522 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
4523 }
4524 ly_buf_used++;
4525
4526 /* build the path */
4527 buf[index] = '\0';
Michal Vasko5efa25c2017-01-10 11:34:30 +01004528 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index, 0);
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004529 result = strdup(&buf[index]);
Radek Krejcia8d111f2017-05-31 13:57:37 +02004530 if (!result) {
4531 LOGMEM;
4532 /* pass through to cleanup */
4533 }
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004534
4535 /* restore the shared internal buffer */
4536 if (buf_backup) {
4537 strcpy(buf, buf_backup);
4538 free(buf_backup);
4539 }
4540 ly_buf_used--;
4541
4542 return result;
4543}
Radek Krejci9ad23f42016-10-31 15:46:52 +01004544
Michal Vaskobb520442017-05-23 10:55:18 +02004545struct lys_node_augment *
4546lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target)
4547{
4548 int i, j, last_found;
4549
4550 if (!last) {
4551 last_found = 1;
4552 } else {
4553 last_found = 0;
4554 }
4555
4556 /* search module augments */
4557 for (i = 0; i < mod->augment_size; ++i) {
4558 if (!mod->augment[i].target) {
4559 /* still unresolved, skip */
4560 continue;
4561 }
4562
4563 if (mod->augment[i].target == aug_target) {
4564 if (last_found) {
4565 /* next match after last */
4566 return &mod->augment[i];
4567 }
4568
4569 if (&mod->augment[i] == last) {
4570 last_found = 1;
4571 }
4572 }
4573 }
4574
4575 /* search submodule augments */
4576 for (i = 0; i < mod->inc_size; ++i) {
4577 for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) {
4578 if (!mod->inc[i].submodule->augment[j].target) {
4579 continue;
4580 }
4581
4582 if (mod->inc[i].submodule->augment[j].target == aug_target) {
4583 if (last_found) {
4584 /* next match after last */
4585 return &mod->inc[i].submodule->augment[j];
4586 }
4587
4588 if (&mod->inc[i].submodule->augment[j] == last) {
4589 last_found = 1;
4590 }
4591 }
4592 }
4593 }
4594
4595 return NULL;
4596}
4597
Radek Krejci8d6b7422017-02-03 14:42:13 +01004598static void
4599lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt)
4600{
4601 struct lyext_substmt *info;
Radek Krejcib71243e2017-02-08 16:20:08 +01004602 const char **str, ***a;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004603 int c;
4604
4605 str = lys_ext_complex_get_substmt(stmt, ext, &info);
4606 if (!str || !(*str)) {
4607 return;
4608 }
4609 if (info->cardinality >= LY_STMT_CARD_SOME) {
4610 /* we have array */
Radek Krejcib71243e2017-02-08 16:20:08 +01004611 a = (const char ***)str;
Radek Krejci56c80412017-02-09 10:44:16 +01004612 for (str = (*(const char ***)str), c = 0; str[c]; c++) {
4613 lydict_remove(ctx, str[c]);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004614 }
Radek Krejci56c80412017-02-09 10:44:16 +01004615 free(a[0]);
4616 if (stmt == LY_STMT_BELONGSTO) {
4617 for (str = a[1], c = 0; str[c]; c++) {
4618 lydict_remove(ctx, str[c]);
4619 }
4620 free(a[1]);
PavolVican99c70722017-02-18 17:25:52 +01004621 } else if (stmt == LY_STMT_ARGUMENT) {
4622 free(a[1]);
Radek Krejci56c80412017-02-09 10:44:16 +01004623 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004624 } else {
Radek Krejci56c80412017-02-09 10:44:16 +01004625 lydict_remove(ctx, str[0]);
4626 if (stmt == LY_STMT_BELONGSTO) {
4627 lydict_remove(ctx, str[1]);
4628 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004629 }
4630}
4631void
Radek Krejci5138e9f2017-04-12 13:10:46 +02004632lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size,
4633 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci8d6b7422017-02-03 14:42:13 +01004634{
Radek Krejcif8d05c22017-02-10 15:33:35 +01004635 unsigned int i, j, k;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004636 struct lyext_substmt *substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004637 void **pp, **start;
Radek Krejcif95b6292017-02-13 15:57:37 +01004638 struct lys_node *siter, *snext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004639
4640#define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \
Radek Krejcib84686f2017-02-09 16:04:55 +01004641 pp = lys_ext_complex_get_substmt(STMT, (struct lys_ext_instance_complex *)e[i], NULL); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004642 if (!pp || !(*pp)) { break; } \
Radek Krejcib84686f2017-02-09 16:04:55 +01004643 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004644 for (start = pp = *pp; *pp; pp++) { \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004645 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004646 if (FREE) { free(*pp); } \
4647 } \
4648 free(start); \
4649 } else { /* single item */ \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004650 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004651 if (FREE) { free(*pp); } \
4652 }
4653
4654 if (!size || !e || !(*e)) {
4655 return;
4656 }
4657
4658 for (i = 0; i < size; i++) {
4659 if (!e[i]) {
4660 continue;
4661 }
4662
4663 if (e[i]->flags & (LYEXT_OPT_INHERIT)) {
4664 /* no free, this is just a shadow copy of the original extension instance */
4665 } else {
4666 if (e[i]->flags & (LYEXT_OPT_YANG)) {
4667 free(e[i]->def); /* remove name of instance extension */
PavolVican19dc6152017-02-06 12:04:15 +01004668 e[i]->def = NULL;
PavolVicandb0e8172017-02-20 00:46:09 +01004669 yang_free_ext_data((struct yang_ext_substmt *)e[i]->parent); /* remove backup part of yang file */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004670 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02004671 /* remove private object */
4672 if (e[i]->priv && private_destructor) {
4673 private_destructor((struct lys_node*)e[i], e[i]->priv);
4674 }
4675 lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size, private_destructor);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004676 lydict_remove(ctx, e[i]->arg_value);
4677 }
4678
4679 if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX) {
Radek Krejcifebdad72017-02-06 11:35:51 +01004680 substmt = ((struct lys_ext_instance_complex *)e[i])->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004681 for (j = 0; substmt[j].stmt; j++) {
4682 switch(substmt[j].stmt) {
4683 case LY_STMT_DESCRIPTION:
4684 case LY_STMT_REFERENCE:
4685 case LY_STMT_UNITS:
Radek Krejcib71243e2017-02-08 16:20:08 +01004686 case LY_STMT_ARGUMENT:
4687 case LY_STMT_DEFAULT:
4688 case LY_STMT_ERRTAG:
4689 case LY_STMT_ERRMSG:
4690 case LY_STMT_PREFIX:
4691 case LY_STMT_NAMESPACE:
4692 case LY_STMT_PRESENCE:
4693 case LY_STMT_REVISIONDATE:
4694 case LY_STMT_KEY:
4695 case LY_STMT_BASE:
4696 case LY_STMT_BELONGSTO:
4697 case LY_STMT_CONTACT:
4698 case LY_STMT_ORGANIZATION:
4699 case LY_STMT_PATH:
Radek Krejci8d6b7422017-02-03 14:42:13 +01004700 lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt);
4701 break;
4702 case LY_STMT_TYPE:
4703 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1);
4704 break;
Radek Krejci63fc0962017-02-15 13:20:18 +01004705 case LY_STMT_TYPEDEF:
4706 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1);
4707 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004708 case LY_STMT_IFFEATURE:
4709 EXTCOMPLEX_FREE_STRUCT(LY_STMT_IFFEATURE, struct lys_iffeature, lys_iffeature_free, 0, 1);
4710 break;
Radek Krejci5496fae2017-02-10 13:26:48 +01004711 case LY_STMT_MAX:
4712 case LY_STMT_MIN:
4713 case LY_STMT_POSITION:
PavolVican2ed9f4e2017-02-16 00:08:45 +01004714 case LY_STMT_VALUE:
Radek Krejcif8d05c22017-02-10 15:33:35 +01004715 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
Radek Krejci716cd7a2017-02-15 12:23:41 +01004716 if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) {
Radek Krejcif8d05c22017-02-10 15:33:35 +01004717 for(k = 0; ((uint32_t**)(*pp))[k]; k++) {
4718 free(((uint32_t**)(*pp))[k]);
4719 }
4720 }
4721 free(*pp);
4722 break;
4723 case LY_STMT_DIGITS:
Radek Krejcib84686f2017-02-09 16:04:55 +01004724 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4725 /* free the array */
4726 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4727 free(*pp);
4728 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004729 break;
Radek Krejci37f9ba32017-02-10 16:50:35 +01004730 case LY_STMT_MODULE:
4731 /* modules are part of the context, so they will be freed there */
4732 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4733 /* free the array */
4734 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4735 free(*pp);
4736 }
4737 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01004738 case LY_STMT_ACTION:
Radek Krejcib31762b2017-02-15 10:48:42 +01004739 case LY_STMT_ANYDATA:
4740 case LY_STMT_ANYXML:
4741 case LY_STMT_CASE:
4742 case LY_STMT_CHOICE:
4743 case LY_STMT_CONTAINER:
4744 case LY_STMT_GROUPING:
4745 case LY_STMT_INPUT:
4746 case LY_STMT_LEAF:
4747 case LY_STMT_LEAFLIST:
4748 case LY_STMT_LIST:
4749 case LY_STMT_NOTIFICATION:
4750 case LY_STMT_OUTPUT:
4751 case LY_STMT_RPC:
4752 case LY_STMT_USES:
Radek Krejcif95b6292017-02-13 15:57:37 +01004753 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4754 LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) {
4755 lys_node_free(siter, NULL, 0);
4756 }
Radek Krejcib31762b2017-02-15 10:48:42 +01004757 *pp = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01004758 break;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01004759 case LY_STMT_UNIQUE:
4760 pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL);
4761 if (!pp || !(*pp)) {
4762 break;
4763 }
4764 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4765 for (start = pp = *pp; *pp; pp++) {
4766 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4767 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4768 }
4769 free((*(struct lys_unique**)pp)->expr);
4770 free(*pp);
4771 }
4772 free(start);
4773 } else { /* single item */
4774 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4775 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4776 }
4777 free((*(struct lys_unique**)pp)->expr);
4778 free(*pp);
4779 }
4780 break;
Radek Krejciaa9c5202017-02-15 16:10:14 +01004781 case LY_STMT_LENGTH:
4782 case LY_STMT_MUST:
4783 case LY_STMT_PATTERN:
4784 case LY_STMT_RANGE:
4785 EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1);
4786 break;
Radek Krejcic5cc5302017-02-16 10:07:46 +01004787 case LY_STMT_WHEN:
4788 EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0);
4789 break;
Radek Krejci7417a082017-02-16 11:07:59 +01004790 case LY_STMT_REVISION:
4791 pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL);
4792 if (!pp || !(*pp)) {
4793 break;
4794 }
4795 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4796 for (start = pp = *pp; *pp; pp++) {
4797 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4798 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4799 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004800 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004801 free(*pp);
4802 }
4803 free(start);
4804 } else { /* single item */
4805 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4806 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4807 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004808 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004809 free(*pp);
4810 }
4811 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004812 default:
Radek Krejcib84686f2017-02-09 16:04:55 +01004813 /* nothing to free */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004814 break;
4815 }
4816 }
4817 }
4818
4819 free(e[i]);
4820 }
4821 free(e);
4822
4823#undef EXTCOMPLEX_FREE_STRUCT
4824}