blob: b0cef10a0263cc627f101af3c3a73375b1a8f350 [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
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020040static int
41lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +020042 int tpdftype, struct unres_schema *unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020043
Radek Krejci9ff0a922016-07-14 13:08:05 +020044API const struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +010045lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020046{
Radek Krejci9ff0a922016-07-14 13:08:05 +020047 int i;
Radek Krejci48061fb2015-08-05 15:41:07 +020048
Radek Krejci27fe55e2016-09-13 17:13:35 +020049 if (!node) {
50 return NULL;
51 }
52
Radek Krejci48061fb2015-08-05 15:41:07 +020053check:
54 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
55 /* input/output does not have if-feature, so skip them */
56
57 /* check local if-features */
Michal Vaskoc5c26b02016-06-29 11:10:29 +020058 for (i = 0; i < node->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +020059 if (!resolve_iffeature(&node->iffeature[i])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +020060 return node;
Radek Krejci48061fb2015-08-05 15:41:07 +020061 }
62 }
63 }
64
65 if (!recursive) {
66 return NULL;
67 }
68
69 /* go through parents */
70 if (node->nodetype == LYS_AUGMENT) {
71 /* go to parent actually means go to the target node */
72 node = ((struct lys_node_augment *)node)->target;
Radek Krejci48061fb2015-08-05 15:41:07 +020073 } else if (node->parent) {
74 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +020075 } else {
76 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +020077 }
78
Radek Krejci074bf852015-08-19 14:22:16 +020079 if (recursive == 2) {
80 /* continue only if the node cannot have a data instance */
81 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
82 return NULL;
83 }
84 }
85 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +020086}
87
Michal Vasko1dca6882015-10-22 14:29:42 +020088int
Michal Vasko36cbaa42015-12-14 13:15:48 +010089lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
90 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +020091{
Radek Krejcic071c542016-01-27 14:57:51 +010092 const struct lys_node *node, *parent = NULL;
93 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +010094 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +020095
Michal Vasko36cbaa42015-12-14 13:15:48 +010096 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +020097 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +020098
Michal Vasko36cbaa42015-12-14 13:15:48 +010099 /* fill the lengths in case the caller is so indifferent */
100 if (!mod_name_len) {
101 mod_name_len = strlen(mod_name);
102 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200103 if (!nam_len) {
104 nam_len = strlen(name);
105 }
106
Michal Vasko36cbaa42015-12-14 13:15:48 +0100107 /* set mod correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100108 parent = lys_parent(siblings);
109 if (!parent) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100110 mod = lys_node_module(siblings);
Michal Vasko1dca6882015-10-22 14:29:42 +0200111 }
112
Radek Krejcic071c542016-01-27 14:57:51 +0100113 /* try to find the node */
114 node = NULL;
115 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
116 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100117 /* module name comparison */
118 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200119 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 +0100120 continue;
121 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200122
Radek Krejcic071c542016-01-27 14:57:51 +0100123 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200124 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100125 if (ret) {
126 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200127 }
Radek Krejcic071c542016-01-27 14:57:51 +0100128 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200129 }
130 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200131 }
132
133 return EXIT_FAILURE;
134}
135
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200136int
Michal Vasko1e62a092015-12-01 12:27:20 +0100137lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type,
138 const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200139{
Michal Vasko1e62a092015-12-01 12:27:20 +0100140 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200141
142 assert(siblings && name);
143 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
144
145 /* find the beginning */
146 while (siblings->prev->next) {
147 siblings = siblings->prev;
148 }
149
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200150 if (!mod) {
151 mod = siblings->module;
152 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200153
Michal Vasko4f0dad02016-02-15 14:08:23 +0100154 /* try to find the node */
155 node = NULL;
Michal Vaskodcf98e62016-05-05 17:53:53 +0200156 while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100157 if (!type || (node->nodetype & type)) {
158 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200159 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100160 continue;
161 }
162
Michal Vasko4f0dad02016-02-15 14:08:23 +0100163 /* direct name check */
Radek Krejci749190d2016-02-18 16:26:25 +0100164 if (ly_strequal(node->name, name, 0)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100165 if (ret) {
166 *ret = node;
167 }
168 return EXIT_SUCCESS;
169 }
Radek Krejcic071c542016-01-27 14:57:51 +0100170 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200171 }
172
173 return EXIT_FAILURE;
174}
175
Michal Vasko1e62a092015-12-01 12:27:20 +0100176API const struct lys_node *
177lys_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 +0200178{
Michal Vasko1e62a092015-12-01 12:27:20 +0100179 const struct lys_node *next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200180
Radek Krejci8bc87f62015-09-02 16:19:05 +0200181 if (!last) {
182 /* first call */
183
184 /* get know where to start */
185 if (parent) {
186 /* schema subtree */
187 next = last = parent->child;
188 } else {
189 /* top level data */
190 assert(module);
191 next = last = module->data;
192 }
Radek Krejci972724f2016-08-12 15:24:40 +0200193 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
194 /* continue with uses content */
195 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200196 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200197 /* continue after the last returned value */
198 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200199 }
200
201repeat:
Michal Vasko7c386e72015-10-07 15:13:33 +0200202 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200203 if (options & LYS_GETNEXT_WITHGROUPING) {
204 return next;
205 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200206 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200207 }
208
Radek Krejci972724f2016-08-12 15:24:40 +0200209 if (!next) { /* cover case when parent is augment */
210 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200211 /* no next element */
212 return NULL;
213 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200214 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200215 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200216 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200217 } else {
218 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200219 }
220
221 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200222 case LYS_INPUT:
223 case LYS_OUTPUT:
224 if (options & LYS_GETNEXT_WITHINOUT) {
225 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200226 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200227 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200228 } else {
229 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200230 }
Radek Krejci972724f2016-08-12 15:24:40 +0200231 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200232
Michal Vaskoa5835e92015-10-20 15:07:39 +0200233 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200234 if (options & LYS_GETNEXT_WITHCASE) {
235 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200236 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200237 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200238 } else {
239 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200240 }
Radek Krejci972724f2016-08-12 15:24:40 +0200241 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200242
Michal Vasko1dca6882015-10-22 14:29:42 +0200243 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200244 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200245 if (options & LYS_GETNEXT_WITHUSES) {
246 return next;
247 } else if (next->child) {
248 next = next->child;
249 } else {
250 next = next->next;
251 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200252 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200253
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200254 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200255 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200256 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200257 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200258 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200259 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200260 case LYS_LIST:
261 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200262 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200263
Radek Krejci972724f2016-08-12 15:24:40 +0200264 case LYS_CONTAINER:
265 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
266 if (next->child) {
267 /* go into */
268 next = next->child;
269 } else {
270 next = next->next;
271 }
272 goto repeat;
273 } else {
274 return next;
275 }
276
Radek Krejci8bc87f62015-09-02 16:19:05 +0200277 case LYS_CHOICE:
278 if (options & LYS_GETNEXT_WITHCHOICE) {
279 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200280 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200281 /* go into */
282 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200283 } else {
284 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200285 }
Radek Krejci972724f2016-08-12 15:24:40 +0200286 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200287
Radek Krejci7f40ce32015-08-12 20:38:46 +0200288 default:
289 /* we should not be here */
290 return NULL;
291 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200292
293
294}
295
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200296void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200297lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200298{
Radek Krejci76512572015-08-04 09:47:08 +0200299 struct lys_node *parent, *first;
Radek Krejcic071c542016-01-27 14:57:51 +0100300 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200301
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200302 if (!node) {
303 return;
304 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200305
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200306 /* unlink from data model if necessary */
307 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100308 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100309 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100310 if (main_module->data == node) {
311 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200312 }
313 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200314
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200315 /* store pointers to important nodes */
316 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200317 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200318 /* handle augments - first, unlink it from the augment parent ... */
319 if (parent->child == node) {
320 parent->child = node->next;
321 }
322 /* and then continue with the target parent */
Radek Krejci76512572015-08-04 09:47:08 +0200323 parent = ((struct lys_node_augment *)parent)->target;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200324 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200325
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200326 /* unlink from parent */
327 if (parent) {
328 if (parent->child == node) {
329 parent->child = node->next;
330 }
331 node->parent = NULL;
332 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200333
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200334 /* unlink from siblings */
335 if (node->prev == node) {
336 /* there are no more siblings */
337 return;
338 }
339 if (node->next) {
340 node->next->prev = node->prev;
341 } else {
342 /* unlinking the last element */
343 if (parent) {
344 first = parent->child;
345 } else {
346 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200347 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200348 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200349 }
350 }
351 first->prev = node->prev;
352 }
353 if (node->prev->next) {
354 node->prev->next = node->next;
355 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200356
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200357 /* clean up the unlinked element */
358 node->next = NULL;
359 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200360}
361
Michal Vasko563ef092015-09-04 13:17:23 +0200362struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100363lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200364{
365 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200366
367 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200368 /* top-level augment, look into module (uses augment is handled correctly below) */
369 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
370 par_iter = par_iter->parent->module->data;
371 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200372 break;
373 }
374 }
375
Michal Vasko6f929da2015-10-02 16:23:25 +0200376 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200377 continue;
378 }
379
380 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
381 if (!stop) {
382 stop = par_iter;
383 } else if (iter == stop) {
384 break;
385 }
386 if (iter->nodetype != LYS_GROUPING) {
387 continue;
388 }
389
Radek Krejcif8426a72015-10-31 23:14:03 +0100390 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200391 return (struct lys_node_grp *)iter;
392 }
393 }
394 }
395
Michal Vasko563ef092015-09-04 13:17:23 +0200396 return NULL;
397}
398
Radek Krejci10c760e2015-08-14 14:45:43 +0200399/*
400 * get next grouping in the root's subtree, in the
401 * first call, tha last is NULL
402 */
403static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200404lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200405{
Radek Krejci10c760e2015-08-14 14:45:43 +0200406 struct lys_node *last = (struct lys_node *)lastgrp;
407 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200408
Radek Krejci10c760e2015-08-14 14:45:43 +0200409 assert(root);
410
411 if (!last) {
412 last = root;
413 }
414
415 while (1) {
416 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
417 next = last->child;
418 } else {
419 next = NULL;
420 }
421 if (!next) {
422 if (last == root) {
423 /* we are done */
424 return NULL;
425 }
426
427 /* no children, go to siblings */
428 next = last->next;
429 }
430 while (!next) {
431 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100432 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200433 /* we are done */
434 return NULL;
435 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200436 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100437 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200438 }
439
440 if (next->nodetype == LYS_GROUPING) {
441 return (struct lys_node_grp *)next;
442 }
443
444 last = next;
445 }
446}
447
Michal Vasko0d343d12015-08-24 14:57:36 +0200448/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200449int
Radek Krejci07911992015-08-14 15:13:31 +0200450lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
451{
Michal Vasko563ef092015-09-04 13:17:23 +0200452 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200453 struct lys_node_grp *grp;
454 int down;
455
456 assert(node);
457
458 if (!parent) {
459 assert(module);
460 } else {
461 module = parent->module;
462 }
463
464 switch (node->nodetype) {
465 case LYS_GROUPING:
466 /* 6.2.1, rule 6 */
467 if (parent) {
468 if (parent->child) {
469 down = 1;
470 start = parent->child;
471 } else {
472 down = 0;
473 start = parent;
474 }
475 } else {
476 down = 1;
477 start = module->data;
478 }
479 /* go up */
Radek Krejcic071c542016-01-27 14:57:51 +0100480 if (lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100481 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200482 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200483 }
484 /* go down, because grouping can be defined after e.g. container in which is collision */
485 if (down) {
486 for (iter = start, stop = NULL; iter; iter = iter->prev) {
487 if (!stop) {
488 stop = start;
489 } else if (iter == stop) {
490 break;
491 }
492 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
493 continue;
494 }
495
496 grp = NULL;
497 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100498 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100499 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200500 return EXIT_FAILURE;
501 }
502 }
503 }
504 }
505 break;
506 case LYS_LEAF:
507 case LYS_LEAFLIST:
508 case LYS_LIST:
509 case LYS_CONTAINER:
510 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200511 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200512 /* 6.2.1, rule 7 */
513 if (parent) {
514 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200515 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
516 if (iter->nodetype == LYS_AUGMENT) {
517 if (((struct lys_node_augment *)iter)->target) {
518 /* augment is resolved, go up */
519 iter = ((struct lys_node_augment *)iter)->target;
520 continue;
521 }
522 /* augment is not resolved, this is the final parent */
523 break;
524 }
Radek Krejci07911992015-08-14 15:13:31 +0200525 iter = iter->parent;
526 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200527
Radek Krejci07911992015-08-14 15:13:31 +0200528 if (!iter) {
529 stop = NULL;
530 iter = module->data;
531 } else {
532 stop = iter;
533 iter = iter->child;
534 }
535 } else {
536 stop = NULL;
537 iter = module->data;
538 }
539 while (iter) {
540 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
541 iter = iter->child;
542 continue;
543 }
544
Radek Krejcibf2abff2016-08-23 15:51:52 +0200545 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100546 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100547 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200548 return EXIT_FAILURE;
549 }
550 }
551
552 /* special case for choice - we must check the choice's name as
553 * well as the names of nodes under the choice
554 */
555 if (iter->nodetype == LYS_CHOICE) {
556 iter = iter->child;
557 continue;
558 }
559
560 /* go to siblings */
561 if (!iter->next) {
562 /* no sibling, go to parent's sibling */
563 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200564 /* for parent LYS_AUGMENT */
565 if (iter->parent == stop) {
566 iter = stop;
567 break;
568 }
569 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200570 if (iter && iter->next) {
571 break;
572 }
573 } while (iter != stop);
574
575 if (iter == stop) {
576 break;
577 }
578 }
579 iter = iter->next;
580 }
581 break;
582 case LYS_CASE:
583 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100584 if (parent) {
585 start = parent->child;
586 } else {
587 start = module->data;
588 }
589
590 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200591 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200592 continue;
593 }
594
Radek Krejci749190d2016-02-18 16:26:25 +0100595 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100596 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200597 return EXIT_FAILURE;
598 }
599 }
600 break;
601 default:
602 /* no check needed */
603 break;
604 }
605
606 return EXIT_SUCCESS;
607}
608
Michal Vasko0d343d12015-08-24 14:57:36 +0200609/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200610int
Radek Krejci10c760e2015-08-14 14:45:43 +0200611lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
612{
Radek Krejci92720552015-10-05 15:28:27 +0200613 struct lys_node *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200614 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200615
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200616 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200617
Radek Krejci10c760e2015-08-14 14:45:43 +0200618 if (parent) {
619 type = parent->nodetype;
620 module = parent->module;
621 } else {
622 assert(module);
623 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200624 }
625
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200626 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200627 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200628 case LYS_CONTAINER:
629 case LYS_LIST:
630 case LYS_GROUPING:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200631 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200632 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200633 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) {
634 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
635 return EXIT_FAILURE;
636 }
637 break;
Radek Krejci76512572015-08-04 09:47:08 +0200638 case LYS_USES:
639 case LYS_INPUT:
640 case LYS_OUTPUT:
641 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200642 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200643 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200644 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100645 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200646 return EXIT_FAILURE;
647 }
648 break;
Radek Krejci76512572015-08-04 09:47:08 +0200649 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200650 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200651 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100652 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200653 return EXIT_FAILURE;
654 }
655 break;
Radek Krejci76512572015-08-04 09:47:08 +0200656 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200657 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200658 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100659 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200660 return EXIT_FAILURE;
661 }
662 break;
Radek Krejci76512572015-08-04 09:47:08 +0200663 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200664 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200665 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100666 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200667 return EXIT_FAILURE;
668 }
669 break;
Radek Krejci76512572015-08-04 09:47:08 +0200670 case LYS_LEAF:
671 case LYS_LEAFLIST:
672 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200673 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100674 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
675 LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100676 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200677 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200678 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200679 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200680 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200681 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100682 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200683 return EXIT_FAILURE;
684 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200685 break;
686 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200687 /* top level */
688 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200689 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200690 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100691 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200692 return EXIT_FAILURE;
693 }
694
Radek Krejcic071c542016-01-27 14:57:51 +0100695 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200696 }
697
698 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200699 if (lys_check_id(child, parent, module)) {
700 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200701 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200702
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200703 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200704 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200705 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200706
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200707 /* connect the child correctly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200708 if (!parent) {
Radek Krejci92720552015-10-05 15:28:27 +0200709 if (module->data) {
710 module->data->prev->next = child;
711 child->prev = module->data->prev;
712 module->data->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200713 } else {
Radek Krejci92720552015-10-05 15:28:27 +0200714 module->data = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200715 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200716 } else {
Radek Krejci10c760e2015-08-14 14:45:43 +0200717 if (!parent->child) {
718 /* the only/first child of the parent */
719 parent->child = child;
720 child->parent = parent;
721 iter = child;
722 } else {
723 /* add a new child at the end of parent's child list */
724 iter = parent->child->prev;
725 iter->next = child;
726 child->prev = iter;
727 }
728 while (iter->next) {
729 iter = iter->next;
730 iter->parent = parent;
731 }
732 parent->child->prev = iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200733 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200734
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200735 /* check config value */
736 if (parent && !(parent->nodetype & (LYS_GROUPING | LYS_AUGMENT))) {
737 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
738 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
739 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
740 LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children.");
741 return EXIT_FAILURE;
742 }
743 }
744
Radek Krejci41771502016-04-14 17:52:32 +0200745 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200746 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200747 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200748 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200749 /* store it only into container or list - the only data inner nodes */
750 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
751 if (iter->flags & LYS_INCL_STATUS) {
752 /* done, someone else set it already from here */
753 break;
754 }
755 /* set flag about including status data */
756 iter->flags |= LYS_INCL_STATUS;
757 }
758 }
759 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200760 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200761}
762
Radek Krejcia1df1682016-04-11 14:56:59 +0200763static const struct lys_module *
764lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200765{
Radek Krejcia1df1682016-04-11 14:56:59 +0200766 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200767 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200768 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200769
Radek Krejcif347abc2016-06-22 10:18:47 +0200770 ly_errno = LY_SUCCESS;
771
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200772 if (!ctx || !data) {
773 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
774 return NULL;
775 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200776
Radek Krejcia1df1682016-04-11 14:56:59 +0200777 if (!internal && format == LYS_IN_YANG) {
778 /* enlarge data by 2 bytes for flex */
779 len = strlen(data);
780 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
781 if (!enlarged_data) {
782 LOGMEM;
783 return NULL;
784 }
785 memcpy(enlarged_data, data, len);
786 enlarged_data[len] = enlarged_data[len + 1] = '\0';
787 data = enlarged_data;
788 }
789
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200790 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200791 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +0100792 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200793 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200794 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100795 mod = yang_read_module(ctx, data, 0, NULL, 1);
796 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200797 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200798 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200799 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200800 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200801
Radek Krejcia1df1682016-04-11 14:56:59 +0200802 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200803 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200804}
805
Radek Krejcia1df1682016-04-11 14:56:59 +0200806API const struct lys_module *
807lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
808{
809 return lys_parse_mem_(ctx, data, format, 0);
810}
811
Michal Vasko5a721fd2016-02-16 12:16:48 +0100812struct lys_submodule *
813lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200814{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100815 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200816
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200817 assert(module);
818 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +0200819
Radek Krejcic071c542016-01-27 14:57:51 +0100820 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +0200821 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200822
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200823 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200824 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +0100825 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200826 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200827 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100828 submod = yang_read_submodule(module, data, 0, unres);
829 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200830 default:
Radek Krejci90a550a2016-04-13 16:00:58 +0200831 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200832 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200833 }
Radek Krejciefaeba32015-05-27 14:30:57 +0200834
Michal Vasko5a721fd2016-02-16 12:16:48 +0100835 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +0200836}
837
Michal Vasko1e62a092015-12-01 12:27:20 +0100838API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +0100839lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
840{
841 int fd;
842 const struct lys_module *ret;
843
844 if (!ctx || !path) {
845 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
846 return NULL;
847 }
848
849 fd = open(path, O_RDONLY);
850 if (fd == -1) {
851 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
852 return NULL;
853 }
854
855 ret = lys_parse_fd(ctx, fd, format);
856 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100857
Radek Krejcia77904e2016-02-25 16:23:45 +0100858 if (ret && !ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100859 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100860 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100861 }
862
Michal Vasko662610a2015-12-07 11:25:45 +0100863 return ret;
864}
865
866API const struct lys_module *
867lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +0200868{
Michal Vasko1e62a092015-12-01 12:27:20 +0100869 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +0200870 struct stat sb;
871 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +0100872 char buf[PATH_MAX];
873 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +0200874
875 if (!ctx || fd < 0) {
876 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
877 return NULL;
878 }
879
Radek Krejci10a833c2015-12-16 15:28:37 +0100880 if (fstat(fd, &sb) == -1) {
881 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
882 return NULL;
883 }
Radek Krejcib051f722016-02-25 15:12:21 +0100884 if (!S_ISREG(sb.st_mode)) {
885 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
886 return NULL;
887 }
888
Michal Vasko164d9012016-04-01 10:16:59 +0200889 if (!sb.st_size) {
890 LOGERR(LY_EINVAL, "File empty.");
891 return NULL;
892 }
893
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100894 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100895 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +0100896 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +0100897 return NULL;
898 }
Radek Krejcia1df1682016-04-11 14:56:59 +0200899 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100900 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200901
Radek Krejcia77904e2016-02-25 16:23:45 +0100902 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +0100903 /* get URI if there is /proc */
904 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +0100905 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
906 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
907 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
908 }
909 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +0100910 }
Radek Krejcib051f722016-02-25 15:12:21 +0100911 }
912
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200913 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200914}
915
Michal Vasko5a721fd2016-02-16 12:16:48 +0100916struct lys_submodule *
917lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200918{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100919 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200920 struct stat sb;
921 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +0200922
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200923 assert(module);
924 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +0200925
Radek Krejci10a833c2015-12-16 15:28:37 +0100926 if (fstat(fd, &sb) == -1) {
927 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +0100928 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +0100929 }
Michal Vasko164d9012016-04-01 10:16:59 +0200930
931 if (!sb.st_size) {
932 LOGERR(LY_EINVAL, "File empty.");
933 return NULL;
934 }
935
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100936 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100937 if (addr == MAP_FAILED) {
938 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +0100939 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +0100940 }
Michal Vasko5a721fd2016-02-16 12:16:48 +0100941 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100942 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +0200943
Michal Vasko5a721fd2016-02-16 12:16:48 +0100944 return submodule;
945
Radek Krejciefaeba32015-05-27 14:30:57 +0200946}
947
Radek Krejci1d82ef62015-08-07 14:44:40 +0200948static struct lys_restr *
949lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200950{
Radek Krejci1574a8d2015-08-03 14:16:52 +0200951 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200952 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200953
Radek Krejci3733a802015-06-19 13:43:21 +0200954 if (!size) {
955 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200956 }
Radek Krejci3733a802015-06-19 13:43:21 +0200957
958 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +0100959 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +0100960 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +0100961 return NULL;
962 }
Radek Krejci3733a802015-06-19 13:43:21 +0200963 for (i = 0; i < size; i++) {
964 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
965 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
966 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
967 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
968 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
969 }
970
971 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200972}
973
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200974void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200975lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +0200976{
977 assert(ctx);
978 if (!restr) {
979 return;
980 }
981
982 lydict_remove(ctx, restr->expr);
983 lydict_remove(ctx, restr->dsc);
984 lydict_remove(ctx, restr->ref);
985 lydict_remove(ctx, restr->eapptag);
986 lydict_remove(ctx, restr->emsg);
987}
988
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200989static void
990lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
991{
992 uint8_t i;
993
994 for (i = 0; i < iffeature_size; ++i) {
995 free(iffeature[i].expr);
996 free(iffeature[i].features);
997 }
998 free(iffeature);
999}
1000
Michal Vaskob84f88a2015-09-24 13:16:10 +02001001static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001002type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001003 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001004{
1005 int i;
1006
1007 switch (base) {
1008 case LY_TYPE_BINARY:
1009 if (old->info.binary.length) {
1010 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1011 }
1012 break;
1013
1014 case LY_TYPE_BITS:
1015 new->info.bits.count = old->info.bits.count;
1016 if (new->info.bits.count) {
1017 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1018 if (!new->info.bits.bit) {
1019 LOGMEM;
1020 return -1;
1021 }
1022 for (i = 0; i < new->info.bits.count; i++) {
1023 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1024 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1025 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1026 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1027 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1028 }
1029 }
1030 break;
1031
1032 case LY_TYPE_DEC64:
1033 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001034 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001035 if (old->info.dec64.range) {
1036 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1037 }
1038 break;
1039
1040 case LY_TYPE_ENUM:
1041 new->info.enums.count = old->info.enums.count;
1042 if (new->info.enums.count) {
1043 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1044 if (!new->info.enums.enm) {
1045 LOGMEM;
1046 return -1;
1047 }
1048 for (i = 0; i < new->info.enums.count; i++) {
1049 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1050 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1051 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1052 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1053 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1054 }
1055 }
1056 break;
1057
1058 case LY_TYPE_IDENT:
Michal Vasko878e38d2016-09-05 12:17:53 +02001059 if (old->info.ident.count) {
1060 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1061 if (!new->info.ident.ref) {
1062 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001063 return -1;
1064 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001065 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1066 } else {
1067 /* there can be several unresolved base identities, duplicate them all */
1068 i = -1;
1069 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1070 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1071 return -1;
1072 }
1073 --i;
1074 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001075 }
1076 break;
1077
1078 case LY_TYPE_INST:
1079 new->info.inst.req = old->info.inst.req;
1080 break;
1081
1082 case LY_TYPE_INT8:
1083 case LY_TYPE_INT16:
1084 case LY_TYPE_INT32:
1085 case LY_TYPE_INT64:
1086 case LY_TYPE_UINT8:
1087 case LY_TYPE_UINT16:
1088 case LY_TYPE_UINT32:
1089 case LY_TYPE_UINT64:
1090 if (old->info.num.range) {
1091 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1092 }
1093 break;
1094
1095 case LY_TYPE_LEAFREF:
1096 if (old->info.lref.path) {
1097 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001098 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001099 return -1;
1100 }
1101 }
1102 break;
1103
1104 case LY_TYPE_STRING:
1105 if (old->info.str.length) {
1106 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1107 }
1108 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1109 new->info.str.pat_count = old->info.str.pat_count;
1110 break;
1111
1112 case LY_TYPE_UNION:
1113 new->info.uni.count = old->info.uni.count;
1114 if (new->info.uni.count) {
1115 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1116 if (!new->info.uni.types) {
1117 LOGMEM;
1118 return -1;
1119 }
1120 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001121 if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001122 return -1;
1123 }
1124 }
1125 }
1126 break;
1127
1128 default:
1129 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1130 break;
1131 }
1132 return EXIT_SUCCESS;
1133}
1134
1135struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001136lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1137 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001138{
1139 struct yang_type *new;
1140
1141 new = calloc(1, sizeof *new);
1142 if (!new) {
1143 LOGMEM;
1144 return NULL;
1145 }
1146 new->flags = old->flags;
1147 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001148 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001149 new->type = type;
1150 if (!new->name) {
1151 LOGMEM;
1152 goto error;
1153 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001154 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001155 new->type->base = new->base;
1156 lys_type_free(module->ctx, new->type);
1157 memset(&new->type->info, 0, sizeof new->type->info);
1158 goto error;
1159 }
1160 return new;
1161
1162 error:
1163 free(new);
1164 return NULL;
1165}
1166
1167static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001168lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001169 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001170{
1171 int i;
1172
Michal Vasko1dca6882015-10-22 14:29:42 +02001173 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001174 new->base = old->base;
1175 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001176 new->parent = (struct lys_tpdf *)parent;
Radek Krejci3733a802015-06-19 13:43:21 +02001177
Michal Vasko878e38d2016-09-05 12:17:53 +02001178 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001179 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001180 /* HACK (serious one) for unres */
1181 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001182 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001183 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, tpdftype, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001184 } else {
1185 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1186 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001187 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001188 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001189 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001190 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001191 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001192 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001193 }
1194
Radek Krejci3a5501d2016-07-18 22:03:34 +02001195 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001196}
1197
1198void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001199lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001200{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001201 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001202
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001203 assert(ctx);
1204 if (!type) {
1205 return;
1206 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001207
Michal Vasko1dca6882015-10-22 14:29:42 +02001208 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001209
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001210 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001211 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001212 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001213 free(type->info.binary.length);
1214 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001215 case LY_TYPE_BITS:
1216 for (i = 0; i < type->info.bits.count; i++) {
1217 lydict_remove(ctx, type->info.bits.bit[i].name);
1218 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1219 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001220 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001221 }
1222 free(type->info.bits.bit);
1223 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001224
1225 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001226 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001227 free(type->info.dec64.range);
1228 break;
1229
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001230 case LY_TYPE_ENUM:
1231 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001232 lydict_remove(ctx, type->info.enums.enm[i].name);
1233 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1234 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001235 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001236 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001237 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001238 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001239
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001240 case LY_TYPE_INT8:
1241 case LY_TYPE_INT16:
1242 case LY_TYPE_INT32:
1243 case LY_TYPE_INT64:
1244 case LY_TYPE_UINT8:
1245 case LY_TYPE_UINT16:
1246 case LY_TYPE_UINT32:
1247 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001248 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001249 free(type->info.num.range);
1250 break;
1251
Radek Krejcidc4c1412015-06-19 15:39:54 +02001252 case LY_TYPE_LEAFREF:
1253 lydict_remove(ctx, type->info.lref.path);
1254 break;
1255
Radek Krejci3733a802015-06-19 13:43:21 +02001256 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001257 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001258 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001259 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001260 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001261 }
1262 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001263 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001264
Radek Krejcie4c366b2015-07-02 10:11:31 +02001265 case LY_TYPE_UNION:
1266 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001267 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001268 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001269 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001270 break;
1271
Michal Vaskod3282192016-09-05 11:27:57 +02001272 case LY_TYPE_IDENT:
1273 free(type->info.ident.ref);
1274 break;
1275
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001276 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001277 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001278 break;
1279 }
Radek Krejci5a065542015-05-22 15:02:07 +02001280}
1281
Radek Krejci1d82ef62015-08-07 14:44:40 +02001282static void
1283lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001284{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001285 assert(ctx);
1286 if (!tpdf) {
1287 return;
1288 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001289
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001290 lydict_remove(ctx, tpdf->name);
1291 lydict_remove(ctx, tpdf->dsc);
1292 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001293
Radek Krejci1d82ef62015-08-07 14:44:40 +02001294 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001295
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001296 lydict_remove(ctx, tpdf->units);
1297 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001298}
1299
Michal Vaskob84f88a2015-09-24 13:16:10 +02001300static struct lys_tpdf *
1301lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1302{
1303 struct lys_tpdf *result;
1304 int i, j;
1305
1306 if (!size) {
1307 return NULL;
1308 }
1309
1310 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001311 if (!result) {
1312 LOGMEM;
1313 return NULL;
1314 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001315 for (i = 0; i < size; i++) {
1316 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1317 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1318 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1319 result[i].flags = old[i].flags;
1320 result[i].module = old[i].module;
1321
Radek Krejci3a5501d2016-07-18 22:03:34 +02001322 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001323 for (j = 0; j <= i; ++j) {
1324 lys_tpdf_free(mod->ctx, &result[j]);
1325 }
1326 free(result);
1327 return NULL;
1328 }
1329
1330 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1331 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1332 }
1333
1334 return result;
1335}
1336
Radek Krejci1d82ef62015-08-07 14:44:40 +02001337static struct lys_when *
1338lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001339{
Radek Krejci76512572015-08-04 09:47:08 +02001340 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001341
1342 if (!old) {
1343 return NULL;
1344 }
1345
1346 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001347 if (!new) {
1348 LOGMEM;
1349 return NULL;
1350 }
Radek Krejci00768f42015-06-18 17:04:04 +02001351 new->cond = lydict_insert(ctx, old->cond, 0);
1352 new->dsc = lydict_insert(ctx, old->dsc, 0);
1353 new->ref = lydict_insert(ctx, old->ref, 0);
1354
1355 return new;
1356}
1357
Michal Vasko0308dd62015-10-07 09:14:40 +02001358void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001359lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001360{
1361 if (!w) {
1362 return;
1363 }
1364
1365 lydict_remove(ctx, w->cond);
1366 lydict_remove(ctx, w->dsc);
1367 lydict_remove(ctx, w->ref);
1368
1369 free(w);
1370}
1371
Radek Krejcib7f5e412015-08-13 10:15:51 +02001372static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001373lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02001374{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001375 struct lys_node *next, *sub;
1376
Radek Krejcic071c542016-01-27 14:57:51 +01001377 /* children from a resolved augment are freed under the target node */
Michal Vaskod2fbade2016-05-02 17:14:00 +02001378 if (!aug->target) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001379 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001380 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001381 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001382 }
1383
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001384 lydict_remove(ctx, aug->target_name);
1385 lydict_remove(ctx, aug->dsc);
1386 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001387
Radek Krejci9ff0a922016-07-14 13:08:05 +02001388 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001389
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001390 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001391}
1392
Radek Krejci76512572015-08-04 09:47:08 +02001393static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001394lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001395{
Radek Krejci76512572015-08-04 09:47:08 +02001396 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001397 struct lys_node *old_child, *new_child;
1398 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001399
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001400 if (!size) {
1401 return NULL;
1402 }
Radek Krejci106efc02015-06-10 14:36:27 +02001403
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001404 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001405 if (!new) {
1406 LOGMEM;
1407 return NULL;
1408 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001409 for (i = 0; i < size; i++) {
1410 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1411 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1412 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1413 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001414 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001415 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001416
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001417 /* this must succeed, it was already resolved once */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001418 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL,
1419 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001420 LOGINT;
1421 free(new);
1422 return NULL;
1423 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001424 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001425
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001426 /* Correct the augment nodes.
1427 * This function can only be called from lys_node_dup() with uses
1428 * being the node duplicated, so we must have a case of grouping
1429 * with a uses with augments. The augmented nodes have already been
1430 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001431 * (it was set to their actual data parent, not an augment), and
1432 * the new augment does not have child pointer to its augment nodes,
1433 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001434 */
1435 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001436 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001437 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001438 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001439 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001440 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001441 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001442 LY_TREE_FOR(old[i].child, old_child) {
1443 /* all augment nodes were connected as siblings, there can be no more after this */
1444 if (old_child->parent != (struct lys_node *)&old[i]) {
1445 break;
1446 }
1447
Radek Krejci749190d2016-02-18 16:26:25 +01001448 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001449
1450 new_child->parent = (struct lys_node *)&new[i];
1451 new_child = new_child->next;
1452 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001453 }
Radek Krejci106efc02015-06-10 14:36:27 +02001454
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001455 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001456}
1457
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001458static const char **
1459lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1460{
1461 int i;
1462 const char **result;
1463
1464 if (!size) {
1465 return NULL;
1466 }
1467
1468 result = calloc(size, sizeof *result);
1469 if (!result) {
1470 LOGMEM;
1471 return NULL;
1472 }
1473
1474 for (i = 0; i < size; i++) {
1475 result[i] = lydict_insert(ctx, old[i], 0);
1476 }
1477 return result;
1478}
1479
Radek Krejci76512572015-08-04 09:47:08 +02001480static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001481lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001482{
Radek Krejci76512572015-08-04 09:47:08 +02001483 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001484 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001485
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001486 if (!size) {
1487 return NULL;
1488 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001489
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001490 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001491 if (!result) {
1492 LOGMEM;
1493 return NULL;
1494 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001495 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001496 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001497 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1498 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001499 result[i].flags = old[i].flags;
1500 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001501
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001502 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001503 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001504
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001505 result[i].dflt_size = old[i].dflt_size;
1506 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
1507
1508 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001509 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001510 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001511 result[i].mod.list = old[i].mod.list;
1512 }
1513 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001514
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001515 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001516}
1517
Radek Krejci1d82ef62015-08-07 14:44:40 +02001518static void
1519lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001520{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001521 assert(ctx);
1522 if (!ident) {
1523 return;
1524 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001525
Radek Krejci018f1f52016-08-03 16:01:20 +02001526 free(ident->base);
Radek Krejci1b61d0e2016-04-15 13:55:44 +02001527 free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001528 lydict_remove(ctx, ident->name);
1529 lydict_remove(ctx, ident->dsc);
1530 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001531 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejci6793db02015-05-22 17:49:54 +02001532
1533}
1534
Radek Krejci1d82ef62015-08-07 14:44:40 +02001535static void
1536lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001537{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001538 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001539
Radek Krejcid12f57b2015-08-06 10:43:39 +02001540 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001541 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001542 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001543 }
1544 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001545}
1546
Radek Krejci1d82ef62015-08-07 14:44:40 +02001547static void
Michal Vasko44fb6382016-06-29 11:12:27 +02001548lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02001549{
1550 int i;
1551
1552 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1553 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001554 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001555 }
1556 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02001557
1558 for (i = 0; i < io->must_size; i++) {
1559 lys_restr_free(ctx, &io->must[i]);
1560 }
1561 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001562}
1563
Radek Krejci1d82ef62015-08-07 14:44:40 +02001564static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02001565lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
1566{
1567 int i;
1568
1569 for (i = 0; i < notif->must_size; i++) {
1570 lys_restr_free(ctx, &notif->must[i]);
1571 }
1572 free(notif->must);
1573
1574 for (i = 0; i < notif->tpdf_size; i++) {
1575 lys_tpdf_free(ctx, &notif->tpdf[i]);
1576 }
1577 free(notif->tpdf);
1578}
1579static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02001580lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001581{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001582 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001583
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001584 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001585 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001586 }
1587 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001588
Radek Krejci1d82ef62015-08-07 14:44:40 +02001589 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001590}
1591
Radek Krejci1d82ef62015-08-07 14:44:40 +02001592static void
1593lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001594{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001595 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001596
Radek Krejci46c4cd72016-01-21 15:13:52 +01001597 if (leaf->child) {
1598 /* leafref backlinks */
1599 ly_set_free((struct ly_set *)leaf->child);
1600 }
1601
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001602 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001603 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001604 }
1605 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001606
Radek Krejci1d82ef62015-08-07 14:44:40 +02001607 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001608
Radek Krejci1d82ef62015-08-07 14:44:40 +02001609 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001610 lydict_remove(ctx, leaf->units);
1611 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001612}
1613
Radek Krejci1d82ef62015-08-07 14:44:40 +02001614static void
1615lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001616{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001617 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001618
Radek Krejci46c4cd72016-01-21 15:13:52 +01001619 if (llist->child) {
1620 /* leafref backlinks */
1621 ly_set_free((struct ly_set *)llist->child);
1622 }
1623
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001624 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001625 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001626 }
1627 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001628
Pavol Vican38321d02016-08-16 14:56:02 +02001629 for (i = 0; i < llist->dflt_size; i++) {
1630 lydict_remove(ctx, llist->dflt[i]);
1631 }
1632 free(llist->dflt);
1633
Radek Krejci1d82ef62015-08-07 14:44:40 +02001634 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001635
Radek Krejci1d82ef62015-08-07 14:44:40 +02001636 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001637 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001638}
1639
Radek Krejci1d82ef62015-08-07 14:44:40 +02001640static void
1641lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001642{
Radek Krejci581ce772015-11-10 17:22:40 +01001643 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001644
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001645 /* handle only specific parts for LY_NODE_LIST */
1646 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001647 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001648 }
1649 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001650
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001651 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001652 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001653 }
1654 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001655
Radek Krejci1d82ef62015-08-07 14:44:40 +02001656 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001657
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001658 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02001659 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001660 lydict_remove(ctx, list->unique[i].expr[j]);
1661 }
1662 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001663 }
1664 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001665
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001666 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001667}
1668
Radek Krejci1d82ef62015-08-07 14:44:40 +02001669static void
1670lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001671{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001672 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001673
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001674 /* handle only specific parts for LY_NODE_CONTAINER */
1675 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001676
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001677 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001678 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001679 }
1680 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001681
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001682 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001683 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001684 }
1685 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001686
Radek Krejci1d82ef62015-08-07 14:44:40 +02001687 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001688}
1689
Radek Krejci1d82ef62015-08-07 14:44:40 +02001690static void
1691lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001692{
1693 lydict_remove(ctx, f->name);
1694 lydict_remove(ctx, f->dsc);
1695 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001696 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001697}
1698
Radek Krejci1d82ef62015-08-07 14:44:40 +02001699static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001700lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001701{
Radek Krejci581ce772015-11-10 17:22:40 +01001702 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001703 struct ly_ctx *ctx;
1704 struct lys_node *next, *elem;
1705
1706 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001707
1708 lydict_remove(ctx, dev->target_name);
1709 lydict_remove(ctx, dev->dsc);
1710 lydict_remove(ctx, dev->ref);
1711
Pavol Vican64d0b762016-08-25 10:44:59 +02001712 if (!dev->deviate) {
1713 return ;
1714 }
1715
Michal Vaskoff006c12016-02-17 11:15:19 +01001716 /* the module was freed, but we only need the context from orig_node, use ours */
1717 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1718 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1719 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1720 elem->module = module;
1721
1722 LY_TREE_DFS_END(dev->orig_node, next, elem);
1723 }
1724 lys_node_free(dev->orig_node, NULL, 0);
1725 } else {
1726 /* it's just a shallow copy, freeing one node */
1727 dev->orig_node->module = module;
1728 lys_node_free(dev->orig_node, NULL, 1);
1729 }
1730
Radek Krejcieb00f512015-07-01 16:44:58 +02001731 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001732 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02001733 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02001734 }
1735 free(dev->deviate[i].dflt);
1736
Radek Krejcieb00f512015-07-01 16:44:58 +02001737 lydict_remove(ctx, dev->deviate[i].units);
1738
1739 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1740 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001741 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001742 }
1743 free(dev->deviate[i].must);
1744
1745 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001746 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1747 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1748 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001749 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001750 }
1751 free(dev->deviate[i].unique);
1752 }
1753 }
1754 free(dev->deviate);
1755}
1756
Radek Krejci1d82ef62015-08-07 14:44:40 +02001757static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001758lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02001759{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001760 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001761
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001762 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001763 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001764 lydict_remove(ctx, uses->refine[i].dsc);
1765 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001766
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001767 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001768 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001769 }
1770 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001771
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001772 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02001773 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001774 }
1775 free(uses->refine[i].dflt);
1776
1777 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001778 lydict_remove(ctx, uses->refine[i].mod.presence);
1779 }
1780 }
1781 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001782
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001783 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001784 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001785 }
1786 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001787
Radek Krejci1d82ef62015-08-07 14:44:40 +02001788 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001789}
1790
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001791void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001792lys_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 +02001793{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001794 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001795 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001796
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001797 if (!node) {
1798 return;
1799 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001800
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001801 assert(node->module);
1802 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001803
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001804 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001805
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001806 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001807 if (node->priv && private_destructor) {
1808 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001809 }
1810
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001811 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001812 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001813 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001814 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001815 lydict_remove(ctx, node->dsc);
1816 lydict_remove(ctx, node->ref);
1817 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001818
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001819 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001820 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001821 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001822 }
1823 }
1824
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001825 /* specific part */
1826 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001827 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001828 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001829 break;
Radek Krejci76512572015-08-04 09:47:08 +02001830 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001831 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001832 break;
Radek Krejci76512572015-08-04 09:47:08 +02001833 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001834 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001835 break;
Radek Krejci76512572015-08-04 09:47:08 +02001836 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001837 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001838 break;
Radek Krejci76512572015-08-04 09:47:08 +02001839 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001840 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001841 break;
Radek Krejci76512572015-08-04 09:47:08 +02001842 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001843 case LYS_ANYDATA:
1844 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001845 break;
Radek Krejci76512572015-08-04 09:47:08 +02001846 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001847 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001848 break;
Radek Krejci76512572015-08-04 09:47:08 +02001849 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001850 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001851 break;
Radek Krejci76512572015-08-04 09:47:08 +02001852 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001853 /* do nothing */
1854 break;
Radek Krejci76512572015-08-04 09:47:08 +02001855 case LYS_GROUPING:
1856 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001857 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001858 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001859 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02001860 case LYS_NOTIF:
1861 lys_notif_free(ctx, (struct lys_node_notif *)node);
1862 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001863 case LYS_INPUT:
1864 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001865 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001866 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001867 case LYS_UNKNOWN:
1868 LOGINT;
1869 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001870 }
Radek Krejci5a065542015-05-22 15:02:07 +02001871
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001872 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001873 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001874 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001875}
1876
Michal Vasko1e62a092015-12-01 12:27:20 +01001877const struct lys_module *
1878lys_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 +02001879{
Radek Krejcic071c542016-01-27 14:57:51 +01001880 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02001881 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001882 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02001883
Michal Vaskoa7789a82016-02-11 15:42:55 +01001884 assert(!prefix || !name);
1885
Michal Vaskob6729c62015-10-21 12:09:47 +02001886 if (prefix && !pref_len) {
1887 pref_len = strlen(prefix);
1888 }
1889 if (name && !name_len) {
1890 name_len = strlen(name);
1891 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02001892
Radek Krejcic4283442016-04-22 09:19:27 +02001893 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01001894
1895 /* module own prefix, submodule own prefix, (sub)module own name */
1896 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
1897 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01001898 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01001899 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02001900 }
1901
Michal Vasko89563fc2016-07-28 16:19:35 +02001902 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02001903 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001904 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
1905 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02001906 return module->imp[i].module;
1907 }
1908 }
1909
Michal Vasko89563fc2016-07-28 16:19:35 +02001910 /* module required by a foreign grouping, deviation, or submodule */
1911 if (name) {
1912 str = strndup(name, name_len);
1913 if (!str) {
1914 LOGMEM;
1915 return NULL;
1916 }
1917 main_module = ly_ctx_get_module(module->ctx, str, NULL);
1918 free(str);
1919 return main_module;
1920 }
1921
Michal Vasko8ce24d72015-10-21 11:27:26 +02001922 return NULL;
1923}
1924
Michal Vasko13b15832015-08-19 11:04:48 +02001925/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001926static void
Michal Vaskob746fff2016-02-11 11:37:50 +01001927module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02001928{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001929 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01001930 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001931 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001932
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001933 assert(module->ctx);
1934 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001935
Michal Vaskob746fff2016-02-11 11:37:50 +01001936 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01001937 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02001938 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02001939 lydict_remove(ctx, module->imp[i].dsc);
1940 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci225376f2016-02-16 17:36:22 +01001941 }
Radek Krejcidce51452015-06-16 15:20:08 +02001942 free(module->imp);
1943
Radek Krejcic071c542016-01-27 14:57:51 +01001944 /* submodules don't have data tree, the data nodes
1945 * are placed in the main module altogether */
1946 if (!module->type) {
1947 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001948 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001949 }
Radek Krejci21181962015-06-30 14:11:00 +02001950 }
Radek Krejci5a065542015-05-22 15:02:07 +02001951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001952 lydict_remove(ctx, module->dsc);
1953 lydict_remove(ctx, module->ref);
1954 lydict_remove(ctx, module->org);
1955 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01001956 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001957
Radek Krejcieb00f512015-07-01 16:44:58 +02001958 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001959 for (i = 0; i < module->rev_size; i++) {
1960 lydict_remove(ctx, module->rev[i].dsc);
1961 lydict_remove(ctx, module->rev[i].ref);
1962 }
1963 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001964
Radek Krejcieb00f512015-07-01 16:44:58 +02001965 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001966 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001967 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001968 }
1969 module->ident_size = 0;
1970 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001971
Radek Krejcieb00f512015-07-01 16:44:58 +02001972 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001973 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001974 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001975 }
1976 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001977
Radek Krejcieb00f512015-07-01 16:44:58 +02001978 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001979 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02001980 lydict_remove(ctx, module->inc[i].dsc);
1981 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcic071c542016-01-27 14:57:51 +01001982 /* complete submodule free is done only from main module since
1983 * submodules propagate their includes to the main module */
1984 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01001985 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01001986 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001987 }
1988 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02001989
Radek Krejcieb00f512015-07-01 16:44:58 +02001990 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02001991 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001992 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02001993 }
1994 free(module->augment);
1995
Radek Krejcieb00f512015-07-01 16:44:58 +02001996 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02001997 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001998 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001999 }
2000 free(module->features);
2001
Radek Krejcieb00f512015-07-01 16:44:58 +02002002 /* deviations */
2003 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002004 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002005 }
2006 free(module->deviation);
2007
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002008 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002009 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002010}
2011
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002012void
Michal Vaskob746fff2016-02-11 11:37:50 +01002013lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002014{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002015 if (!submodule) {
2016 return;
2017 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002018
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002019 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002020 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002021
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002022 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002023
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002024 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002025}
2026
Radek Krejci3a5501d2016-07-18 22:03:34 +02002027static int
2028ingrouping(const struct lys_node *node)
2029{
2030 const struct lys_node *iter = node;
2031 assert(node);
2032
2033 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2034 if (!iter) {
2035 return 0;
2036 } else {
2037 return 1;
2038 }
2039}
2040
Radek Krejci76512572015-08-04 09:47:08 +02002041struct lys_node *
Radek Krejcic071c542016-01-27 14:57:51 +01002042lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t flags,
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002043 uint8_t nacm, struct unres_schema *unres, int shallow)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002044{
Radek Krejcic071c542016-01-27 14:57:51 +01002045 struct lys_node *retval = NULL, *child;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002046 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002047 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002048 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002049 struct unres_list_uniq *unique_info;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002050
Michal Vaskoc07187d2015-08-13 15:20:57 +02002051 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002052 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002053 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002054 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002055 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002056 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002057 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002058 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002059 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002060 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002061 struct lys_node_anydata *any = NULL;
2062 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002063 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002064 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002065 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002066 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002067 struct lys_node_rpc_action *rpc = NULL;
2068 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2069 struct lys_node_inout *io = NULL;
2070 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2071 struct lys_node_rpc_action *ntf = NULL;
2072 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002073 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002074 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002075
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002076 /* we cannot just duplicate memory since the strings are stored in
2077 * dictionary and we need to update dictionary counters.
2078 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002079
Radek Krejci1d82ef62015-08-07 14:44:40 +02002080 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002081 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002082 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002083 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002084 break;
2085
Radek Krejci76512572015-08-04 09:47:08 +02002086 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002087 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002088 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002089 break;
2090
Radek Krejci76512572015-08-04 09:47:08 +02002091 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002092 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002093 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002094 break;
2095
Radek Krejci76512572015-08-04 09:47:08 +02002096 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002097 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002098 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002099 break;
2100
Radek Krejci76512572015-08-04 09:47:08 +02002101 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002102 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002103 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002104 break;
2105
Radek Krejci76512572015-08-04 09:47:08 +02002106 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002107 case LYS_ANYDATA:
2108 any = calloc(1, sizeof *any);
2109 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002110 break;
2111
Radek Krejci76512572015-08-04 09:47:08 +02002112 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002114 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002115 break;
2116
Radek Krejci76512572015-08-04 09:47:08 +02002117 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002118 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002119 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002120 break;
2121
Radek Krejci76512572015-08-04 09:47:08 +02002122 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002123 grp = calloc(1, sizeof *grp);
2124 retval = (struct lys_node *)grp;
2125 break;
2126
Radek Krejci76512572015-08-04 09:47:08 +02002127 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002128 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002129 rpc = calloc(1, sizeof *rpc);
2130 retval = (struct lys_node *)rpc;
2131 break;
2132
Radek Krejci76512572015-08-04 09:47:08 +02002133 case LYS_INPUT:
2134 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002135 io = calloc(1, sizeof *io);
2136 retval = (struct lys_node *)io;
2137 break;
2138
Radek Krejci76512572015-08-04 09:47:08 +02002139 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002140 ntf = calloc(1, sizeof *ntf);
2141 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002142 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002143
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002144 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002145 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002146 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002147 }
Radek Krejcib388c152015-06-04 17:03:03 +02002148
Michal Vasko253035f2015-12-17 16:58:13 +01002149 if (!retval) {
2150 LOGMEM;
2151 return NULL;
2152 }
2153
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002154 /*
2155 * duplicate generic part of the structure
2156 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002157 retval->name = lydict_insert(ctx, node->name, 0);
2158 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2159 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002160 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002161 retval->flags = node->flags;
Radek Krejci1574a8d2015-08-03 14:16:52 +02002162 if (!(retval->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002163 /* set parent's config flag */
Radek Krejci1574a8d2015-08-03 14:16:52 +02002164 retval->flags |= flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002165 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002166
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002167 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002168 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002169
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002170 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002171
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002172 retval->iffeature_size = node->iffeature_size;
2173 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2174 if (!retval->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01002175 LOGMEM;
2176 goto error;
2177 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002178
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002179 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002180 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002181 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2182 if (size1) {
2183 /* there is something to duplicate */
2184
2185 /* duplicate compiled expression */
2186 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2187 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2188 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2189
2190 /* list of feature pointer must be updated to point to the resulting tree */
2191 retval->iffeature[i].features = malloc(size2 * sizeof *retval->iffeature[i].features);
2192 for (j = 0; (unsigned int)j < size2; j++) {
2193 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2194 &retval->iffeature[i].features[j]);
2195 if (rc == EXIT_SUCCESS) {
2196 /* feature is not resolved, duplicate the expression string */
2197 retval->iffeature[i].features[j] = (void *)strdup((char *)node->iffeature[i].features[j]);
2198 } else if (rc == EXIT_FAILURE) {
2199 /* feature is resolved in origin, so copy it
2200 * - duplication is used for instantiating groupings
2201 * and if-feature inside grouping is supposed to be
2202 * resolved inside the original grouping, so we want
2203 * to keep pointers to features from the grouping
2204 * context */
2205 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2206 } else if (rc == -1) {
2207 goto error;
2208 }
2209 }
2210 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002211 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002212
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002213 /* connect it to the parent */
2214 if (lys_node_addchild(parent, retval->module, retval)) {
2215 goto error;
2216 }
Radek Krejcidce51452015-06-16 15:20:08 +02002217
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002218 /* go recursively */
2219 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2220 LY_TREE_FOR(node->child, child) {
2221 if (!lys_node_dup(module, retval, child, retval->flags, retval->nacm, unres, 0)) {
2222 goto error;
2223 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002224 }
2225 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002226 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002227 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002228 }
2229
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002230 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002231 * duplicate specific part of the structure
2232 */
2233 switch (node->nodetype) {
2234 case LYS_CONTAINER:
2235 if (cont_orig->when) {
2236 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002237 }
2238 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002239
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002240 cont->must_size = cont_orig->must_size;
2241 cont->tpdf_size = cont_orig->tpdf_size;
2242
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002243 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002244 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002245 break;
2246
2247 case LYS_CHOICE:
2248 if (choice_orig->when) {
2249 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002250 }
2251
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002252 if (!shallow) {
2253 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002254 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2255 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2256 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002257 if (rc) {
2258 if (rc == EXIT_FAILURE) {
2259 LOGINT;
2260 }
2261 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002262 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002263 } else {
2264 /* useless to check return value, we don't know whether
2265 * there really wasn't any default defined or it just hasn't
2266 * been resolved, we just hope for the best :)
2267 */
2268 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002269 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002270 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002271 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002272 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002273 break;
2274
2275 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002276 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002277 goto error;
2278 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002279 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2280
2281 if (leaf_orig->dflt) {
2282 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci48464ed2016-03-17 15:44:09 +01002283 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002284 goto error;
2285 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002286 }
2287
2288 leaf->must_size = leaf_orig->must_size;
2289 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002290
2291 if (leaf_orig->when) {
2292 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002293 }
2294 break;
2295
2296 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002297 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002298 goto error;
2299 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002300 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2301
2302 llist->min = llist_orig->min;
2303 llist->max = llist_orig->max;
2304
2305 llist->must_size = llist_orig->must_size;
2306 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002307
2308 if (llist_orig->when) {
2309 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002310 }
2311 break;
2312
2313 case LYS_LIST:
2314 list->min = list_orig->min;
2315 list->max = list_orig->max;
2316
2317 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002318 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002319
Radek Krejci581ce772015-11-10 17:22:40 +01002320 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002321 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002322
Radek Krejci581ce772015-11-10 17:22:40 +01002323 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002324 if (list->keys_size) {
2325 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01002326 if (!list->keys) {
2327 LOGMEM;
2328 goto error;
2329 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002330
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002331 if (!shallow) {
2332 /* we managed to resolve it before, resolve it again manually */
2333 if (list_orig->keys[0]) {
2334 for (i = 0; i < list->keys_size; ++i) {
2335 rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF,
2336 (const struct lys_node **)&list->keys[i]);
2337 if (rc) {
2338 if (rc == EXIT_FAILURE) {
2339 LOGINT;
2340 }
2341 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002342 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002343 }
2344 /* it was not resolved yet, add unres copy */
2345 } else {
2346 if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) {
2347 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002348 goto error;
2349 }
Michal Vasko38d01f72015-06-15 09:41:06 +02002350 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002351 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002352 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002353 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002354 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002355
Radek Krejci581ce772015-11-10 17:22:40 +01002356 list->unique_size = list_orig->unique_size;
2357 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002358 if (!list->unique) {
2359 LOGMEM;
2360 goto error;
2361 }
Radek Krejci581ce772015-11-10 17:22:40 +01002362 for (i = 0; i < list->unique_size; ++i) {
2363 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2364 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002365 if (!list->unique[i].expr) {
2366 LOGMEM;
2367 goto error;
2368 }
Radek Krejci581ce772015-11-10 17:22:40 +01002369 for (j = 0; j < list->unique[i].expr_size; j++) {
2370 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2371
2372 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02002373 unique_info = malloc(sizeof *unique_info);
2374 unique_info->list = (struct lys_node *)list;
2375 unique_info->expr = list->unique[i].expr[j];
2376 unique_info->trg_type = &list->unique[i].trg_type;
2377 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002378 }
2379 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002380
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002381 if (list_orig->when) {
2382 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02002383 }
Radek Krejcidce51452015-06-16 15:20:08 +02002384 break;
2385
2386 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002387 case LYS_ANYDATA:
2388 any->must_size = any_orig->must_size;
2389 any->must = lys_restr_dup(ctx, any_orig->must, any->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002390
Radek Krejcibf2abff2016-08-23 15:51:52 +02002391 if (any_orig->when) {
2392 any->when = lys_when_dup(ctx, any_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002393 }
2394 break;
2395
2396 case LYS_USES:
2397 uses->grp = uses_orig->grp;
2398
2399 if (uses_orig->when) {
2400 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002401 }
2402
2403 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002404 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002405 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002406 if (!shallow) {
2407 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Michal Vaskocda51712016-05-19 15:22:11 +02002408 if (!uses->grp || uses->grp->nacm) {
2409 assert(!uses->child);
Radek Krejci48464ed2016-03-17 15:44:09 +01002410 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002411 goto error;
2412 }
Michal Vasko49168a22015-08-17 16:35:41 +02002413 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002414 } else {
2415 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002416 }
2417 break;
2418
Radek Krejcidce51452015-06-16 15:20:08 +02002419 case LYS_CASE:
2420 if (cs_orig->when) {
2421 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02002422 }
2423 break;
2424
2425 case LYS_GROUPING:
2426 grp->tpdf_size = grp_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002427 grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002428 break;
2429
2430 case LYS_RPC:
2431 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002432 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002433 break;
2434
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002435 case LYS_INPUT:
2436 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002437 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002438 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002439 break;
2440
Radek Krejcida04f4a2015-05-21 12:54:09 +02002441 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002442 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002443 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002444 break;
2445
2446 default:
2447 /* LY_NODE_AUGMENT */
2448 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002449 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002450 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002451
2452 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002453
2454error:
2455
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002456 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002457 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002458}
2459
Michal Vasko13b15832015-08-19 11:04:48 +02002460void
Michal Vaskoff006c12016-02-17 11:15:19 +01002461lys_node_switch(struct lys_node *dst, struct lys_node *src)
2462{
2463 struct lys_node *child;
2464
Michal Vaskob42b6972016-06-06 14:21:30 +02002465 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01002466
2467 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02002468 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002469 dst->prev->next = src;
2470 }
2471
2472 /* sibling prev */
2473 if (dst->next) {
2474 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02002475 } else {
2476 for (child = dst->prev; child->prev->next; child = child->prev);
2477 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01002478 }
2479
2480 /* next */
2481 src->next = dst->next;
2482 dst->next = NULL;
2483
2484 /* prev */
2485 if (dst->prev != dst) {
2486 src->prev = dst->prev;
2487 }
2488 dst->prev = dst;
2489
2490 /* parent child */
2491 if (dst->parent && (dst->parent->child == dst)) {
2492 dst->parent->child = src;
2493 }
2494
2495 /* parent */
2496 src->parent = dst->parent;
2497 dst->parent = NULL;
2498
2499 /* child parent */
2500 LY_TREE_FOR(dst->child, child) {
2501 if (child->parent == dst) {
2502 child->parent = src;
2503 }
2504 }
2505
2506 /* child */
2507 src->child = dst->child;
2508 dst->child = NULL;
2509}
2510
2511void
Michal Vasko627975a2016-02-11 11:39:03 +01002512lys_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 +02002513{
2514 struct ly_ctx *ctx;
2515 int i;
2516
2517 if (!module) {
2518 return;
2519 }
2520
2521 /* remove schema from the context */
2522 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002523 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002524 for (i = 0; i < ctx->models.used; i++) {
2525 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002526 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002527 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002528 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 +02002529 ctx->models.list[ctx->models.used] = NULL;
2530 /* we are done */
2531 break;
2532 }
2533 }
2534 }
2535
2536 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002537 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002538
2539 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002540 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002541
2542 free(module);
2543}
Radek Krejci7e97c352015-06-19 16:26:34 +02002544
2545/*
2546 * op: 1 - enable, 0 - disable
2547 */
2548static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002549lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002550{
2551 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002552 int i, j, k;
Radek Krejci7e97c352015-06-19 16:26:34 +02002553
2554 if (!module || !name || !strlen(name)) {
2555 return EXIT_FAILURE;
2556 }
2557
2558 if (!strcmp(name, "*")) {
2559 /* enable all */
2560 all = 1;
2561 }
2562
2563 /* module itself */
2564 for (i = 0; i < module->features_size; i++) {
2565 if (all || !strcmp(module->features[i].name, name)) {
2566 if (op) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002567 /* check referenced features if they are enabled */
2568 for (j = 0; j < module->features[i].iffeature_size; j++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02002569 if (!resolve_iffeature(&module->features[i].iffeature[j])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002570 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2571 module->features[i].name, j + 1);
2572 return EXIT_FAILURE;
2573 }
2574 }
2575
Radek Krejci1574a8d2015-08-03 14:16:52 +02002576 module->features[i].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002577 } else {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002578 module->features[i].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002579 }
2580 if (!all) {
2581 return EXIT_SUCCESS;
2582 }
2583 }
2584 }
2585
2586 /* submodules */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002587 for (i = 0; i < module->inc_size; i++) {
2588 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
2589 if (all || !strcmp(module->inc[i].submodule->features[j].name, name)) {
Radek Krejci7e97c352015-06-19 16:26:34 +02002590 if (op) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002591 /* check referenced features if they are enabled */
2592 for (k = 0; k < module->inc[i].submodule->features[j].iffeature_size; k++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02002593 if (!resolve_iffeature(&module->inc[i].submodule->features[j].iffeature[k])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002594 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2595 module->inc[i].submodule->features[j].name, k + 1);
2596 return EXIT_FAILURE;
2597 }
2598 }
2599
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002600 module->inc[i].submodule->features[j].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002601 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002602 module->inc[i].submodule->features[j].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002603 }
2604 if (!all) {
2605 return EXIT_SUCCESS;
2606 }
2607 }
2608 }
2609 }
2610
2611 if (all) {
2612 return EXIT_SUCCESS;
2613 } else {
2614 return EXIT_FAILURE;
2615 }
2616}
2617
2618API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002619lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002620{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002621 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002622}
2623
2624API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002625lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002626{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002627 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002628}
2629
2630API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002631lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002632{
2633 int i, j;
2634
2635 if (!module || !feature) {
2636 return -1;
2637 }
2638
2639 /* search for the specified feature */
2640 /* module itself */
2641 for (i = 0; i < module->features_size; i++) {
2642 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002643 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002644 return 1;
2645 } else {
2646 return 0;
2647 }
2648 }
2649 }
2650
2651 /* submodules */
2652 for (j = 0; j < module->inc_size; j++) {
2653 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2654 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002655 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002656 return 1;
2657 } else {
2658 return 0;
2659 }
2660 }
2661 }
2662 }
2663
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002664 /* feature definition not found */
2665 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002666}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002667
Radek Krejci96a10da2015-07-30 11:00:14 +02002668API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01002669lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002670{
Radek Krejci96a10da2015-07-30 11:00:14 +02002671 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002672 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002673 unsigned int count;
2674
2675 if (!module) {
2676 return NULL;
2677 }
2678
2679 count = module->features_size;
2680 for (i = 0; i < module->inc_size; i++) {
2681 count += module->inc[i].submodule->features_size;
2682 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002683 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01002684 if (!result) {
2685 LOGMEM;
2686 return NULL;
2687 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002688 if (states) {
2689 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01002690 if (!(*states)) {
2691 LOGMEM;
2692 free(result);
2693 return NULL;
2694 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002695 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002696 count = 0;
2697
2698 /* module itself */
2699 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002700 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002701 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002702 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002703 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002704 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002705 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002706 }
2707 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002708 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002709 }
2710
2711 /* submodules */
2712 for (j = 0; j < module->inc_size; j++) {
2713 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002714 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02002715 if (states) {
2716 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
2717 (*states)[count] = 1;
2718 } else {
2719 (*states)[count] = 0;
2720 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002721 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002722 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002723 }
2724 }
2725
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002726 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02002727 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002728
2729 return result;
2730}
Michal Vaskobaefb032015-09-24 14:52:10 +02002731
Radek Krejci6910a032016-04-13 10:06:21 +02002732API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01002733lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01002734{
2735 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
2736}
2737
Radek Krejci6910a032016-04-13 10:06:21 +02002738API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02002739lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01002740{
2741 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
2742}
2743
Michal Vaskobaefb032015-09-24 14:52:10 +02002744API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01002745lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02002746{
2747 if (!node || !node->parent) {
2748 return NULL;
2749 }
2750
2751 if (node->parent->nodetype == LYS_AUGMENT) {
2752 return ((struct lys_node_augment *)node->parent)->target;
2753 }
2754
2755 return node->parent;
2756}
Michal Vasko1b229152016-01-13 11:28:38 +01002757
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002758API void *
Michal Vasko1b229152016-01-13 11:28:38 +01002759lys_set_private(const struct lys_node *node, void *priv)
2760{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002761 void *prev;
2762
Michal Vasko1b229152016-01-13 11:28:38 +01002763 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002764 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
2765 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01002766 }
2767
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002768 prev = node->priv;
2769 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002770
2771 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01002772}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002773
Michal Vasko01c6fd22016-05-20 11:43:05 +02002774int
2775lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
2776{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02002777 struct lys_node_leaf *iter = leafref_target;
2778
Michal Vasko48a573d2016-07-01 11:46:02 +02002779 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002780 LOGINT;
2781 return -1;
2782 }
2783
Pavol Vican93175152016-08-30 15:34:44 +02002784 /* check for config flag */
2785 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
2786 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
2787 "The %s is config but refers to a non-config %s.",
2788 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
2789 return -1;
2790 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02002791 /* check for cycles */
2792 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
2793 if ((void *)iter == (void *)leafref) {
2794 /* cycle detected */
2795 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
2796 return -1;
2797 }
2798 iter = iter->type.info.lref.target;
2799 }
2800
2801 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02002802 * leafrefs referencing the leaf(-list) */
Michal Vasko01c6fd22016-05-20 11:43:05 +02002803 if (!leafref_target->child) {
2804 leafref_target->child = (void*)ly_set_new();
2805 if (!leafref_target->child) {
2806 LOGMEM;
2807 return -1;
2808 }
2809 }
Radek Krejci09891a22016-06-10 10:59:22 +02002810 ly_set_add((struct ly_set *)leafref_target->child, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02002811
2812 return 0;
2813}
2814
Michal Vasko8548e082016-07-22 12:00:18 +02002815/* not needed currently */
2816#if 0
2817
Michal Vasko5b3492c2016-07-20 09:37:40 +02002818static const char *
2819lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
2820{
2821 struct lys_module *prev_mod;
2822 uint32_t str_len, mod_len, buf_idx;
2823
Radek Krejcibf2abff2016-08-23 15:51:52 +02002824 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02002825 LOGINT;
2826 return NULL;
2827 }
2828
2829 buf_idx = buf_len - 1;
2830 buf[buf_idx] = '\0';
2831
2832 while (node) {
2833 if (lys_parent(node)) {
2834 prev_mod = lys_node_module(lys_parent(node));
2835 } else {
2836 prev_mod = NULL;
2837 }
2838
Radek Krejcibf2abff2016-08-23 15:51:52 +02002839 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02002840 str_len = strlen(node->name);
2841
2842 if (prev_mod != node->module) {
2843 mod_len = strlen(node->module->name);
2844 } else {
2845 mod_len = 0;
2846 }
2847
2848 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
2849 LOGINT;
2850 return NULL;
2851 }
2852
2853 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
2854
2855 buf[buf_idx] = '/';
2856 if (mod_len) {
2857 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
2858 buf[buf_idx + 1 + mod_len] = ':';
2859 }
2860 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
2861 }
2862
2863 node = lys_parent(node);
2864 }
2865
2866 return buf + buf_idx;
2867}
2868
Michal Vasko8548e082016-07-22 12:00:18 +02002869#endif
2870
2871API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02002872lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02002873{
2874 struct lyxp_set set;
2875 struct ly_set *ret_set;
2876 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02002877 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02002878
2879 if (!node || !expr) {
2880 ly_errno = LY_EINVAL;
2881 return NULL;
2882 }
2883
2884 memset(&set, 0, sizeof set);
2885
Michal Vaskodb1da032016-09-08 10:07:38 +02002886 opts = LYXP_SNODE;
2887 if (options & LYS_FIND_OUTPUT) {
2888 opts |= LYXP_SNODE_OUTPUT;
2889 }
2890
Michal Vasko46a4bf92016-09-08 08:23:49 +02002891 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02002892 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02002893 free(set.val.snodes);
2894 return NULL;
2895 }
2896
2897 ret_set = ly_set_new();
2898
2899 for (i = 0; i < set.used; ++i) {
2900 if (!set.val.snodes[i].in_ctx) {
2901 continue;
2902 }
2903 assert(set.val.snodes[i].in_ctx == 1);
2904
2905 switch (set.val.snodes[i].type) {
2906 case LYXP_NODE_ELEM:
2907 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
2908 ly_set_free(ret_set);
2909 free(set.val.snodes);
2910 return NULL;
2911 }
2912 break;
2913 default:
2914 /* ignore roots, text and attr should not ever appear */
2915 break;
2916 }
2917 }
2918
2919 free(set.val.snodes);
2920 return ret_set;
2921}
2922
2923API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02002924lys_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 +02002925{
Michal Vasko508a50d2016-09-07 14:50:33 +02002926 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02002927 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02002928 uint32_t i;
2929
Michal Vaskob94a5e42016-09-08 14:01:56 +02002930 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02002931 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02002932 }
2933
Michal Vaskob94a5e42016-09-08 14:01:56 +02002934 /* adjust the root */
2935 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
2936 do {
2937 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
2938 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
2939 }
2940
Michal Vasko508a50d2016-09-07 14:50:33 +02002941 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02002942
2943 if (options & LYXP_MUST) {
2944 options &= ~LYXP_MUST;
2945 options |= LYXP_SNODE_MUST;
2946 } else if (options & LYXP_WHEN) {
2947 options &= ~LYXP_WHEN;
2948 options |= LYXP_SNODE_WHEN;
2949 } else {
2950 options |= LYXP_SNODE;
2951 }
2952
Michal Vasko508a50d2016-09-07 14:50:33 +02002953 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
2954 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02002955 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02002956 }
2957
Michal Vasko8548e082016-07-22 12:00:18 +02002958 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02002959
Michal Vasko508a50d2016-09-07 14:50:33 +02002960 for (i = 0; i < set.used; ++i) {
2961 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02002962 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02002963 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02002964 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02002965 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02002966 return NULL;
2967 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02002968 break;
2969 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02002970 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02002971 break;
2972 }
2973 }
2974
Michal Vasko508a50d2016-09-07 14:50:33 +02002975 free(set.val.snodes);
2976 return ret_set;
2977}
2978
2979API struct ly_set *
2980lys_node_xpath_atomize(const struct lys_node *node, int options)
2981{
2982 const struct lys_node *next, *elem, *parent, *tmp;
2983 struct lyxp_set set;
2984 struct ly_set *ret_set;
2985 uint16_t i;
2986
2987 if (!node) {
2988 return NULL;
2989 }
2990
2991 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
2992 if (!parent) {
2993 /* not in input, output, or notification */
2994 return NULL;
2995 }
2996
2997 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02002998 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02002999 return NULL;
3000 }
3001
3002 LY_TREE_DFS_BEGIN(node, next, elem) {
3003 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) {
3004 /* elem has no dependencies from other subtrees and local nodes get discarded */
3005 goto next_iter;
3006 }
3007
3008 if (lyxp_node_atomize(elem, &set)) {
3009 ly_set_free(ret_set);
3010 free(set.val.snodes);
3011 return NULL;
3012 }
3013
3014 for (i = 0; i < set.used; ++i) {
3015 switch (set.val.snodes[i].type) {
3016 case LYXP_NODE_ELEM:
3017 if (options & LYXP_NO_LOCAL) {
3018 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3019 if (tmp) {
3020 /* in local subtree, discard */
3021 break;
3022 }
3023 }
3024 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3025 ly_set_free(ret_set);
3026 free(set.val.snodes);
3027 return NULL;
3028 }
3029 break;
3030 default:
3031 /* ignore roots, text and attr should not ever appear */
3032 break;
3033 }
3034 }
3035
3036 free(set.val.snodes);
3037 if (!(options & LYXP_RECURSIVE)) {
3038 break;
3039 }
3040next_iter:
3041 LY_TREE_DFS_END(node, next, elem);
3042 }
3043
Michal Vasko8548e082016-07-22 12:00:18 +02003044 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003045}
3046
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003047static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003048lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003049{
3050 int ret;
3051 char *parent_path;
3052 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003053
Pavol Vican64d0b762016-08-25 10:44:59 +02003054 if (!dev->deviate) {
3055 return ;
3056 }
3057
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003058 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3059 if (dev->orig_node) {
3060 /* removing not-supported deviation ... */
3061 if (strrchr(dev->target_name, '/') != dev->target_name) {
3062 /* ... from a parent */
3063 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3064
3065 target = NULL;
3066 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target);
3067 free(parent_path);
3068 if (ret || !target) {
3069 LOGINT;
3070 return;
3071 }
3072
3073 lys_node_addchild(target, NULL, dev->orig_node);
3074 } else {
3075 /* ... from top-level data */
3076 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3077 }
3078
3079 dev->orig_node = NULL;
3080 } else {
3081 /* adding not-supported deviation */
3082 target = NULL;
3083 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3084 if (ret || !target) {
3085 LOGINT;
3086 return;
3087 }
3088
3089 lys_node_unlink(target);
3090 dev->orig_node = target;
3091 }
3092 } else {
3093 target = NULL;
3094 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3095 if (ret || !target) {
3096 LOGINT;
3097 return;
3098 }
3099
3100 lys_node_switch(target, dev->orig_node);
3101 dev->orig_node = target;
3102 }
3103}
3104
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003105/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3106void
3107lys_switch_deviations(struct lys_module *module)
3108{
Michal Vasko89563fc2016-07-28 16:19:35 +02003109 uint32_t i = 0, j;
3110 const struct lys_module *mod;
3111 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003112
Michal Vasko89563fc2016-07-28 16:19:35 +02003113 if (module->deviated) {
3114 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3115 if (mod == module) {
3116 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003117 }
3118
Michal Vasko89563fc2016-07-28 16:19:35 +02003119 for (j = 0; j < mod->deviation_size; ++j) {
3120 ptr = strstr(mod->deviation[j].target_name, module->name);
3121 if (ptr && ptr[strlen(module->name)] == ':') {
3122 lys_switch_deviation(&mod->deviation[j], module);
3123 }
3124 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003125 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003126
Michal Vasko89563fc2016-07-28 16:19:35 +02003127 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003128 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003129 } else {
3130 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003131 }
3132 }
3133}
3134
3135/* not needed currently, but tested and working */
3136#if 0
3137
3138void
3139lys_sub_module_apply_devs_augs(struct lys_module *module)
3140{
3141 int i;
3142 struct lys_node_augment *aug;
3143 struct lys_node *last;
3144
3145 /* re-apply deviations */
3146 for (i = 0; i < module->deviation_size; ++i) {
3147 lys_switch_deviation(&module->deviation[i], module);
3148 assert(module->deviation[i].orig_node);
Michal Vasko89563fc2016-07-28 16:19:35 +02003149 lys_node_module(module->deviation[i].orig_node)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003150 }
3151
3152 /* re-apply augments */
3153 for (i = 0; i < module->augment_size; ++i) {
3154 aug = &module->augment[i];
3155 assert(aug->target);
3156
3157 /* reconnect augmenting data into the target - add them to the target child list */
3158 if (aug->target->child) {
3159 last = aug->target->child->prev;
3160 last->next = aug->child;
3161 aug->target->child->prev = aug->child->prev;
3162 aug->child->prev = last;
3163 } else {
3164 aug->target->child = aug->child;
3165 }
3166 }
3167}
3168
3169#endif
3170
3171void
3172lys_sub_module_remove_devs_augs(struct lys_module *module)
3173{
Michal Vasko89563fc2016-07-28 16:19:35 +02003174 uint32_t i = 0, j;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003175 struct lys_node *last, *elem;
Michal Vasko89563fc2016-07-28 16:19:35 +02003176 const struct lys_module *mod;
3177 struct lys_module *target_mod;
3178 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003179
3180 /* remove applied deviations */
3181 for (i = 0; i < module->deviation_size; ++i) {
Pavol Vican64d0b762016-08-25 10:44:59 +02003182 if (module->deviation[i].orig_node) {
3183 target_mod = lys_node_module(module->deviation[i].orig_node);
3184 } else {
3185 target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1,
3186 strcspn(module->deviation[i].target_name, ":") - 1);
3187 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003188 lys_switch_deviation(&module->deviation[i], module);
Michal Vasko89563fc2016-07-28 16:19:35 +02003189 assert(target_mod->deviated == 1);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003190
Michal Vasko89563fc2016-07-28 16:19:35 +02003191 /* clear the deviation flag if possible */
3192 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3193 if ((mod == module) || (mod == target_mod)) {
3194 continue;
3195 }
3196
3197 for (j = 0; j < mod->deviation_size; ++j) {
3198 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
3199 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
3200 /* some other module deviation targets the inspected module, flag remains */
3201 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003202 }
3203 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003204
3205 if (j < mod->deviation_size) {
3206 break;
3207 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003208 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003209
3210 if (!mod) {
3211 target_mod->deviated = 0;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003212 }
3213 }
3214
3215 /* remove applied augments */
3216 for (i = 0; i < module->augment_size; ++i) {
Radek Krejcidbc15262016-06-16 14:58:29 +02003217 if (!module->augment[i].target) {
3218 /* skip not resolved augments */
3219 continue;
3220 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003221
3222 elem = module->augment[i].child;
3223 if (elem) {
3224 LY_TREE_FOR(elem, last) {
3225 if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) {
3226 break;
3227 }
3228 }
3229 /* elem is first augment child, last is the last child */
3230
3231 /* parent child ptr */
3232 if (module->augment[i].target->child == elem) {
3233 module->augment[i].target->child = last->next;
3234 }
3235
3236 /* parent child next ptr */
3237 if (elem->prev->next) {
3238 elem->prev->next = last->next;
3239 }
3240
3241 /* parent child prev ptr */
3242 if (last->next) {
3243 last->next->prev = elem->prev;
3244 } else if (module->augment[i].target->child) {
3245 module->augment[i].target->child->prev = elem->prev;
3246 }
3247
3248 /* update augment children themselves */
3249 elem->prev = last;
3250 last->next = NULL;
3251 }
Michal Vaskob8f71322016-05-03 11:39:56 +02003252
3253 /* needs to be NULL for lys_augment_free() to free the children */
3254 module->augment[i].target = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003255 }
3256}
3257
Radek Krejci27fe55e2016-09-13 17:13:35 +02003258static int
3259lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
3260{
3261 struct lys_node *root, *next, *node;
3262 uint8_t i;
3263
3264 for (i = 0; i < module->augment_size; i++) {
3265 /* apply augment */
3266 if (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1) {
3267 return -1;
3268 }
3269 }
3270 LY_TREE_FOR(module->data, root) {
3271 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
3272 LY_TREE_DFS_BEGIN(root, next, node) {
3273 if (node->nodetype == LYS_GROUPING) {
3274 goto nextsibling;
3275 }
3276 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3277 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
3278 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
3279 UNRES_TYPE_LEAFREF, node) == -1) {
3280 return EXIT_FAILURE;
3281 }
3282 }
3283 }
3284
3285 /* modified LY_TREE_DFS_END */
3286 next = node->child;
3287 /* child exception for leafs, leaflists and anyxml without children */
3288 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
3289 next = NULL;
3290 }
3291 if (!next) {
3292nextsibling:
3293 /* no children */
3294 if (node == root) {
3295 /* we are done, root has no children */
3296 break;
3297 }
3298 /* try siblings */
3299 next = node->next;
3300 }
3301 while (!next) {
3302 /* parent is already processed, go to its sibling */
3303 node = lys_parent(node);
3304 /* no siblings, go back through parents */
3305 if (lys_parent(node) == lys_parent(root)) {
3306 /* we are done, no next element to process */
3307 break;
3308 }
3309 next = node->next;
3310 }
3311 }
3312 }
3313
3314 return EXIT_SUCCESS;
3315}
3316
3317API int
3318lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02003319{
3320 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003321 struct unres_schema *unres;
3322 int i, j;
Michal Vasko26055752016-05-03 11:36:31 +02003323
Radek Krejci27fe55e2016-09-13 17:13:35 +02003324 if (!module) {
3325 ly_errno = LY_EINVAL;
3326 return EXIT_FAILURE;
3327 }
3328
3329 module = lys_main_module(module);
Michal Vasko26055752016-05-03 11:36:31 +02003330 if (module->implemented) {
3331 return EXIT_SUCCESS;
3332 }
3333
3334 ctx = module->ctx;
3335
3336 for (i = 0; i < ctx->models.used; ++i) {
3337 if (module == ctx->models.list[i]) {
3338 continue;
3339 }
3340
3341 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
3342 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
3343 return EXIT_FAILURE;
3344 }
3345 }
3346
Radek Krejci27fe55e2016-09-13 17:13:35 +02003347 unres = calloc(1, sizeof *unres);
3348 if (!unres) {
3349 LOGMEM;
3350 return EXIT_FAILURE;
3351 }
3352 /* recursively make the module implemented */
3353 ((struct lys_module *)module)->implemented = 1;
3354 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
3355 goto error;
3356 }
3357 /* process augments in submodules */
3358 for (i = 0; i < module->inc_size; ++i) {
3359 if (!module->inc[i].submodule) {
3360 continue;
3361 }
3362 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
3363 /* apply augment */
3364 if (unres_schema_add_node((struct lys_module *)module->inc[i].submodule, unres,
3365 &module->inc[i].submodule->augment[i], UNRES_AUGMENT, NULL) == -1) {
3366 goto error;
3367 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003368 }
3369 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003370 /* resolve rest of unres items */
3371 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
3372 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003373 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003374 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02003375
3376 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003377
3378error:
3379 ((struct lys_module *)module)->implemented = 0;
3380 unres_schema_free((struct lys_module *)module, &unres);
3381 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003382}
3383
3384void
3385lys_submodule_module_data_free(struct lys_submodule *submodule)
3386{
3387 struct lys_node *next, *elem;
3388
3389 /* remove parsed data */
3390 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3391 if (elem->module == (struct lys_module *)submodule) {
3392 lys_node_free(elem, NULL, 0);
3393 }
3394 }
3395}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02003396
3397int
3398lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
3399{
3400 uint8_t i;
3401
3402 for (i = 0; i < list->keys_size; i++) {
3403 if (list->keys[i] == leaf) {
3404 return i + 1;
3405 }
3406 }
3407
3408 return 0;
3409}