blob: eb6481a7589dc2287730ae69e00eb957d5eeba67 [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;
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100141 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Radek Krejcic071c542016-01-27 14:57:51 +0100142 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 Vasko0f99d3e2017-01-10 10:50:40 +0100163lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, int nam_len,
164 LYS_NODE type, 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 */
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100190 if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) {
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 }
Radek Krejcifec2e142017-01-05 15:19:03 +0100348
349 if (parent->flags & LYS_NOTAPPLIED) {
350 /* data are not connected in the target, so we cannot continue with the target as a parent */
351 parent = NULL;
352 } else {
353 /* data are connected in target, so we will continue with the target as a parent */
354 parent = ((struct lys_node_augment *)parent)->target;
355 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200356 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200357
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200358 /* unlink from parent */
359 if (parent) {
360 if (parent->child == node) {
361 parent->child = node->next;
362 }
363 node->parent = NULL;
364 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200365
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200366 /* unlink from siblings */
367 if (node->prev == node) {
368 /* there are no more siblings */
369 return;
370 }
371 if (node->next) {
372 node->next->prev = node->prev;
373 } else {
374 /* unlinking the last element */
375 if (parent) {
376 first = parent->child;
377 } else {
378 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200379 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200380 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200381 }
382 }
383 first->prev = node->prev;
384 }
385 if (node->prev->next) {
386 node->prev->next = node->next;
387 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200388
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200389 /* clean up the unlinked element */
390 node->next = NULL;
391 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200392}
393
Michal Vasko563ef092015-09-04 13:17:23 +0200394struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100395lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200396{
397 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200398
399 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200400 /* top-level augment, look into module (uses augment is handled correctly below) */
401 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
402 par_iter = par_iter->parent->module->data;
403 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200404 break;
405 }
406 }
407
Michal Vasko6f929da2015-10-02 16:23:25 +0200408 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200409 continue;
410 }
411
412 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
413 if (!stop) {
414 stop = par_iter;
415 } else if (iter == stop) {
416 break;
417 }
418 if (iter->nodetype != LYS_GROUPING) {
419 continue;
420 }
421
Radek Krejcif8426a72015-10-31 23:14:03 +0100422 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200423 return (struct lys_node_grp *)iter;
424 }
425 }
426 }
427
Michal Vasko563ef092015-09-04 13:17:23 +0200428 return NULL;
429}
430
Radek Krejci10c760e2015-08-14 14:45:43 +0200431/*
432 * get next grouping in the root's subtree, in the
433 * first call, tha last is NULL
434 */
435static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200436lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200437{
Radek Krejci10c760e2015-08-14 14:45:43 +0200438 struct lys_node *last = (struct lys_node *)lastgrp;
439 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200440
Radek Krejci10c760e2015-08-14 14:45:43 +0200441 assert(root);
442
443 if (!last) {
444 last = root;
445 }
446
447 while (1) {
448 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
449 next = last->child;
450 } else {
451 next = NULL;
452 }
453 if (!next) {
454 if (last == root) {
455 /* we are done */
456 return NULL;
457 }
458
459 /* no children, go to siblings */
460 next = last->next;
461 }
462 while (!next) {
463 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100464 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200465 /* we are done */
466 return NULL;
467 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200468 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100469 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200470 }
471
472 if (next->nodetype == LYS_GROUPING) {
473 return (struct lys_node_grp *)next;
474 }
475
476 last = next;
477 }
478}
479
Michal Vasko0d343d12015-08-24 14:57:36 +0200480/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200481int
Radek Krejci07911992015-08-14 15:13:31 +0200482lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
483{
Michal Vasko563ef092015-09-04 13:17:23 +0200484 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200485 struct lys_node_grp *grp;
486 int down;
487
488 assert(node);
489
490 if (!parent) {
491 assert(module);
492 } else {
493 module = parent->module;
494 }
495
496 switch (node->nodetype) {
497 case LYS_GROUPING:
498 /* 6.2.1, rule 6 */
499 if (parent) {
500 if (parent->child) {
501 down = 1;
502 start = parent->child;
503 } else {
504 down = 0;
505 start = parent;
506 }
507 } else {
508 down = 1;
509 start = module->data;
510 }
511 /* go up */
Radek Krejcic071c542016-01-27 14:57:51 +0100512 if (lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100513 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200514 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200515 }
516 /* go down, because grouping can be defined after e.g. container in which is collision */
517 if (down) {
518 for (iter = start, stop = NULL; iter; iter = iter->prev) {
519 if (!stop) {
520 stop = start;
521 } else if (iter == stop) {
522 break;
523 }
524 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
525 continue;
526 }
527
528 grp = NULL;
529 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100530 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100531 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200532 return EXIT_FAILURE;
533 }
534 }
535 }
536 }
537 break;
538 case LYS_LEAF:
539 case LYS_LEAFLIST:
540 case LYS_LIST:
541 case LYS_CONTAINER:
542 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200543 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200544 /* 6.2.1, rule 7 */
545 if (parent) {
546 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200547 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
548 if (iter->nodetype == LYS_AUGMENT) {
549 if (((struct lys_node_augment *)iter)->target) {
550 /* augment is resolved, go up */
551 iter = ((struct lys_node_augment *)iter)->target;
552 continue;
553 }
554 /* augment is not resolved, this is the final parent */
555 break;
556 }
Radek Krejci07911992015-08-14 15:13:31 +0200557 iter = iter->parent;
558 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200559
Radek Krejci07911992015-08-14 15:13:31 +0200560 if (!iter) {
561 stop = NULL;
562 iter = module->data;
563 } else {
564 stop = iter;
565 iter = iter->child;
566 }
567 } else {
568 stop = NULL;
569 iter = module->data;
570 }
571 while (iter) {
572 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
573 iter = iter->child;
574 continue;
575 }
576
Radek Krejcibf2abff2016-08-23 15:51:52 +0200577 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100578 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100579 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200580 return EXIT_FAILURE;
581 }
582 }
583
584 /* special case for choice - we must check the choice's name as
585 * well as the names of nodes under the choice
586 */
587 if (iter->nodetype == LYS_CHOICE) {
588 iter = iter->child;
589 continue;
590 }
591
592 /* go to siblings */
593 if (!iter->next) {
594 /* no sibling, go to parent's sibling */
595 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200596 /* for parent LYS_AUGMENT */
597 if (iter->parent == stop) {
598 iter = stop;
599 break;
600 }
601 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200602 if (iter && iter->next) {
603 break;
604 }
605 } while (iter != stop);
606
607 if (iter == stop) {
608 break;
609 }
610 }
611 iter = iter->next;
612 }
613 break;
614 case LYS_CASE:
615 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100616 if (parent) {
617 start = parent->child;
618 } else {
619 start = module->data;
620 }
621
622 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200623 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200624 continue;
625 }
626
Radek Krejci749190d2016-02-18 16:26:25 +0100627 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100628 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200629 return EXIT_FAILURE;
630 }
631 }
632 break;
633 default:
634 /* no check needed */
635 break;
636 }
637
638 return EXIT_SUCCESS;
639}
640
Michal Vasko0d343d12015-08-24 14:57:36 +0200641/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200642int
Radek Krejci10c760e2015-08-14 14:45:43 +0200643lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
644{
Radek Krejci92720552015-10-05 15:28:27 +0200645 struct lys_node *iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200646 struct lys_node_inout *in, *out, *inout;
Radek Krejci07911992015-08-14 15:13:31 +0200647 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200648
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200649 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200650
Radek Krejci10c760e2015-08-14 14:45:43 +0200651 if (parent) {
652 type = parent->nodetype;
653 module = parent->module;
654 } else {
655 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100656 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200657 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200658 }
659
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200660 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200661 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200662 case LYS_CONTAINER:
663 case LYS_LIST:
664 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100665 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200666 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200667 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200668 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200669 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
670 return EXIT_FAILURE;
671 }
672 break;
Radek Krejci76512572015-08-04 09:47:08 +0200673 case LYS_INPUT:
674 case LYS_OUTPUT:
675 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200676 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200677 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200678 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100679 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200680 return EXIT_FAILURE;
681 }
682 break;
Radek Krejci76512572015-08-04 09:47:08 +0200683 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200684 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200685 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100686 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200687 return EXIT_FAILURE;
688 }
689 break;
Radek Krejci76512572015-08-04 09:47:08 +0200690 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200691 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200692 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100693 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694 return EXIT_FAILURE;
695 }
696 break;
Radek Krejci76512572015-08-04 09:47:08 +0200697 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200698 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200699 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100700 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200701 return EXIT_FAILURE;
702 }
703 break;
Radek Krejci76512572015-08-04 09:47:08 +0200704 case LYS_LEAF:
705 case LYS_LEAFLIST:
706 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200707 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100708 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vasko51e5c582017-01-19 14:16:39 +0100709 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100710 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200711 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200712 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200713 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200714 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200715 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100716 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200717 return EXIT_FAILURE;
718 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200719 break;
720 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200721 /* top level */
722 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200723 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200724 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100725 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200726 return EXIT_FAILURE;
727 }
728
Radek Krejcic071c542016-01-27 14:57:51 +0100729 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200730 }
731
732 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200733 if (lys_check_id(child, parent, module)) {
734 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200735 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200736
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200737 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200738 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200739 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200740
Radek Krejci41a349b2016-10-24 19:21:59 +0200741 if (child->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
742 /* replace the implicit input/output node */
743 if (child->nodetype == LYS_OUTPUT) {
744 inout = (struct lys_node_inout *)parent->child->next;
745 } else { /* LYS_INPUT */
746 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200747 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200748 }
749 if (inout->next) {
750 child->next = inout->next;
751 inout->next->prev = child;
752 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200753 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200754 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200755 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200756 child->prev = inout->prev;
757 if (inout->prev->next) {
758 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200759 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200760 inout->prev = (struct lys_node *)inout;
761 child->parent = parent;
762 inout->parent = NULL;
763 lys_node_free((struct lys_node *)inout, NULL, 0);
764 } else {
765 /* connect the child correctly */
766 if (!parent) {
767 if (module->data) {
768 module->data->prev->next = child;
769 child->prev = module->data->prev;
770 module->data->prev = child;
771 } else {
772 module->data = child;
773 }
774 } else {
775 if (!parent->child) {
776 /* the only/first child of the parent */
777 parent->child = child;
778 child->parent = parent;
779 iter = child;
780 } else {
781 /* add a new child at the end of parent's child list */
782 iter = parent->child->prev;
783 iter->next = child;
784 child->prev = iter;
785 }
786 while (iter->next) {
787 iter = iter->next;
788 iter->parent = parent;
789 }
790 parent->child->prev = iter;
791 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200792 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200793
Michal Vaskoe022a562016-09-27 14:24:15 +0200794 /* check config value (but ignore them in groupings and augments) */
795 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent);
796 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200797 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
798 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
799 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +0100800 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200801 return EXIT_FAILURE;
802 }
803 }
804
Radek Krejci41771502016-04-14 17:52:32 +0200805 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200806 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200807 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200808 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200809 /* store it only into container or list - the only data inner nodes */
810 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
811 if (iter->flags & LYS_INCL_STATUS) {
812 /* done, someone else set it already from here */
813 break;
814 }
815 /* set flag about including status data */
816 iter->flags |= LYS_INCL_STATUS;
817 }
818 }
819 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200820
821 /* create implicit input/output nodes to have available them as possible target for augment */
822 if (child->nodetype & (LYS_RPC | LYS_ACTION)) {
823 in = calloc(1, sizeof *in);
824 in->nodetype = LYS_INPUT;
825 in->name = lydict_insert(child->module->ctx, "input", 5);
826 out = calloc(1, sizeof *out);
827 out->name = lydict_insert(child->module->ctx, "output", 6);
828 out->nodetype = LYS_OUTPUT;
829 in->module = out->module = child->module;
830 in->parent = out->parent = child;
831 in->flags = out->flags = LYS_IMPLICIT;
832 in->next = (struct lys_node *)out;
833 in->prev = (struct lys_node *)out;
834 out->prev = (struct lys_node *)in;
835 child->child = (struct lys_node *)in;
836 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200837 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200838}
839
Radek Krejcia1df1682016-04-11 14:56:59 +0200840static const struct lys_module *
841lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200842{
Radek Krejcia1df1682016-04-11 14:56:59 +0200843 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200844 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200845 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200846
Radek Krejci00a0e712016-10-26 10:24:46 +0200847 ly_err_clean(1);
Radek Krejcif347abc2016-06-22 10:18:47 +0200848
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200849 if (!ctx || !data) {
850 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
851 return NULL;
852 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200853
Radek Krejcia1df1682016-04-11 14:56:59 +0200854 if (!internal && format == LYS_IN_YANG) {
855 /* enlarge data by 2 bytes for flex */
856 len = strlen(data);
857 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
858 if (!enlarged_data) {
859 LOGMEM;
860 return NULL;
861 }
862 memcpy(enlarged_data, data, len);
863 enlarged_data[len] = enlarged_data[len + 1] = '\0';
864 data = enlarged_data;
865 }
866
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200867 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200868 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +0100869 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200870 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200871 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100872 mod = yang_read_module(ctx, data, 0, NULL, 1);
873 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200874 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200875 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200876 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200877 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200878
Radek Krejcia1df1682016-04-11 14:56:59 +0200879 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200880 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200881}
882
Radek Krejcia1df1682016-04-11 14:56:59 +0200883API const struct lys_module *
884lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
885{
886 return lys_parse_mem_(ctx, data, format, 0);
887}
888
Michal Vasko5a721fd2016-02-16 12:16:48 +0100889struct lys_submodule *
890lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200891{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100892 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200893
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200894 assert(module);
895 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +0200896
Radek Krejcic071c542016-01-27 14:57:51 +0100897 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +0200898 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200899
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200900 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200901 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +0100902 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200903 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200904 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100905 submod = yang_read_submodule(module, data, 0, unres);
906 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200907 default:
Radek Krejci90a550a2016-04-13 16:00:58 +0200908 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200909 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200910 }
Radek Krejciefaeba32015-05-27 14:30:57 +0200911
Michal Vasko5a721fd2016-02-16 12:16:48 +0100912 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +0200913}
914
Michal Vasko1e62a092015-12-01 12:27:20 +0100915API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +0100916lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
917{
918 int fd;
919 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +0200920 const char *rev, *dot, *filename;
921 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +0100922
923 if (!ctx || !path) {
924 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
925 return NULL;
926 }
927
928 fd = open(path, O_RDONLY);
929 if (fd == -1) {
930 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
931 return NULL;
932 }
933
934 ret = lys_parse_fd(ctx, fd, format);
935 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100936
Radek Krejcid80c8602016-10-25 11:56:03 +0200937 if (!ret) {
938 /* error */
939 return NULL;
940 }
941
942 /* check that name and revision match filename */
943 filename = strrchr(path, '/');
944 if (!filename) {
945 filename = path;
946 } else {
947 filename++;
948 }
949 rev = strchr(filename, '@');
950 dot = strrchr(filename, '.');
951
952 /* name */
953 len = strlen(ret->name);
954 if (strncmp(filename, ret->name, len) ||
955 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejcic0c82302016-10-25 14:21:33 +0200956 LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +0200957 }
958 if (rev) {
959 len = dot - ++rev;
960 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
961 LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename,
962 ret->rev_size ? ret->rev[0].date : "none");
963 }
964 }
965
966 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100967 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100968 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100969 }
970
Michal Vasko662610a2015-12-07 11:25:45 +0100971 return ret;
972}
973
974API const struct lys_module *
975lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +0200976{
Michal Vasko1e62a092015-12-01 12:27:20 +0100977 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +0200978 struct stat sb;
979 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +0100980 char buf[PATH_MAX];
981 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +0200982
983 if (!ctx || fd < 0) {
984 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
985 return NULL;
986 }
987
Radek Krejci10a833c2015-12-16 15:28:37 +0100988 if (fstat(fd, &sb) == -1) {
989 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
990 return NULL;
991 }
Radek Krejcib051f722016-02-25 15:12:21 +0100992 if (!S_ISREG(sb.st_mode)) {
993 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
994 return NULL;
995 }
996
Michal Vasko164d9012016-04-01 10:16:59 +0200997 if (!sb.st_size) {
998 LOGERR(LY_EINVAL, "File empty.");
999 return NULL;
1000 }
1001
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001002 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001003 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +01001004 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001005 return NULL;
1006 }
Radek Krejcia1df1682016-04-11 14:56:59 +02001007 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001008 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001009
Radek Krejcia77904e2016-02-25 16:23:45 +01001010 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001011 /* get URI if there is /proc */
1012 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001013 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1014 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1015 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1016 }
1017 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001018 }
Radek Krejcib051f722016-02-25 15:12:21 +01001019 }
1020
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001021 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001022}
1023
Michal Vasko5a721fd2016-02-16 12:16:48 +01001024struct lys_submodule *
1025lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001026{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001027 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001028 struct stat sb;
1029 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001030
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001031 assert(module);
1032 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001033
Radek Krejci10a833c2015-12-16 15:28:37 +01001034 if (fstat(fd, &sb) == -1) {
1035 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +01001036 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +01001037 }
Michal Vasko164d9012016-04-01 10:16:59 +02001038
1039 if (!sb.st_size) {
1040 LOGERR(LY_EINVAL, "File empty.");
1041 return NULL;
1042 }
1043
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001044 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001045 if (addr == MAP_FAILED) {
1046 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001047 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001048 }
Michal Vasko5a721fd2016-02-16 12:16:48 +01001049 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001050 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +02001051
Michal Vasko5a721fd2016-02-16 12:16:48 +01001052 return submodule;
1053
Radek Krejciefaeba32015-05-27 14:30:57 +02001054}
1055
Radek Krejci1d82ef62015-08-07 14:44:40 +02001056static struct lys_restr *
1057lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001058{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001059 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001060 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001061
Radek Krejci3733a802015-06-19 13:43:21 +02001062 if (!size) {
1063 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001064 }
Radek Krejci3733a802015-06-19 13:43:21 +02001065
1066 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001067 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001068 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001069 return NULL;
1070 }
Radek Krejci3733a802015-06-19 13:43:21 +02001071 for (i = 0; i < size; i++) {
1072 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1073 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1074 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1075 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1076 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1077 }
1078
1079 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001080}
1081
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001082void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001083lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001084{
1085 assert(ctx);
1086 if (!restr) {
1087 return;
1088 }
1089
1090 lydict_remove(ctx, restr->expr);
1091 lydict_remove(ctx, restr->dsc);
1092 lydict_remove(ctx, restr->ref);
1093 lydict_remove(ctx, restr->eapptag);
1094 lydict_remove(ctx, restr->emsg);
1095}
1096
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001097static void
1098lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
1099{
1100 uint8_t i;
1101
1102 for (i = 0; i < iffeature_size; ++i) {
1103 free(iffeature[i].expr);
1104 free(iffeature[i].features);
1105 }
1106 free(iffeature);
1107}
1108
Michal Vaskob84f88a2015-09-24 13:16:10 +02001109static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001110type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001111 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001112{
1113 int i;
1114
1115 switch (base) {
1116 case LY_TYPE_BINARY:
1117 if (old->info.binary.length) {
1118 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1119 }
1120 break;
1121
1122 case LY_TYPE_BITS:
1123 new->info.bits.count = old->info.bits.count;
1124 if (new->info.bits.count) {
1125 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1126 if (!new->info.bits.bit) {
1127 LOGMEM;
1128 return -1;
1129 }
1130 for (i = 0; i < new->info.bits.count; i++) {
1131 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1132 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1133 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1134 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1135 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1136 }
1137 }
1138 break;
1139
1140 case LY_TYPE_DEC64:
1141 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001142 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001143 if (old->info.dec64.range) {
1144 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1145 }
1146 break;
1147
1148 case LY_TYPE_ENUM:
1149 new->info.enums.count = old->info.enums.count;
1150 if (new->info.enums.count) {
1151 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1152 if (!new->info.enums.enm) {
1153 LOGMEM;
1154 return -1;
1155 }
1156 for (i = 0; i < new->info.enums.count; i++) {
1157 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1158 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1159 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1160 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1161 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1162 }
1163 }
1164 break;
1165
1166 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001167 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001168 if (old->info.ident.count) {
1169 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1170 if (!new->info.ident.ref) {
1171 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001172 return -1;
1173 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001174 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1175 } else {
1176 /* there can be several unresolved base identities, duplicate them all */
1177 i = -1;
1178 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1179 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1180 return -1;
1181 }
1182 --i;
1183 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001184 }
1185 break;
1186
1187 case LY_TYPE_INST:
1188 new->info.inst.req = old->info.inst.req;
1189 break;
1190
1191 case LY_TYPE_INT8:
1192 case LY_TYPE_INT16:
1193 case LY_TYPE_INT32:
1194 case LY_TYPE_INT64:
1195 case LY_TYPE_UINT8:
1196 case LY_TYPE_UINT16:
1197 case LY_TYPE_UINT32:
1198 case LY_TYPE_UINT64:
1199 if (old->info.num.range) {
1200 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1201 }
1202 break;
1203
1204 case LY_TYPE_LEAFREF:
1205 if (old->info.lref.path) {
1206 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001207 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001208 return -1;
1209 }
1210 }
1211 break;
1212
1213 case LY_TYPE_STRING:
1214 if (old->info.str.length) {
1215 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1216 }
1217 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1218 new->info.str.pat_count = old->info.str.pat_count;
1219 break;
1220
1221 case LY_TYPE_UNION:
1222 new->info.uni.count = old->info.uni.count;
1223 if (new->info.uni.count) {
1224 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1225 if (!new->info.uni.types) {
1226 LOGMEM;
1227 return -1;
1228 }
1229 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001230 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 +02001231 return -1;
1232 }
1233 }
1234 }
1235 break;
1236
1237 default:
1238 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1239 break;
1240 }
1241 return EXIT_SUCCESS;
1242}
1243
1244struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001245lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1246 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001247{
1248 struct yang_type *new;
1249
1250 new = calloc(1, sizeof *new);
1251 if (!new) {
1252 LOGMEM;
1253 return NULL;
1254 }
1255 new->flags = old->flags;
1256 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001257 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001258 new->type = type;
1259 if (!new->name) {
1260 LOGMEM;
1261 goto error;
1262 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001263 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001264 new->type->base = new->base;
1265 lys_type_free(module->ctx, new->type);
1266 memset(&new->type->info, 0, sizeof new->type->info);
1267 goto error;
1268 }
1269 return new;
1270
1271 error:
1272 free(new);
1273 return NULL;
1274}
1275
1276static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001277lys_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 +02001278 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001279{
1280 int i;
1281
Michal Vasko1dca6882015-10-22 14:29:42 +02001282 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001283 new->base = old->base;
1284 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001285 new->parent = (struct lys_tpdf *)parent;
Radek Krejci3733a802015-06-19 13:43:21 +02001286
Michal Vasko878e38d2016-09-05 12:17:53 +02001287 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001288 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001289 /* HACK (serious one) for unres */
1290 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001291 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001292 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 +02001293 } else {
1294 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1295 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001296 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001297 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001298 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001299 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001300 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001301 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001302 }
1303
Radek Krejci3a5501d2016-07-18 22:03:34 +02001304 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001305}
1306
1307void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001308lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001309{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001310 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001311
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001312 assert(ctx);
1313 if (!type) {
1314 return;
1315 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001316
Michal Vasko1dca6882015-10-22 14:29:42 +02001317 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001318
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001319 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001320 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001321 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001322 free(type->info.binary.length);
1323 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001324 case LY_TYPE_BITS:
1325 for (i = 0; i < type->info.bits.count; i++) {
1326 lydict_remove(ctx, type->info.bits.bit[i].name);
1327 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1328 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001329 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001330 }
1331 free(type->info.bits.bit);
1332 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001333
1334 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001335 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001336 free(type->info.dec64.range);
1337 break;
1338
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001339 case LY_TYPE_ENUM:
1340 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001341 lydict_remove(ctx, type->info.enums.enm[i].name);
1342 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1343 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001344 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001345 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001346 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001347 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001348
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001349 case LY_TYPE_INT8:
1350 case LY_TYPE_INT16:
1351 case LY_TYPE_INT32:
1352 case LY_TYPE_INT64:
1353 case LY_TYPE_UINT8:
1354 case LY_TYPE_UINT16:
1355 case LY_TYPE_UINT32:
1356 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001357 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001358 free(type->info.num.range);
1359 break;
1360
Radek Krejcidc4c1412015-06-19 15:39:54 +02001361 case LY_TYPE_LEAFREF:
1362 lydict_remove(ctx, type->info.lref.path);
1363 break;
1364
Radek Krejci3733a802015-06-19 13:43:21 +02001365 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001366 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001367 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001368 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001369 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001370 }
1371 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001372 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001373
Radek Krejcie4c366b2015-07-02 10:11:31 +02001374 case LY_TYPE_UNION:
1375 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001376 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001377 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001378 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001379 break;
1380
Michal Vaskod3282192016-09-05 11:27:57 +02001381 case LY_TYPE_IDENT:
1382 free(type->info.ident.ref);
1383 break;
1384
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001385 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001386 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001387 break;
1388 }
Radek Krejci5a065542015-05-22 15:02:07 +02001389}
1390
Radek Krejci1d82ef62015-08-07 14:44:40 +02001391static void
1392lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001393{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001394 assert(ctx);
1395 if (!tpdf) {
1396 return;
1397 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001398
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001399 lydict_remove(ctx, tpdf->name);
1400 lydict_remove(ctx, tpdf->dsc);
1401 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001402
Radek Krejci1d82ef62015-08-07 14:44:40 +02001403 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001404
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001405 lydict_remove(ctx, tpdf->units);
1406 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001407}
1408
Michal Vaskob84f88a2015-09-24 13:16:10 +02001409static struct lys_tpdf *
1410lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1411{
1412 struct lys_tpdf *result;
1413 int i, j;
1414
1415 if (!size) {
1416 return NULL;
1417 }
1418
1419 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001420 if (!result) {
1421 LOGMEM;
1422 return NULL;
1423 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001424 for (i = 0; i < size; i++) {
1425 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1426 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1427 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1428 result[i].flags = old[i].flags;
1429 result[i].module = old[i].module;
1430
Radek Krejci3a5501d2016-07-18 22:03:34 +02001431 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001432 for (j = 0; j <= i; ++j) {
1433 lys_tpdf_free(mod->ctx, &result[j]);
1434 }
1435 free(result);
1436 return NULL;
1437 }
1438
1439 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1440 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1441 }
1442
1443 return result;
1444}
1445
Radek Krejci1d82ef62015-08-07 14:44:40 +02001446static struct lys_when *
1447lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001448{
Radek Krejci76512572015-08-04 09:47:08 +02001449 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001450
1451 if (!old) {
1452 return NULL;
1453 }
1454
1455 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001456 if (!new) {
1457 LOGMEM;
1458 return NULL;
1459 }
Radek Krejci00768f42015-06-18 17:04:04 +02001460 new->cond = lydict_insert(ctx, old->cond, 0);
1461 new->dsc = lydict_insert(ctx, old->dsc, 0);
1462 new->ref = lydict_insert(ctx, old->ref, 0);
1463
1464 return new;
1465}
1466
Michal Vasko0308dd62015-10-07 09:14:40 +02001467void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001468lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001469{
1470 if (!w) {
1471 return;
1472 }
1473
1474 lydict_remove(ctx, w->cond);
1475 lydict_remove(ctx, w->dsc);
1476 lydict_remove(ctx, w->ref);
1477
1478 free(w);
1479}
1480
Radek Krejcib7f5e412015-08-13 10:15:51 +02001481static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001482lys_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 +02001483{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001484 struct lys_node *next, *sub;
1485
Radek Krejcic071c542016-01-27 14:57:51 +01001486 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01001487 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001488 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001489 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001490 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001491 }
1492
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001493 lydict_remove(ctx, aug->target_name);
1494 lydict_remove(ctx, aug->dsc);
1495 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001496
Radek Krejci9ff0a922016-07-14 13:08:05 +02001497 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001498
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001499 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001500}
1501
Radek Krejci76512572015-08-04 09:47:08 +02001502static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001503lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001504{
Radek Krejci76512572015-08-04 09:47:08 +02001505 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001506 struct lys_node *old_child, *new_child;
1507 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001508
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001509 if (!size) {
1510 return NULL;
1511 }
Radek Krejci106efc02015-06-10 14:36:27 +02001512
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001513 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001514 if (!new) {
1515 LOGMEM;
1516 return NULL;
1517 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001518 for (i = 0; i < size; i++) {
1519 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1520 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1521 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1522 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001523 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001524 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001525
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001526 /* this must succeed, it was already resolved once */
Radek Krejcidf46e222016-11-08 11:57:37 +01001527 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, 1,
Michal Vasko3edeaf72016-02-11 13:17:43 +01001528 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001529 LOGINT;
1530 free(new);
1531 return NULL;
1532 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001533 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001534
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001535 /* Correct the augment nodes.
1536 * This function can only be called from lys_node_dup() with uses
1537 * being the node duplicated, so we must have a case of grouping
1538 * with a uses with augments. The augmented nodes have already been
1539 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001540 * (it was set to their actual data parent, not an augment), and
1541 * the new augment does not have child pointer to its augment nodes,
1542 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001543 */
1544 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001545 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001546 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001547 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001548 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001549 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001550 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001551 LY_TREE_FOR(old[i].child, old_child) {
1552 /* all augment nodes were connected as siblings, there can be no more after this */
1553 if (old_child->parent != (struct lys_node *)&old[i]) {
1554 break;
1555 }
1556
Radek Krejci749190d2016-02-18 16:26:25 +01001557 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001558
1559 new_child->parent = (struct lys_node *)&new[i];
1560 new_child = new_child->next;
1561 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001562 }
Radek Krejci106efc02015-06-10 14:36:27 +02001563
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001564 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001565}
1566
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001567static const char **
1568lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1569{
1570 int i;
1571 const char **result;
1572
1573 if (!size) {
1574 return NULL;
1575 }
1576
1577 result = calloc(size, sizeof *result);
1578 if (!result) {
1579 LOGMEM;
1580 return NULL;
1581 }
1582
1583 for (i = 0; i < size; i++) {
1584 result[i] = lydict_insert(ctx, old[i], 0);
1585 }
1586 return result;
1587}
1588
Radek Krejci76512572015-08-04 09:47:08 +02001589static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001590lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001591{
Radek Krejci76512572015-08-04 09:47:08 +02001592 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001593 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001594
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001595 if (!size) {
1596 return NULL;
1597 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001598
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001599 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001600 if (!result) {
1601 LOGMEM;
1602 return NULL;
1603 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001604 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001605 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001606 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1607 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001608 result[i].flags = old[i].flags;
1609 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001610
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001611 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001612 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001613
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001614 result[i].dflt_size = old[i].dflt_size;
1615 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
1616
1617 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001618 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001619 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001620 result[i].mod.list = old[i].mod.list;
1621 }
1622 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001623
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001624 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001625}
1626
Radek Krejci1d82ef62015-08-07 14:44:40 +02001627static void
1628lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001629{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001630 assert(ctx);
1631 if (!ident) {
1632 return;
1633 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001634
Radek Krejci018f1f52016-08-03 16:01:20 +02001635 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02001636 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001637 lydict_remove(ctx, ident->name);
1638 lydict_remove(ctx, ident->dsc);
1639 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001640 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejci6793db02015-05-22 17:49:54 +02001641
1642}
1643
Radek Krejci1d82ef62015-08-07 14:44:40 +02001644static void
1645lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001646{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001647 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001648
Radek Krejcid12f57b2015-08-06 10:43:39 +02001649 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001650 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001651 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001652 }
1653 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001654}
1655
Radek Krejci1d82ef62015-08-07 14:44:40 +02001656static void
Michal Vasko44fb6382016-06-29 11:12:27 +02001657lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02001658{
1659 int i;
1660
1661 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1662 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001663 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001664 }
1665 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02001666
1667 for (i = 0; i < io->must_size; i++) {
1668 lys_restr_free(ctx, &io->must[i]);
1669 }
1670 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001671}
1672
Radek Krejci1d82ef62015-08-07 14:44:40 +02001673static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02001674lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
1675{
1676 int i;
1677
1678 for (i = 0; i < notif->must_size; i++) {
1679 lys_restr_free(ctx, &notif->must[i]);
1680 }
1681 free(notif->must);
1682
1683 for (i = 0; i < notif->tpdf_size; i++) {
1684 lys_tpdf_free(ctx, &notif->tpdf[i]);
1685 }
1686 free(notif->tpdf);
1687}
1688static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02001689lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001690{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001691 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001692
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001693 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001694 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001695 }
1696 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001697
Radek Krejci1d82ef62015-08-07 14:44:40 +02001698 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001699}
1700
Radek Krejci1d82ef62015-08-07 14:44:40 +02001701static void
1702lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001703{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001704 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001705
Radek Krejci85a54be2016-10-20 12:39:56 +02001706 /* leafref backlinks */
1707 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001708
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001709 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001710 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001711 }
1712 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001713
Radek Krejci1d82ef62015-08-07 14:44:40 +02001714 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001715
Radek Krejci1d82ef62015-08-07 14:44:40 +02001716 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001717 lydict_remove(ctx, leaf->units);
1718 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001719}
1720
Radek Krejci1d82ef62015-08-07 14:44:40 +02001721static void
1722lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001723{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001724 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001725
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001726 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001727 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001728 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001729 }
1730
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001731 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001732 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001733 }
1734 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001735
Pavol Vican38321d02016-08-16 14:56:02 +02001736 for (i = 0; i < llist->dflt_size; i++) {
1737 lydict_remove(ctx, llist->dflt[i]);
1738 }
1739 free(llist->dflt);
1740
Radek Krejci1d82ef62015-08-07 14:44:40 +02001741 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001742
Radek Krejci1d82ef62015-08-07 14:44:40 +02001743 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001744 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001745}
1746
Radek Krejci1d82ef62015-08-07 14:44:40 +02001747static void
1748lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001749{
Radek Krejci581ce772015-11-10 17:22:40 +01001750 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001751
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001752 /* handle only specific parts for LY_NODE_LIST */
1753 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001754 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001755 }
1756 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001757
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001758 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001759 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001760 }
1761 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001762
Radek Krejci1d82ef62015-08-07 14:44:40 +02001763 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001764
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001765 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02001766 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001767 lydict_remove(ctx, list->unique[i].expr[j]);
1768 }
1769 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001770 }
1771 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001772
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001773 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001774}
1775
Radek Krejci1d82ef62015-08-07 14:44:40 +02001776static void
1777lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001778{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001779 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001780
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001781 /* handle only specific parts for LY_NODE_CONTAINER */
1782 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001783
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001784 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001785 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001786 }
1787 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001788
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001789 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001790 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001791 }
1792 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001793
Radek Krejci1d82ef62015-08-07 14:44:40 +02001794 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001795}
1796
Radek Krejci1d82ef62015-08-07 14:44:40 +02001797static void
1798lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001799{
1800 lydict_remove(ctx, f->name);
1801 lydict_remove(ctx, f->dsc);
1802 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001803 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci9de2c042016-10-19 16:53:06 +02001804 ly_set_free(f->depfeatures);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001805}
1806
Radek Krejci1d82ef62015-08-07 14:44:40 +02001807static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001808lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001809{
Radek Krejci581ce772015-11-10 17:22:40 +01001810 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001811 struct ly_ctx *ctx;
1812 struct lys_node *next, *elem;
1813
1814 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001815
1816 lydict_remove(ctx, dev->target_name);
1817 lydict_remove(ctx, dev->dsc);
1818 lydict_remove(ctx, dev->ref);
1819
Pavol Vican64d0b762016-08-25 10:44:59 +02001820 if (!dev->deviate) {
1821 return ;
1822 }
1823
Michal Vaskoff006c12016-02-17 11:15:19 +01001824 /* the module was freed, but we only need the context from orig_node, use ours */
1825 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1826 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1827 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1828 elem->module = module;
1829
1830 LY_TREE_DFS_END(dev->orig_node, next, elem);
1831 }
1832 lys_node_free(dev->orig_node, NULL, 0);
1833 } else {
1834 /* it's just a shallow copy, freeing one node */
1835 dev->orig_node->module = module;
1836 lys_node_free(dev->orig_node, NULL, 1);
1837 }
1838
Radek Krejcieb00f512015-07-01 16:44:58 +02001839 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001840 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02001841 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02001842 }
1843 free(dev->deviate[i].dflt);
1844
Radek Krejcieb00f512015-07-01 16:44:58 +02001845 lydict_remove(ctx, dev->deviate[i].units);
1846
1847 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1848 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001849 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001850 }
1851 free(dev->deviate[i].must);
1852
1853 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001854 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1855 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1856 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001857 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001858 }
1859 free(dev->deviate[i].unique);
1860 }
1861 }
1862 free(dev->deviate);
1863}
1864
Radek Krejci1d82ef62015-08-07 14:44:40 +02001865static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001866lys_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 +02001867{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001868 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001869
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001870 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001871 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001872 lydict_remove(ctx, uses->refine[i].dsc);
1873 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001874
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001875 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001876 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001877 }
1878 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001879
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001880 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02001881 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001882 }
1883 free(uses->refine[i].dflt);
1884
1885 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001886 lydict_remove(ctx, uses->refine[i].mod.presence);
1887 }
1888 }
1889 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001890
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001891 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001892 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001893 }
1894 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001895
Radek Krejci1d82ef62015-08-07 14:44:40 +02001896 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001897}
1898
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001899void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001900lys_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 +02001901{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001902 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001903 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001904
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001905 if (!node) {
1906 return;
1907 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001908
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001909 assert(node->module);
1910 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001911
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001912 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001913
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001914 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001915 if (node->priv && private_destructor) {
1916 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001917 }
1918
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001919 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001920 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001921 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001922 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001923 lydict_remove(ctx, node->dsc);
1924 lydict_remove(ctx, node->ref);
1925 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001926
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001927 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001928 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001929 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001930 }
1931 }
1932
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001933 /* specific part */
1934 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001935 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001936 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001937 break;
Radek Krejci76512572015-08-04 09:47:08 +02001938 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001939 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001940 break;
Radek Krejci76512572015-08-04 09:47:08 +02001941 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001942 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001943 break;
Radek Krejci76512572015-08-04 09:47:08 +02001944 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001945 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001946 break;
Radek Krejci76512572015-08-04 09:47:08 +02001947 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001948 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001949 break;
Radek Krejci76512572015-08-04 09:47:08 +02001950 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001951 case LYS_ANYDATA:
1952 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001953 break;
Radek Krejci76512572015-08-04 09:47:08 +02001954 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001955 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001956 break;
Radek Krejci76512572015-08-04 09:47:08 +02001957 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001958 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001959 break;
Radek Krejci76512572015-08-04 09:47:08 +02001960 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001961 /* do nothing */
1962 break;
Radek Krejci76512572015-08-04 09:47:08 +02001963 case LYS_GROUPING:
1964 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001965 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001966 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001967 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02001968 case LYS_NOTIF:
1969 lys_notif_free(ctx, (struct lys_node_notif *)node);
1970 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001971 case LYS_INPUT:
1972 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001973 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001974 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001975 case LYS_UNKNOWN:
1976 LOGINT;
1977 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001978 }
Radek Krejci5a065542015-05-22 15:02:07 +02001979
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001980 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001981 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001982 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001983}
1984
Radek Krejci2eee5c02016-12-06 19:18:05 +01001985API struct lys_module *
1986lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02001987{
1988 struct ly_ctx *ctx;
1989 int i;
1990
1991 if (!mod || mod->implemented) {
1992 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01001993 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02001994 }
1995
1996 ctx = mod->ctx;
1997 for (i = 0; i < ctx->models.used; i++) {
1998 if (!ctx->models.list[i]->implemented) {
1999 continue;
2000 }
2001
2002 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2003 /* we have some revision of the module implemented */
2004 return ctx->models.list[i];
2005 }
2006 }
2007
2008 /* we have no revision of the module implemented, return the module itself,
2009 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002010 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002011}
2012
2013const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01002014lys_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 +02002015{
Radek Krejcic071c542016-01-27 14:57:51 +01002016 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02002017 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002018 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002019
Michal Vaskoa7789a82016-02-11 15:42:55 +01002020 assert(!prefix || !name);
2021
Michal Vaskob6729c62015-10-21 12:09:47 +02002022 if (prefix && !pref_len) {
2023 pref_len = strlen(prefix);
2024 }
2025 if (name && !name_len) {
2026 name_len = strlen(name);
2027 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002028
Radek Krejcic4283442016-04-22 09:19:27 +02002029 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002030
2031 /* module own prefix, submodule own prefix, (sub)module own name */
2032 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2033 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002034 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002035 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002036 }
2037
Michal Vasko89563fc2016-07-28 16:19:35 +02002038 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002039 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002040 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2041 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002042 return module->imp[i].module;
2043 }
2044 }
2045
Michal Vasko89563fc2016-07-28 16:19:35 +02002046 /* module required by a foreign grouping, deviation, or submodule */
2047 if (name) {
2048 str = strndup(name, name_len);
2049 if (!str) {
2050 LOGMEM;
2051 return NULL;
2052 }
2053 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2054 free(str);
2055 return main_module;
2056 }
2057
Michal Vasko8ce24d72015-10-21 11:27:26 +02002058 return NULL;
2059}
2060
Michal Vasko13b15832015-08-19 11:04:48 +02002061/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002062static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002063module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002064{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002065 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002066 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002067 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002068
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002069 assert(module->ctx);
2070 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002071
Michal Vaskob746fff2016-02-11 11:37:50 +01002072 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002073 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002074 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002075 lydict_remove(ctx, module->imp[i].dsc);
2076 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci225376f2016-02-16 17:36:22 +01002077 }
Radek Krejcidce51452015-06-16 15:20:08 +02002078 free(module->imp);
2079
Radek Krejcic071c542016-01-27 14:57:51 +01002080 /* submodules don't have data tree, the data nodes
2081 * are placed in the main module altogether */
2082 if (!module->type) {
2083 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002084 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002085 }
Radek Krejci21181962015-06-30 14:11:00 +02002086 }
Radek Krejci5a065542015-05-22 15:02:07 +02002087
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002088 lydict_remove(ctx, module->dsc);
2089 lydict_remove(ctx, module->ref);
2090 lydict_remove(ctx, module->org);
2091 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002092 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002093
Radek Krejcieb00f512015-07-01 16:44:58 +02002094 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002095 for (i = 0; i < module->rev_size; i++) {
2096 lydict_remove(ctx, module->rev[i].dsc);
2097 lydict_remove(ctx, module->rev[i].ref);
2098 }
2099 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002100
Radek Krejcieb00f512015-07-01 16:44:58 +02002101 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002102 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002103 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002104 }
2105 module->ident_size = 0;
2106 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002107
Radek Krejcieb00f512015-07-01 16:44:58 +02002108 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002109 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002110 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002111 }
2112 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002113
Radek Krejcieb00f512015-07-01 16:44:58 +02002114 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002115 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002116 lydict_remove(ctx, module->inc[i].dsc);
2117 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcic071c542016-01-27 14:57:51 +01002118 /* complete submodule free is done only from main module since
2119 * submodules propagate their includes to the main module */
2120 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002121 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002122 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002123 }
2124 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002125
Radek Krejcieb00f512015-07-01 16:44:58 +02002126 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002127 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002128 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002129 }
2130 free(module->augment);
2131
Radek Krejcieb00f512015-07-01 16:44:58 +02002132 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002133 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002134 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002135 }
2136 free(module->features);
2137
Radek Krejcieb00f512015-07-01 16:44:58 +02002138 /* deviations */
2139 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002140 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002141 }
2142 free(module->deviation);
2143
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002144 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002145 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002146}
2147
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002148void
Michal Vaskob746fff2016-02-11 11:37:50 +01002149lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002150{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002151 if (!submodule) {
2152 return;
2153 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002154
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002155 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002156 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002157
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002158 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002159
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002160 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002161}
2162
Radek Krejci3a5501d2016-07-18 22:03:34 +02002163static int
2164ingrouping(const struct lys_node *node)
2165{
2166 const struct lys_node *iter = node;
2167 assert(node);
2168
2169 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2170 if (!iter) {
2171 return 0;
2172 } else {
2173 return 1;
2174 }
2175}
2176
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002177/*
2178 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2179 */
2180static struct lys_node *
2181lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2182 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002183{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002184 struct lys_node *retval = NULL, *iter, *p;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002185 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002186 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002187 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002188 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002189 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002190
Michal Vaskoc07187d2015-08-13 15:20:57 +02002191 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002192 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002193 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002194 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002195 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002196 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002197 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002198 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002199 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002200 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002201 struct lys_node_anydata *any = NULL;
2202 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002203 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002204 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002205 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002206 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002207 struct lys_node_rpc_action *rpc = NULL;
2208 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2209 struct lys_node_inout *io = NULL;
2210 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2211 struct lys_node_rpc_action *ntf = NULL;
2212 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002213 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002214 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002215
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002216 /* we cannot just duplicate memory since the strings are stored in
2217 * dictionary and we need to update dictionary counters.
2218 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002219
Radek Krejci1d82ef62015-08-07 14:44:40 +02002220 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002221 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002222 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002223 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002224 break;
2225
Radek Krejci76512572015-08-04 09:47:08 +02002226 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002227 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002228 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002229 break;
2230
Radek Krejci76512572015-08-04 09:47:08 +02002231 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002232 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002233 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002234 break;
2235
Radek Krejci76512572015-08-04 09:47:08 +02002236 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002237 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002238 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002239 break;
2240
Radek Krejci76512572015-08-04 09:47:08 +02002241 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002242 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002243 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002244 break;
2245
Radek Krejci76512572015-08-04 09:47:08 +02002246 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002247 case LYS_ANYDATA:
2248 any = calloc(1, sizeof *any);
2249 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002250 break;
2251
Radek Krejci76512572015-08-04 09:47:08 +02002252 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002253 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002254 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002255 break;
2256
Radek Krejci76512572015-08-04 09:47:08 +02002257 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002258 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002259 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002260 break;
2261
Radek Krejci76512572015-08-04 09:47:08 +02002262 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002263 grp = calloc(1, sizeof *grp);
2264 retval = (struct lys_node *)grp;
2265 break;
2266
Radek Krejci76512572015-08-04 09:47:08 +02002267 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002268 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002269 rpc = calloc(1, sizeof *rpc);
2270 retval = (struct lys_node *)rpc;
2271 break;
2272
Radek Krejci76512572015-08-04 09:47:08 +02002273 case LYS_INPUT:
2274 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002275 io = calloc(1, sizeof *io);
2276 retval = (struct lys_node *)io;
2277 break;
2278
Radek Krejci76512572015-08-04 09:47:08 +02002279 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002280 ntf = calloc(1, sizeof *ntf);
2281 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002282 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002283
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002284 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002285 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002286 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002287 }
Radek Krejcib388c152015-06-04 17:03:03 +02002288
Michal Vasko253035f2015-12-17 16:58:13 +01002289 if (!retval) {
2290 LOGMEM;
2291 return NULL;
2292 }
2293
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002294 /*
2295 * duplicate generic part of the structure
2296 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002297 retval->name = lydict_insert(ctx, node->name, 0);
2298 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2299 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002300 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002301 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002302
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002303 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002304 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002305
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002306 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002307
Radek Krejci06214042016-11-04 16:25:58 +01002308 if (node->iffeature_size) {
2309 retval->iffeature_size = node->iffeature_size;
2310 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2311 if (!retval->iffeature) {
2312 LOGMEM;
2313 goto error;
2314 }
Michal Vasko253035f2015-12-17 16:58:13 +01002315 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002316
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002317 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002318 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002319 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2320 if (size1) {
2321 /* there is something to duplicate */
2322
2323 /* duplicate compiled expression */
2324 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2325 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2326 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2327
2328 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002329 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002330 for (j = 0; (unsigned int)j < size2; j++) {
2331 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2332 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002333 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002334 /* feature is resolved in origin, so copy it
2335 * - duplication is used for instantiating groupings
2336 * and if-feature inside grouping is supposed to be
2337 * resolved inside the original grouping, so we want
2338 * to keep pointers to features from the grouping
2339 * context */
2340 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2341 } else if (rc == -1) {
2342 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002343 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002344 }
2345 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002346 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002347
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002348 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002349 p = parent;
2350 do {
2351 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2352 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002353 if (iter) {
2354 flags = iter->flags & LYS_CONFIG_MASK;
2355 } else {
2356 /* default */
2357 flags = LYS_CONFIG_W;
2358 }
2359
2360 switch (finalize) {
2361 case 1:
2362 /* inherit config flags */
2363 if (retval->flags & LYS_CONFIG_SET) {
2364 /* skip nodes with an explicit config value */
2365 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2366 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
Michal Vasko51e5c582017-01-19 14:16:39 +01002367 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002368 goto error;
2369 }
2370 break;
2371 }
2372
2373 if (retval->nodetype != LYS_USES) {
2374 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2375 }
2376 break;
2377 case 2:
2378 /* erase config flags */
2379 retval->flags &= ~LYS_CONFIG_MASK;
2380 retval->flags &= ~LYS_CONFIG_SET;
2381 break;
2382 }
2383
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002384 /* connect it to the parent */
2385 if (lys_node_addchild(parent, retval->module, retval)) {
2386 goto error;
2387 }
Radek Krejcidce51452015-06-16 15:20:08 +02002388
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002389 /* go recursively */
2390 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002391 LY_TREE_FOR(node->child, iter) {
2392 if (!lys_node_dup_recursion(module, retval, iter, retval->nacm, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002393 goto error;
2394 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002395 }
2396 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002397
2398 if (finalize == 1) {
2399 /* check that configuration lists have keys
2400 * - we really want to check keys_size in original node, because the keys are
2401 * not yet resolved here, it is done below in nodetype specific part */
2402 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2403 && !((struct lys_node_list *)node)->keys_size) {
2404 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2405 goto error;
2406 }
2407 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002408 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002409 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002410 }
2411
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002412 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002413 * duplicate specific part of the structure
2414 */
2415 switch (node->nodetype) {
2416 case LYS_CONTAINER:
2417 if (cont_orig->when) {
2418 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002419 }
2420 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002421
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002422 cont->must_size = cont_orig->must_size;
2423 cont->tpdf_size = cont_orig->tpdf_size;
2424
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002425 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002426 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002427 break;
2428
2429 case LYS_CHOICE:
2430 if (choice_orig->when) {
2431 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002432 }
2433
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002434 if (!shallow) {
2435 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002436 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2437 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2438 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002439 if (rc) {
2440 if (rc == EXIT_FAILURE) {
2441 LOGINT;
2442 }
2443 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002444 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002445 } else {
2446 /* useless to check return value, we don't know whether
2447 * there really wasn't any default defined or it just hasn't
2448 * been resolved, we just hope for the best :)
2449 */
2450 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002451 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002452 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002453 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002454 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002455 break;
2456
2457 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002458 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002459 goto error;
2460 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002461 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2462
2463 if (leaf_orig->dflt) {
2464 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci51673202016-11-01 17:00:32 +01002465 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT,
2466 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002467 goto error;
2468 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002469 }
2470
2471 leaf->must_size = leaf_orig->must_size;
2472 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002473
2474 if (leaf_orig->when) {
2475 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002476 }
2477 break;
2478
2479 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002480 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002481 goto error;
2482 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002483 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2484
2485 llist->min = llist_orig->min;
2486 llist->max = llist_orig->max;
2487
2488 llist->must_size = llist_orig->must_size;
2489 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002490
Radek Krejci51673202016-11-01 17:00:32 +01002491 llist->dflt_size = llist_orig->dflt_size;
2492 llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt);
2493 for (i = 0; i < llist->dflt_size; i++) {
2494 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
2495 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
2496 (struct lys_node *)(&llist->dflt[i])) == -1) {
2497 goto error;
2498 }
2499 }
2500
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002501 if (llist_orig->when) {
2502 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002503 }
2504 break;
2505
2506 case LYS_LIST:
2507 list->min = list_orig->min;
2508 list->max = list_orig->max;
2509
2510 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002511 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002512
Radek Krejci581ce772015-11-10 17:22:40 +01002513 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002514 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002515
Radek Krejci581ce772015-11-10 17:22:40 +01002516 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002517 if (list->keys_size) {
2518 list->keys = calloc(list->keys_size, sizeof *list->keys);
Radek Krejci5c08a992016-11-02 13:30:04 +01002519 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Michal Vasko253035f2015-12-17 16:58:13 +01002520 if (!list->keys) {
2521 LOGMEM;
2522 goto error;
2523 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002524
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002525 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01002526 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
2527 * in another grouping */
2528 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
2529 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
2530 goto error;
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
Radek Krejci96935402016-11-04 16:27:28 +01002611 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02002612 case LYS_RPC:
2613 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002614 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002615 break;
2616
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002617 case LYS_INPUT:
2618 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002619 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002620 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002621 break;
2622
Radek Krejcida04f4a2015-05-21 12:54:09 +02002623 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002624 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002625 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002626 break;
2627
Radek Krejci96935402016-11-04 16:27:28 +01002628
Radek Krejcida04f4a2015-05-21 12:54:09 +02002629 default:
2630 /* LY_NODE_AUGMENT */
2631 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002632 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002633 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002634
2635 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002636
2637error:
2638
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002639 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002640 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002641}
2642
Radek Krejcib3142312016-11-09 11:04:12 +01002643int
2644lys_has_xpath(const struct lys_node *node)
2645{
2646 assert(node);
2647
2648 switch (node->nodetype) {
2649 case LYS_AUGMENT:
2650 if (((struct lys_node_augment *)node)->when) {
2651 return 1;
2652 }
2653 break;
2654 case LYS_CASE:
2655 if (((struct lys_node_case *)node)->when) {
2656 return 1;
2657 }
2658 break;
2659 case LYS_CHOICE:
2660 if (((struct lys_node_choice *)node)->when) {
2661 return 1;
2662 }
2663 break;
2664 case LYS_ANYDATA:
2665 if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) {
2666 return 1;
2667 }
2668 break;
2669 case LYS_LEAF:
2670 if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) {
2671 return 1;
2672 }
2673 break;
2674 case LYS_LEAFLIST:
2675 if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) {
2676 return 1;
2677 }
2678 break;
2679 case LYS_LIST:
2680 if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) {
2681 return 1;
2682 }
2683 break;
2684 case LYS_CONTAINER:
2685 if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) {
2686 return 1;
2687 }
2688 break;
2689 case LYS_INPUT:
2690 case LYS_OUTPUT:
2691 if (((struct lys_node_inout *)node)->must_size) {
2692 return 1;
2693 }
2694 break;
2695 case LYS_NOTIF:
2696 if (((struct lys_node_notif *)node)->must_size) {
2697 return 1;
2698 }
2699 break;
2700 case LYS_USES:
2701 if (((struct lys_node_uses *)node)->when) {
2702 return 1;
2703 }
2704 break;
2705 default:
2706 /* does not have XPath */
2707 break;
2708 }
2709
2710 return 0;
2711}
2712
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002713struct lys_node *
2714lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2715 struct unres_schema *unres, int shallow)
2716{
2717 struct lys_node *p = NULL;
Radek Krejcib3142312016-11-09 11:04:12 +01002718 int finalize = 0;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002719 struct lys_node *result, *iter, *next;
2720
2721 if (!shallow) {
2722 /* get know where in schema tre we are to know what should be done during instantiation of the grouping */
2723 for (p = parent;
2724 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
2725 p = lys_parent(p));
2726 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
2727 }
2728
2729 result = lys_node_dup_recursion(module, parent, node, nacm, unres, shallow, finalize);
2730 if (finalize) {
2731 /* check xpath expressions in the instantiated tree */
2732 for (iter = next = parent->child; iter; iter = next) {
Radek Krejcib3142312016-11-09 11:04:12 +01002733 if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
Radek Krejci3c48d042016-11-07 14:42:36 +01002734 /* invalid xpath */
2735 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002736 }
2737
2738 /* select next item */
2739 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
2740 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
2741 next = NULL;
2742 } else {
2743 next = iter->child;
2744 }
2745 if (!next) {
2746 /* no children, try siblings */
2747 next = iter->next;
2748 }
2749 while (!next) {
2750 /* parent is already processed, go to its sibling */
2751 iter = lys_parent(iter);
2752 if (iter == parent) {
2753 /* we are done, no next element to process */
2754 break;
2755 }
2756 next = iter->next;
2757 }
2758 }
2759 }
2760
2761 return result;
2762}
2763
Michal Vasko13b15832015-08-19 11:04:48 +02002764void
Michal Vaskoff006c12016-02-17 11:15:19 +01002765lys_node_switch(struct lys_node *dst, struct lys_node *src)
2766{
2767 struct lys_node *child;
2768
Michal Vaskob42b6972016-06-06 14:21:30 +02002769 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01002770
2771 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02002772 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002773 dst->prev->next = src;
2774 }
2775
2776 /* sibling prev */
2777 if (dst->next) {
2778 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02002779 } else {
2780 for (child = dst->prev; child->prev->next; child = child->prev);
2781 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01002782 }
2783
2784 /* next */
2785 src->next = dst->next;
2786 dst->next = NULL;
2787
2788 /* prev */
2789 if (dst->prev != dst) {
2790 src->prev = dst->prev;
2791 }
2792 dst->prev = dst;
2793
2794 /* parent child */
2795 if (dst->parent && (dst->parent->child == dst)) {
2796 dst->parent->child = src;
2797 }
2798
2799 /* parent */
2800 src->parent = dst->parent;
2801 dst->parent = NULL;
2802
2803 /* child parent */
2804 LY_TREE_FOR(dst->child, child) {
2805 if (child->parent == dst) {
2806 child->parent = src;
2807 }
2808 }
2809
2810 /* child */
2811 src->child = dst->child;
2812 dst->child = NULL;
2813}
2814
2815void
Michal Vasko627975a2016-02-11 11:39:03 +01002816lys_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 +02002817{
2818 struct ly_ctx *ctx;
2819 int i;
2820
2821 if (!module) {
2822 return;
2823 }
2824
2825 /* remove schema from the context */
2826 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002827 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002828 for (i = 0; i < ctx->models.used; i++) {
2829 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002830 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002831 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002832 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 +02002833 ctx->models.list[ctx->models.used] = NULL;
2834 /* we are done */
2835 break;
2836 }
2837 }
2838 }
2839
2840 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002841 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002842
2843 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002844 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002845
2846 free(module);
2847}
Radek Krejci7e97c352015-06-19 16:26:34 +02002848
Radek Krejci9de2c042016-10-19 16:53:06 +02002849static void
2850lys_features_disable_recursive(struct lys_feature *f)
2851{
2852 unsigned int i;
2853 struct lys_feature *depf;
2854
2855 /* disable the feature */
2856 f->flags &= ~LYS_FENABLED;
2857
2858 /* by disabling feature we have to disable also all features that depends on this feature */
2859 if (f->depfeatures) {
2860 for (i = 0; i < f->depfeatures->number; i++) {
2861 depf = (struct lys_feature *)f->depfeatures->set.g[i];
2862 if (depf->flags & LYS_FENABLED) {
2863 lys_features_disable_recursive(depf);
2864 }
2865 }
2866 }
2867}
2868
2869
Radek Krejci7e97c352015-06-19 16:26:34 +02002870/*
2871 * op: 1 - enable, 0 - disable
2872 */
2873static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002874lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002875{
2876 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002877 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02002878 int progress, faili, failj, failk;
2879
2880 uint8_t fsize;
2881 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02002882
2883 if (!module || !name || !strlen(name)) {
2884 return EXIT_FAILURE;
2885 }
2886
2887 if (!strcmp(name, "*")) {
2888 /* enable all */
2889 all = 1;
2890 }
2891
Radek Krejcia889c1f2016-10-19 15:50:11 +02002892 progress = failk = 1;
2893 while (progress && failk) {
2894 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
2895 if (i == -1) {
2896 fsize = module->features_size;
2897 f = module->features;
2898 } else {
2899 fsize = module->inc[i].submodule->features_size;
2900 f = module->inc[i].submodule->features;
2901 }
2902
2903 for (j = 0; j < fsize; j++) {
2904 if (all || !strcmp(f[j].name, name)) {
Michal Vasko34c3b552016-11-21 09:07:43 +01002905 if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) {
2906 if (all) {
2907 /* skip already set features */
2908 continue;
2909 } else {
2910 /* feature already set correctly */
2911 return EXIT_SUCCESS;
2912 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02002913 }
2914
2915 if (op) {
2916 /* check referenced features if they are enabled */
2917 for (k = 0; k < f[j].iffeature_size; k++) {
2918 if (!resolve_iffeature(&f[j].iffeature[k])) {
2919 if (all) {
2920 faili = i;
2921 failj = j;
2922 failk = k + 1;
2923 break;
2924 } else {
2925 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2926 f[j].name, k + 1);
2927 return EXIT_FAILURE;
2928 }
2929 }
2930 }
2931
2932 if (k == f[j].iffeature_size) {
2933 /* the last check passed, do the change */
2934 f[j].flags |= LYS_FENABLED;
2935 progress++;
2936 }
2937 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02002938 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02002939 progress++;
2940 }
2941 if (!all) {
2942 /* stop in case changing a single feature */
2943 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002944 }
2945 }
Radek Krejci7e97c352015-06-19 16:26:34 +02002946 }
2947 }
2948 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02002949 if (failk) {
2950 /* print info about the last failing feature */
2951 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2952 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
2953 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02002954 }
2955
2956 if (all) {
2957 return EXIT_SUCCESS;
2958 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02002959 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02002960 return EXIT_FAILURE;
2961 }
2962}
2963
2964API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002965lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002966{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002967 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002968}
2969
2970API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002971lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002972{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002973 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002974}
2975
2976API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002977lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002978{
2979 int i, j;
2980
2981 if (!module || !feature) {
2982 return -1;
2983 }
2984
2985 /* search for the specified feature */
2986 /* module itself */
2987 for (i = 0; i < module->features_size; i++) {
2988 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002989 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002990 return 1;
2991 } else {
2992 return 0;
2993 }
2994 }
2995 }
2996
2997 /* submodules */
2998 for (j = 0; j < module->inc_size; j++) {
2999 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
3000 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003001 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003002 return 1;
3003 } else {
3004 return 0;
3005 }
3006 }
3007 }
3008 }
3009
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003010 /* feature definition not found */
3011 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02003012}
Michal Vasko2367e7c2015-07-07 11:33:44 +02003013
Radek Krejci96a10da2015-07-30 11:00:14 +02003014API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01003015lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02003016{
Radek Krejci96a10da2015-07-30 11:00:14 +02003017 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003018 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003019 unsigned int count;
3020
3021 if (!module) {
3022 return NULL;
3023 }
3024
3025 count = module->features_size;
3026 for (i = 0; i < module->inc_size; i++) {
3027 count += module->inc[i].submodule->features_size;
3028 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003029 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01003030 if (!result) {
3031 LOGMEM;
3032 return NULL;
3033 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003034 if (states) {
3035 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01003036 if (!(*states)) {
3037 LOGMEM;
3038 free(result);
3039 return NULL;
3040 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003041 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003042 count = 0;
3043
3044 /* module itself */
3045 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003046 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003047 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003048 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003049 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003050 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003051 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003052 }
3053 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003054 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003055 }
3056
3057 /* submodules */
3058 for (j = 0; j < module->inc_size; j++) {
3059 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003060 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003061 if (states) {
3062 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3063 (*states)[count] = 1;
3064 } else {
3065 (*states)[count] = 0;
3066 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003067 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003068 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003069 }
3070 }
3071
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003072 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003073 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003074
3075 return result;
3076}
Michal Vaskobaefb032015-09-24 14:52:10 +02003077
Radek Krejci6910a032016-04-13 10:06:21 +02003078API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003079lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003080{
Michal Vaskof53187d2017-01-13 13:23:14 +01003081 if (!node) {
3082 return NULL;
3083 }
3084
Radek Krejcic071c542016-01-27 14:57:51 +01003085 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3086}
3087
Radek Krejci6910a032016-04-13 10:06:21 +02003088API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003089lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003090{
Michal Vaskof53187d2017-01-13 13:23:14 +01003091 if (!module) {
3092 return NULL;
3093 }
3094
Michal Vasko320e8532016-02-15 13:11:57 +01003095 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3096}
3097
Michal Vaskobaefb032015-09-24 14:52:10 +02003098API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003099lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003100{
3101 if (!node || !node->parent) {
3102 return NULL;
3103 }
3104
3105 if (node->parent->nodetype == LYS_AUGMENT) {
3106 return ((struct lys_node_augment *)node->parent)->target;
3107 }
3108
3109 return node->parent;
3110}
Michal Vasko1b229152016-01-13 11:28:38 +01003111
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003112API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003113lys_set_private(const struct lys_node *node, void *priv)
3114{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003115 void *prev;
3116
Michal Vasko1b229152016-01-13 11:28:38 +01003117 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003118 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
3119 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003120 }
3121
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003122 prev = node->priv;
3123 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003124
3125 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003126}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003127
Michal Vasko01c6fd22016-05-20 11:43:05 +02003128int
3129lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3130{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003131 struct lys_node_leaf *iter = leafref_target;
3132
Michal Vasko48a573d2016-07-01 11:46:02 +02003133 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003134 LOGINT;
3135 return -1;
3136 }
3137
Pavol Vican93175152016-08-30 15:34:44 +02003138 /* check for config flag */
3139 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
3140 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
3141 "The %s is config but refers to a non-config %s.",
3142 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3143 return -1;
3144 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003145 /* check for cycles */
3146 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3147 if ((void *)iter == (void *)leafref) {
3148 /* cycle detected */
3149 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3150 return -1;
3151 }
3152 iter = iter->type.info.lref.target;
3153 }
3154
3155 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003156 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003157 if (!leafref_target->backlinks) {
3158 leafref_target->backlinks = (void*)ly_set_new();
3159 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003160 LOGMEM;
3161 return -1;
3162 }
3163 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003164 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003165
3166 return 0;
3167}
3168
Michal Vasko8548e082016-07-22 12:00:18 +02003169/* not needed currently */
3170#if 0
3171
Michal Vasko5b3492c2016-07-20 09:37:40 +02003172static const char *
3173lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3174{
3175 struct lys_module *prev_mod;
3176 uint32_t str_len, mod_len, buf_idx;
3177
Radek Krejcibf2abff2016-08-23 15:51:52 +02003178 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003179 LOGINT;
3180 return NULL;
3181 }
3182
3183 buf_idx = buf_len - 1;
3184 buf[buf_idx] = '\0';
3185
3186 while (node) {
3187 if (lys_parent(node)) {
3188 prev_mod = lys_node_module(lys_parent(node));
3189 } else {
3190 prev_mod = NULL;
3191 }
3192
Radek Krejcibf2abff2016-08-23 15:51:52 +02003193 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003194 str_len = strlen(node->name);
3195
3196 if (prev_mod != node->module) {
3197 mod_len = strlen(node->module->name);
3198 } else {
3199 mod_len = 0;
3200 }
3201
3202 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3203 LOGINT;
3204 return NULL;
3205 }
3206
3207 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3208
3209 buf[buf_idx] = '/';
3210 if (mod_len) {
3211 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3212 buf[buf_idx + 1 + mod_len] = ':';
3213 }
3214 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3215 }
3216
3217 node = lys_parent(node);
3218 }
3219
3220 return buf + buf_idx;
3221}
3222
Michal Vasko8548e082016-07-22 12:00:18 +02003223#endif
3224
3225API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02003226lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003227{
3228 struct lyxp_set set;
3229 struct ly_set *ret_set;
3230 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003231 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003232
3233 if (!node || !expr) {
3234 ly_errno = LY_EINVAL;
3235 return NULL;
3236 }
3237
3238 memset(&set, 0, sizeof set);
3239
Michal Vaskodb1da032016-09-08 10:07:38 +02003240 opts = LYXP_SNODE;
3241 if (options & LYS_FIND_OUTPUT) {
3242 opts |= LYXP_SNODE_OUTPUT;
3243 }
3244
Michal Vasko46a4bf92016-09-08 08:23:49 +02003245 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02003246 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003247 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003248 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko46a4bf92016-09-08 08:23:49 +02003249 return NULL;
3250 }
3251
3252 ret_set = ly_set_new();
3253
3254 for (i = 0; i < set.used; ++i) {
3255 if (!set.val.snodes[i].in_ctx) {
3256 continue;
3257 }
3258 assert(set.val.snodes[i].in_ctx == 1);
3259
3260 switch (set.val.snodes[i].type) {
3261 case LYXP_NODE_ELEM:
3262 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3263 ly_set_free(ret_set);
3264 free(set.val.snodes);
3265 return NULL;
3266 }
3267 break;
3268 default:
3269 /* ignore roots, text and attr should not ever appear */
3270 break;
3271 }
3272 }
3273
3274 free(set.val.snodes);
3275 return ret_set;
3276}
3277
3278API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003279lys_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 +02003280{
Michal Vasko508a50d2016-09-07 14:50:33 +02003281 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003282 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003283 uint32_t i;
3284
Michal Vaskob94a5e42016-09-08 14:01:56 +02003285 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003286 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003287 }
3288
Michal Vaskob94a5e42016-09-08 14:01:56 +02003289 /* adjust the root */
3290 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3291 do {
3292 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3293 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3294 }
3295
Michal Vasko508a50d2016-09-07 14:50:33 +02003296 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003297
3298 if (options & LYXP_MUST) {
3299 options &= ~LYXP_MUST;
3300 options |= LYXP_SNODE_MUST;
3301 } else if (options & LYXP_WHEN) {
3302 options &= ~LYXP_WHEN;
3303 options |= LYXP_SNODE_WHEN;
3304 } else {
3305 options |= LYXP_SNODE;
3306 }
3307
Michal Vasko508a50d2016-09-07 14:50:33 +02003308 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3309 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003310 LOGVAL(LYE_SPEC, LY_VLOG_LYS, cur_snode, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003311 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003312 }
3313
Michal Vasko8548e082016-07-22 12:00:18 +02003314 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003315
Michal Vasko508a50d2016-09-07 14:50:33 +02003316 for (i = 0; i < set.used; ++i) {
3317 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003318 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003319 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003320 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003321 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003322 return NULL;
3323 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003324 break;
3325 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003326 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003327 break;
3328 }
3329 }
3330
Michal Vasko508a50d2016-09-07 14:50:33 +02003331 free(set.val.snodes);
3332 return ret_set;
3333}
3334
3335API struct ly_set *
3336lys_node_xpath_atomize(const struct lys_node *node, int options)
3337{
3338 const struct lys_node *next, *elem, *parent, *tmp;
3339 struct lyxp_set set;
3340 struct ly_set *ret_set;
3341 uint16_t i;
3342
3343 if (!node) {
3344 return NULL;
3345 }
3346
3347 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3348 if (!parent) {
3349 /* not in input, output, or notification */
3350 return NULL;
3351 }
3352
3353 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003354 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003355 return NULL;
3356 }
3357
3358 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003359 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003360 /* elem has no dependencies from other subtrees and local nodes get discarded */
3361 goto next_iter;
3362 }
3363
3364 if (lyxp_node_atomize(elem, &set)) {
3365 ly_set_free(ret_set);
3366 free(set.val.snodes);
3367 return NULL;
3368 }
3369
3370 for (i = 0; i < set.used; ++i) {
3371 switch (set.val.snodes[i].type) {
3372 case LYXP_NODE_ELEM:
3373 if (options & LYXP_NO_LOCAL) {
3374 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3375 if (tmp) {
3376 /* in local subtree, discard */
3377 break;
3378 }
3379 }
3380 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3381 ly_set_free(ret_set);
3382 free(set.val.snodes);
3383 return NULL;
3384 }
3385 break;
3386 default:
3387 /* ignore roots, text and attr should not ever appear */
3388 break;
3389 }
3390 }
3391
3392 free(set.val.snodes);
3393 if (!(options & LYXP_RECURSIVE)) {
3394 break;
3395 }
3396next_iter:
3397 LY_TREE_DFS_END(node, next, elem);
3398 }
3399
Michal Vasko8548e082016-07-22 12:00:18 +02003400 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003401}
3402
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003403static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003404lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003405{
3406 int ret;
3407 char *parent_path;
3408 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003409
Pavol Vican64d0b762016-08-25 10:44:59 +02003410 if (!dev->deviate) {
3411 return ;
3412 }
3413
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003414 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3415 if (dev->orig_node) {
3416 /* removing not-supported deviation ... */
3417 if (strrchr(dev->target_name, '/') != dev->target_name) {
3418 /* ... from a parent */
3419 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3420
3421 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003422 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, 1,
3423 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003424 free(parent_path);
3425 if (ret || !target) {
3426 LOGINT;
3427 return;
3428 }
3429
3430 lys_node_addchild(target, NULL, dev->orig_node);
3431 } else {
3432 /* ... from top-level data */
3433 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3434 }
3435
3436 dev->orig_node = NULL;
3437 } else {
3438 /* adding not-supported deviation */
3439 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003440 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1,
3441 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003442 if (ret || !target) {
3443 LOGINT;
3444 return;
3445 }
3446
3447 lys_node_unlink(target);
3448 dev->orig_node = target;
3449 }
3450 } else {
3451 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003452 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1,
3453 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003454 if (ret || !target) {
3455 LOGINT;
3456 return;
3457 }
3458
3459 lys_node_switch(target, dev->orig_node);
3460 dev->orig_node = target;
3461 }
3462}
3463
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003464/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3465void
3466lys_switch_deviations(struct lys_module *module)
3467{
Michal Vasko89563fc2016-07-28 16:19:35 +02003468 uint32_t i = 0, j;
3469 const struct lys_module *mod;
3470 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003471
Michal Vasko89563fc2016-07-28 16:19:35 +02003472 if (module->deviated) {
3473 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3474 if (mod == module) {
3475 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003476 }
3477
Michal Vasko89563fc2016-07-28 16:19:35 +02003478 for (j = 0; j < mod->deviation_size; ++j) {
3479 ptr = strstr(mod->deviation[j].target_name, module->name);
3480 if (ptr && ptr[strlen(module->name)] == ':') {
3481 lys_switch_deviation(&mod->deviation[j], module);
3482 }
3483 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003484 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003485
Michal Vasko89563fc2016-07-28 16:19:35 +02003486 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003487 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003488 } else {
3489 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003490 }
3491 }
3492}
3493
Radek Krejci0ec51da2016-12-14 16:42:03 +01003494static void
3495apply_dev(struct lys_deviation *dev, const struct lys_module *module)
3496{
3497 lys_switch_deviation(dev, module);
3498
3499 assert(dev->orig_node);
3500 lys_node_module(dev->orig_node)->deviated = 1;
3501}
3502
3503static void
3504apply_aug(struct lys_node_augment *augment)
3505{
3506 struct lys_node *last;
3507
3508 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
3509
3510 /* reconnect augmenting data into the target - add them to the target child list */
3511 if (augment->target->child) {
3512 last = augment->target->child->prev;
3513 last->next = augment->child;
3514 augment->target->child->prev = augment->child->prev;
3515 augment->child->prev = last;
3516 } else {
3517 augment->target->child = augment->child;
3518 }
3519
3520 /* remove the flag about not applicability */
3521 augment->flags &= ~LYS_NOTAPPLIED;
3522}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003523
3524void
3525lys_sub_module_apply_devs_augs(struct lys_module *module)
3526{
Radek Krejci0ec51da2016-12-14 16:42:03 +01003527 uint8_t u, v;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003528
Radek Krejci0ec51da2016-12-14 16:42:03 +01003529 /* remove applied deviations */
3530 for (u = 0; u < module->deviation_size; ++u) {
3531 apply_dev(&module->deviation[u], module);
3532 }
3533 /* remove applied augments */
3534 for (u = 0; u < module->augment_size; ++u) {
3535 apply_aug(&module->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003536 }
3537
Radek Krejci0ec51da2016-12-14 16:42:03 +01003538 /* remove deviation and augments defined in submodules */
3539 for (v = 0; v < module->inc_size; ++v) {
3540 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
3541 apply_dev(&module->inc[v].submodule->deviation[u], module);
3542 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003543
Radek Krejci0ec51da2016-12-14 16:42:03 +01003544 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
3545 apply_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003546 }
3547 }
3548}
3549
Radek Krejcib2541a32016-12-12 16:45:57 +01003550static void
3551remove_dev(struct lys_deviation *dev, const struct lys_module *module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003552{
Radek Krejcib2541a32016-12-12 16:45:57 +01003553 uint32_t idx = 0, j;
Michal Vasko89563fc2016-07-28 16:19:35 +02003554 const struct lys_module *mod;
3555 struct lys_module *target_mod;
3556 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003557
Radek Krejcib2541a32016-12-12 16:45:57 +01003558 if (dev->orig_node) {
3559 target_mod = lys_node_module(dev->orig_node);
3560 } else {
Radek Krejci0ec51da2016-12-14 16:42:03 +01003561 LOGINT;
3562 return;
Radek Krejcib2541a32016-12-12 16:45:57 +01003563 }
3564 lys_switch_deviation(dev, module);
3565
3566 /* clear the deviation flag if possible */
3567 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
3568 if ((mod == module) || (mod == target_mod)) {
3569 continue;
Pavol Vican64d0b762016-08-25 10:44:59 +02003570 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003571
Radek Krejcib2541a32016-12-12 16:45:57 +01003572 for (j = 0; j < mod->deviation_size; ++j) {
3573 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
3574 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
3575 /* some other module deviation targets the inspected module, flag remains */
Michal Vasko89563fc2016-07-28 16:19:35 +02003576 break;
3577 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003578 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003579
Radek Krejcib2541a32016-12-12 16:45:57 +01003580 if (j < mod->deviation_size) {
3581 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003582 }
3583 }
3584
Radek Krejcib2541a32016-12-12 16:45:57 +01003585 if (!mod) {
3586 target_mod->deviated = 0;
3587 }
3588}
3589
3590static void
3591remove_aug(struct lys_node_augment *augment)
3592{
3593 struct lys_node *last, *elem;
3594
3595 if (!augment->target) {
3596 /* skip not resolved augments */
3597 return;
3598 }
3599
3600 elem = augment->child;
3601 if (elem) {
3602 LY_TREE_FOR(elem, last) {
3603 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
3604 break;
3605 }
3606 }
3607 /* elem is first augment child, last is the last child */
3608
3609 /* parent child ptr */
3610 if (augment->target->child == elem) {
3611 augment->target->child = last->next;
3612 }
3613
3614 /* parent child next ptr */
3615 if (elem->prev->next) {
3616 elem->prev->next = last->next;
3617 }
3618
3619 /* parent child prev ptr */
3620 if (last->next) {
3621 last->next->prev = elem->prev;
3622 } else if (augment->target->child) {
3623 augment->target->child->prev = elem->prev;
3624 }
3625
3626 /* update augment children themselves */
3627 elem->prev = last;
3628 last->next = NULL;
3629 }
3630
Radek Krejci0ec51da2016-12-14 16:42:03 +01003631 /* augment->target still keeps the resolved target, but for lys_augment_free()
3632 * we have to keep information that this augment is not applied to free its data */
3633 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01003634}
3635
3636void
3637lys_sub_module_remove_devs_augs(struct lys_module *module)
3638{
3639 uint8_t u, v;
3640
3641 /* remove applied deviations */
3642 for (u = 0; u < module->deviation_size; ++u) {
3643 remove_dev(&module->deviation[u], module);
3644 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003645 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01003646 for (u = 0; u < module->augment_size; ++u) {
3647 remove_aug(&module->augment[u]);
3648 }
3649
3650 /* remove deviation and augments defined in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01003651 for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) {
Radek Krejcib2541a32016-12-12 16:45:57 +01003652 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
3653 remove_dev(&module->inc[v].submodule->deviation[u], module);
Radek Krejcidbc15262016-06-16 14:58:29 +02003654 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003655
Radek Krejcib2541a32016-12-12 16:45:57 +01003656 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
3657 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003658 }
3659 }
3660}
3661
Radek Krejci27fe55e2016-09-13 17:13:35 +02003662static int
3663lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
3664{
3665 struct lys_node *root, *next, *node;
3666 uint8_t i;
3667
3668 for (i = 0; i < module->augment_size; i++) {
3669 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003670 if (!module->augment[i].target
3671 && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003672 return -1;
3673 }
3674 }
3675 LY_TREE_FOR(module->data, root) {
3676 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
3677 LY_TREE_DFS_BEGIN(root, next, node) {
3678 if (node->nodetype == LYS_GROUPING) {
3679 goto nextsibling;
3680 }
3681 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3682 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
3683 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
3684 UNRES_TYPE_LEAFREF, node) == -1) {
3685 return EXIT_FAILURE;
3686 }
3687 }
3688 }
3689
3690 /* modified LY_TREE_DFS_END */
3691 next = node->child;
3692 /* child exception for leafs, leaflists and anyxml without children */
3693 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
3694 next = NULL;
3695 }
3696 if (!next) {
3697nextsibling:
3698 /* no children */
3699 if (node == root) {
3700 /* we are done, root has no children */
3701 break;
3702 }
3703 /* try siblings */
3704 next = node->next;
3705 }
3706 while (!next) {
3707 /* parent is already processed, go to its sibling */
3708 node = lys_parent(node);
3709 /* no siblings, go back through parents */
3710 if (lys_parent(node) == lys_parent(root)) {
3711 /* we are done, no next element to process */
3712 break;
3713 }
3714 next = node->next;
3715 }
3716 }
3717 }
3718
3719 return EXIT_SUCCESS;
3720}
3721
3722API int
3723lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02003724{
3725 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003726 struct unres_schema *unres;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003727 int i, j, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02003728
Radek Krejci27fe55e2016-09-13 17:13:35 +02003729 if (!module) {
3730 ly_errno = LY_EINVAL;
3731 return EXIT_FAILURE;
3732 }
3733
3734 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01003735
3736 if (module->disabled) {
3737 disabled = 1;
3738 lys_set_enabled(module);
3739 }
3740
Michal Vasko26055752016-05-03 11:36:31 +02003741 if (module->implemented) {
3742 return EXIT_SUCCESS;
3743 }
3744
3745 ctx = module->ctx;
3746
3747 for (i = 0; i < ctx->models.used; ++i) {
3748 if (module == ctx->models.list[i]) {
3749 continue;
3750 }
3751
3752 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
3753 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01003754 if (disabled) {
3755 /* set it back disabled */
3756 lys_set_disabled(module);
3757 }
Michal Vasko26055752016-05-03 11:36:31 +02003758 return EXIT_FAILURE;
3759 }
3760 }
3761
Radek Krejci27fe55e2016-09-13 17:13:35 +02003762 unres = calloc(1, sizeof *unres);
3763 if (!unres) {
3764 LOGMEM;
Radek Krejci0ec51da2016-12-14 16:42:03 +01003765 if (disabled) {
3766 /* set it back disabled */
3767 lys_set_disabled(module);
3768 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003769 return EXIT_FAILURE;
3770 }
3771 /* recursively make the module implemented */
3772 ((struct lys_module *)module)->implemented = 1;
3773 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
3774 goto error;
3775 }
3776 /* process augments in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01003777 for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003778 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
3779 /* apply augment */
Radek Krejcic8c22532016-11-09 15:11:24 +01003780 if (!module->inc[i].submodule->augment[j].target
3781 && (unres_schema_add_node((struct lys_module *)module->inc[j].submodule, unres,
3782 &module->inc[i].submodule->augment[j], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci0ec51da2016-12-14 16:42:03 +01003783
Radek Krejci27fe55e2016-09-13 17:13:35 +02003784 goto error;
3785 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003786 }
3787 }
Radek Krejcidf46e222016-11-08 11:57:37 +01003788 /* try again resolve augments in other modules possibly augmenting this one,
3789 * since we have just enabled it
3790 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02003791 /* resolve rest of unres items */
3792 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
3793 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003794 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003795 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02003796
3797 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003798
3799error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01003800
3801 if (disabled) {
3802 /* set it back disabled */
3803 lys_set_disabled(module);
3804 }
3805
Radek Krejci27fe55e2016-09-13 17:13:35 +02003806 ((struct lys_module *)module)->implemented = 0;
3807 unres_schema_free((struct lys_module *)module, &unres);
3808 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003809}
3810
3811void
3812lys_submodule_module_data_free(struct lys_submodule *submodule)
3813{
3814 struct lys_node *next, *elem;
3815
3816 /* remove parsed data */
3817 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3818 if (elem->module == (struct lys_module *)submodule) {
3819 lys_node_free(elem, NULL, 0);
3820 }
3821 }
3822}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02003823
3824int
3825lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
3826{
3827 uint8_t i;
3828
3829 for (i = 0; i < list->keys_size; i++) {
3830 if (list->keys[i] == leaf) {
3831 return i + 1;
3832 }
3833 }
3834
3835 return 0;
3836}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02003837
3838API char *
3839lys_path(const struct lys_node *node)
3840{
3841 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
3842 uint16_t index = LY_BUF_SIZE - 1;
3843
3844 if (!node) {
3845 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
3846 return NULL;
3847 }
3848
3849 /* backup the shared internal buffer */
3850 if (ly_buf_used && buf[0]) {
3851 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
3852 }
3853 ly_buf_used++;
3854
3855 /* build the path */
3856 buf[index] = '\0';
Michal Vasko5efa25c2017-01-10 11:34:30 +01003857 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index, 0);
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02003858 result = strdup(&buf[index]);
3859
3860 /* restore the shared internal buffer */
3861 if (buf_backup) {
3862 strcpy(buf, buf_backup);
3863 free(buf_backup);
3864 }
3865 ly_buf_used--;
3866
3867 return result;
3868}
Radek Krejci9ad23f42016-10-31 15:46:52 +01003869