blob: d75ecced53b8a55cafedd9d4020ebe3f67b0b956 [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 Vasko9e635ac2016-10-17 11:44:09 +0200107 while (siblings && (siblings->nodetype == LYS_USES)) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200108 siblings = siblings->child;
109 }
Michal Vasko9e635ac2016-10-17 11:44:09 +0200110 if (!siblings) {
111 /* unresolved uses */
112 return EXIT_FAILURE;
113 }
114
Michal Vasko680f8b42016-10-17 10:27:37 +0200115 if (siblings->nodetype == LYS_GROUPING) {
116 for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev);
117 if (node->nodetype == LYS_GROUPING) {
118 /* we went through all the siblings, only groupings there - no valid sibling */
119 return EXIT_FAILURE;
120 }
121 /* update siblings to be valid */
122 siblings = node;
123 }
124
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200125 /* set parent correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100126 parent = lys_parent(siblings);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200127
Michal Vasko680f8b42016-10-17 10:27:37 +0200128 /* go up all uses */
129 while (parent && (parent->nodetype == LYS_USES)) {
130 parent = lys_parent(parent);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200131 }
132
Radek Krejcic071c542016-01-27 14:57:51 +0100133 if (!parent) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200134 /* handle situation when there is a top-level uses referencing a foreign grouping */
135 for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node));
136 mod = lys_node_module(node);
Michal Vasko1dca6882015-10-22 14:29:42 +0200137 }
138
Radek Krejcic071c542016-01-27 14:57:51 +0100139 /* try to find the node */
140 node = NULL;
141 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
142 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100143 /* module name comparison */
144 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200145 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 +0100146 continue;
147 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200148
Radek Krejcic071c542016-01-27 14:57:51 +0100149 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200150 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100151 if (ret) {
152 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200153 }
Radek Krejcic071c542016-01-27 14:57:51 +0100154 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200155 }
156 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200157 }
158
159 return EXIT_FAILURE;
160}
161
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200162int
Michal Vasko1e62a092015-12-01 12:27:20 +0100163lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type,
164 const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200165{
Michal Vasko1e62a092015-12-01 12:27:20 +0100166 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200167
168 assert(siblings && name);
169 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
170
171 /* find the beginning */
172 while (siblings->prev->next) {
173 siblings = siblings->prev;
174 }
175
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200176 if (!mod) {
177 mod = siblings->module;
178 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200179
Michal Vasko4f0dad02016-02-15 14:08:23 +0100180 /* try to find the node */
181 node = NULL;
Michal Vaskodcf98e62016-05-05 17:53:53 +0200182 while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100183 if (!type || (node->nodetype & type)) {
184 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200185 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100186 continue;
187 }
188
Michal Vasko4f0dad02016-02-15 14:08:23 +0100189 /* direct name check */
Radek Krejci749190d2016-02-18 16:26:25 +0100190 if (ly_strequal(node->name, name, 0)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100191 if (ret) {
192 *ret = node;
193 }
194 return EXIT_SUCCESS;
195 }
Radek Krejcic071c542016-01-27 14:57:51 +0100196 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200197 }
198
199 return EXIT_FAILURE;
200}
201
Michal Vasko1e62a092015-12-01 12:27:20 +0100202API const struct lys_node *
203lys_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 +0200204{
Michal Vasko1e62a092015-12-01 12:27:20 +0100205 const struct lys_node *next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200206
Radek Krejci8bc87f62015-09-02 16:19:05 +0200207 if (!last) {
208 /* first call */
209
210 /* get know where to start */
211 if (parent) {
212 /* schema subtree */
213 next = last = parent->child;
214 } else {
215 /* top level data */
216 assert(module);
217 next = last = module->data;
218 }
Radek Krejci972724f2016-08-12 15:24:40 +0200219 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
220 /* continue with uses content */
221 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200222 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200223 /* continue after the last returned value */
224 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200225 }
226
227repeat:
Michal Vasko7c386e72015-10-07 15:13:33 +0200228 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200229 if (options & LYS_GETNEXT_WITHGROUPING) {
230 return next;
231 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200232 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200233 }
234
Radek Krejci972724f2016-08-12 15:24:40 +0200235 if (!next) { /* cover case when parent is augment */
236 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200237 /* no next element */
238 return NULL;
239 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200240 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200241 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200242 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200243 } else {
244 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200245 }
246
247 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200248 case LYS_INPUT:
249 case LYS_OUTPUT:
250 if (options & LYS_GETNEXT_WITHINOUT) {
251 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200252 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200253 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200254 } else {
255 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200256 }
Radek Krejci972724f2016-08-12 15:24:40 +0200257 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200258
Michal Vaskoa5835e92015-10-20 15:07:39 +0200259 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200260 if (options & LYS_GETNEXT_WITHCASE) {
261 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200262 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200263 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200264 } else {
265 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200266 }
Radek Krejci972724f2016-08-12 15:24:40 +0200267 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200268
Michal Vasko1dca6882015-10-22 14:29:42 +0200269 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200270 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200271 if (options & LYS_GETNEXT_WITHUSES) {
272 return next;
273 } else if (next->child) {
274 next = next->child;
275 } else {
276 next = next->next;
277 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200278 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200279
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200280 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200281 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200282 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200283 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200284 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200285 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200286 case LYS_LIST:
287 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200288 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200289
Radek Krejci972724f2016-08-12 15:24:40 +0200290 case LYS_CONTAINER:
291 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
292 if (next->child) {
293 /* go into */
294 next = next->child;
295 } else {
296 next = next->next;
297 }
298 goto repeat;
299 } else {
300 return next;
301 }
302
Radek Krejci8bc87f62015-09-02 16:19:05 +0200303 case LYS_CHOICE:
304 if (options & LYS_GETNEXT_WITHCHOICE) {
305 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200306 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200307 /* go into */
308 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200309 } else {
310 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200311 }
Radek Krejci972724f2016-08-12 15:24:40 +0200312 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200313
Radek Krejci7f40ce32015-08-12 20:38:46 +0200314 default:
315 /* we should not be here */
316 return NULL;
317 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200318
319
320}
321
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200322void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200323lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200324{
Radek Krejci76512572015-08-04 09:47:08 +0200325 struct lys_node *parent, *first;
Radek Krejcic071c542016-01-27 14:57:51 +0100326 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200327
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200328 if (!node) {
329 return;
330 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200331
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200332 /* unlink from data model if necessary */
333 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100334 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100335 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100336 if (main_module->data == node) {
337 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200338 }
339 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200340
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200341 /* store pointers to important nodes */
342 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200343 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200344 /* handle augments - first, unlink it from the augment parent ... */
345 if (parent->child == node) {
346 parent->child = node->next;
347 }
348 /* and then continue with the target parent */
Radek Krejci76512572015-08-04 09:47:08 +0200349 parent = ((struct lys_node_augment *)parent)->target;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200350 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200351
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200352 /* unlink from parent */
353 if (parent) {
354 if (parent->child == node) {
355 parent->child = node->next;
356 }
357 node->parent = NULL;
358 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200359
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200360 /* unlink from siblings */
361 if (node->prev == node) {
362 /* there are no more siblings */
363 return;
364 }
365 if (node->next) {
366 node->next->prev = node->prev;
367 } else {
368 /* unlinking the last element */
369 if (parent) {
370 first = parent->child;
371 } else {
372 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200373 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200374 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200375 }
376 }
377 first->prev = node->prev;
378 }
379 if (node->prev->next) {
380 node->prev->next = node->next;
381 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200382
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200383 /* clean up the unlinked element */
384 node->next = NULL;
385 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200386}
387
Michal Vasko563ef092015-09-04 13:17:23 +0200388struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100389lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200390{
391 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200392
393 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200394 /* top-level augment, look into module (uses augment is handled correctly below) */
395 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
396 par_iter = par_iter->parent->module->data;
397 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200398 break;
399 }
400 }
401
Michal Vasko6f929da2015-10-02 16:23:25 +0200402 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200403 continue;
404 }
405
406 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
407 if (!stop) {
408 stop = par_iter;
409 } else if (iter == stop) {
410 break;
411 }
412 if (iter->nodetype != LYS_GROUPING) {
413 continue;
414 }
415
Radek Krejcif8426a72015-10-31 23:14:03 +0100416 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200417 return (struct lys_node_grp *)iter;
418 }
419 }
420 }
421
Michal Vasko563ef092015-09-04 13:17:23 +0200422 return NULL;
423}
424
Radek Krejci10c760e2015-08-14 14:45:43 +0200425/*
426 * get next grouping in the root's subtree, in the
427 * first call, tha last is NULL
428 */
429static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200430lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200431{
Radek Krejci10c760e2015-08-14 14:45:43 +0200432 struct lys_node *last = (struct lys_node *)lastgrp;
433 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200434
Radek Krejci10c760e2015-08-14 14:45:43 +0200435 assert(root);
436
437 if (!last) {
438 last = root;
439 }
440
441 while (1) {
442 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
443 next = last->child;
444 } else {
445 next = NULL;
446 }
447 if (!next) {
448 if (last == root) {
449 /* we are done */
450 return NULL;
451 }
452
453 /* no children, go to siblings */
454 next = last->next;
455 }
456 while (!next) {
457 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100458 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200459 /* we are done */
460 return NULL;
461 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200462 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100463 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200464 }
465
466 if (next->nodetype == LYS_GROUPING) {
467 return (struct lys_node_grp *)next;
468 }
469
470 last = next;
471 }
472}
473
Michal Vasko0d343d12015-08-24 14:57:36 +0200474/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200475int
Radek Krejci07911992015-08-14 15:13:31 +0200476lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
477{
Michal Vasko563ef092015-09-04 13:17:23 +0200478 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200479 struct lys_node_grp *grp;
480 int down;
481
482 assert(node);
483
484 if (!parent) {
485 assert(module);
486 } else {
487 module = parent->module;
488 }
489
490 switch (node->nodetype) {
491 case LYS_GROUPING:
492 /* 6.2.1, rule 6 */
493 if (parent) {
494 if (parent->child) {
495 down = 1;
496 start = parent->child;
497 } else {
498 down = 0;
499 start = parent;
500 }
501 } else {
502 down = 1;
503 start = module->data;
504 }
505 /* go up */
Radek Krejcic071c542016-01-27 14:57:51 +0100506 if (lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100507 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200508 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200509 }
510 /* go down, because grouping can be defined after e.g. container in which is collision */
511 if (down) {
512 for (iter = start, stop = NULL; iter; iter = iter->prev) {
513 if (!stop) {
514 stop = start;
515 } else if (iter == stop) {
516 break;
517 }
518 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
519 continue;
520 }
521
522 grp = NULL;
523 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100524 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100525 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200526 return EXIT_FAILURE;
527 }
528 }
529 }
530 }
531 break;
532 case LYS_LEAF:
533 case LYS_LEAFLIST:
534 case LYS_LIST:
535 case LYS_CONTAINER:
536 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200537 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200538 /* 6.2.1, rule 7 */
539 if (parent) {
540 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200541 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
542 if (iter->nodetype == LYS_AUGMENT) {
543 if (((struct lys_node_augment *)iter)->target) {
544 /* augment is resolved, go up */
545 iter = ((struct lys_node_augment *)iter)->target;
546 continue;
547 }
548 /* augment is not resolved, this is the final parent */
549 break;
550 }
Radek Krejci07911992015-08-14 15:13:31 +0200551 iter = iter->parent;
552 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200553
Radek Krejci07911992015-08-14 15:13:31 +0200554 if (!iter) {
555 stop = NULL;
556 iter = module->data;
557 } else {
558 stop = iter;
559 iter = iter->child;
560 }
561 } else {
562 stop = NULL;
563 iter = module->data;
564 }
565 while (iter) {
566 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
567 iter = iter->child;
568 continue;
569 }
570
Radek Krejcibf2abff2016-08-23 15:51:52 +0200571 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100572 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100573 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200574 return EXIT_FAILURE;
575 }
576 }
577
578 /* special case for choice - we must check the choice's name as
579 * well as the names of nodes under the choice
580 */
581 if (iter->nodetype == LYS_CHOICE) {
582 iter = iter->child;
583 continue;
584 }
585
586 /* go to siblings */
587 if (!iter->next) {
588 /* no sibling, go to parent's sibling */
589 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200590 /* for parent LYS_AUGMENT */
591 if (iter->parent == stop) {
592 iter = stop;
593 break;
594 }
595 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200596 if (iter && iter->next) {
597 break;
598 }
599 } while (iter != stop);
600
601 if (iter == stop) {
602 break;
603 }
604 }
605 iter = iter->next;
606 }
607 break;
608 case LYS_CASE:
609 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100610 if (parent) {
611 start = parent->child;
612 } else {
613 start = module->data;
614 }
615
616 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200617 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200618 continue;
619 }
620
Radek Krejci749190d2016-02-18 16:26:25 +0100621 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100622 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200623 return EXIT_FAILURE;
624 }
625 }
626 break;
627 default:
628 /* no check needed */
629 break;
630 }
631
632 return EXIT_SUCCESS;
633}
634
Michal Vasko0d343d12015-08-24 14:57:36 +0200635/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200636int
Radek Krejci10c760e2015-08-14 14:45:43 +0200637lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
638{
Radek Krejci92720552015-10-05 15:28:27 +0200639 struct lys_node *iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200640 struct lys_node_inout *in, *out, *inout;
Radek Krejci07911992015-08-14 15:13:31 +0200641 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200642
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200643 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200644
Radek Krejci10c760e2015-08-14 14:45:43 +0200645 if (parent) {
646 type = parent->nodetype;
647 module = parent->module;
648 } else {
649 assert(module);
650 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200651 }
652
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200653 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200654 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200655 case LYS_CONTAINER:
656 case LYS_LIST:
657 case LYS_GROUPING:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200658 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200659 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200660 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200661 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
662 return EXIT_FAILURE;
663 }
664 break;
Radek Krejci76512572015-08-04 09:47:08 +0200665 case LYS_USES:
666 case LYS_INPUT:
667 case LYS_OUTPUT:
668 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200669 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200670 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200671 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100672 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200673 return EXIT_FAILURE;
674 }
675 break;
Radek Krejci76512572015-08-04 09:47:08 +0200676 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200677 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200678 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100679 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200680 return EXIT_FAILURE;
681 }
682 break;
Radek Krejci76512572015-08-04 09:47:08 +0200683 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200684 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200685 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100686 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200687 return EXIT_FAILURE;
688 }
689 break;
Radek Krejci76512572015-08-04 09:47:08 +0200690 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200691 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200692 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100693 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200694 return EXIT_FAILURE;
695 }
696 break;
Radek Krejci76512572015-08-04 09:47:08 +0200697 case LYS_LEAF:
698 case LYS_LEAFLIST:
699 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200700 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100701 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
702 LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100703 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200704 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200705 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200706 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200707 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200708 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100709 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200710 return EXIT_FAILURE;
711 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200712 break;
713 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200714 /* top level */
715 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200716 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200717 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100718 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200719 return EXIT_FAILURE;
720 }
721
Radek Krejcic071c542016-01-27 14:57:51 +0100722 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200723 }
724
725 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200726 if (lys_check_id(child, parent, module)) {
727 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200728 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200729
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200730 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200731 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200732 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200733
Radek Krejci41a349b2016-10-24 19:21:59 +0200734 if (child->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
735 /* replace the implicit input/output node */
736 if (child->nodetype == LYS_OUTPUT) {
737 inout = (struct lys_node_inout *)parent->child->next;
738 } else { /* LYS_INPUT */
739 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200740 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200741 }
742 if (inout->next) {
743 child->next = inout->next;
744 inout->next->prev = child;
745 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200746 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200747 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200748 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200749 child->prev = inout->prev;
750 if (inout->prev->next) {
751 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200752 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200753 inout->prev = (struct lys_node *)inout;
754 child->parent = parent;
755 inout->parent = NULL;
756 lys_node_free((struct lys_node *)inout, NULL, 0);
757 } else {
758 /* connect the child correctly */
759 if (!parent) {
760 if (module->data) {
761 module->data->prev->next = child;
762 child->prev = module->data->prev;
763 module->data->prev = child;
764 } else {
765 module->data = child;
766 }
767 } else {
768 if (!parent->child) {
769 /* the only/first child of the parent */
770 parent->child = child;
771 child->parent = parent;
772 iter = child;
773 } else {
774 /* add a new child at the end of parent's child list */
775 iter = parent->child->prev;
776 iter->next = child;
777 child->prev = iter;
778 }
779 while (iter->next) {
780 iter = iter->next;
781 iter->parent = parent;
782 }
783 parent->child->prev = iter;
784 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200785 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200786
Michal Vaskoe022a562016-09-27 14:24:15 +0200787 /* check config value (but ignore them in groupings and augments) */
788 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent);
789 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200790 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
791 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
792 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
793 LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children.");
794 return EXIT_FAILURE;
795 }
796 }
797
Radek Krejci41771502016-04-14 17:52:32 +0200798 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200799 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200800 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200801 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200802 /* store it only into container or list - the only data inner nodes */
803 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
804 if (iter->flags & LYS_INCL_STATUS) {
805 /* done, someone else set it already from here */
806 break;
807 }
808 /* set flag about including status data */
809 iter->flags |= LYS_INCL_STATUS;
810 }
811 }
812 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200813
814 /* create implicit input/output nodes to have available them as possible target for augment */
815 if (child->nodetype & (LYS_RPC | LYS_ACTION)) {
816 in = calloc(1, sizeof *in);
817 in->nodetype = LYS_INPUT;
818 in->name = lydict_insert(child->module->ctx, "input", 5);
819 out = calloc(1, sizeof *out);
820 out->name = lydict_insert(child->module->ctx, "output", 6);
821 out->nodetype = LYS_OUTPUT;
822 in->module = out->module = child->module;
823 in->parent = out->parent = child;
824 in->flags = out->flags = LYS_IMPLICIT;
825 in->next = (struct lys_node *)out;
826 in->prev = (struct lys_node *)out;
827 out->prev = (struct lys_node *)in;
828 child->child = (struct lys_node *)in;
829 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200830 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200831}
832
Radek Krejcia1df1682016-04-11 14:56:59 +0200833static const struct lys_module *
834lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200835{
Radek Krejcia1df1682016-04-11 14:56:59 +0200836 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200837 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200838 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200839
Radek Krejci2467a492016-10-24 15:16:59 +0200840 ly_err_clean();
Radek Krejcif347abc2016-06-22 10:18:47 +0200841
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200842 if (!ctx || !data) {
843 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
844 return NULL;
845 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200846
Radek Krejcia1df1682016-04-11 14:56:59 +0200847 if (!internal && format == LYS_IN_YANG) {
848 /* enlarge data by 2 bytes for flex */
849 len = strlen(data);
850 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
851 if (!enlarged_data) {
852 LOGMEM;
853 return NULL;
854 }
855 memcpy(enlarged_data, data, len);
856 enlarged_data[len] = enlarged_data[len + 1] = '\0';
857 data = enlarged_data;
858 }
859
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200860 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200861 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +0100862 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200863 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200864 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100865 mod = yang_read_module(ctx, data, 0, NULL, 1);
866 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200867 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200868 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200869 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200870 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200871
Radek Krejcia1df1682016-04-11 14:56:59 +0200872 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200873 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200874}
875
Radek Krejcia1df1682016-04-11 14:56:59 +0200876API const struct lys_module *
877lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
878{
879 return lys_parse_mem_(ctx, data, format, 0);
880}
881
Michal Vasko5a721fd2016-02-16 12:16:48 +0100882struct lys_submodule *
883lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200884{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100885 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200886
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200887 assert(module);
888 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +0200889
Radek Krejcic071c542016-01-27 14:57:51 +0100890 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +0200891 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200892
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200893 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200894 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +0100895 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200896 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200897 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100898 submod = yang_read_submodule(module, data, 0, unres);
899 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200900 default:
Radek Krejci90a550a2016-04-13 16:00:58 +0200901 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200902 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200903 }
Radek Krejciefaeba32015-05-27 14:30:57 +0200904
Michal Vasko5a721fd2016-02-16 12:16:48 +0100905 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +0200906}
907
Michal Vasko1e62a092015-12-01 12:27:20 +0100908API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +0100909lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
910{
911 int fd;
912 const struct lys_module *ret;
913
914 if (!ctx || !path) {
915 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
916 return NULL;
917 }
918
919 fd = open(path, O_RDONLY);
920 if (fd == -1) {
921 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
922 return NULL;
923 }
924
925 ret = lys_parse_fd(ctx, fd, format);
926 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100927
Radek Krejcia77904e2016-02-25 16:23:45 +0100928 if (ret && !ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100929 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100930 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100931 }
932
Michal Vasko662610a2015-12-07 11:25:45 +0100933 return ret;
934}
935
936API const struct lys_module *
937lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +0200938{
Michal Vasko1e62a092015-12-01 12:27:20 +0100939 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +0200940 struct stat sb;
941 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +0100942 char buf[PATH_MAX];
943 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +0200944
945 if (!ctx || fd < 0) {
946 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
947 return NULL;
948 }
949
Radek Krejci10a833c2015-12-16 15:28:37 +0100950 if (fstat(fd, &sb) == -1) {
951 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
952 return NULL;
953 }
Radek Krejcib051f722016-02-25 15:12:21 +0100954 if (!S_ISREG(sb.st_mode)) {
955 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
956 return NULL;
957 }
958
Michal Vasko164d9012016-04-01 10:16:59 +0200959 if (!sb.st_size) {
960 LOGERR(LY_EINVAL, "File empty.");
961 return NULL;
962 }
963
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100964 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100965 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +0100966 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +0100967 return NULL;
968 }
Radek Krejcia1df1682016-04-11 14:56:59 +0200969 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100970 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200971
Radek Krejcia77904e2016-02-25 16:23:45 +0100972 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +0100973 /* get URI if there is /proc */
974 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +0100975 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
976 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
977 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
978 }
979 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +0100980 }
Radek Krejcib051f722016-02-25 15:12:21 +0100981 }
982
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200983 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200984}
985
Michal Vasko5a721fd2016-02-16 12:16:48 +0100986struct lys_submodule *
987lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200988{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100989 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200990 struct stat sb;
991 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +0200992
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200993 assert(module);
994 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +0200995
Radek Krejci10a833c2015-12-16 15:28:37 +0100996 if (fstat(fd, &sb) == -1) {
997 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +0100998 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +0100999 }
Michal Vasko164d9012016-04-01 10:16:59 +02001000
1001 if (!sb.st_size) {
1002 LOGERR(LY_EINVAL, "File empty.");
1003 return NULL;
1004 }
1005
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001006 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001007 if (addr == MAP_FAILED) {
1008 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001009 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001010 }
Michal Vasko5a721fd2016-02-16 12:16:48 +01001011 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001012 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +02001013
Michal Vasko5a721fd2016-02-16 12:16:48 +01001014 return submodule;
1015
Radek Krejciefaeba32015-05-27 14:30:57 +02001016}
1017
Radek Krejci1d82ef62015-08-07 14:44:40 +02001018static struct lys_restr *
1019lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001020{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001021 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001022 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001023
Radek Krejci3733a802015-06-19 13:43:21 +02001024 if (!size) {
1025 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001026 }
Radek Krejci3733a802015-06-19 13:43:21 +02001027
1028 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001029 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001030 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001031 return NULL;
1032 }
Radek Krejci3733a802015-06-19 13:43:21 +02001033 for (i = 0; i < size; i++) {
1034 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1035 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1036 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1037 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1038 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1039 }
1040
1041 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001042}
1043
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001044void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001045lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001046{
1047 assert(ctx);
1048 if (!restr) {
1049 return;
1050 }
1051
1052 lydict_remove(ctx, restr->expr);
1053 lydict_remove(ctx, restr->dsc);
1054 lydict_remove(ctx, restr->ref);
1055 lydict_remove(ctx, restr->eapptag);
1056 lydict_remove(ctx, restr->emsg);
1057}
1058
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001059static void
1060lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
1061{
1062 uint8_t i;
1063
1064 for (i = 0; i < iffeature_size; ++i) {
1065 free(iffeature[i].expr);
1066 free(iffeature[i].features);
1067 }
1068 free(iffeature);
1069}
1070
Michal Vaskob84f88a2015-09-24 13:16:10 +02001071static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001072type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001073 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001074{
1075 int i;
1076
1077 switch (base) {
1078 case LY_TYPE_BINARY:
1079 if (old->info.binary.length) {
1080 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1081 }
1082 break;
1083
1084 case LY_TYPE_BITS:
1085 new->info.bits.count = old->info.bits.count;
1086 if (new->info.bits.count) {
1087 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1088 if (!new->info.bits.bit) {
1089 LOGMEM;
1090 return -1;
1091 }
1092 for (i = 0; i < new->info.bits.count; i++) {
1093 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1094 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1095 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1096 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1097 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1098 }
1099 }
1100 break;
1101
1102 case LY_TYPE_DEC64:
1103 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001104 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001105 if (old->info.dec64.range) {
1106 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1107 }
1108 break;
1109
1110 case LY_TYPE_ENUM:
1111 new->info.enums.count = old->info.enums.count;
1112 if (new->info.enums.count) {
1113 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1114 if (!new->info.enums.enm) {
1115 LOGMEM;
1116 return -1;
1117 }
1118 for (i = 0; i < new->info.enums.count; i++) {
1119 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1120 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1121 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1122 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1123 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1124 }
1125 }
1126 break;
1127
1128 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001129 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001130 if (old->info.ident.count) {
1131 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1132 if (!new->info.ident.ref) {
1133 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001134 return -1;
1135 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001136 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1137 } else {
1138 /* there can be several unresolved base identities, duplicate them all */
1139 i = -1;
1140 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1141 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1142 return -1;
1143 }
1144 --i;
1145 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001146 }
1147 break;
1148
1149 case LY_TYPE_INST:
1150 new->info.inst.req = old->info.inst.req;
1151 break;
1152
1153 case LY_TYPE_INT8:
1154 case LY_TYPE_INT16:
1155 case LY_TYPE_INT32:
1156 case LY_TYPE_INT64:
1157 case LY_TYPE_UINT8:
1158 case LY_TYPE_UINT16:
1159 case LY_TYPE_UINT32:
1160 case LY_TYPE_UINT64:
1161 if (old->info.num.range) {
1162 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1163 }
1164 break;
1165
1166 case LY_TYPE_LEAFREF:
1167 if (old->info.lref.path) {
1168 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001169 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001170 return -1;
1171 }
1172 }
1173 break;
1174
1175 case LY_TYPE_STRING:
1176 if (old->info.str.length) {
1177 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1178 }
1179 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1180 new->info.str.pat_count = old->info.str.pat_count;
1181 break;
1182
1183 case LY_TYPE_UNION:
1184 new->info.uni.count = old->info.uni.count;
1185 if (new->info.uni.count) {
1186 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1187 if (!new->info.uni.types) {
1188 LOGMEM;
1189 return -1;
1190 }
1191 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001192 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 +02001193 return -1;
1194 }
1195 }
1196 }
1197 break;
1198
1199 default:
1200 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1201 break;
1202 }
1203 return EXIT_SUCCESS;
1204}
1205
1206struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001207lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1208 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001209{
1210 struct yang_type *new;
1211
1212 new = calloc(1, sizeof *new);
1213 if (!new) {
1214 LOGMEM;
1215 return NULL;
1216 }
1217 new->flags = old->flags;
1218 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001219 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001220 new->type = type;
1221 if (!new->name) {
1222 LOGMEM;
1223 goto error;
1224 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001225 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001226 new->type->base = new->base;
1227 lys_type_free(module->ctx, new->type);
1228 memset(&new->type->info, 0, sizeof new->type->info);
1229 goto error;
1230 }
1231 return new;
1232
1233 error:
1234 free(new);
1235 return NULL;
1236}
1237
1238static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001239lys_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 +02001240 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001241{
1242 int i;
1243
Michal Vasko1dca6882015-10-22 14:29:42 +02001244 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001245 new->base = old->base;
1246 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001247 new->parent = (struct lys_tpdf *)parent;
Radek Krejci3733a802015-06-19 13:43:21 +02001248
Michal Vasko878e38d2016-09-05 12:17:53 +02001249 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001250 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001251 /* HACK (serious one) for unres */
1252 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001253 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001254 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 +02001255 } else {
1256 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1257 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001258 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001259 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001260 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001261 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001262 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001263 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001264 }
1265
Radek Krejci3a5501d2016-07-18 22:03:34 +02001266 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001267}
1268
1269void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001270lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001271{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001272 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001273
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001274 assert(ctx);
1275 if (!type) {
1276 return;
1277 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001278
Michal Vasko1dca6882015-10-22 14:29:42 +02001279 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001280
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001281 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001282 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001283 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001284 free(type->info.binary.length);
1285 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001286 case LY_TYPE_BITS:
1287 for (i = 0; i < type->info.bits.count; i++) {
1288 lydict_remove(ctx, type->info.bits.bit[i].name);
1289 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1290 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001291 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001292 }
1293 free(type->info.bits.bit);
1294 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001295
1296 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001297 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001298 free(type->info.dec64.range);
1299 break;
1300
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001301 case LY_TYPE_ENUM:
1302 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001303 lydict_remove(ctx, type->info.enums.enm[i].name);
1304 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1305 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001306 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001307 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001308 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001309 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001310
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001311 case LY_TYPE_INT8:
1312 case LY_TYPE_INT16:
1313 case LY_TYPE_INT32:
1314 case LY_TYPE_INT64:
1315 case LY_TYPE_UINT8:
1316 case LY_TYPE_UINT16:
1317 case LY_TYPE_UINT32:
1318 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001319 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001320 free(type->info.num.range);
1321 break;
1322
Radek Krejcidc4c1412015-06-19 15:39:54 +02001323 case LY_TYPE_LEAFREF:
1324 lydict_remove(ctx, type->info.lref.path);
1325 break;
1326
Radek Krejci3733a802015-06-19 13:43:21 +02001327 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001328 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001329 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001330 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001331 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001332 }
1333 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001334 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001335
Radek Krejcie4c366b2015-07-02 10:11:31 +02001336 case LY_TYPE_UNION:
1337 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001338 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001339 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001340 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001341 break;
1342
Michal Vaskod3282192016-09-05 11:27:57 +02001343 case LY_TYPE_IDENT:
1344 free(type->info.ident.ref);
1345 break;
1346
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001347 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001348 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001349 break;
1350 }
Radek Krejci5a065542015-05-22 15:02:07 +02001351}
1352
Radek Krejci1d82ef62015-08-07 14:44:40 +02001353static void
1354lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001355{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001356 assert(ctx);
1357 if (!tpdf) {
1358 return;
1359 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001360
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001361 lydict_remove(ctx, tpdf->name);
1362 lydict_remove(ctx, tpdf->dsc);
1363 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001364
Radek Krejci1d82ef62015-08-07 14:44:40 +02001365 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001366
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001367 lydict_remove(ctx, tpdf->units);
1368 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001369}
1370
Michal Vaskob84f88a2015-09-24 13:16:10 +02001371static struct lys_tpdf *
1372lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1373{
1374 struct lys_tpdf *result;
1375 int i, j;
1376
1377 if (!size) {
1378 return NULL;
1379 }
1380
1381 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001382 if (!result) {
1383 LOGMEM;
1384 return NULL;
1385 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001386 for (i = 0; i < size; i++) {
1387 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1388 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1389 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1390 result[i].flags = old[i].flags;
1391 result[i].module = old[i].module;
1392
Radek Krejci3a5501d2016-07-18 22:03:34 +02001393 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001394 for (j = 0; j <= i; ++j) {
1395 lys_tpdf_free(mod->ctx, &result[j]);
1396 }
1397 free(result);
1398 return NULL;
1399 }
1400
1401 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1402 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1403 }
1404
1405 return result;
1406}
1407
Radek Krejci1d82ef62015-08-07 14:44:40 +02001408static struct lys_when *
1409lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001410{
Radek Krejci76512572015-08-04 09:47:08 +02001411 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001412
1413 if (!old) {
1414 return NULL;
1415 }
1416
1417 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001418 if (!new) {
1419 LOGMEM;
1420 return NULL;
1421 }
Radek Krejci00768f42015-06-18 17:04:04 +02001422 new->cond = lydict_insert(ctx, old->cond, 0);
1423 new->dsc = lydict_insert(ctx, old->dsc, 0);
1424 new->ref = lydict_insert(ctx, old->ref, 0);
1425
1426 return new;
1427}
1428
Michal Vasko0308dd62015-10-07 09:14:40 +02001429void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001430lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001431{
1432 if (!w) {
1433 return;
1434 }
1435
1436 lydict_remove(ctx, w->cond);
1437 lydict_remove(ctx, w->dsc);
1438 lydict_remove(ctx, w->ref);
1439
1440 free(w);
1441}
1442
Radek Krejcib7f5e412015-08-13 10:15:51 +02001443static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001444lys_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 +02001445{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001446 struct lys_node *next, *sub;
1447
Radek Krejcic071c542016-01-27 14:57:51 +01001448 /* children from a resolved augment are freed under the target node */
Michal Vaskod2fbade2016-05-02 17:14:00 +02001449 if (!aug->target) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001450 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001451 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001452 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001453 }
1454
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001455 lydict_remove(ctx, aug->target_name);
1456 lydict_remove(ctx, aug->dsc);
1457 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001458
Radek Krejci9ff0a922016-07-14 13:08:05 +02001459 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001460
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001461 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001462}
1463
Radek Krejci76512572015-08-04 09:47:08 +02001464static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001465lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001466{
Radek Krejci76512572015-08-04 09:47:08 +02001467 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001468 struct lys_node *old_child, *new_child;
1469 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001470
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001471 if (!size) {
1472 return NULL;
1473 }
Radek Krejci106efc02015-06-10 14:36:27 +02001474
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001475 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001476 if (!new) {
1477 LOGMEM;
1478 return NULL;
1479 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001480 for (i = 0; i < size; i++) {
1481 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1482 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1483 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1484 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001485 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001486 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001487
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001488 /* this must succeed, it was already resolved once */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001489 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL,
1490 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001491 LOGINT;
1492 free(new);
1493 return NULL;
1494 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001495 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001496
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001497 /* Correct the augment nodes.
1498 * This function can only be called from lys_node_dup() with uses
1499 * being the node duplicated, so we must have a case of grouping
1500 * with a uses with augments. The augmented nodes have already been
1501 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001502 * (it was set to their actual data parent, not an augment), and
1503 * the new augment does not have child pointer to its augment nodes,
1504 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001505 */
1506 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001507 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001508 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001509 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001510 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001511 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001512 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001513 LY_TREE_FOR(old[i].child, old_child) {
1514 /* all augment nodes were connected as siblings, there can be no more after this */
1515 if (old_child->parent != (struct lys_node *)&old[i]) {
1516 break;
1517 }
1518
Radek Krejci749190d2016-02-18 16:26:25 +01001519 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001520
1521 new_child->parent = (struct lys_node *)&new[i];
1522 new_child = new_child->next;
1523 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001524 }
Radek Krejci106efc02015-06-10 14:36:27 +02001525
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001526 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001527}
1528
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001529static const char **
1530lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1531{
1532 int i;
1533 const char **result;
1534
1535 if (!size) {
1536 return NULL;
1537 }
1538
1539 result = calloc(size, sizeof *result);
1540 if (!result) {
1541 LOGMEM;
1542 return NULL;
1543 }
1544
1545 for (i = 0; i < size; i++) {
1546 result[i] = lydict_insert(ctx, old[i], 0);
1547 }
1548 return result;
1549}
1550
Radek Krejci76512572015-08-04 09:47:08 +02001551static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001552lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001553{
Radek Krejci76512572015-08-04 09:47:08 +02001554 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001555 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001556
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001557 if (!size) {
1558 return NULL;
1559 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001560
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001561 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001562 if (!result) {
1563 LOGMEM;
1564 return NULL;
1565 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001566 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001567 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001568 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1569 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001570 result[i].flags = old[i].flags;
1571 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001572
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001573 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001574 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001575
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001576 result[i].dflt_size = old[i].dflt_size;
1577 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
1578
1579 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001580 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001581 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001582 result[i].mod.list = old[i].mod.list;
1583 }
1584 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001585
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001586 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001587}
1588
Radek Krejci1d82ef62015-08-07 14:44:40 +02001589static void
1590lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001591{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001592 assert(ctx);
1593 if (!ident) {
1594 return;
1595 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001596
Radek Krejci018f1f52016-08-03 16:01:20 +02001597 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02001598 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001599 lydict_remove(ctx, ident->name);
1600 lydict_remove(ctx, ident->dsc);
1601 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001602 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejci6793db02015-05-22 17:49:54 +02001603
1604}
1605
Radek Krejci1d82ef62015-08-07 14:44:40 +02001606static void
1607lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001608{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001609 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001610
Radek Krejcid12f57b2015-08-06 10:43:39 +02001611 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001612 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001613 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001614 }
1615 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001616}
1617
Radek Krejci1d82ef62015-08-07 14:44:40 +02001618static void
Michal Vasko44fb6382016-06-29 11:12:27 +02001619lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02001620{
1621 int i;
1622
1623 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1624 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001625 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001626 }
1627 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02001628
1629 for (i = 0; i < io->must_size; i++) {
1630 lys_restr_free(ctx, &io->must[i]);
1631 }
1632 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001633}
1634
Radek Krejci1d82ef62015-08-07 14:44:40 +02001635static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02001636lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
1637{
1638 int i;
1639
1640 for (i = 0; i < notif->must_size; i++) {
1641 lys_restr_free(ctx, &notif->must[i]);
1642 }
1643 free(notif->must);
1644
1645 for (i = 0; i < notif->tpdf_size; i++) {
1646 lys_tpdf_free(ctx, &notif->tpdf[i]);
1647 }
1648 free(notif->tpdf);
1649}
1650static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02001651lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001652{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001653 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001654
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001655 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001656 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001657 }
1658 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001659
Radek Krejci1d82ef62015-08-07 14:44:40 +02001660 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001661}
1662
Radek Krejci1d82ef62015-08-07 14:44:40 +02001663static void
1664lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001665{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001666 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001667
Radek Krejci85a54be2016-10-20 12:39:56 +02001668 /* leafref backlinks */
1669 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001670
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001671 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001672 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001673 }
1674 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001675
Radek Krejci1d82ef62015-08-07 14:44:40 +02001676 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001677
Radek Krejci1d82ef62015-08-07 14:44:40 +02001678 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001679 lydict_remove(ctx, leaf->units);
1680 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001681}
1682
Radek Krejci1d82ef62015-08-07 14:44:40 +02001683static void
1684lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001685{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001686 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001687
Radek Krejci46c4cd72016-01-21 15:13:52 +01001688 if (llist->child) {
1689 /* leafref backlinks */
1690 ly_set_free((struct ly_set *)llist->child);
1691 }
1692
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001693 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001694 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001695 }
1696 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001697
Pavol Vican38321d02016-08-16 14:56:02 +02001698 for (i = 0; i < llist->dflt_size; i++) {
1699 lydict_remove(ctx, llist->dflt[i]);
1700 }
1701 free(llist->dflt);
1702
Radek Krejci1d82ef62015-08-07 14:44:40 +02001703 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001704
Radek Krejci1d82ef62015-08-07 14:44:40 +02001705 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001706 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001707}
1708
Radek Krejci1d82ef62015-08-07 14:44:40 +02001709static void
1710lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001711{
Radek Krejci581ce772015-11-10 17:22:40 +01001712 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001713
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001714 /* handle only specific parts for LY_NODE_LIST */
1715 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001716 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001717 }
1718 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001719
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001720 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001721 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001722 }
1723 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001724
Radek Krejci1d82ef62015-08-07 14:44:40 +02001725 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001726
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001727 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02001728 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001729 lydict_remove(ctx, list->unique[i].expr[j]);
1730 }
1731 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001732 }
1733 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001734
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001735 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001736}
1737
Radek Krejci1d82ef62015-08-07 14:44:40 +02001738static void
1739lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001740{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001741 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001742
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001743 /* handle only specific parts for LY_NODE_CONTAINER */
1744 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001746 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001747 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001748 }
1749 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001750
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001751 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001752 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001753 }
1754 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001755
Radek Krejci1d82ef62015-08-07 14:44:40 +02001756 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001757}
1758
Radek Krejci1d82ef62015-08-07 14:44:40 +02001759static void
1760lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001761{
1762 lydict_remove(ctx, f->name);
1763 lydict_remove(ctx, f->dsc);
1764 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001765 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci9de2c042016-10-19 16:53:06 +02001766 ly_set_free(f->depfeatures);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001767}
1768
Radek Krejci1d82ef62015-08-07 14:44:40 +02001769static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001770lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001771{
Radek Krejci581ce772015-11-10 17:22:40 +01001772 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001773 struct ly_ctx *ctx;
1774 struct lys_node *next, *elem;
1775
1776 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001777
1778 lydict_remove(ctx, dev->target_name);
1779 lydict_remove(ctx, dev->dsc);
1780 lydict_remove(ctx, dev->ref);
1781
Pavol Vican64d0b762016-08-25 10:44:59 +02001782 if (!dev->deviate) {
1783 return ;
1784 }
1785
Michal Vaskoff006c12016-02-17 11:15:19 +01001786 /* the module was freed, but we only need the context from orig_node, use ours */
1787 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1788 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1789 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1790 elem->module = module;
1791
1792 LY_TREE_DFS_END(dev->orig_node, next, elem);
1793 }
1794 lys_node_free(dev->orig_node, NULL, 0);
1795 } else {
1796 /* it's just a shallow copy, freeing one node */
1797 dev->orig_node->module = module;
1798 lys_node_free(dev->orig_node, NULL, 1);
1799 }
1800
Radek Krejcieb00f512015-07-01 16:44:58 +02001801 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001802 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02001803 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02001804 }
1805 free(dev->deviate[i].dflt);
1806
Radek Krejcieb00f512015-07-01 16:44:58 +02001807 lydict_remove(ctx, dev->deviate[i].units);
1808
1809 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1810 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001811 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001812 }
1813 free(dev->deviate[i].must);
1814
1815 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001816 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1817 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1818 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001819 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001820 }
1821 free(dev->deviate[i].unique);
1822 }
1823 }
1824 free(dev->deviate);
1825}
1826
Radek Krejci1d82ef62015-08-07 14:44:40 +02001827static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001828lys_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 +02001829{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001830 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001831
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001832 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001833 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001834 lydict_remove(ctx, uses->refine[i].dsc);
1835 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001836
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001837 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001838 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001839 }
1840 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001841
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001842 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02001843 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001844 }
1845 free(uses->refine[i].dflt);
1846
1847 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001848 lydict_remove(ctx, uses->refine[i].mod.presence);
1849 }
1850 }
1851 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001852
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001853 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001854 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001855 }
1856 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001857
Radek Krejci1d82ef62015-08-07 14:44:40 +02001858 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001859}
1860
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001861void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001862lys_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 +02001863{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001864 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001865 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001866
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001867 if (!node) {
1868 return;
1869 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001870
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001871 assert(node->module);
1872 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001873
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001874 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001875
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001876 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001877 if (node->priv && private_destructor) {
1878 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001879 }
1880
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001881 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001882 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001883 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001884 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001885 lydict_remove(ctx, node->dsc);
1886 lydict_remove(ctx, node->ref);
1887 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001888
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001889 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001890 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001891 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001892 }
1893 }
1894
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001895 /* specific part */
1896 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001897 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001898 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001899 break;
Radek Krejci76512572015-08-04 09:47:08 +02001900 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001901 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001902 break;
Radek Krejci76512572015-08-04 09:47:08 +02001903 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001904 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001905 break;
Radek Krejci76512572015-08-04 09:47:08 +02001906 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001907 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001908 break;
Radek Krejci76512572015-08-04 09:47:08 +02001909 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001910 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001911 break;
Radek Krejci76512572015-08-04 09:47:08 +02001912 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001913 case LYS_ANYDATA:
1914 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001915 break;
Radek Krejci76512572015-08-04 09:47:08 +02001916 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001917 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001918 break;
Radek Krejci76512572015-08-04 09:47:08 +02001919 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001920 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001921 break;
Radek Krejci76512572015-08-04 09:47:08 +02001922 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001923 /* do nothing */
1924 break;
Radek Krejci76512572015-08-04 09:47:08 +02001925 case LYS_GROUPING:
1926 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001927 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001928 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001929 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02001930 case LYS_NOTIF:
1931 lys_notif_free(ctx, (struct lys_node_notif *)node);
1932 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001933 case LYS_INPUT:
1934 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001935 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001936 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001937 case LYS_UNKNOWN:
1938 LOGINT;
1939 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001940 }
Radek Krejci5a065542015-05-22 15:02:07 +02001941
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001942 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001943 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001944 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001945}
1946
Michal Vasko1e62a092015-12-01 12:27:20 +01001947const struct lys_module *
Radek Krejci0fa54e92016-09-14 14:01:05 +02001948lys_get_implemented_module(const struct lys_module *mod)
1949{
1950 struct ly_ctx *ctx;
1951 int i;
1952
1953 if (!mod || mod->implemented) {
1954 /* invalid argument or the module itself is implemented */
1955 return mod;
1956 }
1957
1958 ctx = mod->ctx;
1959 for (i = 0; i < ctx->models.used; i++) {
1960 if (!ctx->models.list[i]->implemented) {
1961 continue;
1962 }
1963
1964 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
1965 /* we have some revision of the module implemented */
1966 return ctx->models.list[i];
1967 }
1968 }
1969
1970 /* we have no revision of the module implemented, return the module itself,
1971 * it is up to the caller to set the module implemented when needed */
1972 return mod;
1973}
1974
1975const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01001976lys_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 +02001977{
Radek Krejcic071c542016-01-27 14:57:51 +01001978 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02001979 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01001980 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02001981
Michal Vaskoa7789a82016-02-11 15:42:55 +01001982 assert(!prefix || !name);
1983
Michal Vaskob6729c62015-10-21 12:09:47 +02001984 if (prefix && !pref_len) {
1985 pref_len = strlen(prefix);
1986 }
1987 if (name && !name_len) {
1988 name_len = strlen(name);
1989 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02001990
Radek Krejcic4283442016-04-22 09:19:27 +02001991 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01001992
1993 /* module own prefix, submodule own prefix, (sub)module own name */
1994 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
1995 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01001996 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01001997 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02001998 }
1999
Michal Vasko89563fc2016-07-28 16:19:35 +02002000 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002001 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002002 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2003 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002004 return module->imp[i].module;
2005 }
2006 }
2007
Michal Vasko89563fc2016-07-28 16:19:35 +02002008 /* module required by a foreign grouping, deviation, or submodule */
2009 if (name) {
2010 str = strndup(name, name_len);
2011 if (!str) {
2012 LOGMEM;
2013 return NULL;
2014 }
2015 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2016 free(str);
2017 return main_module;
2018 }
2019
Michal Vasko8ce24d72015-10-21 11:27:26 +02002020 return NULL;
2021}
2022
Michal Vasko13b15832015-08-19 11:04:48 +02002023/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002024static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002025module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002026{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002027 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002028 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002029 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002030
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002031 assert(module->ctx);
2032 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002033
Michal Vaskob746fff2016-02-11 11:37:50 +01002034 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002035 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002036 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002037 lydict_remove(ctx, module->imp[i].dsc);
2038 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci225376f2016-02-16 17:36:22 +01002039 }
Radek Krejcidce51452015-06-16 15:20:08 +02002040 free(module->imp);
2041
Radek Krejcic071c542016-01-27 14:57:51 +01002042 /* submodules don't have data tree, the data nodes
2043 * are placed in the main module altogether */
2044 if (!module->type) {
2045 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002046 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002047 }
Radek Krejci21181962015-06-30 14:11:00 +02002048 }
Radek Krejci5a065542015-05-22 15:02:07 +02002049
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002050 lydict_remove(ctx, module->dsc);
2051 lydict_remove(ctx, module->ref);
2052 lydict_remove(ctx, module->org);
2053 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002054 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002055
Radek Krejcieb00f512015-07-01 16:44:58 +02002056 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002057 for (i = 0; i < module->rev_size; i++) {
2058 lydict_remove(ctx, module->rev[i].dsc);
2059 lydict_remove(ctx, module->rev[i].ref);
2060 }
2061 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002062
Radek Krejcieb00f512015-07-01 16:44:58 +02002063 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002064 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002065 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002066 }
2067 module->ident_size = 0;
2068 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002069
Radek Krejcieb00f512015-07-01 16:44:58 +02002070 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002071 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002072 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002073 }
2074 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002075
Radek Krejcieb00f512015-07-01 16:44:58 +02002076 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002077 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002078 lydict_remove(ctx, module->inc[i].dsc);
2079 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcic071c542016-01-27 14:57:51 +01002080 /* complete submodule free is done only from main module since
2081 * submodules propagate their includes to the main module */
2082 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002083 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002084 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002085 }
2086 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002087
Radek Krejcieb00f512015-07-01 16:44:58 +02002088 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002089 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002090 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002091 }
2092 free(module->augment);
2093
Radek Krejcieb00f512015-07-01 16:44:58 +02002094 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002095 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002096 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002097 }
2098 free(module->features);
2099
Radek Krejcieb00f512015-07-01 16:44:58 +02002100 /* deviations */
2101 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002102 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002103 }
2104 free(module->deviation);
2105
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002106 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002107 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002108}
2109
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002110void
Michal Vaskob746fff2016-02-11 11:37:50 +01002111lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002112{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 if (!submodule) {
2114 return;
2115 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002116
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002117 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002118 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002119
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002120 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002121
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002122 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002123}
2124
Radek Krejci3a5501d2016-07-18 22:03:34 +02002125static int
2126ingrouping(const struct lys_node *node)
2127{
2128 const struct lys_node *iter = node;
2129 assert(node);
2130
2131 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2132 if (!iter) {
2133 return 0;
2134 } else {
2135 return 1;
2136 }
2137}
2138
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002139/*
2140 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2141 */
2142static struct lys_node *
2143lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2144 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002145{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002146 struct lys_node *retval = NULL, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002147 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002148 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002149 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002150 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002151 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002152
Michal Vaskoc07187d2015-08-13 15:20:57 +02002153 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002154 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002155 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002156 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002157 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002158 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002159 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002160 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002161 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002162 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002163 struct lys_node_anydata *any = NULL;
2164 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002165 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002166 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002167 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002168 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002169 struct lys_node_rpc_action *rpc = NULL;
2170 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2171 struct lys_node_inout *io = NULL;
2172 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2173 struct lys_node_rpc_action *ntf = NULL;
2174 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002175 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002176 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002177
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002178 /* we cannot just duplicate memory since the strings are stored in
2179 * dictionary and we need to update dictionary counters.
2180 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002181
Radek Krejci1d82ef62015-08-07 14:44:40 +02002182 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002183 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002184 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002185 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002186 break;
2187
Radek Krejci76512572015-08-04 09:47:08 +02002188 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002189 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002190 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002191 break;
2192
Radek Krejci76512572015-08-04 09:47:08 +02002193 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002194 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002195 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002196 break;
2197
Radek Krejci76512572015-08-04 09:47:08 +02002198 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002199 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002200 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002201 break;
2202
Radek Krejci76512572015-08-04 09:47:08 +02002203 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002204 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002205 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002206 break;
2207
Radek Krejci76512572015-08-04 09:47:08 +02002208 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002209 case LYS_ANYDATA:
2210 any = calloc(1, sizeof *any);
2211 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002212 break;
2213
Radek Krejci76512572015-08-04 09:47:08 +02002214 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002215 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002216 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002217 break;
2218
Radek Krejci76512572015-08-04 09:47:08 +02002219 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002220 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002221 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002222 break;
2223
Radek Krejci76512572015-08-04 09:47:08 +02002224 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002225 grp = calloc(1, sizeof *grp);
2226 retval = (struct lys_node *)grp;
2227 break;
2228
Radek Krejci76512572015-08-04 09:47:08 +02002229 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002230 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002231 rpc = calloc(1, sizeof *rpc);
2232 retval = (struct lys_node *)rpc;
2233 break;
2234
Radek Krejci76512572015-08-04 09:47:08 +02002235 case LYS_INPUT:
2236 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002237 io = calloc(1, sizeof *io);
2238 retval = (struct lys_node *)io;
2239 break;
2240
Radek Krejci76512572015-08-04 09:47:08 +02002241 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002242 ntf = calloc(1, sizeof *ntf);
2243 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002244 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002245
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002246 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002247 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002248 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002249 }
Radek Krejcib388c152015-06-04 17:03:03 +02002250
Michal Vasko253035f2015-12-17 16:58:13 +01002251 if (!retval) {
2252 LOGMEM;
2253 return NULL;
2254 }
2255
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002256 /*
2257 * duplicate generic part of the structure
2258 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002259 retval->name = lydict_insert(ctx, node->name, 0);
2260 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2261 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002262 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002263 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002264
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002265 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002266 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002267
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002268 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002269
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002270 retval->iffeature_size = node->iffeature_size;
2271 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2272 if (!retval->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01002273 LOGMEM;
2274 goto error;
2275 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002276
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002277 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002278 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002279 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2280 if (size1) {
2281 /* there is something to duplicate */
2282
2283 /* duplicate compiled expression */
2284 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2285 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2286 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2287
2288 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002289 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002290 for (j = 0; (unsigned int)j < size2; j++) {
2291 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2292 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002293 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002294 /* feature is resolved in origin, so copy it
2295 * - duplication is used for instantiating groupings
2296 * and if-feature inside grouping is supposed to be
2297 * resolved inside the original grouping, so we want
2298 * to keep pointers to features from the grouping
2299 * context */
2300 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2301 } else if (rc == -1) {
2302 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002303 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002304 }
2305 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002306 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002307
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002308 /* inherit config flags */
2309 for (iter = parent; iter && (iter->nodetype == LYS_USES); iter = lys_parent(iter));
2310 if (iter) {
2311 flags = iter->flags & LYS_CONFIG_MASK;
2312 } else {
2313 /* default */
2314 flags = LYS_CONFIG_W;
2315 }
2316
2317 switch (finalize) {
2318 case 1:
2319 /* inherit config flags */
2320 if (retval->flags & LYS_CONFIG_SET) {
2321 /* skip nodes with an explicit config value */
2322 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2323 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2324 LOGVAL(LYE_SPEC, LY_VLOG_LYS, retval, "State nodes cannot have configuration nodes as children.");
2325 goto error;
2326 }
2327 break;
2328 }
2329
2330 if (retval->nodetype != LYS_USES) {
2331 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2332 }
2333 break;
2334 case 2:
2335 /* erase config flags */
2336 retval->flags &= ~LYS_CONFIG_MASK;
2337 retval->flags &= ~LYS_CONFIG_SET;
2338 break;
2339 }
2340
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002341 /* connect it to the parent */
2342 if (lys_node_addchild(parent, retval->module, retval)) {
2343 goto error;
2344 }
Radek Krejcidce51452015-06-16 15:20:08 +02002345
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002346 /* go recursively */
2347 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002348 LY_TREE_FOR(node->child, iter) {
2349 if (!lys_node_dup_recursion(module, retval, iter, retval->nacm, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002350 goto error;
2351 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002352 }
2353 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002354
2355 if (finalize == 1) {
2356 /* check that configuration lists have keys
2357 * - we really want to check keys_size in original node, because the keys are
2358 * not yet resolved here, it is done below in nodetype specific part */
2359 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2360 && !((struct lys_node_list *)node)->keys_size) {
2361 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2362 goto error;
2363 }
2364 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002365 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002366 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002367 }
2368
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002369 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002370 * duplicate specific part of the structure
2371 */
2372 switch (node->nodetype) {
2373 case LYS_CONTAINER:
2374 if (cont_orig->when) {
2375 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002376 }
2377 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002378
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002379 cont->must_size = cont_orig->must_size;
2380 cont->tpdf_size = cont_orig->tpdf_size;
2381
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002382 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002383 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002384 break;
2385
2386 case LYS_CHOICE:
2387 if (choice_orig->when) {
2388 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002389 }
2390
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002391 if (!shallow) {
2392 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002393 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2394 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2395 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002396 if (rc) {
2397 if (rc == EXIT_FAILURE) {
2398 LOGINT;
2399 }
2400 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002401 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002402 } else {
2403 /* useless to check return value, we don't know whether
2404 * there really wasn't any default defined or it just hasn't
2405 * been resolved, we just hope for the best :)
2406 */
2407 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002408 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002409 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002410 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002411 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002412 break;
2413
2414 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002415 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002416 goto error;
2417 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002418 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2419
2420 if (leaf_orig->dflt) {
2421 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci48464ed2016-03-17 15:44:09 +01002422 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002423 goto error;
2424 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002425 }
2426
2427 leaf->must_size = leaf_orig->must_size;
2428 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002429
2430 if (leaf_orig->when) {
2431 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002432 }
2433 break;
2434
2435 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002436 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002437 goto error;
2438 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002439 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2440
2441 llist->min = llist_orig->min;
2442 llist->max = llist_orig->max;
2443
2444 llist->must_size = llist_orig->must_size;
2445 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002446
2447 if (llist_orig->when) {
2448 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002449 }
2450 break;
2451
2452 case LYS_LIST:
2453 list->min = list_orig->min;
2454 list->max = list_orig->max;
2455
2456 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002457 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002458
Radek Krejci581ce772015-11-10 17:22:40 +01002459 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002460 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002461
Radek Krejci581ce772015-11-10 17:22:40 +01002462 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002463 if (list->keys_size) {
2464 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01002465 if (!list->keys) {
2466 LOGMEM;
2467 goto error;
2468 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002469
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002470 if (!shallow) {
2471 /* we managed to resolve it before, resolve it again manually */
2472 if (list_orig->keys[0]) {
2473 for (i = 0; i < list->keys_size; ++i) {
2474 rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF,
2475 (const struct lys_node **)&list->keys[i]);
2476 if (rc) {
2477 if (rc == EXIT_FAILURE) {
2478 LOGINT;
2479 }
2480 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002481 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002482 }
2483 /* it was not resolved yet, add unres copy */
2484 } else {
2485 if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) {
2486 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002487 goto error;
2488 }
Michal Vasko38d01f72015-06-15 09:41:06 +02002489 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002490 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002491 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002492 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002493 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002494
Radek Krejci581ce772015-11-10 17:22:40 +01002495 list->unique_size = list_orig->unique_size;
2496 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002497 if (!list->unique) {
2498 LOGMEM;
2499 goto error;
2500 }
Radek Krejci581ce772015-11-10 17:22:40 +01002501 for (i = 0; i < list->unique_size; ++i) {
2502 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2503 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002504 if (!list->unique[i].expr) {
2505 LOGMEM;
2506 goto error;
2507 }
Radek Krejci581ce772015-11-10 17:22:40 +01002508 for (j = 0; j < list->unique[i].expr_size; j++) {
2509 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2510
2511 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02002512 unique_info = malloc(sizeof *unique_info);
2513 unique_info->list = (struct lys_node *)list;
2514 unique_info->expr = list->unique[i].expr[j];
2515 unique_info->trg_type = &list->unique[i].trg_type;
2516 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002517 }
2518 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002519
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002520 if (list_orig->when) {
2521 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02002522 }
Radek Krejcidce51452015-06-16 15:20:08 +02002523 break;
2524
2525 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002526 case LYS_ANYDATA:
2527 any->must_size = any_orig->must_size;
2528 any->must = lys_restr_dup(ctx, any_orig->must, any->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002529
Radek Krejcibf2abff2016-08-23 15:51:52 +02002530 if (any_orig->when) {
2531 any->when = lys_when_dup(ctx, any_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002532 }
2533 break;
2534
2535 case LYS_USES:
2536 uses->grp = uses_orig->grp;
2537
2538 if (uses_orig->when) {
2539 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002540 }
2541
2542 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002543 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002544 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002545 if (!shallow) {
2546 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Michal Vaskocda51712016-05-19 15:22:11 +02002547 if (!uses->grp || uses->grp->nacm) {
2548 assert(!uses->child);
Radek Krejci48464ed2016-03-17 15:44:09 +01002549 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002550 goto error;
2551 }
Michal Vasko49168a22015-08-17 16:35:41 +02002552 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002553 } else {
2554 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002555 }
2556 break;
2557
Radek Krejcidce51452015-06-16 15:20:08 +02002558 case LYS_CASE:
2559 if (cs_orig->when) {
2560 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02002561 }
2562 break;
2563
2564 case LYS_GROUPING:
2565 grp->tpdf_size = grp_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002566 grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002567 break;
2568
2569 case LYS_RPC:
2570 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002571 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002572 break;
2573
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002574 case LYS_INPUT:
2575 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002576 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002577 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002578 break;
2579
Radek Krejcida04f4a2015-05-21 12:54:09 +02002580 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002581 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002582 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002583 break;
2584
2585 default:
2586 /* LY_NODE_AUGMENT */
2587 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002588 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002589 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002590
2591 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002592
2593error:
2594
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002595 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002596 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002597}
2598
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002599struct lys_node *
2600lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2601 struct unres_schema *unres, int shallow)
2602{
2603 struct lys_node *p = NULL;
2604 int finalize = 0;
2605 struct lys_node *result, *iter, *next;
2606
2607 if (!shallow) {
2608 /* get know where in schema tre we are to know what should be done during instantiation of the grouping */
2609 for (p = parent;
2610 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
2611 p = lys_parent(p));
2612 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
2613 }
2614
2615 result = lys_node_dup_recursion(module, parent, node, nacm, unres, shallow, finalize);
2616 if (finalize) {
2617 /* check xpath expressions in the instantiated tree */
2618 for (iter = next = parent->child; iter; iter = next) {
2619 if (iter->nodetype != LYS_GROUPING) {
2620 if (lys_check_xpath(iter, 0)) {
2621 /* invalid xpath */
2622 return NULL;
2623 }
2624 }
2625
2626 /* select next item */
2627 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
2628 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
2629 next = NULL;
2630 } else {
2631 next = iter->child;
2632 }
2633 if (!next) {
2634 /* no children, try siblings */
2635 next = iter->next;
2636 }
2637 while (!next) {
2638 /* parent is already processed, go to its sibling */
2639 iter = lys_parent(iter);
2640 if (iter == parent) {
2641 /* we are done, no next element to process */
2642 break;
2643 }
2644 next = iter->next;
2645 }
2646 }
2647 }
2648
2649 return result;
2650}
2651
Michal Vasko13b15832015-08-19 11:04:48 +02002652void
Michal Vaskoff006c12016-02-17 11:15:19 +01002653lys_node_switch(struct lys_node *dst, struct lys_node *src)
2654{
2655 struct lys_node *child;
2656
Michal Vaskob42b6972016-06-06 14:21:30 +02002657 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01002658
2659 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02002660 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002661 dst->prev->next = src;
2662 }
2663
2664 /* sibling prev */
2665 if (dst->next) {
2666 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02002667 } else {
2668 for (child = dst->prev; child->prev->next; child = child->prev);
2669 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01002670 }
2671
2672 /* next */
2673 src->next = dst->next;
2674 dst->next = NULL;
2675
2676 /* prev */
2677 if (dst->prev != dst) {
2678 src->prev = dst->prev;
2679 }
2680 dst->prev = dst;
2681
2682 /* parent child */
2683 if (dst->parent && (dst->parent->child == dst)) {
2684 dst->parent->child = src;
2685 }
2686
2687 /* parent */
2688 src->parent = dst->parent;
2689 dst->parent = NULL;
2690
2691 /* child parent */
2692 LY_TREE_FOR(dst->child, child) {
2693 if (child->parent == dst) {
2694 child->parent = src;
2695 }
2696 }
2697
2698 /* child */
2699 src->child = dst->child;
2700 dst->child = NULL;
2701}
2702
2703void
Michal Vasko627975a2016-02-11 11:39:03 +01002704lys_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 +02002705{
2706 struct ly_ctx *ctx;
2707 int i;
2708
2709 if (!module) {
2710 return;
2711 }
2712
2713 /* remove schema from the context */
2714 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002715 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002716 for (i = 0; i < ctx->models.used; i++) {
2717 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002718 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002719 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002720 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 +02002721 ctx->models.list[ctx->models.used] = NULL;
2722 /* we are done */
2723 break;
2724 }
2725 }
2726 }
2727
2728 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002729 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002730
2731 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002732 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002733
2734 free(module);
2735}
Radek Krejci7e97c352015-06-19 16:26:34 +02002736
Radek Krejci9de2c042016-10-19 16:53:06 +02002737static void
2738lys_features_disable_recursive(struct lys_feature *f)
2739{
2740 unsigned int i;
2741 struct lys_feature *depf;
2742
2743 /* disable the feature */
2744 f->flags &= ~LYS_FENABLED;
2745
2746 /* by disabling feature we have to disable also all features that depends on this feature */
2747 if (f->depfeatures) {
2748 for (i = 0; i < f->depfeatures->number; i++) {
2749 depf = (struct lys_feature *)f->depfeatures->set.g[i];
2750 if (depf->flags & LYS_FENABLED) {
2751 lys_features_disable_recursive(depf);
2752 }
2753 }
2754 }
2755}
2756
2757
Radek Krejci7e97c352015-06-19 16:26:34 +02002758/*
2759 * op: 1 - enable, 0 - disable
2760 */
2761static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002762lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002763{
2764 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002765 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02002766 int progress, faili, failj, failk;
2767
2768 uint8_t fsize;
2769 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02002770
2771 if (!module || !name || !strlen(name)) {
2772 return EXIT_FAILURE;
2773 }
2774
2775 if (!strcmp(name, "*")) {
2776 /* enable all */
2777 all = 1;
2778 }
2779
Radek Krejcia889c1f2016-10-19 15:50:11 +02002780 progress = failk = 1;
2781 while (progress && failk) {
2782 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
2783 if (i == -1) {
2784 fsize = module->features_size;
2785 f = module->features;
2786 } else {
2787 fsize = module->inc[i].submodule->features_size;
2788 f = module->inc[i].submodule->features;
2789 }
2790
2791 for (j = 0; j < fsize; j++) {
2792 if (all || !strcmp(f[j].name, name)) {
2793 /* skip already set features */
2794 if (op && (f[j].flags & LYS_FENABLED)) {
2795 continue;
2796 } else if (!op && !(f[j].flags & LYS_FENABLED)) {
2797 continue;
2798 }
2799
2800 if (op) {
2801 /* check referenced features if they are enabled */
2802 for (k = 0; k < f[j].iffeature_size; k++) {
2803 if (!resolve_iffeature(&f[j].iffeature[k])) {
2804 if (all) {
2805 faili = i;
2806 failj = j;
2807 failk = k + 1;
2808 break;
2809 } else {
2810 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2811 f[j].name, k + 1);
2812 return EXIT_FAILURE;
2813 }
2814 }
2815 }
2816
2817 if (k == f[j].iffeature_size) {
2818 /* the last check passed, do the change */
2819 f[j].flags |= LYS_FENABLED;
2820 progress++;
2821 }
2822 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02002823 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02002824 progress++;
2825 }
2826 if (!all) {
2827 /* stop in case changing a single feature */
2828 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002829 }
2830 }
Radek Krejci7e97c352015-06-19 16:26:34 +02002831 }
2832 }
2833 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02002834 if (failk) {
2835 /* print info about the last failing feature */
2836 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2837 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
2838 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02002839 }
2840
2841 if (all) {
2842 return EXIT_SUCCESS;
2843 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02002844 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02002845 return EXIT_FAILURE;
2846 }
2847}
2848
2849API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002850lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002851{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002852 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002853}
2854
2855API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002856lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002857{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002858 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002859}
2860
2861API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002862lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002863{
2864 int i, j;
2865
2866 if (!module || !feature) {
2867 return -1;
2868 }
2869
2870 /* search for the specified feature */
2871 /* module itself */
2872 for (i = 0; i < module->features_size; i++) {
2873 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002874 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002875 return 1;
2876 } else {
2877 return 0;
2878 }
2879 }
2880 }
2881
2882 /* submodules */
2883 for (j = 0; j < module->inc_size; j++) {
2884 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2885 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002886 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002887 return 1;
2888 } else {
2889 return 0;
2890 }
2891 }
2892 }
2893 }
2894
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002895 /* feature definition not found */
2896 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002897}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002898
Radek Krejci96a10da2015-07-30 11:00:14 +02002899API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01002900lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002901{
Radek Krejci96a10da2015-07-30 11:00:14 +02002902 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002903 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002904 unsigned int count;
2905
2906 if (!module) {
2907 return NULL;
2908 }
2909
2910 count = module->features_size;
2911 for (i = 0; i < module->inc_size; i++) {
2912 count += module->inc[i].submodule->features_size;
2913 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002914 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01002915 if (!result) {
2916 LOGMEM;
2917 return NULL;
2918 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002919 if (states) {
2920 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01002921 if (!(*states)) {
2922 LOGMEM;
2923 free(result);
2924 return NULL;
2925 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002926 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002927 count = 0;
2928
2929 /* module itself */
2930 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002931 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002932 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002933 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002934 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002935 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002936 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002937 }
2938 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002939 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002940 }
2941
2942 /* submodules */
2943 for (j = 0; j < module->inc_size; j++) {
2944 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002945 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02002946 if (states) {
2947 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
2948 (*states)[count] = 1;
2949 } else {
2950 (*states)[count] = 0;
2951 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002952 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002953 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002954 }
2955 }
2956
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002957 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02002958 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002959
2960 return result;
2961}
Michal Vaskobaefb032015-09-24 14:52:10 +02002962
Radek Krejci6910a032016-04-13 10:06:21 +02002963API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01002964lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01002965{
2966 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
2967}
2968
Radek Krejci6910a032016-04-13 10:06:21 +02002969API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02002970lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01002971{
2972 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
2973}
2974
Michal Vaskobaefb032015-09-24 14:52:10 +02002975API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01002976lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02002977{
2978 if (!node || !node->parent) {
2979 return NULL;
2980 }
2981
2982 if (node->parent->nodetype == LYS_AUGMENT) {
2983 return ((struct lys_node_augment *)node->parent)->target;
2984 }
2985
2986 return node->parent;
2987}
Michal Vasko1b229152016-01-13 11:28:38 +01002988
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002989API void *
Michal Vasko1b229152016-01-13 11:28:38 +01002990lys_set_private(const struct lys_node *node, void *priv)
2991{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002992 void *prev;
2993
Michal Vasko1b229152016-01-13 11:28:38 +01002994 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002995 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
2996 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01002997 }
2998
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002999 prev = node->priv;
3000 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003001
3002 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003003}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003004
Michal Vasko01c6fd22016-05-20 11:43:05 +02003005int
3006lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3007{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003008 struct lys_node_leaf *iter = leafref_target;
3009
Michal Vasko48a573d2016-07-01 11:46:02 +02003010 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003011 LOGINT;
3012 return -1;
3013 }
3014
Pavol Vican93175152016-08-30 15:34:44 +02003015 /* check for config flag */
3016 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
3017 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
3018 "The %s is config but refers to a non-config %s.",
3019 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3020 return -1;
3021 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003022 /* check for cycles */
3023 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3024 if ((void *)iter == (void *)leafref) {
3025 /* cycle detected */
3026 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3027 return -1;
3028 }
3029 iter = iter->type.info.lref.target;
3030 }
3031
3032 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003033 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003034 if (!leafref_target->backlinks) {
3035 leafref_target->backlinks = (void*)ly_set_new();
3036 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003037 LOGMEM;
3038 return -1;
3039 }
3040 }
Radek Krejci85a54be2016-10-20 12:39:56 +02003041 ly_set_add((struct ly_set *)leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003042
3043 return 0;
3044}
3045
Michal Vasko8548e082016-07-22 12:00:18 +02003046/* not needed currently */
3047#if 0
3048
Michal Vasko5b3492c2016-07-20 09:37:40 +02003049static const char *
3050lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3051{
3052 struct lys_module *prev_mod;
3053 uint32_t str_len, mod_len, buf_idx;
3054
Radek Krejcibf2abff2016-08-23 15:51:52 +02003055 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003056 LOGINT;
3057 return NULL;
3058 }
3059
3060 buf_idx = buf_len - 1;
3061 buf[buf_idx] = '\0';
3062
3063 while (node) {
3064 if (lys_parent(node)) {
3065 prev_mod = lys_node_module(lys_parent(node));
3066 } else {
3067 prev_mod = NULL;
3068 }
3069
Radek Krejcibf2abff2016-08-23 15:51:52 +02003070 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003071 str_len = strlen(node->name);
3072
3073 if (prev_mod != node->module) {
3074 mod_len = strlen(node->module->name);
3075 } else {
3076 mod_len = 0;
3077 }
3078
3079 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3080 LOGINT;
3081 return NULL;
3082 }
3083
3084 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3085
3086 buf[buf_idx] = '/';
3087 if (mod_len) {
3088 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3089 buf[buf_idx + 1 + mod_len] = ':';
3090 }
3091 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3092 }
3093
3094 node = lys_parent(node);
3095 }
3096
3097 return buf + buf_idx;
3098}
3099
Michal Vasko8548e082016-07-22 12:00:18 +02003100#endif
3101
3102API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02003103lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003104{
3105 struct lyxp_set set;
3106 struct ly_set *ret_set;
3107 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003108 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003109
3110 if (!node || !expr) {
3111 ly_errno = LY_EINVAL;
3112 return NULL;
3113 }
3114
3115 memset(&set, 0, sizeof set);
3116
Michal Vaskodb1da032016-09-08 10:07:38 +02003117 opts = LYXP_SNODE;
3118 if (options & LYS_FIND_OUTPUT) {
3119 opts |= LYXP_SNODE_OUTPUT;
3120 }
3121
Michal Vasko46a4bf92016-09-08 08:23:49 +02003122 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02003123 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003124 free(set.val.snodes);
3125 return NULL;
3126 }
3127
3128 ret_set = ly_set_new();
3129
3130 for (i = 0; i < set.used; ++i) {
3131 if (!set.val.snodes[i].in_ctx) {
3132 continue;
3133 }
3134 assert(set.val.snodes[i].in_ctx == 1);
3135
3136 switch (set.val.snodes[i].type) {
3137 case LYXP_NODE_ELEM:
3138 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3139 ly_set_free(ret_set);
3140 free(set.val.snodes);
3141 return NULL;
3142 }
3143 break;
3144 default:
3145 /* ignore roots, text and attr should not ever appear */
3146 break;
3147 }
3148 }
3149
3150 free(set.val.snodes);
3151 return ret_set;
3152}
3153
3154API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003155lys_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 +02003156{
Michal Vasko508a50d2016-09-07 14:50:33 +02003157 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003158 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003159 uint32_t i;
3160
Michal Vaskob94a5e42016-09-08 14:01:56 +02003161 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003162 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003163 }
3164
Michal Vaskob94a5e42016-09-08 14:01:56 +02003165 /* adjust the root */
3166 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3167 do {
3168 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3169 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3170 }
3171
Michal Vasko508a50d2016-09-07 14:50:33 +02003172 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003173
3174 if (options & LYXP_MUST) {
3175 options &= ~LYXP_MUST;
3176 options |= LYXP_SNODE_MUST;
3177 } else if (options & LYXP_WHEN) {
3178 options &= ~LYXP_WHEN;
3179 options |= LYXP_SNODE_WHEN;
3180 } else {
3181 options |= LYXP_SNODE;
3182 }
3183
Michal Vasko508a50d2016-09-07 14:50:33 +02003184 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3185 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003186 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003187 }
3188
Michal Vasko8548e082016-07-22 12:00:18 +02003189 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003190
Michal Vasko508a50d2016-09-07 14:50:33 +02003191 for (i = 0; i < set.used; ++i) {
3192 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003193 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003194 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003195 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003196 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003197 return NULL;
3198 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003199 break;
3200 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003201 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003202 break;
3203 }
3204 }
3205
Michal Vasko508a50d2016-09-07 14:50:33 +02003206 free(set.val.snodes);
3207 return ret_set;
3208}
3209
3210API struct ly_set *
3211lys_node_xpath_atomize(const struct lys_node *node, int options)
3212{
3213 const struct lys_node *next, *elem, *parent, *tmp;
3214 struct lyxp_set set;
3215 struct ly_set *ret_set;
3216 uint16_t i;
3217
3218 if (!node) {
3219 return NULL;
3220 }
3221
3222 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3223 if (!parent) {
3224 /* not in input, output, or notification */
3225 return NULL;
3226 }
3227
3228 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003229 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003230 return NULL;
3231 }
3232
3233 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe9914d12016-10-07 14:32:37 +02003234 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_VALID_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003235 /* elem has no dependencies from other subtrees and local nodes get discarded */
3236 goto next_iter;
3237 }
3238
3239 if (lyxp_node_atomize(elem, &set)) {
3240 ly_set_free(ret_set);
3241 free(set.val.snodes);
3242 return NULL;
3243 }
3244
3245 for (i = 0; i < set.used; ++i) {
3246 switch (set.val.snodes[i].type) {
3247 case LYXP_NODE_ELEM:
3248 if (options & LYXP_NO_LOCAL) {
3249 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3250 if (tmp) {
3251 /* in local subtree, discard */
3252 break;
3253 }
3254 }
3255 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3256 ly_set_free(ret_set);
3257 free(set.val.snodes);
3258 return NULL;
3259 }
3260 break;
3261 default:
3262 /* ignore roots, text and attr should not ever appear */
3263 break;
3264 }
3265 }
3266
3267 free(set.val.snodes);
3268 if (!(options & LYXP_RECURSIVE)) {
3269 break;
3270 }
3271next_iter:
3272 LY_TREE_DFS_END(node, next, elem);
3273 }
3274
Michal Vasko8548e082016-07-22 12:00:18 +02003275 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003276}
3277
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003278static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003279lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003280{
3281 int ret;
3282 char *parent_path;
3283 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003284
Pavol Vican64d0b762016-08-25 10:44:59 +02003285 if (!dev->deviate) {
3286 return ;
3287 }
3288
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003289 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3290 if (dev->orig_node) {
3291 /* removing not-supported deviation ... */
3292 if (strrchr(dev->target_name, '/') != dev->target_name) {
3293 /* ... from a parent */
3294 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3295
3296 target = NULL;
3297 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target);
3298 free(parent_path);
3299 if (ret || !target) {
3300 LOGINT;
3301 return;
3302 }
3303
3304 lys_node_addchild(target, NULL, dev->orig_node);
3305 } else {
3306 /* ... from top-level data */
3307 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3308 }
3309
3310 dev->orig_node = NULL;
3311 } else {
3312 /* adding not-supported deviation */
3313 target = NULL;
3314 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3315 if (ret || !target) {
3316 LOGINT;
3317 return;
3318 }
3319
3320 lys_node_unlink(target);
3321 dev->orig_node = target;
3322 }
3323 } else {
3324 target = NULL;
3325 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3326 if (ret || !target) {
3327 LOGINT;
3328 return;
3329 }
3330
3331 lys_node_switch(target, dev->orig_node);
3332 dev->orig_node = target;
3333 }
3334}
3335
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003336/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3337void
3338lys_switch_deviations(struct lys_module *module)
3339{
Michal Vasko89563fc2016-07-28 16:19:35 +02003340 uint32_t i = 0, j;
3341 const struct lys_module *mod;
3342 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003343
Michal Vasko89563fc2016-07-28 16:19:35 +02003344 if (module->deviated) {
3345 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3346 if (mod == module) {
3347 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003348 }
3349
Michal Vasko89563fc2016-07-28 16:19:35 +02003350 for (j = 0; j < mod->deviation_size; ++j) {
3351 ptr = strstr(mod->deviation[j].target_name, module->name);
3352 if (ptr && ptr[strlen(module->name)] == ':') {
3353 lys_switch_deviation(&mod->deviation[j], module);
3354 }
3355 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003356 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003357
Michal Vasko89563fc2016-07-28 16:19:35 +02003358 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003359 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003360 } else {
3361 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003362 }
3363 }
3364}
3365
3366/* not needed currently, but tested and working */
3367#if 0
3368
3369void
3370lys_sub_module_apply_devs_augs(struct lys_module *module)
3371{
3372 int i;
3373 struct lys_node_augment *aug;
3374 struct lys_node *last;
3375
3376 /* re-apply deviations */
3377 for (i = 0; i < module->deviation_size; ++i) {
3378 lys_switch_deviation(&module->deviation[i], module);
3379 assert(module->deviation[i].orig_node);
Michal Vasko89563fc2016-07-28 16:19:35 +02003380 lys_node_module(module->deviation[i].orig_node)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003381 }
3382
3383 /* re-apply augments */
3384 for (i = 0; i < module->augment_size; ++i) {
3385 aug = &module->augment[i];
3386 assert(aug->target);
3387
3388 /* reconnect augmenting data into the target - add them to the target child list */
3389 if (aug->target->child) {
3390 last = aug->target->child->prev;
3391 last->next = aug->child;
3392 aug->target->child->prev = aug->child->prev;
3393 aug->child->prev = last;
3394 } else {
3395 aug->target->child = aug->child;
3396 }
3397 }
3398}
3399
3400#endif
3401
3402void
3403lys_sub_module_remove_devs_augs(struct lys_module *module)
3404{
Michal Vasko89563fc2016-07-28 16:19:35 +02003405 uint32_t i = 0, j;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003406 struct lys_node *last, *elem;
Michal Vasko89563fc2016-07-28 16:19:35 +02003407 const struct lys_module *mod;
3408 struct lys_module *target_mod;
3409 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003410
3411 /* remove applied deviations */
3412 for (i = 0; i < module->deviation_size; ++i) {
Pavol Vican64d0b762016-08-25 10:44:59 +02003413 if (module->deviation[i].orig_node) {
3414 target_mod = lys_node_module(module->deviation[i].orig_node);
3415 } else {
3416 target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1,
3417 strcspn(module->deviation[i].target_name, ":") - 1);
Radek Krejci0fa54e92016-09-14 14:01:05 +02003418 target_mod = (struct lys_module *)lys_get_implemented_module(target_mod);
Pavol Vican64d0b762016-08-25 10:44:59 +02003419 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003420 lys_switch_deviation(&module->deviation[i], module);
3421
Michal Vasko89563fc2016-07-28 16:19:35 +02003422 /* clear the deviation flag if possible */
3423 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3424 if ((mod == module) || (mod == target_mod)) {
3425 continue;
3426 }
3427
3428 for (j = 0; j < mod->deviation_size; ++j) {
3429 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
3430 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
3431 /* some other module deviation targets the inspected module, flag remains */
3432 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003433 }
3434 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003435
3436 if (j < mod->deviation_size) {
3437 break;
3438 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003439 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003440
3441 if (!mod) {
3442 target_mod->deviated = 0;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003443 }
3444 }
3445
3446 /* remove applied augments */
3447 for (i = 0; i < module->augment_size; ++i) {
Radek Krejcidbc15262016-06-16 14:58:29 +02003448 if (!module->augment[i].target) {
3449 /* skip not resolved augments */
3450 continue;
3451 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003452
3453 elem = module->augment[i].child;
3454 if (elem) {
3455 LY_TREE_FOR(elem, last) {
3456 if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) {
3457 break;
3458 }
3459 }
3460 /* elem is first augment child, last is the last child */
3461
3462 /* parent child ptr */
3463 if (module->augment[i].target->child == elem) {
3464 module->augment[i].target->child = last->next;
3465 }
3466
3467 /* parent child next ptr */
3468 if (elem->prev->next) {
3469 elem->prev->next = last->next;
3470 }
3471
3472 /* parent child prev ptr */
3473 if (last->next) {
3474 last->next->prev = elem->prev;
3475 } else if (module->augment[i].target->child) {
3476 module->augment[i].target->child->prev = elem->prev;
3477 }
3478
3479 /* update augment children themselves */
3480 elem->prev = last;
3481 last->next = NULL;
3482 }
Michal Vaskob8f71322016-05-03 11:39:56 +02003483
3484 /* needs to be NULL for lys_augment_free() to free the children */
3485 module->augment[i].target = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003486 }
3487}
3488
Radek Krejci27fe55e2016-09-13 17:13:35 +02003489static int
3490lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
3491{
3492 struct lys_node *root, *next, *node;
3493 uint8_t i;
3494
3495 for (i = 0; i < module->augment_size; i++) {
3496 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003497 if (!module->augment[i].target
3498 && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003499 return -1;
3500 }
3501 }
3502 LY_TREE_FOR(module->data, root) {
3503 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
3504 LY_TREE_DFS_BEGIN(root, next, node) {
3505 if (node->nodetype == LYS_GROUPING) {
3506 goto nextsibling;
3507 }
3508 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3509 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
3510 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
3511 UNRES_TYPE_LEAFREF, node) == -1) {
3512 return EXIT_FAILURE;
3513 }
3514 }
3515 }
3516
3517 /* modified LY_TREE_DFS_END */
3518 next = node->child;
3519 /* child exception for leafs, leaflists and anyxml without children */
3520 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
3521 next = NULL;
3522 }
3523 if (!next) {
3524nextsibling:
3525 /* no children */
3526 if (node == root) {
3527 /* we are done, root has no children */
3528 break;
3529 }
3530 /* try siblings */
3531 next = node->next;
3532 }
3533 while (!next) {
3534 /* parent is already processed, go to its sibling */
3535 node = lys_parent(node);
3536 /* no siblings, go back through parents */
3537 if (lys_parent(node) == lys_parent(root)) {
3538 /* we are done, no next element to process */
3539 break;
3540 }
3541 next = node->next;
3542 }
3543 }
3544 }
3545
3546 return EXIT_SUCCESS;
3547}
3548
3549API int
3550lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02003551{
3552 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003553 struct unres_schema *unres;
3554 int i, j;
Michal Vasko26055752016-05-03 11:36:31 +02003555
Radek Krejci27fe55e2016-09-13 17:13:35 +02003556 if (!module) {
3557 ly_errno = LY_EINVAL;
3558 return EXIT_FAILURE;
3559 }
3560
3561 module = lys_main_module(module);
Michal Vasko26055752016-05-03 11:36:31 +02003562 if (module->implemented) {
3563 return EXIT_SUCCESS;
3564 }
3565
3566 ctx = module->ctx;
3567
3568 for (i = 0; i < ctx->models.used; ++i) {
3569 if (module == ctx->models.list[i]) {
3570 continue;
3571 }
3572
3573 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
3574 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
3575 return EXIT_FAILURE;
3576 }
3577 }
3578
Radek Krejci27fe55e2016-09-13 17:13:35 +02003579 unres = calloc(1, sizeof *unres);
3580 if (!unres) {
3581 LOGMEM;
3582 return EXIT_FAILURE;
3583 }
3584 /* recursively make the module implemented */
3585 ((struct lys_module *)module)->implemented = 1;
3586 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
3587 goto error;
3588 }
3589 /* process augments in submodules */
3590 for (i = 0; i < module->inc_size; ++i) {
3591 if (!module->inc[i].submodule) {
3592 continue;
3593 }
3594 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
3595 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003596 if (!module->inc[i].submodule->augment[i].target
3597 && (unres_schema_add_node((struct lys_module *)module->inc[i].submodule, unres,
3598 &module->inc[i].submodule->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003599 goto error;
3600 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003601 }
3602 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003603 /* resolve rest of unres items */
3604 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
3605 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003606 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003607 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02003608
3609 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003610
3611error:
3612 ((struct lys_module *)module)->implemented = 0;
3613 unres_schema_free((struct lys_module *)module, &unres);
3614 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003615}
3616
3617void
3618lys_submodule_module_data_free(struct lys_submodule *submodule)
3619{
3620 struct lys_node *next, *elem;
3621
3622 /* remove parsed data */
3623 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3624 if (elem->module == (struct lys_module *)submodule) {
3625 lys_node_free(elem, NULL, 0);
3626 }
3627 }
3628}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02003629
3630int
3631lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
3632{
3633 uint8_t i;
3634
3635 for (i = 0; i < list->keys_size; i++) {
3636 if (list->keys[i] == leaf) {
3637 return i + 1;
3638 }
3639 }
3640
3641 return 0;
3642}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02003643
3644API char *
3645lys_path(const struct lys_node *node)
3646{
3647 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
3648 uint16_t index = LY_BUF_SIZE - 1;
3649
3650 if (!node) {
3651 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
3652 return NULL;
3653 }
3654
3655 /* backup the shared internal buffer */
3656 if (ly_buf_used && buf[0]) {
3657 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
3658 }
3659 ly_buf_used++;
3660
3661 /* build the path */
3662 buf[index] = '\0';
3663 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index);
3664 result = strdup(&buf[index]);
3665
3666 /* restore the shared internal buffer */
3667 if (buf_backup) {
3668 strcpy(buf, buf_backup);
3669 free(buf_backup);
3670 }
3671 ly_buf_used--;
3672
3673 return result;
3674}