blob: 7484a990a35208aedb6dd9b2430ce224e402a653 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
Michal Vasko2d162e12015-09-24 14:33:29 +02002 * @file tree_schema.c
Radek Krejcida04f4a2015-05-21 12:54:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko2d162e12015-09-24 14:33:29 +02004 * @brief Manipulation with libyang schema data structures
Radek Krejcida04f4a2015-05-21 12:54:09 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
Radek Krejci54f6fb32016-02-24 12:56:39 +010014
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
Radek Krejcida04f4a2015-05-21 12:54:09 +020016
Radek Krejci812b10a2015-05-28 16:48:25 +020017#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020018#include <ctype.h>
Radek Krejcib051f722016-02-25 15:12:21 +010019#include <limits.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020020#include <stdlib.h>
21#include <sys/mman.h>
Michal Vasko662610a2015-12-07 11:25:45 +010022#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <sys/stat.h>
Michal Vasko662610a2015-12-07 11:25:45 +010024#include <fcntl.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020025#include <string.h>
Michal Vasko662610a2015-12-07 11:25:45 +010026#include <unistd.h>
27#include <errno.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020028
29#include "common.h"
30#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020031#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020032#include "resolve.h"
Michal Vasko88c29542015-11-27 14:57:53 +010033#include "xml.h"
Michal Vasko5b3492c2016-07-20 09:37:40 +020034#include "xpath.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020035#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020036#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020037#include "validation.h"
Pavol Vicanf7cc2852016-03-22 23:27:35 +010038#include "parser_yang.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020039
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020040static int
41lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +020042 int tpdftype, struct unres_schema *unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020043
Radek Krejci9ff0a922016-07-14 13:08:05 +020044API const struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +010045lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020046{
Radek Krejci9ff0a922016-07-14 13:08:05 +020047 int i;
Radek Krejci48061fb2015-08-05 15:41:07 +020048
Radek Krejci27fe55e2016-09-13 17:13:35 +020049 if (!node) {
50 return NULL;
51 }
52
Radek Krejci48061fb2015-08-05 15:41:07 +020053check:
54 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
55 /* input/output does not have if-feature, so skip them */
56
57 /* check local if-features */
Michal Vaskoc5c26b02016-06-29 11:10:29 +020058 for (i = 0; i < node->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +020059 if (!resolve_iffeature(&node->iffeature[i])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +020060 return node;
Radek Krejci48061fb2015-08-05 15:41:07 +020061 }
62 }
63 }
64
65 if (!recursive) {
66 return NULL;
67 }
68
69 /* go through parents */
70 if (node->nodetype == LYS_AUGMENT) {
71 /* go to parent actually means go to the target node */
72 node = ((struct lys_node_augment *)node)->target;
Radek Krejci48061fb2015-08-05 15:41:07 +020073 } else if (node->parent) {
74 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +020075 } else {
76 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +020077 }
78
Radek Krejci074bf852015-08-19 14:22:16 +020079 if (recursive == 2) {
80 /* continue only if the node cannot have a data instance */
81 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
82 return NULL;
83 }
84 }
85 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +020086}
87
Michal Vasko1dca6882015-10-22 14:29:42 +020088int
Michal Vasko36cbaa42015-12-14 13:15:48 +010089lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
90 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +020091{
Radek Krejcic071c542016-01-27 14:57:51 +010092 const struct lys_node *node, *parent = NULL;
93 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +010094 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +020095
Michal Vasko36cbaa42015-12-14 13:15:48 +010096 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +020097 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +020098
Michal Vasko36cbaa42015-12-14 13:15:48 +010099 /* fill the lengths in case the caller is so indifferent */
100 if (!mod_name_len) {
101 mod_name_len = strlen(mod_name);
102 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200103 if (!nam_len) {
104 nam_len = strlen(name);
105 }
106
Michal Vasko9e635ac2016-10-17 11:44:09 +0200107 while (siblings && (siblings->nodetype == LYS_USES)) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200108 siblings = siblings->child;
109 }
Michal Vasko9e635ac2016-10-17 11:44:09 +0200110 if (!siblings) {
111 /* unresolved uses */
112 return EXIT_FAILURE;
113 }
114
Michal Vasko680f8b42016-10-17 10:27:37 +0200115 if (siblings->nodetype == LYS_GROUPING) {
116 for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev);
117 if (node->nodetype == LYS_GROUPING) {
118 /* we went through all the siblings, only groupings there - no valid sibling */
119 return EXIT_FAILURE;
120 }
121 /* update siblings to be valid */
122 siblings = node;
123 }
124
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200125 /* set parent correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100126 parent = lys_parent(siblings);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200127
Michal Vasko680f8b42016-10-17 10:27:37 +0200128 /* go up all uses */
129 while (parent && (parent->nodetype == LYS_USES)) {
130 parent = lys_parent(parent);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200131 }
132
Radek Krejcic071c542016-01-27 14:57:51 +0100133 if (!parent) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200134 /* handle situation when there is a top-level uses referencing a foreign grouping */
135 for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node));
136 mod = lys_node_module(node);
Michal Vasko1dca6882015-10-22 14:29:42 +0200137 }
138
Radek Krejcic071c542016-01-27 14:57:51 +0100139 /* try to find the node */
140 node = NULL;
141 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
142 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100143 /* module name comparison */
144 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200145 if (!ly_strequal(node_mod_name, mod_name, 1) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100146 continue;
147 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200148
Radek Krejcic071c542016-01-27 14:57:51 +0100149 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200150 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100151 if (ret) {
152 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200153 }
Radek Krejcic071c542016-01-27 14:57:51 +0100154 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200155 }
156 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200157 }
158
159 return EXIT_FAILURE;
160}
161
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200162int
Michal Vasko1e62a092015-12-01 12:27:20 +0100163lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type,
164 const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200165{
Michal Vasko1e62a092015-12-01 12:27:20 +0100166 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200167
168 assert(siblings && name);
169 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
170
171 /* find the beginning */
172 while (siblings->prev->next) {
173 siblings = siblings->prev;
174 }
175
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200176 if (!mod) {
177 mod = siblings->module;
178 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200179
Michal Vasko4f0dad02016-02-15 14:08:23 +0100180 /* try to find the node */
181 node = NULL;
Michal Vaskodcf98e62016-05-05 17:53:53 +0200182 while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100183 if (!type || (node->nodetype & type)) {
184 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200185 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100186 continue;
187 }
188
Michal Vasko4f0dad02016-02-15 14:08:23 +0100189 /* direct name check */
Radek Krejci749190d2016-02-18 16:26:25 +0100190 if (ly_strequal(node->name, name, 0)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100191 if (ret) {
192 *ret = node;
193 }
194 return EXIT_SUCCESS;
195 }
Radek Krejcic071c542016-01-27 14:57:51 +0100196 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200197 }
198
199 return EXIT_FAILURE;
200}
201
Michal Vasko1e62a092015-12-01 12:27:20 +0100202API const struct lys_node *
203lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200204{
Michal Vasko1e62a092015-12-01 12:27:20 +0100205 const struct lys_node *next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200206
Radek Krejci8bc87f62015-09-02 16:19:05 +0200207 if (!last) {
208 /* first call */
209
210 /* get know where to start */
211 if (parent) {
212 /* schema subtree */
213 next = last = parent->child;
214 } else {
215 /* top level data */
216 assert(module);
217 next = last = module->data;
218 }
Radek Krejci972724f2016-08-12 15:24:40 +0200219 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
220 /* continue with uses content */
221 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200222 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200223 /* continue after the last returned value */
224 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200225 }
226
227repeat:
Michal Vasko7c386e72015-10-07 15:13:33 +0200228 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200229 if (options & LYS_GETNEXT_WITHGROUPING) {
230 return next;
231 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200232 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200233 }
234
Radek Krejci972724f2016-08-12 15:24:40 +0200235 if (!next) { /* cover case when parent is augment */
236 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200237 /* no next element */
238 return NULL;
239 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200240 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200241 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200242 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200243 } else {
244 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200245 }
246
247 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200248 case LYS_INPUT:
249 case LYS_OUTPUT:
250 if (options & LYS_GETNEXT_WITHINOUT) {
251 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200252 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200253 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200254 } else {
255 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200256 }
Radek Krejci972724f2016-08-12 15:24:40 +0200257 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200258
Michal Vaskoa5835e92015-10-20 15:07:39 +0200259 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200260 if (options & LYS_GETNEXT_WITHCASE) {
261 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200262 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200263 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200264 } else {
265 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200266 }
Radek Krejci972724f2016-08-12 15:24:40 +0200267 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200268
Michal Vasko1dca6882015-10-22 14:29:42 +0200269 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200270 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200271 if (options & LYS_GETNEXT_WITHUSES) {
272 return next;
273 } else if (next->child) {
274 next = next->child;
275 } else {
276 next = next->next;
277 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200278 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200279
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200280 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200281 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200282 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200283 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200284 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200285 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200286 case LYS_LIST:
287 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200288 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200289
Radek Krejci972724f2016-08-12 15:24:40 +0200290 case LYS_CONTAINER:
291 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
292 if (next->child) {
293 /* go into */
294 next = next->child;
295 } else {
296 next = next->next;
297 }
298 goto repeat;
299 } else {
300 return next;
301 }
302
Radek Krejci8bc87f62015-09-02 16:19:05 +0200303 case LYS_CHOICE:
304 if (options & LYS_GETNEXT_WITHCHOICE) {
305 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200306 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200307 /* go into */
308 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200309 } else {
310 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200311 }
Radek Krejci972724f2016-08-12 15:24:40 +0200312 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200313
Radek Krejci7f40ce32015-08-12 20:38:46 +0200314 default:
315 /* we should not be here */
316 return NULL;
317 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200318
319
320}
321
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200322void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200323lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200324{
Radek Krejci76512572015-08-04 09:47:08 +0200325 struct lys_node *parent, *first;
Radek Krejcic071c542016-01-27 14:57:51 +0100326 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200327
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200328 if (!node) {
329 return;
330 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200331
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200332 /* unlink from data model if necessary */
333 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100334 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100335 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100336 if (main_module->data == node) {
337 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200338 }
339 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200340
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200341 /* store pointers to important nodes */
342 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200343 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200344 /* handle augments - first, unlink it from the augment parent ... */
345 if (parent->child == node) {
346 parent->child = node->next;
347 }
348 /* and then continue with the target parent */
Radek Krejci76512572015-08-04 09:47:08 +0200349 parent = ((struct lys_node_augment *)parent)->target;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200350 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200351
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200352 /* unlink from parent */
353 if (parent) {
354 if (parent->child == node) {
355 parent->child = node->next;
356 }
357 node->parent = NULL;
358 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200359
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200360 /* unlink from siblings */
361 if (node->prev == node) {
362 /* there are no more siblings */
363 return;
364 }
365 if (node->next) {
366 node->next->prev = node->prev;
367 } else {
368 /* unlinking the last element */
369 if (parent) {
370 first = parent->child;
371 } else {
372 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200373 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200374 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200375 }
376 }
377 first->prev = node->prev;
378 }
379 if (node->prev->next) {
380 node->prev->next = node->next;
381 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200382
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200383 /* clean up the unlinked element */
384 node->next = NULL;
385 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200386}
387
Michal Vasko563ef092015-09-04 13:17:23 +0200388struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100389lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200390{
391 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200392
393 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200394 /* top-level augment, look into module (uses augment is handled correctly below) */
395 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
396 par_iter = par_iter->parent->module->data;
397 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200398 break;
399 }
400 }
401
Michal Vasko6f929da2015-10-02 16:23:25 +0200402 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200403 continue;
404 }
405
406 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
407 if (!stop) {
408 stop = par_iter;
409 } else if (iter == stop) {
410 break;
411 }
412 if (iter->nodetype != LYS_GROUPING) {
413 continue;
414 }
415
Radek Krejcif8426a72015-10-31 23:14:03 +0100416 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200417 return (struct lys_node_grp *)iter;
418 }
419 }
420 }
421
Michal Vasko563ef092015-09-04 13:17:23 +0200422 return NULL;
423}
424
Radek Krejci10c760e2015-08-14 14:45:43 +0200425/*
426 * get next grouping in the root's subtree, in the
427 * first call, tha last is NULL
428 */
429static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200430lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200431{
Radek Krejci10c760e2015-08-14 14:45:43 +0200432 struct lys_node *last = (struct lys_node *)lastgrp;
433 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200434
Radek Krejci10c760e2015-08-14 14:45:43 +0200435 assert(root);
436
437 if (!last) {
438 last = root;
439 }
440
441 while (1) {
442 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
443 next = last->child;
444 } else {
445 next = NULL;
446 }
447 if (!next) {
448 if (last == root) {
449 /* we are done */
450 return NULL;
451 }
452
453 /* no children, go to siblings */
454 next = last->next;
455 }
456 while (!next) {
457 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100458 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200459 /* we are done */
460 return NULL;
461 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200462 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100463 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200464 }
465
466 if (next->nodetype == LYS_GROUPING) {
467 return (struct lys_node_grp *)next;
468 }
469
470 last = next;
471 }
472}
473
Michal Vasko0d343d12015-08-24 14:57:36 +0200474/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200475int
Radek Krejci07911992015-08-14 15:13:31 +0200476lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
477{
Michal Vasko563ef092015-09-04 13:17:23 +0200478 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200479 struct lys_node_grp *grp;
480 int down;
481
482 assert(node);
483
484 if (!parent) {
485 assert(module);
486 } else {
487 module = parent->module;
488 }
489
490 switch (node->nodetype) {
491 case LYS_GROUPING:
492 /* 6.2.1, rule 6 */
493 if (parent) {
494 if (parent->child) {
495 down = 1;
496 start = parent->child;
497 } else {
498 down = 0;
499 start = parent;
500 }
501 } else {
502 down = 1;
503 start = module->data;
504 }
505 /* go up */
Radek Krejcic071c542016-01-27 14:57:51 +0100506 if (lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100507 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200508 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200509 }
510 /* go down, because grouping can be defined after e.g. container in which is collision */
511 if (down) {
512 for (iter = start, stop = NULL; iter; iter = iter->prev) {
513 if (!stop) {
514 stop = start;
515 } else if (iter == stop) {
516 break;
517 }
518 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
519 continue;
520 }
521
522 grp = NULL;
523 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100524 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100525 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200526 return EXIT_FAILURE;
527 }
528 }
529 }
530 }
531 break;
532 case LYS_LEAF:
533 case LYS_LEAFLIST:
534 case LYS_LIST:
535 case LYS_CONTAINER:
536 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200537 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200538 /* 6.2.1, rule 7 */
539 if (parent) {
540 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200541 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
542 if (iter->nodetype == LYS_AUGMENT) {
543 if (((struct lys_node_augment *)iter)->target) {
544 /* augment is resolved, go up */
545 iter = ((struct lys_node_augment *)iter)->target;
546 continue;
547 }
548 /* augment is not resolved, this is the final parent */
549 break;
550 }
Radek Krejci07911992015-08-14 15:13:31 +0200551 iter = iter->parent;
552 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200553
Radek Krejci07911992015-08-14 15:13:31 +0200554 if (!iter) {
555 stop = NULL;
556 iter = module->data;
557 } else {
558 stop = iter;
559 iter = iter->child;
560 }
561 } else {
562 stop = NULL;
563 iter = module->data;
564 }
565 while (iter) {
566 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
567 iter = iter->child;
568 continue;
569 }
570
Radek Krejcibf2abff2016-08-23 15:51:52 +0200571 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100572 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100573 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200574 return EXIT_FAILURE;
575 }
576 }
577
578 /* special case for choice - we must check the choice's name as
579 * well as the names of nodes under the choice
580 */
581 if (iter->nodetype == LYS_CHOICE) {
582 iter = iter->child;
583 continue;
584 }
585
586 /* go to siblings */
587 if (!iter->next) {
588 /* no sibling, go to parent's sibling */
589 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200590 /* for parent LYS_AUGMENT */
591 if (iter->parent == stop) {
592 iter = stop;
593 break;
594 }
595 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200596 if (iter && iter->next) {
597 break;
598 }
599 } while (iter != stop);
600
601 if (iter == stop) {
602 break;
603 }
604 }
605 iter = iter->next;
606 }
607 break;
608 case LYS_CASE:
609 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100610 if (parent) {
611 start = parent->child;
612 } else {
613 start = module->data;
614 }
615
616 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200617 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200618 continue;
619 }
620
Radek Krejci749190d2016-02-18 16:26:25 +0100621 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100622 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200623 return EXIT_FAILURE;
624 }
625 }
626 break;
627 default:
628 /* no check needed */
629 break;
630 }
631
632 return EXIT_SUCCESS;
633}
634
Michal Vasko0d343d12015-08-24 14:57:36 +0200635/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200636int
Radek Krejci10c760e2015-08-14 14:45:43 +0200637lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
638{
Radek Krejci92720552015-10-05 15:28:27 +0200639 struct lys_node *iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200640 struct lys_node_inout *in, *out, *inout;
Radek Krejci07911992015-08-14 15:13:31 +0200641 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200642
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200643 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200644
Radek Krejci10c760e2015-08-14 14:45:43 +0200645 if (parent) {
646 type = parent->nodetype;
647 module = parent->module;
648 } else {
649 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100650 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200651 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200652 }
653
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200654 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200655 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200656 case LYS_CONTAINER:
657 case LYS_LIST:
658 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100659 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200660 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200661 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200662 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200663 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
664 return EXIT_FAILURE;
665 }
666 break;
Radek Krejci76512572015-08-04 09:47:08 +0200667 case LYS_INPUT:
668 case LYS_OUTPUT:
669 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200670 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200671 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200672 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100673 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200674 return EXIT_FAILURE;
675 }
676 break;
Radek Krejci76512572015-08-04 09:47:08 +0200677 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200678 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200679 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100680 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200681 return EXIT_FAILURE;
682 }
683 break;
Radek Krejci76512572015-08-04 09:47:08 +0200684 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200685 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200686 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100687 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 return EXIT_FAILURE;
689 }
690 break;
Radek Krejci76512572015-08-04 09:47:08 +0200691 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200692 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200693 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100694 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200695 return EXIT_FAILURE;
696 }
697 break;
Radek Krejci76512572015-08-04 09:47:08 +0200698 case LYS_LEAF:
699 case LYS_LEAFLIST:
700 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200701 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100702 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
703 LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100704 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200705 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200706 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200707 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200708 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200709 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100710 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200711 return EXIT_FAILURE;
712 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200713 break;
714 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200715 /* top level */
716 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200717 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200718 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100719 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200720 return EXIT_FAILURE;
721 }
722
Radek Krejcic071c542016-01-27 14:57:51 +0100723 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200724 }
725
726 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200727 if (lys_check_id(child, parent, module)) {
728 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200729 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200730
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200731 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200732 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200733 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200734
Radek Krejci41a349b2016-10-24 19:21:59 +0200735 if (child->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
736 /* replace the implicit input/output node */
737 if (child->nodetype == LYS_OUTPUT) {
738 inout = (struct lys_node_inout *)parent->child->next;
739 } else { /* LYS_INPUT */
740 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200741 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200742 }
743 if (inout->next) {
744 child->next = inout->next;
745 inout->next->prev = child;
746 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200747 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200748 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200749 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200750 child->prev = inout->prev;
751 if (inout->prev->next) {
752 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200753 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200754 inout->prev = (struct lys_node *)inout;
755 child->parent = parent;
756 inout->parent = NULL;
757 lys_node_free((struct lys_node *)inout, NULL, 0);
758 } else {
759 /* connect the child correctly */
760 if (!parent) {
761 if (module->data) {
762 module->data->prev->next = child;
763 child->prev = module->data->prev;
764 module->data->prev = child;
765 } else {
766 module->data = child;
767 }
768 } else {
769 if (!parent->child) {
770 /* the only/first child of the parent */
771 parent->child = child;
772 child->parent = parent;
773 iter = child;
774 } else {
775 /* add a new child at the end of parent's child list */
776 iter = parent->child->prev;
777 iter->next = child;
778 child->prev = iter;
779 }
780 while (iter->next) {
781 iter = iter->next;
782 iter->parent = parent;
783 }
784 parent->child->prev = iter;
785 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200786 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200787
Michal Vaskoe022a562016-09-27 14:24:15 +0200788 /* check config value (but ignore them in groupings and augments) */
789 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent);
790 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200791 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
792 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
793 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
794 LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children.");
795 return EXIT_FAILURE;
796 }
797 }
798
Radek Krejci41771502016-04-14 17:52:32 +0200799 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200800 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200801 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200802 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200803 /* store it only into container or list - the only data inner nodes */
804 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
805 if (iter->flags & LYS_INCL_STATUS) {
806 /* done, someone else set it already from here */
807 break;
808 }
809 /* set flag about including status data */
810 iter->flags |= LYS_INCL_STATUS;
811 }
812 }
813 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200814
815 /* create implicit input/output nodes to have available them as possible target for augment */
816 if (child->nodetype & (LYS_RPC | LYS_ACTION)) {
817 in = calloc(1, sizeof *in);
818 in->nodetype = LYS_INPUT;
819 in->name = lydict_insert(child->module->ctx, "input", 5);
820 out = calloc(1, sizeof *out);
821 out->name = lydict_insert(child->module->ctx, "output", 6);
822 out->nodetype = LYS_OUTPUT;
823 in->module = out->module = child->module;
824 in->parent = out->parent = child;
825 in->flags = out->flags = LYS_IMPLICIT;
826 in->next = (struct lys_node *)out;
827 in->prev = (struct lys_node *)out;
828 out->prev = (struct lys_node *)in;
829 child->child = (struct lys_node *)in;
830 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200831 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200832}
833
Radek Krejcia1df1682016-04-11 14:56:59 +0200834static const struct lys_module *
835lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200836{
Radek Krejcia1df1682016-04-11 14:56:59 +0200837 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200838 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200839 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200840
Radek Krejci00a0e712016-10-26 10:24:46 +0200841 ly_err_clean(1);
Radek Krejcif347abc2016-06-22 10:18:47 +0200842
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200843 if (!ctx || !data) {
844 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
845 return NULL;
846 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200847
Radek Krejcia1df1682016-04-11 14:56:59 +0200848 if (!internal && format == LYS_IN_YANG) {
849 /* enlarge data by 2 bytes for flex */
850 len = strlen(data);
851 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
852 if (!enlarged_data) {
853 LOGMEM;
854 return NULL;
855 }
856 memcpy(enlarged_data, data, len);
857 enlarged_data[len] = enlarged_data[len + 1] = '\0';
858 data = enlarged_data;
859 }
860
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200861 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200862 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +0100863 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200864 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200865 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100866 mod = yang_read_module(ctx, data, 0, NULL, 1);
867 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200868 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200869 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200870 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200871 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200872
Radek Krejcia1df1682016-04-11 14:56:59 +0200873 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200874 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200875}
876
Radek Krejcia1df1682016-04-11 14:56:59 +0200877API const struct lys_module *
878lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
879{
880 return lys_parse_mem_(ctx, data, format, 0);
881}
882
Michal Vasko5a721fd2016-02-16 12:16:48 +0100883struct lys_submodule *
884lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200885{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100886 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200887
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200888 assert(module);
889 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +0200890
Radek Krejcic071c542016-01-27 14:57:51 +0100891 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +0200892 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200893
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200894 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200895 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +0100896 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200897 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200898 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100899 submod = yang_read_submodule(module, data, 0, unres);
900 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200901 default:
Radek Krejci90a550a2016-04-13 16:00:58 +0200902 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200903 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200904 }
Radek Krejciefaeba32015-05-27 14:30:57 +0200905
Michal Vasko5a721fd2016-02-16 12:16:48 +0100906 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +0200907}
908
Michal Vasko1e62a092015-12-01 12:27:20 +0100909API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +0100910lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
911{
912 int fd;
913 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +0200914 const char *rev, *dot, *filename;
915 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +0100916
917 if (!ctx || !path) {
918 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
919 return NULL;
920 }
921
922 fd = open(path, O_RDONLY);
923 if (fd == -1) {
924 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
925 return NULL;
926 }
927
928 ret = lys_parse_fd(ctx, fd, format);
929 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100930
Radek Krejcid80c8602016-10-25 11:56:03 +0200931 if (!ret) {
932 /* error */
933 return NULL;
934 }
935
936 /* check that name and revision match filename */
937 filename = strrchr(path, '/');
938 if (!filename) {
939 filename = path;
940 } else {
941 filename++;
942 }
943 rev = strchr(filename, '@');
944 dot = strrchr(filename, '.');
945
946 /* name */
947 len = strlen(ret->name);
948 if (strncmp(filename, ret->name, len) ||
949 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejcic0c82302016-10-25 14:21:33 +0200950 LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +0200951 }
952 if (rev) {
953 len = dot - ++rev;
954 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
955 LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename,
956 ret->rev_size ? ret->rev[0].date : "none");
957 }
958 }
959
960 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100961 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100962 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100963 }
964
Michal Vasko662610a2015-12-07 11:25:45 +0100965 return ret;
966}
967
968API const struct lys_module *
969lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +0200970{
Michal Vasko1e62a092015-12-01 12:27:20 +0100971 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +0200972 struct stat sb;
973 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +0100974 char buf[PATH_MAX];
975 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +0200976
977 if (!ctx || fd < 0) {
978 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
979 return NULL;
980 }
981
Radek Krejci10a833c2015-12-16 15:28:37 +0100982 if (fstat(fd, &sb) == -1) {
983 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
984 return NULL;
985 }
Radek Krejcib051f722016-02-25 15:12:21 +0100986 if (!S_ISREG(sb.st_mode)) {
987 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
988 return NULL;
989 }
990
Michal Vasko164d9012016-04-01 10:16:59 +0200991 if (!sb.st_size) {
992 LOGERR(LY_EINVAL, "File empty.");
993 return NULL;
994 }
995
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100996 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +0100997 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +0100998 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +0100999 return NULL;
1000 }
Radek Krejcia1df1682016-04-11 14:56:59 +02001001 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001002 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001003
Radek Krejcia77904e2016-02-25 16:23:45 +01001004 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001005 /* get URI if there is /proc */
1006 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001007 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1008 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1009 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1010 }
1011 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001012 }
Radek Krejcib051f722016-02-25 15:12:21 +01001013 }
1014
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001015 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001016}
1017
Michal Vasko5a721fd2016-02-16 12:16:48 +01001018struct lys_submodule *
1019lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001020{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001021 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001022 struct stat sb;
1023 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001024
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001025 assert(module);
1026 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001027
Radek Krejci10a833c2015-12-16 15:28:37 +01001028 if (fstat(fd, &sb) == -1) {
1029 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +01001030 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +01001031 }
Michal Vasko164d9012016-04-01 10:16:59 +02001032
1033 if (!sb.st_size) {
1034 LOGERR(LY_EINVAL, "File empty.");
1035 return NULL;
1036 }
1037
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001038 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001039 if (addr == MAP_FAILED) {
1040 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001041 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001042 }
Michal Vasko5a721fd2016-02-16 12:16:48 +01001043 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001044 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +02001045
Michal Vasko5a721fd2016-02-16 12:16:48 +01001046 return submodule;
1047
Radek Krejciefaeba32015-05-27 14:30:57 +02001048}
1049
Radek Krejci1d82ef62015-08-07 14:44:40 +02001050static struct lys_restr *
1051lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001052{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001053 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001054 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001055
Radek Krejci3733a802015-06-19 13:43:21 +02001056 if (!size) {
1057 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001058 }
Radek Krejci3733a802015-06-19 13:43:21 +02001059
1060 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001061 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001062 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001063 return NULL;
1064 }
Radek Krejci3733a802015-06-19 13:43:21 +02001065 for (i = 0; i < size; i++) {
1066 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1067 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1068 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1069 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1070 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1071 }
1072
1073 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001074}
1075
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001076void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001077lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001078{
1079 assert(ctx);
1080 if (!restr) {
1081 return;
1082 }
1083
1084 lydict_remove(ctx, restr->expr);
1085 lydict_remove(ctx, restr->dsc);
1086 lydict_remove(ctx, restr->ref);
1087 lydict_remove(ctx, restr->eapptag);
1088 lydict_remove(ctx, restr->emsg);
1089}
1090
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001091static void
1092lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
1093{
1094 uint8_t i;
1095
1096 for (i = 0; i < iffeature_size; ++i) {
1097 free(iffeature[i].expr);
1098 free(iffeature[i].features);
1099 }
1100 free(iffeature);
1101}
1102
Michal Vaskob84f88a2015-09-24 13:16:10 +02001103static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001104type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001105 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001106{
1107 int i;
1108
1109 switch (base) {
1110 case LY_TYPE_BINARY:
1111 if (old->info.binary.length) {
1112 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1113 }
1114 break;
1115
1116 case LY_TYPE_BITS:
1117 new->info.bits.count = old->info.bits.count;
1118 if (new->info.bits.count) {
1119 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1120 if (!new->info.bits.bit) {
1121 LOGMEM;
1122 return -1;
1123 }
1124 for (i = 0; i < new->info.bits.count; i++) {
1125 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1126 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1127 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1128 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1129 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1130 }
1131 }
1132 break;
1133
1134 case LY_TYPE_DEC64:
1135 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001136 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001137 if (old->info.dec64.range) {
1138 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1139 }
1140 break;
1141
1142 case LY_TYPE_ENUM:
1143 new->info.enums.count = old->info.enums.count;
1144 if (new->info.enums.count) {
1145 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1146 if (!new->info.enums.enm) {
1147 LOGMEM;
1148 return -1;
1149 }
1150 for (i = 0; i < new->info.enums.count; i++) {
1151 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1152 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1153 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1154 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1155 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1156 }
1157 }
1158 break;
1159
1160 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001161 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001162 if (old->info.ident.count) {
1163 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1164 if (!new->info.ident.ref) {
1165 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001166 return -1;
1167 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001168 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1169 } else {
1170 /* there can be several unresolved base identities, duplicate them all */
1171 i = -1;
1172 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1173 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1174 return -1;
1175 }
1176 --i;
1177 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001178 }
1179 break;
1180
1181 case LY_TYPE_INST:
1182 new->info.inst.req = old->info.inst.req;
1183 break;
1184
1185 case LY_TYPE_INT8:
1186 case LY_TYPE_INT16:
1187 case LY_TYPE_INT32:
1188 case LY_TYPE_INT64:
1189 case LY_TYPE_UINT8:
1190 case LY_TYPE_UINT16:
1191 case LY_TYPE_UINT32:
1192 case LY_TYPE_UINT64:
1193 if (old->info.num.range) {
1194 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1195 }
1196 break;
1197
1198 case LY_TYPE_LEAFREF:
1199 if (old->info.lref.path) {
1200 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001201 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001202 return -1;
1203 }
1204 }
1205 break;
1206
1207 case LY_TYPE_STRING:
1208 if (old->info.str.length) {
1209 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1210 }
1211 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1212 new->info.str.pat_count = old->info.str.pat_count;
1213 break;
1214
1215 case LY_TYPE_UNION:
1216 new->info.uni.count = old->info.uni.count;
1217 if (new->info.uni.count) {
1218 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1219 if (!new->info.uni.types) {
1220 LOGMEM;
1221 return -1;
1222 }
1223 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001224 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 +02001225 return -1;
1226 }
1227 }
1228 }
1229 break;
1230
1231 default:
1232 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1233 break;
1234 }
1235 return EXIT_SUCCESS;
1236}
1237
1238struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001239lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1240 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001241{
1242 struct yang_type *new;
1243
1244 new = calloc(1, sizeof *new);
1245 if (!new) {
1246 LOGMEM;
1247 return NULL;
1248 }
1249 new->flags = old->flags;
1250 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001251 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001252 new->type = type;
1253 if (!new->name) {
1254 LOGMEM;
1255 goto error;
1256 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001257 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001258 new->type->base = new->base;
1259 lys_type_free(module->ctx, new->type);
1260 memset(&new->type->info, 0, sizeof new->type->info);
1261 goto error;
1262 }
1263 return new;
1264
1265 error:
1266 free(new);
1267 return NULL;
1268}
1269
1270static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001271lys_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 +02001272 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001273{
1274 int i;
1275
Michal Vasko1dca6882015-10-22 14:29:42 +02001276 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001277 new->base = old->base;
1278 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001279 new->parent = (struct lys_tpdf *)parent;
Radek Krejci3733a802015-06-19 13:43:21 +02001280
Michal Vasko878e38d2016-09-05 12:17:53 +02001281 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001282 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001283 /* HACK (serious one) for unres */
1284 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001285 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001286 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 +02001287 } else {
1288 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1289 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001290 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001291 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001292 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001293 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001294 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001295 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001296 }
1297
Radek Krejci3a5501d2016-07-18 22:03:34 +02001298 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001299}
1300
1301void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001302lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001303{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001304 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001305
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001306 assert(ctx);
1307 if (!type) {
1308 return;
1309 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001310
Michal Vasko1dca6882015-10-22 14:29:42 +02001311 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001312
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001314 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001315 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001316 free(type->info.binary.length);
1317 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001318 case LY_TYPE_BITS:
1319 for (i = 0; i < type->info.bits.count; i++) {
1320 lydict_remove(ctx, type->info.bits.bit[i].name);
1321 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1322 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001323 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001324 }
1325 free(type->info.bits.bit);
1326 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001327
1328 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001329 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001330 free(type->info.dec64.range);
1331 break;
1332
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001333 case LY_TYPE_ENUM:
1334 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001335 lydict_remove(ctx, type->info.enums.enm[i].name);
1336 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1337 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001338 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001339 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001340 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001341 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001342
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001343 case LY_TYPE_INT8:
1344 case LY_TYPE_INT16:
1345 case LY_TYPE_INT32:
1346 case LY_TYPE_INT64:
1347 case LY_TYPE_UINT8:
1348 case LY_TYPE_UINT16:
1349 case LY_TYPE_UINT32:
1350 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001351 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001352 free(type->info.num.range);
1353 break;
1354
Radek Krejcidc4c1412015-06-19 15:39:54 +02001355 case LY_TYPE_LEAFREF:
1356 lydict_remove(ctx, type->info.lref.path);
1357 break;
1358
Radek Krejci3733a802015-06-19 13:43:21 +02001359 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001360 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001361 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001362 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001363 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001364 }
1365 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001366 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001367
Radek Krejcie4c366b2015-07-02 10:11:31 +02001368 case LY_TYPE_UNION:
1369 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001370 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001371 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001372 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001373 break;
1374
Michal Vaskod3282192016-09-05 11:27:57 +02001375 case LY_TYPE_IDENT:
1376 free(type->info.ident.ref);
1377 break;
1378
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001379 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001380 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001381 break;
1382 }
Radek Krejci5a065542015-05-22 15:02:07 +02001383}
1384
Radek Krejci1d82ef62015-08-07 14:44:40 +02001385static void
1386lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001387{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001388 assert(ctx);
1389 if (!tpdf) {
1390 return;
1391 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001392
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001393 lydict_remove(ctx, tpdf->name);
1394 lydict_remove(ctx, tpdf->dsc);
1395 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001396
Radek Krejci1d82ef62015-08-07 14:44:40 +02001397 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001398
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001399 lydict_remove(ctx, tpdf->units);
1400 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001401}
1402
Michal Vaskob84f88a2015-09-24 13:16:10 +02001403static struct lys_tpdf *
1404lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1405{
1406 struct lys_tpdf *result;
1407 int i, j;
1408
1409 if (!size) {
1410 return NULL;
1411 }
1412
1413 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001414 if (!result) {
1415 LOGMEM;
1416 return NULL;
1417 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001418 for (i = 0; i < size; i++) {
1419 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1420 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1421 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1422 result[i].flags = old[i].flags;
1423 result[i].module = old[i].module;
1424
Radek Krejci3a5501d2016-07-18 22:03:34 +02001425 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001426 for (j = 0; j <= i; ++j) {
1427 lys_tpdf_free(mod->ctx, &result[j]);
1428 }
1429 free(result);
1430 return NULL;
1431 }
1432
1433 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1434 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1435 }
1436
1437 return result;
1438}
1439
Radek Krejci1d82ef62015-08-07 14:44:40 +02001440static struct lys_when *
1441lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001442{
Radek Krejci76512572015-08-04 09:47:08 +02001443 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001444
1445 if (!old) {
1446 return NULL;
1447 }
1448
1449 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001450 if (!new) {
1451 LOGMEM;
1452 return NULL;
1453 }
Radek Krejci00768f42015-06-18 17:04:04 +02001454 new->cond = lydict_insert(ctx, old->cond, 0);
1455 new->dsc = lydict_insert(ctx, old->dsc, 0);
1456 new->ref = lydict_insert(ctx, old->ref, 0);
1457
1458 return new;
1459}
1460
Michal Vasko0308dd62015-10-07 09:14:40 +02001461void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001462lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001463{
1464 if (!w) {
1465 return;
1466 }
1467
1468 lydict_remove(ctx, w->cond);
1469 lydict_remove(ctx, w->dsc);
1470 lydict_remove(ctx, w->ref);
1471
1472 free(w);
1473}
1474
Radek Krejcib7f5e412015-08-13 10:15:51 +02001475static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001476lys_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 +02001477{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001478 struct lys_node *next, *sub;
1479
Radek Krejcic071c542016-01-27 14:57:51 +01001480 /* children from a resolved augment are freed under the target node */
Michal Vaskod2fbade2016-05-02 17:14:00 +02001481 if (!aug->target) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001482 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001483 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001484 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001485 }
1486
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001487 lydict_remove(ctx, aug->target_name);
1488 lydict_remove(ctx, aug->dsc);
1489 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001490
Radek Krejci9ff0a922016-07-14 13:08:05 +02001491 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001492
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001493 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001494}
1495
Radek Krejci76512572015-08-04 09:47:08 +02001496static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001497lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001498{
Radek Krejci76512572015-08-04 09:47:08 +02001499 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001500 struct lys_node *old_child, *new_child;
1501 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001502
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001503 if (!size) {
1504 return NULL;
1505 }
Radek Krejci106efc02015-06-10 14:36:27 +02001506
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001507 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001508 if (!new) {
1509 LOGMEM;
1510 return NULL;
1511 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001512 for (i = 0; i < size; i++) {
1513 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1514 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1515 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1516 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001517 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001518 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001519
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001520 /* this must succeed, it was already resolved once */
Radek Krejcidf46e222016-11-08 11:57:37 +01001521 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, 1,
Michal Vasko3edeaf72016-02-11 13:17:43 +01001522 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001523 LOGINT;
1524 free(new);
1525 return NULL;
1526 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001527 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001528
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001529 /* Correct the augment nodes.
1530 * This function can only be called from lys_node_dup() with uses
1531 * being the node duplicated, so we must have a case of grouping
1532 * with a uses with augments. The augmented nodes have already been
1533 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001534 * (it was set to their actual data parent, not an augment), and
1535 * the new augment does not have child pointer to its augment nodes,
1536 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001537 */
1538 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001539 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001540 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001541 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001542 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001543 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001544 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001545 LY_TREE_FOR(old[i].child, old_child) {
1546 /* all augment nodes were connected as siblings, there can be no more after this */
1547 if (old_child->parent != (struct lys_node *)&old[i]) {
1548 break;
1549 }
1550
Radek Krejci749190d2016-02-18 16:26:25 +01001551 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001552
1553 new_child->parent = (struct lys_node *)&new[i];
1554 new_child = new_child->next;
1555 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001556 }
Radek Krejci106efc02015-06-10 14:36:27 +02001557
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001558 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001559}
1560
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001561static const char **
1562lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1563{
1564 int i;
1565 const char **result;
1566
1567 if (!size) {
1568 return NULL;
1569 }
1570
1571 result = calloc(size, sizeof *result);
1572 if (!result) {
1573 LOGMEM;
1574 return NULL;
1575 }
1576
1577 for (i = 0; i < size; i++) {
1578 result[i] = lydict_insert(ctx, old[i], 0);
1579 }
1580 return result;
1581}
1582
Radek Krejci76512572015-08-04 09:47:08 +02001583static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001584lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001585{
Radek Krejci76512572015-08-04 09:47:08 +02001586 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001587 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001588
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001589 if (!size) {
1590 return NULL;
1591 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001592
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001593 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001594 if (!result) {
1595 LOGMEM;
1596 return NULL;
1597 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001598 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001599 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001600 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1601 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001602 result[i].flags = old[i].flags;
1603 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001604
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001605 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001606 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001607
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001608 result[i].dflt_size = old[i].dflt_size;
1609 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
1610
1611 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001612 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001613 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001614 result[i].mod.list = old[i].mod.list;
1615 }
1616 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001617
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001618 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001619}
1620
Radek Krejci1d82ef62015-08-07 14:44:40 +02001621static void
1622lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001623{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001624 assert(ctx);
1625 if (!ident) {
1626 return;
1627 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001628
Radek Krejci018f1f52016-08-03 16:01:20 +02001629 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02001630 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001631 lydict_remove(ctx, ident->name);
1632 lydict_remove(ctx, ident->dsc);
1633 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001634 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejci6793db02015-05-22 17:49:54 +02001635
1636}
1637
Radek Krejci1d82ef62015-08-07 14:44:40 +02001638static void
1639lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001640{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001641 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001642
Radek Krejcid12f57b2015-08-06 10:43:39 +02001643 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001644 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001645 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001646 }
1647 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001648}
1649
Radek Krejci1d82ef62015-08-07 14:44:40 +02001650static void
Michal Vasko44fb6382016-06-29 11:12:27 +02001651lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02001652{
1653 int i;
1654
1655 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1656 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001657 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001658 }
1659 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02001660
1661 for (i = 0; i < io->must_size; i++) {
1662 lys_restr_free(ctx, &io->must[i]);
1663 }
1664 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001665}
1666
Radek Krejci1d82ef62015-08-07 14:44:40 +02001667static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02001668lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
1669{
1670 int i;
1671
1672 for (i = 0; i < notif->must_size; i++) {
1673 lys_restr_free(ctx, &notif->must[i]);
1674 }
1675 free(notif->must);
1676
1677 for (i = 0; i < notif->tpdf_size; i++) {
1678 lys_tpdf_free(ctx, &notif->tpdf[i]);
1679 }
1680 free(notif->tpdf);
1681}
1682static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02001683lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001684{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001685 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001686
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001687 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001688 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001689 }
1690 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001691
Radek Krejci1d82ef62015-08-07 14:44:40 +02001692 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001693}
1694
Radek Krejci1d82ef62015-08-07 14:44:40 +02001695static void
1696lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001697{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001698 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001699
Radek Krejci85a54be2016-10-20 12:39:56 +02001700 /* leafref backlinks */
1701 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001702
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001703 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001704 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001705 }
1706 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001707
Radek Krejci1d82ef62015-08-07 14:44:40 +02001708 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001709
Radek Krejci1d82ef62015-08-07 14:44:40 +02001710 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001711 lydict_remove(ctx, leaf->units);
1712 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001713}
1714
Radek Krejci1d82ef62015-08-07 14:44:40 +02001715static void
1716lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001717{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001718 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001719
Radek Krejci46c4cd72016-01-21 15:13:52 +01001720 if (llist->child) {
1721 /* leafref backlinks */
1722 ly_set_free((struct ly_set *)llist->child);
1723 }
1724
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001725 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001726 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001727 }
1728 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001729
Pavol Vican38321d02016-08-16 14:56:02 +02001730 for (i = 0; i < llist->dflt_size; i++) {
1731 lydict_remove(ctx, llist->dflt[i]);
1732 }
1733 free(llist->dflt);
1734
Radek Krejci1d82ef62015-08-07 14:44:40 +02001735 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001736
Radek Krejci1d82ef62015-08-07 14:44:40 +02001737 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001738 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001739}
1740
Radek Krejci1d82ef62015-08-07 14:44:40 +02001741static void
1742lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001743{
Radek Krejci581ce772015-11-10 17:22:40 +01001744 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001746 /* handle only specific parts for LY_NODE_LIST */
1747 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001748 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001749 }
1750 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001751
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001752 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001753 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001754 }
1755 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001756
Radek Krejci1d82ef62015-08-07 14:44:40 +02001757 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001758
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001759 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02001760 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001761 lydict_remove(ctx, list->unique[i].expr[j]);
1762 }
1763 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001764 }
1765 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001766
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001767 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001768}
1769
Radek Krejci1d82ef62015-08-07 14:44:40 +02001770static void
1771lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001772{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001773 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001774
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001775 /* handle only specific parts for LY_NODE_CONTAINER */
1776 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001777
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001778 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001779 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001780 }
1781 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001782
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001783 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001784 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001785 }
1786 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001787
Radek Krejci1d82ef62015-08-07 14:44:40 +02001788 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001789}
1790
Radek Krejci1d82ef62015-08-07 14:44:40 +02001791static void
1792lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001793{
1794 lydict_remove(ctx, f->name);
1795 lydict_remove(ctx, f->dsc);
1796 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001797 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci9de2c042016-10-19 16:53:06 +02001798 ly_set_free(f->depfeatures);
Radek Krejci3cf9e222015-06-18 11:37:50 +02001799}
1800
Radek Krejci1d82ef62015-08-07 14:44:40 +02001801static void
Michal Vaskoff006c12016-02-17 11:15:19 +01001802lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001803{
Radek Krejci581ce772015-11-10 17:22:40 +01001804 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01001805 struct ly_ctx *ctx;
1806 struct lys_node *next, *elem;
1807
1808 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02001809
1810 lydict_remove(ctx, dev->target_name);
1811 lydict_remove(ctx, dev->dsc);
1812 lydict_remove(ctx, dev->ref);
1813
Pavol Vican64d0b762016-08-25 10:44:59 +02001814 if (!dev->deviate) {
1815 return ;
1816 }
1817
Michal Vaskoff006c12016-02-17 11:15:19 +01001818 /* the module was freed, but we only need the context from orig_node, use ours */
1819 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
1820 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
1821 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
1822 elem->module = module;
1823
1824 LY_TREE_DFS_END(dev->orig_node, next, elem);
1825 }
1826 lys_node_free(dev->orig_node, NULL, 0);
1827 } else {
1828 /* it's just a shallow copy, freeing one node */
1829 dev->orig_node->module = module;
1830 lys_node_free(dev->orig_node, NULL, 1);
1831 }
1832
Radek Krejcieb00f512015-07-01 16:44:58 +02001833 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02001834 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02001835 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02001836 }
1837 free(dev->deviate[i].dflt);
1838
Radek Krejcieb00f512015-07-01 16:44:58 +02001839 lydict_remove(ctx, dev->deviate[i].units);
1840
1841 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1842 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001843 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001844 }
1845 free(dev->deviate[i].must);
1846
1847 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001848 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1849 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1850 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01001851 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001852 }
1853 free(dev->deviate[i].unique);
1854 }
1855 }
1856 free(dev->deviate);
1857}
1858
Radek Krejci1d82ef62015-08-07 14:44:40 +02001859static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001860lys_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 +02001861{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001862 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001863
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001864 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001865 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001866 lydict_remove(ctx, uses->refine[i].dsc);
1867 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001868
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001869 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001870 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001871 }
1872 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001873
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001874 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02001875 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001876 }
1877 free(uses->refine[i].dflt);
1878
1879 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001880 lydict_remove(ctx, uses->refine[i].mod.presence);
1881 }
1882 }
1883 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001884
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001885 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001886 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001887 }
1888 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001889
Radek Krejci1d82ef62015-08-07 14:44:40 +02001890 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001891}
1892
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001893void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001894lys_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 +02001895{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001896 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001897 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001898
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001899 if (!node) {
1900 return;
1901 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001902
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001903 assert(node->module);
1904 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001905
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001906 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001907
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001908 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01001909 if (node->priv && private_destructor) {
1910 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001911 }
1912
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001913 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02001914 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001915 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001916 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001917 lydict_remove(ctx, node->dsc);
1918 lydict_remove(ctx, node->ref);
1919 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001920
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001921 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01001922 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001923 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01001924 }
1925 }
1926
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001927 /* specific part */
1928 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001929 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001930 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001931 break;
Radek Krejci76512572015-08-04 09:47:08 +02001932 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001933 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001934 break;
Radek Krejci76512572015-08-04 09:47:08 +02001935 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001936 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001937 break;
Radek Krejci76512572015-08-04 09:47:08 +02001938 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001939 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001940 break;
Radek Krejci76512572015-08-04 09:47:08 +02001941 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001942 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001943 break;
Radek Krejci76512572015-08-04 09:47:08 +02001944 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001945 case LYS_ANYDATA:
1946 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001947 break;
Radek Krejci76512572015-08-04 09:47:08 +02001948 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001949 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001950 break;
Radek Krejci76512572015-08-04 09:47:08 +02001951 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001952 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001953 break;
Radek Krejci76512572015-08-04 09:47:08 +02001954 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001955 /* do nothing */
1956 break;
Radek Krejci76512572015-08-04 09:47:08 +02001957 case LYS_GROUPING:
1958 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02001959 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001960 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001961 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02001962 case LYS_NOTIF:
1963 lys_notif_free(ctx, (struct lys_node_notif *)node);
1964 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001965 case LYS_INPUT:
1966 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02001967 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001968 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001969 case LYS_UNKNOWN:
1970 LOGINT;
1971 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001972 }
Radek Krejci5a065542015-05-22 15:02:07 +02001973
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001974 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001975 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001976 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001977}
1978
Michal Vasko1e62a092015-12-01 12:27:20 +01001979const struct lys_module *
Radek Krejci0fa54e92016-09-14 14:01:05 +02001980lys_get_implemented_module(const struct lys_module *mod)
1981{
1982 struct ly_ctx *ctx;
1983 int i;
1984
1985 if (!mod || mod->implemented) {
1986 /* invalid argument or the module itself is implemented */
1987 return mod;
1988 }
1989
1990 ctx = mod->ctx;
1991 for (i = 0; i < ctx->models.used; i++) {
1992 if (!ctx->models.list[i]->implemented) {
1993 continue;
1994 }
1995
1996 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
1997 /* we have some revision of the module implemented */
1998 return ctx->models.list[i];
1999 }
2000 }
2001
2002 /* we have no revision of the module implemented, return the module itself,
2003 * it is up to the caller to set the module implemented when needed */
2004 return mod;
2005}
2006
2007const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01002008lys_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 +02002009{
Radek Krejcic071c542016-01-27 14:57:51 +01002010 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02002011 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002012 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002013
Michal Vaskoa7789a82016-02-11 15:42:55 +01002014 assert(!prefix || !name);
2015
Michal Vaskob6729c62015-10-21 12:09:47 +02002016 if (prefix && !pref_len) {
2017 pref_len = strlen(prefix);
2018 }
2019 if (name && !name_len) {
2020 name_len = strlen(name);
2021 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002022
Radek Krejcic4283442016-04-22 09:19:27 +02002023 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002024
2025 /* module own prefix, submodule own prefix, (sub)module own name */
2026 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2027 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002028 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002029 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002030 }
2031
Michal Vasko89563fc2016-07-28 16:19:35 +02002032 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002033 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002034 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2035 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002036 return module->imp[i].module;
2037 }
2038 }
2039
Michal Vasko89563fc2016-07-28 16:19:35 +02002040 /* module required by a foreign grouping, deviation, or submodule */
2041 if (name) {
2042 str = strndup(name, name_len);
2043 if (!str) {
2044 LOGMEM;
2045 return NULL;
2046 }
2047 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2048 free(str);
2049 return main_module;
2050 }
2051
Michal Vasko8ce24d72015-10-21 11:27:26 +02002052 return NULL;
2053}
2054
Michal Vasko13b15832015-08-19 11:04:48 +02002055/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002056static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002057module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002058{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002059 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002060 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002061 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002062
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002063 assert(module->ctx);
2064 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002065
Michal Vaskob746fff2016-02-11 11:37:50 +01002066 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002067 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002068 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002069 lydict_remove(ctx, module->imp[i].dsc);
2070 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci225376f2016-02-16 17:36:22 +01002071 }
Radek Krejcidce51452015-06-16 15:20:08 +02002072 free(module->imp);
2073
Radek Krejcic071c542016-01-27 14:57:51 +01002074 /* submodules don't have data tree, the data nodes
2075 * are placed in the main module altogether */
2076 if (!module->type) {
2077 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002078 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002079 }
Radek Krejci21181962015-06-30 14:11:00 +02002080 }
Radek Krejci5a065542015-05-22 15:02:07 +02002081
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002082 lydict_remove(ctx, module->dsc);
2083 lydict_remove(ctx, module->ref);
2084 lydict_remove(ctx, module->org);
2085 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002086 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002087
Radek Krejcieb00f512015-07-01 16:44:58 +02002088 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002089 for (i = 0; i < module->rev_size; i++) {
2090 lydict_remove(ctx, module->rev[i].dsc);
2091 lydict_remove(ctx, module->rev[i].ref);
2092 }
2093 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002094
Radek Krejcieb00f512015-07-01 16:44:58 +02002095 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002096 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002097 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002098 }
2099 module->ident_size = 0;
2100 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002101
Radek Krejcieb00f512015-07-01 16:44:58 +02002102 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002103 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002104 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002105 }
2106 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002107
Radek Krejcieb00f512015-07-01 16:44:58 +02002108 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002109 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002110 lydict_remove(ctx, module->inc[i].dsc);
2111 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcic071c542016-01-27 14:57:51 +01002112 /* complete submodule free is done only from main module since
2113 * submodules propagate their includes to the main module */
2114 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002115 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002116 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002117 }
2118 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002119
Radek Krejcieb00f512015-07-01 16:44:58 +02002120 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002121 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002122 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002123 }
2124 free(module->augment);
2125
Radek Krejcieb00f512015-07-01 16:44:58 +02002126 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002127 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002128 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002129 }
2130 free(module->features);
2131
Radek Krejcieb00f512015-07-01 16:44:58 +02002132 /* deviations */
2133 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002134 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002135 }
2136 free(module->deviation);
2137
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002138 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002139 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002140}
2141
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002142void
Michal Vaskob746fff2016-02-11 11:37:50 +01002143lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002144{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002145 if (!submodule) {
2146 return;
2147 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002148
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002149 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002150 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002151
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002152 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002153
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002154 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002155}
2156
Radek Krejci3a5501d2016-07-18 22:03:34 +02002157static int
2158ingrouping(const struct lys_node *node)
2159{
2160 const struct lys_node *iter = node;
2161 assert(node);
2162
2163 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2164 if (!iter) {
2165 return 0;
2166 } else {
2167 return 1;
2168 }
2169}
2170
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002171/*
2172 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2173 */
2174static struct lys_node *
2175lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2176 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002177{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002178 struct lys_node *retval = NULL, *iter, *p;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002179 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002180 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002181 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002182 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002183 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002184
Michal Vaskoc07187d2015-08-13 15:20:57 +02002185 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002186 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002187 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002188 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002189 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002190 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002191 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002192 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002193 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002194 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002195 struct lys_node_anydata *any = NULL;
2196 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002197 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002198 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002199 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002200 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002201 struct lys_node_rpc_action *rpc = NULL;
2202 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2203 struct lys_node_inout *io = NULL;
2204 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2205 struct lys_node_rpc_action *ntf = NULL;
2206 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002207 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002208 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002209
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002210 /* we cannot just duplicate memory since the strings are stored in
2211 * dictionary and we need to update dictionary counters.
2212 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002213
Radek Krejci1d82ef62015-08-07 14:44:40 +02002214 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002215 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002216 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002217 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002218 break;
2219
Radek Krejci76512572015-08-04 09:47:08 +02002220 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002221 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002222 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002223 break;
2224
Radek Krejci76512572015-08-04 09:47:08 +02002225 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002226 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002227 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002228 break;
2229
Radek Krejci76512572015-08-04 09:47:08 +02002230 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002231 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002232 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002233 break;
2234
Radek Krejci76512572015-08-04 09:47:08 +02002235 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002236 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002237 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002238 break;
2239
Radek Krejci76512572015-08-04 09:47:08 +02002240 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002241 case LYS_ANYDATA:
2242 any = calloc(1, sizeof *any);
2243 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002244 break;
2245
Radek Krejci76512572015-08-04 09:47:08 +02002246 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002247 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002248 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002249 break;
2250
Radek Krejci76512572015-08-04 09:47:08 +02002251 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002252 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002253 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002254 break;
2255
Radek Krejci76512572015-08-04 09:47:08 +02002256 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002257 grp = calloc(1, sizeof *grp);
2258 retval = (struct lys_node *)grp;
2259 break;
2260
Radek Krejci76512572015-08-04 09:47:08 +02002261 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002262 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002263 rpc = calloc(1, sizeof *rpc);
2264 retval = (struct lys_node *)rpc;
2265 break;
2266
Radek Krejci76512572015-08-04 09:47:08 +02002267 case LYS_INPUT:
2268 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002269 io = calloc(1, sizeof *io);
2270 retval = (struct lys_node *)io;
2271 break;
2272
Radek Krejci76512572015-08-04 09:47:08 +02002273 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002274 ntf = calloc(1, sizeof *ntf);
2275 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002276 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002277
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002278 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002279 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002280 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002281 }
Radek Krejcib388c152015-06-04 17:03:03 +02002282
Michal Vasko253035f2015-12-17 16:58:13 +01002283 if (!retval) {
2284 LOGMEM;
2285 return NULL;
2286 }
2287
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002288 /*
2289 * duplicate generic part of the structure
2290 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002291 retval->name = lydict_insert(ctx, node->name, 0);
2292 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2293 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002294 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002295 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002296
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002297 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002298 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002299
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002300 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002301
Radek Krejci06214042016-11-04 16:25:58 +01002302 if (node->iffeature_size) {
2303 retval->iffeature_size = node->iffeature_size;
2304 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2305 if (!retval->iffeature) {
2306 LOGMEM;
2307 goto error;
2308 }
Michal Vasko253035f2015-12-17 16:58:13 +01002309 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002310
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002311 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002312 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002313 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2314 if (size1) {
2315 /* there is something to duplicate */
2316
2317 /* duplicate compiled expression */
2318 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2319 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2320 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2321
2322 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002323 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002324 for (j = 0; (unsigned int)j < size2; j++) {
2325 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2326 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002327 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002328 /* feature is resolved in origin, so copy it
2329 * - duplication is used for instantiating groupings
2330 * and if-feature inside grouping is supposed to be
2331 * resolved inside the original grouping, so we want
2332 * to keep pointers to features from the grouping
2333 * context */
2334 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2335 } else if (rc == -1) {
2336 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002337 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002338 }
2339 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002340 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002341
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002342 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002343 p = parent;
2344 do {
2345 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2346 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002347 if (iter) {
2348 flags = iter->flags & LYS_CONFIG_MASK;
2349 } else {
2350 /* default */
2351 flags = LYS_CONFIG_W;
2352 }
2353
2354 switch (finalize) {
2355 case 1:
2356 /* inherit config flags */
2357 if (retval->flags & LYS_CONFIG_SET) {
2358 /* skip nodes with an explicit config value */
2359 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2360 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2361 LOGVAL(LYE_SPEC, LY_VLOG_LYS, retval, "State nodes cannot have configuration nodes as children.");
2362 goto error;
2363 }
2364 break;
2365 }
2366
2367 if (retval->nodetype != LYS_USES) {
2368 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2369 }
2370 break;
2371 case 2:
2372 /* erase config flags */
2373 retval->flags &= ~LYS_CONFIG_MASK;
2374 retval->flags &= ~LYS_CONFIG_SET;
2375 break;
2376 }
2377
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002378 /* connect it to the parent */
2379 if (lys_node_addchild(parent, retval->module, retval)) {
2380 goto error;
2381 }
Radek Krejcidce51452015-06-16 15:20:08 +02002382
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002383 /* go recursively */
2384 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002385 LY_TREE_FOR(node->child, iter) {
2386 if (!lys_node_dup_recursion(module, retval, iter, retval->nacm, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002387 goto error;
2388 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002389 }
2390 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002391
2392 if (finalize == 1) {
2393 /* check that configuration lists have keys
2394 * - we really want to check keys_size in original node, because the keys are
2395 * not yet resolved here, it is done below in nodetype specific part */
2396 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2397 && !((struct lys_node_list *)node)->keys_size) {
2398 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2399 goto error;
2400 }
2401 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002402 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002403 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002404 }
2405
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002406 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002407 * duplicate specific part of the structure
2408 */
2409 switch (node->nodetype) {
2410 case LYS_CONTAINER:
2411 if (cont_orig->when) {
2412 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002413 }
2414 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002415
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002416 cont->must_size = cont_orig->must_size;
2417 cont->tpdf_size = cont_orig->tpdf_size;
2418
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002419 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002420 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002421 break;
2422
2423 case LYS_CHOICE:
2424 if (choice_orig->when) {
2425 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002426 }
2427
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002428 if (!shallow) {
2429 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002430 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2431 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2432 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002433 if (rc) {
2434 if (rc == EXIT_FAILURE) {
2435 LOGINT;
2436 }
2437 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002438 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002439 } else {
2440 /* useless to check return value, we don't know whether
2441 * there really wasn't any default defined or it just hasn't
2442 * been resolved, we just hope for the best :)
2443 */
2444 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002445 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002446 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002447 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002448 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002449 break;
2450
2451 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002452 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002453 goto error;
2454 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002455 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2456
2457 if (leaf_orig->dflt) {
2458 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci51673202016-11-01 17:00:32 +01002459 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT,
2460 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002461 goto error;
2462 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002463 }
2464
2465 leaf->must_size = leaf_orig->must_size;
2466 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002467
2468 if (leaf_orig->when) {
2469 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002470 }
2471 break;
2472
2473 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002474 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002475 goto error;
2476 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002477 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2478
2479 llist->min = llist_orig->min;
2480 llist->max = llist_orig->max;
2481
2482 llist->must_size = llist_orig->must_size;
2483 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002484
Radek Krejci51673202016-11-01 17:00:32 +01002485 llist->dflt_size = llist_orig->dflt_size;
2486 llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt);
2487 for (i = 0; i < llist->dflt_size; i++) {
2488 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
2489 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
2490 (struct lys_node *)(&llist->dflt[i])) == -1) {
2491 goto error;
2492 }
2493 }
2494
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002495 if (llist_orig->when) {
2496 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002497 }
2498 break;
2499
2500 case LYS_LIST:
2501 list->min = list_orig->min;
2502 list->max = list_orig->max;
2503
2504 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002505 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002506
Radek Krejci581ce772015-11-10 17:22:40 +01002507 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002508 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002509
Radek Krejci581ce772015-11-10 17:22:40 +01002510 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002511 if (list->keys_size) {
2512 list->keys = calloc(list->keys_size, sizeof *list->keys);
Radek Krejci5c08a992016-11-02 13:30:04 +01002513 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Michal Vasko253035f2015-12-17 16:58:13 +01002514 if (!list->keys) {
2515 LOGMEM;
2516 goto error;
2517 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002518
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002519 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01002520 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
2521 * in another grouping */
2522 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
2523 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
2524 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02002525 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002526 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002527 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002528 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002529 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002530
Radek Krejci581ce772015-11-10 17:22:40 +01002531 list->unique_size = list_orig->unique_size;
2532 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002533 if (!list->unique) {
2534 LOGMEM;
2535 goto error;
2536 }
Radek Krejci581ce772015-11-10 17:22:40 +01002537 for (i = 0; i < list->unique_size; ++i) {
2538 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2539 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002540 if (!list->unique[i].expr) {
2541 LOGMEM;
2542 goto error;
2543 }
Radek Krejci581ce772015-11-10 17:22:40 +01002544 for (j = 0; j < list->unique[i].expr_size; j++) {
2545 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2546
2547 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02002548 unique_info = malloc(sizeof *unique_info);
2549 unique_info->list = (struct lys_node *)list;
2550 unique_info->expr = list->unique[i].expr[j];
2551 unique_info->trg_type = &list->unique[i].trg_type;
2552 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002553 }
2554 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002555
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002556 if (list_orig->when) {
2557 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02002558 }
Radek Krejcidce51452015-06-16 15:20:08 +02002559 break;
2560
2561 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002562 case LYS_ANYDATA:
2563 any->must_size = any_orig->must_size;
2564 any->must = lys_restr_dup(ctx, any_orig->must, any->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002565
Radek Krejcibf2abff2016-08-23 15:51:52 +02002566 if (any_orig->when) {
2567 any->when = lys_when_dup(ctx, any_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002568 }
2569 break;
2570
2571 case LYS_USES:
2572 uses->grp = uses_orig->grp;
2573
2574 if (uses_orig->when) {
2575 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002576 }
2577
2578 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002579 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002580 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002581 if (!shallow) {
2582 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Michal Vaskocda51712016-05-19 15:22:11 +02002583 if (!uses->grp || uses->grp->nacm) {
2584 assert(!uses->child);
Radek Krejci48464ed2016-03-17 15:44:09 +01002585 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002586 goto error;
2587 }
Michal Vasko49168a22015-08-17 16:35:41 +02002588 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002589 } else {
2590 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002591 }
2592 break;
2593
Radek Krejcidce51452015-06-16 15:20:08 +02002594 case LYS_CASE:
2595 if (cs_orig->when) {
2596 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02002597 }
2598 break;
2599
2600 case LYS_GROUPING:
2601 grp->tpdf_size = grp_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002602 grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002603 break;
2604
Radek Krejci96935402016-11-04 16:27:28 +01002605 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02002606 case LYS_RPC:
2607 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002608 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02002609 break;
2610
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002611 case LYS_INPUT:
2612 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02002613 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002614 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002615 break;
2616
Radek Krejcida04f4a2015-05-21 12:54:09 +02002617 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002618 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002619 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002620 break;
2621
Radek Krejci96935402016-11-04 16:27:28 +01002622
Radek Krejcida04f4a2015-05-21 12:54:09 +02002623 default:
2624 /* LY_NODE_AUGMENT */
2625 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002626 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002627 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002628
2629 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002630
2631error:
2632
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002633 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02002634 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002635}
2636
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002637struct lys_node *
2638lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm,
2639 struct unres_schema *unres, int shallow)
2640{
2641 struct lys_node *p = NULL;
Radek Krejci3c48d042016-11-07 14:42:36 +01002642 int finalize = 0, has_xpath;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002643 struct lys_node *result, *iter, *next;
2644
2645 if (!shallow) {
2646 /* get know where in schema tre we are to know what should be done during instantiation of the grouping */
2647 for (p = parent;
2648 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
2649 p = lys_parent(p));
2650 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
2651 }
2652
2653 result = lys_node_dup_recursion(module, parent, node, nacm, unres, shallow, finalize);
2654 if (finalize) {
2655 /* check xpath expressions in the instantiated tree */
2656 for (iter = next = parent->child; iter; iter = next) {
Radek Krejcib07da472016-11-08 08:27:40 +01002657 has_xpath = 0;
Radek Krejci3c48d042016-11-07 14:42:36 +01002658 switch (iter->nodetype) {
2659 case LYS_AUGMENT:
2660 if (((struct lys_node_augment *)iter)->when) {
2661 has_xpath = 1;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002662 }
Radek Krejci3c48d042016-11-07 14:42:36 +01002663 break;
2664 case LYS_CASE:
2665 if (((struct lys_node_case *)iter)->when) {
2666 has_xpath = 1;
2667 }
2668 break;
2669 case LYS_CHOICE:
2670 if (((struct lys_node_choice *)iter)->when) {
2671 has_xpath = 1;
2672 }
2673 break;
2674 case LYS_ANYDATA:
2675 if (((struct lys_node_anydata *)iter)->when || ((struct lys_node_anydata *)iter)->must_size) {
2676 has_xpath = 1;
2677 }
2678 break;
2679 case LYS_LEAF:
2680 if (((struct lys_node_leaf *)iter)->when || ((struct lys_node_leaf *)iter)->must_size) {
2681 has_xpath = 1;
2682 }
2683 break;
2684 case LYS_LEAFLIST:
2685 if (((struct lys_node_leaflist *)iter)->when || ((struct lys_node_leaflist *)iter)->must_size) {
2686 has_xpath = 1;
2687 }
2688 break;
2689 case LYS_LIST:
2690 if (((struct lys_node_list *)iter)->when || ((struct lys_node_list *)iter)->must_size) {
2691 has_xpath = 1;
2692 }
2693 break;
2694 case LYS_CONTAINER:
2695 if (((struct lys_node_container *)iter)->when || ((struct lys_node_container *)iter)->must_size) {
2696 has_xpath = 1;
2697 }
2698 break;
2699 case LYS_INPUT:
2700 case LYS_OUTPUT:
2701 if (((struct lys_node_inout *)iter)->must_size) {
2702 has_xpath = 1;
2703 }
2704 break;
2705 case LYS_NOTIF:
2706 if (((struct lys_node_notif *)iter)->must_size) {
2707 has_xpath = 1;
2708 }
2709 break;
2710 case LYS_USES:
2711 if (((struct lys_node_uses *)iter)->when) {
2712 has_xpath = 1;
2713 }
2714 break;
2715 default:
Radek Krejcib07da472016-11-08 08:27:40 +01002716 /* do nothing, has_xpath is zero */;
2717 break;
Radek Krejci3c48d042016-11-07 14:42:36 +01002718 }
2719 if (has_xpath && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
2720 /* invalid xpath */
2721 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002722 }
2723
2724 /* select next item */
2725 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
2726 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
2727 next = NULL;
2728 } else {
2729 next = iter->child;
2730 }
2731 if (!next) {
2732 /* no children, try siblings */
2733 next = iter->next;
2734 }
2735 while (!next) {
2736 /* parent is already processed, go to its sibling */
2737 iter = lys_parent(iter);
2738 if (iter == parent) {
2739 /* we are done, no next element to process */
2740 break;
2741 }
2742 next = iter->next;
2743 }
2744 }
2745 }
2746
2747 return result;
2748}
2749
Michal Vasko13b15832015-08-19 11:04:48 +02002750void
Michal Vaskoff006c12016-02-17 11:15:19 +01002751lys_node_switch(struct lys_node *dst, struct lys_node *src)
2752{
2753 struct lys_node *child;
2754
Michal Vaskob42b6972016-06-06 14:21:30 +02002755 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01002756
2757 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02002758 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002759 dst->prev->next = src;
2760 }
2761
2762 /* sibling prev */
2763 if (dst->next) {
2764 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02002765 } else {
2766 for (child = dst->prev; child->prev->next; child = child->prev);
2767 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01002768 }
2769
2770 /* next */
2771 src->next = dst->next;
2772 dst->next = NULL;
2773
2774 /* prev */
2775 if (dst->prev != dst) {
2776 src->prev = dst->prev;
2777 }
2778 dst->prev = dst;
2779
2780 /* parent child */
2781 if (dst->parent && (dst->parent->child == dst)) {
2782 dst->parent->child = src;
2783 }
2784
2785 /* parent */
2786 src->parent = dst->parent;
2787 dst->parent = NULL;
2788
2789 /* child parent */
2790 LY_TREE_FOR(dst->child, child) {
2791 if (child->parent == dst) {
2792 child->parent = src;
2793 }
2794 }
2795
2796 /* child */
2797 src->child = dst->child;
2798 dst->child = NULL;
2799}
2800
2801void
Michal Vasko627975a2016-02-11 11:39:03 +01002802lys_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 +02002803{
2804 struct ly_ctx *ctx;
2805 int i;
2806
2807 if (!module) {
2808 return;
2809 }
2810
2811 /* remove schema from the context */
2812 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01002813 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02002814 for (i = 0; i < ctx->models.used; i++) {
2815 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01002816 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02002817 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01002818 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 +02002819 ctx->models.list[ctx->models.used] = NULL;
2820 /* we are done */
2821 break;
2822 }
2823 }
2824 }
2825
2826 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01002827 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002828
2829 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01002830 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002831
2832 free(module);
2833}
Radek Krejci7e97c352015-06-19 16:26:34 +02002834
Radek Krejci9de2c042016-10-19 16:53:06 +02002835static void
2836lys_features_disable_recursive(struct lys_feature *f)
2837{
2838 unsigned int i;
2839 struct lys_feature *depf;
2840
2841 /* disable the feature */
2842 f->flags &= ~LYS_FENABLED;
2843
2844 /* by disabling feature we have to disable also all features that depends on this feature */
2845 if (f->depfeatures) {
2846 for (i = 0; i < f->depfeatures->number; i++) {
2847 depf = (struct lys_feature *)f->depfeatures->set.g[i];
2848 if (depf->flags & LYS_FENABLED) {
2849 lys_features_disable_recursive(depf);
2850 }
2851 }
2852 }
2853}
2854
2855
Radek Krejci7e97c352015-06-19 16:26:34 +02002856/*
2857 * op: 1 - enable, 0 - disable
2858 */
2859static int
Michal Vasko1e62a092015-12-01 12:27:20 +01002860lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002861{
2862 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002863 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02002864 int progress, faili, failj, failk;
2865
2866 uint8_t fsize;
2867 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02002868
2869 if (!module || !name || !strlen(name)) {
2870 return EXIT_FAILURE;
2871 }
2872
2873 if (!strcmp(name, "*")) {
2874 /* enable all */
2875 all = 1;
2876 }
2877
Radek Krejcia889c1f2016-10-19 15:50:11 +02002878 progress = failk = 1;
2879 while (progress && failk) {
2880 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
2881 if (i == -1) {
2882 fsize = module->features_size;
2883 f = module->features;
2884 } else {
2885 fsize = module->inc[i].submodule->features_size;
2886 f = module->inc[i].submodule->features;
2887 }
2888
2889 for (j = 0; j < fsize; j++) {
2890 if (all || !strcmp(f[j].name, name)) {
2891 /* skip already set features */
2892 if (op && (f[j].flags & LYS_FENABLED)) {
2893 continue;
2894 } else if (!op && !(f[j].flags & LYS_FENABLED)) {
2895 continue;
2896 }
2897
2898 if (op) {
2899 /* check referenced features if they are enabled */
2900 for (k = 0; k < f[j].iffeature_size; k++) {
2901 if (!resolve_iffeature(&f[j].iffeature[k])) {
2902 if (all) {
2903 faili = i;
2904 failj = j;
2905 failk = k + 1;
2906 break;
2907 } else {
2908 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2909 f[j].name, k + 1);
2910 return EXIT_FAILURE;
2911 }
2912 }
2913 }
2914
2915 if (k == f[j].iffeature_size) {
2916 /* the last check passed, do the change */
2917 f[j].flags |= LYS_FENABLED;
2918 progress++;
2919 }
2920 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02002921 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02002922 progress++;
2923 }
2924 if (!all) {
2925 /* stop in case changing a single feature */
2926 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002927 }
2928 }
Radek Krejci7e97c352015-06-19 16:26:34 +02002929 }
2930 }
2931 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02002932 if (failk) {
2933 /* print info about the last failing feature */
2934 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
2935 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
2936 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02002937 }
2938
2939 if (all) {
2940 return EXIT_SUCCESS;
2941 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02002942 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02002943 return EXIT_FAILURE;
2944 }
2945}
2946
2947API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002948lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002949{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002950 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002951}
2952
2953API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002954lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002955{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002956 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002957}
2958
2959API int
Michal Vasko1e62a092015-12-01 12:27:20 +01002960lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002961{
2962 int i, j;
2963
2964 if (!module || !feature) {
2965 return -1;
2966 }
2967
2968 /* search for the specified feature */
2969 /* module itself */
2970 for (i = 0; i < module->features_size; i++) {
2971 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002972 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002973 return 1;
2974 } else {
2975 return 0;
2976 }
2977 }
2978 }
2979
2980 /* submodules */
2981 for (j = 0; j < module->inc_size; j++) {
2982 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2983 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002984 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002985 return 1;
2986 } else {
2987 return 0;
2988 }
2989 }
2990 }
2991 }
2992
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002993 /* feature definition not found */
2994 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002995}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002996
Radek Krejci96a10da2015-07-30 11:00:14 +02002997API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01002998lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002999{
Radek Krejci96a10da2015-07-30 11:00:14 +02003000 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003001 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003002 unsigned int count;
3003
3004 if (!module) {
3005 return NULL;
3006 }
3007
3008 count = module->features_size;
3009 for (i = 0; i < module->inc_size; i++) {
3010 count += module->inc[i].submodule->features_size;
3011 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003012 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01003013 if (!result) {
3014 LOGMEM;
3015 return NULL;
3016 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003017 if (states) {
3018 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01003019 if (!(*states)) {
3020 LOGMEM;
3021 free(result);
3022 return NULL;
3023 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003024 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003025 count = 0;
3026
3027 /* module itself */
3028 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003029 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003030 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003031 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003032 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003033 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003034 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003035 }
3036 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003037 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003038 }
3039
3040 /* submodules */
3041 for (j = 0; j < module->inc_size; j++) {
3042 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003043 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003044 if (states) {
3045 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3046 (*states)[count] = 1;
3047 } else {
3048 (*states)[count] = 0;
3049 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003050 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003051 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003052 }
3053 }
3054
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003055 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003056 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003057
3058 return result;
3059}
Michal Vaskobaefb032015-09-24 14:52:10 +02003060
Radek Krejci6910a032016-04-13 10:06:21 +02003061API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003062lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003063{
3064 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3065}
3066
Radek Krejci6910a032016-04-13 10:06:21 +02003067API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003068lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003069{
3070 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3071}
3072
Michal Vaskobaefb032015-09-24 14:52:10 +02003073API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003074lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003075{
3076 if (!node || !node->parent) {
3077 return NULL;
3078 }
3079
3080 if (node->parent->nodetype == LYS_AUGMENT) {
3081 return ((struct lys_node_augment *)node->parent)->target;
3082 }
3083
3084 return node->parent;
3085}
Michal Vasko1b229152016-01-13 11:28:38 +01003086
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003087API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003088lys_set_private(const struct lys_node *node, void *priv)
3089{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003090 void *prev;
3091
Michal Vasko1b229152016-01-13 11:28:38 +01003092 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003093 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
3094 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003095 }
3096
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003097 prev = node->priv;
3098 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003099
3100 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003101}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003102
Michal Vasko01c6fd22016-05-20 11:43:05 +02003103int
3104lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3105{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003106 struct lys_node_leaf *iter = leafref_target;
3107
Michal Vasko48a573d2016-07-01 11:46:02 +02003108 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003109 LOGINT;
3110 return -1;
3111 }
3112
Pavol Vican93175152016-08-30 15:34:44 +02003113 /* check for config flag */
3114 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
3115 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
3116 "The %s is config but refers to a non-config %s.",
3117 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3118 return -1;
3119 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003120 /* check for cycles */
3121 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3122 if ((void *)iter == (void *)leafref) {
3123 /* cycle detected */
3124 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3125 return -1;
3126 }
3127 iter = iter->type.info.lref.target;
3128 }
3129
3130 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003131 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003132 if (!leafref_target->backlinks) {
3133 leafref_target->backlinks = (void*)ly_set_new();
3134 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003135 LOGMEM;
3136 return -1;
3137 }
3138 }
Radek Krejci85a54be2016-10-20 12:39:56 +02003139 ly_set_add((struct ly_set *)leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003140
3141 return 0;
3142}
3143
Michal Vasko8548e082016-07-22 12:00:18 +02003144/* not needed currently */
3145#if 0
3146
Michal Vasko5b3492c2016-07-20 09:37:40 +02003147static const char *
3148lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3149{
3150 struct lys_module *prev_mod;
3151 uint32_t str_len, mod_len, buf_idx;
3152
Radek Krejcibf2abff2016-08-23 15:51:52 +02003153 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003154 LOGINT;
3155 return NULL;
3156 }
3157
3158 buf_idx = buf_len - 1;
3159 buf[buf_idx] = '\0';
3160
3161 while (node) {
3162 if (lys_parent(node)) {
3163 prev_mod = lys_node_module(lys_parent(node));
3164 } else {
3165 prev_mod = NULL;
3166 }
3167
Radek Krejcibf2abff2016-08-23 15:51:52 +02003168 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003169 str_len = strlen(node->name);
3170
3171 if (prev_mod != node->module) {
3172 mod_len = strlen(node->module->name);
3173 } else {
3174 mod_len = 0;
3175 }
3176
3177 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3178 LOGINT;
3179 return NULL;
3180 }
3181
3182 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3183
3184 buf[buf_idx] = '/';
3185 if (mod_len) {
3186 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3187 buf[buf_idx + 1 + mod_len] = ':';
3188 }
3189 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3190 }
3191
3192 node = lys_parent(node);
3193 }
3194
3195 return buf + buf_idx;
3196}
3197
Michal Vasko8548e082016-07-22 12:00:18 +02003198#endif
3199
3200API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02003201lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003202{
3203 struct lyxp_set set;
3204 struct ly_set *ret_set;
3205 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003206 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003207
3208 if (!node || !expr) {
3209 ly_errno = LY_EINVAL;
3210 return NULL;
3211 }
3212
3213 memset(&set, 0, sizeof set);
3214
Michal Vaskodb1da032016-09-08 10:07:38 +02003215 opts = LYXP_SNODE;
3216 if (options & LYS_FIND_OUTPUT) {
3217 opts |= LYXP_SNODE_OUTPUT;
3218 }
3219
Michal Vasko46a4bf92016-09-08 08:23:49 +02003220 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02003221 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003222 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003223 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko46a4bf92016-09-08 08:23:49 +02003224 return NULL;
3225 }
3226
3227 ret_set = ly_set_new();
3228
3229 for (i = 0; i < set.used; ++i) {
3230 if (!set.val.snodes[i].in_ctx) {
3231 continue;
3232 }
3233 assert(set.val.snodes[i].in_ctx == 1);
3234
3235 switch (set.val.snodes[i].type) {
3236 case LYXP_NODE_ELEM:
3237 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3238 ly_set_free(ret_set);
3239 free(set.val.snodes);
3240 return NULL;
3241 }
3242 break;
3243 default:
3244 /* ignore roots, text and attr should not ever appear */
3245 break;
3246 }
3247 }
3248
3249 free(set.val.snodes);
3250 return ret_set;
3251}
3252
3253API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003254lys_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 +02003255{
Michal Vasko508a50d2016-09-07 14:50:33 +02003256 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003257 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003258 uint32_t i;
3259
Michal Vaskob94a5e42016-09-08 14:01:56 +02003260 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003261 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003262 }
3263
Michal Vaskob94a5e42016-09-08 14:01:56 +02003264 /* adjust the root */
3265 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3266 do {
3267 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3268 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3269 }
3270
Michal Vasko508a50d2016-09-07 14:50:33 +02003271 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003272
3273 if (options & LYXP_MUST) {
3274 options &= ~LYXP_MUST;
3275 options |= LYXP_SNODE_MUST;
3276 } else if (options & LYXP_WHEN) {
3277 options &= ~LYXP_WHEN;
3278 options |= LYXP_SNODE_WHEN;
3279 } else {
3280 options |= LYXP_SNODE;
3281 }
3282
Michal Vasko508a50d2016-09-07 14:50:33 +02003283 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3284 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003285 LOGVAL(LYE_SPEC, LY_VLOG_LYS, cur_snode, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003286 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003287 }
3288
Michal Vasko8548e082016-07-22 12:00:18 +02003289 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003290
Michal Vasko508a50d2016-09-07 14:50:33 +02003291 for (i = 0; i < set.used; ++i) {
3292 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003293 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003294 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003295 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003296 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003297 return NULL;
3298 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003299 break;
3300 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003301 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003302 break;
3303 }
3304 }
3305
Michal Vasko508a50d2016-09-07 14:50:33 +02003306 free(set.val.snodes);
3307 return ret_set;
3308}
3309
3310API struct ly_set *
3311lys_node_xpath_atomize(const struct lys_node *node, int options)
3312{
3313 const struct lys_node *next, *elem, *parent, *tmp;
3314 struct lyxp_set set;
3315 struct ly_set *ret_set;
3316 uint16_t i;
3317
3318 if (!node) {
3319 return NULL;
3320 }
3321
3322 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3323 if (!parent) {
3324 /* not in input, output, or notification */
3325 return NULL;
3326 }
3327
3328 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003329 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003330 return NULL;
3331 }
3332
3333 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe9914d12016-10-07 14:32:37 +02003334 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_VALID_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003335 /* elem has no dependencies from other subtrees and local nodes get discarded */
3336 goto next_iter;
3337 }
3338
3339 if (lyxp_node_atomize(elem, &set)) {
3340 ly_set_free(ret_set);
3341 free(set.val.snodes);
3342 return NULL;
3343 }
3344
3345 for (i = 0; i < set.used; ++i) {
3346 switch (set.val.snodes[i].type) {
3347 case LYXP_NODE_ELEM:
3348 if (options & LYXP_NO_LOCAL) {
3349 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3350 if (tmp) {
3351 /* in local subtree, discard */
3352 break;
3353 }
3354 }
3355 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3356 ly_set_free(ret_set);
3357 free(set.val.snodes);
3358 return NULL;
3359 }
3360 break;
3361 default:
3362 /* ignore roots, text and attr should not ever appear */
3363 break;
3364 }
3365 }
3366
3367 free(set.val.snodes);
3368 if (!(options & LYXP_RECURSIVE)) {
3369 break;
3370 }
3371next_iter:
3372 LY_TREE_DFS_END(node, next, elem);
3373 }
3374
Michal Vasko8548e082016-07-22 12:00:18 +02003375 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003376}
3377
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003378static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003379lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003380{
3381 int ret;
3382 char *parent_path;
3383 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003384
Pavol Vican64d0b762016-08-25 10:44:59 +02003385 if (!dev->deviate) {
3386 return ;
3387 }
3388
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003389 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3390 if (dev->orig_node) {
3391 /* removing not-supported deviation ... */
3392 if (strrchr(dev->target_name, '/') != dev->target_name) {
3393 /* ... from a parent */
3394 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3395
3396 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003397 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, 1,
3398 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003399 free(parent_path);
3400 if (ret || !target) {
3401 LOGINT;
3402 return;
3403 }
3404
3405 lys_node_addchild(target, NULL, dev->orig_node);
3406 } else {
3407 /* ... from top-level data */
3408 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3409 }
3410
3411 dev->orig_node = NULL;
3412 } else {
3413 /* adding not-supported deviation */
3414 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003415 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1,
3416 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003417 if (ret || !target) {
3418 LOGINT;
3419 return;
3420 }
3421
3422 lys_node_unlink(target);
3423 dev->orig_node = target;
3424 }
3425 } else {
3426 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003427 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1,
3428 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003429 if (ret || !target) {
3430 LOGINT;
3431 return;
3432 }
3433
3434 lys_node_switch(target, dev->orig_node);
3435 dev->orig_node = target;
3436 }
3437}
3438
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003439/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3440void
3441lys_switch_deviations(struct lys_module *module)
3442{
Michal Vasko89563fc2016-07-28 16:19:35 +02003443 uint32_t i = 0, j;
3444 const struct lys_module *mod;
3445 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003446
Michal Vasko89563fc2016-07-28 16:19:35 +02003447 if (module->deviated) {
3448 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3449 if (mod == module) {
3450 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003451 }
3452
Michal Vasko89563fc2016-07-28 16:19:35 +02003453 for (j = 0; j < mod->deviation_size; ++j) {
3454 ptr = strstr(mod->deviation[j].target_name, module->name);
3455 if (ptr && ptr[strlen(module->name)] == ':') {
3456 lys_switch_deviation(&mod->deviation[j], module);
3457 }
3458 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003459 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003460
Michal Vasko89563fc2016-07-28 16:19:35 +02003461 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003462 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003463 } else {
3464 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003465 }
3466 }
3467}
3468
3469/* not needed currently, but tested and working */
3470#if 0
3471
3472void
3473lys_sub_module_apply_devs_augs(struct lys_module *module)
3474{
3475 int i;
3476 struct lys_node_augment *aug;
3477 struct lys_node *last;
3478
3479 /* re-apply deviations */
3480 for (i = 0; i < module->deviation_size; ++i) {
3481 lys_switch_deviation(&module->deviation[i], module);
3482 assert(module->deviation[i].orig_node);
Michal Vasko89563fc2016-07-28 16:19:35 +02003483 lys_node_module(module->deviation[i].orig_node)->deviated = 1;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003484 }
3485
3486 /* re-apply augments */
3487 for (i = 0; i < module->augment_size; ++i) {
3488 aug = &module->augment[i];
3489 assert(aug->target);
3490
3491 /* reconnect augmenting data into the target - add them to the target child list */
3492 if (aug->target->child) {
3493 last = aug->target->child->prev;
3494 last->next = aug->child;
3495 aug->target->child->prev = aug->child->prev;
3496 aug->child->prev = last;
3497 } else {
3498 aug->target->child = aug->child;
3499 }
3500 }
3501}
3502
3503#endif
3504
3505void
3506lys_sub_module_remove_devs_augs(struct lys_module *module)
3507{
Michal Vasko89563fc2016-07-28 16:19:35 +02003508 uint32_t i = 0, j;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003509 struct lys_node *last, *elem;
Michal Vasko89563fc2016-07-28 16:19:35 +02003510 const struct lys_module *mod;
3511 struct lys_module *target_mod;
3512 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003513
3514 /* remove applied deviations */
3515 for (i = 0; i < module->deviation_size; ++i) {
Pavol Vican64d0b762016-08-25 10:44:59 +02003516 if (module->deviation[i].orig_node) {
3517 target_mod = lys_node_module(module->deviation[i].orig_node);
3518 } else {
3519 target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1,
3520 strcspn(module->deviation[i].target_name, ":") - 1);
Radek Krejci0fa54e92016-09-14 14:01:05 +02003521 target_mod = (struct lys_module *)lys_get_implemented_module(target_mod);
Pavol Vican64d0b762016-08-25 10:44:59 +02003522 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003523 lys_switch_deviation(&module->deviation[i], module);
3524
Michal Vasko89563fc2016-07-28 16:19:35 +02003525 /* clear the deviation flag if possible */
3526 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3527 if ((mod == module) || (mod == target_mod)) {
3528 continue;
3529 }
3530
3531 for (j = 0; j < mod->deviation_size; ++j) {
3532 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
3533 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
3534 /* some other module deviation targets the inspected module, flag remains */
3535 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003536 }
3537 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003538
3539 if (j < mod->deviation_size) {
3540 break;
3541 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003542 }
Michal Vasko89563fc2016-07-28 16:19:35 +02003543
3544 if (!mod) {
3545 target_mod->deviated = 0;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003546 }
3547 }
3548
3549 /* remove applied augments */
3550 for (i = 0; i < module->augment_size; ++i) {
Radek Krejcidbc15262016-06-16 14:58:29 +02003551 if (!module->augment[i].target) {
3552 /* skip not resolved augments */
3553 continue;
3554 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003555
3556 elem = module->augment[i].child;
3557 if (elem) {
3558 LY_TREE_FOR(elem, last) {
3559 if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) {
3560 break;
3561 }
3562 }
3563 /* elem is first augment child, last is the last child */
3564
3565 /* parent child ptr */
3566 if (module->augment[i].target->child == elem) {
3567 module->augment[i].target->child = last->next;
3568 }
3569
3570 /* parent child next ptr */
3571 if (elem->prev->next) {
3572 elem->prev->next = last->next;
3573 }
3574
3575 /* parent child prev ptr */
3576 if (last->next) {
3577 last->next->prev = elem->prev;
3578 } else if (module->augment[i].target->child) {
3579 module->augment[i].target->child->prev = elem->prev;
3580 }
3581
3582 /* update augment children themselves */
3583 elem->prev = last;
3584 last->next = NULL;
3585 }
Michal Vaskob8f71322016-05-03 11:39:56 +02003586
3587 /* needs to be NULL for lys_augment_free() to free the children */
3588 module->augment[i].target = NULL;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003589 }
3590}
3591
Radek Krejci27fe55e2016-09-13 17:13:35 +02003592static int
3593lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
3594{
3595 struct lys_node *root, *next, *node;
3596 uint8_t i;
3597
3598 for (i = 0; i < module->augment_size; i++) {
3599 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003600 if (!module->augment[i].target
3601 && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003602 return -1;
3603 }
3604 }
3605 LY_TREE_FOR(module->data, root) {
3606 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
3607 LY_TREE_DFS_BEGIN(root, next, node) {
3608 if (node->nodetype == LYS_GROUPING) {
3609 goto nextsibling;
3610 }
3611 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3612 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
3613 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
3614 UNRES_TYPE_LEAFREF, node) == -1) {
3615 return EXIT_FAILURE;
3616 }
3617 }
3618 }
3619
3620 /* modified LY_TREE_DFS_END */
3621 next = node->child;
3622 /* child exception for leafs, leaflists and anyxml without children */
3623 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
3624 next = NULL;
3625 }
3626 if (!next) {
3627nextsibling:
3628 /* no children */
3629 if (node == root) {
3630 /* we are done, root has no children */
3631 break;
3632 }
3633 /* try siblings */
3634 next = node->next;
3635 }
3636 while (!next) {
3637 /* parent is already processed, go to its sibling */
3638 node = lys_parent(node);
3639 /* no siblings, go back through parents */
3640 if (lys_parent(node) == lys_parent(root)) {
3641 /* we are done, no next element to process */
3642 break;
3643 }
3644 next = node->next;
3645 }
3646 }
3647 }
3648
3649 return EXIT_SUCCESS;
3650}
3651
3652API int
3653lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02003654{
3655 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003656 struct unres_schema *unres;
3657 int i, j;
Michal Vasko26055752016-05-03 11:36:31 +02003658
Radek Krejci27fe55e2016-09-13 17:13:35 +02003659 if (!module) {
3660 ly_errno = LY_EINVAL;
3661 return EXIT_FAILURE;
3662 }
3663
3664 module = lys_main_module(module);
Michal Vasko26055752016-05-03 11:36:31 +02003665 if (module->implemented) {
3666 return EXIT_SUCCESS;
3667 }
3668
3669 ctx = module->ctx;
3670
3671 for (i = 0; i < ctx->models.used; ++i) {
3672 if (module == ctx->models.list[i]) {
3673 continue;
3674 }
3675
3676 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
3677 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
3678 return EXIT_FAILURE;
3679 }
3680 }
3681
Radek Krejci27fe55e2016-09-13 17:13:35 +02003682 unres = calloc(1, sizeof *unres);
3683 if (!unres) {
3684 LOGMEM;
3685 return EXIT_FAILURE;
3686 }
3687 /* recursively make the module implemented */
3688 ((struct lys_module *)module)->implemented = 1;
3689 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
3690 goto error;
3691 }
3692 /* process augments in submodules */
3693 for (i = 0; i < module->inc_size; ++i) {
3694 if (!module->inc[i].submodule) {
3695 continue;
3696 }
3697 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
3698 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02003699 if (!module->inc[i].submodule->augment[i].target
3700 && (unres_schema_add_node((struct lys_module *)module->inc[i].submodule, unres,
3701 &module->inc[i].submodule->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02003702 goto error;
3703 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003704 }
3705 }
Radek Krejcidf46e222016-11-08 11:57:37 +01003706 /* try again resolve augments in other modules possibly augmenting this one,
3707 * since we have just enabled it
3708 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02003709 /* resolve rest of unres items */
3710 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
3711 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003712 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02003713 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02003714
3715 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02003716
3717error:
3718 ((struct lys_module *)module)->implemented = 0;
3719 unres_schema_free((struct lys_module *)module, &unres);
3720 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003721}
3722
3723void
3724lys_submodule_module_data_free(struct lys_submodule *submodule)
3725{
3726 struct lys_node *next, *elem;
3727
3728 /* remove parsed data */
3729 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
3730 if (elem->module == (struct lys_module *)submodule) {
3731 lys_node_free(elem, NULL, 0);
3732 }
3733 }
3734}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02003735
3736int
3737lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
3738{
3739 uint8_t i;
3740
3741 for (i = 0; i < list->keys_size; i++) {
3742 if (list->keys[i] == leaf) {
3743 return i + 1;
3744 }
3745 }
3746
3747 return 0;
3748}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02003749
3750API char *
3751lys_path(const struct lys_node *node)
3752{
3753 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
3754 uint16_t index = LY_BUF_SIZE - 1;
3755
3756 if (!node) {
3757 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
3758 return NULL;
3759 }
3760
3761 /* backup the shared internal buffer */
3762 if (ly_buf_used && buf[0]) {
3763 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
3764 }
3765 ly_buf_used++;
3766
3767 /* build the path */
3768 buf[index] = '\0';
3769 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index);
3770 result = strdup(&buf[index]);
3771
3772 /* restore the shared internal buffer */
3773 if (buf_backup) {
3774 strcpy(buf, buf_backup);
3775 free(buf_backup);
3776 }
3777 ly_buf_used--;
3778
3779 return result;
3780}
Radek Krejci9ad23f42016-10-31 15:46:52 +01003781