blob: 2f0a4458c78b66c0baca731c54d3dc0a2d6ef3b7 [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 Vasko0f99d3e2017-01-10 10:50:40 +0100166lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, int nam_len,
167 LYS_NODE type, const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200168{
Michal Vasko24476fa2017-03-08 12:33:48 +0100169 const struct lys_node *node, *parent;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200170
171 assert(siblings && name);
172 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
173
Michal Vasko24476fa2017-03-08 12:33:48 +0100174 parent = lys_parent(siblings);
175 while (parent && (parent->nodetype == LYS_USES)) {
176 parent = lys_parent(parent);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200177 }
178
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200179 if (!mod) {
180 mod = siblings->module;
181 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200182
Michal Vasko4f0dad02016-02-15 14:08:23 +0100183 /* try to find the node */
184 node = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +0100185 while ((node = lys_getnext(node, parent, mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100186 if (!type || (node->nodetype & type)) {
187 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200188 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100189 continue;
190 }
191
Michal Vasko4f0dad02016-02-15 14:08:23 +0100192 /* direct name check */
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100193 if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100194 if (ret) {
195 *ret = node;
196 }
197 return EXIT_SUCCESS;
198 }
Radek Krejcic071c542016-01-27 14:57:51 +0100199 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200200 }
201
202 return EXIT_FAILURE;
203}
204
Michal Vasko1e62a092015-12-01 12:27:20 +0100205API const struct lys_node *
206lys_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 +0200207{
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100208 const struct lys_node *next, *aug_parent;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100209 struct lys_node **snode;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200210
Michal Vasko24476fa2017-03-08 12:33:48 +0100211 if ((!parent && !module) || (parent && (parent->nodetype == LYS_USES) && !(options & LYS_GETNEXT_PARENTUSES))) {
212 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
213 return NULL;
214 }
215
Radek Krejci8bc87f62015-09-02 16:19:05 +0200216 if (!last) {
217 /* first call */
218
219 /* get know where to start */
220 if (parent) {
221 /* schema subtree */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100222 snode = lys_child(parent, LYS_UNKNOWN);
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100223 /* do not return anything if the augment does not have any children */
224 if (!snode || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100225 return NULL;
226 }
227 next = last = *snode;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200228 } else {
229 /* top level data */
230 assert(module);
231 next = last = module->data;
232 }
Radek Krejci972724f2016-08-12 15:24:40 +0200233 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
234 /* continue with uses content */
235 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200236 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200237 /* continue after the last returned value */
238 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200239 }
240
241repeat:
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100242 if (parent && (parent->nodetype == LYS_AUGMENT) && next) {
243 /* do not return anything outside the parent augment */
244 aug_parent = next->parent;
245 do {
246 while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) {
247 aug_parent = aug_parent->parent;
248 }
249 if (aug_parent) {
250 if (aug_parent == parent) {
251 break;
252 }
253 aug_parent = ((struct lys_node_augment *)aug_parent)->target;
254 }
255
256 } while (aug_parent);
257 if (!aug_parent) {
258 return NULL;
259 }
260 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200261 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200262 if (options & LYS_GETNEXT_WITHGROUPING) {
263 return next;
264 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200265 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200266 }
267
Radek Krejci972724f2016-08-12 15:24:40 +0200268 if (!next) { /* cover case when parent is augment */
269 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200270 /* no next element */
271 return NULL;
272 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200273 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200274 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200275 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200276 } else {
277 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200278 }
279
280 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200281 case LYS_INPUT:
282 case LYS_OUTPUT:
283 if (options & LYS_GETNEXT_WITHINOUT) {
284 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200285 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200286 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200287 } else {
288 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200289 }
Radek Krejci972724f2016-08-12 15:24:40 +0200290 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200291
Michal Vaskoa5835e92015-10-20 15:07:39 +0200292 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200293 if (options & LYS_GETNEXT_WITHCASE) {
294 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200295 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200296 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200297 } else {
298 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200299 }
Radek Krejci972724f2016-08-12 15:24:40 +0200300 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200301
Michal Vasko1dca6882015-10-22 14:29:42 +0200302 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200303 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200304 if (options & LYS_GETNEXT_WITHUSES) {
305 return next;
306 } else if (next->child) {
307 next = next->child;
308 } else {
309 next = next->next;
310 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200311 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200312
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200313 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200314 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200315 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200316 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200317 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200318 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200319 case LYS_LIST:
320 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200321 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200322
Radek Krejci972724f2016-08-12 15:24:40 +0200323 case LYS_CONTAINER:
324 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
325 if (next->child) {
326 /* go into */
327 next = next->child;
328 } else {
329 next = next->next;
330 }
331 goto repeat;
332 } else {
333 return next;
334 }
335
Radek Krejci8bc87f62015-09-02 16:19:05 +0200336 case LYS_CHOICE:
337 if (options & LYS_GETNEXT_WITHCHOICE) {
338 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200339 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200340 /* go into */
341 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200342 } else {
343 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200344 }
Radek Krejci972724f2016-08-12 15:24:40 +0200345 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200346
Radek Krejci7f40ce32015-08-12 20:38:46 +0200347 default:
348 /* we should not be here */
349 return NULL;
350 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200351}
352
Radek Krejcibf285832017-01-26 16:05:41 +0100353void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200354lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200355{
Radek Krejcif95b6292017-02-13 15:57:37 +0100356 struct lys_node *parent, *first, **pp;
Radek Krejcic071c542016-01-27 14:57:51 +0100357 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200358
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200359 if (!node) {
360 return;
361 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200362
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200363 /* unlink from data model if necessary */
364 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100365 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100366 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100367 if (main_module->data == node) {
368 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200369 }
370 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200371
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200372 /* store pointers to important nodes */
373 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200374 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200375 /* handle augments - first, unlink it from the augment parent ... */
376 if (parent->child == node) {
377 parent->child = node->next;
378 }
Radek Krejcifec2e142017-01-05 15:19:03 +0100379
380 if (parent->flags & LYS_NOTAPPLIED) {
381 /* data are not connected in the target, so we cannot continue with the target as a parent */
382 parent = NULL;
383 } else {
384 /* data are connected in target, so we will continue with the target as a parent */
385 parent = ((struct lys_node_augment *)parent)->target;
386 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200387 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200388
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200389 /* unlink from parent */
390 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100391 if (parent->nodetype == LYS_EXT) {
392 pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype),
393 (struct lys_ext_instance_complex*)parent, NULL);
394 if (*pp == node) {
395 *pp = node->next;
396 }
397 } else if (parent->child == node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200398 parent->child = node->next;
399 }
400 node->parent = NULL;
401 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200402
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200403 /* unlink from siblings */
404 if (node->prev == node) {
405 /* there are no more siblings */
406 return;
407 }
408 if (node->next) {
409 node->next->prev = node->prev;
410 } else {
411 /* unlinking the last element */
412 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100413 if (parent->nodetype == LYS_EXT) {
414 first = *(struct lys_node **)pp;
415 } else {
416 first = parent->child;
417 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200418 } else {
419 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200420 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200421 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200422 }
423 }
424 first->prev = node->prev;
425 }
426 if (node->prev->next) {
427 node->prev->next = node->next;
428 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200429
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200430 /* clean up the unlinked element */
431 node->next = NULL;
432 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200433}
434
Michal Vasko563ef092015-09-04 13:17:23 +0200435struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100436lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200437{
438 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200439
440 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200441 /* top-level augment, look into module (uses augment is handled correctly below) */
442 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
Radek Krejci115fa882017-03-01 16:15:07 +0100443 par_iter = lys_main_module(par_iter->parent->module)->data;
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200444 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200445 break;
446 }
447 }
448
Michal Vasko6f929da2015-10-02 16:23:25 +0200449 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200450 continue;
451 }
452
453 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
454 if (!stop) {
455 stop = par_iter;
456 } else if (iter == stop) {
457 break;
458 }
459 if (iter->nodetype != LYS_GROUPING) {
460 continue;
461 }
462
Radek Krejcif8426a72015-10-31 23:14:03 +0100463 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200464 return (struct lys_node_grp *)iter;
465 }
466 }
467 }
468
Michal Vasko563ef092015-09-04 13:17:23 +0200469 return NULL;
470}
471
Radek Krejci10c760e2015-08-14 14:45:43 +0200472/*
473 * get next grouping in the root's subtree, in the
474 * first call, tha last is NULL
475 */
476static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200477lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200478{
Radek Krejci10c760e2015-08-14 14:45:43 +0200479 struct lys_node *last = (struct lys_node *)lastgrp;
480 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200481
Radek Krejci10c760e2015-08-14 14:45:43 +0200482 assert(root);
483
484 if (!last) {
485 last = root;
486 }
487
488 while (1) {
489 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
490 next = last->child;
491 } else {
492 next = NULL;
493 }
494 if (!next) {
495 if (last == root) {
496 /* we are done */
497 return NULL;
498 }
499
500 /* no children, go to siblings */
501 next = last->next;
502 }
503 while (!next) {
504 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100505 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200506 /* we are done */
507 return NULL;
508 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200509 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100510 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200511 }
512
513 if (next->nodetype == LYS_GROUPING) {
514 return (struct lys_node_grp *)next;
515 }
516
517 last = next;
518 }
519}
520
Michal Vasko0d343d12015-08-24 14:57:36 +0200521/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200522int
Radek Krejci07911992015-08-14 15:13:31 +0200523lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
524{
Michal Vasko563ef092015-09-04 13:17:23 +0200525 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200526 struct lys_node_grp *grp;
Radek Krejcif95b6292017-02-13 15:57:37 +0100527 int down, up;
Radek Krejci07911992015-08-14 15:13:31 +0200528
529 assert(node);
530
531 if (!parent) {
532 assert(module);
533 } else {
534 module = parent->module;
535 }
Radek Krejci115fa882017-03-01 16:15:07 +0100536 module = lys_main_module(module);
Radek Krejci07911992015-08-14 15:13:31 +0200537
538 switch (node->nodetype) {
539 case LYS_GROUPING:
540 /* 6.2.1, rule 6 */
541 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100542 start = *lys_child(parent, LYS_GROUPING);
543 if (!start) {
Radek Krejci07911992015-08-14 15:13:31 +0200544 down = 0;
545 start = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100546 } else {
547 down = 1;
548 }
549 if (parent->nodetype == LYS_EXT) {
550 up = 0;
551 } else {
552 up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200553 }
554 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100555 down = up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200556 start = module->data;
557 }
558 /* go up */
Radek Krejcif95b6292017-02-13 15:57:37 +0100559 if (up && lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100560 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200561 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200562 }
563 /* go down, because grouping can be defined after e.g. container in which is collision */
564 if (down) {
565 for (iter = start, stop = NULL; iter; iter = iter->prev) {
566 if (!stop) {
567 stop = start;
568 } else if (iter == stop) {
569 break;
570 }
571 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
572 continue;
573 }
574
575 grp = NULL;
576 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100577 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100578 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200579 return EXIT_FAILURE;
580 }
581 }
582 }
583 }
584 break;
585 case LYS_LEAF:
586 case LYS_LEAFLIST:
587 case LYS_LIST:
588 case LYS_CONTAINER:
589 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200590 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200591 /* 6.2.1, rule 7 */
592 if (parent) {
593 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200594 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
595 if (iter->nodetype == LYS_AUGMENT) {
596 if (((struct lys_node_augment *)iter)->target) {
597 /* augment is resolved, go up */
598 iter = ((struct lys_node_augment *)iter)->target;
599 continue;
600 }
601 /* augment is not resolved, this is the final parent */
602 break;
603 }
Radek Krejci07911992015-08-14 15:13:31 +0200604 iter = iter->parent;
605 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200606
Radek Krejci07911992015-08-14 15:13:31 +0200607 if (!iter) {
608 stop = NULL;
609 iter = module->data;
Radek Krejcif95b6292017-02-13 15:57:37 +0100610 } else if (iter->nodetype == LYS_EXT) {
611 stop = iter;
612 iter = *lys_child(iter, node->nodetype);
Radek Krejci07911992015-08-14 15:13:31 +0200613 } else {
614 stop = iter;
615 iter = iter->child;
616 }
617 } else {
618 stop = NULL;
619 iter = module->data;
620 }
621 while (iter) {
622 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
623 iter = iter->child;
624 continue;
625 }
626
Radek Krejcibf2abff2016-08-23 15:51:52 +0200627 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100628 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100629 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200630 return EXIT_FAILURE;
631 }
632 }
633
634 /* special case for choice - we must check the choice's name as
635 * well as the names of nodes under the choice
636 */
637 if (iter->nodetype == LYS_CHOICE) {
638 iter = iter->child;
639 continue;
640 }
641
642 /* go to siblings */
643 if (!iter->next) {
644 /* no sibling, go to parent's sibling */
645 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200646 /* for parent LYS_AUGMENT */
647 if (iter->parent == stop) {
648 iter = stop;
649 break;
650 }
651 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200652 if (iter && iter->next) {
653 break;
654 }
655 } while (iter != stop);
656
657 if (iter == stop) {
658 break;
659 }
660 }
661 iter = iter->next;
662 }
663 break;
664 case LYS_CASE:
665 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100666 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100667 start = *lys_child(parent, LYS_CASE);
Radek Krejcic071c542016-01-27 14:57:51 +0100668 } else {
669 start = module->data;
670 }
671
672 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200673 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200674 continue;
675 }
676
Radek Krejci749190d2016-02-18 16:26:25 +0100677 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100678 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200679 return EXIT_FAILURE;
680 }
681 }
682 break;
683 default:
684 /* no check needed */
685 break;
686 }
687
688 return EXIT_SUCCESS;
689}
690
Michal Vasko0d343d12015-08-24 14:57:36 +0200691/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200692int
Radek Krejci10c760e2015-08-14 14:45:43 +0200693lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
694{
Radek Krejcif95b6292017-02-13 15:57:37 +0100695 struct lys_node *iter, *next, **pchild;
Radek Krejci41a349b2016-10-24 19:21:59 +0200696 struct lys_node_inout *in, *out, *inout;
Radek Krejci744c2d42017-03-26 13:30:00 -0500697 struct lys_node_case *c;
698 int type, shortcase = 0;
Radek Krejcif95b6292017-02-13 15:57:37 +0100699 void *p;
700 struct lyext_substmt *info = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200701
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200702 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200703
Radek Krejci10c760e2015-08-14 14:45:43 +0200704 if (parent) {
705 type = parent->nodetype;
706 module = parent->module;
707 } else {
708 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100709 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200710 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200711 }
712
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200713 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200714 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200715 case LYS_CONTAINER:
716 case LYS_LIST:
717 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100718 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200719 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200720 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200721 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200722 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
723 return EXIT_FAILURE;
724 }
725 break;
Radek Krejci76512572015-08-04 09:47:08 +0200726 case LYS_INPUT:
727 case LYS_OUTPUT:
728 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200729 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200730 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200731 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100732 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200733 return EXIT_FAILURE;
734 }
735 break;
Radek Krejci76512572015-08-04 09:47:08 +0200736 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200737 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200738 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100739 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200740 return EXIT_FAILURE;
741 }
Radek Krejci744c2d42017-03-26 13:30:00 -0500742 if (child->nodetype != LYS_CASE) {
743 shortcase = 1;
744 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200745 break;
Radek Krejci76512572015-08-04 09:47:08 +0200746 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200747 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200748 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100749 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200750 return EXIT_FAILURE;
751 }
752 break;
Radek Krejci76512572015-08-04 09:47:08 +0200753 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200754 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200755 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100756 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200757 return EXIT_FAILURE;
758 }
759 break;
Radek Krejci76512572015-08-04 09:47:08 +0200760 case LYS_LEAF:
761 case LYS_LEAFLIST:
762 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200763 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100764 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vasko51e5c582017-01-19 14:16:39 +0100765 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100766 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200767 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200768 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200769 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200770 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskodb017262017-01-24 13:10:04 +0100771 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100772 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200773 return EXIT_FAILURE;
774 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200775 break;
776 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200777 /* top level */
778 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200779 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200780 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100781 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200782 return EXIT_FAILURE;
783 }
Radek Krejcif95b6292017-02-13 15:57:37 +0100784 break;
785 case LYS_EXT:
786 /* plugin-defined */
787 p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info);
788 if (!p) {
789 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype),
790 ((struct lys_ext_instance_complex*)parent)->def->name);
791 return EXIT_FAILURE;
792 }
793 /* TODO check cardinality */
Radek Krejcic071c542016-01-27 14:57:51 +0100794 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200795 }
796
797 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200798 if (lys_check_id(child, parent, module)) {
799 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200800 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200801
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200802 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200803 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200804 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200805
Radek Krejcif95b6292017-02-13 15:57:37 +0100806 if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200807 /* replace the implicit input/output node */
808 if (child->nodetype == LYS_OUTPUT) {
809 inout = (struct lys_node_inout *)parent->child->next;
810 } else { /* LYS_INPUT */
811 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200812 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200813 }
814 if (inout->next) {
815 child->next = inout->next;
816 inout->next->prev = child;
817 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200818 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200819 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200820 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200821 child->prev = inout->prev;
822 if (inout->prev->next) {
823 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200824 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200825 inout->prev = (struct lys_node *)inout;
826 child->parent = parent;
827 inout->parent = NULL;
828 lys_node_free((struct lys_node *)inout, NULL, 0);
829 } else {
Radek Krejci744c2d42017-03-26 13:30:00 -0500830 if (shortcase) {
831 /* create the implicit case to allow it to serve as a target of the augments,
832 * it won't be printed, but it will be present in the tree */
833 c = calloc(1, sizeof *c);
834 c->name = lydict_insert(module->ctx, child->name, 0);
835 c->flags = LYS_IMPLICIT;
836 c->module = module;
837 c->nodetype = LYS_CASE;
838 c->prev = (struct lys_node*)c;
839 lys_node_addchild(parent, module, (struct lys_node*)c);
840 parent = (struct lys_node*)c;
841 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200842 /* connect the child correctly */
843 if (!parent) {
844 if (module->data) {
845 module->data->prev->next = child;
846 child->prev = module->data->prev;
847 module->data->prev = child;
848 } else {
849 module->data = child;
850 }
851 } else {
Radek Krejci30bfcd22017-01-27 16:54:48 +0100852 next = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +0100853 pchild = lys_child(parent, child->nodetype);
854 assert(pchild);
855
856 if (!(*pchild)) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200857 /* the only/first child of the parent */
Radek Krejcif95b6292017-02-13 15:57:37 +0100858 *pchild = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200859 child->parent = parent;
860 iter = child;
Radek Krejci30bfcd22017-01-27 16:54:48 +0100861 } else if (type == LYS_AUGMENT) {
862 /* add a new child as a last child of the augment (no matter if applied or not) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100863 for (iter = (*pchild)->prev; iter->parent != parent; iter = iter->prev);
Radek Krejci30bfcd22017-01-27 16:54:48 +0100864 next = iter->next;
865 iter->next = child;
866 child->prev = iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200867 } else {
868 /* add a new child at the end of parent's child list */
Radek Krejcif95b6292017-02-13 15:57:37 +0100869 iter = (*pchild)->prev;
Radek Krejci41a349b2016-10-24 19:21:59 +0200870 iter->next = child;
871 child->prev = iter;
872 }
873 while (iter->next) {
874 iter = iter->next;
875 iter->parent = parent;
876 }
Radek Krejci30bfcd22017-01-27 16:54:48 +0100877 if (next) {
878 /* we are in applied augment, its target has some additional nodes after the nodes from this augment */
879 iter->next = next;
880 next->prev = iter;
881 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100882 (*pchild)->prev = iter;
Radek Krejci30bfcd22017-01-27 16:54:48 +0100883 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200884 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200885 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200886
Michal Vaskoe022a562016-09-27 14:24:15 +0200887 /* check config value (but ignore them in groupings and augments) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100888 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); iter = iter->parent);
Michal Vaskoe022a562016-09-27 14:24:15 +0200889 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200890 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
891 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
892 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +0100893 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200894 return EXIT_FAILURE;
895 }
896 }
897
Radek Krejci41771502016-04-14 17:52:32 +0200898 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200899 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200900 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200901 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200902 /* store it only into container or list - the only data inner nodes */
903 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
904 if (iter->flags & LYS_INCL_STATUS) {
905 /* done, someone else set it already from here */
906 break;
907 }
908 /* set flag about including status data */
909 iter->flags |= LYS_INCL_STATUS;
910 }
911 }
912 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200913
914 /* create implicit input/output nodes to have available them as possible target for augment */
915 if (child->nodetype & (LYS_RPC | LYS_ACTION)) {
916 in = calloc(1, sizeof *in);
917 in->nodetype = LYS_INPUT;
918 in->name = lydict_insert(child->module->ctx, "input", 5);
919 out = calloc(1, sizeof *out);
920 out->name = lydict_insert(child->module->ctx, "output", 6);
921 out->nodetype = LYS_OUTPUT;
922 in->module = out->module = child->module;
923 in->parent = out->parent = child;
924 in->flags = out->flags = LYS_IMPLICIT;
925 in->next = (struct lys_node *)out;
926 in->prev = (struct lys_node *)out;
927 out->prev = (struct lys_node *)in;
928 child->child = (struct lys_node *)in;
929 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200930 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200931}
932
Michal Vasko29245662017-04-18 15:56:31 +0200933const struct lys_module *
934lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal, int implement)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200935{
Radek Krejcia1df1682016-04-11 14:56:59 +0200936 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200937 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200938 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200939
Radek Krejci00a0e712016-10-26 10:24:46 +0200940 ly_err_clean(1);
Radek Krejcif347abc2016-06-22 10:18:47 +0200941
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942 if (!ctx || !data) {
943 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
944 return NULL;
945 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200946
Radek Krejcia1df1682016-04-11 14:56:59 +0200947 if (!internal && format == LYS_IN_YANG) {
948 /* enlarge data by 2 bytes for flex */
949 len = strlen(data);
950 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
951 if (!enlarged_data) {
952 LOGMEM;
953 return NULL;
954 }
955 memcpy(enlarged_data, data, len);
956 enlarged_data[len] = enlarged_data[len + 1] = '\0';
957 data = enlarged_data;
958 }
959
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200960 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200961 case LYS_IN_YIN:
Michal Vasko29245662017-04-18 15:56:31 +0200962 mod = yin_read_module(ctx, data, NULL, implement);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200963 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200964 case LYS_IN_YANG:
Michal Vasko29245662017-04-18 15:56:31 +0200965 mod = yang_read_module(ctx, data, 0, NULL, implement);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100966 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200967 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200968 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200969 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200970 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200971
Radek Krejcia1df1682016-04-11 14:56:59 +0200972 free(enlarged_data);
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100973
974 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
975 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
976 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
977 * the anotation definitions available in the internal schema structure. There is another hack in schema
978 * printers to do not print this internally added annotation. */
979 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci5b190662017-04-13 08:56:14 +0200980 if (lyp_add_ietf_netconf_annotations(mod)) {
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100981 lys_free(mod, NULL, 1);
982 return NULL;
983 }
Radek Krejcia68ddeb2017-02-24 12:49:44 +0100984 }
985
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200986 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200987}
988
Radek Krejcia1df1682016-04-11 14:56:59 +0200989API const struct lys_module *
990lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
991{
Michal Vasko29245662017-04-18 15:56:31 +0200992 return lys_parse_mem_(ctx, data, format, 0, 1);
Radek Krejcia1df1682016-04-11 14:56:59 +0200993}
994
Michal Vasko5a721fd2016-02-16 12:16:48 +0100995struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +0100996lys_sub_parse_mem(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200997{
Michal Vasko5b998712017-01-26 10:34:06 +0100998 char *enlarged_data = NULL;
Michal Vasko5a721fd2016-02-16 12:16:48 +0100999 struct lys_submodule *submod = NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001000 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001001
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001002 assert(module);
1003 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001004
Michal Vasko5b998712017-01-26 10:34:06 +01001005 if (format == LYS_IN_YANG) {
1006 /* enlarge data by 2 bytes for flex */
1007 len = strlen(data);
1008 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
1009 if (!enlarged_data) {
1010 LOGMEM;
1011 return NULL;
1012 }
1013 memcpy(enlarged_data, data, len);
1014 enlarged_data[len] = enlarged_data[len + 1] = '\0';
1015 data = enlarged_data;
1016 }
1017
Radek Krejcic071c542016-01-27 14:57:51 +01001018 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +02001019 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001020
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001021 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001022 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +01001023 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001024 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001025 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001026 submod = yang_read_submodule(module, data, 0, unres);
1027 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001028 default:
Radek Krejci90a550a2016-04-13 16:00:58 +02001029 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001030 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001031 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001032
Michal Vasko5b998712017-01-26 10:34:06 +01001033 free(enlarged_data);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001034 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001035}
1036
Michal Vasko1e62a092015-12-01 12:27:20 +01001037API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +01001038lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1039{
1040 int fd;
1041 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +02001042 const char *rev, *dot, *filename;
1043 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +01001044
1045 if (!ctx || !path) {
1046 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1047 return NULL;
1048 }
1049
1050 fd = open(path, O_RDONLY);
1051 if (fd == -1) {
1052 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
1053 return NULL;
1054 }
1055
1056 ret = lys_parse_fd(ctx, fd, format);
1057 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +01001058
Radek Krejcid80c8602016-10-25 11:56:03 +02001059 if (!ret) {
1060 /* error */
1061 return NULL;
1062 }
1063
1064 /* check that name and revision match filename */
1065 filename = strrchr(path, '/');
1066 if (!filename) {
1067 filename = path;
1068 } else {
1069 filename++;
1070 }
1071 rev = strchr(filename, '@');
1072 dot = strrchr(filename, '.');
1073
1074 /* name */
1075 len = strlen(ret->name);
1076 if (strncmp(filename, ret->name, len) ||
1077 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejcic0c82302016-10-25 14:21:33 +02001078 LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +02001079 }
1080 if (rev) {
1081 len = dot - ++rev;
1082 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
1083 LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename,
1084 ret->rev_size ? ret->rev[0].date : "none");
1085 }
1086 }
1087
1088 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +01001089 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +01001090 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +01001091 }
1092
Michal Vasko662610a2015-12-07 11:25:45 +01001093 return ret;
1094}
1095
1096API const struct lys_module *
1097lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001098{
Michal Vasko1e62a092015-12-01 12:27:20 +01001099 const struct lys_module *module;
Radek Krejci0fb11502017-01-31 16:45:42 +01001100 size_t length;
Radek Krejci63a91a92015-07-29 13:31:04 +02001101 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +01001102 char buf[PATH_MAX];
1103 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +02001104
1105 if (!ctx || fd < 0) {
1106 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1107 return NULL;
1108 }
1109
Radek Krejci0fb11502017-01-31 16:45:42 +01001110 addr = lyp_mmap(fd, format == LYS_IN_YANG ? 1 : 0, &length);
Pavol Vicane36ea262015-11-12 11:57:47 +01001111 if (addr == MAP_FAILED) {
Radek Krejci0fb11502017-01-31 16:45:42 +01001112 LOGERR(LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001113 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001114 } else if (!addr) {
1115 LOGERR(LY_EINVAL, "Empty schema file.");
Pavol Vicane36ea262015-11-12 11:57:47 +01001116 return NULL;
1117 }
Radek Krejci0fb11502017-01-31 16:45:42 +01001118
Michal Vasko29245662017-04-18 15:56:31 +02001119 module = lys_parse_mem_(ctx, addr, format, 1, 1);
Radek Krejci0fb11502017-01-31 16:45:42 +01001120 lyp_munmap(addr, length);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001121
Radek Krejcia77904e2016-02-25 16:23:45 +01001122 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001123 /* get URI if there is /proc */
1124 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001125 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1126 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1127 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1128 }
1129 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001130 }
Radek Krejcib051f722016-02-25 15:12:21 +01001131 }
1132
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001133 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001134}
1135
Michal Vasko5a721fd2016-02-16 12:16:48 +01001136struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001137lys_sub_parse_fd(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001138{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001139 struct lys_submodule *submodule;
Radek Krejci0fb11502017-01-31 16:45:42 +01001140 size_t length;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001141 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001142
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001143 assert(module);
1144 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001145
Radek Krejci0fb11502017-01-31 16:45:42 +01001146 addr = lyp_mmap(fd, format == LYS_IN_YANG ? 1 : 0, &length);
Pavol Vicane36ea262015-11-12 11:57:47 +01001147 if (addr == MAP_FAILED) {
Radek Krejci0fb11502017-01-31 16:45:42 +01001148 LOGERR(LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001149 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001150 } else if (!addr) {
1151 LOGERR(LY_EINVAL, "Empty submodule schema file.");
Michal Vasko2e7241e2016-02-15 16:06:34 +01001152 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001153 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001154
Michal Vasko5b998712017-01-26 10:34:06 +01001155 /* get the main module */
1156 module = lys_main_module(module);
1157
1158 switch (format) {
1159 case LYS_IN_YIN:
1160 submodule = yin_read_submodule(module, addr, unres);
1161 break;
1162 case LYS_IN_YANG:
1163 submodule = yang_read_submodule(module, addr, 0, unres);
1164 break;
1165 default:
Michal Vasko85d41522017-02-24 09:49:16 +01001166 LOGINT;
1167 return NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001168 }
1169
Radek Krejcic645a3a2017-01-31 16:59:00 +01001170 lyp_munmap(addr, length);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001171 return submodule;
1172
Radek Krejciefaeba32015-05-27 14:30:57 +02001173}
1174
Radek Krejcibf285832017-01-26 16:05:41 +01001175int
1176lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt)
1177{
1178 unsigned int u;
1179
1180 for (u = start; u < ext_size; u++) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001181 if (ext[u]->insubstmt == substmt) {
Radek Krejcibf285832017-01-26 16:05:41 +01001182 return u;
1183 }
1184 }
1185
1186 return -1;
1187}
1188
Radek Krejcifdc0d702017-01-23 15:58:38 +01001189/*
1190 * duplicate extension instance
1191 */
1192int
Radek Krejci5138e9f2017-04-12 13:10:46 +02001193lys_ext_dup(struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent, LYEXT_PAR parent_type,
1194 struct lys_ext_instance ***new, int shallow, struct unres_schema *unres)
Radek Krejcifdc0d702017-01-23 15:58:38 +01001195{
1196 int i;
1197 uint8_t u = 0;
1198 struct lys_ext_instance **result;
1199 struct unres_ext *info, *info_orig;
1200
1201 assert(new);
1202
1203 if (!size) {
1204 if (orig) {
1205 LOGINT;
1206 return EXIT_FAILURE;
1207 }
1208 (*new) = NULL;
1209 return EXIT_SUCCESS;
1210 }
1211
1212 (*new) = result = calloc(size, sizeof *result);
1213 for (u = 0; u < size; u++) {
1214 if (orig[u]) {
1215 /* resolved extension instance, just duplicate it */
Radek Krejci8de8f612017-02-16 15:03:32 +01001216 switch(orig[u]->ext_type) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001217 case LYEXT_FLAG:
1218 result[u] = malloc(sizeof(struct lys_ext_instance));
1219 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001220 case LYEXT_COMPLEX:
1221 result[u] = calloc(1, ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size);
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);
1262 info->datatype = info_orig->datatype;
1263 if (info->datatype == LYS_IN_YIN) {
Radek Krejci8d6b7422017-02-03 14:42:13 +01001264 info->data.yin = lyxml_dup_elem(mod->ctx, info_orig->data.yin, NULL, 1);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001265 } /* else TODO YANG */
1266 info->parent = parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001267 info->mod = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001268 info->parent_type = parent_type;
1269 info->ext_index = u;
1270 if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) {
1271 goto error;
1272 }
1273 }
1274 }
1275
1276 return EXIT_SUCCESS;
1277
1278error:
1279 (*new) = NULL;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001280 lys_extension_instances_free(mod->ctx, result, u, NULL);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001281 return EXIT_FAILURE;
1282}
1283
Radek Krejci1d82ef62015-08-07 14:44:40 +02001284static struct lys_restr *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001285lys_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 +02001286{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001287 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001288 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001289
Radek Krejci3733a802015-06-19 13:43:21 +02001290 if (!size) {
1291 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001292 }
Radek Krejci3733a802015-06-19 13:43:21 +02001293
1294 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001295 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001296 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001297 return NULL;
1298 }
Radek Krejci3733a802015-06-19 13:43:21 +02001299 for (i = 0; i < size; i++) {
Radek Krejci77f22b22017-01-17 15:23:03 +01001300 result[i].ext_size = old[i].ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001301 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 +01001302 result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0);
1303 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1304 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1305 result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0);
1306 result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001307 }
1308
1309 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001310}
1311
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001312void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001313lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr,
1314 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci0bd5db42015-06-19 13:30:07 +02001315{
1316 assert(ctx);
1317 if (!restr) {
1318 return;
1319 }
1320
Radek Krejci5138e9f2017-04-12 13:10:46 +02001321 lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001322 lydict_remove(ctx, restr->expr);
1323 lydict_remove(ctx, restr->dsc);
1324 lydict_remove(ctx, restr->ref);
1325 lydict_remove(ctx, restr->eapptag);
1326 lydict_remove(ctx, restr->emsg);
1327}
1328
Pavol Vican05810b62016-11-23 14:07:22 +01001329void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001330lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size,
1331 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001332{
1333 uint8_t i;
1334
1335 for (i = 0; i < iffeature_size; ++i) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001336 lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001337 free(iffeature[i].expr);
1338 free(iffeature[i].features);
1339 }
1340 free(iffeature);
1341}
1342
Michal Vaskob84f88a2015-09-24 13:16:10 +02001343static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001344type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001345 LY_DATA_TYPE base, int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001346{
1347 int i;
1348
1349 switch (base) {
1350 case LY_TYPE_BINARY:
1351 if (old->info.binary.length) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001352 new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001353 }
1354 break;
1355
1356 case LY_TYPE_BITS:
1357 new->info.bits.count = old->info.bits.count;
1358 if (new->info.bits.count) {
1359 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1360 if (!new->info.bits.bit) {
1361 LOGMEM;
1362 return -1;
1363 }
1364 for (i = 0; i < new->info.bits.count; i++) {
1365 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1366 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1367 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1368 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1369 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001370 new->info.bits.bit[i].ext_size = old->info.bits.bit[i].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001371 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 +01001372 &new->info.bits.bit[i], LYEXT_PAR_TYPE_BIT,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001373 &new->info.bits.bit[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001374 return -1;
1375 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001376 }
1377 }
1378 break;
1379
1380 case LY_TYPE_DEC64:
1381 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001382 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001383 if (old->info.dec64.range) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001384 new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001385 }
1386 break;
1387
1388 case LY_TYPE_ENUM:
1389 new->info.enums.count = old->info.enums.count;
1390 if (new->info.enums.count) {
1391 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1392 if (!new->info.enums.enm) {
1393 LOGMEM;
1394 return -1;
1395 }
1396 for (i = 0; i < new->info.enums.count; i++) {
1397 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1398 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1399 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1400 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1401 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001402 new->info.enums.enm[i].ext_size = old->info.enums.enm[i].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001403 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 +01001404 &new->info.enums.enm[i], LYEXT_PAR_TYPE_ENUM,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001405 &new->info.enums.enm[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001406 return -1;
1407 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001408 }
1409 }
1410 break;
1411
1412 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001413 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001414 if (old->info.ident.count) {
1415 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1416 if (!new->info.ident.ref) {
1417 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001418 return -1;
1419 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001420 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1421 } else {
1422 /* there can be several unresolved base identities, duplicate them all */
1423 i = -1;
Radek Krejciddddd0d2017-01-20 15:20:46 +01001424 do {
1425 i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF);
1426 if (i != -1) {
1427 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1428 return -1;
1429 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001430 }
1431 --i;
Radek Krejciddddd0d2017-01-20 15:20:46 +01001432 } while (i > -1);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001433 }
1434 break;
1435
1436 case LY_TYPE_INST:
1437 new->info.inst.req = old->info.inst.req;
1438 break;
1439
1440 case LY_TYPE_INT8:
1441 case LY_TYPE_INT16:
1442 case LY_TYPE_INT32:
1443 case LY_TYPE_INT64:
1444 case LY_TYPE_UINT8:
1445 case LY_TYPE_UINT16:
1446 case LY_TYPE_UINT32:
1447 case LY_TYPE_UINT64:
1448 if (old->info.num.range) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001449 new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001450 }
1451 break;
1452
1453 case LY_TYPE_LEAFREF:
1454 if (old->info.lref.path) {
1455 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko1c007172017-03-10 10:20:44 +01001456 if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001457 return -1;
1458 }
1459 }
1460 break;
1461
1462 case LY_TYPE_STRING:
1463 if (old->info.str.length) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001464 new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001465 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001466 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 +02001467 new->info.str.pat_count = old->info.str.pat_count;
1468 break;
1469
1470 case LY_TYPE_UNION:
1471 new->info.uni.count = old->info.uni.count;
1472 if (new->info.uni.count) {
1473 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1474 if (!new->info.uni.types) {
1475 LOGMEM;
1476 return -1;
1477 }
1478 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001479 if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]),
1480 in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001481 return -1;
1482 }
1483 }
1484 }
1485 break;
1486
1487 default:
1488 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1489 break;
1490 }
1491 return EXIT_SUCCESS;
1492}
1493
1494struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001495lys_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 +02001496 int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001497{
1498 struct yang_type *new;
1499
1500 new = calloc(1, sizeof *new);
1501 if (!new) {
1502 LOGMEM;
1503 return NULL;
1504 }
1505 new->flags = old->flags;
1506 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001507 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001508 new->type = type;
1509 if (!new->name) {
1510 LOGMEM;
1511 goto error;
1512 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001513 if (type_dup(module, parent, type, old->type, new->base, in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001514 new->type->base = new->base;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001515 lys_type_free(module->ctx, new->type, NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001516 memset(&new->type->info, 0, sizeof new->type->info);
1517 goto error;
1518 }
1519 return new;
1520
1521 error:
1522 free(new);
1523 return NULL;
1524}
1525
Radek Krejci43ce4b72017-01-04 11:02:38 +01001526API const void *
1527lys_ext_instance_substmt(const struct lys_ext_instance *ext)
1528{
1529 if (!ext) {
1530 return NULL;
1531 }
1532
Radek Krejcifebdad72017-02-06 11:35:51 +01001533 switch (ext->insubstmt) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001534 case LYEXT_SUBSTMT_SELF:
1535 case LYEXT_SUBSTMT_MODIFIER:
1536 case LYEXT_SUBSTMT_VERSION:
1537 return NULL;
1538 case LYEXT_SUBSTMT_ARGUMENT:
1539 if (ext->parent_type == LYEXT_PAR_EXT) {
1540 return ((struct lys_ext_instance*)ext->parent)->arg_value;
1541 }
1542 break;
1543 case LYEXT_SUBSTMT_BASE:
1544 if (ext->parent_type == LYEXT_PAR_TYPE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001545 return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001546 } else if (ext->parent_type == LYEXT_PAR_IDENT) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001547 return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001548 }
1549 break;
1550 case LYEXT_SUBSTMT_BELONGSTO:
1551 if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) {
1552 return ((struct lys_submodule*)ext->parent)->belongsto;
1553 }
1554 break;
1555 case LYEXT_SUBSTMT_CONFIG:
1556 case LYEXT_SUBSTMT_MANDATORY:
1557 if (ext->parent_type == LYEXT_PAR_NODE) {
1558 return &((struct lys_node*)ext->parent)->flags;
1559 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1560 return &((struct lys_deviate*)ext->parent)->flags;
1561 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1562 return &((struct lys_refine*)ext->parent)->flags;
1563 }
1564 break;
1565 case LYEXT_SUBSTMT_CONTACT:
1566 if (ext->parent_type == LYEXT_PAR_MODULE) {
1567 return ((struct lys_module*)ext->parent)->contact;
1568 }
1569 break;
1570 case LYEXT_SUBSTMT_DEFAULT:
1571 if (ext->parent_type == LYEXT_PAR_NODE) {
1572 switch (((struct lys_node*)ext->parent)->nodetype) {
1573 case LYS_LEAF:
1574 case LYS_LEAFLIST:
1575 /* in case of leaf, the index is supposed to be 0, so it will return the
1576 * correct pointer despite the leaf structure does not have dflt as array */
Radek Krejcifebdad72017-02-06 11:35:51 +01001577 return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001578 case LYS_CHOICE:
1579 return ((struct lys_node_choice*)ext->parent)->dflt;
1580 default:
1581 /* internal error */
1582 break;
1583 }
1584 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1585 return ((struct lys_tpdf*)ext->parent)->dflt;
1586 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001587 return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001588 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001589 return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001590 }
1591 break;
1592 case LYEXT_SUBSTMT_DESCRIPTION:
1593 switch (ext->parent_type) {
1594 case LYEXT_PAR_NODE:
1595 return ((struct lys_node*)ext->parent)->dsc;
1596 case LYEXT_PAR_MODULE:
1597 return ((struct lys_module*)ext->parent)->dsc;
1598 case LYEXT_PAR_IMPORT:
1599 return ((struct lys_import*)ext->parent)->dsc;
1600 case LYEXT_PAR_INCLUDE:
1601 return ((struct lys_include*)ext->parent)->dsc;
1602 case LYEXT_PAR_EXT:
1603 return ((struct lys_ext*)ext->parent)->dsc;
1604 case LYEXT_PAR_FEATURE:
1605 return ((struct lys_feature*)ext->parent)->dsc;
1606 case LYEXT_PAR_TPDF:
1607 return ((struct lys_tpdf*)ext->parent)->dsc;
1608 case LYEXT_PAR_TYPE_BIT:
1609 return ((struct lys_type_bit*)ext->parent)->dsc;
1610 case LYEXT_PAR_TYPE_ENUM:
1611 return ((struct lys_type_enum*)ext->parent)->dsc;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001612 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001613 return ((struct lys_restr*)ext->parent)->dsc;
1614 case LYEXT_PAR_WHEN:
1615 return ((struct lys_when*)ext->parent)->dsc;
1616 case LYEXT_PAR_IDENT:
1617 return ((struct lys_ident*)ext->parent)->dsc;
1618 case LYEXT_PAR_DEVIATION:
1619 return ((struct lys_deviation*)ext->parent)->dsc;
1620 case LYEXT_PAR_REVISION:
1621 return ((struct lys_revision*)ext->parent)->dsc;
1622 case LYEXT_PAR_REFINE:
1623 return ((struct lys_refine*)ext->parent)->dsc;
1624 default:
1625 break;
1626 }
1627 break;
1628 case LYEXT_SUBSTMT_ERRTAG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001629 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001630 return ((struct lys_restr*)ext->parent)->eapptag;
1631 }
1632 break;
1633 case LYEXT_SUBSTMT_ERRMSG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001634 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001635 return ((struct lys_restr*)ext->parent)->emsg;
1636 }
1637 break;
1638 case LYEXT_SUBSTMT_DIGITS:
1639 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) {
1640 return &((struct lys_type*)ext->parent)->info.dec64.dig;
1641 }
1642 break;
1643 case LYEXT_SUBSTMT_KEY:
1644 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1645 return ((struct lys_node_list*)ext->parent)->keys;
1646 }
1647 break;
1648 case LYEXT_SUBSTMT_MAX:
1649 if (ext->parent_type == LYEXT_PAR_NODE) {
1650 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1651 return &((struct lys_node_list*)ext->parent)->max;
1652 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1653 return &((struct lys_node_leaflist*)ext->parent)->max;
1654 }
1655 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1656 return &((struct lys_refine*)ext->parent)->mod.list.max;
1657 }
1658 break;
1659 case LYEXT_SUBSTMT_MIN:
1660 if (ext->parent_type == LYEXT_PAR_NODE) {
1661 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1662 return &((struct lys_node_list*)ext->parent)->min;
1663 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1664 return &((struct lys_node_leaflist*)ext->parent)->min;
1665 }
1666 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1667 return &((struct lys_refine*)ext->parent)->mod.list.min;
1668 }
1669 break;
1670 case LYEXT_SUBSTMT_NAMESPACE:
1671 if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) {
1672 return ((struct lys_module*)ext->parent)->ns;
1673 }
1674 break;
1675 case LYEXT_SUBSTMT_ORDEREDBY:
1676 if (ext->parent_type == LYEXT_PAR_NODE &&
1677 (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1678 return &((struct lys_node_list*)ext->parent)->flags;
1679 }
1680 break;
1681 case LYEXT_SUBSTMT_ORGANIZATION:
1682 if (ext->parent_type == LYEXT_PAR_MODULE) {
1683 return ((struct lys_module*)ext->parent)->org;
1684 }
1685 break;
1686 case LYEXT_SUBSTMT_PATH:
1687 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1688 return ((struct lys_type*)ext->parent)->info.lref.path;
1689 }
1690 break;
1691 case LYEXT_SUBSTMT_POSITION:
1692 if (ext->parent_type == LYEXT_PAR_TYPE_BIT) {
1693 return &((struct lys_type_bit*)ext->parent)->pos;
1694 }
1695 break;
1696 case LYEXT_SUBSTMT_PREFIX:
1697 if (ext->parent_type == LYEXT_PAR_MODULE) {
1698 /* covers also lys_submodule */
1699 return ((struct lys_module*)ext->parent)->prefix;
1700 } else if (ext->parent_type == LYEXT_PAR_IMPORT) {
1701 return ((struct lys_import*)ext->parent)->prefix;
1702 }
1703 break;
1704 case LYEXT_SUBSTMT_PRESENCE:
1705 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) {
1706 return ((struct lys_node_container*)ext->parent)->presence;
1707 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1708 return ((struct lys_refine*)ext->parent)->mod.presence;
1709 }
1710 break;
1711 case LYEXT_SUBSTMT_REFERENCE:
1712 switch (ext->parent_type) {
1713 case LYEXT_PAR_NODE:
1714 return ((struct lys_node*)ext->parent)->ref;
1715 case LYEXT_PAR_MODULE:
1716 return ((struct lys_module*)ext->parent)->ref;
1717 case LYEXT_PAR_IMPORT:
1718 return ((struct lys_import*)ext->parent)->ref;
1719 case LYEXT_PAR_INCLUDE:
1720 return ((struct lys_include*)ext->parent)->ref;
1721 case LYEXT_PAR_EXT:
1722 return ((struct lys_ext*)ext->parent)->ref;
1723 case LYEXT_PAR_FEATURE:
1724 return ((struct lys_feature*)ext->parent)->ref;
1725 case LYEXT_PAR_TPDF:
1726 return ((struct lys_tpdf*)ext->parent)->ref;
1727 case LYEXT_PAR_TYPE_BIT:
1728 return ((struct lys_type_bit*)ext->parent)->ref;
1729 case LYEXT_PAR_TYPE_ENUM:
1730 return ((struct lys_type_enum*)ext->parent)->ref;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001731 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001732 return ((struct lys_restr*)ext->parent)->ref;
1733 case LYEXT_PAR_WHEN:
1734 return ((struct lys_when*)ext->parent)->ref;
1735 case LYEXT_PAR_IDENT:
1736 return ((struct lys_ident*)ext->parent)->ref;
1737 case LYEXT_PAR_DEVIATION:
1738 return ((struct lys_deviation*)ext->parent)->ref;
1739 case LYEXT_PAR_REVISION:
1740 return ((struct lys_revision*)ext->parent)->ref;
1741 case LYEXT_PAR_REFINE:
1742 return ((struct lys_refine*)ext->parent)->ref;
1743 default:
1744 break;
1745 }
1746 break;
Radek Krejcibe336392017-02-07 10:54:24 +01001747 case LYEXT_SUBSTMT_REQINSTANCE:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001748 if (ext->parent_type == LYEXT_PAR_TYPE) {
1749 if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1750 return &((struct lys_type*)ext->parent)->info.lref.req;
1751 } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) {
1752 return &((struct lys_type*)ext->parent)->info.inst.req;
1753 }
1754 }
1755 break;
1756 case LYEXT_SUBSTMT_REVISIONDATE:
1757 if (ext->parent_type == LYEXT_PAR_IMPORT) {
1758 return ((struct lys_import*)ext->parent)->rev;
1759 } else if (ext->parent_type == LYEXT_PAR_INCLUDE) {
1760 return ((struct lys_include*)ext->parent)->rev;
1761 }
1762 break;
1763 case LYEXT_SUBSTMT_STATUS:
1764 switch (ext->parent_type) {
1765 case LYEXT_PAR_NODE:
1766 case LYEXT_PAR_IDENT:
1767 case LYEXT_PAR_TPDF:
1768 case LYEXT_PAR_EXT:
1769 case LYEXT_PAR_FEATURE:
1770 case LYEXT_PAR_TYPE_ENUM:
1771 case LYEXT_PAR_TYPE_BIT:
1772 /* in all structures the flags member is at the same offset */
1773 return &((struct lys_node*)ext->parent)->flags;
1774 default:
1775 break;
1776 }
1777 break;
1778 case LYEXT_SUBSTMT_UNIQUE:
1779 if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001780 return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001781 } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001782 return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001783 }
1784 break;
1785 case LYEXT_SUBSTMT_UNITS:
1786 if (ext->parent_type == LYEXT_PAR_NODE &&
1787 (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1788 /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */
1789 return ((struct lys_node_leaf*)ext->parent)->units;
1790 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1791 return ((struct lys_tpdf*)ext->parent)->units;
1792 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1793 return ((struct lys_deviate*)ext->parent)->units;
1794 }
1795 break;
1796 case LYEXT_SUBSTMT_VALUE:
1797 if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) {
1798 return &((struct lys_type_enum*)ext->parent)->value;
1799 }
1800 break;
1801 case LYEXT_SUBSTMT_YINELEM:
1802 if (ext->parent_type == LYEXT_PAR_EXT) {
1803 return &((struct lys_ext*)ext->parent)->flags;
1804 }
1805 break;
1806 }
1807 LOGINT;
1808 return NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01001809}
1810
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001811static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001812lys_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 +02001813 int in_grp, int shallow, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001814{
1815 int i;
1816
Michal Vasko1dca6882015-10-22 14:29:42 +02001817 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001818 new->base = old->base;
1819 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001820 new->parent = (struct lys_tpdf *)parent;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001821 new->ext_size = old->ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001822 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 +01001823 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01001824 }
Radek Krejci3733a802015-06-19 13:43:21 +02001825
Michal Vasko1c007172017-03-10 10:20:44 +01001826 i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001827 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001828 /* HACK (serious one) for unres */
1829 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001830 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001831 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp,
1832 shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001833 } else {
1834 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1835 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001836 /* all these unres additions can fail even though they did not before */
Michal Vasko1c007172017-03-10 10:20:44 +01001837 if (!new->der || (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent) == -1)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001838 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001839 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001840 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001841 }
1842
Radek Krejci5138e9f2017-04-12 13:10:46 +02001843 return type_dup(mod, parent, new, old, new->base, in_grp, shallow, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001844}
1845
1846void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001847lys_type_free(struct ly_ctx *ctx, struct lys_type *type,
1848 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02001849{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001850 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001851
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001852 assert(ctx);
1853 if (!type) {
1854 return;
1855 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001856
Michal Vasko1dca6882015-10-22 14:29:42 +02001857 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001858
Radek Krejci5138e9f2017-04-12 13:10:46 +02001859 lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01001860
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001861 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001862 case LY_TYPE_BINARY:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001863 lys_restr_free(ctx, type->info.binary.length, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001864 free(type->info.binary.length);
1865 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001866 case LY_TYPE_BITS:
1867 for (i = 0; i < type->info.bits.count; i++) {
1868 lydict_remove(ctx, type->info.bits.bit[i].name);
1869 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1870 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02001871 lys_iffeature_free(ctx, type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size,
1872 private_destructor);
1873 lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size,
1874 private_destructor);
Radek Krejci994b6f62015-06-18 16:47:27 +02001875 }
1876 free(type->info.bits.bit);
1877 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001878
1879 case LY_TYPE_DEC64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001880 lys_restr_free(ctx, type->info.dec64.range, private_destructor);
Radek Krejcif9401c32015-06-26 16:47:36 +02001881 free(type->info.dec64.range);
1882 break;
1883
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001884 case LY_TYPE_ENUM:
1885 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001886 lydict_remove(ctx, type->info.enums.enm[i].name);
1887 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1888 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02001889 lys_iffeature_free(ctx, type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size,
1890 private_destructor);
1891 lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size,
1892 private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001893 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001894 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001895 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001896
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001897 case LY_TYPE_INT8:
1898 case LY_TYPE_INT16:
1899 case LY_TYPE_INT32:
1900 case LY_TYPE_INT64:
1901 case LY_TYPE_UINT8:
1902 case LY_TYPE_UINT16:
1903 case LY_TYPE_UINT32:
1904 case LY_TYPE_UINT64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001905 lys_restr_free(ctx, type->info.num.range, private_destructor);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001906 free(type->info.num.range);
1907 break;
1908
Radek Krejcidc4c1412015-06-19 15:39:54 +02001909 case LY_TYPE_LEAFREF:
1910 lydict_remove(ctx, type->info.lref.path);
1911 break;
1912
Radek Krejci3733a802015-06-19 13:43:21 +02001913 case LY_TYPE_STRING:
Radek Krejci5138e9f2017-04-12 13:10:46 +02001914 lys_restr_free(ctx, type->info.str.length, private_destructor);
Radek Krejci3733a802015-06-19 13:43:21 +02001915 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001916 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001917 lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001918 }
1919 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001920 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001921
Radek Krejcie4c366b2015-07-02 10:11:31 +02001922 case LY_TYPE_UNION:
1923 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001924 lys_type_free(ctx, &type->info.uni.types[i], private_destructor);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001925 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001926 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001927 break;
1928
Michal Vaskod3282192016-09-05 11:27:57 +02001929 case LY_TYPE_IDENT:
1930 free(type->info.ident.ref);
1931 break;
1932
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001933 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001934 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001935 break;
1936 }
Radek Krejci5a065542015-05-22 15:02:07 +02001937}
1938
Radek Krejci1d82ef62015-08-07 14:44:40 +02001939static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001940lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf,
1941 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02001942{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001943 assert(ctx);
1944 if (!tpdf) {
1945 return;
1946 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001947
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001948 lydict_remove(ctx, tpdf->name);
1949 lydict_remove(ctx, tpdf->dsc);
1950 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001951
Radek Krejci5138e9f2017-04-12 13:10:46 +02001952 lys_type_free(ctx, &tpdf->type, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001953
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001954 lydict_remove(ctx, tpdf->units);
1955 lydict_remove(ctx, tpdf->dflt);
Radek Krejcie534c132016-11-23 13:32:31 +01001956
Radek Krejci5138e9f2017-04-12 13:10:46 +02001957 lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001958}
1959
Radek Krejci1d82ef62015-08-07 14:44:40 +02001960static struct lys_when *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001961lys_when_dup(struct lys_module *mod, struct lys_when *old, int shallow, struct unres_schema *unres)
Radek Krejci00768f42015-06-18 17:04:04 +02001962{
Radek Krejci76512572015-08-04 09:47:08 +02001963 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001964
1965 if (!old) {
1966 return NULL;
1967 }
1968
1969 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001970 if (!new) {
1971 LOGMEM;
1972 return NULL;
1973 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01001974 new->cond = lydict_insert(mod->ctx, old->cond, 0);
1975 new->dsc = lydict_insert(mod->ctx, old->dsc, 0);
1976 new->ref = lydict_insert(mod->ctx, old->ref, 0);
Radek Krejcif0bb3602017-01-25 17:05:08 +01001977 new->ext_size = old->ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001978 lys_ext_dup(mod, old->ext, old->ext_size, new, LYEXT_PAR_WHEN, &new->ext, shallow, unres);
Radek Krejci00768f42015-06-18 17:04:04 +02001979
1980 return new;
1981}
1982
Michal Vasko0308dd62015-10-07 09:14:40 +02001983void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001984lys_when_free(struct ly_ctx *ctx, struct lys_when *w,
1985 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001986{
1987 if (!w) {
1988 return;
1989 }
1990
Radek Krejci5138e9f2017-04-12 13:10:46 +02001991 lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001992 lydict_remove(ctx, w->cond);
1993 lydict_remove(ctx, w->dsc);
1994 lydict_remove(ctx, w->ref);
1995
1996 free(w);
1997}
1998
Radek Krejcib7f5e412015-08-13 10:15:51 +02001999static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002000lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug,
2001 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02002002{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002003 struct lys_node *next, *sub;
2004
Radek Krejcic071c542016-01-27 14:57:51 +01002005 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01002006 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002007 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002008 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002009 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002010 }
2011
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002012 lydict_remove(ctx, aug->target_name);
2013 lydict_remove(ctx, aug->dsc);
2014 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002015
Radek Krejci5138e9f2017-04-12 13:10:46 +02002016 lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, private_destructor);
2017 lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002018
Radek Krejci5138e9f2017-04-12 13:10:46 +02002019 lys_when_free(ctx, aug->when, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002020}
2021
Radek Krejci1d82ef62015-08-07 14:44:40 +02002022static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002023lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident,
2024 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci6793db02015-05-22 17:49:54 +02002025{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002026 assert(ctx);
2027 if (!ident) {
2028 return;
2029 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002030
Radek Krejci018f1f52016-08-03 16:01:20 +02002031 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02002032 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002033 lydict_remove(ctx, ident->name);
2034 lydict_remove(ctx, ident->dsc);
2035 lydict_remove(ctx, ident->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002036 lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, private_destructor);
2037 lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor);
Radek Krejci6793db02015-05-22 17:49:54 +02002038
2039}
2040
Radek Krejci1d82ef62015-08-07 14:44:40 +02002041static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002042lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp,
2043 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002044{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002045 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002046
Radek Krejcid12f57b2015-08-06 10:43:39 +02002047 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002048 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002049 lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002050 }
2051 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002052}
2053
Radek Krejci1d82ef62015-08-07 14:44:40 +02002054static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002055lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io,
2056 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcid12f57b2015-08-06 10:43:39 +02002057{
2058 int i;
2059
2060 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
2061 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002062 lys_tpdf_free(ctx, &io->tpdf[i], private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002063 }
2064 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002065
2066 for (i = 0; i < io->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002067 lys_restr_free(ctx, &io->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002068 }
2069 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002070}
2071
Radek Krejci1d82ef62015-08-07 14:44:40 +02002072static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002073lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif,
2074 void (*private_destructor)(const struct lys_node *node, void *priv))
Pavol Vican7cff97e2016-08-09 14:56:08 +02002075{
2076 int i;
2077
2078 for (i = 0; i < notif->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002079 lys_restr_free(ctx, &notif->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002080 }
2081 free(notif->must);
2082
2083 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002084 lys_tpdf_free(ctx, &notif->tpdf[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002085 }
2086 free(notif->tpdf);
2087}
2088static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002089lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml,
2090 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci537cf382015-06-04 11:07:01 +02002091{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002092 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002093
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002094 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002095 lys_restr_free(ctx, &anyxml->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002096 }
2097 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002098
Radek Krejci5138e9f2017-04-12 13:10:46 +02002099 lys_when_free(ctx, anyxml->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002100}
2101
Radek Krejci1d82ef62015-08-07 14:44:40 +02002102static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002103lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf,
2104 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002105{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002106 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002107
Radek Krejci85a54be2016-10-20 12:39:56 +02002108 /* leafref backlinks */
2109 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002110
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002111 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002112 lys_restr_free(ctx, &leaf->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 }
2114 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002115
Radek Krejci5138e9f2017-04-12 13:10:46 +02002116 lys_when_free(ctx, leaf->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002117
Radek Krejci5138e9f2017-04-12 13:10:46 +02002118 lys_type_free(ctx, &leaf->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002119 lydict_remove(ctx, leaf->units);
2120 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02002121}
2122
Radek Krejci1d82ef62015-08-07 14:44:40 +02002123static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002124lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist,
2125 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002126{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002127 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002128
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002129 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002130 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002131 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002132 }
2133
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002134 for (i = 0; i < llist->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002135 lys_restr_free(ctx, &llist->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002136 }
2137 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002138
Pavol Vican38321d02016-08-16 14:56:02 +02002139 for (i = 0; i < llist->dflt_size; i++) {
2140 lydict_remove(ctx, llist->dflt[i]);
2141 }
2142 free(llist->dflt);
2143
Radek Krejci5138e9f2017-04-12 13:10:46 +02002144 lys_when_free(ctx, llist->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002145
Radek Krejci5138e9f2017-04-12 13:10:46 +02002146 lys_type_free(ctx, &llist->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002147 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02002148}
2149
Radek Krejci1d82ef62015-08-07 14:44:40 +02002150static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002151lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list,
2152 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002153{
Radek Krejci581ce772015-11-10 17:22:40 +01002154 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002155
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002156 /* handle only specific parts for LY_NODE_LIST */
2157 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002158 lys_tpdf_free(ctx, &list->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002159 }
2160 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002161
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002162 for (i = 0; i < list->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002163 lys_restr_free(ctx, &list->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002164 }
2165 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002166
Radek Krejci5138e9f2017-04-12 13:10:46 +02002167 lys_when_free(ctx, list->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002168
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002169 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02002170 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002171 lydict_remove(ctx, list->unique[i].expr[j]);
2172 }
2173 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002174 }
2175 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02002176
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002177 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002178}
2179
Radek Krejci1d82ef62015-08-07 14:44:40 +02002180static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002181lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont,
2182 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002183{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002184 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002185
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002186 /* handle only specific parts for LY_NODE_CONTAINER */
2187 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02002188
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002189 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002190 lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002191 }
2192 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02002193
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002194 for (i = 0; i < cont->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002195 lys_restr_free(ctx, &cont->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002196 }
2197 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002198
Radek Krejci5138e9f2017-04-12 13:10:46 +02002199 lys_when_free(ctx, cont->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002200}
2201
Radek Krejci1d82ef62015-08-07 14:44:40 +02002202static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002203lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f,
2204 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci3cf9e222015-06-18 11:37:50 +02002205{
2206 lydict_remove(ctx, f->name);
2207 lydict_remove(ctx, f->dsc);
2208 lydict_remove(ctx, f->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002209 lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, private_destructor);
Radek Krejci9de2c042016-10-19 16:53:06 +02002210 ly_set_free(f->depfeatures);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002211 lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002212}
2213
2214static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002215lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e,
2216 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie534c132016-11-23 13:32:31 +01002217{
2218 lydict_remove(ctx, e->name);
2219 lydict_remove(ctx, e->dsc);
2220 lydict_remove(ctx, e->ref);
2221 lydict_remove(ctx, e->argument);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002222 lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002223}
2224
Radek Krejci1d82ef62015-08-07 14:44:40 +02002225static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002226lys_deviation_free(struct lys_module *module, struct lys_deviation *dev,
2227 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcieb00f512015-07-01 16:44:58 +02002228{
Radek Krejci581ce772015-11-10 17:22:40 +01002229 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01002230 struct ly_ctx *ctx;
2231 struct lys_node *next, *elem;
2232
2233 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02002234
2235 lydict_remove(ctx, dev->target_name);
2236 lydict_remove(ctx, dev->dsc);
2237 lydict_remove(ctx, dev->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002238 lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002239
Pavol Vican64d0b762016-08-25 10:44:59 +02002240 if (!dev->deviate) {
2241 return ;
2242 }
2243
Michal Vaskoff006c12016-02-17 11:15:19 +01002244 /* the module was freed, but we only need the context from orig_node, use ours */
2245 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2246 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
2247 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
2248 elem->module = module;
2249
2250 LY_TREE_DFS_END(dev->orig_node, next, elem);
2251 }
2252 lys_node_free(dev->orig_node, NULL, 0);
2253 } else {
2254 /* it's just a shallow copy, freeing one node */
2255 dev->orig_node->module = module;
2256 lys_node_free(dev->orig_node, NULL, 1);
2257 }
2258
Radek Krejcieb00f512015-07-01 16:44:58 +02002259 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002260 lys_extension_instances_free(ctx, dev->deviate[i].ext, dev->deviate[i].ext_size, private_destructor);
Radek Krejci3c4b0ea2017-01-24 16:00:47 +01002261
Radek Krejcid5a5c282016-08-15 15:38:08 +02002262 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02002263 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02002264 }
2265 free(dev->deviate[i].dflt);
2266
Radek Krejcieb00f512015-07-01 16:44:58 +02002267 lydict_remove(ctx, dev->deviate[i].units);
2268
2269 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
2270 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002271 lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002272 }
2273 free(dev->deviate[i].must);
2274
2275 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002276 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
2277 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
2278 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01002279 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002280 }
2281 free(dev->deviate[i].unique);
2282 }
2283 }
2284 free(dev->deviate);
2285}
2286
Radek Krejci1d82ef62015-08-07 14:44:40 +02002287static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002288lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses,
2289 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02002290{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002291 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02002292
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002293 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002294 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002295 lydict_remove(ctx, uses->refine[i].dsc);
2296 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002297
Michal Vaskoa275c0a2015-09-24 09:55:42 +02002298 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002299 lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002300 }
2301 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002302
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002303 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02002304 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002305 }
2306 free(uses->refine[i].dflt);
2307
Radek Krejci5138e9f2017-04-12 13:10:46 +02002308 lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002309
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002310 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002311 lydict_remove(ctx, uses->refine[i].mod.presence);
2312 }
2313 }
2314 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002315
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002316 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002317 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002318 }
2319 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002320
Radek Krejci5138e9f2017-04-12 13:10:46 +02002321 lys_when_free(ctx, uses->when, private_destructor);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002322}
2323
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002324void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002325lys_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 +02002326{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002327 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02002328 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002329
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002330 if (!node) {
2331 return;
2332 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002333
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002334 assert(node->module);
2335 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02002336
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002337 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002338
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002339 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002340 if (node->priv && private_destructor) {
2341 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002342 }
2343
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002344 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002345 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002346 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002347 lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002348 lydict_remove(ctx, node->dsc);
2349 lydict_remove(ctx, node->ref);
2350 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002351
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002352 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002353 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002354 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002355 }
2356 }
2357
Radek Krejci5138e9f2017-04-12 13:10:46 +02002358 lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002359
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002360 /* specific part */
2361 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002362 case LYS_CONTAINER:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002363 lys_container_free(ctx, (struct lys_node_container *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002364 break;
Radek Krejci76512572015-08-04 09:47:08 +02002365 case LYS_CHOICE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002366 lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002367 break;
Radek Krejci76512572015-08-04 09:47:08 +02002368 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002369 lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002370 break;
Radek Krejci76512572015-08-04 09:47:08 +02002371 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002372 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002373 break;
Radek Krejci76512572015-08-04 09:47:08 +02002374 case LYS_LIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002375 lys_list_free(ctx, (struct lys_node_list *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002376 break;
Radek Krejci76512572015-08-04 09:47:08 +02002377 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002378 case LYS_ANYDATA:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002379 lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002380 break;
Radek Krejci76512572015-08-04 09:47:08 +02002381 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002382 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002383 break;
Radek Krejci76512572015-08-04 09:47:08 +02002384 case LYS_CASE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002385 lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002386 break;
Radek Krejci76512572015-08-04 09:47:08 +02002387 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002388 /* do nothing */
2389 break;
Radek Krejci76512572015-08-04 09:47:08 +02002390 case LYS_GROUPING:
2391 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002392 case LYS_ACTION:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002393 lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002394 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02002395 case LYS_NOTIF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002396 lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002397 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002398 case LYS_INPUT:
2399 case LYS_OUTPUT:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002400 lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002401 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01002402 case LYS_EXT:
Michal Vasko591e0b22015-08-13 13:53:43 +02002403 case LYS_UNKNOWN:
2404 LOGINT;
2405 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002406 }
Radek Krejci5a065542015-05-22 15:02:07 +02002407
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002408 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002409 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002410 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002411}
2412
Radek Krejci2eee5c02016-12-06 19:18:05 +01002413API struct lys_module *
2414lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02002415{
2416 struct ly_ctx *ctx;
2417 int i;
2418
2419 if (!mod || mod->implemented) {
2420 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002421 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002422 }
2423
2424 ctx = mod->ctx;
2425 for (i = 0; i < ctx->models.used; i++) {
2426 if (!ctx->models.list[i]->implemented) {
2427 continue;
2428 }
2429
2430 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2431 /* we have some revision of the module implemented */
2432 return ctx->models.list[i];
2433 }
2434 }
2435
2436 /* we have no revision of the module implemented, return the module itself,
2437 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002438 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002439}
2440
2441const struct lys_module *
Radek Krejcie534c132016-11-23 13:32:31 +01002442lys_get_import_module_ns(const struct lys_module *module, const char *ns)
2443{
2444 int i;
2445
2446 assert(module && ns);
2447
Radek Krejcic789d692017-01-11 11:31:14 +01002448 if (module->type) {
2449 /* the module is actually submodule and to get the namespace, we need the main module */
2450 if (ly_strequal(((struct lys_submodule *)module)->belongsto->ns, ns, 0)) {
2451 return ((struct lys_submodule *)module)->belongsto;
2452 }
2453 } else {
2454 /* modul's own namespace */
2455 if (ly_strequal(module->ns, ns, 0)) {
2456 return module;
2457 }
Radek Krejcie534c132016-11-23 13:32:31 +01002458 }
2459
2460 /* imported modules */
2461 for (i = 0; i < module->imp_size; ++i) {
2462 if (ly_strequal(module->imp[i].module->ns, ns, 0)) {
2463 return module->imp[i].module;
2464 }
2465 }
2466
2467 return NULL;
2468}
2469
2470const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01002471lys_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 +02002472{
Radek Krejcic071c542016-01-27 14:57:51 +01002473 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02002474 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002475 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002476
Michal Vaskoa7789a82016-02-11 15:42:55 +01002477 assert(!prefix || !name);
2478
Michal Vaskob6729c62015-10-21 12:09:47 +02002479 if (prefix && !pref_len) {
2480 pref_len = strlen(prefix);
2481 }
2482 if (name && !name_len) {
2483 name_len = strlen(name);
2484 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002485
Radek Krejcic4283442016-04-22 09:19:27 +02002486 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002487
2488 /* module own prefix, submodule own prefix, (sub)module own name */
2489 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2490 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002491 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002492 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002493 }
2494
Michal Vasko89563fc2016-07-28 16:19:35 +02002495 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002496 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002497 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2498 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002499 return module->imp[i].module;
2500 }
2501 }
2502
Michal Vasko89563fc2016-07-28 16:19:35 +02002503 /* module required by a foreign grouping, deviation, or submodule */
2504 if (name) {
2505 str = strndup(name, name_len);
2506 if (!str) {
2507 LOGMEM;
2508 return NULL;
2509 }
2510 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2511 free(str);
2512 return main_module;
2513 }
2514
Michal Vasko8ce24d72015-10-21 11:27:26 +02002515 return NULL;
2516}
2517
Michal Vasko13b15832015-08-19 11:04:48 +02002518/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002519static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002520module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002521{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002522 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002523 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002524 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002525
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002526 assert(module->ctx);
2527 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002528
Michal Vaskob746fff2016-02-11 11:37:50 +01002529 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002530 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002531 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002532 lydict_remove(ctx, module->imp[i].dsc);
2533 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002534 lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size, private_destructor);
Radek Krejci225376f2016-02-16 17:36:22 +01002535 }
Radek Krejcidce51452015-06-16 15:20:08 +02002536 free(module->imp);
2537
Radek Krejcic071c542016-01-27 14:57:51 +01002538 /* submodules don't have data tree, the data nodes
2539 * are placed in the main module altogether */
2540 if (!module->type) {
2541 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002542 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002543 }
Radek Krejci21181962015-06-30 14:11:00 +02002544 }
Radek Krejci5a065542015-05-22 15:02:07 +02002545
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002546 lydict_remove(ctx, module->dsc);
2547 lydict_remove(ctx, module->ref);
2548 lydict_remove(ctx, module->org);
2549 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002550 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002551
Radek Krejcieb00f512015-07-01 16:44:58 +02002552 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002553 for (i = 0; i < module->rev_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002554 lys_extension_instances_free(ctx, module->rev[i].ext, module->rev[i].ext_size, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002555 lydict_remove(ctx, module->rev[i].dsc);
2556 lydict_remove(ctx, module->rev[i].ref);
2557 }
2558 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002559
Radek Krejcieb00f512015-07-01 16:44:58 +02002560 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002561 for (i = 0; i < module->ident_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002562 lys_ident_free(ctx, &module->ident[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002563 }
2564 module->ident_size = 0;
2565 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002566
Radek Krejcieb00f512015-07-01 16:44:58 +02002567 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002568 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002569 lys_tpdf_free(ctx, &module->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002570 }
2571 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002572
Radek Krejcie534c132016-11-23 13:32:31 +01002573 /* extension instances */
Radek Krejci5138e9f2017-04-12 13:10:46 +02002574 lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002575
Radek Krejcieb00f512015-07-01 16:44:58 +02002576 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002577 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002578 lydict_remove(ctx, module->inc[i].dsc);
2579 lydict_remove(ctx, module->inc[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002580 lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002581 /* complete submodule free is done only from main module since
2582 * submodules propagate their includes to the main module */
2583 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002584 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002585 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002586 }
2587 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002588
Radek Krejcieb00f512015-07-01 16:44:58 +02002589 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002590 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002591 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002592 }
2593 free(module->augment);
2594
Radek Krejcieb00f512015-07-01 16:44:58 +02002595 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002596 for (i = 0; i < module->features_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002597 lys_feature_free(ctx, &module->features[i], private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002598 }
2599 free(module->features);
2600
Radek Krejcieb00f512015-07-01 16:44:58 +02002601 /* deviations */
2602 for (i = 0; i < module->deviation_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002603 lys_deviation_free(module, &module->deviation[i], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002604 }
2605 free(module->deviation);
2606
Radek Krejcie534c132016-11-23 13:32:31 +01002607 /* extensions */
2608 for (i = 0; i < module->extensions_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002609 lys_extension_free(ctx, &module->extensions[i], private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002610 }
2611 free(module->extensions);
2612
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002613 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002614 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002615}
2616
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002617void
Michal Vaskob746fff2016-02-11 11:37:50 +01002618lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002619{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002620 if (!submodule) {
2621 return;
2622 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002623
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002624 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002625 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002626
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002627 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002628
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002629 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002630}
2631
Radek Krejci3a5501d2016-07-18 22:03:34 +02002632static int
2633ingrouping(const struct lys_node *node)
2634{
2635 const struct lys_node *iter = node;
2636 assert(node);
2637
2638 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2639 if (!iter) {
2640 return 0;
2641 } else {
2642 return 1;
2643 }
2644}
2645
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002646/*
2647 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2648 */
2649static struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01002650lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002651 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002652{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002653 struct lys_node *retval = NULL, *iter, *p;
Michal Vaskofe31cc02017-03-10 15:52:31 +01002654 struct lys_module *tmp_mod;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002655 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002656 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002657 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002658 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002659 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002660
Michal Vaskoc07187d2015-08-13 15:20:57 +02002661 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002662 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002663 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002664 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002665 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002666 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002667 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002668 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002669 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002670 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002671 struct lys_node_anydata *any = NULL;
2672 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002673 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002674 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002675 struct lys_node_rpc_action *rpc = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02002676 struct lys_node_inout *io = NULL;
Radek Krejcic43151c2017-03-12 07:10:52 +01002677 struct lys_node_notif *ntf = NULL;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002678 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002679 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002680
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002681 /* we cannot just duplicate memory since the strings are stored in
2682 * dictionary and we need to update dictionary counters.
2683 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002684
Radek Krejci1d82ef62015-08-07 14:44:40 +02002685 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002686 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002687 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002688 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002689 break;
2690
Radek Krejci76512572015-08-04 09:47:08 +02002691 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002692 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002693 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002694 break;
2695
Radek Krejci76512572015-08-04 09:47:08 +02002696 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002697 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002698 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002699 break;
2700
Radek Krejci76512572015-08-04 09:47:08 +02002701 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002702 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002703 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002704 break;
2705
Radek Krejci76512572015-08-04 09:47:08 +02002706 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002707 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002708 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002709 break;
2710
Radek Krejci76512572015-08-04 09:47:08 +02002711 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002712 case LYS_ANYDATA:
2713 any = calloc(1, sizeof *any);
2714 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002715 break;
2716
Radek Krejci76512572015-08-04 09:47:08 +02002717 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002718 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002719 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002720 break;
2721
Radek Krejci76512572015-08-04 09:47:08 +02002722 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002723 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002724 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002725 break;
2726
Radek Krejci76512572015-08-04 09:47:08 +02002727 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002728 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002729 rpc = calloc(1, sizeof *rpc);
2730 retval = (struct lys_node *)rpc;
2731 break;
2732
Radek Krejci76512572015-08-04 09:47:08 +02002733 case LYS_INPUT:
2734 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002735 io = calloc(1, sizeof *io);
2736 retval = (struct lys_node *)io;
2737 break;
2738
Radek Krejci76512572015-08-04 09:47:08 +02002739 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002740 ntf = calloc(1, sizeof *ntf);
2741 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002742 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002743
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002744 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002745 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002746 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002747 }
Radek Krejcib388c152015-06-04 17:03:03 +02002748
Michal Vasko253035f2015-12-17 16:58:13 +01002749 if (!retval) {
2750 LOGMEM;
2751 return NULL;
2752 }
2753
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002754 /*
2755 * duplicate generic part of the structure
2756 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002757 retval->name = lydict_insert(ctx, node->name, 0);
2758 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2759 retval->ref = lydict_insert(ctx, node->ref, 0);
2760 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002761
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002762 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002763 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002764
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002765 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002766
Radek Krejcif0bb3602017-01-25 17:05:08 +01002767 retval->ext_size = node->ext_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02002768 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 +01002769 goto error;
2770 }
2771
Radek Krejci06214042016-11-04 16:25:58 +01002772 if (node->iffeature_size) {
2773 retval->iffeature_size = node->iffeature_size;
2774 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2775 if (!retval->iffeature) {
2776 LOGMEM;
2777 goto error;
2778 }
Michal Vasko253035f2015-12-17 16:58:13 +01002779 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002780
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002781 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002782 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002783 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2784 if (size1) {
2785 /* there is something to duplicate */
2786
2787 /* duplicate compiled expression */
2788 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2789 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2790 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2791
2792 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002793 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002794 for (j = 0; (unsigned int)j < size2; j++) {
2795 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2796 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002797 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002798 /* feature is resolved in origin, so copy it
2799 * - duplication is used for instantiating groupings
2800 * and if-feature inside grouping is supposed to be
2801 * resolved inside the original grouping, so we want
2802 * to keep pointers to features from the grouping
2803 * context */
2804 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2805 } else if (rc == -1) {
2806 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002807 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002808 }
2809 }
Radek Krejcif0bb3602017-01-25 17:05:08 +01002810
2811 /* duplicate if-feature's extensions */
2812 retval->iffeature[i].ext_size = node->iffeature[i].ext_size;
Radek Krejci8d6b7422017-02-03 14:42:13 +01002813 if (lys_ext_dup(module, node->iffeature[i].ext, node->iffeature[i].ext_size,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002814 &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002815 goto error;
2816 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002817 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002818
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002819 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002820 p = parent;
2821 do {
2822 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2823 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002824 if (iter) {
2825 flags = iter->flags & LYS_CONFIG_MASK;
2826 } else {
2827 /* default */
2828 flags = LYS_CONFIG_W;
2829 }
2830
2831 switch (finalize) {
2832 case 1:
2833 /* inherit config flags */
2834 if (retval->flags & LYS_CONFIG_SET) {
2835 /* skip nodes with an explicit config value */
2836 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2837 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +01002838 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002839 goto error;
2840 }
2841 break;
2842 }
2843
2844 if (retval->nodetype != LYS_USES) {
2845 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2846 }
2847 break;
2848 case 2:
2849 /* erase config flags */
2850 retval->flags &= ~LYS_CONFIG_MASK;
2851 retval->flags &= ~LYS_CONFIG_SET;
2852 break;
2853 }
2854
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002855 /* connect it to the parent */
2856 if (lys_node_addchild(parent, retval->module, retval)) {
2857 goto error;
2858 }
Radek Krejcidce51452015-06-16 15:20:08 +02002859
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002860 /* go recursively */
2861 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002862 LY_TREE_FOR(node->child, iter) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002863 if (iter->nodetype & LYS_GROUPING) {
2864 /* do not instantiate groupings */
2865 continue;
2866 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01002867 if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002868 goto error;
2869 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002870 }
2871 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002872
2873 if (finalize == 1) {
2874 /* check that configuration lists have keys
2875 * - we really want to check keys_size in original node, because the keys are
2876 * not yet resolved here, it is done below in nodetype specific part */
2877 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2878 && !((struct lys_node_list *)node)->keys_size) {
2879 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2880 goto error;
2881 }
2882 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002883 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002884 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002885 }
2886
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002887 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002888 * duplicate specific part of the structure
2889 */
2890 switch (node->nodetype) {
2891 case LYS_CONTAINER:
2892 if (cont_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002893 cont->when = lys_when_dup(module, cont_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002894 }
2895 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002896
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002897 cont->must_size = cont_orig->must_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02002898 cont->must = lys_restr_dup(module, cont_orig->must, cont->must_size, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002899
Radek Krejcif0bb3602017-01-25 17:05:08 +01002900 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
2901
2902 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002903 case LYS_CHOICE:
2904 if (choice_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002905 choice->when = lys_when_dup(module, choice_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002906 }
2907
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002908 if (!shallow) {
2909 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002910 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2911 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2912 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002913 if (rc) {
2914 if (rc == EXIT_FAILURE) {
2915 LOGINT;
2916 }
2917 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002918 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002919 } else {
2920 /* useless to check return value, we don't know whether
2921 * there really wasn't any default defined or it just hasn't
2922 * been resolved, we just hope for the best :)
2923 */
2924 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002925 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002926 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002927 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002928 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002929 break;
2930
2931 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002932 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002933 goto error;
2934 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002935 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2936
2937 if (leaf_orig->dflt) {
2938 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Michal Vaskofe31cc02017-03-10 15:52:31 +01002939 if (!ingrouping(retval) || (leaf->type.base != LY_TYPE_LEAFREF)) {
Radek Krejci968f6942017-03-25 15:55:01 -05002940 /* problem is when it is an identityref referencing an identity from a module
2941 * and we are using the grouping in a different module */
Radek Krejcid06f5a42017-03-25 16:03:42 -05002942 if (leaf->type.base == LY_TYPE_IDENT) {
Michal Vaskofe31cc02017-03-10 15:52:31 +01002943 tmp_mod = leaf_orig->module;
2944 } else {
2945 tmp_mod = module;
2946 }
2947 if (unres_schema_add_node(tmp_mod, unres, &leaf->type, UNRES_TYPE_DFLT,
2948 (struct lys_node *)(&leaf->dflt)) == -1) {
2949 goto error;
2950 }
Michal Vasko49168a22015-08-17 16:35:41 +02002951 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002952 }
2953
2954 leaf->must_size = leaf_orig->must_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02002955 leaf->must = lys_restr_dup(module, leaf_orig->must, leaf->must_size, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002956
2957 if (leaf_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002958 leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002959 }
2960 break;
2961
2962 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002963 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002964 goto error;
2965 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002966 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2967
2968 llist->min = llist_orig->min;
2969 llist->max = llist_orig->max;
2970
2971 llist->must_size = llist_orig->must_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02002972 llist->must = lys_restr_dup(module, llist_orig->must, llist->must_size, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002973
Radek Krejci51673202016-11-01 17:00:32 +01002974 llist->dflt_size = llist_orig->dflt_size;
2975 llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt);
2976 for (i = 0; i < llist->dflt_size; i++) {
2977 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
Michal Vaskofe31cc02017-03-10 15:52:31 +01002978 if (!ingrouping(retval) || (llist->type.base != LY_TYPE_LEAFREF)) {
2979 if ((llist->type.base == LY_TYPE_IDENT) && !strchr(llist->dflt[i], ':') && (module != llist_orig->module)) {
2980 tmp_mod = llist_orig->module;
2981 } else {
2982 tmp_mod = module;
2983 }
2984 if (unres_schema_add_node(tmp_mod, unres, &llist->type, UNRES_TYPE_DFLT,
2985 (struct lys_node *)(&llist->dflt[i])) == -1) {
2986 goto error;
2987 }
Radek Krejci51673202016-11-01 17:00:32 +01002988 }
2989 }
2990
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002991 if (llist_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002992 llist->when = lys_when_dup(module, llist_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002993 }
2994 break;
2995
2996 case LYS_LIST:
2997 list->min = list_orig->min;
2998 list->max = list_orig->max;
2999
3000 list->must_size = list_orig->must_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02003001 list->must = lys_restr_dup(module, list_orig->must, list->must_size, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003002
Radek Krejcif0bb3602017-01-25 17:05:08 +01003003 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Michal Vasko38d01f72015-06-15 09:41:06 +02003004
Radek Krejci581ce772015-11-10 17:22:40 +01003005 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02003006 if (list->keys_size) {
3007 list->keys = calloc(list->keys_size, sizeof *list->keys);
Radek Krejci5c08a992016-11-02 13:30:04 +01003008 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Michal Vasko253035f2015-12-17 16:58:13 +01003009 if (!list->keys) {
3010 LOGMEM;
3011 goto error;
3012 }
Michal Vasko0ea41032015-06-16 08:53:55 +02003013
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003014 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01003015 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
3016 * in another grouping */
3017 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
3018 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
3019 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02003020 }
Michal Vasko0ea41032015-06-16 08:53:55 +02003021 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003022 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02003023 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003024 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003025
Radek Krejci581ce772015-11-10 17:22:40 +01003026 list->unique_size = list_orig->unique_size;
3027 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01003028 if (!list->unique) {
3029 LOGMEM;
3030 goto error;
3031 }
Radek Krejci581ce772015-11-10 17:22:40 +01003032 for (i = 0; i < list->unique_size; ++i) {
3033 list->unique[i].expr_size = list_orig->unique[i].expr_size;
3034 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01003035 if (!list->unique[i].expr) {
3036 LOGMEM;
3037 goto error;
3038 }
Radek Krejci581ce772015-11-10 17:22:40 +01003039 for (j = 0; j < list->unique[i].expr_size; j++) {
3040 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
3041
3042 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02003043 unique_info = malloc(sizeof *unique_info);
3044 unique_info->list = (struct lys_node *)list;
3045 unique_info->expr = list->unique[i].expr[j];
3046 unique_info->trg_type = &list->unique[i].trg_type;
3047 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003048 }
3049 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003050
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003051 if (list_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003052 list->when = lys_when_dup(module, list_orig->when, shallow, unres);
Radek Krejciefaeba32015-05-27 14:30:57 +02003053 }
Radek Krejcidce51452015-06-16 15:20:08 +02003054 break;
3055
3056 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02003057 case LYS_ANYDATA:
3058 any->must_size = any_orig->must_size;
Radek Krejci5138e9f2017-04-12 13:10:46 +02003059 any->must = lys_restr_dup(module, any_orig->must, any->must_size, shallow, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003060
Radek Krejcibf2abff2016-08-23 15:51:52 +02003061 if (any_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003062 any->when = lys_when_dup(module, any_orig->when, shallow, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003063 }
3064 break;
3065
3066 case LYS_USES:
3067 uses->grp = uses_orig->grp;
3068
3069 if (uses_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003070 uses->when = lys_when_dup(module, uses_orig->when, shallow, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003071 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01003072 /* it is not needed to duplicate refine, nor augment. They are already applied to the uses children */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003073 break;
3074
3075 case LYS_CASE:
3076 if (cs_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003077 cs->when = lys_when_dup(module, cs_orig->when, shallow, unres);
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);
Michal Vasko253035f2015-12-17 16:58:13 +01003538 if (!result) {
3539 LOGMEM;
3540 return NULL;
3541 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003542 if (states) {
3543 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01003544 if (!(*states)) {
3545 LOGMEM;
3546 free(result);
3547 return NULL;
3548 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003549 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003550 count = 0;
3551
3552 /* module itself */
3553 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003554 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003555 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003556 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003557 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003558 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003559 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003560 }
3561 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003562 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003563 }
3564
3565 /* submodules */
3566 for (j = 0; j < module->inc_size; j++) {
3567 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003568 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003569 if (states) {
3570 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3571 (*states)[count] = 1;
3572 } else {
3573 (*states)[count] = 0;
3574 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003575 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003576 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003577 }
3578 }
3579
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003580 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003581 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003582
3583 return result;
3584}
Michal Vaskobaefb032015-09-24 14:52:10 +02003585
Radek Krejci6910a032016-04-13 10:06:21 +02003586API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003587lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003588{
Michal Vaskof53187d2017-01-13 13:23:14 +01003589 if (!node) {
3590 return NULL;
3591 }
3592
Radek Krejcic071c542016-01-27 14:57:51 +01003593 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3594}
3595
Radek Krejci6910a032016-04-13 10:06:21 +02003596API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003597lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003598{
Michal Vaskof53187d2017-01-13 13:23:14 +01003599 if (!module) {
3600 return NULL;
3601 }
3602
Michal Vasko320e8532016-02-15 13:11:57 +01003603 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3604}
3605
Michal Vaskobaefb032015-09-24 14:52:10 +02003606API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003607lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003608{
Radek Krejcif95b6292017-02-13 15:57:37 +01003609 struct lys_node *parent;
3610
3611 if (!node) {
Michal Vaskobaefb032015-09-24 14:52:10 +02003612 return NULL;
3613 }
3614
Radek Krejcif95b6292017-02-13 15:57:37 +01003615 if (node->nodetype == LYS_EXT) {
3616 if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) {
3617 return NULL;
3618 }
3619 parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent;
3620 } else if (!node->parent) {
3621 return NULL;
3622 } else {
3623 parent = node->parent;
Michal Vaskobaefb032015-09-24 14:52:10 +02003624 }
3625
Radek Krejcif95b6292017-02-13 15:57:37 +01003626 if (parent->nodetype == LYS_AUGMENT) {
3627 return ((struct lys_node_augment *)parent)->target;
3628 } else {
3629 return parent;
3630 }
3631}
3632
3633struct lys_node **
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003634lys_child(const struct lys_node *node, LYS_NODE nodetype)
Radek Krejcif95b6292017-02-13 15:57:37 +01003635{
3636 void *pp;
3637 assert(node);
3638
3639 if (node->nodetype == LYS_EXT) {
3640 pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL);
3641 if (!pp) {
3642 return NULL;
3643 }
3644 return (struct lys_node **)pp;
3645 } else {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003646 return (struct lys_node **)&node->child;
Radek Krejcif95b6292017-02-13 15:57:37 +01003647 }
Michal Vaskobaefb032015-09-24 14:52:10 +02003648}
Michal Vasko1b229152016-01-13 11:28:38 +01003649
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003650API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003651lys_set_private(const struct lys_node *node, void *priv)
3652{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003653 void *prev;
3654
Michal Vasko1b229152016-01-13 11:28:38 +01003655 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003656 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
3657 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003658 }
3659
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003660 prev = node->priv;
3661 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003662
3663 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003664}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003665
Michal Vasko01c6fd22016-05-20 11:43:05 +02003666int
3667lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3668{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003669 struct lys_node_leaf *iter = leafref_target;
3670
Michal Vasko48a573d2016-07-01 11:46:02 +02003671 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003672 LOGINT;
3673 return -1;
3674 }
3675
Pavol Vican93175152016-08-30 15:34:44 +02003676 /* check for config flag */
Radek Krejcic688ca02017-03-20 12:54:39 +01003677 if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 &&
3678 (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
Pavol Vican93175152016-08-30 15:34:44 +02003679 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
Radek Krejcid831dd42017-03-16 12:59:30 +01003680 "The leafref %s is config but refers to a non-config %s.",
Pavol Vican93175152016-08-30 15:34:44 +02003681 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3682 return -1;
3683 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003684 /* check for cycles */
3685 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3686 if ((void *)iter == (void *)leafref) {
3687 /* cycle detected */
3688 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3689 return -1;
3690 }
3691 iter = iter->type.info.lref.target;
3692 }
3693
3694 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003695 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003696 if (!leafref_target->backlinks) {
3697 leafref_target->backlinks = (void*)ly_set_new();
3698 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003699 LOGMEM;
3700 return -1;
3701 }
3702 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003703 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003704
3705 return 0;
3706}
3707
Michal Vasko8548e082016-07-22 12:00:18 +02003708/* not needed currently */
3709#if 0
3710
Michal Vasko5b3492c2016-07-20 09:37:40 +02003711static const char *
3712lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3713{
3714 struct lys_module *prev_mod;
3715 uint32_t str_len, mod_len, buf_idx;
3716
Radek Krejcibf2abff2016-08-23 15:51:52 +02003717 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003718 LOGINT;
3719 return NULL;
3720 }
3721
3722 buf_idx = buf_len - 1;
3723 buf[buf_idx] = '\0';
3724
3725 while (node) {
3726 if (lys_parent(node)) {
3727 prev_mod = lys_node_module(lys_parent(node));
3728 } else {
3729 prev_mod = NULL;
3730 }
3731
Radek Krejcibf2abff2016-08-23 15:51:52 +02003732 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003733 str_len = strlen(node->name);
3734
3735 if (prev_mod != node->module) {
3736 mod_len = strlen(node->module->name);
3737 } else {
3738 mod_len = 0;
3739 }
3740
3741 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3742 LOGINT;
3743 return NULL;
3744 }
3745
3746 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3747
3748 buf[buf_idx] = '/';
3749 if (mod_len) {
3750 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3751 buf[buf_idx + 1 + mod_len] = ':';
3752 }
3753 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3754 }
3755
3756 node = lys_parent(node);
3757 }
3758
3759 return buf + buf_idx;
3760}
3761
Michal Vasko8548e082016-07-22 12:00:18 +02003762#endif
3763
3764API struct ly_set *
Michal Vasko2611e192017-01-23 10:33:21 +01003765lys_find_xpath(struct ly_ctx *ctx, const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003766{
3767 struct lyxp_set set;
3768 struct ly_set *ret_set;
3769 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003770 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003771
Michal Vasko2611e192017-01-23 10:33:21 +01003772 if ((!ctx && !node) || !expr) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003773 ly_errno = LY_EINVAL;
3774 return NULL;
3775 }
3776
Michal Vasko2611e192017-01-23 10:33:21 +01003777 if (!node) {
3778 node = ly_ctx_get_node(ctx, NULL, "/ietf-yang-library:modules-state");
3779 if (!node) {
3780 ly_errno = LY_EINT;
3781 return NULL;
3782 }
3783 }
3784
Michal Vasko46a4bf92016-09-08 08:23:49 +02003785 memset(&set, 0, sizeof set);
3786
Michal Vaskodb1da032016-09-08 10:07:38 +02003787 opts = LYXP_SNODE;
3788 if (options & LYS_FIND_OUTPUT) {
3789 opts |= LYXP_SNODE_OUTPUT;
3790 }
3791
Michal Vaskodb1da032016-09-08 10:07:38 +02003792 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vaskoc90f3ff2017-01-23 13:51:58 +01003793 /* just find a relevant node to put in path, if it fails, use the original one */
3794 for (i = 0; i < set.used; ++i) {
3795 if (set.val.snodes[i].in_ctx == 1) {
3796 node = set.val.snodes[i].snode;
3797 break;
3798 }
3799 }
Michal Vasko46a4bf92016-09-08 08:23:49 +02003800 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003801 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko46a4bf92016-09-08 08:23:49 +02003802 return NULL;
3803 }
3804
3805 ret_set = ly_set_new();
3806
3807 for (i = 0; i < set.used; ++i) {
3808 if (!set.val.snodes[i].in_ctx) {
3809 continue;
3810 }
3811 assert(set.val.snodes[i].in_ctx == 1);
3812
3813 switch (set.val.snodes[i].type) {
3814 case LYXP_NODE_ELEM:
3815 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3816 ly_set_free(ret_set);
3817 free(set.val.snodes);
3818 return NULL;
3819 }
3820 break;
3821 default:
3822 /* ignore roots, text and attr should not ever appear */
3823 break;
3824 }
3825 }
3826
3827 free(set.val.snodes);
3828 return ret_set;
3829}
3830
3831API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003832lys_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 +02003833{
Michal Vasko508a50d2016-09-07 14:50:33 +02003834 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003835 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003836 uint32_t i;
3837
Michal Vaskob94a5e42016-09-08 14:01:56 +02003838 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003839 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003840 }
3841
Michal Vaskob94a5e42016-09-08 14:01:56 +02003842 /* adjust the root */
3843 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3844 do {
3845 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3846 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3847 }
3848
Michal Vasko508a50d2016-09-07 14:50:33 +02003849 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003850
3851 if (options & LYXP_MUST) {
3852 options &= ~LYXP_MUST;
3853 options |= LYXP_SNODE_MUST;
3854 } else if (options & LYXP_WHEN) {
3855 options &= ~LYXP_WHEN;
3856 options |= LYXP_SNODE_WHEN;
3857 } else {
3858 options |= LYXP_SNODE;
3859 }
3860
Michal Vasko508a50d2016-09-07 14:50:33 +02003861 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3862 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003863 LOGVAL(LYE_SPEC, LY_VLOG_LYS, cur_snode, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003864 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003865 }
3866
Michal Vasko8548e082016-07-22 12:00:18 +02003867 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003868
Michal Vasko508a50d2016-09-07 14:50:33 +02003869 for (i = 0; i < set.used; ++i) {
3870 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003871 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003872 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003873 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003874 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003875 return NULL;
3876 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003877 break;
3878 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003879 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003880 break;
3881 }
3882 }
3883
Michal Vasko508a50d2016-09-07 14:50:33 +02003884 free(set.val.snodes);
3885 return ret_set;
3886}
3887
3888API struct ly_set *
3889lys_node_xpath_atomize(const struct lys_node *node, int options)
3890{
3891 const struct lys_node *next, *elem, *parent, *tmp;
3892 struct lyxp_set set;
3893 struct ly_set *ret_set;
3894 uint16_t i;
3895
3896 if (!node) {
3897 return NULL;
3898 }
3899
3900 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3901 if (!parent) {
3902 /* not in input, output, or notification */
3903 return NULL;
3904 }
3905
3906 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003907 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003908 return NULL;
3909 }
3910
3911 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003912 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003913 /* elem has no dependencies from other subtrees and local nodes get discarded */
3914 goto next_iter;
3915 }
3916
Michal Vasko769f8032017-01-24 13:11:55 +01003917 if (lyxp_node_atomize(elem, &set, 0)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003918 ly_set_free(ret_set);
3919 free(set.val.snodes);
3920 return NULL;
3921 }
3922
3923 for (i = 0; i < set.used; ++i) {
3924 switch (set.val.snodes[i].type) {
3925 case LYXP_NODE_ELEM:
3926 if (options & LYXP_NO_LOCAL) {
3927 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3928 if (tmp) {
3929 /* in local subtree, discard */
3930 break;
3931 }
3932 }
3933 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3934 ly_set_free(ret_set);
3935 free(set.val.snodes);
3936 return NULL;
3937 }
3938 break;
3939 default:
3940 /* ignore roots, text and attr should not ever appear */
3941 break;
3942 }
3943 }
3944
3945 free(set.val.snodes);
3946 if (!(options & LYXP_RECURSIVE)) {
3947 break;
3948 }
3949next_iter:
3950 LY_TREE_DFS_END(node, next, elem);
3951 }
3952
Michal Vasko8548e082016-07-22 12:00:18 +02003953 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003954}
3955
Michal Vasko44ab1462017-05-18 13:18:36 +02003956/* logs */
3957int
3958apply_aug(struct lys_node_augment *augment, struct unres_schema *unres)
Radek Krejci0ec51da2016-12-14 16:42:03 +01003959{
Michal Vasko44ab1462017-05-18 13:18:36 +02003960 struct lys_node *child, *parent;
3961 int clear_config;
3962 unsigned int u;
3963 struct lys_ext_instance *ext;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003964
3965 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
3966
3967 /* reconnect augmenting data into the target - add them to the target child list */
3968 if (augment->target->child) {
Michal Vasko44ab1462017-05-18 13:18:36 +02003969 child = augment->target->child->prev;
3970 child->next = augment->child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003971 augment->target->child->prev = augment->child->prev;
Michal Vasko44ab1462017-05-18 13:18:36 +02003972 augment->child->prev = child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003973 } else {
3974 augment->target->child = augment->child;
3975 }
3976
Michal Vasko44ab1462017-05-18 13:18:36 +02003977 /* inherit config information from actual parent */
3978 for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent));
3979 clear_config = (parent) ? 1 : 0;
3980 LY_TREE_FOR(augment->child, child) {
3981 if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) {
3982 return -1;
3983 }
3984 }
3985
3986 /* inherit extensions if any */
3987 for (u = 0; u < augment->target->ext_size; u++) {
3988 ext = augment->target->ext[u]; /* shortcut */
3989 if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) {
3990 if (unres_schema_add_node(lys_main_module(augment->module), unres, &ext, UNRES_EXT_FINALIZE, NULL) == -1) {
3991 /* something really bad happend since the extension finalization is not actually
3992 * being resolved while adding into unres, so something more serious with the unres
3993 * list itself must happened */
3994 return -1;
3995 }
3996 }
3997 }
3998
Radek Krejci0ec51da2016-12-14 16:42:03 +01003999 /* remove the flag about not applicability */
4000 augment->flags &= ~LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004001 return EXIT_SUCCESS;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004002}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004003
Radek Krejcib2541a32016-12-12 16:45:57 +01004004static void
4005remove_aug(struct lys_node_augment *augment)
4006{
4007 struct lys_node *last, *elem;
4008
Michal Vasko44ab1462017-05-18 13:18:36 +02004009 if (augment->flags & LYS_NOTAPPLIED) {
4010 /* skip already not applied augment */
Radek Krejcib2541a32016-12-12 16:45:57 +01004011 return;
4012 }
4013
4014 elem = augment->child;
4015 if (elem) {
4016 LY_TREE_FOR(elem, last) {
4017 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
4018 break;
4019 }
4020 }
4021 /* elem is first augment child, last is the last child */
4022
4023 /* parent child ptr */
4024 if (augment->target->child == elem) {
4025 augment->target->child = last->next;
4026 }
4027
4028 /* parent child next ptr */
4029 if (elem->prev->next) {
4030 elem->prev->next = last->next;
4031 }
4032
4033 /* parent child prev ptr */
4034 if (last->next) {
4035 last->next->prev = elem->prev;
4036 } else if (augment->target->child) {
4037 augment->target->child->prev = elem->prev;
4038 }
4039
4040 /* update augment children themselves */
4041 elem->prev = last;
4042 last->next = NULL;
4043 }
4044
Radek Krejci0ec51da2016-12-14 16:42:03 +01004045 /* augment->target still keeps the resolved target, but for lys_augment_free()
4046 * we have to keep information that this augment is not applied to free its data */
4047 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01004048}
4049
Radek Krejci30bfcd22017-01-27 16:54:48 +01004050/*
4051 * @param[in] module - the module where the deviation is defined
4052 */
4053static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004054lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004055{
4056 int ret;
4057 char *parent_path;
4058 struct lys_node *target = NULL, *parent;
4059
4060 if (!dev->deviate) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004061 return;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004062 }
4063
4064 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
4065 if (dev->orig_node) {
4066 /* removing not-supported deviation ... */
4067 if (strrchr(dev->target_name, '/') != dev->target_name) {
4068 /* ... from a parent */
4069
4070 /* reconnect to its previous position */
4071 parent = dev->orig_node->parent;
4072 if (parent) {
4073 /* the original node was actually from augment, we have to get know if the augment is
4074 * applied (its module is enabled and implemented). If yes, the node will be connected
4075 * to the augment and the linkage with the target will be fixed if needed, otherwise
4076 * it will be connected only to the augment */
4077 /* first, connect it into the augment */
4078 lys_node_addchild(parent, NULL, dev->orig_node);
4079 if (!parent->module->disabled && parent->module->implemented) {
4080 /* augment is supposed to be applied, so fix pointers in target and the status of the original node */
4081 if (parent->child == dev->orig_node) {
4082 /* the only node in augment */
4083 dev->orig_node->flags |= LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004084 apply_aug((struct lys_node_augment *)parent, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004085 } else {
4086 /* other nodes from augment applied, nothing more needed in target, everything was done
4087 * by lys_node_addchild() */
4088 dev->orig_node->flags |= parent->child->flags & LYS_NOTAPPLIED;
4089 }
4090 } else {
4091 /* augment is not supposed to be applied */
4092 dev->orig_node->flags |= LYS_NOTAPPLIED;
4093 }
4094 } else {
4095 /* non-augment, non-toplevel */
4096 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
4097 ret = resolve_augment_schema_nodeid(parent_path, NULL, module, 1,
4098 (const struct lys_node **)&target);
4099 free(parent_path);
4100 if (ret || !target) {
4101 LOGINT;
4102 return;
4103 }
4104 lys_node_addchild(target, NULL, dev->orig_node);
4105 }
4106 } else {
4107 /* ... from top-level data */
4108 lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node);
4109 }
4110
4111 dev->orig_node = NULL;
4112 } else {
4113 /* adding not-supported deviation */
4114 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, module, 1,
4115 (const struct lys_node **)&target);
4116 if (ret || !target) {
4117 LOGINT;
4118 return;
4119 }
4120
4121 /* unlink and store the original node */
4122 parent = target->parent;
4123 lys_node_unlink(target);
4124 if (parent && parent->nodetype == LYS_AUGMENT) {
4125 /* hack for augment, because when the original will be sometime reconnected back, we actually need
4126 * to reconnect it to both - the augment and its target (which is deduced from the deviations target
4127 * path), so we need to remember the augment as an addition */
4128 target->parent = parent;
4129 }
4130 dev->orig_node = target;
4131 }
4132 } else {
4133 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, module, 1,
4134 (const struct lys_node **)&target);
4135 if (ret || !target) {
4136 LOGINT;
4137 return;
4138 }
4139
4140 lys_node_switch(target, dev->orig_node);
4141 dev->orig_node = target;
4142 }
4143}
4144
4145/* temporarily removes or applies deviations, updates module deviation flag accordingly */
4146void
4147lys_switch_deviations(struct lys_module *module)
4148{
4149 uint32_t i = 0, j;
4150 const struct lys_module *mod;
4151 const char *ptr;
Michal Vasko44ab1462017-05-18 13:18:36 +02004152 struct unres_schema *unres;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004153
4154 if (module->deviated) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004155 unres = calloc(1, sizeof *unres);
4156 if (!unres) {
4157 LOGMEM;
4158 return;
4159 }
4160
Radek Krejci30bfcd22017-01-27 16:54:48 +01004161 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
4162 if (mod == module) {
4163 continue;
4164 }
4165
4166 for (j = 0; j < mod->deviation_size; ++j) {
4167 ptr = strstr(mod->deviation[j].target_name, module->name);
4168 if (ptr && ptr[strlen(module->name)] == ':') {
Michal Vasko44ab1462017-05-18 13:18:36 +02004169 lys_switch_deviation(&mod->deviation[j], mod, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004170 }
4171 }
4172 }
4173
4174 if (module->deviated == 2) {
4175 module->deviated = 1;
4176 } else {
4177 module->deviated = 2;
4178 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004179
4180 if (unres->count) {
4181 resolve_unres_schema(module, unres);
4182 }
4183 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004184 }
4185}
4186
4187static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004188apply_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004189{
Michal Vasko44ab1462017-05-18 13:18:36 +02004190 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004191
4192 assert(dev->orig_node);
4193 lys_node_module(dev->orig_node)->deviated = 1;
4194}
4195
4196static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004197remove_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004198{
4199 uint32_t idx = 0, j;
4200 const struct lys_module *mod;
4201 struct lys_module *target_mod;
4202 const char *ptr;
4203
4204 if (dev->orig_node) {
4205 target_mod = lys_node_module(dev->orig_node);
4206 } else {
4207 LOGINT;
4208 return;
4209 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004210 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004211
4212 /* clear the deviation flag if possible */
4213 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
4214 if ((mod == module) || (mod == target_mod)) {
4215 continue;
4216 }
4217
4218 for (j = 0; j < mod->deviation_size; ++j) {
4219 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
4220 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
4221 /* some other module deviation targets the inspected module, flag remains */
4222 break;
4223 }
4224 }
4225
4226 if (j < mod->deviation_size) {
4227 break;
4228 }
4229 }
4230
4231 if (!mod) {
4232 target_mod->deviated = 0;
4233 }
4234}
4235
4236void
4237lys_sub_module_apply_devs_augs(struct lys_module *module)
4238{
4239 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004240 struct unres_schema *unres;
4241
4242 unres = calloc(1, sizeof *unres);
4243 if (!unres) {
4244 LOGMEM;
4245 return;
4246 }
Radek Krejci30bfcd22017-01-27 16:54:48 +01004247
4248 /* remove applied deviations */
4249 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004250 apply_dev(&module->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004251 }
4252 /* remove applied augments */
4253 for (u = 0; u < module->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004254 apply_aug(&module->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004255 }
4256
4257 /* remove deviation and augments defined in submodules */
4258 for (v = 0; v < module->inc_size; ++v) {
4259 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004260 apply_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004261 }
4262
4263 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004264 apply_aug(&module->inc[v].submodule->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004265 }
4266 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004267
4268 if (unres->count) {
4269 resolve_unres_schema(module, unres);
4270 }
4271 /* nothing else left to do even if something is not resolved */
4272 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004273}
4274
Radek Krejcib2541a32016-12-12 16:45:57 +01004275void
4276lys_sub_module_remove_devs_augs(struct lys_module *module)
4277{
4278 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004279 struct unres_schema *unres;
4280
4281 unres = calloc(1, sizeof *unres);
4282 if (!unres) {
4283 LOGMEM;
4284 return;
4285 }
Radek Krejcib2541a32016-12-12 16:45:57 +01004286
4287 /* remove applied deviations */
4288 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004289 remove_dev(&module->deviation[u], module, unres);
Radek Krejcib2541a32016-12-12 16:45:57 +01004290 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004291 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01004292 for (u = 0; u < module->augment_size; ++u) {
4293 remove_aug(&module->augment[u]);
4294 }
4295
4296 /* remove deviation and augments defined in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004297 for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) {
Radek Krejcib2541a32016-12-12 16:45:57 +01004298 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004299 remove_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejcidbc15262016-06-16 14:58:29 +02004300 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004301
Radek Krejcib2541a32016-12-12 16:45:57 +01004302 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4303 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004304 }
4305 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004306
4307 if (unres->count) {
4308 resolve_unres_schema(module, unres);
4309 }
4310 /* nothing else left to do even if something is not resolved */
4311 unres_schema_free(module, &unres, 1);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004312}
4313
Radek Krejci27fe55e2016-09-13 17:13:35 +02004314static int
4315lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
4316{
4317 struct lys_node *root, *next, *node;
Radek Krejci9e6af732017-04-27 14:40:25 +02004318 uint16_t i, j;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004319
4320 for (i = 0; i < module->augment_size; i++) {
4321 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004322 assert(module->augment[i].target);
4323 if (apply_aug(&module->augment[i], unres)) {
4324 return EXIT_FAILURE;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004325 }
4326 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004327
4328 /* identities */
4329 for (i = 0; i < module->ident_size; i++) {
4330 for (j = 0; j < module->ident[i].base_size; j++) {
4331 resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]);
4332 }
4333 }
4334
Radek Krejci27fe55e2016-09-13 17:13:35 +02004335 LY_TREE_FOR(module->data, root) {
4336 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
4337 LY_TREE_DFS_BEGIN(root, next, node) {
4338 if (node->nodetype == LYS_GROUPING) {
4339 goto nextsibling;
4340 }
4341 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4342 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
4343 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
4344 UNRES_TYPE_LEAFREF, node) == -1) {
4345 return EXIT_FAILURE;
4346 }
4347 }
4348 }
4349
4350 /* modified LY_TREE_DFS_END */
4351 next = node->child;
4352 /* child exception for leafs, leaflists and anyxml without children */
4353 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4354 next = NULL;
4355 }
4356 if (!next) {
4357nextsibling:
4358 /* no children */
4359 if (node == root) {
4360 /* we are done, root has no children */
4361 break;
4362 }
4363 /* try siblings */
4364 next = node->next;
4365 }
4366 while (!next) {
4367 /* parent is already processed, go to its sibling */
4368 node = lys_parent(node);
4369 /* no siblings, go back through parents */
4370 if (lys_parent(node) == lys_parent(root)) {
4371 /* we are done, no next element to process */
4372 break;
4373 }
4374 next = node->next;
4375 }
4376 }
4377 }
4378
4379 return EXIT_SUCCESS;
4380}
4381
4382API int
4383lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02004384{
4385 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004386 struct unres_schema *unres;
Radek Krejci9e6af732017-04-27 14:40:25 +02004387 int i, j, k, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02004388
Radek Krejci27fe55e2016-09-13 17:13:35 +02004389 if (!module) {
4390 ly_errno = LY_EINVAL;
4391 return EXIT_FAILURE;
4392 }
4393
4394 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004395
4396 if (module->disabled) {
4397 disabled = 1;
4398 lys_set_enabled(module);
4399 }
4400
Michal Vasko26055752016-05-03 11:36:31 +02004401 if (module->implemented) {
4402 return EXIT_SUCCESS;
4403 }
4404
4405 ctx = module->ctx;
4406
4407 for (i = 0; i < ctx->models.used; ++i) {
4408 if (module == ctx->models.list[i]) {
4409 continue;
4410 }
4411
4412 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
4413 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004414 if (disabled) {
4415 /* set it back disabled */
4416 lys_set_disabled(module);
4417 }
Michal Vasko26055752016-05-03 11:36:31 +02004418 return EXIT_FAILURE;
4419 }
4420 }
4421
Radek Krejci27fe55e2016-09-13 17:13:35 +02004422 unres = calloc(1, sizeof *unres);
4423 if (!unres) {
4424 LOGMEM;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004425 if (disabled) {
4426 /* set it back disabled */
4427 lys_set_disabled(module);
4428 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004429 return EXIT_FAILURE;
4430 }
4431 /* recursively make the module implemented */
4432 ((struct lys_module *)module)->implemented = 1;
4433 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
4434 goto error;
4435 }
4436 /* process augments in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004437 for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004438 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
4439 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004440 assert(module->inc[i].submodule->augment[j].target);
4441 if (apply_aug(&module->inc[i].submodule->augment[j], unres)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004442 goto error;
4443 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004444 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004445
4446 /* identities */
4447 for (j = 0; j < module->inc[i].submodule->ident_size; j++) {
4448 for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) {
4449 resolve_identity_backlink_update(&module->inc[i].submodule->ident[j],
4450 module->inc[i].submodule->ident[j].base[k]);
4451 }
4452 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004453 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004454 /* try again resolve augments in other modules possibly augmenting this one,
4455 * since we have just enabled it
4456 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004457 /* resolve rest of unres items */
4458 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
4459 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004460 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004461 unres_schema_free(NULL, &unres, 0);
Michal Vasko26055752016-05-03 11:36:31 +02004462
4463 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004464
4465error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004466 if (disabled) {
4467 /* set it back disabled */
4468 lys_set_disabled(module);
4469 }
4470
Radek Krejci27fe55e2016-09-13 17:13:35 +02004471 ((struct lys_module *)module)->implemented = 0;
Michal Vasko44ab1462017-05-18 13:18:36 +02004472 unres_schema_free((struct lys_module *)module, &unres, 1);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004473 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004474}
4475
4476void
4477lys_submodule_module_data_free(struct lys_submodule *submodule)
4478{
4479 struct lys_node *next, *elem;
4480
4481 /* remove parsed data */
4482 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
4483 if (elem->module == (struct lys_module *)submodule) {
4484 lys_node_free(elem, NULL, 0);
4485 }
4486 }
4487}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02004488
4489int
4490lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
4491{
4492 uint8_t i;
4493
4494 for (i = 0; i < list->keys_size; i++) {
4495 if (list->keys[i] == leaf) {
4496 return i + 1;
4497 }
4498 }
4499
4500 return 0;
4501}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004502
4503API char *
4504lys_path(const struct lys_node *node)
4505{
4506 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
4507 uint16_t index = LY_BUF_SIZE - 1;
4508
4509 if (!node) {
4510 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
4511 return NULL;
4512 }
4513
4514 /* backup the shared internal buffer */
4515 if (ly_buf_used && buf[0]) {
4516 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
4517 }
4518 ly_buf_used++;
4519
4520 /* build the path */
4521 buf[index] = '\0';
Michal Vasko5efa25c2017-01-10 11:34:30 +01004522 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index, 0);
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004523 result = strdup(&buf[index]);
4524
4525 /* restore the shared internal buffer */
4526 if (buf_backup) {
4527 strcpy(buf, buf_backup);
4528 free(buf_backup);
4529 }
4530 ly_buf_used--;
4531
4532 return result;
4533}
Radek Krejci9ad23f42016-10-31 15:46:52 +01004534
Radek Krejci8d6b7422017-02-03 14:42:13 +01004535static void
4536lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt)
4537{
4538 struct lyext_substmt *info;
Radek Krejcib71243e2017-02-08 16:20:08 +01004539 const char **str, ***a;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004540 int c;
4541
4542 str = lys_ext_complex_get_substmt(stmt, ext, &info);
4543 if (!str || !(*str)) {
4544 return;
4545 }
4546 if (info->cardinality >= LY_STMT_CARD_SOME) {
4547 /* we have array */
Radek Krejcib71243e2017-02-08 16:20:08 +01004548 a = (const char ***)str;
Radek Krejci56c80412017-02-09 10:44:16 +01004549 for (str = (*(const char ***)str), c = 0; str[c]; c++) {
4550 lydict_remove(ctx, str[c]);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004551 }
Radek Krejci56c80412017-02-09 10:44:16 +01004552 free(a[0]);
4553 if (stmt == LY_STMT_BELONGSTO) {
4554 for (str = a[1], c = 0; str[c]; c++) {
4555 lydict_remove(ctx, str[c]);
4556 }
4557 free(a[1]);
PavolVican99c70722017-02-18 17:25:52 +01004558 } else if (stmt == LY_STMT_ARGUMENT) {
4559 free(a[1]);
Radek Krejci56c80412017-02-09 10:44:16 +01004560 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004561 } else {
Radek Krejci56c80412017-02-09 10:44:16 +01004562 lydict_remove(ctx, str[0]);
4563 if (stmt == LY_STMT_BELONGSTO) {
4564 lydict_remove(ctx, str[1]);
4565 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004566 }
4567}
4568void
Radek Krejci5138e9f2017-04-12 13:10:46 +02004569lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size,
4570 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci8d6b7422017-02-03 14:42:13 +01004571{
Radek Krejcif8d05c22017-02-10 15:33:35 +01004572 unsigned int i, j, k;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004573 struct lyext_substmt *substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004574 void **pp, **start;
Radek Krejcif95b6292017-02-13 15:57:37 +01004575 struct lys_node *siter, *snext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004576
4577#define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \
Radek Krejcib84686f2017-02-09 16:04:55 +01004578 pp = lys_ext_complex_get_substmt(STMT, (struct lys_ext_instance_complex *)e[i], NULL); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004579 if (!pp || !(*pp)) { break; } \
Radek Krejcib84686f2017-02-09 16:04:55 +01004580 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004581 for (start = pp = *pp; *pp; pp++) { \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004582 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004583 if (FREE) { free(*pp); } \
4584 } \
4585 free(start); \
4586 } else { /* single item */ \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004587 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004588 if (FREE) { free(*pp); } \
4589 }
4590
4591 if (!size || !e || !(*e)) {
4592 return;
4593 }
4594
4595 for (i = 0; i < size; i++) {
4596 if (!e[i]) {
4597 continue;
4598 }
4599
4600 if (e[i]->flags & (LYEXT_OPT_INHERIT)) {
4601 /* no free, this is just a shadow copy of the original extension instance */
4602 } else {
4603 if (e[i]->flags & (LYEXT_OPT_YANG)) {
4604 free(e[i]->def); /* remove name of instance extension */
PavolVican19dc6152017-02-06 12:04:15 +01004605 e[i]->def = NULL;
PavolVicandb0e8172017-02-20 00:46:09 +01004606 yang_free_ext_data((struct yang_ext_substmt *)e[i]->parent); /* remove backup part of yang file */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004607 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02004608 /* remove private object */
4609 if (e[i]->priv && private_destructor) {
4610 private_destructor((struct lys_node*)e[i], e[i]->priv);
4611 }
4612 lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size, private_destructor);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004613 lydict_remove(ctx, e[i]->arg_value);
4614 }
4615
4616 if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX) {
Radek Krejcifebdad72017-02-06 11:35:51 +01004617 substmt = ((struct lys_ext_instance_complex *)e[i])->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004618 for (j = 0; substmt[j].stmt; j++) {
4619 switch(substmt[j].stmt) {
4620 case LY_STMT_DESCRIPTION:
4621 case LY_STMT_REFERENCE:
4622 case LY_STMT_UNITS:
Radek Krejcib71243e2017-02-08 16:20:08 +01004623 case LY_STMT_ARGUMENT:
4624 case LY_STMT_DEFAULT:
4625 case LY_STMT_ERRTAG:
4626 case LY_STMT_ERRMSG:
4627 case LY_STMT_PREFIX:
4628 case LY_STMT_NAMESPACE:
4629 case LY_STMT_PRESENCE:
4630 case LY_STMT_REVISIONDATE:
4631 case LY_STMT_KEY:
4632 case LY_STMT_BASE:
4633 case LY_STMT_BELONGSTO:
4634 case LY_STMT_CONTACT:
4635 case LY_STMT_ORGANIZATION:
4636 case LY_STMT_PATH:
Radek Krejci8d6b7422017-02-03 14:42:13 +01004637 lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt);
4638 break;
4639 case LY_STMT_TYPE:
4640 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1);
4641 break;
Radek Krejci63fc0962017-02-15 13:20:18 +01004642 case LY_STMT_TYPEDEF:
4643 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1);
4644 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004645 case LY_STMT_IFFEATURE:
4646 EXTCOMPLEX_FREE_STRUCT(LY_STMT_IFFEATURE, struct lys_iffeature, lys_iffeature_free, 0, 1);
4647 break;
Radek Krejci5496fae2017-02-10 13:26:48 +01004648 case LY_STMT_MAX:
4649 case LY_STMT_MIN:
4650 case LY_STMT_POSITION:
PavolVican2ed9f4e2017-02-16 00:08:45 +01004651 case LY_STMT_VALUE:
Radek Krejcif8d05c22017-02-10 15:33:35 +01004652 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
Radek Krejci716cd7a2017-02-15 12:23:41 +01004653 if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) {
Radek Krejcif8d05c22017-02-10 15:33:35 +01004654 for(k = 0; ((uint32_t**)(*pp))[k]; k++) {
4655 free(((uint32_t**)(*pp))[k]);
4656 }
4657 }
4658 free(*pp);
4659 break;
4660 case LY_STMT_DIGITS:
Radek Krejcib84686f2017-02-09 16:04:55 +01004661 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4662 /* free the array */
4663 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4664 free(*pp);
4665 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004666 break;
Radek Krejci37f9ba32017-02-10 16:50:35 +01004667 case LY_STMT_MODULE:
4668 /* modules are part of the context, so they will be freed there */
4669 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4670 /* free the array */
4671 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4672 free(*pp);
4673 }
4674 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01004675 case LY_STMT_ACTION:
Radek Krejcib31762b2017-02-15 10:48:42 +01004676 case LY_STMT_ANYDATA:
4677 case LY_STMT_ANYXML:
4678 case LY_STMT_CASE:
4679 case LY_STMT_CHOICE:
4680 case LY_STMT_CONTAINER:
4681 case LY_STMT_GROUPING:
4682 case LY_STMT_INPUT:
4683 case LY_STMT_LEAF:
4684 case LY_STMT_LEAFLIST:
4685 case LY_STMT_LIST:
4686 case LY_STMT_NOTIFICATION:
4687 case LY_STMT_OUTPUT:
4688 case LY_STMT_RPC:
4689 case LY_STMT_USES:
Radek Krejcif95b6292017-02-13 15:57:37 +01004690 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4691 LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) {
4692 lys_node_free(siter, NULL, 0);
4693 }
Radek Krejcib31762b2017-02-15 10:48:42 +01004694 *pp = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01004695 break;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01004696 case LY_STMT_UNIQUE:
4697 pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL);
4698 if (!pp || !(*pp)) {
4699 break;
4700 }
4701 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4702 for (start = pp = *pp; *pp; pp++) {
4703 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4704 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4705 }
4706 free((*(struct lys_unique**)pp)->expr);
4707 free(*pp);
4708 }
4709 free(start);
4710 } else { /* single item */
4711 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4712 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4713 }
4714 free((*(struct lys_unique**)pp)->expr);
4715 free(*pp);
4716 }
4717 break;
Radek Krejciaa9c5202017-02-15 16:10:14 +01004718 case LY_STMT_LENGTH:
4719 case LY_STMT_MUST:
4720 case LY_STMT_PATTERN:
4721 case LY_STMT_RANGE:
4722 EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1);
4723 break;
Radek Krejcic5cc5302017-02-16 10:07:46 +01004724 case LY_STMT_WHEN:
4725 EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0);
4726 break;
Radek Krejci7417a082017-02-16 11:07:59 +01004727 case LY_STMT_REVISION:
4728 pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL);
4729 if (!pp || !(*pp)) {
4730 break;
4731 }
4732 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4733 for (start = pp = *pp; *pp; pp++) {
4734 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4735 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4736 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004737 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004738 free(*pp);
4739 }
4740 free(start);
4741 } else { /* single item */
4742 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4743 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4744 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004745 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004746 free(*pp);
4747 }
4748 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004749 default:
Radek Krejcib84686f2017-02-09 16:04:55 +01004750 /* nothing to free */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004751 break;
4752 }
4753 }
4754 }
4755
4756 free(e[i]);
4757 }
4758 free(e);
4759
4760#undef EXTCOMPLEX_FREE_STRUCT
4761}