blob: 6dd11e800eccdfc599c0b9065f7ea65c8966d9a5 [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 Vaskob15cae22016-09-15 09:40:56 +0200633 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200634 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 &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200651 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
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 Vaskoe022a562016-09-27 14:24:15 +0200735 /* check config value (but ignore them in groupings and augments) */
736 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent);
737 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200738 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
739 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
740 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
741 LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children.");
742 return EXIT_FAILURE;
743 }
744 }
745
Radek Krejci41771502016-04-14 17:52:32 +0200746 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200747 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200748 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200749 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200750 /* store it only into container or list - the only data inner nodes */
751 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
752 if (iter->flags & LYS_INCL_STATUS) {
753 /* done, someone else set it already from here */
754 break;
755 }
756 /* set flag about including status data */
757 iter->flags |= LYS_INCL_STATUS;
758 }
759 }
760 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200761 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200762}
763
Radek Krejcia1df1682016-04-11 14:56:59 +0200764static const struct lys_module *
765lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200766{
Radek Krejcia1df1682016-04-11 14:56:59 +0200767 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200768 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200769 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200770
Radek Krejcif347abc2016-06-22 10:18:47 +0200771 ly_errno = LY_SUCCESS;
772
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200773 if (!ctx || !data) {
774 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
775 return NULL;
776 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200777
Radek Krejcia1df1682016-04-11 14:56:59 +0200778 if (!internal && format == LYS_IN_YANG) {
779 /* enlarge data by 2 bytes for flex */
780 len = strlen(data);
781 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
782 if (!enlarged_data) {
783 LOGMEM;
784 return NULL;
785 }
786 memcpy(enlarged_data, data, len);
787 enlarged_data[len] = enlarged_data[len + 1] = '\0';
788 data = enlarged_data;
789 }
790
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200791 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200792 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +0100793 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200794 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200795 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100796 mod = yang_read_module(ctx, data, 0, NULL, 1);
797 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200798 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200799 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200800 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200801 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200802
Radek Krejcia1df1682016-04-11 14:56:59 +0200803 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200804 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200805}
806
Radek Krejcia1df1682016-04-11 14:56:59 +0200807API const struct lys_module *
808lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
809{
810 return lys_parse_mem_(ctx, data, format, 0);
811}
812
Michal Vasko5a721fd2016-02-16 12:16:48 +0100813struct lys_submodule *
814lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200815{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100816 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200817
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200818 assert(module);
819 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +0200820
Radek Krejcic071c542016-01-27 14:57:51 +0100821 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +0200822 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200823
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200824 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200825 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +0100826 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200827 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200828 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100829 submod = yang_read_submodule(module, data, 0, unres);
830 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200831 default:
Radek Krejci90a550a2016-04-13 16:00:58 +0200832 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200833 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200834 }
Radek Krejciefaeba32015-05-27 14:30:57 +0200835
Michal Vasko5a721fd2016-02-16 12:16:48 +0100836 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +0200837}
838
Michal Vasko1e62a092015-12-01 12:27:20 +0100839API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +0100840lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
841{
842 int fd;
843 const struct lys_module *ret;
844
845 if (!ctx || !path) {
846 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
847 return NULL;
848 }
849
850 fd = open(path, O_RDONLY);
851 if (fd == -1) {
852 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
853 return NULL;
854 }
855
856 ret = lys_parse_fd(ctx, fd, format);
857 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100858
Radek Krejcia77904e2016-02-25 16:23:45 +0100859 if (ret && !ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100860 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100861 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100862 }
863
Michal Vasko662610a2015-12-07 11:25:45 +0100864 return ret;
865}
866
867API const struct lys_module *
868lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +0200869{
Michal Vasko1e62a092015-12-01 12:27:20 +0100870 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +0200871 struct stat sb;
872 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +0100873 char buf[PATH_MAX];
874 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +0200875
876 if (!ctx || fd < 0) {
877 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
878 return NULL;
879 }
880
Radek Krejci10a833c2015-12-16 15:28:37 +0100881 if (fstat(fd, &sb) == -1) {
882 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
883 return NULL;
884 }
Radek Krejcib051f722016-02-25 15:12:21 +0100885 if (!S_ISREG(sb.st_mode)) {
886 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
887 return NULL;
888 }
889
Michal Vasko164d9012016-04-01 10:16:59 +0200890 if (!sb.st_size) {
891 LOGERR(LY_EINVAL, "File empty.");
892 return NULL;
893 }
894
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100895 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100896 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +0100897 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +0100898 return NULL;
899 }
Radek Krejcia1df1682016-04-11 14:56:59 +0200900 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100901 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200902
Radek Krejcia77904e2016-02-25 16:23:45 +0100903 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +0100904 /* get URI if there is /proc */
905 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +0100906 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
907 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
908 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
909 }
910 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +0100911 }
Radek Krejcib051f722016-02-25 15:12:21 +0100912 }
913
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200914 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200915}
916
Michal Vasko5a721fd2016-02-16 12:16:48 +0100917struct lys_submodule *
918lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200919{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100920 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200921 struct stat sb;
922 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +0200923
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200924 assert(module);
925 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +0200926
Radek Krejci10a833c2015-12-16 15:28:37 +0100927 if (fstat(fd, &sb) == -1) {
928 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +0100929 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +0100930 }
Michal Vasko164d9012016-04-01 10:16:59 +0200931
932 if (!sb.st_size) {
933 LOGERR(LY_EINVAL, "File empty.");
934 return NULL;
935 }
936
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100937 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100938 if (addr == MAP_FAILED) {
939 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +0100940 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +0100941 }
Michal Vasko5a721fd2016-02-16 12:16:48 +0100942 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100943 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +0200944
Michal Vasko5a721fd2016-02-16 12:16:48 +0100945 return submodule;
946
Radek Krejciefaeba32015-05-27 14:30:57 +0200947}
948
Radek Krejci1d82ef62015-08-07 14:44:40 +0200949static struct lys_restr *
950lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200951{
Radek Krejci1574a8d2015-08-03 14:16:52 +0200952 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200953 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200954
Radek Krejci3733a802015-06-19 13:43:21 +0200955 if (!size) {
956 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200957 }
Radek Krejci3733a802015-06-19 13:43:21 +0200958
959 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +0100960 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +0100961 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +0100962 return NULL;
963 }
Radek Krejci3733a802015-06-19 13:43:21 +0200964 for (i = 0; i < size; i++) {
965 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
966 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
967 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
968 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
969 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
970 }
971
972 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +0200973}
974
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200976lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +0200977{
978 assert(ctx);
979 if (!restr) {
980 return;
981 }
982
983 lydict_remove(ctx, restr->expr);
984 lydict_remove(ctx, restr->dsc);
985 lydict_remove(ctx, restr->ref);
986 lydict_remove(ctx, restr->eapptag);
987 lydict_remove(ctx, restr->emsg);
988}
989
Radek Krejcif1ee2e22016-08-02 16:36:48 +0200990static void
991lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
992{
993 uint8_t i;
994
995 for (i = 0; i < iffeature_size; ++i) {
996 free(iffeature[i].expr);
997 free(iffeature[i].features);
998 }
999 free(iffeature);
1000}
1001
Michal Vaskob84f88a2015-09-24 13:16:10 +02001002static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001003type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001004 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001005{
1006 int i;
1007
1008 switch (base) {
1009 case LY_TYPE_BINARY:
1010 if (old->info.binary.length) {
1011 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1012 }
1013 break;
1014
1015 case LY_TYPE_BITS:
1016 new->info.bits.count = old->info.bits.count;
1017 if (new->info.bits.count) {
1018 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1019 if (!new->info.bits.bit) {
1020 LOGMEM;
1021 return -1;
1022 }
1023 for (i = 0; i < new->info.bits.count; i++) {
1024 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1025 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1026 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1027 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1028 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1029 }
1030 }
1031 break;
1032
1033 case LY_TYPE_DEC64:
1034 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001035 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001036 if (old->info.dec64.range) {
1037 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1038 }
1039 break;
1040
1041 case LY_TYPE_ENUM:
1042 new->info.enums.count = old->info.enums.count;
1043 if (new->info.enums.count) {
1044 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1045 if (!new->info.enums.enm) {
1046 LOGMEM;
1047 return -1;
1048 }
1049 for (i = 0; i < new->info.enums.count; i++) {
1050 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1051 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1052 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1053 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1054 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1055 }
1056 }
1057 break;
1058
1059 case LY_TYPE_IDENT:
Michal Vasko878e38d2016-09-05 12:17:53 +02001060 if (old->info.ident.count) {
1061 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1062 if (!new->info.ident.ref) {
1063 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001064 return -1;
1065 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001066 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1067 } else {
1068 /* there can be several unresolved base identities, duplicate them all */
1069 i = -1;
1070 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1071 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1072 return -1;
1073 }
1074 --i;
1075 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001076 }
1077 break;
1078
1079 case LY_TYPE_INST:
1080 new->info.inst.req = old->info.inst.req;
1081 break;
1082
1083 case LY_TYPE_INT8:
1084 case LY_TYPE_INT16:
1085 case LY_TYPE_INT32:
1086 case LY_TYPE_INT64:
1087 case LY_TYPE_UINT8:
1088 case LY_TYPE_UINT16:
1089 case LY_TYPE_UINT32:
1090 case LY_TYPE_UINT64:
1091 if (old->info.num.range) {
1092 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1093 }
1094 break;
1095
1096 case LY_TYPE_LEAFREF:
1097 if (old->info.lref.path) {
1098 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001099 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001100 return -1;
1101 }
1102 }
1103 break;
1104
1105 case LY_TYPE_STRING:
1106 if (old->info.str.length) {
1107 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1108 }
1109 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1110 new->info.str.pat_count = old->info.str.pat_count;
1111 break;
1112
1113 case LY_TYPE_UNION:
1114 new->info.uni.count = old->info.uni.count;
1115 if (new->info.uni.count) {
1116 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1117 if (!new->info.uni.types) {
1118 LOGMEM;
1119 return -1;
1120 }
1121 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001122 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 +02001123 return -1;
1124 }
1125 }
1126 }
1127 break;
1128
1129 default:
1130 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1131 break;
1132 }
1133 return EXIT_SUCCESS;
1134}
1135
1136struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001137lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1138 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001139{
1140 struct yang_type *new;
1141
1142 new = calloc(1, sizeof *new);
1143 if (!new) {
1144 LOGMEM;
1145 return NULL;
1146 }
1147 new->flags = old->flags;
1148 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001149 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001150 new->type = type;
1151 if (!new->name) {
1152 LOGMEM;
1153 goto error;
1154 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001155 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001156 new->type->base = new->base;
1157 lys_type_free(module->ctx, new->type);
1158 memset(&new->type->info, 0, sizeof new->type->info);
1159 goto error;
1160 }
1161 return new;
1162
1163 error:
1164 free(new);
1165 return NULL;
1166}
1167
1168static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001169lys_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 +02001170 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001171{
1172 int i;
1173
Michal Vasko1dca6882015-10-22 14:29:42 +02001174 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001175 new->base = old->base;
1176 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001177 new->parent = (struct lys_tpdf *)parent;
Radek Krejci3733a802015-06-19 13:43:21 +02001178
Michal Vasko878e38d2016-09-05 12:17:53 +02001179 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001180 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001181 /* HACK (serious one) for unres */
1182 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001183 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001184 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 +02001185 } else {
1186 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1187 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001188 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001189 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001190 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001191 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001192 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001193 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001194 }
1195
Radek Krejci3a5501d2016-07-18 22:03:34 +02001196 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001197}
1198
1199void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001200lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001201{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001202 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001203
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001204 assert(ctx);
1205 if (!type) {
1206 return;
1207 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001208
Michal Vasko1dca6882015-10-22 14:29:42 +02001209 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001210
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001211 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001212 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001213 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001214 free(type->info.binary.length);
1215 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001216 case LY_TYPE_BITS:
1217 for (i = 0; i < type->info.bits.count; i++) {
1218 lydict_remove(ctx, type->info.bits.bit[i].name);
1219 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1220 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001221 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001222 }
1223 free(type->info.bits.bit);
1224 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001225
1226 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001227 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001228 free(type->info.dec64.range);
1229 break;
1230
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001231 case LY_TYPE_ENUM:
1232 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001233 lydict_remove(ctx, type->info.enums.enm[i].name);
1234 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1235 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001236 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001237 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001238 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001239 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001240
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001241 case LY_TYPE_INT8:
1242 case LY_TYPE_INT16:
1243 case LY_TYPE_INT32:
1244 case LY_TYPE_INT64:
1245 case LY_TYPE_UINT8:
1246 case LY_TYPE_UINT16:
1247 case LY_TYPE_UINT32:
1248 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001249 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001250 free(type->info.num.range);
1251 break;
1252
Radek Krejcidc4c1412015-06-19 15:39:54 +02001253 case LY_TYPE_LEAFREF:
1254 lydict_remove(ctx, type->info.lref.path);
1255 break;
1256
Radek Krejci3733a802015-06-19 13:43:21 +02001257 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001258 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001259 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001260 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001261 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001262 }
1263 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001264 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001265
Radek Krejcie4c366b2015-07-02 10:11:31 +02001266 case LY_TYPE_UNION:
1267 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001268 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001269 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001270 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001271 break;
1272
Michal Vaskod3282192016-09-05 11:27:57 +02001273 case LY_TYPE_IDENT:
1274 free(type->info.ident.ref);
1275 break;
1276
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001277 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001278 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001279 break;
1280 }
Radek Krejci5a065542015-05-22 15:02:07 +02001281}
1282
Radek Krejci1d82ef62015-08-07 14:44:40 +02001283static void
1284lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001285{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001286 assert(ctx);
1287 if (!tpdf) {
1288 return;
1289 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001290
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001291 lydict_remove(ctx, tpdf->name);
1292 lydict_remove(ctx, tpdf->dsc);
1293 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001294
Radek Krejci1d82ef62015-08-07 14:44:40 +02001295 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001296
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001297 lydict_remove(ctx, tpdf->units);
1298 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001299}
1300
Michal Vaskob84f88a2015-09-24 13:16:10 +02001301static struct lys_tpdf *
1302lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1303{
1304 struct lys_tpdf *result;
1305 int i, j;
1306
1307 if (!size) {
1308 return NULL;
1309 }
1310
1311 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001312 if (!result) {
1313 LOGMEM;
1314 return NULL;
1315 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001316 for (i = 0; i < size; i++) {
1317 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1318 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1319 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1320 result[i].flags = old[i].flags;
1321 result[i].module = old[i].module;
1322
Radek Krejci3a5501d2016-07-18 22:03:34 +02001323 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001324 for (j = 0; j <= i; ++j) {
1325 lys_tpdf_free(mod->ctx, &result[j]);
1326 }
1327 free(result);
1328 return NULL;
1329 }
1330
1331 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1332 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1333 }
1334
1335 return result;
1336}
1337
Radek Krejci1d82ef62015-08-07 14:44:40 +02001338static struct lys_when *
1339lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001340{
Radek Krejci76512572015-08-04 09:47:08 +02001341 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001342
1343 if (!old) {
1344 return NULL;
1345 }
1346
1347 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001348 if (!new) {
1349 LOGMEM;
1350 return NULL;
1351 }
Radek Krejci00768f42015-06-18 17:04:04 +02001352 new->cond = lydict_insert(ctx, old->cond, 0);
1353 new->dsc = lydict_insert(ctx, old->dsc, 0);
1354 new->ref = lydict_insert(ctx, old->ref, 0);
1355
1356 return new;
1357}
1358
Michal Vasko0308dd62015-10-07 09:14:40 +02001359void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001360lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001361{
1362 if (!w) {
1363 return;
1364 }
1365
1366 lydict_remove(ctx, w->cond);
1367 lydict_remove(ctx, w->dsc);
1368 lydict_remove(ctx, w->ref);
1369
1370 free(w);
1371}
1372
Radek Krejcib7f5e412015-08-13 10:15:51 +02001373static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001374lys_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 +02001375{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001376 struct lys_node *next, *sub;
1377
Radek Krejcic071c542016-01-27 14:57:51 +01001378 /* children from a resolved augment are freed under the target node */
Michal Vaskod2fbade2016-05-02 17:14:00 +02001379 if (!aug->target) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001380 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001381 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001382 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001383 }
1384
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001385 lydict_remove(ctx, aug->target_name);
1386 lydict_remove(ctx, aug->dsc);
1387 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001388
Radek Krejci9ff0a922016-07-14 13:08:05 +02001389 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001390
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001391 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001392}
1393
Radek Krejci76512572015-08-04 09:47:08 +02001394static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001395lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001396{
Radek Krejci76512572015-08-04 09:47:08 +02001397 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001398 struct lys_node *old_child, *new_child;
1399 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001400
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001401 if (!size) {
1402 return NULL;
1403 }
Radek Krejci106efc02015-06-10 14:36:27 +02001404
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001405 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001406 if (!new) {
1407 LOGMEM;
1408 return NULL;
1409 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001410 for (i = 0; i < size; i++) {
1411 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1412 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1413 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1414 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001415 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001416 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001417
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001418 /* this must succeed, it was already resolved once */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001419 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL,
1420 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001421 LOGINT;
1422 free(new);
1423 return NULL;
1424 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001425 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001426
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001427 /* Correct the augment nodes.
1428 * This function can only be called from lys_node_dup() with uses
1429 * being the node duplicated, so we must have a case of grouping
1430 * with a uses with augments. The augmented nodes have already been
1431 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001432 * (it was set to their actual data parent, not an augment), and
1433 * the new augment does not have child pointer to its augment nodes,
1434 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001435 */
1436 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001437 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001438 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001439 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001440 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001441 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001442 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001443 LY_TREE_FOR(old[i].child, old_child) {
1444 /* all augment nodes were connected as siblings, there can be no more after this */
1445 if (old_child->parent != (struct lys_node *)&old[i]) {
1446 break;
1447 }
1448
Radek Krejci749190d2016-02-18 16:26:25 +01001449 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001450
1451 new_child->parent = (struct lys_node *)&new[i];
1452 new_child = new_child->next;
1453 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001454 }
Radek Krejci106efc02015-06-10 14:36:27 +02001455
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001456 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001457}
1458
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001459static const char **
1460lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1461{
1462 int i;
1463 const char **result;
1464
1465 if (!size) {
1466 return NULL;
1467 }
1468
1469 result = calloc(size, sizeof *result);
1470 if (!result) {
1471 LOGMEM;
1472 return NULL;
1473 }
1474
1475 for (i = 0; i < size; i++) {
1476 result[i] = lydict_insert(ctx, old[i], 0);
1477 }
1478 return result;
1479}
1480
Radek Krejci76512572015-08-04 09:47:08 +02001481static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001482lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001483{
Radek Krejci76512572015-08-04 09:47:08 +02001484 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001485 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001486
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001487 if (!size) {
1488 return NULL;
1489 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001490
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001491 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001492 if (!result) {
1493 LOGMEM;
1494 return NULL;
1495 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001496 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001497 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001498 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1499 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001500 result[i].flags = old[i].flags;
1501 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001502
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001503 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001504 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001505
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001506 result[i].dflt_size = old[i].dflt_size;
1507 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
1508
1509 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001510 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001511 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001512 result[i].mod.list = old[i].mod.list;
1513 }
1514 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001515
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001516 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001517}
1518
Radek Krejci1d82ef62015-08-07 14:44:40 +02001519static void
1520lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001521{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001522 assert(ctx);
1523 if (!ident) {
1524 return;
1525 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001526
Radek Krejci018f1f52016-08-03 16:01:20 +02001527 free(ident->base);
Radek Krejci1b61d0e2016-04-15 13:55:44 +02001528 free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001529 lydict_remove(ctx, ident->name);
1530 lydict_remove(ctx, ident->dsc);
1531 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001532 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejci6793db02015-05-22 17:49:54 +02001533
1534}
1535
Radek Krejci1d82ef62015-08-07 14:44:40 +02001536static void
1537lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001538{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001539 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001540
Radek Krejcid12f57b2015-08-06 10:43:39 +02001541 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001542 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001543 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001544 }
1545 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001546}
1547
Radek Krejci1d82ef62015-08-07 14:44:40 +02001548static void
Michal Vasko44fb6382016-06-29 11:12:27 +02001549lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02001550{
1551 int i;
1552
1553 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1554 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001555 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001556 }
1557 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02001558
1559 for (i = 0; i < io->must_size; i++) {
1560 lys_restr_free(ctx, &io->must[i]);
1561 }
1562 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001563}
1564
Radek Krejci1d82ef62015-08-07 14:44:40 +02001565static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02001566lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
1567{
1568 int i;
1569
1570 for (i = 0; i < notif->must_size; i++) {
1571 lys_restr_free(ctx, &notif->must[i]);
1572 }
1573 free(notif->must);
1574
1575 for (i = 0; i < notif->tpdf_size; i++) {
1576 lys_tpdf_free(ctx, &notif->tpdf[i]);
1577 }
1578 free(notif->tpdf);
1579}
1580static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02001581lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001582{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001583 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001584
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001585 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001586 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001587 }
1588 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001589
Radek Krejci1d82ef62015-08-07 14:44:40 +02001590 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001591}
1592
Radek Krejci1d82ef62015-08-07 14:44:40 +02001593static void
1594lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001595{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001596 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001597
Radek Krejci46c4cd72016-01-21 15:13:52 +01001598 if (leaf->child) {
1599 /* leafref backlinks */
1600 ly_set_free((struct ly_set *)leaf->child);
1601 }
1602
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001603 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001604 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001605 }
1606 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001607
Radek Krejci1d82ef62015-08-07 14:44:40 +02001608 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001609
Radek Krejci1d82ef62015-08-07 14:44:40 +02001610 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001611 lydict_remove(ctx, leaf->units);
1612 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001613}
1614
Radek Krejci1d82ef62015-08-07 14:44:40 +02001615static void
1616lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001617{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001618 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001619
Radek Krejci46c4cd72016-01-21 15:13:52 +01001620 if (llist->child) {
1621 /* leafref backlinks */
1622 ly_set_free((struct ly_set *)llist->child);
1623 }
1624
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001625 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001626 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001627 }
1628 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001629
Pavol Vican38321d02016-08-16 14:56:02 +02001630 for (i = 0; i < llist->dflt_size; i++) {
1631 lydict_remove(ctx, llist->dflt[i]);
1632 }
1633 free(llist->dflt);
1634
Radek Krejci1d82ef62015-08-07 14:44:40 +02001635 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001636
Radek Krejci1d82ef62015-08-07 14:44:40 +02001637 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001638 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001639}
1640
Radek Krejci1d82ef62015-08-07 14:44:40 +02001641static void
1642lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001643{
Radek Krejci581ce772015-11-10 17:22:40 +01001644 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001645
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001646 /* handle only specific parts for LY_NODE_LIST */
1647 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001648 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001649 }
1650 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001651
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001652 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001653 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001654 }
1655 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001656
Radek Krejci1d82ef62015-08-07 14:44:40 +02001657 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001658
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001659 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02001660 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001661 lydict_remove(ctx, list->unique[i].expr[j]);
1662 }
1663 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001664 }
1665 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001666
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001667 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001668}
1669
Radek Krejci1d82ef62015-08-07 14:44:40 +02001670static void
1671lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001672{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001673 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001674
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001675 /* handle only specific parts for LY_NODE_CONTAINER */
1676 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001677
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001678 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001679 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001680 }
1681 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001682
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001683 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001684 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001685 }
1686 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001687
Radek Krejci1d82ef62015-08-07 14:44:40 +02001688 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001689}
1690
Radek Krejci1d82ef62015-08-07 14:44:40 +02001691static void
1692lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001693{
1694 lydict_remove(ctx, f->name);
1695 lydict_remove(ctx, f->dsc);
1696 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001697 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001698}
1699
Radek Krejci1d82ef62015-08-07 14:44:40 +02001700static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001701lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001702{
Radek Krejci581ce772015-11-10 17:22:40 +01001703 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001704 struct ly_ctx *ctx;
1705 struct lys_node *next, *elem;
1706
1707 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001708
1709 lydict_remove(ctx, dev->target_name);
1710 lydict_remove(ctx, dev->dsc);
1711 lydict_remove(ctx, dev->ref);
1712
Pavol Vican64d0b762016-08-25 10:44:59 +02001713 if (!dev->deviate) {
1714 return ;
1715 }
1716
Michal Vaskoff006c12016-02-17 11:15:19 +01001717 /* the module was freed, but we only need the context from orig_node, use ours */
1718 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1719 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1720 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1721 elem->module = module;
1722
1723 LY_TREE_DFS_END(dev->orig_node, next, elem);
1724 }
1725 lys_node_free(dev->orig_node, NULL, 0);
1726 } else {
1727 /* it's just a shallow copy, freeing one node */
1728 dev->orig_node->module = module;
1729 lys_node_free(dev->orig_node, NULL, 1);
1730 }
1731
Radek Krejcieb00f512015-07-01 16:44:58 +02001732 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001733 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02001734 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02001735 }
1736 free(dev->deviate[i].dflt);
1737
Radek Krejcieb00f512015-07-01 16:44:58 +02001738 lydict_remove(ctx, dev->deviate[i].units);
1739
1740 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1741 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001742 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001743 }
1744 free(dev->deviate[i].must);
1745
1746 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001747 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1748 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1749 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001750 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001751 }
1752 free(dev->deviate[i].unique);
1753 }
1754 }
1755 free(dev->deviate);
1756}
1757
Radek Krejci1d82ef62015-08-07 14:44:40 +02001758static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001759lys_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 +02001760{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001761 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001762
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001763 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001764 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001765 lydict_remove(ctx, uses->refine[i].dsc);
1766 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001767
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001768 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001769 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001770 }
1771 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001772
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001773 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02001774 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001775 }
1776 free(uses->refine[i].dflt);
1777
1778 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001779 lydict_remove(ctx, uses->refine[i].mod.presence);
1780 }
1781 }
1782 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001783
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001784 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001785 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001786 }
1787 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001788
Radek Krejci1d82ef62015-08-07 14:44:40 +02001789 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001790}
1791
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001792void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001793lys_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 +02001794{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001795 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001796 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001797
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001798 if (!node) {
1799 return;
1800 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001801
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001802 assert(node->module);
1803 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001804
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001805 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001806
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001807 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001808 if (node->priv && private_destructor) {
1809 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001810 }
1811
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001812 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001813 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001814 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001815 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001816 lydict_remove(ctx, node->dsc);
1817 lydict_remove(ctx, node->ref);
1818 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001819
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001820 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001821 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001822 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001823 }
1824 }
1825
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001826 /* specific part */
1827 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001828 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001829 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001830 break;
Radek Krejci76512572015-08-04 09:47:08 +02001831 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001832 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001833 break;
Radek Krejci76512572015-08-04 09:47:08 +02001834 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001835 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001836 break;
Radek Krejci76512572015-08-04 09:47:08 +02001837 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001838 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001839 break;
Radek Krejci76512572015-08-04 09:47:08 +02001840 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001841 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001842 break;
Radek Krejci76512572015-08-04 09:47:08 +02001843 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001844 case LYS_ANYDATA:
1845 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001846 break;
Radek Krejci76512572015-08-04 09:47:08 +02001847 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001848 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001849 break;
Radek Krejci76512572015-08-04 09:47:08 +02001850 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001851 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001852 break;
Radek Krejci76512572015-08-04 09:47:08 +02001853 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001854 /* do nothing */
1855 break;
Radek Krejci76512572015-08-04 09:47:08 +02001856 case LYS_GROUPING:
1857 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001858 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001859 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001860 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02001861 case LYS_NOTIF:
1862 lys_notif_free(ctx, (struct lys_node_notif *)node);
1863 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001864 case LYS_INPUT:
1865 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001866 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001867 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001868 case LYS_UNKNOWN:
1869 LOGINT;
1870 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001871 }
Radek Krejci5a065542015-05-22 15:02:07 +02001872
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001873 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001874 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001875 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001876}
1877
Michal Vasko1e62a092015-12-01 12:27:20 +01001878const struct lys_module *
Radek Krejci0fa54e92016-09-14 14:01:05 +02001879lys_get_implemented_module(const struct lys_module *mod)
1880{
1881 struct ly_ctx *ctx;
1882 int i;
1883
1884 if (!mod || mod->implemented) {
1885 /* invalid argument or the module itself is implemented */
1886 return mod;
1887 }
1888
1889 ctx = mod->ctx;
1890 for (i = 0; i < ctx->models.used; i++) {
1891 if (!ctx->models.list[i]->implemented) {
1892 continue;
1893 }
1894
1895 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
1896 /* we have some revision of the module implemented */
1897 return ctx->models.list[i];
1898 }
1899 }
1900
1901 /* we have no revision of the module implemented, return the module itself,
1902 * it is up to the caller to set the module implemented when needed */
1903 return mod;
1904}
1905
1906const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01001907lys_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 +02001908{
Radek Krejcic071c542016-01-27 14:57:51 +01001909 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02001910 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001911 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02001912
Michal Vaskoa7789a82016-02-11 15:42:55 +01001913 assert(!prefix || !name);
1914
Michal Vaskob6729c62015-10-21 12:09:47 +02001915 if (prefix && !pref_len) {
1916 pref_len = strlen(prefix);
1917 }
1918 if (name && !name_len) {
1919 name_len = strlen(name);
1920 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02001921
Radek Krejcic4283442016-04-22 09:19:27 +02001922 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01001923
1924 /* module own prefix, submodule own prefix, (sub)module own name */
1925 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
1926 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01001927 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01001928 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02001929 }
1930
Michal Vasko89563fc2016-07-28 16:19:35 +02001931 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02001932 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01001933 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
1934 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02001935 return module->imp[i].module;
1936 }
1937 }
1938
Michal Vasko89563fc2016-07-28 16:19:35 +02001939 /* module required by a foreign grouping, deviation, or submodule */
1940 if (name) {
1941 str = strndup(name, name_len);
1942 if (!str) {
1943 LOGMEM;
1944 return NULL;
1945 }
1946 main_module = ly_ctx_get_module(module->ctx, str, NULL);
1947 free(str);
1948 return main_module;
1949 }
1950
Michal Vasko8ce24d72015-10-21 11:27:26 +02001951 return NULL;
1952}
1953
Michal Vasko13b15832015-08-19 11:04:48 +02001954/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001955static void
Michal Vaskob746fff2016-02-11 11:37:50 +01001956module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02001957{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001958 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01001959 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001960 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001961
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001962 assert(module->ctx);
1963 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001964
Michal Vaskob746fff2016-02-11 11:37:50 +01001965 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01001966 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02001967 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02001968 lydict_remove(ctx, module->imp[i].dsc);
1969 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci225376f2016-02-16 17:36:22 +01001970 }
Radek Krejcidce51452015-06-16 15:20:08 +02001971 free(module->imp);
1972
Radek Krejcic071c542016-01-27 14:57:51 +01001973 /* submodules don't have data tree, the data nodes
1974 * are placed in the main module altogether */
1975 if (!module->type) {
1976 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001977 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001978 }
Radek Krejci21181962015-06-30 14:11:00 +02001979 }
Radek Krejci5a065542015-05-22 15:02:07 +02001980
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001981 lydict_remove(ctx, module->dsc);
1982 lydict_remove(ctx, module->ref);
1983 lydict_remove(ctx, module->org);
1984 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01001985 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001986
Radek Krejcieb00f512015-07-01 16:44:58 +02001987 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001988 for (i = 0; i < module->rev_size; i++) {
1989 lydict_remove(ctx, module->rev[i].dsc);
1990 lydict_remove(ctx, module->rev[i].ref);
1991 }
1992 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001993
Radek Krejcieb00f512015-07-01 16:44:58 +02001994 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001995 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001996 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001997 }
1998 module->ident_size = 0;
1999 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002000
Radek Krejcieb00f512015-07-01 16:44:58 +02002001 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002002 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002003 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002004 }
2005 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002006
Radek Krejcieb00f512015-07-01 16:44:58 +02002007 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002008 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002009 lydict_remove(ctx, module->inc[i].dsc);
2010 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcic071c542016-01-27 14:57:51 +01002011 /* complete submodule free is done only from main module since
2012 * submodules propagate their includes to the main module */
2013 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002014 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002015 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002016 }
2017 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002018
Radek Krejcieb00f512015-07-01 16:44:58 +02002019 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002020 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002021 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002022 }
2023 free(module->augment);
2024
Radek Krejcieb00f512015-07-01 16:44:58 +02002025 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002026 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002027 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002028 }
2029 free(module->features);
2030
Radek Krejcieb00f512015-07-01 16:44:58 +02002031 /* deviations */
2032 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002033 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002034 }
2035 free(module->deviation);
2036
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002037 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002038 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002039}
2040
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002041void
Michal Vaskob746fff2016-02-11 11:37:50 +01002042lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002043{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002044 if (!submodule) {
2045 return;
2046 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002048 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002049 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002050
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002051 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002052
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002053 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002054}
2055
Radek Krejci3a5501d2016-07-18 22:03:34 +02002056static int
2057ingrouping(const struct lys_node *node)
2058{
2059 const struct lys_node *iter = node;
2060 assert(node);
2061
2062 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2063 if (!iter) {
2064 return 0;
2065 } else {
2066 return 1;
2067 }
2068}
2069
Radek Krejci76512572015-08-04 09:47:08 +02002070struct lys_node *
Michal Vaskoe022a562016-09-27 14:24:15 +02002071lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2072 struct unres_schema *unres, int shallow)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002073{
Radek Krejcic071c542016-01-27 14:57:51 +01002074 struct lys_node *retval = NULL, *child;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002075 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002076 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002077 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002078 struct unres_list_uniq *unique_info;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002079
Michal Vaskoc07187d2015-08-13 15:20:57 +02002080 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002081 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002082 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002083 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002084 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002085 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002086 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002087 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002088 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002089 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002090 struct lys_node_anydata *any = NULL;
2091 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002092 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002093 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002094 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002095 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002096 struct lys_node_rpc_action *rpc = NULL;
2097 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2098 struct lys_node_inout *io = NULL;
2099 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2100 struct lys_node_rpc_action *ntf = NULL;
2101 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002102 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002103 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002104
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002105 /* we cannot just duplicate memory since the strings are stored in
2106 * dictionary and we need to update dictionary counters.
2107 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002108
Radek Krejci1d82ef62015-08-07 14:44:40 +02002109 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002110 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002111 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002112 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 break;
2114
Radek Krejci76512572015-08-04 09:47:08 +02002115 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002116 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002117 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002118 break;
2119
Radek Krejci76512572015-08-04 09:47:08 +02002120 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002121 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002122 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002123 break;
2124
Radek Krejci76512572015-08-04 09:47:08 +02002125 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002126 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002127 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002128 break;
2129
Radek Krejci76512572015-08-04 09:47:08 +02002130 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002131 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002132 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002133 break;
2134
Radek Krejci76512572015-08-04 09:47:08 +02002135 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002136 case LYS_ANYDATA:
2137 any = calloc(1, sizeof *any);
2138 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002139 break;
2140
Radek Krejci76512572015-08-04 09:47:08 +02002141 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002142 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002143 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002144 break;
2145
Radek Krejci76512572015-08-04 09:47:08 +02002146 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002147 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002148 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002149 break;
2150
Radek Krejci76512572015-08-04 09:47:08 +02002151 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002152 grp = calloc(1, sizeof *grp);
2153 retval = (struct lys_node *)grp;
2154 break;
2155
Radek Krejci76512572015-08-04 09:47:08 +02002156 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002157 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002158 rpc = calloc(1, sizeof *rpc);
2159 retval = (struct lys_node *)rpc;
2160 break;
2161
Radek Krejci76512572015-08-04 09:47:08 +02002162 case LYS_INPUT:
2163 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002164 io = calloc(1, sizeof *io);
2165 retval = (struct lys_node *)io;
2166 break;
2167
Radek Krejci76512572015-08-04 09:47:08 +02002168 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002169 ntf = calloc(1, sizeof *ntf);
2170 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002171 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002172
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002173 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002174 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002175 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002176 }
Radek Krejcib388c152015-06-04 17:03:03 +02002177
Michal Vasko253035f2015-12-17 16:58:13 +01002178 if (!retval) {
2179 LOGMEM;
2180 return NULL;
2181 }
2182
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002183 /*
2184 * duplicate generic part of the structure
2185 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002186 retval->name = lydict_insert(ctx, node->name, 0);
2187 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2188 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002189 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002190 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002191
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002192 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002193 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002194
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002195 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002196
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002197 retval->iffeature_size = node->iffeature_size;
2198 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2199 if (!retval->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01002200 LOGMEM;
2201 goto error;
2202 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002203
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002204 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002205 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002206 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2207 if (size1) {
2208 /* there is something to duplicate */
2209
2210 /* duplicate compiled expression */
2211 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2212 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2213 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2214
2215 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002216 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002217 for (j = 0; (unsigned int)j < size2; j++) {
2218 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2219 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002220 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002221 /* feature is resolved in origin, so copy it
2222 * - duplication is used for instantiating groupings
2223 * and if-feature inside grouping is supposed to be
2224 * resolved inside the original grouping, so we want
2225 * to keep pointers to features from the grouping
2226 * context */
2227 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2228 } else if (rc == -1) {
2229 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002230 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002231 }
2232 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002233 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002234
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002235 /* connect it to the parent */
2236 if (lys_node_addchild(parent, retval->module, retval)) {
2237 goto error;
2238 }
Radek Krejcidce51452015-06-16 15:20:08 +02002239
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002240 /* go recursively */
2241 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2242 LY_TREE_FOR(node->child, child) {
Michal Vaskoe022a562016-09-27 14:24:15 +02002243 if (!lys_node_dup(module, retval, child, retval->nacm, unres, 0)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002244 goto error;
2245 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002246 }
2247 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002248 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002249 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002250 }
2251
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002252 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002253 * duplicate specific part of the structure
2254 */
2255 switch (node->nodetype) {
2256 case LYS_CONTAINER:
2257 if (cont_orig->when) {
2258 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002259 }
2260 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002261
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002262 cont->must_size = cont_orig->must_size;
2263 cont->tpdf_size = cont_orig->tpdf_size;
2264
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002265 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002266 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002267 break;
2268
2269 case LYS_CHOICE:
2270 if (choice_orig->when) {
2271 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002272 }
2273
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002274 if (!shallow) {
2275 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002276 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2277 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2278 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002279 if (rc) {
2280 if (rc == EXIT_FAILURE) {
2281 LOGINT;
2282 }
2283 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002284 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002285 } else {
2286 /* useless to check return value, we don't know whether
2287 * there really wasn't any default defined or it just hasn't
2288 * been resolved, we just hope for the best :)
2289 */
2290 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002291 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002292 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002293 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002294 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002295 break;
2296
2297 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002298 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002299 goto error;
2300 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002301 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2302
2303 if (leaf_orig->dflt) {
2304 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci48464ed2016-03-17 15:44:09 +01002305 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002306 goto error;
2307 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002308 }
2309
2310 leaf->must_size = leaf_orig->must_size;
2311 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002312
2313 if (leaf_orig->when) {
2314 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002315 }
2316 break;
2317
2318 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002319 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002320 goto error;
2321 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002322 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2323
2324 llist->min = llist_orig->min;
2325 llist->max = llist_orig->max;
2326
2327 llist->must_size = llist_orig->must_size;
2328 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002329
2330 if (llist_orig->when) {
2331 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002332 }
2333 break;
2334
2335 case LYS_LIST:
2336 list->min = list_orig->min;
2337 list->max = list_orig->max;
2338
2339 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002340 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002341
Radek Krejci581ce772015-11-10 17:22:40 +01002342 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002343 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002344
Radek Krejci581ce772015-11-10 17:22:40 +01002345 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002346 if (list->keys_size) {
2347 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01002348 if (!list->keys) {
2349 LOGMEM;
2350 goto error;
2351 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002352
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002353 if (!shallow) {
2354 /* we managed to resolve it before, resolve it again manually */
2355 if (list_orig->keys[0]) {
2356 for (i = 0; i < list->keys_size; ++i) {
2357 rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF,
2358 (const struct lys_node **)&list->keys[i]);
2359 if (rc) {
2360 if (rc == EXIT_FAILURE) {
2361 LOGINT;
2362 }
2363 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002364 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002365 }
2366 /* it was not resolved yet, add unres copy */
2367 } else {
2368 if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) {
2369 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002370 goto error;
2371 }
Michal Vasko38d01f72015-06-15 09:41:06 +02002372 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002373 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002374 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002375 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002376 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002377
Radek Krejci581ce772015-11-10 17:22:40 +01002378 list->unique_size = list_orig->unique_size;
2379 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002380 if (!list->unique) {
2381 LOGMEM;
2382 goto error;
2383 }
Radek Krejci581ce772015-11-10 17:22:40 +01002384 for (i = 0; i < list->unique_size; ++i) {
2385 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2386 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002387 if (!list->unique[i].expr) {
2388 LOGMEM;
2389 goto error;
2390 }
Radek Krejci581ce772015-11-10 17:22:40 +01002391 for (j = 0; j < list->unique[i].expr_size; j++) {
2392 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2393
2394 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02002395 unique_info = malloc(sizeof *unique_info);
2396 unique_info->list = (struct lys_node *)list;
2397 unique_info->expr = list->unique[i].expr[j];
2398 unique_info->trg_type = &list->unique[i].trg_type;
2399 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002400 }
2401 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002402
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002403 if (list_orig->when) {
2404 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02002405 }
Radek Krejcidce51452015-06-16 15:20:08 +02002406 break;
2407
2408 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002409 case LYS_ANYDATA:
2410 any->must_size = any_orig->must_size;
2411 any->must = lys_restr_dup(ctx, any_orig->must, any->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002412
Radek Krejcibf2abff2016-08-23 15:51:52 +02002413 if (any_orig->when) {
2414 any->when = lys_when_dup(ctx, any_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002415 }
2416 break;
2417
2418 case LYS_USES:
2419 uses->grp = uses_orig->grp;
2420
2421 if (uses_orig->when) {
2422 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002423 }
2424
2425 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002426 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002427 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002428 if (!shallow) {
2429 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Michal Vaskocda51712016-05-19 15:22:11 +02002430 if (!uses->grp || uses->grp->nacm) {
2431 assert(!uses->child);
Radek Krejci48464ed2016-03-17 15:44:09 +01002432 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002433 goto error;
2434 }
Michal Vasko49168a22015-08-17 16:35:41 +02002435 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002436 } else {
2437 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002438 }
2439 break;
2440
Radek Krejcidce51452015-06-16 15:20:08 +02002441 case LYS_CASE:
2442 if (cs_orig->when) {
2443 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02002444 }
2445 break;
2446
2447 case LYS_GROUPING:
2448 grp->tpdf_size = grp_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002449 grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002450 break;
2451
2452 case LYS_RPC:
2453 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002454 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002455 break;
2456
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002457 case LYS_INPUT:
2458 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002459 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002460 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002461 break;
2462
Radek Krejcida04f4a2015-05-21 12:54:09 +02002463 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002464 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002465 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002466 break;
2467
2468 default:
2469 /* LY_NODE_AUGMENT */
2470 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002471 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002472 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002473
2474 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002475
2476error:
2477
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002478 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002479 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002480}
2481
Michal Vasko13b15832015-08-19 11:04:48 +02002482void
Michal Vaskoff006c12016-02-17 11:15:19 +01002483lys_node_switch(struct lys_node *dst, struct lys_node *src)
2484{
2485 struct lys_node *child;
2486
Michal Vaskob42b6972016-06-06 14:21:30 +02002487 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01002488
2489 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02002490 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002491 dst->prev->next = src;
2492 }
2493
2494 /* sibling prev */
2495 if (dst->next) {
2496 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02002497 } else {
2498 for (child = dst->prev; child->prev->next; child = child->prev);
2499 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01002500 }
2501
2502 /* next */
2503 src->next = dst->next;
2504 dst->next = NULL;
2505
2506 /* prev */
2507 if (dst->prev != dst) {
2508 src->prev = dst->prev;
2509 }
2510 dst->prev = dst;
2511
2512 /* parent child */
2513 if (dst->parent && (dst->parent->child == dst)) {
2514 dst->parent->child = src;
2515 }
2516
2517 /* parent */
2518 src->parent = dst->parent;
2519 dst->parent = NULL;
2520
2521 /* child parent */
2522 LY_TREE_FOR(dst->child, child) {
2523 if (child->parent == dst) {
2524 child->parent = src;
2525 }
2526 }
2527
2528 /* child */
2529 src->child = dst->child;
2530 dst->child = NULL;
2531}
2532
2533void
Michal Vasko627975a2016-02-11 11:39:03 +01002534lys_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 +02002535{
2536 struct ly_ctx *ctx;
2537 int i;
2538
2539 if (!module) {
2540 return;
2541 }
2542
2543 /* remove schema from the context */
2544 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002545 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002546 for (i = 0; i < ctx->models.used; i++) {
2547 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002548 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002549 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002550 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 +02002551 ctx->models.list[ctx->models.used] = NULL;
2552 /* we are done */
2553 break;
2554 }
2555 }
2556 }
2557
2558 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002559 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002560
2561 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002562 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002563
2564 free(module);
2565}
Radek Krejci7e97c352015-06-19 16:26:34 +02002566
2567/*
2568 * op: 1 - enable, 0 - disable
2569 */
2570static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002571lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002572{
2573 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002574 int i, j, k;
Radek Krejci7e97c352015-06-19 16:26:34 +02002575
2576 if (!module || !name || !strlen(name)) {
2577 return EXIT_FAILURE;
2578 }
2579
2580 if (!strcmp(name, "*")) {
2581 /* enable all */
2582 all = 1;
2583 }
2584
2585 /* module itself */
2586 for (i = 0; i < module->features_size; i++) {
2587 if (all || !strcmp(module->features[i].name, name)) {
2588 if (op) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002589 /* check referenced features if they are enabled */
2590 for (j = 0; j < module->features[i].iffeature_size; j++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02002591 if (!resolve_iffeature(&module->features[i].iffeature[j])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002592 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2593 module->features[i].name, j + 1);
2594 return EXIT_FAILURE;
2595 }
2596 }
2597
Radek Krejci1574a8d2015-08-03 14:16:52 +02002598 module->features[i].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002599 } else {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002600 module->features[i].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002601 }
2602 if (!all) {
2603 return EXIT_SUCCESS;
2604 }
2605 }
2606 }
2607
2608 /* submodules */
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002609 for (i = 0; i < module->inc_size; i++) {
2610 for (j = 0; j < module->inc[i].submodule->features_size; j++) {
2611 if (all || !strcmp(module->inc[i].submodule->features[j].name, name)) {
Radek Krejci7e97c352015-06-19 16:26:34 +02002612 if (op) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002613 /* check referenced features if they are enabled */
2614 for (k = 0; k < module->inc[i].submodule->features[j].iffeature_size; k++) {
Radek Krejci69b8d922016-07-27 13:13:41 +02002615 if (!resolve_iffeature(&module->inc[i].submodule->features[j].iffeature[k])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002616 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2617 module->inc[i].submodule->features[j].name, k + 1);
2618 return EXIT_FAILURE;
2619 }
2620 }
2621
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002622 module->inc[i].submodule->features[j].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002623 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002624 module->inc[i].submodule->features[j].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002625 }
2626 if (!all) {
2627 return EXIT_SUCCESS;
2628 }
2629 }
2630 }
2631 }
2632
2633 if (all) {
2634 return EXIT_SUCCESS;
2635 } else {
2636 return EXIT_FAILURE;
2637 }
2638}
2639
2640API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002641lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002642{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002643 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002644}
2645
2646API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002647lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002648{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002649 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002650}
2651
2652API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002653lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002654{
2655 int i, j;
2656
2657 if (!module || !feature) {
2658 return -1;
2659 }
2660
2661 /* search for the specified feature */
2662 /* module itself */
2663 for (i = 0; i < module->features_size; i++) {
2664 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002665 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002666 return 1;
2667 } else {
2668 return 0;
2669 }
2670 }
2671 }
2672
2673 /* submodules */
2674 for (j = 0; j < module->inc_size; j++) {
2675 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2676 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002677 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002678 return 1;
2679 } else {
2680 return 0;
2681 }
2682 }
2683 }
2684 }
2685
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002686 /* feature definition not found */
2687 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002688}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002689
Radek Krejci96a10da2015-07-30 11:00:14 +02002690API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01002691lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002692{
Radek Krejci96a10da2015-07-30 11:00:14 +02002693 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002694 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002695 unsigned int count;
2696
2697 if (!module) {
2698 return NULL;
2699 }
2700
2701 count = module->features_size;
2702 for (i = 0; i < module->inc_size; i++) {
2703 count += module->inc[i].submodule->features_size;
2704 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002705 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01002706 if (!result) {
2707 LOGMEM;
2708 return NULL;
2709 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002710 if (states) {
2711 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01002712 if (!(*states)) {
2713 LOGMEM;
2714 free(result);
2715 return NULL;
2716 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002717 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002718 count = 0;
2719
2720 /* module itself */
2721 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002722 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002723 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002724 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002725 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002726 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002727 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002728 }
2729 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002730 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002731 }
2732
2733 /* submodules */
2734 for (j = 0; j < module->inc_size; j++) {
2735 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002736 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02002737 if (states) {
2738 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
2739 (*states)[count] = 1;
2740 } else {
2741 (*states)[count] = 0;
2742 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002743 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002744 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002745 }
2746 }
2747
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002748 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02002749 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002750
2751 return result;
2752}
Michal Vaskobaefb032015-09-24 14:52:10 +02002753
Radek Krejci6910a032016-04-13 10:06:21 +02002754API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01002755lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01002756{
2757 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
2758}
2759
Radek Krejci6910a032016-04-13 10:06:21 +02002760API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02002761lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01002762{
2763 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
2764}
2765
Michal Vaskobaefb032015-09-24 14:52:10 +02002766API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01002767lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02002768{
2769 if (!node || !node->parent) {
2770 return NULL;
2771 }
2772
2773 if (node->parent->nodetype == LYS_AUGMENT) {
2774 return ((struct lys_node_augment *)node->parent)->target;
2775 }
2776
2777 return node->parent;
2778}
Michal Vasko1b229152016-01-13 11:28:38 +01002779
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002780API void *
Michal Vasko1b229152016-01-13 11:28:38 +01002781lys_set_private(const struct lys_node *node, void *priv)
2782{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002783 void *prev;
2784
Michal Vasko1b229152016-01-13 11:28:38 +01002785 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002786 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
2787 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01002788 }
2789
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002790 prev = node->priv;
2791 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002792
2793 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01002794}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002795
Michal Vasko01c6fd22016-05-20 11:43:05 +02002796int
2797lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
2798{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02002799 struct lys_node_leaf *iter = leafref_target;
2800
Michal Vasko48a573d2016-07-01 11:46:02 +02002801 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02002802 LOGINT;
2803 return -1;
2804 }
2805
Pavol Vican93175152016-08-30 15:34:44 +02002806 /* check for config flag */
2807 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
2808 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
2809 "The %s is config but refers to a non-config %s.",
2810 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
2811 return -1;
2812 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02002813 /* check for cycles */
2814 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
2815 if ((void *)iter == (void *)leafref) {
2816 /* cycle detected */
2817 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
2818 return -1;
2819 }
2820 iter = iter->type.info.lref.target;
2821 }
2822
2823 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02002824 * leafrefs referencing the leaf(-list) */
Michal Vasko01c6fd22016-05-20 11:43:05 +02002825 if (!leafref_target->child) {
2826 leafref_target->child = (void*)ly_set_new();
2827 if (!leafref_target->child) {
2828 LOGMEM;
2829 return -1;
2830 }
2831 }
Radek Krejci09891a22016-06-10 10:59:22 +02002832 ly_set_add((struct ly_set *)leafref_target->child, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02002833
2834 return 0;
2835}
2836
Michal Vasko8548e082016-07-22 12:00:18 +02002837/* not needed currently */
2838#if 0
2839
Michal Vasko5b3492c2016-07-20 09:37:40 +02002840static const char *
2841lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
2842{
2843 struct lys_module *prev_mod;
2844 uint32_t str_len, mod_len, buf_idx;
2845
Radek Krejcibf2abff2016-08-23 15:51:52 +02002846 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02002847 LOGINT;
2848 return NULL;
2849 }
2850
2851 buf_idx = buf_len - 1;
2852 buf[buf_idx] = '\0';
2853
2854 while (node) {
2855 if (lys_parent(node)) {
2856 prev_mod = lys_node_module(lys_parent(node));
2857 } else {
2858 prev_mod = NULL;
2859 }
2860
Radek Krejcibf2abff2016-08-23 15:51:52 +02002861 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02002862 str_len = strlen(node->name);
2863
2864 if (prev_mod != node->module) {
2865 mod_len = strlen(node->module->name);
2866 } else {
2867 mod_len = 0;
2868 }
2869
2870 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
2871 LOGINT;
2872 return NULL;
2873 }
2874
2875 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
2876
2877 buf[buf_idx] = '/';
2878 if (mod_len) {
2879 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
2880 buf[buf_idx + 1 + mod_len] = ':';
2881 }
2882 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
2883 }
2884
2885 node = lys_parent(node);
2886 }
2887
2888 return buf + buf_idx;
2889}
2890
Michal Vasko8548e082016-07-22 12:00:18 +02002891#endif
2892
2893API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02002894lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02002895{
2896 struct lyxp_set set;
2897 struct ly_set *ret_set;
2898 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02002899 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02002900
2901 if (!node || !expr) {
2902 ly_errno = LY_EINVAL;
2903 return NULL;
2904 }
2905
2906 memset(&set, 0, sizeof set);
2907
Michal Vaskodb1da032016-09-08 10:07:38 +02002908 opts = LYXP_SNODE;
2909 if (options & LYS_FIND_OUTPUT) {
2910 opts |= LYXP_SNODE_OUTPUT;
2911 }
2912
Michal Vasko46a4bf92016-09-08 08:23:49 +02002913 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02002914 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02002915 free(set.val.snodes);
2916 return NULL;
2917 }
2918
2919 ret_set = ly_set_new();
2920
2921 for (i = 0; i < set.used; ++i) {
2922 if (!set.val.snodes[i].in_ctx) {
2923 continue;
2924 }
2925 assert(set.val.snodes[i].in_ctx == 1);
2926
2927 switch (set.val.snodes[i].type) {
2928 case LYXP_NODE_ELEM:
2929 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
2930 ly_set_free(ret_set);
2931 free(set.val.snodes);
2932 return NULL;
2933 }
2934 break;
2935 default:
2936 /* ignore roots, text and attr should not ever appear */
2937 break;
2938 }
2939 }
2940
2941 free(set.val.snodes);
2942 return ret_set;
2943}
2944
2945API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02002946lys_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 +02002947{
Michal Vasko508a50d2016-09-07 14:50:33 +02002948 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02002949 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02002950 uint32_t i;
2951
Michal Vaskob94a5e42016-09-08 14:01:56 +02002952 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02002953 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02002954 }
2955
Michal Vaskob94a5e42016-09-08 14:01:56 +02002956 /* adjust the root */
2957 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
2958 do {
2959 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
2960 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
2961 }
2962
Michal Vasko508a50d2016-09-07 14:50:33 +02002963 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02002964
2965 if (options & LYXP_MUST) {
2966 options &= ~LYXP_MUST;
2967 options |= LYXP_SNODE_MUST;
2968 } else if (options & LYXP_WHEN) {
2969 options &= ~LYXP_WHEN;
2970 options |= LYXP_SNODE_WHEN;
2971 } else {
2972 options |= LYXP_SNODE;
2973 }
2974
Michal Vasko508a50d2016-09-07 14:50:33 +02002975 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
2976 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02002977 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02002978 }
2979
Michal Vasko8548e082016-07-22 12:00:18 +02002980 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02002981
Michal Vasko508a50d2016-09-07 14:50:33 +02002982 for (i = 0; i < set.used; ++i) {
2983 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02002984 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02002985 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02002986 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02002987 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02002988 return NULL;
2989 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02002990 break;
2991 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02002992 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02002993 break;
2994 }
2995 }
2996
Michal Vasko508a50d2016-09-07 14:50:33 +02002997 free(set.val.snodes);
2998 return ret_set;
2999}
3000
3001API struct ly_set *
3002lys_node_xpath_atomize(const struct lys_node *node, int options)
3003{
3004 const struct lys_node *next, *elem, *parent, *tmp;
3005 struct lyxp_set set;
3006 struct ly_set *ret_set;
3007 uint16_t i;
3008
3009 if (!node) {
3010 return NULL;
3011 }
3012
3013 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3014 if (!parent) {
3015 /* not in input, output, or notification */
3016 return NULL;
3017 }
3018
3019 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003020 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003021 return NULL;
3022 }
3023
3024 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe9914d12016-10-07 14:32:37 +02003025 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_VALID_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003026 /* elem has no dependencies from other subtrees and local nodes get discarded */
3027 goto next_iter;
3028 }
3029
3030 if (lyxp_node_atomize(elem, &set)) {
3031 ly_set_free(ret_set);
3032 free(set.val.snodes);
3033 return NULL;
3034 }
3035
3036 for (i = 0; i < set.used; ++i) {
3037 switch (set.val.snodes[i].type) {
3038 case LYXP_NODE_ELEM:
3039 if (options & LYXP_NO_LOCAL) {
3040 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3041 if (tmp) {
3042 /* in local subtree, discard */
3043 break;
3044 }
3045 }
3046 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3047 ly_set_free(ret_set);
3048 free(set.val.snodes);
3049 return NULL;
3050 }
3051 break;
3052 default:
3053 /* ignore roots, text and attr should not ever appear */
3054 break;
3055 }
3056 }
3057
3058 free(set.val.snodes);
3059 if (!(options & LYXP_RECURSIVE)) {
3060 break;
3061 }
3062next_iter:
3063 LY_TREE_DFS_END(node, next, elem);
3064 }
3065
Michal Vasko8548e082016-07-22 12:00:18 +02003066 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003067}
3068
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003069static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003070lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003071{
3072 int ret;
3073 char *parent_path;
3074 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003075
Pavol Vican64d0b762016-08-25 10:44:59 +02003076 if (!dev->deviate) {
3077 return ;
3078 }
3079
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003080 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3081 if (dev->orig_node) {
3082 /* removing not-supported deviation ... */
3083 if (strrchr(dev->target_name, '/') != dev->target_name) {
3084 /* ... from a parent */
3085 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3086
3087 target = NULL;
3088 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target);
3089 free(parent_path);
3090 if (ret || !target) {
3091 LOGINT;
3092 return;
3093 }
3094
3095 lys_node_addchild(target, NULL, dev->orig_node);
3096 } else {
3097 /* ... from top-level data */
3098 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3099 }
3100
3101 dev->orig_node = NULL;
3102 } else {
3103 /* adding not-supported deviation */
3104 target = NULL;
3105 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3106 if (ret || !target) {
3107 LOGINT;
3108 return;
3109 }
3110
3111 lys_node_unlink(target);
3112 dev->orig_node = target;
3113 }
3114 } else {
3115 target = NULL;
3116 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3117 if (ret || !target) {
3118 LOGINT;
3119 return;
3120 }
3121
3122 lys_node_switch(target, dev->orig_node);
3123 dev->orig_node = target;
3124 }
3125}
3126
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003127/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3128void
3129lys_switch_deviations(struct lys_module *module)
3130{
Michal Vasko89563fc2016-07-28 16:19:35 +02003131 uint32_t i = 0, j;
3132 const struct lys_module *mod;
3133 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003134
Michal Vasko89563fc2016-07-28 16:19:35 +02003135 if (module->deviated) {
3136 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3137 if (mod == module) {
3138 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003139 }
3140
Michal Vasko89563fc2016-07-28 16:19:35 +02003141 for (j = 0; j < mod->deviation_size; ++j) {
3142 ptr = strstr(mod->deviation[j].target_name, module->name);
3143 if (ptr && ptr[strlen(module->name)] == ':') {
3144 lys_switch_deviation(&mod->deviation[j], module);
3145 }
3146 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003147 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003148
Michal Vasko89563fc2016-07-28 16:19:35 +02003149 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003150 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003151 } else {
3152 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003153 }
3154 }
3155}
3156
3157/* not needed currently, but tested and working */
3158#if 0
3159
3160void
3161lys_sub_module_apply_devs_augs(struct lys_module *module)
3162{
3163 int i;
3164 struct lys_node_augment *aug;
3165 struct lys_node *last;
3166
3167 /* re-apply deviations */
3168 for (i = 0; i < module->deviation_size; ++i) {
3169 lys_switch_deviation(&module->deviation[i], module);
3170 assert(module->deviation[i].orig_node);
Michal Vasko89563fc2016-07-28 16:19:35 +02003171 lys_node_module(module->deviation[i].orig_node)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003172 }
3173
3174 /* re-apply augments */
3175 for (i = 0; i < module->augment_size; ++i) {
3176 aug = &module->augment[i];
3177 assert(aug->target);
3178
3179 /* reconnect augmenting data into the target - add them to the target child list */
3180 if (aug->target->child) {
3181 last = aug->target->child->prev;
3182 last->next = aug->child;
3183 aug->target->child->prev = aug->child->prev;
3184 aug->child->prev = last;
3185 } else {
3186 aug->target->child = aug->child;
3187 }
3188 }
3189}
3190
3191#endif
3192
3193void
3194lys_sub_module_remove_devs_augs(struct lys_module *module)
3195{
Michal Vasko89563fc2016-07-28 16:19:35 +02003196 uint32_t i = 0, j;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003197 struct lys_node *last, *elem;
Michal Vasko89563fc2016-07-28 16:19:35 +02003198 const struct lys_module *mod;
3199 struct lys_module *target_mod;
3200 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003201
3202 /* remove applied deviations */
3203 for (i = 0; i < module->deviation_size; ++i) {
Pavol Vican64d0b762016-08-25 10:44:59 +02003204 if (module->deviation[i].orig_node) {
3205 target_mod = lys_node_module(module->deviation[i].orig_node);
3206 } else {
3207 target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1,
3208 strcspn(module->deviation[i].target_name, ":") - 1);
Radek Krejci0fa54e92016-09-14 14:01:05 +02003209 target_mod = (struct lys_module *)lys_get_implemented_module(target_mod);
Pavol Vican64d0b762016-08-25 10:44:59 +02003210 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003211 lys_switch_deviation(&module->deviation[i], module);
3212
Michal Vasko89563fc2016-07-28 16:19:35 +02003213 /* clear the deviation flag if possible */
3214 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3215 if ((mod == module) || (mod == target_mod)) {
3216 continue;
3217 }
3218
3219 for (j = 0; j < mod->deviation_size; ++j) {
3220 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
3221 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
3222 /* some other module deviation targets the inspected module, flag remains */
3223 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003224 }
3225 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003226
3227 if (j < mod->deviation_size) {
3228 break;
3229 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003230 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003231
3232 if (!mod) {
3233 target_mod->deviated = 0;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003234 }
3235 }
3236
3237 /* remove applied augments */
3238 for (i = 0; i < module->augment_size; ++i) {
Radek Krejcidbc15262016-06-16 14:58:29 +02003239 if (!module->augment[i].target) {
3240 /* skip not resolved augments */
3241 continue;
3242 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003243
3244 elem = module->augment[i].child;
3245 if (elem) {
3246 LY_TREE_FOR(elem, last) {
3247 if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) {
3248 break;
3249 }
3250 }
3251 /* elem is first augment child, last is the last child */
3252
3253 /* parent child ptr */
3254 if (module->augment[i].target->child == elem) {
3255 module->augment[i].target->child = last->next;
3256 }
3257
3258 /* parent child next ptr */
3259 if (elem->prev->next) {
3260 elem->prev->next = last->next;
3261 }
3262
3263 /* parent child prev ptr */
3264 if (last->next) {
3265 last->next->prev = elem->prev;
3266 } else if (module->augment[i].target->child) {
3267 module->augment[i].target->child->prev = elem->prev;
3268 }
3269
3270 /* update augment children themselves */
3271 elem->prev = last;
3272 last->next = NULL;
3273 }
Michal Vaskob8f71322016-05-03 11:39:56 +02003274
3275 /* needs to be NULL for lys_augment_free() to free the children */
3276 module->augment[i].target = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003277 }
3278}
3279
Radek Krejci27fe55e2016-09-13 17:13:35 +02003280static int
3281lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
3282{
3283 struct lys_node *root, *next, *node;
3284 uint8_t i;
3285
3286 for (i = 0; i < module->augment_size; i++) {
3287 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003288 if (!module->augment[i].target
3289 && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003290 return -1;
3291 }
3292 }
3293 LY_TREE_FOR(module->data, root) {
3294 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
3295 LY_TREE_DFS_BEGIN(root, next, node) {
3296 if (node->nodetype == LYS_GROUPING) {
3297 goto nextsibling;
3298 }
3299 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3300 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
3301 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
3302 UNRES_TYPE_LEAFREF, node) == -1) {
3303 return EXIT_FAILURE;
3304 }
3305 }
3306 }
3307
3308 /* modified LY_TREE_DFS_END */
3309 next = node->child;
3310 /* child exception for leafs, leaflists and anyxml without children */
3311 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
3312 next = NULL;
3313 }
3314 if (!next) {
3315nextsibling:
3316 /* no children */
3317 if (node == root) {
3318 /* we are done, root has no children */
3319 break;
3320 }
3321 /* try siblings */
3322 next = node->next;
3323 }
3324 while (!next) {
3325 /* parent is already processed, go to its sibling */
3326 node = lys_parent(node);
3327 /* no siblings, go back through parents */
3328 if (lys_parent(node) == lys_parent(root)) {
3329 /* we are done, no next element to process */
3330 break;
3331 }
3332 next = node->next;
3333 }
3334 }
3335 }
3336
3337 return EXIT_SUCCESS;
3338}
3339
3340API int
3341lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02003342{
3343 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003344 struct unres_schema *unres;
3345 int i, j;
Michal Vasko26055752016-05-03 11:36:31 +02003346
Radek Krejci27fe55e2016-09-13 17:13:35 +02003347 if (!module) {
3348 ly_errno = LY_EINVAL;
3349 return EXIT_FAILURE;
3350 }
3351
3352 module = lys_main_module(module);
Michal Vasko26055752016-05-03 11:36:31 +02003353 if (module->implemented) {
3354 return EXIT_SUCCESS;
3355 }
3356
3357 ctx = module->ctx;
3358
3359 for (i = 0; i < ctx->models.used; ++i) {
3360 if (module == ctx->models.list[i]) {
3361 continue;
3362 }
3363
3364 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
3365 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
3366 return EXIT_FAILURE;
3367 }
3368 }
3369
Radek Krejci27fe55e2016-09-13 17:13:35 +02003370 unres = calloc(1, sizeof *unres);
3371 if (!unres) {
3372 LOGMEM;
3373 return EXIT_FAILURE;
3374 }
3375 /* recursively make the module implemented */
3376 ((struct lys_module *)module)->implemented = 1;
3377 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
3378 goto error;
3379 }
3380 /* process augments in submodules */
3381 for (i = 0; i < module->inc_size; ++i) {
3382 if (!module->inc[i].submodule) {
3383 continue;
3384 }
3385 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
3386 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003387 if (!module->inc[i].submodule->augment[i].target
3388 && (unres_schema_add_node((struct lys_module *)module->inc[i].submodule, unres,
3389 &module->inc[i].submodule->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003390 goto error;
3391 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003392 }
3393 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003394 /* resolve rest of unres items */
3395 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
3396 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003397 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003398 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02003399
3400 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003401
3402error:
3403 ((struct lys_module *)module)->implemented = 0;
3404 unres_schema_free((struct lys_module *)module, &unres);
3405 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003406}
3407
3408void
3409lys_submodule_module_data_free(struct lys_submodule *submodule)
3410{
3411 struct lys_node *next, *elem;
3412
3413 /* remove parsed data */
3414 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3415 if (elem->module == (struct lys_module *)submodule) {
3416 lys_node_free(elem, NULL, 0);
3417 }
3418 }
3419}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02003420
3421int
3422lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
3423{
3424 uint8_t i;
3425
3426 for (i = 0; i < list->keys_size; i++) {
3427 if (list->keys[i] == leaf) {
3428 return i + 1;
3429 }
3430 }
3431
3432 return 0;
3433}