blob: 27bf8713acb5303aa7ca6a5ab414d1c0f22995ee [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 Krejci00a0e712016-10-26 10:24:46 +0200840 ly_err_clean(1);
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;
Radek Krejcid80c8602016-10-25 11:56:03 +0200913 const char *rev, *dot, *filename;
914 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +0100915
916 if (!ctx || !path) {
917 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
918 return NULL;
919 }
920
921 fd = open(path, O_RDONLY);
922 if (fd == -1) {
923 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
924 return NULL;
925 }
926
927 ret = lys_parse_fd(ctx, fd, format);
928 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100929
Radek Krejcid80c8602016-10-25 11:56:03 +0200930 if (!ret) {
931 /* error */
932 return NULL;
933 }
934
935 /* check that name and revision match filename */
936 filename = strrchr(path, '/');
937 if (!filename) {
938 filename = path;
939 } else {
940 filename++;
941 }
942 rev = strchr(filename, '@');
943 dot = strrchr(filename, '.');
944
945 /* name */
946 len = strlen(ret->name);
947 if (strncmp(filename, ret->name, len) ||
948 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejcic0c82302016-10-25 14:21:33 +0200949 LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +0200950 }
951 if (rev) {
952 len = dot - ++rev;
953 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
954 LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename,
955 ret->rev_size ? ret->rev[0].date : "none");
956 }
957 }
958
959 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100960 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100961 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100962 }
963
Michal Vasko662610a2015-12-07 11:25:45 +0100964 return ret;
965}
966
967API const struct lys_module *
968lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +0200969{
Michal Vasko1e62a092015-12-01 12:27:20 +0100970 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +0200971 struct stat sb;
972 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +0100973 char buf[PATH_MAX];
974 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +0200975
976 if (!ctx || fd < 0) {
977 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
978 return NULL;
979 }
980
Radek Krejci10a833c2015-12-16 15:28:37 +0100981 if (fstat(fd, &sb) == -1) {
982 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
983 return NULL;
984 }
Radek Krejcib051f722016-02-25 15:12:21 +0100985 if (!S_ISREG(sb.st_mode)) {
986 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
987 return NULL;
988 }
989
Michal Vasko164d9012016-04-01 10:16:59 +0200990 if (!sb.st_size) {
991 LOGERR(LY_EINVAL, "File empty.");
992 return NULL;
993 }
994
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100995 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100996 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +0100997 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +0100998 return NULL;
999 }
Radek Krejcia1df1682016-04-11 14:56:59 +02001000 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001001 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001002
Radek Krejcia77904e2016-02-25 16:23:45 +01001003 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001004 /* get URI if there is /proc */
1005 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001006 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1007 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1008 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1009 }
1010 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001011 }
Radek Krejcib051f722016-02-25 15:12:21 +01001012 }
1013
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001014 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001015}
1016
Michal Vasko5a721fd2016-02-16 12:16:48 +01001017struct lys_submodule *
1018lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001019{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001020 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001021 struct stat sb;
1022 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001023
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001024 assert(module);
1025 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001026
Radek Krejci10a833c2015-12-16 15:28:37 +01001027 if (fstat(fd, &sb) == -1) {
1028 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +01001029 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +01001030 }
Michal Vasko164d9012016-04-01 10:16:59 +02001031
1032 if (!sb.st_size) {
1033 LOGERR(LY_EINVAL, "File empty.");
1034 return NULL;
1035 }
1036
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001037 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001038 if (addr == MAP_FAILED) {
1039 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001040 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001041 }
Michal Vasko5a721fd2016-02-16 12:16:48 +01001042 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001043 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +02001044
Michal Vasko5a721fd2016-02-16 12:16:48 +01001045 return submodule;
1046
Radek Krejciefaeba32015-05-27 14:30:57 +02001047}
1048
Radek Krejci1d82ef62015-08-07 14:44:40 +02001049static struct lys_restr *
1050lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001051{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001052 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001053 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001054
Radek Krejci3733a802015-06-19 13:43:21 +02001055 if (!size) {
1056 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001057 }
Radek Krejci3733a802015-06-19 13:43:21 +02001058
1059 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001060 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001061 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001062 return NULL;
1063 }
Radek Krejci3733a802015-06-19 13:43:21 +02001064 for (i = 0; i < size; i++) {
1065 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1066 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1067 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1068 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1069 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1070 }
1071
1072 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001073}
1074
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001075void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001076lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001077{
1078 assert(ctx);
1079 if (!restr) {
1080 return;
1081 }
1082
1083 lydict_remove(ctx, restr->expr);
1084 lydict_remove(ctx, restr->dsc);
1085 lydict_remove(ctx, restr->ref);
1086 lydict_remove(ctx, restr->eapptag);
1087 lydict_remove(ctx, restr->emsg);
1088}
1089
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001090static void
1091lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
1092{
1093 uint8_t i;
1094
1095 for (i = 0; i < iffeature_size; ++i) {
1096 free(iffeature[i].expr);
1097 free(iffeature[i].features);
1098 }
1099 free(iffeature);
1100}
1101
Michal Vaskob84f88a2015-09-24 13:16:10 +02001102static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001103type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001104 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001105{
1106 int i;
1107
1108 switch (base) {
1109 case LY_TYPE_BINARY:
1110 if (old->info.binary.length) {
1111 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1112 }
1113 break;
1114
1115 case LY_TYPE_BITS:
1116 new->info.bits.count = old->info.bits.count;
1117 if (new->info.bits.count) {
1118 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1119 if (!new->info.bits.bit) {
1120 LOGMEM;
1121 return -1;
1122 }
1123 for (i = 0; i < new->info.bits.count; i++) {
1124 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1125 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1126 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1127 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1128 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1129 }
1130 }
1131 break;
1132
1133 case LY_TYPE_DEC64:
1134 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001135 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001136 if (old->info.dec64.range) {
1137 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1138 }
1139 break;
1140
1141 case LY_TYPE_ENUM:
1142 new->info.enums.count = old->info.enums.count;
1143 if (new->info.enums.count) {
1144 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1145 if (!new->info.enums.enm) {
1146 LOGMEM;
1147 return -1;
1148 }
1149 for (i = 0; i < new->info.enums.count; i++) {
1150 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1151 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1152 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1153 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1154 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1155 }
1156 }
1157 break;
1158
1159 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001160 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001161 if (old->info.ident.count) {
1162 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1163 if (!new->info.ident.ref) {
1164 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001165 return -1;
1166 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001167 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1168 } else {
1169 /* there can be several unresolved base identities, duplicate them all */
1170 i = -1;
1171 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1172 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1173 return -1;
1174 }
1175 --i;
1176 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001177 }
1178 break;
1179
1180 case LY_TYPE_INST:
1181 new->info.inst.req = old->info.inst.req;
1182 break;
1183
1184 case LY_TYPE_INT8:
1185 case LY_TYPE_INT16:
1186 case LY_TYPE_INT32:
1187 case LY_TYPE_INT64:
1188 case LY_TYPE_UINT8:
1189 case LY_TYPE_UINT16:
1190 case LY_TYPE_UINT32:
1191 case LY_TYPE_UINT64:
1192 if (old->info.num.range) {
1193 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1194 }
1195 break;
1196
1197 case LY_TYPE_LEAFREF:
1198 if (old->info.lref.path) {
1199 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001200 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001201 return -1;
1202 }
1203 }
1204 break;
1205
1206 case LY_TYPE_STRING:
1207 if (old->info.str.length) {
1208 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1209 }
1210 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1211 new->info.str.pat_count = old->info.str.pat_count;
1212 break;
1213
1214 case LY_TYPE_UNION:
1215 new->info.uni.count = old->info.uni.count;
1216 if (new->info.uni.count) {
1217 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1218 if (!new->info.uni.types) {
1219 LOGMEM;
1220 return -1;
1221 }
1222 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001223 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 +02001224 return -1;
1225 }
1226 }
1227 }
1228 break;
1229
1230 default:
1231 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1232 break;
1233 }
1234 return EXIT_SUCCESS;
1235}
1236
1237struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001238lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1239 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001240{
1241 struct yang_type *new;
1242
1243 new = calloc(1, sizeof *new);
1244 if (!new) {
1245 LOGMEM;
1246 return NULL;
1247 }
1248 new->flags = old->flags;
1249 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001250 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001251 new->type = type;
1252 if (!new->name) {
1253 LOGMEM;
1254 goto error;
1255 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001256 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001257 new->type->base = new->base;
1258 lys_type_free(module->ctx, new->type);
1259 memset(&new->type->info, 0, sizeof new->type->info);
1260 goto error;
1261 }
1262 return new;
1263
1264 error:
1265 free(new);
1266 return NULL;
1267}
1268
1269static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001270lys_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 +02001271 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001272{
1273 int i;
1274
Michal Vasko1dca6882015-10-22 14:29:42 +02001275 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001276 new->base = old->base;
1277 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001278 new->parent = (struct lys_tpdf *)parent;
Radek Krejci3733a802015-06-19 13:43:21 +02001279
Michal Vasko878e38d2016-09-05 12:17:53 +02001280 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001281 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001282 /* HACK (serious one) for unres */
1283 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001284 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001285 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 +02001286 } else {
1287 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1288 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001289 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001290 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001291 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001292 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001293 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001294 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001295 }
1296
Radek Krejci3a5501d2016-07-18 22:03:34 +02001297 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001298}
1299
1300void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001301lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001302{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001303 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001304
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001305 assert(ctx);
1306 if (!type) {
1307 return;
1308 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001309
Michal Vasko1dca6882015-10-22 14:29:42 +02001310 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001311
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001312 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001313 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001314 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001315 free(type->info.binary.length);
1316 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001317 case LY_TYPE_BITS:
1318 for (i = 0; i < type->info.bits.count; i++) {
1319 lydict_remove(ctx, type->info.bits.bit[i].name);
1320 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1321 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001322 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001323 }
1324 free(type->info.bits.bit);
1325 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001326
1327 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001328 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001329 free(type->info.dec64.range);
1330 break;
1331
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001332 case LY_TYPE_ENUM:
1333 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001334 lydict_remove(ctx, type->info.enums.enm[i].name);
1335 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1336 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001337 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001338 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001339 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001340 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001341
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001342 case LY_TYPE_INT8:
1343 case LY_TYPE_INT16:
1344 case LY_TYPE_INT32:
1345 case LY_TYPE_INT64:
1346 case LY_TYPE_UINT8:
1347 case LY_TYPE_UINT16:
1348 case LY_TYPE_UINT32:
1349 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001350 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001351 free(type->info.num.range);
1352 break;
1353
Radek Krejcidc4c1412015-06-19 15:39:54 +02001354 case LY_TYPE_LEAFREF:
1355 lydict_remove(ctx, type->info.lref.path);
1356 break;
1357
Radek Krejci3733a802015-06-19 13:43:21 +02001358 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001359 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001360 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001361 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001362 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001363 }
1364 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001365 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001366
Radek Krejcie4c366b2015-07-02 10:11:31 +02001367 case LY_TYPE_UNION:
1368 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001369 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001370 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001371 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001372 break;
1373
Michal Vaskod3282192016-09-05 11:27:57 +02001374 case LY_TYPE_IDENT:
1375 free(type->info.ident.ref);
1376 break;
1377
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001378 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001379 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001380 break;
1381 }
Radek Krejci5a065542015-05-22 15:02:07 +02001382}
1383
Radek Krejci1d82ef62015-08-07 14:44:40 +02001384static void
1385lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001386{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001387 assert(ctx);
1388 if (!tpdf) {
1389 return;
1390 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001391
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001392 lydict_remove(ctx, tpdf->name);
1393 lydict_remove(ctx, tpdf->dsc);
1394 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001395
Radek Krejci1d82ef62015-08-07 14:44:40 +02001396 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001397
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001398 lydict_remove(ctx, tpdf->units);
1399 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001400}
1401
Michal Vaskob84f88a2015-09-24 13:16:10 +02001402static struct lys_tpdf *
1403lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1404{
1405 struct lys_tpdf *result;
1406 int i, j;
1407
1408 if (!size) {
1409 return NULL;
1410 }
1411
1412 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001413 if (!result) {
1414 LOGMEM;
1415 return NULL;
1416 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001417 for (i = 0; i < size; i++) {
1418 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1419 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1420 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1421 result[i].flags = old[i].flags;
1422 result[i].module = old[i].module;
1423
Radek Krejci3a5501d2016-07-18 22:03:34 +02001424 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001425 for (j = 0; j <= i; ++j) {
1426 lys_tpdf_free(mod->ctx, &result[j]);
1427 }
1428 free(result);
1429 return NULL;
1430 }
1431
1432 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1433 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1434 }
1435
1436 return result;
1437}
1438
Radek Krejci1d82ef62015-08-07 14:44:40 +02001439static struct lys_when *
1440lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001441{
Radek Krejci76512572015-08-04 09:47:08 +02001442 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001443
1444 if (!old) {
1445 return NULL;
1446 }
1447
1448 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001449 if (!new) {
1450 LOGMEM;
1451 return NULL;
1452 }
Radek Krejci00768f42015-06-18 17:04:04 +02001453 new->cond = lydict_insert(ctx, old->cond, 0);
1454 new->dsc = lydict_insert(ctx, old->dsc, 0);
1455 new->ref = lydict_insert(ctx, old->ref, 0);
1456
1457 return new;
1458}
1459
Michal Vasko0308dd62015-10-07 09:14:40 +02001460void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001461lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001462{
1463 if (!w) {
1464 return;
1465 }
1466
1467 lydict_remove(ctx, w->cond);
1468 lydict_remove(ctx, w->dsc);
1469 lydict_remove(ctx, w->ref);
1470
1471 free(w);
1472}
1473
Radek Krejcib7f5e412015-08-13 10:15:51 +02001474static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001475lys_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 +02001476{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001477 struct lys_node *next, *sub;
1478
Radek Krejcic071c542016-01-27 14:57:51 +01001479 /* children from a resolved augment are freed under the target node */
Michal Vaskod2fbade2016-05-02 17:14:00 +02001480 if (!aug->target) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001481 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001482 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001483 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001484 }
1485
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001486 lydict_remove(ctx, aug->target_name);
1487 lydict_remove(ctx, aug->dsc);
1488 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001489
Radek Krejci9ff0a922016-07-14 13:08:05 +02001490 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001491
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001492 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001493}
1494
Radek Krejci76512572015-08-04 09:47:08 +02001495static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001496lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001497{
Radek Krejci76512572015-08-04 09:47:08 +02001498 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001499 struct lys_node *old_child, *new_child;
1500 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001501
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001502 if (!size) {
1503 return NULL;
1504 }
Radek Krejci106efc02015-06-10 14:36:27 +02001505
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001506 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001507 if (!new) {
1508 LOGMEM;
1509 return NULL;
1510 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001511 for (i = 0; i < size; i++) {
1512 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1513 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1514 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1515 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001516 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001517 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001518
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001519 /* this must succeed, it was already resolved once */
Michal Vasko3edeaf72016-02-11 13:17:43 +01001520 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL,
1521 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001522 LOGINT;
1523 free(new);
1524 return NULL;
1525 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001526 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001527
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001528 /* Correct the augment nodes.
1529 * This function can only be called from lys_node_dup() with uses
1530 * being the node duplicated, so we must have a case of grouping
1531 * with a uses with augments. The augmented nodes have already been
1532 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001533 * (it was set to their actual data parent, not an augment), and
1534 * the new augment does not have child pointer to its augment nodes,
1535 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001536 */
1537 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001538 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001539 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001540 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001541 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001542 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001543 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001544 LY_TREE_FOR(old[i].child, old_child) {
1545 /* all augment nodes were connected as siblings, there can be no more after this */
1546 if (old_child->parent != (struct lys_node *)&old[i]) {
1547 break;
1548 }
1549
Radek Krejci749190d2016-02-18 16:26:25 +01001550 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001551
1552 new_child->parent = (struct lys_node *)&new[i];
1553 new_child = new_child->next;
1554 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001555 }
Radek Krejci106efc02015-06-10 14:36:27 +02001556
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001557 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001558}
1559
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001560static const char **
1561lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1562{
1563 int i;
1564 const char **result;
1565
1566 if (!size) {
1567 return NULL;
1568 }
1569
1570 result = calloc(size, sizeof *result);
1571 if (!result) {
1572 LOGMEM;
1573 return NULL;
1574 }
1575
1576 for (i = 0; i < size; i++) {
1577 result[i] = lydict_insert(ctx, old[i], 0);
1578 }
1579 return result;
1580}
1581
Radek Krejci76512572015-08-04 09:47:08 +02001582static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001583lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001584{
Radek Krejci76512572015-08-04 09:47:08 +02001585 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001586 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001587
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001588 if (!size) {
1589 return NULL;
1590 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001591
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001592 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001593 if (!result) {
1594 LOGMEM;
1595 return NULL;
1596 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001597 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001598 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001599 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1600 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001601 result[i].flags = old[i].flags;
1602 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001603
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001604 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001605 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001606
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001607 result[i].dflt_size = old[i].dflt_size;
1608 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
1609
1610 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001611 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001612 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001613 result[i].mod.list = old[i].mod.list;
1614 }
1615 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001616
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001617 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001618}
1619
Radek Krejci1d82ef62015-08-07 14:44:40 +02001620static void
1621lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001622{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001623 assert(ctx);
1624 if (!ident) {
1625 return;
1626 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001627
Radek Krejci018f1f52016-08-03 16:01:20 +02001628 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02001629 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001630 lydict_remove(ctx, ident->name);
1631 lydict_remove(ctx, ident->dsc);
1632 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001633 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejci6793db02015-05-22 17:49:54 +02001634
1635}
1636
Radek Krejci1d82ef62015-08-07 14:44:40 +02001637static void
1638lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001639{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001640 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001641
Radek Krejcid12f57b2015-08-06 10:43:39 +02001642 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001643 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001644 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001645 }
1646 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001647}
1648
Radek Krejci1d82ef62015-08-07 14:44:40 +02001649static void
Michal Vasko44fb6382016-06-29 11:12:27 +02001650lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02001651{
1652 int i;
1653
1654 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1655 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001656 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001657 }
1658 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02001659
1660 for (i = 0; i < io->must_size; i++) {
1661 lys_restr_free(ctx, &io->must[i]);
1662 }
1663 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001664}
1665
Radek Krejci1d82ef62015-08-07 14:44:40 +02001666static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02001667lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
1668{
1669 int i;
1670
1671 for (i = 0; i < notif->must_size; i++) {
1672 lys_restr_free(ctx, &notif->must[i]);
1673 }
1674 free(notif->must);
1675
1676 for (i = 0; i < notif->tpdf_size; i++) {
1677 lys_tpdf_free(ctx, &notif->tpdf[i]);
1678 }
1679 free(notif->tpdf);
1680}
1681static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02001682lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001683{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001684 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001685
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001686 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001687 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001688 }
1689 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001690
Radek Krejci1d82ef62015-08-07 14:44:40 +02001691 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001692}
1693
Radek Krejci1d82ef62015-08-07 14:44:40 +02001694static void
1695lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001696{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001697 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001698
Radek Krejci85a54be2016-10-20 12:39:56 +02001699 /* leafref backlinks */
1700 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001701
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001702 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001703 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001704 }
1705 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001706
Radek Krejci1d82ef62015-08-07 14:44:40 +02001707 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001708
Radek Krejci1d82ef62015-08-07 14:44:40 +02001709 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001710 lydict_remove(ctx, leaf->units);
1711 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001712}
1713
Radek Krejci1d82ef62015-08-07 14:44:40 +02001714static void
1715lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001716{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001717 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001718
Radek Krejci46c4cd72016-01-21 15:13:52 +01001719 if (llist->child) {
1720 /* leafref backlinks */
1721 ly_set_free((struct ly_set *)llist->child);
1722 }
1723
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001724 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001725 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001726 }
1727 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001728
Pavol Vican38321d02016-08-16 14:56:02 +02001729 for (i = 0; i < llist->dflt_size; i++) {
1730 lydict_remove(ctx, llist->dflt[i]);
1731 }
1732 free(llist->dflt);
1733
Radek Krejci1d82ef62015-08-07 14:44:40 +02001734 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001735
Radek Krejci1d82ef62015-08-07 14:44:40 +02001736 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001737 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001738}
1739
Radek Krejci1d82ef62015-08-07 14:44:40 +02001740static void
1741lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001742{
Radek Krejci581ce772015-11-10 17:22:40 +01001743 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001744
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001745 /* handle only specific parts for LY_NODE_LIST */
1746 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001747 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001748 }
1749 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001750
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001751 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001752 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001753 }
1754 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001755
Radek Krejci1d82ef62015-08-07 14:44:40 +02001756 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001757
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001758 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02001759 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001760 lydict_remove(ctx, list->unique[i].expr[j]);
1761 }
1762 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001763 }
1764 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001765
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001766 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001767}
1768
Radek Krejci1d82ef62015-08-07 14:44:40 +02001769static void
1770lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001771{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001772 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001773
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001774 /* handle only specific parts for LY_NODE_CONTAINER */
1775 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001776
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001777 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001778 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001779 }
1780 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001781
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001782 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001783 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001784 }
1785 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001786
Radek Krejci1d82ef62015-08-07 14:44:40 +02001787 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001788}
1789
Radek Krejci1d82ef62015-08-07 14:44:40 +02001790static void
1791lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001792{
1793 lydict_remove(ctx, f->name);
1794 lydict_remove(ctx, f->dsc);
1795 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001796 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci9de2c042016-10-19 16:53:06 +02001797 ly_set_free(f->depfeatures);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001798}
1799
Radek Krejci1d82ef62015-08-07 14:44:40 +02001800static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001801lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001802{
Radek Krejci581ce772015-11-10 17:22:40 +01001803 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001804 struct ly_ctx *ctx;
1805 struct lys_node *next, *elem;
1806
1807 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001808
1809 lydict_remove(ctx, dev->target_name);
1810 lydict_remove(ctx, dev->dsc);
1811 lydict_remove(ctx, dev->ref);
1812
Pavol Vican64d0b762016-08-25 10:44:59 +02001813 if (!dev->deviate) {
1814 return ;
1815 }
1816
Michal Vaskoff006c12016-02-17 11:15:19 +01001817 /* the module was freed, but we only need the context from orig_node, use ours */
1818 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1819 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1820 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1821 elem->module = module;
1822
1823 LY_TREE_DFS_END(dev->orig_node, next, elem);
1824 }
1825 lys_node_free(dev->orig_node, NULL, 0);
1826 } else {
1827 /* it's just a shallow copy, freeing one node */
1828 dev->orig_node->module = module;
1829 lys_node_free(dev->orig_node, NULL, 1);
1830 }
1831
Radek Krejcieb00f512015-07-01 16:44:58 +02001832 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001833 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02001834 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02001835 }
1836 free(dev->deviate[i].dflt);
1837
Radek Krejcieb00f512015-07-01 16:44:58 +02001838 lydict_remove(ctx, dev->deviate[i].units);
1839
1840 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1841 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001842 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001843 }
1844 free(dev->deviate[i].must);
1845
1846 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001847 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1848 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1849 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001850 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001851 }
1852 free(dev->deviate[i].unique);
1853 }
1854 }
1855 free(dev->deviate);
1856}
1857
Radek Krejci1d82ef62015-08-07 14:44:40 +02001858static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001859lys_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 +02001860{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001861 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001862
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001863 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001864 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001865 lydict_remove(ctx, uses->refine[i].dsc);
1866 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001867
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001868 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001869 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001870 }
1871 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001872
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001873 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02001874 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001875 }
1876 free(uses->refine[i].dflt);
1877
1878 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001879 lydict_remove(ctx, uses->refine[i].mod.presence);
1880 }
1881 }
1882 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001883
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001884 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001885 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001886 }
1887 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001888
Radek Krejci1d82ef62015-08-07 14:44:40 +02001889 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001890}
1891
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001892void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001893lys_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 +02001894{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001895 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001896 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001897
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001898 if (!node) {
1899 return;
1900 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001901
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001902 assert(node->module);
1903 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001904
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001905 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001906
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001907 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001908 if (node->priv && private_destructor) {
1909 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001910 }
1911
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001912 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001913 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001914 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001915 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001916 lydict_remove(ctx, node->dsc);
1917 lydict_remove(ctx, node->ref);
1918 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001919
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001920 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001921 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001922 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001923 }
1924 }
1925
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001926 /* specific part */
1927 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001928 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001929 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001930 break;
Radek Krejci76512572015-08-04 09:47:08 +02001931 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001932 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001933 break;
Radek Krejci76512572015-08-04 09:47:08 +02001934 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001935 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001936 break;
Radek Krejci76512572015-08-04 09:47:08 +02001937 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001938 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001939 break;
Radek Krejci76512572015-08-04 09:47:08 +02001940 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001941 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001942 break;
Radek Krejci76512572015-08-04 09:47:08 +02001943 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001944 case LYS_ANYDATA:
1945 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001946 break;
Radek Krejci76512572015-08-04 09:47:08 +02001947 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001948 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001949 break;
Radek Krejci76512572015-08-04 09:47:08 +02001950 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001951 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001952 break;
Radek Krejci76512572015-08-04 09:47:08 +02001953 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001954 /* do nothing */
1955 break;
Radek Krejci76512572015-08-04 09:47:08 +02001956 case LYS_GROUPING:
1957 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001958 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001959 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001960 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02001961 case LYS_NOTIF:
1962 lys_notif_free(ctx, (struct lys_node_notif *)node);
1963 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001964 case LYS_INPUT:
1965 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001966 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001967 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001968 case LYS_UNKNOWN:
1969 LOGINT;
1970 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001971 }
Radek Krejci5a065542015-05-22 15:02:07 +02001972
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001973 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001974 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001975 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001976}
1977
Michal Vasko1e62a092015-12-01 12:27:20 +01001978const struct lys_module *
Radek Krejci0fa54e92016-09-14 14:01:05 +02001979lys_get_implemented_module(const struct lys_module *mod)
1980{
1981 struct ly_ctx *ctx;
1982 int i;
1983
1984 if (!mod || mod->implemented) {
1985 /* invalid argument or the module itself is implemented */
1986 return mod;
1987 }
1988
1989 ctx = mod->ctx;
1990 for (i = 0; i < ctx->models.used; i++) {
1991 if (!ctx->models.list[i]->implemented) {
1992 continue;
1993 }
1994
1995 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
1996 /* we have some revision of the module implemented */
1997 return ctx->models.list[i];
1998 }
1999 }
2000
2001 /* we have no revision of the module implemented, return the module itself,
2002 * it is up to the caller to set the module implemented when needed */
2003 return mod;
2004}
2005
2006const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01002007lys_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 +02002008{
Radek Krejcic071c542016-01-27 14:57:51 +01002009 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02002010 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002011 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002012
Michal Vaskoa7789a82016-02-11 15:42:55 +01002013 assert(!prefix || !name);
2014
Michal Vaskob6729c62015-10-21 12:09:47 +02002015 if (prefix && !pref_len) {
2016 pref_len = strlen(prefix);
2017 }
2018 if (name && !name_len) {
2019 name_len = strlen(name);
2020 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002021
Radek Krejcic4283442016-04-22 09:19:27 +02002022 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002023
2024 /* module own prefix, submodule own prefix, (sub)module own name */
2025 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2026 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002027 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002028 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002029 }
2030
Michal Vasko89563fc2016-07-28 16:19:35 +02002031 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002032 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002033 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2034 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002035 return module->imp[i].module;
2036 }
2037 }
2038
Michal Vasko89563fc2016-07-28 16:19:35 +02002039 /* module required by a foreign grouping, deviation, or submodule */
2040 if (name) {
2041 str = strndup(name, name_len);
2042 if (!str) {
2043 LOGMEM;
2044 return NULL;
2045 }
2046 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2047 free(str);
2048 return main_module;
2049 }
2050
Michal Vasko8ce24d72015-10-21 11:27:26 +02002051 return NULL;
2052}
2053
Michal Vasko13b15832015-08-19 11:04:48 +02002054/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002055static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002056module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002057{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002058 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002059 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002060 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002061
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002062 assert(module->ctx);
2063 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002064
Michal Vaskob746fff2016-02-11 11:37:50 +01002065 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002066 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002067 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002068 lydict_remove(ctx, module->imp[i].dsc);
2069 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci225376f2016-02-16 17:36:22 +01002070 }
Radek Krejcidce51452015-06-16 15:20:08 +02002071 free(module->imp);
2072
Radek Krejcic071c542016-01-27 14:57:51 +01002073 /* submodules don't have data tree, the data nodes
2074 * are placed in the main module altogether */
2075 if (!module->type) {
2076 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002077 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002078 }
Radek Krejci21181962015-06-30 14:11:00 +02002079 }
Radek Krejci5a065542015-05-22 15:02:07 +02002080
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002081 lydict_remove(ctx, module->dsc);
2082 lydict_remove(ctx, module->ref);
2083 lydict_remove(ctx, module->org);
2084 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002085 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002086
Radek Krejcieb00f512015-07-01 16:44:58 +02002087 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002088 for (i = 0; i < module->rev_size; i++) {
2089 lydict_remove(ctx, module->rev[i].dsc);
2090 lydict_remove(ctx, module->rev[i].ref);
2091 }
2092 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002093
Radek Krejcieb00f512015-07-01 16:44:58 +02002094 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002095 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002096 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002097 }
2098 module->ident_size = 0;
2099 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002100
Radek Krejcieb00f512015-07-01 16:44:58 +02002101 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002102 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002103 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002104 }
2105 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002106
Radek Krejcieb00f512015-07-01 16:44:58 +02002107 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002108 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002109 lydict_remove(ctx, module->inc[i].dsc);
2110 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcic071c542016-01-27 14:57:51 +01002111 /* complete submodule free is done only from main module since
2112 * submodules propagate their includes to the main module */
2113 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002114 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002115 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002116 }
2117 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002118
Radek Krejcieb00f512015-07-01 16:44:58 +02002119 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002120 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002121 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002122 }
2123 free(module->augment);
2124
Radek Krejcieb00f512015-07-01 16:44:58 +02002125 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002126 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002127 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002128 }
2129 free(module->features);
2130
Radek Krejcieb00f512015-07-01 16:44:58 +02002131 /* deviations */
2132 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002133 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002134 }
2135 free(module->deviation);
2136
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002137 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002138 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002139}
2140
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002141void
Michal Vaskob746fff2016-02-11 11:37:50 +01002142lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002143{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002144 if (!submodule) {
2145 return;
2146 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002147
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002148 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002149 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002150
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002151 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002152
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002153 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002154}
2155
Radek Krejci3a5501d2016-07-18 22:03:34 +02002156static int
2157ingrouping(const struct lys_node *node)
2158{
2159 const struct lys_node *iter = node;
2160 assert(node);
2161
2162 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2163 if (!iter) {
2164 return 0;
2165 } else {
2166 return 1;
2167 }
2168}
2169
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002170/*
2171 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2172 */
2173static struct lys_node *
2174lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2175 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002176{
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002177 struct lys_node *retval = NULL, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002178 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002179 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002180 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002181 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002182 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002183
Michal Vaskoc07187d2015-08-13 15:20:57 +02002184 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002185 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002186 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002187 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002188 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002189 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002190 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002191 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002192 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002193 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002194 struct lys_node_anydata *any = NULL;
2195 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002196 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002197 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002198 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002199 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002200 struct lys_node_rpc_action *rpc = NULL;
2201 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2202 struct lys_node_inout *io = NULL;
2203 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2204 struct lys_node_rpc_action *ntf = NULL;
2205 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002206 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002207 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002208
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002209 /* we cannot just duplicate memory since the strings are stored in
2210 * dictionary and we need to update dictionary counters.
2211 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002212
Radek Krejci1d82ef62015-08-07 14:44:40 +02002213 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002214 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002215 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002216 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002217 break;
2218
Radek Krejci76512572015-08-04 09:47:08 +02002219 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002220 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002221 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002222 break;
2223
Radek Krejci76512572015-08-04 09:47:08 +02002224 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002225 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002226 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002227 break;
2228
Radek Krejci76512572015-08-04 09:47:08 +02002229 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002230 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002231 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002232 break;
2233
Radek Krejci76512572015-08-04 09:47:08 +02002234 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002235 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002236 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002237 break;
2238
Radek Krejci76512572015-08-04 09:47:08 +02002239 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002240 case LYS_ANYDATA:
2241 any = calloc(1, sizeof *any);
2242 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002243 break;
2244
Radek Krejci76512572015-08-04 09:47:08 +02002245 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002246 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002247 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002248 break;
2249
Radek Krejci76512572015-08-04 09:47:08 +02002250 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002251 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002252 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002253 break;
2254
Radek Krejci76512572015-08-04 09:47:08 +02002255 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002256 grp = calloc(1, sizeof *grp);
2257 retval = (struct lys_node *)grp;
2258 break;
2259
Radek Krejci76512572015-08-04 09:47:08 +02002260 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002261 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002262 rpc = calloc(1, sizeof *rpc);
2263 retval = (struct lys_node *)rpc;
2264 break;
2265
Radek Krejci76512572015-08-04 09:47:08 +02002266 case LYS_INPUT:
2267 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002268 io = calloc(1, sizeof *io);
2269 retval = (struct lys_node *)io;
2270 break;
2271
Radek Krejci76512572015-08-04 09:47:08 +02002272 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002273 ntf = calloc(1, sizeof *ntf);
2274 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002275 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002276
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002277 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002278 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002279 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002280 }
Radek Krejcib388c152015-06-04 17:03:03 +02002281
Michal Vasko253035f2015-12-17 16:58:13 +01002282 if (!retval) {
2283 LOGMEM;
2284 return NULL;
2285 }
2286
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002287 /*
2288 * duplicate generic part of the structure
2289 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002290 retval->name = lydict_insert(ctx, node->name, 0);
2291 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2292 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002293 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002294 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002295
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002296 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002297 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002298
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002299 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002300
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002301 retval->iffeature_size = node->iffeature_size;
2302 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2303 if (!retval->iffeature) {
Michal Vasko253035f2015-12-17 16:58:13 +01002304 LOGMEM;
2305 goto error;
2306 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002307
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002308 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002309 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002310 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2311 if (size1) {
2312 /* there is something to duplicate */
2313
2314 /* duplicate compiled expression */
2315 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2316 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2317 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2318
2319 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002320 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002321 for (j = 0; (unsigned int)j < size2; j++) {
2322 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2323 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002324 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002325 /* feature is resolved in origin, so copy it
2326 * - duplication is used for instantiating groupings
2327 * and if-feature inside grouping is supposed to be
2328 * resolved inside the original grouping, so we want
2329 * to keep pointers to features from the grouping
2330 * context */
2331 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2332 } else if (rc == -1) {
2333 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002334 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002335 }
2336 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002337 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002338
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002339 /* inherit config flags */
2340 for (iter = parent; iter && (iter->nodetype == LYS_USES); iter = lys_parent(iter));
2341 if (iter) {
2342 flags = iter->flags & LYS_CONFIG_MASK;
2343 } else {
2344 /* default */
2345 flags = LYS_CONFIG_W;
2346 }
2347
2348 switch (finalize) {
2349 case 1:
2350 /* inherit config flags */
2351 if (retval->flags & LYS_CONFIG_SET) {
2352 /* skip nodes with an explicit config value */
2353 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2354 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2355 LOGVAL(LYE_SPEC, LY_VLOG_LYS, retval, "State nodes cannot have configuration nodes as children.");
2356 goto error;
2357 }
2358 break;
2359 }
2360
2361 if (retval->nodetype != LYS_USES) {
2362 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2363 }
2364 break;
2365 case 2:
2366 /* erase config flags */
2367 retval->flags &= ~LYS_CONFIG_MASK;
2368 retval->flags &= ~LYS_CONFIG_SET;
2369 break;
2370 }
2371
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002372 /* connect it to the parent */
2373 if (lys_node_addchild(parent, retval->module, retval)) {
2374 goto error;
2375 }
Radek Krejcidce51452015-06-16 15:20:08 +02002376
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002377 /* go recursively */
2378 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002379 LY_TREE_FOR(node->child, iter) {
2380 if (!lys_node_dup_recursion(module, retval, iter, retval->nacm, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002381 goto error;
2382 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002383 }
2384 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002385
2386 if (finalize == 1) {
2387 /* check that configuration lists have keys
2388 * - we really want to check keys_size in original node, because the keys are
2389 * not yet resolved here, it is done below in nodetype specific part */
2390 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2391 && !((struct lys_node_list *)node)->keys_size) {
2392 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2393 goto error;
2394 }
2395 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002396 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002397 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002398 }
2399
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002400 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002401 * duplicate specific part of the structure
2402 */
2403 switch (node->nodetype) {
2404 case LYS_CONTAINER:
2405 if (cont_orig->when) {
2406 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002407 }
2408 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002409
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002410 cont->must_size = cont_orig->must_size;
2411 cont->tpdf_size = cont_orig->tpdf_size;
2412
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002413 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002414 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002415 break;
2416
2417 case LYS_CHOICE:
2418 if (choice_orig->when) {
2419 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002420 }
2421
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002422 if (!shallow) {
2423 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002424 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2425 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2426 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002427 if (rc) {
2428 if (rc == EXIT_FAILURE) {
2429 LOGINT;
2430 }
2431 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002432 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002433 } else {
2434 /* useless to check return value, we don't know whether
2435 * there really wasn't any default defined or it just hasn't
2436 * been resolved, we just hope for the best :)
2437 */
2438 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002439 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002440 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002441 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002442 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002443 break;
2444
2445 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002446 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002447 goto error;
2448 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002449 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2450
2451 if (leaf_orig->dflt) {
2452 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci51673202016-11-01 17:00:32 +01002453 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT,
2454 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002455 goto error;
2456 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002457 }
2458
2459 leaf->must_size = leaf_orig->must_size;
2460 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002461
2462 if (leaf_orig->when) {
2463 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002464 }
2465 break;
2466
2467 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002468 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002469 goto error;
2470 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002471 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2472
2473 llist->min = llist_orig->min;
2474 llist->max = llist_orig->max;
2475
2476 llist->must_size = llist_orig->must_size;
2477 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002478
Radek Krejci51673202016-11-01 17:00:32 +01002479 llist->dflt_size = llist_orig->dflt_size;
2480 llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt);
2481 for (i = 0; i < llist->dflt_size; i++) {
2482 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
2483 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
2484 (struct lys_node *)(&llist->dflt[i])) == -1) {
2485 goto error;
2486 }
2487 }
2488
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002489 if (llist_orig->when) {
2490 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002491 }
2492 break;
2493
2494 case LYS_LIST:
2495 list->min = list_orig->min;
2496 list->max = list_orig->max;
2497
2498 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002499 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002500
Radek Krejci581ce772015-11-10 17:22:40 +01002501 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002502 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002503
Radek Krejci581ce772015-11-10 17:22:40 +01002504 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002505 if (list->keys_size) {
2506 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vasko253035f2015-12-17 16:58:13 +01002507 if (!list->keys) {
2508 LOGMEM;
2509 goto error;
2510 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002511
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002512 if (!shallow) {
2513 /* we managed to resolve it before, resolve it again manually */
2514 if (list_orig->keys[0]) {
2515 for (i = 0; i < list->keys_size; ++i) {
2516 rc = lys_get_sibling(list->child, lys_node_module(retval)->name, 0, list_orig->keys[i]->name, 0, LYS_LEAF,
2517 (const struct lys_node **)&list->keys[i]);
2518 if (rc) {
2519 if (rc == EXIT_FAILURE) {
2520 LOGINT;
2521 }
2522 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002523 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002524 }
2525 /* it was not resolved yet, add unres copy */
2526 } else {
2527 if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) {
2528 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002529 goto error;
2530 }
Michal Vasko38d01f72015-06-15 09:41:06 +02002531 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002532 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002533 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002534 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002535 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002536
Radek Krejci581ce772015-11-10 17:22:40 +01002537 list->unique_size = list_orig->unique_size;
2538 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002539 if (!list->unique) {
2540 LOGMEM;
2541 goto error;
2542 }
Radek Krejci581ce772015-11-10 17:22:40 +01002543 for (i = 0; i < list->unique_size; ++i) {
2544 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2545 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002546 if (!list->unique[i].expr) {
2547 LOGMEM;
2548 goto error;
2549 }
Radek Krejci581ce772015-11-10 17:22:40 +01002550 for (j = 0; j < list->unique[i].expr_size; j++) {
2551 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2552
2553 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02002554 unique_info = malloc(sizeof *unique_info);
2555 unique_info->list = (struct lys_node *)list;
2556 unique_info->expr = list->unique[i].expr[j];
2557 unique_info->trg_type = &list->unique[i].trg_type;
2558 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002559 }
2560 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002561
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002562 if (list_orig->when) {
2563 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02002564 }
Radek Krejcidce51452015-06-16 15:20:08 +02002565 break;
2566
2567 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002568 case LYS_ANYDATA:
2569 any->must_size = any_orig->must_size;
2570 any->must = lys_restr_dup(ctx, any_orig->must, any->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002571
Radek Krejcibf2abff2016-08-23 15:51:52 +02002572 if (any_orig->when) {
2573 any->when = lys_when_dup(ctx, any_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002574 }
2575 break;
2576
2577 case LYS_USES:
2578 uses->grp = uses_orig->grp;
2579
2580 if (uses_orig->when) {
2581 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002582 }
2583
2584 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002585 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002586 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002587 if (!shallow) {
2588 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Michal Vaskocda51712016-05-19 15:22:11 +02002589 if (!uses->grp || uses->grp->nacm) {
2590 assert(!uses->child);
Radek Krejci48464ed2016-03-17 15:44:09 +01002591 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002592 goto error;
2593 }
Michal Vasko49168a22015-08-17 16:35:41 +02002594 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002595 } else {
2596 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002597 }
2598 break;
2599
Radek Krejcidce51452015-06-16 15:20:08 +02002600 case LYS_CASE:
2601 if (cs_orig->when) {
2602 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02002603 }
2604 break;
2605
2606 case LYS_GROUPING:
2607 grp->tpdf_size = grp_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002608 grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002609 break;
2610
2611 case LYS_RPC:
2612 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002613 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002614 break;
2615
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002616 case LYS_INPUT:
2617 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002618 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002619 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002620 break;
2621
Radek Krejcida04f4a2015-05-21 12:54:09 +02002622 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002623 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002624 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002625 break;
2626
2627 default:
2628 /* LY_NODE_AUGMENT */
2629 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002630 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002631 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002632
2633 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002634
2635error:
2636
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002637 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002638 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002639}
2640
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002641struct lys_node *
2642lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2643 struct unres_schema *unres, int shallow)
2644{
2645 struct lys_node *p = NULL;
2646 int finalize = 0;
2647 struct lys_node *result, *iter, *next;
2648
2649 if (!shallow) {
2650 /* get know where in schema tre we are to know what should be done during instantiation of the grouping */
2651 for (p = parent;
2652 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
2653 p = lys_parent(p));
2654 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
2655 }
2656
2657 result = lys_node_dup_recursion(module, parent, node, nacm, unres, shallow, finalize);
2658 if (finalize) {
2659 /* check xpath expressions in the instantiated tree */
2660 for (iter = next = parent->child; iter; iter = next) {
2661 if (iter->nodetype != LYS_GROUPING) {
2662 if (lys_check_xpath(iter, 0)) {
2663 /* invalid xpath */
2664 return NULL;
2665 }
2666 }
2667
2668 /* select next item */
2669 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
2670 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
2671 next = NULL;
2672 } else {
2673 next = iter->child;
2674 }
2675 if (!next) {
2676 /* no children, try siblings */
2677 next = iter->next;
2678 }
2679 while (!next) {
2680 /* parent is already processed, go to its sibling */
2681 iter = lys_parent(iter);
2682 if (iter == parent) {
2683 /* we are done, no next element to process */
2684 break;
2685 }
2686 next = iter->next;
2687 }
2688 }
2689 }
2690
2691 return result;
2692}
2693
Michal Vasko13b15832015-08-19 11:04:48 +02002694void
Michal Vaskoff006c12016-02-17 11:15:19 +01002695lys_node_switch(struct lys_node *dst, struct lys_node *src)
2696{
2697 struct lys_node *child;
2698
Michal Vaskob42b6972016-06-06 14:21:30 +02002699 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01002700
2701 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02002702 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002703 dst->prev->next = src;
2704 }
2705
2706 /* sibling prev */
2707 if (dst->next) {
2708 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02002709 } else {
2710 for (child = dst->prev; child->prev->next; child = child->prev);
2711 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01002712 }
2713
2714 /* next */
2715 src->next = dst->next;
2716 dst->next = NULL;
2717
2718 /* prev */
2719 if (dst->prev != dst) {
2720 src->prev = dst->prev;
2721 }
2722 dst->prev = dst;
2723
2724 /* parent child */
2725 if (dst->parent && (dst->parent->child == dst)) {
2726 dst->parent->child = src;
2727 }
2728
2729 /* parent */
2730 src->parent = dst->parent;
2731 dst->parent = NULL;
2732
2733 /* child parent */
2734 LY_TREE_FOR(dst->child, child) {
2735 if (child->parent == dst) {
2736 child->parent = src;
2737 }
2738 }
2739
2740 /* child */
2741 src->child = dst->child;
2742 dst->child = NULL;
2743}
2744
2745void
Michal Vasko627975a2016-02-11 11:39:03 +01002746lys_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 +02002747{
2748 struct ly_ctx *ctx;
2749 int i;
2750
2751 if (!module) {
2752 return;
2753 }
2754
2755 /* remove schema from the context */
2756 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002757 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002758 for (i = 0; i < ctx->models.used; i++) {
2759 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002760 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002761 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002762 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 +02002763 ctx->models.list[ctx->models.used] = NULL;
2764 /* we are done */
2765 break;
2766 }
2767 }
2768 }
2769
2770 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002771 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002772
2773 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002774 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002775
2776 free(module);
2777}
Radek Krejci7e97c352015-06-19 16:26:34 +02002778
Radek Krejci9de2c042016-10-19 16:53:06 +02002779static void
2780lys_features_disable_recursive(struct lys_feature *f)
2781{
2782 unsigned int i;
2783 struct lys_feature *depf;
2784
2785 /* disable the feature */
2786 f->flags &= ~LYS_FENABLED;
2787
2788 /* by disabling feature we have to disable also all features that depends on this feature */
2789 if (f->depfeatures) {
2790 for (i = 0; i < f->depfeatures->number; i++) {
2791 depf = (struct lys_feature *)f->depfeatures->set.g[i];
2792 if (depf->flags & LYS_FENABLED) {
2793 lys_features_disable_recursive(depf);
2794 }
2795 }
2796 }
2797}
2798
2799
Radek Krejci7e97c352015-06-19 16:26:34 +02002800/*
2801 * op: 1 - enable, 0 - disable
2802 */
2803static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002804lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002805{
2806 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002807 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02002808 int progress, faili, failj, failk;
2809
2810 uint8_t fsize;
2811 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02002812
2813 if (!module || !name || !strlen(name)) {
2814 return EXIT_FAILURE;
2815 }
2816
2817 if (!strcmp(name, "*")) {
2818 /* enable all */
2819 all = 1;
2820 }
2821
Radek Krejcia889c1f2016-10-19 15:50:11 +02002822 progress = failk = 1;
2823 while (progress && failk) {
2824 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
2825 if (i == -1) {
2826 fsize = module->features_size;
2827 f = module->features;
2828 } else {
2829 fsize = module->inc[i].submodule->features_size;
2830 f = module->inc[i].submodule->features;
2831 }
2832
2833 for (j = 0; j < fsize; j++) {
2834 if (all || !strcmp(f[j].name, name)) {
2835 /* skip already set features */
2836 if (op && (f[j].flags & LYS_FENABLED)) {
2837 continue;
2838 } else if (!op && !(f[j].flags & LYS_FENABLED)) {
2839 continue;
2840 }
2841
2842 if (op) {
2843 /* check referenced features if they are enabled */
2844 for (k = 0; k < f[j].iffeature_size; k++) {
2845 if (!resolve_iffeature(&f[j].iffeature[k])) {
2846 if (all) {
2847 faili = i;
2848 failj = j;
2849 failk = k + 1;
2850 break;
2851 } else {
2852 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2853 f[j].name, k + 1);
2854 return EXIT_FAILURE;
2855 }
2856 }
2857 }
2858
2859 if (k == f[j].iffeature_size) {
2860 /* the last check passed, do the change */
2861 f[j].flags |= LYS_FENABLED;
2862 progress++;
2863 }
2864 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02002865 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02002866 progress++;
2867 }
2868 if (!all) {
2869 /* stop in case changing a single feature */
2870 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002871 }
2872 }
Radek Krejci7e97c352015-06-19 16:26:34 +02002873 }
2874 }
2875 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02002876 if (failk) {
2877 /* print info about the last failing feature */
2878 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2879 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
2880 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02002881 }
2882
2883 if (all) {
2884 return EXIT_SUCCESS;
2885 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02002886 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02002887 return EXIT_FAILURE;
2888 }
2889}
2890
2891API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002892lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002893{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002894 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002895}
2896
2897API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002898lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002899{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002900 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002901}
2902
2903API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002904lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002905{
2906 int i, j;
2907
2908 if (!module || !feature) {
2909 return -1;
2910 }
2911
2912 /* search for the specified feature */
2913 /* module itself */
2914 for (i = 0; i < module->features_size; i++) {
2915 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002916 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002917 return 1;
2918 } else {
2919 return 0;
2920 }
2921 }
2922 }
2923
2924 /* submodules */
2925 for (j = 0; j < module->inc_size; j++) {
2926 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2927 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002928 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002929 return 1;
2930 } else {
2931 return 0;
2932 }
2933 }
2934 }
2935 }
2936
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002937 /* feature definition not found */
2938 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002939}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002940
Radek Krejci96a10da2015-07-30 11:00:14 +02002941API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01002942lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002943{
Radek Krejci96a10da2015-07-30 11:00:14 +02002944 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002945 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002946 unsigned int count;
2947
2948 if (!module) {
2949 return NULL;
2950 }
2951
2952 count = module->features_size;
2953 for (i = 0; i < module->inc_size; i++) {
2954 count += module->inc[i].submodule->features_size;
2955 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002956 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01002957 if (!result) {
2958 LOGMEM;
2959 return NULL;
2960 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002961 if (states) {
2962 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01002963 if (!(*states)) {
2964 LOGMEM;
2965 free(result);
2966 return NULL;
2967 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002968 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002969 count = 0;
2970
2971 /* module itself */
2972 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002973 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002974 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002975 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002976 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002977 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002978 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002979 }
2980 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002981 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002982 }
2983
2984 /* submodules */
2985 for (j = 0; j < module->inc_size; j++) {
2986 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002987 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02002988 if (states) {
2989 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
2990 (*states)[count] = 1;
2991 } else {
2992 (*states)[count] = 0;
2993 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002994 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002995 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002996 }
2997 }
2998
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002999 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003000 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003001
3002 return result;
3003}
Michal Vaskobaefb032015-09-24 14:52:10 +02003004
Radek Krejci6910a032016-04-13 10:06:21 +02003005API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003006lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003007{
3008 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3009}
3010
Radek Krejci6910a032016-04-13 10:06:21 +02003011API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003012lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003013{
3014 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3015}
3016
Michal Vaskobaefb032015-09-24 14:52:10 +02003017API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003018lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003019{
3020 if (!node || !node->parent) {
3021 return NULL;
3022 }
3023
3024 if (node->parent->nodetype == LYS_AUGMENT) {
3025 return ((struct lys_node_augment *)node->parent)->target;
3026 }
3027
3028 return node->parent;
3029}
Michal Vasko1b229152016-01-13 11:28:38 +01003030
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003031API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003032lys_set_private(const struct lys_node *node, void *priv)
3033{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003034 void *prev;
3035
Michal Vasko1b229152016-01-13 11:28:38 +01003036 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003037 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
3038 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003039 }
3040
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003041 prev = node->priv;
3042 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003043
3044 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003045}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003046
Michal Vasko01c6fd22016-05-20 11:43:05 +02003047int
3048lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3049{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003050 struct lys_node_leaf *iter = leafref_target;
3051
Michal Vasko48a573d2016-07-01 11:46:02 +02003052 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003053 LOGINT;
3054 return -1;
3055 }
3056
Pavol Vican93175152016-08-30 15:34:44 +02003057 /* check for config flag */
3058 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
3059 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
3060 "The %s is config but refers to a non-config %s.",
3061 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3062 return -1;
3063 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003064 /* check for cycles */
3065 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3066 if ((void *)iter == (void *)leafref) {
3067 /* cycle detected */
3068 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3069 return -1;
3070 }
3071 iter = iter->type.info.lref.target;
3072 }
3073
3074 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003075 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003076 if (!leafref_target->backlinks) {
3077 leafref_target->backlinks = (void*)ly_set_new();
3078 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003079 LOGMEM;
3080 return -1;
3081 }
3082 }
Radek Krejci85a54be2016-10-20 12:39:56 +02003083 ly_set_add((struct ly_set *)leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003084
3085 return 0;
3086}
3087
Michal Vasko8548e082016-07-22 12:00:18 +02003088/* not needed currently */
3089#if 0
3090
Michal Vasko5b3492c2016-07-20 09:37:40 +02003091static const char *
3092lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3093{
3094 struct lys_module *prev_mod;
3095 uint32_t str_len, mod_len, buf_idx;
3096
Radek Krejcibf2abff2016-08-23 15:51:52 +02003097 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003098 LOGINT;
3099 return NULL;
3100 }
3101
3102 buf_idx = buf_len - 1;
3103 buf[buf_idx] = '\0';
3104
3105 while (node) {
3106 if (lys_parent(node)) {
3107 prev_mod = lys_node_module(lys_parent(node));
3108 } else {
3109 prev_mod = NULL;
3110 }
3111
Radek Krejcibf2abff2016-08-23 15:51:52 +02003112 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003113 str_len = strlen(node->name);
3114
3115 if (prev_mod != node->module) {
3116 mod_len = strlen(node->module->name);
3117 } else {
3118 mod_len = 0;
3119 }
3120
3121 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3122 LOGINT;
3123 return NULL;
3124 }
3125
3126 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3127
3128 buf[buf_idx] = '/';
3129 if (mod_len) {
3130 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3131 buf[buf_idx + 1 + mod_len] = ':';
3132 }
3133 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3134 }
3135
3136 node = lys_parent(node);
3137 }
3138
3139 return buf + buf_idx;
3140}
3141
Michal Vasko8548e082016-07-22 12:00:18 +02003142#endif
3143
3144API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02003145lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003146{
3147 struct lyxp_set set;
3148 struct ly_set *ret_set;
3149 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003150 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003151
3152 if (!node || !expr) {
3153 ly_errno = LY_EINVAL;
3154 return NULL;
3155 }
3156
3157 memset(&set, 0, sizeof set);
3158
Michal Vaskodb1da032016-09-08 10:07:38 +02003159 opts = LYXP_SNODE;
3160 if (options & LYS_FIND_OUTPUT) {
3161 opts |= LYXP_SNODE_OUTPUT;
3162 }
3163
Michal Vasko46a4bf92016-09-08 08:23:49 +02003164 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02003165 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003166 free(set.val.snodes);
3167 return NULL;
3168 }
3169
3170 ret_set = ly_set_new();
3171
3172 for (i = 0; i < set.used; ++i) {
3173 if (!set.val.snodes[i].in_ctx) {
3174 continue;
3175 }
3176 assert(set.val.snodes[i].in_ctx == 1);
3177
3178 switch (set.val.snodes[i].type) {
3179 case LYXP_NODE_ELEM:
3180 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3181 ly_set_free(ret_set);
3182 free(set.val.snodes);
3183 return NULL;
3184 }
3185 break;
3186 default:
3187 /* ignore roots, text and attr should not ever appear */
3188 break;
3189 }
3190 }
3191
3192 free(set.val.snodes);
3193 return ret_set;
3194}
3195
3196API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003197lys_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 +02003198{
Michal Vasko508a50d2016-09-07 14:50:33 +02003199 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003200 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003201 uint32_t i;
3202
Michal Vaskob94a5e42016-09-08 14:01:56 +02003203 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003204 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003205 }
3206
Michal Vaskob94a5e42016-09-08 14:01:56 +02003207 /* adjust the root */
3208 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3209 do {
3210 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3211 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3212 }
3213
Michal Vasko508a50d2016-09-07 14:50:33 +02003214 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003215
3216 if (options & LYXP_MUST) {
3217 options &= ~LYXP_MUST;
3218 options |= LYXP_SNODE_MUST;
3219 } else if (options & LYXP_WHEN) {
3220 options &= ~LYXP_WHEN;
3221 options |= LYXP_SNODE_WHEN;
3222 } else {
3223 options |= LYXP_SNODE;
3224 }
3225
Michal Vasko508a50d2016-09-07 14:50:33 +02003226 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3227 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003228 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003229 }
3230
Michal Vasko8548e082016-07-22 12:00:18 +02003231 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003232
Michal Vasko508a50d2016-09-07 14:50:33 +02003233 for (i = 0; i < set.used; ++i) {
3234 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003235 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003236 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003237 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003238 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003239 return NULL;
3240 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003241 break;
3242 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003243 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003244 break;
3245 }
3246 }
3247
Michal Vasko508a50d2016-09-07 14:50:33 +02003248 free(set.val.snodes);
3249 return ret_set;
3250}
3251
3252API struct ly_set *
3253lys_node_xpath_atomize(const struct lys_node *node, int options)
3254{
3255 const struct lys_node *next, *elem, *parent, *tmp;
3256 struct lyxp_set set;
3257 struct ly_set *ret_set;
3258 uint16_t i;
3259
3260 if (!node) {
3261 return NULL;
3262 }
3263
3264 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3265 if (!parent) {
3266 /* not in input, output, or notification */
3267 return NULL;
3268 }
3269
3270 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003271 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003272 return NULL;
3273 }
3274
3275 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe9914d12016-10-07 14:32:37 +02003276 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_VALID_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003277 /* elem has no dependencies from other subtrees and local nodes get discarded */
3278 goto next_iter;
3279 }
3280
3281 if (lyxp_node_atomize(elem, &set)) {
3282 ly_set_free(ret_set);
3283 free(set.val.snodes);
3284 return NULL;
3285 }
3286
3287 for (i = 0; i < set.used; ++i) {
3288 switch (set.val.snodes[i].type) {
3289 case LYXP_NODE_ELEM:
3290 if (options & LYXP_NO_LOCAL) {
3291 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3292 if (tmp) {
3293 /* in local subtree, discard */
3294 break;
3295 }
3296 }
3297 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3298 ly_set_free(ret_set);
3299 free(set.val.snodes);
3300 return NULL;
3301 }
3302 break;
3303 default:
3304 /* ignore roots, text and attr should not ever appear */
3305 break;
3306 }
3307 }
3308
3309 free(set.val.snodes);
3310 if (!(options & LYXP_RECURSIVE)) {
3311 break;
3312 }
3313next_iter:
3314 LY_TREE_DFS_END(node, next, elem);
3315 }
3316
Michal Vasko8548e082016-07-22 12:00:18 +02003317 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003318}
3319
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003320static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003321lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003322{
3323 int ret;
3324 char *parent_path;
3325 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003326
Pavol Vican64d0b762016-08-25 10:44:59 +02003327 if (!dev->deviate) {
3328 return ;
3329 }
3330
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003331 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3332 if (dev->orig_node) {
3333 /* removing not-supported deviation ... */
3334 if (strrchr(dev->target_name, '/') != dev->target_name) {
3335 /* ... from a parent */
3336 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3337
3338 target = NULL;
3339 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target);
3340 free(parent_path);
3341 if (ret || !target) {
3342 LOGINT;
3343 return;
3344 }
3345
3346 lys_node_addchild(target, NULL, dev->orig_node);
3347 } else {
3348 /* ... from top-level data */
3349 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3350 }
3351
3352 dev->orig_node = NULL;
3353 } else {
3354 /* adding not-supported deviation */
3355 target = NULL;
3356 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3357 if (ret || !target) {
3358 LOGINT;
3359 return;
3360 }
3361
3362 lys_node_unlink(target);
3363 dev->orig_node = target;
3364 }
3365 } else {
3366 target = NULL;
3367 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target);
3368 if (ret || !target) {
3369 LOGINT;
3370 return;
3371 }
3372
3373 lys_node_switch(target, dev->orig_node);
3374 dev->orig_node = target;
3375 }
3376}
3377
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003378/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3379void
3380lys_switch_deviations(struct lys_module *module)
3381{
Michal Vasko89563fc2016-07-28 16:19:35 +02003382 uint32_t i = 0, j;
3383 const struct lys_module *mod;
3384 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003385
Michal Vasko89563fc2016-07-28 16:19:35 +02003386 if (module->deviated) {
3387 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3388 if (mod == module) {
3389 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003390 }
3391
Michal Vasko89563fc2016-07-28 16:19:35 +02003392 for (j = 0; j < mod->deviation_size; ++j) {
3393 ptr = strstr(mod->deviation[j].target_name, module->name);
3394 if (ptr && ptr[strlen(module->name)] == ':') {
3395 lys_switch_deviation(&mod->deviation[j], module);
3396 }
3397 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003398 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003399
Michal Vasko89563fc2016-07-28 16:19:35 +02003400 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003401 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003402 } else {
3403 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003404 }
3405 }
3406}
3407
3408/* not needed currently, but tested and working */
3409#if 0
3410
3411void
3412lys_sub_module_apply_devs_augs(struct lys_module *module)
3413{
3414 int i;
3415 struct lys_node_augment *aug;
3416 struct lys_node *last;
3417
3418 /* re-apply deviations */
3419 for (i = 0; i < module->deviation_size; ++i) {
3420 lys_switch_deviation(&module->deviation[i], module);
3421 assert(module->deviation[i].orig_node);
Michal Vasko89563fc2016-07-28 16:19:35 +02003422 lys_node_module(module->deviation[i].orig_node)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003423 }
3424
3425 /* re-apply augments */
3426 for (i = 0; i < module->augment_size; ++i) {
3427 aug = &module->augment[i];
3428 assert(aug->target);
3429
3430 /* reconnect augmenting data into the target - add them to the target child list */
3431 if (aug->target->child) {
3432 last = aug->target->child->prev;
3433 last->next = aug->child;
3434 aug->target->child->prev = aug->child->prev;
3435 aug->child->prev = last;
3436 } else {
3437 aug->target->child = aug->child;
3438 }
3439 }
3440}
3441
3442#endif
3443
3444void
3445lys_sub_module_remove_devs_augs(struct lys_module *module)
3446{
Michal Vasko89563fc2016-07-28 16:19:35 +02003447 uint32_t i = 0, j;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003448 struct lys_node *last, *elem;
Michal Vasko89563fc2016-07-28 16:19:35 +02003449 const struct lys_module *mod;
3450 struct lys_module *target_mod;
3451 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003452
3453 /* remove applied deviations */
3454 for (i = 0; i < module->deviation_size; ++i) {
Pavol Vican64d0b762016-08-25 10:44:59 +02003455 if (module->deviation[i].orig_node) {
3456 target_mod = lys_node_module(module->deviation[i].orig_node);
3457 } else {
3458 target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1,
3459 strcspn(module->deviation[i].target_name, ":") - 1);
Radek Krejci0fa54e92016-09-14 14:01:05 +02003460 target_mod = (struct lys_module *)lys_get_implemented_module(target_mod);
Pavol Vican64d0b762016-08-25 10:44:59 +02003461 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003462 lys_switch_deviation(&module->deviation[i], module);
3463
Michal Vasko89563fc2016-07-28 16:19:35 +02003464 /* clear the deviation flag if possible */
3465 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3466 if ((mod == module) || (mod == target_mod)) {
3467 continue;
3468 }
3469
3470 for (j = 0; j < mod->deviation_size; ++j) {
3471 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
3472 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
3473 /* some other module deviation targets the inspected module, flag remains */
3474 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003475 }
3476 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003477
3478 if (j < mod->deviation_size) {
3479 break;
3480 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003481 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003482
3483 if (!mod) {
3484 target_mod->deviated = 0;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003485 }
3486 }
3487
3488 /* remove applied augments */
3489 for (i = 0; i < module->augment_size; ++i) {
Radek Krejcidbc15262016-06-16 14:58:29 +02003490 if (!module->augment[i].target) {
3491 /* skip not resolved augments */
3492 continue;
3493 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003494
3495 elem = module->augment[i].child;
3496 if (elem) {
3497 LY_TREE_FOR(elem, last) {
3498 if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) {
3499 break;
3500 }
3501 }
3502 /* elem is first augment child, last is the last child */
3503
3504 /* parent child ptr */
3505 if (module->augment[i].target->child == elem) {
3506 module->augment[i].target->child = last->next;
3507 }
3508
3509 /* parent child next ptr */
3510 if (elem->prev->next) {
3511 elem->prev->next = last->next;
3512 }
3513
3514 /* parent child prev ptr */
3515 if (last->next) {
3516 last->next->prev = elem->prev;
3517 } else if (module->augment[i].target->child) {
3518 module->augment[i].target->child->prev = elem->prev;
3519 }
3520
3521 /* update augment children themselves */
3522 elem->prev = last;
3523 last->next = NULL;
3524 }
Michal Vaskob8f71322016-05-03 11:39:56 +02003525
3526 /* needs to be NULL for lys_augment_free() to free the children */
3527 module->augment[i].target = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003528 }
3529}
3530
Radek Krejci27fe55e2016-09-13 17:13:35 +02003531static int
3532lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
3533{
3534 struct lys_node *root, *next, *node;
3535 uint8_t i;
3536
3537 for (i = 0; i < module->augment_size; i++) {
3538 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003539 if (!module->augment[i].target
3540 && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003541 return -1;
3542 }
3543 }
3544 LY_TREE_FOR(module->data, root) {
3545 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
3546 LY_TREE_DFS_BEGIN(root, next, node) {
3547 if (node->nodetype == LYS_GROUPING) {
3548 goto nextsibling;
3549 }
3550 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3551 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
3552 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
3553 UNRES_TYPE_LEAFREF, node) == -1) {
3554 return EXIT_FAILURE;
3555 }
3556 }
3557 }
3558
3559 /* modified LY_TREE_DFS_END */
3560 next = node->child;
3561 /* child exception for leafs, leaflists and anyxml without children */
3562 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
3563 next = NULL;
3564 }
3565 if (!next) {
3566nextsibling:
3567 /* no children */
3568 if (node == root) {
3569 /* we are done, root has no children */
3570 break;
3571 }
3572 /* try siblings */
3573 next = node->next;
3574 }
3575 while (!next) {
3576 /* parent is already processed, go to its sibling */
3577 node = lys_parent(node);
3578 /* no siblings, go back through parents */
3579 if (lys_parent(node) == lys_parent(root)) {
3580 /* we are done, no next element to process */
3581 break;
3582 }
3583 next = node->next;
3584 }
3585 }
3586 }
3587
3588 return EXIT_SUCCESS;
3589}
3590
3591API int
3592lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02003593{
3594 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003595 struct unres_schema *unres;
3596 int i, j;
Michal Vasko26055752016-05-03 11:36:31 +02003597
Radek Krejci27fe55e2016-09-13 17:13:35 +02003598 if (!module) {
3599 ly_errno = LY_EINVAL;
3600 return EXIT_FAILURE;
3601 }
3602
3603 module = lys_main_module(module);
Michal Vasko26055752016-05-03 11:36:31 +02003604 if (module->implemented) {
3605 return EXIT_SUCCESS;
3606 }
3607
3608 ctx = module->ctx;
3609
3610 for (i = 0; i < ctx->models.used; ++i) {
3611 if (module == ctx->models.list[i]) {
3612 continue;
3613 }
3614
3615 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
3616 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
3617 return EXIT_FAILURE;
3618 }
3619 }
3620
Radek Krejci27fe55e2016-09-13 17:13:35 +02003621 unres = calloc(1, sizeof *unres);
3622 if (!unres) {
3623 LOGMEM;
3624 return EXIT_FAILURE;
3625 }
3626 /* recursively make the module implemented */
3627 ((struct lys_module *)module)->implemented = 1;
3628 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
3629 goto error;
3630 }
3631 /* process augments in submodules */
3632 for (i = 0; i < module->inc_size; ++i) {
3633 if (!module->inc[i].submodule) {
3634 continue;
3635 }
3636 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
3637 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003638 if (!module->inc[i].submodule->augment[i].target
3639 && (unres_schema_add_node((struct lys_module *)module->inc[i].submodule, unres,
3640 &module->inc[i].submodule->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003641 goto error;
3642 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003643 }
3644 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003645 /* resolve rest of unres items */
3646 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
3647 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003648 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003649 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02003650
3651 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003652
3653error:
3654 ((struct lys_module *)module)->implemented = 0;
3655 unres_schema_free((struct lys_module *)module, &unres);
3656 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003657}
3658
3659void
3660lys_submodule_module_data_free(struct lys_submodule *submodule)
3661{
3662 struct lys_node *next, *elem;
3663
3664 /* remove parsed data */
3665 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3666 if (elem->module == (struct lys_module *)submodule) {
3667 lys_node_free(elem, NULL, 0);
3668 }
3669 }
3670}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02003671
3672int
3673lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
3674{
3675 uint8_t i;
3676
3677 for (i = 0; i < list->keys_size; i++) {
3678 if (list->keys[i] == leaf) {
3679 return i + 1;
3680 }
3681 }
3682
3683 return 0;
3684}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02003685
3686API char *
3687lys_path(const struct lys_node *node)
3688{
3689 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
3690 uint16_t index = LY_BUF_SIZE - 1;
3691
3692 if (!node) {
3693 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
3694 return NULL;
3695 }
3696
3697 /* backup the shared internal buffer */
3698 if (ly_buf_used && buf[0]) {
3699 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
3700 }
3701 ly_buf_used++;
3702
3703 /* build the path */
3704 buf[index] = '\0';
3705 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index);
3706 result = strdup(&buf[index]);
3707
3708 /* restore the shared internal buffer */
3709 if (buf_backup) {
3710 strcpy(buf, buf_backup);
3711 free(buf_backup);
3712 }
3713 ly_buf_used--;
3714
3715 return result;
3716}
Radek Krejci9ad23f42016-10-31 15:46:52 +01003717