blob: 36d3e5a64c17aaf98a890702e318e9552b6d5015 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
Michal Vasko2d162e12015-09-24 14:33:29 +02002 * @file tree_schema.c
Radek Krejcida04f4a2015-05-21 12:54:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko2d162e12015-09-24 14:33:29 +02004 * @brief Manipulation with libyang schema data structures
Radek Krejcida04f4a2015-05-21 12:54:09 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
Radek Krejci54f6fb32016-02-24 12:56:39 +010014
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020015#define _GNU_SOURCE
Radek Krejcida04f4a2015-05-21 12:54:09 +020016
Radek Krejci812b10a2015-05-28 16:48:25 +020017#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020018#include <ctype.h>
Radek Krejcib051f722016-02-25 15:12:21 +010019#include <limits.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020020#include <stdlib.h>
21#include <sys/mman.h>
Michal Vasko662610a2015-12-07 11:25:45 +010022#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <sys/stat.h>
Michal Vasko662610a2015-12-07 11:25:45 +010024#include <fcntl.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020025#include <string.h>
Michal Vasko662610a2015-12-07 11:25:45 +010026#include <unistd.h>
27#include <errno.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020028
29#include "common.h"
30#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020031#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020032#include "resolve.h"
Michal Vasko88c29542015-11-27 14:57:53 +010033#include "xml.h"
Michal Vasko5b3492c2016-07-20 09:37:40 +020034#include "xpath.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020035#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020036#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020037#include "validation.h"
Pavol Vicanf7cc2852016-03-22 23:27:35 +010038#include "parser_yang.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020039
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020040static int
41lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +020042 int tpdftype, struct unres_schema *unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020043
Radek Krejci9ff0a922016-07-14 13:08:05 +020044API const struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +010045lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020046{
Radek Krejci9ff0a922016-07-14 13:08:05 +020047 int i;
Radek Krejci48061fb2015-08-05 15:41:07 +020048
Radek Krejci27fe55e2016-09-13 17:13:35 +020049 if (!node) {
50 return NULL;
51 }
52
Radek Krejci48061fb2015-08-05 15:41:07 +020053check:
54 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
55 /* input/output does not have if-feature, so skip them */
56
57 /* check local if-features */
Michal Vaskoc5c26b02016-06-29 11:10:29 +020058 for (i = 0; i < node->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +020059 if (!resolve_iffeature(&node->iffeature[i])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +020060 return node;
Radek Krejci48061fb2015-08-05 15:41:07 +020061 }
62 }
63 }
64
65 if (!recursive) {
66 return NULL;
67 }
68
69 /* go through parents */
70 if (node->nodetype == LYS_AUGMENT) {
71 /* go to parent actually means go to the target node */
72 node = ((struct lys_node_augment *)node)->target;
Radek Krejci48061fb2015-08-05 15:41:07 +020073 } else if (node->parent) {
74 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +020075 } else {
76 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +020077 }
78
Radek Krejci074bf852015-08-19 14:22:16 +020079 if (recursive == 2) {
80 /* continue only if the node cannot have a data instance */
81 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
82 return NULL;
83 }
84 }
85 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +020086}
87
Michal Vasko1dca6882015-10-22 14:29:42 +020088int
Michal Vasko36cbaa42015-12-14 13:15:48 +010089lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
90 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +020091{
Radek Krejcic071c542016-01-27 14:57:51 +010092 const struct lys_node *node, *parent = NULL;
93 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +010094 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +020095
Michal Vasko36cbaa42015-12-14 13:15:48 +010096 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +020097 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +020098
Michal Vasko36cbaa42015-12-14 13:15:48 +010099 /* fill the lengths in case the caller is so indifferent */
100 if (!mod_name_len) {
101 mod_name_len = strlen(mod_name);
102 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200103 if (!nam_len) {
104 nam_len = strlen(name);
105 }
106
Michal Vasko9e635ac2016-10-17 11:44:09 +0200107 while (siblings && (siblings->nodetype == LYS_USES)) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200108 siblings = siblings->child;
109 }
Michal Vasko9e635ac2016-10-17 11:44:09 +0200110 if (!siblings) {
111 /* unresolved uses */
112 return EXIT_FAILURE;
113 }
114
Michal Vasko680f8b42016-10-17 10:27:37 +0200115 if (siblings->nodetype == LYS_GROUPING) {
116 for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev);
117 if (node->nodetype == LYS_GROUPING) {
118 /* we went through all the siblings, only groupings there - no valid sibling */
119 return EXIT_FAILURE;
120 }
121 /* update siblings to be valid */
122 siblings = node;
123 }
124
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200125 /* set parent correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100126 parent = lys_parent(siblings);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200127
Michal Vasko680f8b42016-10-17 10:27:37 +0200128 /* go up all uses */
129 while (parent && (parent->nodetype == LYS_USES)) {
130 parent = lys_parent(parent);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200131 }
132
Radek Krejcic071c542016-01-27 14:57:51 +0100133 if (!parent) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200134 /* handle situation when there is a top-level uses referencing a foreign grouping */
135 for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node));
136 mod = lys_node_module(node);
Michal Vasko1dca6882015-10-22 14:29:42 +0200137 }
138
Radek Krejcic071c542016-01-27 14:57:51 +0100139 /* try to find the node */
140 node = NULL;
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100141 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Radek Krejcic071c542016-01-27 14:57:51 +0100142 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100143 /* module name comparison */
144 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200145 if (!ly_strequal(node_mod_name, mod_name, 1) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100146 continue;
147 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200148
Radek Krejcic071c542016-01-27 14:57:51 +0100149 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200150 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100151 if (ret) {
152 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200153 }
Radek Krejcic071c542016-01-27 14:57:51 +0100154 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200155 }
156 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200157 }
158
159 return EXIT_FAILURE;
160}
161
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200162int
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100163lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, int nam_len,
164 LYS_NODE type, const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200165{
Michal Vasko1e62a092015-12-01 12:27:20 +0100166 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200167
168 assert(siblings && name);
169 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
170
171 /* find the beginning */
172 while (siblings->prev->next) {
173 siblings = siblings->prev;
174 }
175
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200176 if (!mod) {
177 mod = siblings->module;
178 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200179
Michal Vasko4f0dad02016-02-15 14:08:23 +0100180 /* try to find the node */
181 node = NULL;
Michal Vaskodcf98e62016-05-05 17:53:53 +0200182 while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100183 if (!type || (node->nodetype & type)) {
184 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200185 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100186 continue;
187 }
188
Michal Vasko4f0dad02016-02-15 14:08:23 +0100189 /* direct name check */
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100190 if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100191 if (ret) {
192 *ret = node;
193 }
194 return EXIT_SUCCESS;
195 }
Radek Krejcic071c542016-01-27 14:57:51 +0100196 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200197 }
198
199 return EXIT_FAILURE;
200}
201
Michal Vasko1e62a092015-12-01 12:27:20 +0100202API const struct lys_node *
203lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200204{
Michal Vasko1e62a092015-12-01 12:27:20 +0100205 const struct lys_node *next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200206
Radek Krejci8bc87f62015-09-02 16:19:05 +0200207 if (!last) {
208 /* first call */
209
210 /* get know where to start */
211 if (parent) {
212 /* schema subtree */
213 next = last = parent->child;
214 } else {
215 /* top level data */
216 assert(module);
217 next = last = module->data;
218 }
Radek Krejci972724f2016-08-12 15:24:40 +0200219 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
220 /* continue with uses content */
221 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200222 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200223 /* continue after the last returned value */
224 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200225 }
226
227repeat:
Michal Vasko7c386e72015-10-07 15:13:33 +0200228 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200229 if (options & LYS_GETNEXT_WITHGROUPING) {
230 return next;
231 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200232 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200233 }
234
Radek Krejci972724f2016-08-12 15:24:40 +0200235 if (!next) { /* cover case when parent is augment */
236 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200237 /* no next element */
238 return NULL;
239 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200240 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200241 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200242 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200243 } else {
244 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200245 }
246
247 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200248 case LYS_INPUT:
249 case LYS_OUTPUT:
250 if (options & LYS_GETNEXT_WITHINOUT) {
251 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200252 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200253 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200254 } else {
255 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200256 }
Radek Krejci972724f2016-08-12 15:24:40 +0200257 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200258
Michal Vaskoa5835e92015-10-20 15:07:39 +0200259 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200260 if (options & LYS_GETNEXT_WITHCASE) {
261 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200262 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200263 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200264 } else {
265 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200266 }
Radek Krejci972724f2016-08-12 15:24:40 +0200267 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200268
Michal Vasko1dca6882015-10-22 14:29:42 +0200269 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200270 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200271 if (options & LYS_GETNEXT_WITHUSES) {
272 return next;
273 } else if (next->child) {
274 next = next->child;
275 } else {
276 next = next->next;
277 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200278 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200279
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200280 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200281 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200282 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200283 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200284 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200285 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200286 case LYS_LIST:
287 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200288 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200289
Radek Krejci972724f2016-08-12 15:24:40 +0200290 case LYS_CONTAINER:
291 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
292 if (next->child) {
293 /* go into */
294 next = next->child;
295 } else {
296 next = next->next;
297 }
298 goto repeat;
299 } else {
300 return next;
301 }
302
Radek Krejci8bc87f62015-09-02 16:19:05 +0200303 case LYS_CHOICE:
304 if (options & LYS_GETNEXT_WITHCHOICE) {
305 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200306 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200307 /* go into */
308 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200309 } else {
310 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200311 }
Radek Krejci972724f2016-08-12 15:24:40 +0200312 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200313
Radek Krejci7f40ce32015-08-12 20:38:46 +0200314 default:
315 /* we should not be here */
316 return NULL;
317 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200318
319
320}
321
Radek Krejcie534c132016-11-23 13:32:31 +0100322static void
323lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size)
324{
325 unsigned int i;
326
327 if (!size || !e || !(*e)) {
328 return;
329 }
330
331 for (i = 0; i < size; i++) {
332 if (!e[i]) {
333 continue;
334 }
335
Radek Krejci80056d52017-01-05 13:13:33 +0100336 if (e[i]->flags & LYEXT_OPT_INHERIT) {
337 /* no free, this is just a shadow copy of the original extension instance */
338 } else {
339 lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size);
340 lydict_remove(ctx, e[i]->arg_value);
Radek Krejcie534c132016-11-23 13:32:31 +0100341 }
342 free(e[i]);
343 }
344 free(e);
345}
346
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200347void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200348lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200349{
Radek Krejci76512572015-08-04 09:47:08 +0200350 struct lys_node *parent, *first;
Radek Krejcic071c542016-01-27 14:57:51 +0100351 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200352
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200353 if (!node) {
354 return;
355 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200356
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200357 /* unlink from data model if necessary */
358 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100359 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100360 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100361 if (main_module->data == node) {
362 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200363 }
364 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200365
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200366 /* store pointers to important nodes */
367 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200368 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200369 /* handle augments - first, unlink it from the augment parent ... */
370 if (parent->child == node) {
371 parent->child = node->next;
372 }
Radek Krejcifec2e142017-01-05 15:19:03 +0100373
374 if (parent->flags & LYS_NOTAPPLIED) {
375 /* data are not connected in the target, so we cannot continue with the target as a parent */
376 parent = NULL;
377 } else {
378 /* data are connected in target, so we will continue with the target as a parent */
379 parent = ((struct lys_node_augment *)parent)->target;
380 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200381 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200382
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200383 /* unlink from parent */
384 if (parent) {
385 if (parent->child == node) {
386 parent->child = node->next;
387 }
388 node->parent = NULL;
389 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200390
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200391 /* unlink from siblings */
392 if (node->prev == node) {
393 /* there are no more siblings */
394 return;
395 }
396 if (node->next) {
397 node->next->prev = node->prev;
398 } else {
399 /* unlinking the last element */
400 if (parent) {
401 first = parent->child;
402 } else {
403 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200404 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200405 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200406 }
407 }
408 first->prev = node->prev;
409 }
410 if (node->prev->next) {
411 node->prev->next = node->next;
412 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200413
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200414 /* clean up the unlinked element */
415 node->next = NULL;
416 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200417}
418
Michal Vasko563ef092015-09-04 13:17:23 +0200419struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100420lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200421{
422 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200423
424 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200425 /* top-level augment, look into module (uses augment is handled correctly below) */
426 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
427 par_iter = par_iter->parent->module->data;
428 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200429 break;
430 }
431 }
432
Michal Vasko6f929da2015-10-02 16:23:25 +0200433 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200434 continue;
435 }
436
437 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
438 if (!stop) {
439 stop = par_iter;
440 } else if (iter == stop) {
441 break;
442 }
443 if (iter->nodetype != LYS_GROUPING) {
444 continue;
445 }
446
Radek Krejcif8426a72015-10-31 23:14:03 +0100447 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200448 return (struct lys_node_grp *)iter;
449 }
450 }
451 }
452
Michal Vasko563ef092015-09-04 13:17:23 +0200453 return NULL;
454}
455
Radek Krejci10c760e2015-08-14 14:45:43 +0200456/*
457 * get next grouping in the root's subtree, in the
458 * first call, tha last is NULL
459 */
460static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200461lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200462{
Radek Krejci10c760e2015-08-14 14:45:43 +0200463 struct lys_node *last = (struct lys_node *)lastgrp;
464 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200465
Radek Krejci10c760e2015-08-14 14:45:43 +0200466 assert(root);
467
468 if (!last) {
469 last = root;
470 }
471
472 while (1) {
473 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
474 next = last->child;
475 } else {
476 next = NULL;
477 }
478 if (!next) {
479 if (last == root) {
480 /* we are done */
481 return NULL;
482 }
483
484 /* no children, go to siblings */
485 next = last->next;
486 }
487 while (!next) {
488 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100489 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200490 /* we are done */
491 return NULL;
492 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200493 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100494 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200495 }
496
497 if (next->nodetype == LYS_GROUPING) {
498 return (struct lys_node_grp *)next;
499 }
500
501 last = next;
502 }
503}
504
Michal Vasko0d343d12015-08-24 14:57:36 +0200505/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200506int
Radek Krejci07911992015-08-14 15:13:31 +0200507lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
508{
Michal Vasko563ef092015-09-04 13:17:23 +0200509 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200510 struct lys_node_grp *grp;
511 int down;
512
513 assert(node);
514
515 if (!parent) {
516 assert(module);
517 } else {
518 module = parent->module;
519 }
520
521 switch (node->nodetype) {
522 case LYS_GROUPING:
523 /* 6.2.1, rule 6 */
524 if (parent) {
525 if (parent->child) {
526 down = 1;
527 start = parent->child;
528 } else {
529 down = 0;
530 start = parent;
531 }
532 } else {
533 down = 1;
534 start = module->data;
535 }
536 /* go up */
Radek Krejcic071c542016-01-27 14:57:51 +0100537 if (lys_find_grouping_up(node->name, start)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100538 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200539 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200540 }
541 /* go down, because grouping can be defined after e.g. container in which is collision */
542 if (down) {
543 for (iter = start, stop = NULL; iter; iter = iter->prev) {
544 if (!stop) {
545 stop = start;
546 } else if (iter == stop) {
547 break;
548 }
549 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
550 continue;
551 }
552
553 grp = NULL;
554 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100555 if (ly_strequal(node->name, grp->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100556 LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200557 return EXIT_FAILURE;
558 }
559 }
560 }
561 }
562 break;
563 case LYS_LEAF:
564 case LYS_LEAFLIST:
565 case LYS_LIST:
566 case LYS_CONTAINER:
567 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200568 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200569 /* 6.2.1, rule 7 */
570 if (parent) {
571 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200572 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
573 if (iter->nodetype == LYS_AUGMENT) {
574 if (((struct lys_node_augment *)iter)->target) {
575 /* augment is resolved, go up */
576 iter = ((struct lys_node_augment *)iter)->target;
577 continue;
578 }
579 /* augment is not resolved, this is the final parent */
580 break;
581 }
Radek Krejci07911992015-08-14 15:13:31 +0200582 iter = iter->parent;
583 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200584
Radek Krejci07911992015-08-14 15:13:31 +0200585 if (!iter) {
586 stop = NULL;
587 iter = module->data;
588 } else {
589 stop = iter;
590 iter = iter->child;
591 }
592 } else {
593 stop = NULL;
594 iter = module->data;
595 }
596 while (iter) {
597 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
598 iter = iter->child;
599 continue;
600 }
601
Radek Krejcibf2abff2016-08-23 15:51:52 +0200602 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100603 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100604 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200605 return EXIT_FAILURE;
606 }
607 }
608
609 /* special case for choice - we must check the choice's name as
610 * well as the names of nodes under the choice
611 */
612 if (iter->nodetype == LYS_CHOICE) {
613 iter = iter->child;
614 continue;
615 }
616
617 /* go to siblings */
618 if (!iter->next) {
619 /* no sibling, go to parent's sibling */
620 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200621 /* for parent LYS_AUGMENT */
622 if (iter->parent == stop) {
623 iter = stop;
624 break;
625 }
626 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200627 if (iter && iter->next) {
628 break;
629 }
630 } while (iter != stop);
631
632 if (iter == stop) {
633 break;
634 }
635 }
636 iter = iter->next;
637 }
638 break;
639 case LYS_CASE:
640 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100641 if (parent) {
642 start = parent->child;
643 } else {
644 start = module->data;
645 }
646
647 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200648 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200649 continue;
650 }
651
Radek Krejci749190d2016-02-18 16:26:25 +0100652 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100653 LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200654 return EXIT_FAILURE;
655 }
656 }
657 break;
658 default:
659 /* no check needed */
660 break;
661 }
662
663 return EXIT_SUCCESS;
664}
665
Michal Vasko0d343d12015-08-24 14:57:36 +0200666/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200667int
Radek Krejci10c760e2015-08-14 14:45:43 +0200668lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
669{
Radek Krejci92720552015-10-05 15:28:27 +0200670 struct lys_node *iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200671 struct lys_node_inout *in, *out, *inout;
Radek Krejci07911992015-08-14 15:13:31 +0200672 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200673
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200674 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200675
Radek Krejci10c760e2015-08-14 14:45:43 +0200676 if (parent) {
677 type = parent->nodetype;
678 module = parent->module;
679 } else {
680 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100681 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200682 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200683 }
684
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200685 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200686 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200687 case LYS_CONTAINER:
688 case LYS_LIST:
689 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100690 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200691 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200692 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200693 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200694 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
695 return EXIT_FAILURE;
696 }
697 break;
Radek Krejci76512572015-08-04 09:47:08 +0200698 case LYS_INPUT:
699 case LYS_OUTPUT:
700 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200701 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200702 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200703 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100704 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200705 return EXIT_FAILURE;
706 }
707 break;
Radek Krejci76512572015-08-04 09:47:08 +0200708 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200709 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200710 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100711 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200712 return EXIT_FAILURE;
713 }
714 break;
Radek Krejci76512572015-08-04 09:47:08 +0200715 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200716 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200717 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100718 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200719 return EXIT_FAILURE;
720 }
721 break;
Radek Krejci76512572015-08-04 09:47:08 +0200722 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200723 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200724 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100725 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200726 return EXIT_FAILURE;
727 }
728 break;
Radek Krejci76512572015-08-04 09:47:08 +0200729 case LYS_LEAF:
730 case LYS_LEAFLIST:
731 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200732 case LYS_ANYDATA:
Radek Krejci48464ed2016-03-17 15:44:09 +0100733 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
734 LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100735 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200736 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200737 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200738 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200739 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200740 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100741 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200742 return EXIT_FAILURE;
743 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200744 break;
745 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200746 /* top level */
747 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200748 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200749 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100750 LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200751 return EXIT_FAILURE;
752 }
753
Radek Krejcic071c542016-01-27 14:57:51 +0100754 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200755 }
756
757 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +0200758 if (lys_check_id(child, parent, module)) {
759 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200760 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200761
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200762 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200763 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200764 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200765
Radek Krejci41a349b2016-10-24 19:21:59 +0200766 if (child->nodetype & (LYS_INPUT | LYS_OUTPUT)) {
767 /* replace the implicit input/output node */
768 if (child->nodetype == LYS_OUTPUT) {
769 inout = (struct lys_node_inout *)parent->child->next;
770 } else { /* LYS_INPUT */
771 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200772 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200773 }
774 if (inout->next) {
775 child->next = inout->next;
776 inout->next->prev = child;
777 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200778 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200779 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200780 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200781 child->prev = inout->prev;
782 if (inout->prev->next) {
783 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200784 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200785 inout->prev = (struct lys_node *)inout;
786 child->parent = parent;
787 inout->parent = NULL;
788 lys_node_free((struct lys_node *)inout, NULL, 0);
789 } else {
790 /* connect the child correctly */
791 if (!parent) {
792 if (module->data) {
793 module->data->prev->next = child;
794 child->prev = module->data->prev;
795 module->data->prev = child;
796 } else {
797 module->data = child;
798 }
799 } else {
800 if (!parent->child) {
801 /* the only/first child of the parent */
802 parent->child = child;
803 child->parent = parent;
804 iter = child;
805 } else {
806 /* add a new child at the end of parent's child list */
807 iter = parent->child->prev;
808 iter->next = child;
809 child->prev = iter;
810 }
811 while (iter->next) {
812 iter = iter->next;
813 iter->parent = parent;
814 }
815 parent->child->prev = iter;
816 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200817 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200818
Michal Vaskoe022a562016-09-27 14:24:15 +0200819 /* check config value (but ignore them in groupings and augments) */
820 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent);
821 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200822 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
823 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
824 LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config");
825 LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children.");
826 return EXIT_FAILURE;
827 }
828 }
829
Radek Krejci41771502016-04-14 17:52:32 +0200830 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200831 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200832 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200833 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200834 /* store it only into container or list - the only data inner nodes */
835 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
836 if (iter->flags & LYS_INCL_STATUS) {
837 /* done, someone else set it already from here */
838 break;
839 }
840 /* set flag about including status data */
841 iter->flags |= LYS_INCL_STATUS;
842 }
843 }
844 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200845
846 /* create implicit input/output nodes to have available them as possible target for augment */
847 if (child->nodetype & (LYS_RPC | LYS_ACTION)) {
848 in = calloc(1, sizeof *in);
849 in->nodetype = LYS_INPUT;
850 in->name = lydict_insert(child->module->ctx, "input", 5);
851 out = calloc(1, sizeof *out);
852 out->name = lydict_insert(child->module->ctx, "output", 6);
853 out->nodetype = LYS_OUTPUT;
854 in->module = out->module = child->module;
855 in->parent = out->parent = child;
856 in->flags = out->flags = LYS_IMPLICIT;
857 in->next = (struct lys_node *)out;
858 in->prev = (struct lys_node *)out;
859 out->prev = (struct lys_node *)in;
860 child->child = (struct lys_node *)in;
861 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200862 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200863}
864
Radek Krejcia1df1682016-04-11 14:56:59 +0200865static const struct lys_module *
866lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200867{
Radek Krejcia1df1682016-04-11 14:56:59 +0200868 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200869 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200870 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200871
Radek Krejci00a0e712016-10-26 10:24:46 +0200872 ly_err_clean(1);
Radek Krejcif347abc2016-06-22 10:18:47 +0200873
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200874 if (!ctx || !data) {
875 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
876 return NULL;
877 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200878
Radek Krejcia1df1682016-04-11 14:56:59 +0200879 if (!internal && format == LYS_IN_YANG) {
880 /* enlarge data by 2 bytes for flex */
881 len = strlen(data);
882 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
883 if (!enlarged_data) {
884 LOGMEM;
885 return NULL;
886 }
887 memcpy(enlarged_data, data, len);
888 enlarged_data[len] = enlarged_data[len + 1] = '\0';
889 data = enlarged_data;
890 }
891
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200892 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200893 case LYS_IN_YIN:
Radek Krejciff4874d2016-03-07 12:30:50 +0100894 mod = yin_read_module(ctx, data, NULL, 1);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200895 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200896 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100897 mod = yang_read_module(ctx, data, 0, NULL, 1);
898 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200899 default:
Radek Krejcia1df1682016-04-11 14:56:59 +0200900 LOGERR(LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200901 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200902 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200903
Radek Krejcia1df1682016-04-11 14:56:59 +0200904 free(enlarged_data);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200905 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200906}
907
Radek Krejcia1df1682016-04-11 14:56:59 +0200908API const struct lys_module *
909lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
910{
911 return lys_parse_mem_(ctx, data, format, 0);
912}
913
Michal Vasko5a721fd2016-02-16 12:16:48 +0100914struct lys_submodule *
915lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +0200916{
Michal Vasko5a721fd2016-02-16 12:16:48 +0100917 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200918
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200919 assert(module);
920 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +0200921
Radek Krejcic071c542016-01-27 14:57:51 +0100922 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +0200923 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200924
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200925 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200926 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +0100927 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200928 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200929 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100930 submod = yang_read_submodule(module, data, 0, unres);
931 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200932 default:
Radek Krejci90a550a2016-04-13 16:00:58 +0200933 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200934 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200935 }
Radek Krejciefaeba32015-05-27 14:30:57 +0200936
Michal Vasko5a721fd2016-02-16 12:16:48 +0100937 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +0200938}
939
Michal Vasko1e62a092015-12-01 12:27:20 +0100940API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +0100941lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
942{
943 int fd;
944 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +0200945 const char *rev, *dot, *filename;
946 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +0100947
948 if (!ctx || !path) {
949 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
950 return NULL;
951 }
952
953 fd = open(path, O_RDONLY);
954 if (fd == -1) {
955 LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
956 return NULL;
957 }
958
959 ret = lys_parse_fd(ctx, fd, format);
960 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +0100961
Radek Krejcid80c8602016-10-25 11:56:03 +0200962 if (!ret) {
963 /* error */
964 return NULL;
965 }
966
967 /* check that name and revision match filename */
968 filename = strrchr(path, '/');
969 if (!filename) {
970 filename = path;
971 } else {
972 filename++;
973 }
974 rev = strchr(filename, '@');
975 dot = strrchr(filename, '.');
976
977 /* name */
978 len = strlen(ret->name);
979 if (strncmp(filename, ret->name, len) ||
980 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Radek Krejcic0c82302016-10-25 14:21:33 +0200981 LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +0200982 }
983 if (rev) {
984 len = dot - ++rev;
985 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
986 LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename,
987 ret->rev_size ? ret->rev[0].date : "none");
988 }
989 }
990
991 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +0100992 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +0100993 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +0100994 }
995
Michal Vasko662610a2015-12-07 11:25:45 +0100996 return ret;
997}
998
999API const struct lys_module *
1000lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001001{
Michal Vasko1e62a092015-12-01 12:27:20 +01001002 const struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +02001003 struct stat sb;
1004 char *addr;
Radek Krejcib051f722016-02-25 15:12:21 +01001005 char buf[PATH_MAX];
1006 int len;
Radek Krejci63a91a92015-07-29 13:31:04 +02001007
1008 if (!ctx || fd < 0) {
1009 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1010 return NULL;
1011 }
1012
Radek Krejci10a833c2015-12-16 15:28:37 +01001013 if (fstat(fd, &sb) == -1) {
1014 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
1015 return NULL;
1016 }
Radek Krejcib051f722016-02-25 15:12:21 +01001017 if (!S_ISREG(sb.st_mode)) {
1018 LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file");
1019 return NULL;
1020 }
1021
Michal Vasko164d9012016-04-01 10:16:59 +02001022 if (!sb.st_size) {
1023 LOGERR(LY_EINVAL, "File empty.");
1024 return NULL;
1025 }
1026
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001027 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001028 if (addr == MAP_FAILED) {
Michal Vasko662610a2015-12-07 11:25:45 +01001029 LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001030 return NULL;
1031 }
Radek Krejcia1df1682016-04-11 14:56:59 +02001032 module = lys_parse_mem_(ctx, addr, format, 1);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001033 munmap(addr, sb.st_size + 2);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001034
Radek Krejcia77904e2016-02-25 16:23:45 +01001035 if (module && !module->filepath) {
Radek Krejcib051f722016-02-25 15:12:21 +01001036 /* get URI if there is /proc */
1037 addr = NULL;
Radek Krejci15412ca2016-03-03 11:16:52 +01001038 if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) {
1039 if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) {
1040 ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len);
1041 }
1042 free(addr);
Radek Krejcib051f722016-02-25 15:12:21 +01001043 }
Radek Krejcib051f722016-02-25 15:12:21 +01001044 }
1045
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001046 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001047}
1048
Michal Vasko5a721fd2016-02-16 12:16:48 +01001049struct lys_submodule *
1050lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001051{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001052 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001053 struct stat sb;
1054 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001055
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001056 assert(module);
1057 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001058
Radek Krejci10a833c2015-12-16 15:28:37 +01001059 if (fstat(fd, &sb) == -1) {
1060 LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno));
Michal Vasko5a721fd2016-02-16 12:16:48 +01001061 return NULL;
Radek Krejci10a833c2015-12-16 15:28:37 +01001062 }
Michal Vasko164d9012016-04-01 10:16:59 +02001063
1064 if (!sb.st_size) {
1065 LOGERR(LY_EINVAL, "File empty.");
1066 return NULL;
1067 }
1068
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001069 addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
Pavol Vicane36ea262015-11-12 11:57:47 +01001070 if (addr == MAP_FAILED) {
1071 LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001072 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001073 }
Michal Vasko5a721fd2016-02-16 12:16:48 +01001074 submodule = lys_submodule_parse(module, addr, format, unres);
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001075 munmap(addr, sb.st_size + 2);
Radek Krejciefaeba32015-05-27 14:30:57 +02001076
Michal Vasko5a721fd2016-02-16 12:16:48 +01001077 return submodule;
1078
Radek Krejciefaeba32015-05-27 14:30:57 +02001079}
1080
Radek Krejci1d82ef62015-08-07 14:44:40 +02001081static struct lys_restr *
1082lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001083{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001084 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001085 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001086
Radek Krejci3733a802015-06-19 13:43:21 +02001087 if (!size) {
1088 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001089 }
Radek Krejci3733a802015-06-19 13:43:21 +02001090
1091 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001092 if (!result) {
Radek Krejciaa2a8932016-02-17 15:03:14 +01001093 LOGMEM;
Michal Vasko253035f2015-12-17 16:58:13 +01001094 return NULL;
1095 }
Radek Krejci3733a802015-06-19 13:43:21 +02001096 for (i = 0; i < size; i++) {
1097 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1098 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1099 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1100 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1101 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1102 }
1103
1104 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001105}
1106
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001107void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001108lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001109{
1110 assert(ctx);
1111 if (!restr) {
1112 return;
1113 }
1114
Radek Krejcifccd1442017-01-16 10:26:57 +01001115 lys_extension_instances_free(ctx, restr->ext, restr->ext_size);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001116 lydict_remove(ctx, restr->expr);
1117 lydict_remove(ctx, restr->dsc);
1118 lydict_remove(ctx, restr->ref);
1119 lydict_remove(ctx, restr->eapptag);
1120 lydict_remove(ctx, restr->emsg);
1121}
1122
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001123static void
1124lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size)
1125{
1126 uint8_t i;
1127
1128 for (i = 0; i < iffeature_size; ++i) {
1129 free(iffeature[i].expr);
1130 free(iffeature[i].features);
1131 }
1132 free(iffeature);
1133}
1134
Michal Vaskob84f88a2015-09-24 13:16:10 +02001135static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001136type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci3a5501d2016-07-18 22:03:34 +02001137 LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001138{
1139 int i;
1140
1141 switch (base) {
1142 case LY_TYPE_BINARY:
1143 if (old->info.binary.length) {
1144 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
1145 }
1146 break;
1147
1148 case LY_TYPE_BITS:
1149 new->info.bits.count = old->info.bits.count;
1150 if (new->info.bits.count) {
1151 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1152 if (!new->info.bits.bit) {
1153 LOGMEM;
1154 return -1;
1155 }
1156 for (i = 0; i < new->info.bits.count; i++) {
1157 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1158 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1159 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
1160 new->info.bits.bit[i].flags = old->info.bits.bit[i].flags;
1161 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1162 }
1163 }
1164 break;
1165
1166 case LY_TYPE_DEC64:
1167 new->info.dec64.dig = old->info.dec64.dig;
Radek Krejci8c3b4b62016-06-17 14:32:12 +02001168 new->info.dec64.div = old->info.dec64.div;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001169 if (old->info.dec64.range) {
1170 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
1171 }
1172 break;
1173
1174 case LY_TYPE_ENUM:
1175 new->info.enums.count = old->info.enums.count;
1176 if (new->info.enums.count) {
1177 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
1178 if (!new->info.enums.enm) {
1179 LOGMEM;
1180 return -1;
1181 }
1182 for (i = 0; i < new->info.enums.count; i++) {
1183 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1184 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1185 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1186 new->info.enums.enm[i].flags = old->info.enums.enm[i].flags;
1187 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
1188 }
1189 }
1190 break;
1191
1192 case LY_TYPE_IDENT:
Michal Vaskoaffc37f2016-10-10 18:05:03 +00001193 new->info.ident.count = old->info.ident.count;
Michal Vasko878e38d2016-09-05 12:17:53 +02001194 if (old->info.ident.count) {
1195 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
1196 if (!new->info.ident.ref) {
1197 LOGMEM;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001198 return -1;
1199 }
Michal Vasko878e38d2016-09-05 12:17:53 +02001200 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1201 } else {
1202 /* there can be several unresolved base identities, duplicate them all */
1203 i = -1;
1204 while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) {
1205 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
1206 return -1;
1207 }
1208 --i;
1209 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001210 }
1211 break;
1212
1213 case LY_TYPE_INST:
1214 new->info.inst.req = old->info.inst.req;
1215 break;
1216
1217 case LY_TYPE_INT8:
1218 case LY_TYPE_INT16:
1219 case LY_TYPE_INT32:
1220 case LY_TYPE_INT64:
1221 case LY_TYPE_UINT8:
1222 case LY_TYPE_UINT16:
1223 case LY_TYPE_UINT32:
1224 case LY_TYPE_UINT64:
1225 if (old->info.num.range) {
1226 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
1227 }
1228 break;
1229
1230 case LY_TYPE_LEAFREF:
1231 if (old->info.lref.path) {
1232 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko5d631402016-07-21 13:15:15 +02001233 if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001234 return -1;
1235 }
1236 }
1237 break;
1238
1239 case LY_TYPE_STRING:
1240 if (old->info.str.length) {
1241 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
1242 }
1243 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
1244 new->info.str.pat_count = old->info.str.pat_count;
1245 break;
1246
1247 case LY_TYPE_UNION:
1248 new->info.uni.count = old->info.uni.count;
1249 if (new->info.uni.count) {
1250 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
1251 if (!new->info.uni.types) {
1252 LOGMEM;
1253 return -1;
1254 }
1255 for (i = 0; i < new->info.uni.count; i++) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001256 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 +02001257 return -1;
1258 }
1259 }
1260 }
1261 break;
1262
1263 default:
1264 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1265 break;
1266 }
1267 return EXIT_SUCCESS;
1268}
1269
1270struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001271lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
1272 int tpdftype, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001273{
1274 struct yang_type *new;
1275
1276 new = calloc(1, sizeof *new);
1277 if (!new) {
1278 LOGMEM;
1279 return NULL;
1280 }
1281 new->flags = old->flags;
1282 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001283 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001284 new->type = type;
1285 if (!new->name) {
1286 LOGMEM;
1287 goto error;
1288 }
Radek Krejci3a5501d2016-07-18 22:03:34 +02001289 if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001290 new->type->base = new->base;
1291 lys_type_free(module->ctx, new->type);
1292 memset(&new->type->info, 0, sizeof new->type->info);
1293 goto error;
1294 }
1295 return new;
1296
1297 error:
1298 free(new);
1299 return NULL;
1300}
1301
Radek Krejcie534c132016-11-23 13:32:31 +01001302/*
1303 * duplicate extension instance
1304 */
1305static struct lys_ext_instance **
Radek Krejci43ce4b72017-01-04 11:02:38 +01001306lys_ext_dup(struct lys_module *mod, struct lys_ext_instance **orig, unsigned int size,
1307 void *parent, LYEXT_PAR parent_type, struct unres_schema *unres)
Radek Krejcie534c132016-11-23 13:32:31 +01001308{
1309 int i;
Radek Krejci43ce4b72017-01-04 11:02:38 +01001310 unsigned int u = 0;
Radek Krejcie534c132016-11-23 13:32:31 +01001311 struct lys_ext_instance **result;
Radek Krejcie534c132016-11-23 13:32:31 +01001312 struct unres_ext *info, *info_orig;
1313
1314 assert(size);
1315 assert(!orig);
1316 assert(!(*orig));
1317
1318 result = calloc(size, sizeof *result);
1319 for (u = 0; u < size; u++) {
1320 if (orig[u]) {
1321 /* resolved extension instance, just duplicate it */
1322 result[u]->def = orig[u]->def;
Radek Krejci0aa821a2016-12-08 11:21:35 +01001323 switch(lys_ext_instance_type(orig[u])) {
1324 case LYEXT_FLAG:
1325 result[u] = malloc(sizeof(struct lys_ext_instance));
1326 break;
1327 case LYEXT_ERR:
1328 LOGINT;
1329 break;
Radek Krejcie534c132016-11-23 13:32:31 +01001330 }
Radek Krejci0aa821a2016-12-08 11:21:35 +01001331 /* generic part */
1332 result[u]->arg_value = lydict_insert(mod->ctx, orig[u]->arg_value, 0);
Radek Krejci43ce4b72017-01-04 11:02:38 +01001333 result[u]->parent = parent;
1334 result[u]->parent_type = parent_type;
Radek Krejci80c6b442017-01-04 16:21:13 +01001335 result[u]->substmt = orig[u]->substmt;
1336 result[u]->substmt_index = orig[u]->substmt_index;
Radek Krejcie534c132016-11-23 13:32:31 +01001337 } else {
1338 /* original extension is not yet resolved, so duplicate it in unres */
1339 i = unres_schema_find(unres, -1, orig, UNRES_EXT);
1340 if (i == -1) {
1341 /* extension not found in unres */
1342 LOGINT;
Radek Krejci43ce4b72017-01-04 11:02:38 +01001343 goto error;
Radek Krejcie534c132016-11-23 13:32:31 +01001344 }
1345 info_orig = unres->str_snode[i];
1346 info = malloc(sizeof *info);
1347 info->datatype = info_orig->datatype;
1348 if (info->datatype == LYS_IN_YIN) {
1349 info->data.yin = lyxml_dup_elem(mod->ctx, info_orig->data.yin, NULL, 1);
1350 } /* else TODO YANG */
Radek Krejci43ce4b72017-01-04 11:02:38 +01001351 info->parent = parent;
1352 info->parent_type = parent_type;
Radek Krejcie534c132016-11-23 13:32:31 +01001353 if (unres_schema_add_node(mod, unres, &result[u], UNRES_EXT, (struct lys_node *)info) == -1) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001354 goto error;
Radek Krejcie534c132016-11-23 13:32:31 +01001355 }
1356 }
1357 }
1358
1359 return result;
Radek Krejci43ce4b72017-01-04 11:02:38 +01001360
1361error:
1362 lys_extension_instances_free(mod->ctx, result, u);
1363 return NULL;
1364}
1365
1366API const void *
1367lys_ext_instance_substmt(const struct lys_ext_instance *ext)
1368{
1369 if (!ext) {
1370 return NULL;
1371 }
1372
1373 switch (ext->substmt) {
1374 case LYEXT_SUBSTMT_SELF:
1375 case LYEXT_SUBSTMT_MODIFIER:
1376 case LYEXT_SUBSTMT_VERSION:
1377 return NULL;
1378 case LYEXT_SUBSTMT_ARGUMENT:
1379 if (ext->parent_type == LYEXT_PAR_EXT) {
1380 return ((struct lys_ext_instance*)ext->parent)->arg_value;
1381 }
1382 break;
1383 case LYEXT_SUBSTMT_BASE:
1384 if (ext->parent_type == LYEXT_PAR_TYPE) {
1385 return ((struct lys_type*)ext->parent)->info.ident.ref[ext->substmt_index];
1386 } else if (ext->parent_type == LYEXT_PAR_IDENT) {
1387 return ((struct lys_ident*)ext->parent)->base[ext->substmt_index];
1388 }
1389 break;
1390 case LYEXT_SUBSTMT_BELONGSTO:
1391 if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) {
1392 return ((struct lys_submodule*)ext->parent)->belongsto;
1393 }
1394 break;
1395 case LYEXT_SUBSTMT_CONFIG:
1396 case LYEXT_SUBSTMT_MANDATORY:
1397 if (ext->parent_type == LYEXT_PAR_NODE) {
1398 return &((struct lys_node*)ext->parent)->flags;
1399 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1400 return &((struct lys_deviate*)ext->parent)->flags;
1401 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1402 return &((struct lys_refine*)ext->parent)->flags;
1403 }
1404 break;
1405 case LYEXT_SUBSTMT_CONTACT:
1406 if (ext->parent_type == LYEXT_PAR_MODULE) {
1407 return ((struct lys_module*)ext->parent)->contact;
1408 }
1409 break;
1410 case LYEXT_SUBSTMT_DEFAULT:
1411 if (ext->parent_type == LYEXT_PAR_NODE) {
1412 switch (((struct lys_node*)ext->parent)->nodetype) {
1413 case LYS_LEAF:
1414 case LYS_LEAFLIST:
1415 /* in case of leaf, the index is supposed to be 0, so it will return the
1416 * correct pointer despite the leaf structure does not have dflt as array */
1417 return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->substmt_index];
1418 case LYS_CHOICE:
1419 return ((struct lys_node_choice*)ext->parent)->dflt;
1420 default:
1421 /* internal error */
1422 break;
1423 }
1424 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1425 return ((struct lys_tpdf*)ext->parent)->dflt;
1426 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1427 return ((struct lys_deviate*)ext->parent)->dflt[ext->substmt_index];
1428 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1429 return &((struct lys_refine*)ext->parent)->dflt[ext->substmt_index];
1430 }
1431 break;
1432 case LYEXT_SUBSTMT_DESCRIPTION:
1433 switch (ext->parent_type) {
1434 case LYEXT_PAR_NODE:
1435 return ((struct lys_node*)ext->parent)->dsc;
1436 case LYEXT_PAR_MODULE:
1437 return ((struct lys_module*)ext->parent)->dsc;
1438 case LYEXT_PAR_IMPORT:
1439 return ((struct lys_import*)ext->parent)->dsc;
1440 case LYEXT_PAR_INCLUDE:
1441 return ((struct lys_include*)ext->parent)->dsc;
1442 case LYEXT_PAR_EXT:
1443 return ((struct lys_ext*)ext->parent)->dsc;
1444 case LYEXT_PAR_FEATURE:
1445 return ((struct lys_feature*)ext->parent)->dsc;
1446 case LYEXT_PAR_TPDF:
1447 return ((struct lys_tpdf*)ext->parent)->dsc;
1448 case LYEXT_PAR_TYPE_BIT:
1449 return ((struct lys_type_bit*)ext->parent)->dsc;
1450 case LYEXT_PAR_TYPE_ENUM:
1451 return ((struct lys_type_enum*)ext->parent)->dsc;
1452 case LYEXT_PAR_MUST:
1453 case LYEXT_PAR_RANGE:
1454 case LYEXT_PAR_LENGTH:
1455 case LYEXT_PAR_PATTERN:
1456 return ((struct lys_restr*)ext->parent)->dsc;
1457 case LYEXT_PAR_WHEN:
1458 return ((struct lys_when*)ext->parent)->dsc;
1459 case LYEXT_PAR_IDENT:
1460 return ((struct lys_ident*)ext->parent)->dsc;
1461 case LYEXT_PAR_DEVIATION:
1462 return ((struct lys_deviation*)ext->parent)->dsc;
1463 case LYEXT_PAR_REVISION:
1464 return ((struct lys_revision*)ext->parent)->dsc;
1465 case LYEXT_PAR_REFINE:
1466 return ((struct lys_refine*)ext->parent)->dsc;
1467 default:
1468 break;
1469 }
1470 break;
1471 case LYEXT_SUBSTMT_ERRTAG:
1472 if (ext->parent_type == LYEXT_PAR_MUST || ext->parent_type == LYEXT_PAR_RANGE ||
1473 ext->parent_type == LYEXT_PAR_LENGTH || ext->parent_type == LYEXT_PAR_PATTERN) {
1474 return ((struct lys_restr*)ext->parent)->eapptag;
1475 }
1476 break;
1477 case LYEXT_SUBSTMT_ERRMSG:
1478 if (ext->parent_type == LYEXT_PAR_MUST || ext->parent_type == LYEXT_PAR_RANGE ||
1479 ext->parent_type == LYEXT_PAR_LENGTH || ext->parent_type == LYEXT_PAR_PATTERN) {
1480 return ((struct lys_restr*)ext->parent)->emsg;
1481 }
1482 break;
1483 case LYEXT_SUBSTMT_DIGITS:
1484 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) {
1485 return &((struct lys_type*)ext->parent)->info.dec64.dig;
1486 }
1487 break;
1488 case LYEXT_SUBSTMT_KEY:
1489 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1490 return ((struct lys_node_list*)ext->parent)->keys;
1491 }
1492 break;
1493 case LYEXT_SUBSTMT_MAX:
1494 if (ext->parent_type == LYEXT_PAR_NODE) {
1495 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1496 return &((struct lys_node_list*)ext->parent)->max;
1497 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1498 return &((struct lys_node_leaflist*)ext->parent)->max;
1499 }
1500 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1501 return &((struct lys_refine*)ext->parent)->mod.list.max;
1502 }
1503 break;
1504 case LYEXT_SUBSTMT_MIN:
1505 if (ext->parent_type == LYEXT_PAR_NODE) {
1506 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1507 return &((struct lys_node_list*)ext->parent)->min;
1508 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1509 return &((struct lys_node_leaflist*)ext->parent)->min;
1510 }
1511 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1512 return &((struct lys_refine*)ext->parent)->mod.list.min;
1513 }
1514 break;
1515 case LYEXT_SUBSTMT_NAMESPACE:
1516 if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) {
1517 return ((struct lys_module*)ext->parent)->ns;
1518 }
1519 break;
1520 case LYEXT_SUBSTMT_ORDEREDBY:
1521 if (ext->parent_type == LYEXT_PAR_NODE &&
1522 (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1523 return &((struct lys_node_list*)ext->parent)->flags;
1524 }
1525 break;
1526 case LYEXT_SUBSTMT_ORGANIZATION:
1527 if (ext->parent_type == LYEXT_PAR_MODULE) {
1528 return ((struct lys_module*)ext->parent)->org;
1529 }
1530 break;
1531 case LYEXT_SUBSTMT_PATH:
1532 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1533 return ((struct lys_type*)ext->parent)->info.lref.path;
1534 }
1535 break;
1536 case LYEXT_SUBSTMT_POSITION:
1537 if (ext->parent_type == LYEXT_PAR_TYPE_BIT) {
1538 return &((struct lys_type_bit*)ext->parent)->pos;
1539 }
1540 break;
1541 case LYEXT_SUBSTMT_PREFIX:
1542 if (ext->parent_type == LYEXT_PAR_MODULE) {
1543 /* covers also lys_submodule */
1544 return ((struct lys_module*)ext->parent)->prefix;
1545 } else if (ext->parent_type == LYEXT_PAR_IMPORT) {
1546 return ((struct lys_import*)ext->parent)->prefix;
1547 }
1548 break;
1549 case LYEXT_SUBSTMT_PRESENCE:
1550 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) {
1551 return ((struct lys_node_container*)ext->parent)->presence;
1552 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1553 return ((struct lys_refine*)ext->parent)->mod.presence;
1554 }
1555 break;
1556 case LYEXT_SUBSTMT_REFERENCE:
1557 switch (ext->parent_type) {
1558 case LYEXT_PAR_NODE:
1559 return ((struct lys_node*)ext->parent)->ref;
1560 case LYEXT_PAR_MODULE:
1561 return ((struct lys_module*)ext->parent)->ref;
1562 case LYEXT_PAR_IMPORT:
1563 return ((struct lys_import*)ext->parent)->ref;
1564 case LYEXT_PAR_INCLUDE:
1565 return ((struct lys_include*)ext->parent)->ref;
1566 case LYEXT_PAR_EXT:
1567 return ((struct lys_ext*)ext->parent)->ref;
1568 case LYEXT_PAR_FEATURE:
1569 return ((struct lys_feature*)ext->parent)->ref;
1570 case LYEXT_PAR_TPDF:
1571 return ((struct lys_tpdf*)ext->parent)->ref;
1572 case LYEXT_PAR_TYPE_BIT:
1573 return ((struct lys_type_bit*)ext->parent)->ref;
1574 case LYEXT_PAR_TYPE_ENUM:
1575 return ((struct lys_type_enum*)ext->parent)->ref;
1576 case LYEXT_PAR_MUST:
1577 case LYEXT_PAR_RANGE:
1578 case LYEXT_PAR_LENGTH:
1579 case LYEXT_PAR_PATTERN:
1580 return ((struct lys_restr*)ext->parent)->ref;
1581 case LYEXT_PAR_WHEN:
1582 return ((struct lys_when*)ext->parent)->ref;
1583 case LYEXT_PAR_IDENT:
1584 return ((struct lys_ident*)ext->parent)->ref;
1585 case LYEXT_PAR_DEVIATION:
1586 return ((struct lys_deviation*)ext->parent)->ref;
1587 case LYEXT_PAR_REVISION:
1588 return ((struct lys_revision*)ext->parent)->ref;
1589 case LYEXT_PAR_REFINE:
1590 return ((struct lys_refine*)ext->parent)->ref;
1591 default:
1592 break;
1593 }
1594 break;
1595 case LYEXT_SUBSTMT_REQINST:
1596 if (ext->parent_type == LYEXT_PAR_TYPE) {
1597 if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1598 return &((struct lys_type*)ext->parent)->info.lref.req;
1599 } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) {
1600 return &((struct lys_type*)ext->parent)->info.inst.req;
1601 }
1602 }
1603 break;
1604 case LYEXT_SUBSTMT_REVISIONDATE:
1605 if (ext->parent_type == LYEXT_PAR_IMPORT) {
1606 return ((struct lys_import*)ext->parent)->rev;
1607 } else if (ext->parent_type == LYEXT_PAR_INCLUDE) {
1608 return ((struct lys_include*)ext->parent)->rev;
1609 }
1610 break;
1611 case LYEXT_SUBSTMT_STATUS:
1612 switch (ext->parent_type) {
1613 case LYEXT_PAR_NODE:
1614 case LYEXT_PAR_IDENT:
1615 case LYEXT_PAR_TPDF:
1616 case LYEXT_PAR_EXT:
1617 case LYEXT_PAR_FEATURE:
1618 case LYEXT_PAR_TYPE_ENUM:
1619 case LYEXT_PAR_TYPE_BIT:
1620 /* in all structures the flags member is at the same offset */
1621 return &((struct lys_node*)ext->parent)->flags;
1622 default:
1623 break;
1624 }
1625 break;
1626 case LYEXT_SUBSTMT_UNIQUE:
1627 if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1628 return &((struct lys_deviate*)ext->parent)->unique[ext->substmt_index];
1629 } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1630 return &((struct lys_node_list*)ext->parent)->unique[ext->substmt_index];
1631 }
1632 break;
1633 case LYEXT_SUBSTMT_UNITS:
1634 if (ext->parent_type == LYEXT_PAR_NODE &&
1635 (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1636 /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */
1637 return ((struct lys_node_leaf*)ext->parent)->units;
1638 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1639 return ((struct lys_tpdf*)ext->parent)->units;
1640 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1641 return ((struct lys_deviate*)ext->parent)->units;
1642 }
1643 break;
1644 case LYEXT_SUBSTMT_VALUE:
1645 if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) {
1646 return &((struct lys_type_enum*)ext->parent)->value;
1647 }
1648 break;
1649 case LYEXT_SUBSTMT_YINELEM:
1650 if (ext->parent_type == LYEXT_PAR_EXT) {
1651 return &((struct lys_ext*)ext->parent)->flags;
1652 }
1653 break;
1654 }
1655 LOGINT;
1656 return NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01001657}
1658
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001659static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001660lys_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 +02001661 int tpdftype, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001662{
1663 int i;
1664
Michal Vasko1dca6882015-10-22 14:29:42 +02001665 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001666 new->base = old->base;
1667 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001668 new->parent = (struct lys_tpdf *)parent;
Radek Krejcie534c132016-11-23 13:32:31 +01001669 if (old->ext_size) {
1670 new->ext_size = old->ext_size;
Radek Krejci43ce4b72017-01-04 11:02:38 +01001671 new->ext = lys_ext_dup(mod, old->ext, old->ext_size, new->parent, LYEXT_PAR_TPDF, unres);
Radek Krejcie534c132016-11-23 13:32:31 +01001672 if (!new->ext) {
1673 return -1;
1674 }
1675 }
Radek Krejci3733a802015-06-19 13:43:21 +02001676
Michal Vasko878e38d2016-09-05 12:17:53 +02001677 i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001678 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001679 /* HACK (serious one) for unres */
1680 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001681 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci3a5501d2016-07-18 22:03:34 +02001682 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 +02001683 } else {
1684 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1685 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001686 /* all these unres additions can fail even though they did not before */
Radek Krejci3a5501d2016-07-18 22:03:34 +02001687 if (!new->der || unres_schema_add_node(mod, unres, new,
Michal Vasko5d631402016-07-21 13:15:15 +02001688 tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001689 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001690 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001691 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001692 }
1693
Radek Krejci3a5501d2016-07-18 22:03:34 +02001694 return type_dup(mod, parent, new, old, new->base, tpdftype, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001695}
1696
1697void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001698lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001699{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001700 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001701
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001702 assert(ctx);
1703 if (!type) {
1704 return;
1705 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001706
Michal Vasko1dca6882015-10-22 14:29:42 +02001707 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001708
Radek Krejcie534c132016-11-23 13:32:31 +01001709 lys_extension_instances_free(ctx, type->ext, type->ext_size);
1710
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001711 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001712 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001713 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001714 free(type->info.binary.length);
1715 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001716 case LY_TYPE_BITS:
1717 for (i = 0; i < type->info.bits.count; i++) {
1718 lydict_remove(ctx, type->info.bits.bit[i].name);
1719 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1720 lydict_remove(ctx, type->info.bits.bit[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001721 lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size);
Radek Krejci994b6f62015-06-18 16:47:27 +02001722 }
1723 free(type->info.bits.bit);
1724 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001725
1726 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001727 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001728 free(type->info.dec64.range);
1729 break;
1730
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001731 case LY_TYPE_ENUM:
1732 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001733 lydict_remove(ctx, type->info.enums.enm[i].name);
1734 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1735 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001736 lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001737 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001738 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001739 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001740
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001741 case LY_TYPE_INT8:
1742 case LY_TYPE_INT16:
1743 case LY_TYPE_INT32:
1744 case LY_TYPE_INT64:
1745 case LY_TYPE_UINT8:
1746 case LY_TYPE_UINT16:
1747 case LY_TYPE_UINT32:
1748 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001749 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001750 free(type->info.num.range);
1751 break;
1752
Radek Krejcidc4c1412015-06-19 15:39:54 +02001753 case LY_TYPE_LEAFREF:
1754 lydict_remove(ctx, type->info.lref.path);
1755 break;
1756
Radek Krejci3733a802015-06-19 13:43:21 +02001757 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001758 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001759 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001760 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001761 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001762 }
1763 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001764 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001765
Radek Krejcie4c366b2015-07-02 10:11:31 +02001766 case LY_TYPE_UNION:
1767 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001768 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001769 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001770 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001771 break;
1772
Michal Vaskod3282192016-09-05 11:27:57 +02001773 case LY_TYPE_IDENT:
1774 free(type->info.ident.ref);
1775 break;
1776
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001777 default:
Michal Vaskod3282192016-09-05 11:27:57 +02001778 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001779 break;
1780 }
Radek Krejci5a065542015-05-22 15:02:07 +02001781}
1782
Radek Krejci1d82ef62015-08-07 14:44:40 +02001783static void
1784lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001785{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001786 assert(ctx);
1787 if (!tpdf) {
1788 return;
1789 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001790
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001791 lydict_remove(ctx, tpdf->name);
1792 lydict_remove(ctx, tpdf->dsc);
1793 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001794
Radek Krejci1d82ef62015-08-07 14:44:40 +02001795 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001796
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001797 lydict_remove(ctx, tpdf->units);
1798 lydict_remove(ctx, tpdf->dflt);
Radek Krejcie534c132016-11-23 13:32:31 +01001799
1800 lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001801}
1802
Michal Vaskob84f88a2015-09-24 13:16:10 +02001803static struct lys_tpdf *
1804lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1805{
1806 struct lys_tpdf *result;
1807 int i, j;
1808
1809 if (!size) {
1810 return NULL;
1811 }
1812
1813 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001814 if (!result) {
1815 LOGMEM;
1816 return NULL;
1817 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001818 for (i = 0; i < size; i++) {
1819 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1820 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1821 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1822 result[i].flags = old[i].flags;
1823 result[i].module = old[i].module;
1824
Radek Krejci3a5501d2016-07-18 22:03:34 +02001825 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001826 for (j = 0; j <= i; ++j) {
1827 lys_tpdf_free(mod->ctx, &result[j]);
1828 }
1829 free(result);
1830 return NULL;
1831 }
1832
1833 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1834 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1835 }
1836
1837 return result;
1838}
1839
Radek Krejci1d82ef62015-08-07 14:44:40 +02001840static struct lys_when *
1841lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001842{
Radek Krejci76512572015-08-04 09:47:08 +02001843 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001844
1845 if (!old) {
1846 return NULL;
1847 }
1848
1849 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001850 if (!new) {
1851 LOGMEM;
1852 return NULL;
1853 }
Radek Krejci00768f42015-06-18 17:04:04 +02001854 new->cond = lydict_insert(ctx, old->cond, 0);
1855 new->dsc = lydict_insert(ctx, old->dsc, 0);
1856 new->ref = lydict_insert(ctx, old->ref, 0);
1857
1858 return new;
1859}
1860
Michal Vasko0308dd62015-10-07 09:14:40 +02001861void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001862lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001863{
1864 if (!w) {
1865 return;
1866 }
1867
1868 lydict_remove(ctx, w->cond);
1869 lydict_remove(ctx, w->dsc);
1870 lydict_remove(ctx, w->ref);
1871
1872 free(w);
1873}
1874
Radek Krejcib7f5e412015-08-13 10:15:51 +02001875static void
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001876lys_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 +02001877{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001878 struct lys_node *next, *sub;
1879
Radek Krejcic071c542016-01-27 14:57:51 +01001880 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01001881 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001882 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001883 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01001884 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001885 }
1886
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001887 lydict_remove(ctx, aug->target_name);
1888 lydict_remove(ctx, aug->dsc);
1889 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001890
Radek Krejci9ff0a922016-07-14 13:08:05 +02001891 lys_iffeature_free(aug->iffeature, aug->iffeature_size);
Radek Krejcie534c132016-11-23 13:32:31 +01001892 lys_extension_instances_free(ctx, aug->ext, aug->ext_size);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001893
Michal Vasko9eb6dd02016-05-02 14:52:40 +02001894 lys_when_free(ctx, aug->when);
Radek Krejcib7f5e412015-08-13 10:15:51 +02001895}
1896
Radek Krejci76512572015-08-04 09:47:08 +02001897static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001898lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001899{
Radek Krejci76512572015-08-04 09:47:08 +02001900 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001901 struct lys_node *old_child, *new_child;
1902 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001903
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001904 if (!size) {
1905 return NULL;
1906 }
Radek Krejci106efc02015-06-10 14:36:27 +02001907
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001908 new = calloc(size, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001909 if (!new) {
1910 LOGMEM;
1911 return NULL;
1912 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001913 for (i = 0; i < size; i++) {
1914 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1915 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1916 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1917 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001918 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001919 new[i].nodetype = old[i].nodetype;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01001920
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001921 /* this must succeed, it was already resolved once */
Radek Krejcidf46e222016-11-08 11:57:37 +01001922 if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, 1,
Michal Vasko3edeaf72016-02-11 13:17:43 +01001923 (const struct lys_node **)&new[i].target)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001924 LOGINT;
1925 free(new);
1926 return NULL;
1927 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001928 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001929
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001930 /* Correct the augment nodes.
1931 * This function can only be called from lys_node_dup() with uses
1932 * being the node duplicated, so we must have a case of grouping
1933 * with a uses with augments. The augmented nodes have already been
1934 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001935 * (it was set to their actual data parent, not an augment), and
1936 * the new augment does not have child pointer to its augment nodes,
1937 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001938 */
1939 LY_TREE_FOR(new[i].target->child, new_child) {
Radek Krejci749190d2016-02-18 16:26:25 +01001940 if (ly_strequal(new_child->name, old[i].child->name, 1)) {
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001941 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001942 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001943 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001944 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001945 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001946 LY_TREE_FOR(old[i].child, old_child) {
1947 /* all augment nodes were connected as siblings, there can be no more after this */
1948 if (old_child->parent != (struct lys_node *)&old[i]) {
1949 break;
1950 }
1951
Radek Krejci749190d2016-02-18 16:26:25 +01001952 assert(ly_strequal(old_child->name, new_child->name, 1));
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001953
1954 new_child->parent = (struct lys_node *)&new[i];
1955 new_child = new_child->next;
1956 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001957 }
Radek Krejci106efc02015-06-10 14:36:27 +02001958
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001959 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001960}
1961
Pavol Vican3e7c73a2016-08-17 10:24:11 +02001962static const char **
1963lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size)
1964{
1965 int i;
1966 const char **result;
1967
1968 if (!size) {
1969 return NULL;
1970 }
1971
1972 result = calloc(size, sizeof *result);
1973 if (!result) {
1974 LOGMEM;
1975 return NULL;
1976 }
1977
1978 for (i = 0; i < size; i++) {
1979 result[i] = lydict_insert(ctx, old[i], 0);
1980 }
1981 return result;
1982}
1983
Radek Krejci76512572015-08-04 09:47:08 +02001984static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001985lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001986{
Radek Krejci76512572015-08-04 09:47:08 +02001987 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001988 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001989
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001990 if (!size) {
1991 return NULL;
1992 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001993
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001994 result = calloc(size, sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01001995 if (!result) {
1996 LOGMEM;
1997 return NULL;
1998 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001999 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002000 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002001 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
2002 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002003 result[i].flags = old[i].flags;
2004 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002005
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002006 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002007 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002008
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002009 result[i].dflt_size = old[i].dflt_size;
2010 result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size);
2011
2012 if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002013 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02002014 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002015 result[i].mod.list = old[i].mod.list;
2016 }
2017 }
Michal Vasko1982cad2015-06-08 15:49:30 +02002018
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002019 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02002020}
2021
Radek Krejci1d82ef62015-08-07 14:44:40 +02002022static void
2023lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02002024{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002025 assert(ctx);
2026 if (!ident) {
2027 return;
2028 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002029
Radek Krejci018f1f52016-08-03 16:01:20 +02002030 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02002031 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002032 lydict_remove(ctx, ident->name);
2033 lydict_remove(ctx, ident->dsc);
2034 lydict_remove(ctx, ident->ref);
Radek Krejcif1ee2e22016-08-02 16:36:48 +02002035 lys_iffeature_free(ident->iffeature, ident->iffeature_size);
Radek Krejcie534c132016-11-23 13:32:31 +01002036 lys_extension_instances_free(ctx, ident->ext, ident->ext_size);
Radek Krejci6793db02015-05-22 17:49:54 +02002037
2038}
2039
Radek Krejci1d82ef62015-08-07 14:44:40 +02002040static void
2041lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002042{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002043 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002044
Radek Krejcid12f57b2015-08-06 10:43:39 +02002045 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002046 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002047 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002048 }
2049 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002050}
2051
Radek Krejci1d82ef62015-08-07 14:44:40 +02002052static void
Michal Vasko44fb6382016-06-29 11:12:27 +02002053lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io)
Radek Krejcid12f57b2015-08-06 10:43:39 +02002054{
2055 int i;
2056
2057 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
2058 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002059 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002060 }
2061 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002062
2063 for (i = 0; i < io->must_size; i++) {
2064 lys_restr_free(ctx, &io->must[i]);
2065 }
2066 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002067}
2068
Radek Krejci1d82ef62015-08-07 14:44:40 +02002069static void
Pavol Vican7cff97e2016-08-09 14:56:08 +02002070lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif)
2071{
2072 int i;
2073
2074 for (i = 0; i < notif->must_size; i++) {
2075 lys_restr_free(ctx, &notif->must[i]);
2076 }
2077 free(notif->must);
2078
2079 for (i = 0; i < notif->tpdf_size; i++) {
2080 lys_tpdf_free(ctx, &notif->tpdf[i]);
2081 }
2082 free(notif->tpdf);
2083}
2084static void
Radek Krejcibf2abff2016-08-23 15:51:52 +02002085lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02002086{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002087 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002088
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002089 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002090 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002091 }
2092 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002093
Radek Krejci1d82ef62015-08-07 14:44:40 +02002094 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002095}
2096
Radek Krejci1d82ef62015-08-07 14:44:40 +02002097static void
2098lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02002099{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002100 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002101
Radek Krejci85a54be2016-10-20 12:39:56 +02002102 /* leafref backlinks */
2103 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002104
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002105 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002106 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002107 }
2108 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002109
Radek Krejci1d82ef62015-08-07 14:44:40 +02002110 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002111
Radek Krejci1d82ef62015-08-07 14:44:40 +02002112 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002113 lydict_remove(ctx, leaf->units);
2114 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02002115}
2116
Radek Krejci1d82ef62015-08-07 14:44:40 +02002117static void
2118lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02002119{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002120 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002121
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002122 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002123 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002124 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002125 }
2126
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002127 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002128 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002129 }
2130 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002131
Pavol Vican38321d02016-08-16 14:56:02 +02002132 for (i = 0; i < llist->dflt_size; i++) {
2133 lydict_remove(ctx, llist->dflt[i]);
2134 }
2135 free(llist->dflt);
2136
Radek Krejci1d82ef62015-08-07 14:44:40 +02002137 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002138
Radek Krejci1d82ef62015-08-07 14:44:40 +02002139 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002140 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02002141}
2142
Radek Krejci1d82ef62015-08-07 14:44:40 +02002143static void
2144lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002145{
Radek Krejci581ce772015-11-10 17:22:40 +01002146 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002147
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002148 /* handle only specific parts for LY_NODE_LIST */
2149 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002150 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002151 }
2152 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002153
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002154 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002155 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002156 }
2157 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002158
Radek Krejci1d82ef62015-08-07 14:44:40 +02002159 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002160
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002161 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02002162 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002163 lydict_remove(ctx, list->unique[i].expr[j]);
2164 }
2165 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002166 }
2167 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02002168
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002169 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002170}
2171
Radek Krejci1d82ef62015-08-07 14:44:40 +02002172static void
2173lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02002174{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002175 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002176
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002177 /* handle only specific parts for LY_NODE_CONTAINER */
2178 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02002179
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002180 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002181 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002182 }
2183 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02002184
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002185 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002186 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002187 }
2188 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002189
Radek Krejci1d82ef62015-08-07 14:44:40 +02002190 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002191}
2192
Radek Krejci1d82ef62015-08-07 14:44:40 +02002193static void
2194lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02002195{
2196 lydict_remove(ctx, f->name);
2197 lydict_remove(ctx, f->dsc);
2198 lydict_remove(ctx, f->ref);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002199 lys_iffeature_free(f->iffeature, f->iffeature_size);
Radek Krejci9de2c042016-10-19 16:53:06 +02002200 ly_set_free(f->depfeatures);
Radek Krejcie534c132016-11-23 13:32:31 +01002201 lys_extension_instances_free(ctx, f->ext, f->ext_size);
2202}
2203
2204static void
2205lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e)
2206{
2207 lydict_remove(ctx, e->name);
2208 lydict_remove(ctx, e->dsc);
2209 lydict_remove(ctx, e->ref);
2210 lydict_remove(ctx, e->argument);
2211 lys_extension_instances_free(ctx, e->ext, e->ext_size);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002212}
2213
Radek Krejci1d82ef62015-08-07 14:44:40 +02002214static void
Michal Vaskoff006c12016-02-17 11:15:19 +01002215lys_deviation_free(struct lys_module *module, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02002216{
Radek Krejci581ce772015-11-10 17:22:40 +01002217 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01002218 struct ly_ctx *ctx;
2219 struct lys_node *next, *elem;
2220
2221 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02002222
2223 lydict_remove(ctx, dev->target_name);
2224 lydict_remove(ctx, dev->dsc);
2225 lydict_remove(ctx, dev->ref);
Radek Krejcie534c132016-11-23 13:32:31 +01002226 lys_extension_instances_free(ctx, dev->ext, dev->ext_size);
Radek Krejcieb00f512015-07-01 16:44:58 +02002227
Pavol Vican64d0b762016-08-25 10:44:59 +02002228 if (!dev->deviate) {
2229 return ;
2230 }
2231
Michal Vaskoff006c12016-02-17 11:15:19 +01002232 /* the module was freed, but we only need the context from orig_node, use ours */
2233 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2234 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
2235 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
2236 elem->module = module;
2237
2238 LY_TREE_DFS_END(dev->orig_node, next, elem);
2239 }
2240 lys_node_free(dev->orig_node, NULL, 0);
2241 } else {
2242 /* it's just a shallow copy, freeing one node */
2243 dev->orig_node->module = module;
2244 lys_node_free(dev->orig_node, NULL, 1);
2245 }
2246
Radek Krejcieb00f512015-07-01 16:44:58 +02002247 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejcid5a5c282016-08-15 15:38:08 +02002248 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02002249 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02002250 }
2251 free(dev->deviate[i].dflt);
2252
Radek Krejcieb00f512015-07-01 16:44:58 +02002253 lydict_remove(ctx, dev->deviate[i].units);
2254
2255 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
2256 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002257 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002258 }
2259 free(dev->deviate[i].must);
2260
2261 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002262 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
2263 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
2264 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01002265 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002266 }
2267 free(dev->deviate[i].unique);
2268 }
2269 }
2270 free(dev->deviate);
2271}
2272
Radek Krejci1d82ef62015-08-07 14:44:40 +02002273static void
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002274lys_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 +02002275{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002276 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02002277
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002278 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002279 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002280 lydict_remove(ctx, uses->refine[i].dsc);
2281 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002282
Michal Vaskoa275c0a2015-09-24 09:55:42 +02002283 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002284 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002285 }
2286 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002287
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002288 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02002289 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002290 }
2291 free(uses->refine[i].dflt);
2292
Radek Krejcie534c132016-11-23 13:32:31 +01002293 lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size);
2294
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002295 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002296 lydict_remove(ctx, uses->refine[i].mod.presence);
2297 }
2298 }
2299 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002300
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002301 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002302 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002303 }
2304 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002305
Radek Krejci1d82ef62015-08-07 14:44:40 +02002306 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002307}
2308
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002309void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002310lys_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 +02002311{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002312 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02002313 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002314
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002315 if (!node) {
2316 return;
2317 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002318
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002319 assert(node->module);
2320 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02002321
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002322 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002323
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002324 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002325 if (node->priv && private_destructor) {
2326 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002327 }
2328
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002329 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002330 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002331 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002332 lys_iffeature_free(node->iffeature, node->iffeature_size);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002333 lydict_remove(ctx, node->dsc);
2334 lydict_remove(ctx, node->ref);
2335 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002336
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002337 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002338 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002339 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002340 }
2341 }
2342
Radek Krejcie534c132016-11-23 13:32:31 +01002343 lys_extension_instances_free(ctx, node->ext, node->ext_size);
2344
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002345 /* specific part */
2346 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002347 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002348 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002349 break;
Radek Krejci76512572015-08-04 09:47:08 +02002350 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002351 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002352 break;
Radek Krejci76512572015-08-04 09:47:08 +02002353 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002354 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002355 break;
Radek Krejci76512572015-08-04 09:47:08 +02002356 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002357 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002358 break;
Radek Krejci76512572015-08-04 09:47:08 +02002359 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002360 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002361 break;
Radek Krejci76512572015-08-04 09:47:08 +02002362 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002363 case LYS_ANYDATA:
2364 lys_anydata_free(ctx, (struct lys_node_anydata *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002365 break;
Radek Krejci76512572015-08-04 09:47:08 +02002366 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002367 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002368 break;
Radek Krejci76512572015-08-04 09:47:08 +02002369 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002370 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002371 break;
Radek Krejci76512572015-08-04 09:47:08 +02002372 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002373 /* do nothing */
2374 break;
Radek Krejci76512572015-08-04 09:47:08 +02002375 case LYS_GROUPING:
2376 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002377 case LYS_ACTION:
Radek Krejci1d82ef62015-08-07 14:44:40 +02002378 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002379 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02002380 case LYS_NOTIF:
2381 lys_notif_free(ctx, (struct lys_node_notif *)node);
2382 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002383 case LYS_INPUT:
2384 case LYS_OUTPUT:
Michal Vasko44fb6382016-06-29 11:12:27 +02002385 lys_inout_free(ctx, (struct lys_node_inout *)node);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002386 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02002387 case LYS_UNKNOWN:
2388 LOGINT;
2389 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002390 }
Radek Krejci5a065542015-05-22 15:02:07 +02002391
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002392 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002393 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002394 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002395}
2396
Radek Krejci2eee5c02016-12-06 19:18:05 +01002397API struct lys_module *
2398lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02002399{
2400 struct ly_ctx *ctx;
2401 int i;
2402
2403 if (!mod || mod->implemented) {
2404 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002405 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002406 }
2407
2408 ctx = mod->ctx;
2409 for (i = 0; i < ctx->models.used; i++) {
2410 if (!ctx->models.list[i]->implemented) {
2411 continue;
2412 }
2413
2414 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2415 /* we have some revision of the module implemented */
2416 return ctx->models.list[i];
2417 }
2418 }
2419
2420 /* we have no revision of the module implemented, return the module itself,
2421 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002422 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002423}
2424
2425const struct lys_module *
Radek Krejcie534c132016-11-23 13:32:31 +01002426lys_get_import_module_ns(const struct lys_module *module, const char *ns)
2427{
2428 int i;
2429
2430 assert(module && ns);
2431
Radek Krejcic789d692017-01-11 11:31:14 +01002432 if (module->type) {
2433 /* the module is actually submodule and to get the namespace, we need the main module */
2434 if (ly_strequal(((struct lys_submodule *)module)->belongsto->ns, ns, 0)) {
2435 return ((struct lys_submodule *)module)->belongsto;
2436 }
2437 } else {
2438 /* modul's own namespace */
2439 if (ly_strequal(module->ns, ns, 0)) {
2440 return module;
2441 }
Radek Krejcie534c132016-11-23 13:32:31 +01002442 }
2443
2444 /* imported modules */
2445 for (i = 0; i < module->imp_size; ++i) {
2446 if (ly_strequal(module->imp[i].module->ns, ns, 0)) {
2447 return module->imp[i].module;
2448 }
2449 }
2450
2451 return NULL;
2452}
2453
2454const struct lys_module *
Michal Vasko1e62a092015-12-01 12:27:20 +01002455lys_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 +02002456{
Radek Krejcic071c542016-01-27 14:57:51 +01002457 const struct lys_module *main_module;
Michal Vasko89563fc2016-07-28 16:19:35 +02002458 char *str;
Michal Vasko3edeaf72016-02-11 13:17:43 +01002459 int i;
Michal Vaskob6729c62015-10-21 12:09:47 +02002460
Michal Vaskoa7789a82016-02-11 15:42:55 +01002461 assert(!prefix || !name);
2462
Michal Vaskob6729c62015-10-21 12:09:47 +02002463 if (prefix && !pref_len) {
2464 pref_len = strlen(prefix);
2465 }
2466 if (name && !name_len) {
2467 name_len = strlen(name);
2468 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02002469
Radek Krejcic4283442016-04-22 09:19:27 +02002470 main_module = lys_main_module(module);
Michal Vaskoa7789a82016-02-11 15:42:55 +01002471
2472 /* module own prefix, submodule own prefix, (sub)module own name */
2473 if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len])
2474 || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
Michal Vasko3edeaf72016-02-11 13:17:43 +01002475 && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) {
Radek Krejcic071c542016-01-27 14:57:51 +01002476 return main_module;
Michal Vaskod8da86b2015-10-21 13:35:37 +02002477 }
2478
Michal Vasko89563fc2016-07-28 16:19:35 +02002479 /* standard import */
Michal Vasko8ce24d72015-10-21 11:27:26 +02002480 for (i = 0; i < module->imp_size; ++i) {
Michal Vasko3edeaf72016-02-11 13:17:43 +01002481 if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len]))
2482 && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02002483 return module->imp[i].module;
2484 }
2485 }
2486
Michal Vasko89563fc2016-07-28 16:19:35 +02002487 /* module required by a foreign grouping, deviation, or submodule */
2488 if (name) {
2489 str = strndup(name, name_len);
2490 if (!str) {
2491 LOGMEM;
2492 return NULL;
2493 }
2494 main_module = ly_ctx_get_module(module->ctx, str, NULL);
2495 free(str);
2496 return main_module;
2497 }
2498
Michal Vasko8ce24d72015-10-21 11:27:26 +02002499 return NULL;
2500}
2501
Michal Vasko13b15832015-08-19 11:04:48 +02002502/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002503static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002504module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002505{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002506 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002507 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002508 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002509
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002510 assert(module->ctx);
2511 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002512
Michal Vaskob746fff2016-02-11 11:37:50 +01002513 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002514 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002515 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002516 lydict_remove(ctx, module->imp[i].dsc);
2517 lydict_remove(ctx, module->imp[i].ref);
Radek Krejcie534c132016-11-23 13:32:31 +01002518 lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size);
Radek Krejci225376f2016-02-16 17:36:22 +01002519 }
Radek Krejcidce51452015-06-16 15:20:08 +02002520 free(module->imp);
2521
Radek Krejcic071c542016-01-27 14:57:51 +01002522 /* submodules don't have data tree, the data nodes
2523 * are placed in the main module altogether */
2524 if (!module->type) {
2525 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002526 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002527 }
Radek Krejci21181962015-06-30 14:11:00 +02002528 }
Radek Krejci5a065542015-05-22 15:02:07 +02002529
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002530 lydict_remove(ctx, module->dsc);
2531 lydict_remove(ctx, module->ref);
2532 lydict_remove(ctx, module->org);
2533 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002534 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002535
Radek Krejcieb00f512015-07-01 16:44:58 +02002536 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002537 for (i = 0; i < module->rev_size; i++) {
2538 lydict_remove(ctx, module->rev[i].dsc);
2539 lydict_remove(ctx, module->rev[i].ref);
2540 }
2541 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002542
Radek Krejcieb00f512015-07-01 16:44:58 +02002543 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002544 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002545 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002546 }
2547 module->ident_size = 0;
2548 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002549
Radek Krejcieb00f512015-07-01 16:44:58 +02002550 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002551 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002552 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002553 }
2554 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002555
Radek Krejcie534c132016-11-23 13:32:31 +01002556 /* extension instances */
2557 lys_extension_instances_free(ctx, module->ext, module->ext_size);
2558
Radek Krejcieb00f512015-07-01 16:44:58 +02002559 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002560 for (i = 0; i < module->inc_size; i++) {
Michal Vasko8bfe3812016-07-27 13:37:52 +02002561 lydict_remove(ctx, module->inc[i].dsc);
2562 lydict_remove(ctx, module->inc[i].ref);
Radek Krejcie534c132016-11-23 13:32:31 +01002563 lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size);
Radek Krejcic071c542016-01-27 14:57:51 +01002564 /* complete submodule free is done only from main module since
2565 * submodules propagate their includes to the main module */
2566 if (!module->type) {
Michal Vaskob746fff2016-02-11 11:37:50 +01002567 lys_submodule_free(module->inc[i].submodule, private_destructor);
Radek Krejcic071c542016-01-27 14:57:51 +01002568 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002569 }
2570 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002571
Radek Krejcieb00f512015-07-01 16:44:58 +02002572 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002573 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002574 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002575 }
2576 free(module->augment);
2577
Radek Krejcieb00f512015-07-01 16:44:58 +02002578 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002579 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002580 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002581 }
2582 free(module->features);
2583
Radek Krejcieb00f512015-07-01 16:44:58 +02002584 /* deviations */
2585 for (i = 0; i < module->deviation_size; i++) {
Michal Vaskoff006c12016-02-17 11:15:19 +01002586 lys_deviation_free(module, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002587 }
2588 free(module->deviation);
2589
Radek Krejcie534c132016-11-23 13:32:31 +01002590 /* extensions */
2591 for (i = 0; i < module->extensions_size; i++) {
2592 lys_extension_free(ctx, &module->extensions[i]);
2593 }
2594 free(module->extensions);
2595
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002596 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002597 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002598}
2599
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002600void
Michal Vaskob746fff2016-02-11 11:37:50 +01002601lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002602{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002603 if (!submodule) {
2604 return;
2605 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002606
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002607 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002608 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002609
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002610 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002611
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002612 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002613}
2614
Radek Krejci3a5501d2016-07-18 22:03:34 +02002615static int
2616ingrouping(const struct lys_node *node)
2617{
2618 const struct lys_node *iter = node;
2619 assert(node);
2620
2621 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2622 if (!iter) {
2623 return 0;
2624 } else {
2625 return 1;
2626 }
2627}
2628
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002629/*
2630 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2631 */
2632static struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01002633lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002634 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002635{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002636 struct lys_node *retval = NULL, *iter, *p;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002637 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002638 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002639 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002640 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002641 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002642
Michal Vaskoc07187d2015-08-13 15:20:57 +02002643 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002644 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002645 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002646 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002647 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002648 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002649 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002650 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002651 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002652 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002653 struct lys_node_anydata *any = NULL;
2654 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002655 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002656 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002657 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002658 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002659 struct lys_node_rpc_action *rpc = NULL;
2660 struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node;
2661 struct lys_node_inout *io = NULL;
2662 struct lys_node_inout *io_orig = (struct lys_node_inout *)node;
2663 struct lys_node_rpc_action *ntf = NULL;
2664 struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002665 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002666 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002667
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002668 /* we cannot just duplicate memory since the strings are stored in
2669 * dictionary and we need to update dictionary counters.
2670 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002671
Radek Krejci1d82ef62015-08-07 14:44:40 +02002672 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002673 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002674 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002675 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002676 break;
2677
Radek Krejci76512572015-08-04 09:47:08 +02002678 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002679 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002680 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002681 break;
2682
Radek Krejci76512572015-08-04 09:47:08 +02002683 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002684 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002685 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002686 break;
2687
Radek Krejci76512572015-08-04 09:47:08 +02002688 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002689 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002690 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002691 break;
2692
Radek Krejci76512572015-08-04 09:47:08 +02002693 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002694 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002695 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002696 break;
2697
Radek Krejci76512572015-08-04 09:47:08 +02002698 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002699 case LYS_ANYDATA:
2700 any = calloc(1, sizeof *any);
2701 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002702 break;
2703
Radek Krejci76512572015-08-04 09:47:08 +02002704 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002705 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002706 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002707 break;
2708
Radek Krejci76512572015-08-04 09:47:08 +02002709 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002710 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002711 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002712 break;
2713
Radek Krejci76512572015-08-04 09:47:08 +02002714 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002715 grp = calloc(1, sizeof *grp);
2716 retval = (struct lys_node *)grp;
2717 break;
2718
Radek Krejci76512572015-08-04 09:47:08 +02002719 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002720 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002721 rpc = calloc(1, sizeof *rpc);
2722 retval = (struct lys_node *)rpc;
2723 break;
2724
Radek Krejci76512572015-08-04 09:47:08 +02002725 case LYS_INPUT:
2726 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002727 io = calloc(1, sizeof *io);
2728 retval = (struct lys_node *)io;
2729 break;
2730
Radek Krejci76512572015-08-04 09:47:08 +02002731 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002732 ntf = calloc(1, sizeof *ntf);
2733 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002734 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002735
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002736 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002737 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002738 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002739 }
Radek Krejcib388c152015-06-04 17:03:03 +02002740
Michal Vasko253035f2015-12-17 16:58:13 +01002741 if (!retval) {
2742 LOGMEM;
2743 return NULL;
2744 }
2745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002746 /*
2747 * duplicate generic part of the structure
2748 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002749 retval->name = lydict_insert(ctx, node->name, 0);
2750 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2751 retval->ref = lydict_insert(ctx, node->ref, 0);
2752 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002753
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002754 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002755 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002756
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002757 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002758
Radek Krejci06214042016-11-04 16:25:58 +01002759 if (node->iffeature_size) {
2760 retval->iffeature_size = node->iffeature_size;
2761 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
2762 if (!retval->iffeature) {
2763 LOGMEM;
2764 goto error;
2765 }
Michal Vasko253035f2015-12-17 16:58:13 +01002766 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002767
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002768 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002769 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002770 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2771 if (size1) {
2772 /* there is something to duplicate */
2773
2774 /* duplicate compiled expression */
2775 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2776 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
2777 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2778
2779 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002780 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002781 for (j = 0; (unsigned int)j < size2; j++) {
2782 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2783 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002784 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002785 /* feature is resolved in origin, so copy it
2786 * - duplication is used for instantiating groupings
2787 * and if-feature inside grouping is supposed to be
2788 * resolved inside the original grouping, so we want
2789 * to keep pointers to features from the grouping
2790 * context */
2791 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2792 } else if (rc == -1) {
2793 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002794 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002795 }
2796 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002797 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002798
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002799 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002800 p = parent;
2801 do {
2802 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2803 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002804 if (iter) {
2805 flags = iter->flags & LYS_CONFIG_MASK;
2806 } else {
2807 /* default */
2808 flags = LYS_CONFIG_W;
2809 }
2810
2811 switch (finalize) {
2812 case 1:
2813 /* inherit config flags */
2814 if (retval->flags & LYS_CONFIG_SET) {
2815 /* skip nodes with an explicit config value */
2816 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
2817 LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2818 LOGVAL(LYE_SPEC, LY_VLOG_LYS, retval, "State nodes cannot have configuration nodes as children.");
2819 goto error;
2820 }
2821 break;
2822 }
2823
2824 if (retval->nodetype != LYS_USES) {
2825 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2826 }
2827 break;
2828 case 2:
2829 /* erase config flags */
2830 retval->flags &= ~LYS_CONFIG_MASK;
2831 retval->flags &= ~LYS_CONFIG_SET;
2832 break;
2833 }
2834
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002835 /* connect it to the parent */
2836 if (lys_node_addchild(parent, retval->module, retval)) {
2837 goto error;
2838 }
Radek Krejcidce51452015-06-16 15:20:08 +02002839
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002840 /* go recursively */
2841 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002842 LY_TREE_FOR(node->child, iter) {
Radek Krejci6ff885d2017-01-03 14:06:22 +01002843 if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002844 goto error;
2845 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002846 }
2847 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002848
2849 if (finalize == 1) {
2850 /* check that configuration lists have keys
2851 * - we really want to check keys_size in original node, because the keys are
2852 * not yet resolved here, it is done below in nodetype specific part */
2853 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2854 && !((struct lys_node_list *)node)->keys_size) {
2855 LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
2856 goto error;
2857 }
2858 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002859 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002860 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002861 }
2862
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002863 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002864 * duplicate specific part of the structure
2865 */
2866 switch (node->nodetype) {
2867 case LYS_CONTAINER:
2868 if (cont_orig->when) {
2869 cont->when = lys_when_dup(ctx, cont_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002870 }
2871 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002872
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002873 cont->must_size = cont_orig->must_size;
2874 cont->tpdf_size = cont_orig->tpdf_size;
2875
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002876 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Michal Vaskodcf98e62016-05-05 17:53:53 +02002877 cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002878 break;
2879
2880 case LYS_CHOICE:
2881 if (choice_orig->when) {
2882 choice->when = lys_when_dup(ctx, choice_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002883 }
2884
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002885 if (!shallow) {
2886 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002887 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2888 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2889 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002890 if (rc) {
2891 if (rc == EXIT_FAILURE) {
2892 LOGINT;
2893 }
2894 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002895 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002896 } else {
2897 /* useless to check return value, we don't know whether
2898 * there really wasn't any default defined or it just hasn't
2899 * been resolved, we just hope for the best :)
2900 */
2901 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02002902 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002903 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002904 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002905 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002906 break;
2907
2908 case LYS_LEAF:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002909 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002910 goto error;
2911 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002912 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2913
2914 if (leaf_orig->dflt) {
2915 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejci51673202016-11-01 17:00:32 +01002916 if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT,
2917 (struct lys_node *)(&leaf->dflt)) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002918 goto error;
2919 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002920 }
2921
2922 leaf->must_size = leaf_orig->must_size;
2923 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002924
2925 if (leaf_orig->when) {
2926 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002927 }
2928 break;
2929
2930 case LYS_LEAFLIST:
Radek Krejci3a5501d2016-07-18 22:03:34 +02002931 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02002932 goto error;
2933 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002934 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
2935
2936 llist->min = llist_orig->min;
2937 llist->max = llist_orig->max;
2938
2939 llist->must_size = llist_orig->must_size;
2940 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002941
Radek Krejci51673202016-11-01 17:00:32 +01002942 llist->dflt_size = llist_orig->dflt_size;
2943 llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt);
2944 for (i = 0; i < llist->dflt_size; i++) {
2945 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
2946 if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
2947 (struct lys_node *)(&llist->dflt[i])) == -1) {
2948 goto error;
2949 }
2950 }
2951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002952 if (llist_orig->when) {
2953 llist->when = lys_when_dup(ctx, llist_orig->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002954 }
2955 break;
2956
2957 case LYS_LIST:
2958 list->min = list_orig->min;
2959 list->max = list_orig->max;
2960
2961 list->must_size = list_orig->must_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002962 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002963
Radek Krejci581ce772015-11-10 17:22:40 +01002964 list->tpdf_size = list_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02002965 list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres);
Michal Vasko38d01f72015-06-15 09:41:06 +02002966
Radek Krejci581ce772015-11-10 17:22:40 +01002967 list->keys_size = list_orig->keys_size;
Michal Vasko38d01f72015-06-15 09:41:06 +02002968 if (list->keys_size) {
2969 list->keys = calloc(list->keys_size, sizeof *list->keys);
Radek Krejci5c08a992016-11-02 13:30:04 +01002970 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Michal Vasko253035f2015-12-17 16:58:13 +01002971 if (!list->keys) {
2972 LOGMEM;
2973 goto error;
2974 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002975
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002976 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01002977 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
2978 * in another grouping */
2979 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
2980 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
2981 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02002982 }
Michal Vasko0ea41032015-06-16 08:53:55 +02002983 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002984 memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02002985 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002986 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002987
Radek Krejci581ce772015-11-10 17:22:40 +01002988 list->unique_size = list_orig->unique_size;
2989 list->unique = malloc(list->unique_size * sizeof *list->unique);
Michal Vasko253035f2015-12-17 16:58:13 +01002990 if (!list->unique) {
2991 LOGMEM;
2992 goto error;
2993 }
Radek Krejci581ce772015-11-10 17:22:40 +01002994 for (i = 0; i < list->unique_size; ++i) {
2995 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2996 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko253035f2015-12-17 16:58:13 +01002997 if (!list->unique[i].expr) {
2998 LOGMEM;
2999 goto error;
3000 }
Radek Krejci581ce772015-11-10 17:22:40 +01003001 for (j = 0; j < list->unique[i].expr_size; j++) {
3002 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
3003
3004 /* if it stays in unres list, duplicate it also there */
Radek Krejcid09d1a52016-08-11 14:05:45 +02003005 unique_info = malloc(sizeof *unique_info);
3006 unique_info->list = (struct lys_node *)list;
3007 unique_info->expr = list->unique[i].expr[j];
3008 unique_info->trg_type = &list->unique[i].trg_type;
3009 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003010 }
3011 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003012
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003013 if (list_orig->when) {
3014 list->when = lys_when_dup(ctx, list_orig->when);
Radek Krejciefaeba32015-05-27 14:30:57 +02003015 }
Radek Krejcidce51452015-06-16 15:20:08 +02003016 break;
3017
3018 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02003019 case LYS_ANYDATA:
3020 any->must_size = any_orig->must_size;
3021 any->must = lys_restr_dup(ctx, any_orig->must, any->must_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003022
Radek Krejcibf2abff2016-08-23 15:51:52 +02003023 if (any_orig->when) {
3024 any->when = lys_when_dup(ctx, any_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003025 }
3026 break;
3027
3028 case LYS_USES:
3029 uses->grp = uses_orig->grp;
3030
3031 if (uses_orig->when) {
3032 uses->when = lys_when_dup(ctx, uses_orig->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003033 }
3034
3035 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02003036 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003037 uses->augment_size = uses_orig->augment_size;
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003038 if (!shallow) {
3039 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Radek Krejci6ff885d2017-01-03 14:06:22 +01003040#if __BYTE_ORDER == __LITTLE_ENDIAN
3041 if (!uses->grp || ((uint8_t*)&uses->grp->flags)[1]) {
3042#else
3043 if (!uses->grp || ((uint8_t*)&uses->grp->flags)[0]) {
3044#endif
Michal Vaskocda51712016-05-19 15:22:11 +02003045 assert(!uses->child);
Radek Krejci48464ed2016-03-17 15:44:09 +01003046 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003047 goto error;
3048 }
Michal Vasko49168a22015-08-17 16:35:41 +02003049 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003050 } else {
3051 memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003052 }
3053 break;
3054
Radek Krejcidce51452015-06-16 15:20:08 +02003055 case LYS_CASE:
3056 if (cs_orig->when) {
3057 cs->when = lys_when_dup(ctx, cs_orig->when);
Radek Krejcidce51452015-06-16 15:20:08 +02003058 }
3059 break;
3060
3061 case LYS_GROUPING:
3062 grp->tpdf_size = grp_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02003063 grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02003064 break;
3065
Radek Krejci96935402016-11-04 16:27:28 +01003066 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02003067 case LYS_RPC:
3068 rpc->tpdf_size = rpc_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02003069 rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcidce51452015-06-16 15:20:08 +02003070 break;
3071
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003072 case LYS_INPUT:
3073 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02003074 io->tpdf_size = io_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02003075 io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003076 break;
3077
Radek Krejcida04f4a2015-05-21 12:54:09 +02003078 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003079 ntf->tpdf_size = ntf_orig->tpdf_size;
Michal Vaskodcf98e62016-05-05 17:53:53 +02003080 ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003081 break;
3082
Radek Krejci96935402016-11-04 16:27:28 +01003083
Radek Krejcida04f4a2015-05-21 12:54:09 +02003084 default:
3085 /* LY_NODE_AUGMENT */
3086 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02003087 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003088 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003089
3090 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02003091
3092error:
3093
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003094 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02003095 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003096}
3097
Radek Krejcib3142312016-11-09 11:04:12 +01003098int
3099lys_has_xpath(const struct lys_node *node)
3100{
3101 assert(node);
3102
3103 switch (node->nodetype) {
3104 case LYS_AUGMENT:
3105 if (((struct lys_node_augment *)node)->when) {
3106 return 1;
3107 }
3108 break;
3109 case LYS_CASE:
3110 if (((struct lys_node_case *)node)->when) {
3111 return 1;
3112 }
3113 break;
3114 case LYS_CHOICE:
3115 if (((struct lys_node_choice *)node)->when) {
3116 return 1;
3117 }
3118 break;
3119 case LYS_ANYDATA:
3120 if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) {
3121 return 1;
3122 }
3123 break;
3124 case LYS_LEAF:
3125 if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) {
3126 return 1;
3127 }
3128 break;
3129 case LYS_LEAFLIST:
3130 if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) {
3131 return 1;
3132 }
3133 break;
3134 case LYS_LIST:
3135 if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) {
3136 return 1;
3137 }
3138 break;
3139 case LYS_CONTAINER:
3140 if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) {
3141 return 1;
3142 }
3143 break;
3144 case LYS_INPUT:
3145 case LYS_OUTPUT:
3146 if (((struct lys_node_inout *)node)->must_size) {
3147 return 1;
3148 }
3149 break;
3150 case LYS_NOTIF:
3151 if (((struct lys_node_notif *)node)->must_size) {
3152 return 1;
3153 }
3154 break;
3155 case LYS_USES:
3156 if (((struct lys_node_uses *)node)->when) {
3157 return 1;
3158 }
3159 break;
3160 default:
3161 /* does not have XPath */
3162 break;
3163 }
3164
3165 return 0;
3166}
3167
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003168struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01003169lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003170 struct unres_schema *unres, int shallow)
3171{
3172 struct lys_node *p = NULL;
Radek Krejcib3142312016-11-09 11:04:12 +01003173 int finalize = 0;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003174 struct lys_node *result, *iter, *next;
3175
3176 if (!shallow) {
3177 /* get know where in schema tre we are to know what should be done during instantiation of the grouping */
3178 for (p = parent;
3179 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
3180 p = lys_parent(p));
3181 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
3182 }
3183
Radek Krejci6ff885d2017-01-03 14:06:22 +01003184 result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize);
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003185 if (finalize) {
3186 /* check xpath expressions in the instantiated tree */
3187 for (iter = next = parent->child; iter; iter = next) {
Radek Krejcib3142312016-11-09 11:04:12 +01003188 if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
Radek Krejci3c48d042016-11-07 14:42:36 +01003189 /* invalid xpath */
3190 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003191 }
3192
3193 /* select next item */
3194 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
3195 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
3196 next = NULL;
3197 } else {
3198 next = iter->child;
3199 }
3200 if (!next) {
3201 /* no children, try siblings */
3202 next = iter->next;
3203 }
3204 while (!next) {
3205 /* parent is already processed, go to its sibling */
3206 iter = lys_parent(iter);
3207 if (iter == parent) {
3208 /* we are done, no next element to process */
3209 break;
3210 }
3211 next = iter->next;
3212 }
3213 }
3214 }
3215
3216 return result;
3217}
3218
Michal Vasko13b15832015-08-19 11:04:48 +02003219void
Michal Vaskoff006c12016-02-17 11:15:19 +01003220lys_node_switch(struct lys_node *dst, struct lys_node *src)
3221{
3222 struct lys_node *child;
3223
Michal Vaskob42b6972016-06-06 14:21:30 +02003224 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01003225
3226 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02003227 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01003228 dst->prev->next = src;
3229 }
3230
3231 /* sibling prev */
3232 if (dst->next) {
3233 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02003234 } else {
3235 for (child = dst->prev; child->prev->next; child = child->prev);
3236 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003237 }
3238
3239 /* next */
3240 src->next = dst->next;
3241 dst->next = NULL;
3242
3243 /* prev */
3244 if (dst->prev != dst) {
3245 src->prev = dst->prev;
3246 }
3247 dst->prev = dst;
3248
3249 /* parent child */
3250 if (dst->parent && (dst->parent->child == dst)) {
3251 dst->parent->child = src;
3252 }
3253
3254 /* parent */
3255 src->parent = dst->parent;
3256 dst->parent = NULL;
3257
3258 /* child parent */
3259 LY_TREE_FOR(dst->child, child) {
3260 if (child->parent == dst) {
3261 child->parent = src;
3262 }
3263 }
3264
3265 /* child */
3266 src->child = dst->child;
3267 dst->child = NULL;
3268}
3269
3270void
Michal Vasko627975a2016-02-11 11:39:03 +01003271lys_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 +02003272{
3273 struct ly_ctx *ctx;
3274 int i;
3275
3276 if (!module) {
3277 return;
3278 }
3279
3280 /* remove schema from the context */
3281 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01003282 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02003283 for (i = 0; i < ctx->models.used; i++) {
3284 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01003285 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003286 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01003287 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 +02003288 ctx->models.list[ctx->models.used] = NULL;
3289 /* we are done */
3290 break;
3291 }
3292 }
3293 }
3294
3295 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01003296 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003297
3298 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01003299 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003300
3301 free(module);
3302}
Radek Krejci7e97c352015-06-19 16:26:34 +02003303
Radek Krejci9de2c042016-10-19 16:53:06 +02003304static void
3305lys_features_disable_recursive(struct lys_feature *f)
3306{
3307 unsigned int i;
3308 struct lys_feature *depf;
3309
3310 /* disable the feature */
3311 f->flags &= ~LYS_FENABLED;
3312
3313 /* by disabling feature we have to disable also all features that depends on this feature */
3314 if (f->depfeatures) {
3315 for (i = 0; i < f->depfeatures->number; i++) {
3316 depf = (struct lys_feature *)f->depfeatures->set.g[i];
3317 if (depf->flags & LYS_FENABLED) {
3318 lys_features_disable_recursive(depf);
3319 }
3320 }
3321 }
3322}
3323
3324
Radek Krejci7e97c352015-06-19 16:26:34 +02003325/*
3326 * op: 1 - enable, 0 - disable
3327 */
3328static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003329lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02003330{
3331 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003332 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02003333 int progress, faili, failj, failk;
3334
3335 uint8_t fsize;
3336 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02003337
3338 if (!module || !name || !strlen(name)) {
3339 return EXIT_FAILURE;
3340 }
3341
3342 if (!strcmp(name, "*")) {
3343 /* enable all */
3344 all = 1;
3345 }
3346
Radek Krejcia889c1f2016-10-19 15:50:11 +02003347 progress = failk = 1;
3348 while (progress && failk) {
3349 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
3350 if (i == -1) {
3351 fsize = module->features_size;
3352 f = module->features;
3353 } else {
3354 fsize = module->inc[i].submodule->features_size;
3355 f = module->inc[i].submodule->features;
3356 }
3357
3358 for (j = 0; j < fsize; j++) {
3359 if (all || !strcmp(f[j].name, name)) {
Michal Vasko34c3b552016-11-21 09:07:43 +01003360 if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) {
3361 if (all) {
3362 /* skip already set features */
3363 continue;
3364 } else {
3365 /* feature already set correctly */
3366 return EXIT_SUCCESS;
3367 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003368 }
3369
3370 if (op) {
3371 /* check referenced features if they are enabled */
3372 for (k = 0; k < f[j].iffeature_size; k++) {
3373 if (!resolve_iffeature(&f[j].iffeature[k])) {
3374 if (all) {
3375 faili = i;
3376 failj = j;
3377 failk = k + 1;
3378 break;
3379 } else {
3380 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
3381 f[j].name, k + 1);
3382 return EXIT_FAILURE;
3383 }
3384 }
3385 }
3386
3387 if (k == f[j].iffeature_size) {
3388 /* the last check passed, do the change */
3389 f[j].flags |= LYS_FENABLED;
3390 progress++;
3391 }
3392 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02003393 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02003394 progress++;
3395 }
3396 if (!all) {
3397 /* stop in case changing a single feature */
3398 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003399 }
3400 }
Radek Krejci7e97c352015-06-19 16:26:34 +02003401 }
3402 }
3403 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003404 if (failk) {
3405 /* print info about the last failing feature */
3406 LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
3407 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
3408 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02003409 }
3410
3411 if (all) {
3412 return EXIT_SUCCESS;
3413 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02003414 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02003415 return EXIT_FAILURE;
3416 }
3417}
3418
3419API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003420lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003421{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003422 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02003423}
3424
3425API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003426lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003427{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003428 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003429}
3430
3431API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003432lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003433{
3434 int i, j;
3435
3436 if (!module || !feature) {
3437 return -1;
3438 }
3439
3440 /* search for the specified feature */
3441 /* module itself */
3442 for (i = 0; i < module->features_size; i++) {
3443 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003444 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003445 return 1;
3446 } else {
3447 return 0;
3448 }
3449 }
3450 }
3451
3452 /* submodules */
3453 for (j = 0; j < module->inc_size; j++) {
3454 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
3455 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003456 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003457 return 1;
3458 } else {
3459 return 0;
3460 }
3461 }
3462 }
3463 }
3464
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003465 /* feature definition not found */
3466 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02003467}
Michal Vasko2367e7c2015-07-07 11:33:44 +02003468
Radek Krejci96a10da2015-07-30 11:00:14 +02003469API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01003470lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02003471{
Radek Krejci96a10da2015-07-30 11:00:14 +02003472 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003473 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003474 unsigned int count;
3475
3476 if (!module) {
3477 return NULL;
3478 }
3479
3480 count = module->features_size;
3481 for (i = 0; i < module->inc_size; i++) {
3482 count += module->inc[i].submodule->features_size;
3483 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003484 result = malloc((count + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +01003485 if (!result) {
3486 LOGMEM;
3487 return NULL;
3488 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003489 if (states) {
3490 *states = malloc((count + 1) * sizeof **states);
Michal Vasko253035f2015-12-17 16:58:13 +01003491 if (!(*states)) {
3492 LOGMEM;
3493 free(result);
3494 return NULL;
3495 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003496 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003497 count = 0;
3498
3499 /* module itself */
3500 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003501 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003502 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003503 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003504 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003505 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003506 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003507 }
3508 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003509 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003510 }
3511
3512 /* submodules */
3513 for (j = 0; j < module->inc_size; j++) {
3514 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003515 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003516 if (states) {
3517 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3518 (*states)[count] = 1;
3519 } else {
3520 (*states)[count] = 0;
3521 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003522 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003523 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003524 }
3525 }
3526
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003527 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003528 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003529
3530 return result;
3531}
Michal Vaskobaefb032015-09-24 14:52:10 +02003532
Radek Krejci6910a032016-04-13 10:06:21 +02003533API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003534lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003535{
Michal Vaskof53187d2017-01-13 13:23:14 +01003536 if (!node) {
3537 return NULL;
3538 }
3539
Radek Krejcic071c542016-01-27 14:57:51 +01003540 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3541}
3542
Radek Krejci6910a032016-04-13 10:06:21 +02003543API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003544lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003545{
Michal Vaskof53187d2017-01-13 13:23:14 +01003546 if (!module) {
3547 return NULL;
3548 }
3549
Michal Vasko320e8532016-02-15 13:11:57 +01003550 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3551}
3552
Michal Vaskobaefb032015-09-24 14:52:10 +02003553API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003554lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003555{
3556 if (!node || !node->parent) {
3557 return NULL;
3558 }
3559
3560 if (node->parent->nodetype == LYS_AUGMENT) {
3561 return ((struct lys_node_augment *)node->parent)->target;
3562 }
3563
3564 return node->parent;
3565}
Michal Vasko1b229152016-01-13 11:28:38 +01003566
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003567API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003568lys_set_private(const struct lys_node *node, void *priv)
3569{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003570 void *prev;
3571
Michal Vasko1b229152016-01-13 11:28:38 +01003572 if (!node) {
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003573 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
3574 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003575 }
3576
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003577 prev = node->priv;
3578 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003579
3580 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003581}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003582
Michal Vasko01c6fd22016-05-20 11:43:05 +02003583int
3584lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3585{
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003586 struct lys_node_leaf *iter = leafref_target;
3587
Michal Vasko48a573d2016-07-01 11:46:02 +02003588 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003589 LOGINT;
3590 return -1;
3591 }
3592
Pavol Vican93175152016-08-30 15:34:44 +02003593 /* check for config flag */
3594 if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
3595 LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref,
3596 "The %s is config but refers to a non-config %s.",
3597 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3598 return -1;
3599 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003600 /* check for cycles */
3601 while (iter && iter->type.base == LY_TYPE_LEAFREF) {
3602 if ((void *)iter == (void *)leafref) {
3603 /* cycle detected */
3604 LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
3605 return -1;
3606 }
3607 iter = iter->type.info.lref.target;
3608 }
3609
3610 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003611 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003612 if (!leafref_target->backlinks) {
3613 leafref_target->backlinks = (void*)ly_set_new();
3614 if (!leafref_target->backlinks) {
Michal Vasko01c6fd22016-05-20 11:43:05 +02003615 LOGMEM;
3616 return -1;
3617 }
3618 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003619 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003620
3621 return 0;
3622}
3623
Michal Vasko8548e082016-07-22 12:00:18 +02003624/* not needed currently */
3625#if 0
3626
Michal Vasko5b3492c2016-07-20 09:37:40 +02003627static const char *
3628lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3629{
3630 struct lys_module *prev_mod;
3631 uint32_t str_len, mod_len, buf_idx;
3632
Radek Krejcibf2abff2016-08-23 15:51:52 +02003633 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003634 LOGINT;
3635 return NULL;
3636 }
3637
3638 buf_idx = buf_len - 1;
3639 buf[buf_idx] = '\0';
3640
3641 while (node) {
3642 if (lys_parent(node)) {
3643 prev_mod = lys_node_module(lys_parent(node));
3644 } else {
3645 prev_mod = NULL;
3646 }
3647
Radek Krejcibf2abff2016-08-23 15:51:52 +02003648 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003649 str_len = strlen(node->name);
3650
3651 if (prev_mod != node->module) {
3652 mod_len = strlen(node->module->name);
3653 } else {
3654 mod_len = 0;
3655 }
3656
3657 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3658 LOGINT;
3659 return NULL;
3660 }
3661
3662 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3663
3664 buf[buf_idx] = '/';
3665 if (mod_len) {
3666 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3667 buf[buf_idx + 1 + mod_len] = ':';
3668 }
3669 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3670 }
3671
3672 node = lys_parent(node);
3673 }
3674
3675 return buf + buf_idx;
3676}
3677
Michal Vasko8548e082016-07-22 12:00:18 +02003678#endif
3679
3680API struct ly_set *
Michal Vaskof06fb5b2016-09-08 10:05:56 +02003681lys_find_xpath(const struct lys_node *node, const char *expr, int options)
Michal Vasko46a4bf92016-09-08 08:23:49 +02003682{
3683 struct lyxp_set set;
3684 struct ly_set *ret_set;
3685 uint32_t i;
Michal Vaskodb1da032016-09-08 10:07:38 +02003686 int opts;
Michal Vasko46a4bf92016-09-08 08:23:49 +02003687
3688 if (!node || !expr) {
3689 ly_errno = LY_EINVAL;
3690 return NULL;
3691 }
3692
3693 memset(&set, 0, sizeof set);
3694
Michal Vaskodb1da032016-09-08 10:07:38 +02003695 opts = LYXP_SNODE;
3696 if (options & LYS_FIND_OUTPUT) {
3697 opts |= LYXP_SNODE_OUTPUT;
3698 }
3699
Michal Vasko46a4bf92016-09-08 08:23:49 +02003700 /* node and nodetype won't matter at all since it is absolute */
Michal Vaskodb1da032016-09-08 10:07:38 +02003701 if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) {
Michal Vasko46a4bf92016-09-08 08:23:49 +02003702 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003703 LOGVAL(LYE_SPEC, LY_VLOG_LYS, node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko46a4bf92016-09-08 08:23:49 +02003704 return NULL;
3705 }
3706
3707 ret_set = ly_set_new();
3708
3709 for (i = 0; i < set.used; ++i) {
3710 if (!set.val.snodes[i].in_ctx) {
3711 continue;
3712 }
3713 assert(set.val.snodes[i].in_ctx == 1);
3714
3715 switch (set.val.snodes[i].type) {
3716 case LYXP_NODE_ELEM:
3717 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
3718 ly_set_free(ret_set);
3719 free(set.val.snodes);
3720 return NULL;
3721 }
3722 break;
3723 default:
3724 /* ignore roots, text and attr should not ever appear */
3725 break;
3726 }
3727 }
3728
3729 free(set.val.snodes);
3730 return ret_set;
3731}
3732
3733API struct ly_set *
Michal Vasko508a50d2016-09-07 14:50:33 +02003734lys_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 +02003735{
Michal Vasko508a50d2016-09-07 14:50:33 +02003736 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003737 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003738 uint32_t i;
3739
Michal Vaskob94a5e42016-09-08 14:01:56 +02003740 if (!cur_snode || !expr) {
Michal Vasko8548e082016-07-22 12:00:18 +02003741 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003742 }
3743
Michal Vaskob94a5e42016-09-08 14:01:56 +02003744 /* adjust the root */
3745 if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) {
3746 do {
3747 cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0);
3748 } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R));
3749 }
3750
Michal Vasko508a50d2016-09-07 14:50:33 +02003751 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003752
3753 if (options & LYXP_MUST) {
3754 options &= ~LYXP_MUST;
3755 options |= LYXP_SNODE_MUST;
3756 } else if (options & LYXP_WHEN) {
3757 options &= ~LYXP_WHEN;
3758 options |= LYXP_SNODE_WHEN;
3759 } else {
3760 options |= LYXP_SNODE;
3761 }
3762
Michal Vasko508a50d2016-09-07 14:50:33 +02003763 if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) {
3764 free(set.val.snodes);
Radek Krejci9ee20c32016-11-09 00:04:13 +01003765 LOGVAL(LYE_SPEC, LY_VLOG_LYS, cur_snode, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003766 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003767 }
3768
Michal Vasko8548e082016-07-22 12:00:18 +02003769 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003770
Michal Vasko508a50d2016-09-07 14:50:33 +02003771 for (i = 0; i < set.used; ++i) {
3772 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003773 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003774 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003775 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003776 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003777 return NULL;
3778 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003779 break;
3780 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003781 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003782 break;
3783 }
3784 }
3785
Michal Vasko508a50d2016-09-07 14:50:33 +02003786 free(set.val.snodes);
3787 return ret_set;
3788}
3789
3790API struct ly_set *
3791lys_node_xpath_atomize(const struct lys_node *node, int options)
3792{
3793 const struct lys_node *next, *elem, *parent, *tmp;
3794 struct lyxp_set set;
3795 struct ly_set *ret_set;
3796 uint16_t i;
3797
3798 if (!node) {
3799 return NULL;
3800 }
3801
3802 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3803 if (!parent) {
3804 /* not in input, output, or notification */
3805 return NULL;
3806 }
3807
3808 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003809 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003810 return NULL;
3811 }
3812
3813 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003814 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003815 /* elem has no dependencies from other subtrees and local nodes get discarded */
3816 goto next_iter;
3817 }
3818
3819 if (lyxp_node_atomize(elem, &set)) {
3820 ly_set_free(ret_set);
3821 free(set.val.snodes);
3822 return NULL;
3823 }
3824
3825 for (i = 0; i < set.used; ++i) {
3826 switch (set.val.snodes[i].type) {
3827 case LYXP_NODE_ELEM:
3828 if (options & LYXP_NO_LOCAL) {
3829 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3830 if (tmp) {
3831 /* in local subtree, discard */
3832 break;
3833 }
3834 }
3835 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3836 ly_set_free(ret_set);
3837 free(set.val.snodes);
3838 return NULL;
3839 }
3840 break;
3841 default:
3842 /* ignore roots, text and attr should not ever appear */
3843 break;
3844 }
3845 }
3846
3847 free(set.val.snodes);
3848 if (!(options & LYXP_RECURSIVE)) {
3849 break;
3850 }
3851next_iter:
3852 LY_TREE_DFS_END(node, next, elem);
3853 }
3854
Michal Vasko8548e082016-07-22 12:00:18 +02003855 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003856}
3857
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003858static void
Michal Vasko89563fc2016-07-28 16:19:35 +02003859lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003860{
3861 int ret;
3862 char *parent_path;
3863 struct lys_node *target;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003864
Pavol Vican64d0b762016-08-25 10:44:59 +02003865 if (!dev->deviate) {
3866 return ;
3867 }
3868
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003869 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
3870 if (dev->orig_node) {
3871 /* removing not-supported deviation ... */
3872 if (strrchr(dev->target_name, '/') != dev->target_name) {
3873 /* ... from a parent */
3874 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
3875
3876 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003877 ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, 1,
3878 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003879 free(parent_path);
3880 if (ret || !target) {
3881 LOGINT;
3882 return;
3883 }
3884
3885 lys_node_addchild(target, NULL, dev->orig_node);
3886 } else {
3887 /* ... from top-level data */
3888 lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node);
3889 }
3890
3891 dev->orig_node = NULL;
3892 } else {
3893 /* adding not-supported deviation */
3894 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003895 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1,
3896 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003897 if (ret || !target) {
3898 LOGINT;
3899 return;
3900 }
3901
3902 lys_node_unlink(target);
3903 dev->orig_node = target;
3904 }
3905 } else {
3906 target = NULL;
Radek Krejcidf46e222016-11-08 11:57:37 +01003907 ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, 1,
3908 (const struct lys_node **)&target);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003909 if (ret || !target) {
3910 LOGINT;
3911 return;
3912 }
3913
3914 lys_node_switch(target, dev->orig_node);
3915 dev->orig_node = target;
3916 }
3917}
3918
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003919/* temporarily removes or applies deviations, updates module deviation flag accordingly */
3920void
3921lys_switch_deviations(struct lys_module *module)
3922{
Michal Vasko89563fc2016-07-28 16:19:35 +02003923 uint32_t i = 0, j;
3924 const struct lys_module *mod;
3925 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003926
Michal Vasko89563fc2016-07-28 16:19:35 +02003927 if (module->deviated) {
3928 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
3929 if (mod == module) {
3930 continue;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003931 }
3932
Michal Vasko89563fc2016-07-28 16:19:35 +02003933 for (j = 0; j < mod->deviation_size; ++j) {
3934 ptr = strstr(mod->deviation[j].target_name, module->name);
3935 if (ptr && ptr[strlen(module->name)] == ':') {
3936 lys_switch_deviation(&mod->deviation[j], module);
3937 }
3938 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003939 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003940
Michal Vasko89563fc2016-07-28 16:19:35 +02003941 if (module->deviated == 2) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003942 module->deviated = 1;
Michal Vasko89563fc2016-07-28 16:19:35 +02003943 } else {
3944 module->deviated = 2;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003945 }
3946 }
3947}
3948
Radek Krejci0ec51da2016-12-14 16:42:03 +01003949static void
3950apply_dev(struct lys_deviation *dev, const struct lys_module *module)
3951{
3952 lys_switch_deviation(dev, module);
3953
3954 assert(dev->orig_node);
3955 lys_node_module(dev->orig_node)->deviated = 1;
3956}
3957
3958static void
3959apply_aug(struct lys_node_augment *augment)
3960{
3961 struct lys_node *last;
3962
3963 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
3964
3965 /* reconnect augmenting data into the target - add them to the target child list */
3966 if (augment->target->child) {
3967 last = augment->target->child->prev;
3968 last->next = augment->child;
3969 augment->target->child->prev = augment->child->prev;
3970 augment->child->prev = last;
3971 } else {
3972 augment->target->child = augment->child;
3973 }
3974
3975 /* remove the flag about not applicability */
3976 augment->flags &= ~LYS_NOTAPPLIED;
3977}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003978
3979void
3980lys_sub_module_apply_devs_augs(struct lys_module *module)
3981{
Radek Krejci0ec51da2016-12-14 16:42:03 +01003982 uint8_t u, v;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003983
Radek Krejci0ec51da2016-12-14 16:42:03 +01003984 /* remove applied deviations */
3985 for (u = 0; u < module->deviation_size; ++u) {
3986 apply_dev(&module->deviation[u], module);
3987 }
3988 /* remove applied augments */
3989 for (u = 0; u < module->augment_size; ++u) {
3990 apply_aug(&module->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003991 }
3992
Radek Krejci0ec51da2016-12-14 16:42:03 +01003993 /* remove deviation and augments defined in submodules */
3994 for (v = 0; v < module->inc_size; ++v) {
3995 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
3996 apply_dev(&module->inc[v].submodule->deviation[u], module);
3997 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003998
Radek Krejci0ec51da2016-12-14 16:42:03 +01003999 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4000 apply_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004001 }
4002 }
4003}
4004
Radek Krejcib2541a32016-12-12 16:45:57 +01004005static void
4006remove_dev(struct lys_deviation *dev, const struct lys_module *module)
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004007{
Radek Krejcib2541a32016-12-12 16:45:57 +01004008 uint32_t idx = 0, j;
Michal Vasko89563fc2016-07-28 16:19:35 +02004009 const struct lys_module *mod;
4010 struct lys_module *target_mod;
4011 const char *ptr;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004012
Radek Krejcib2541a32016-12-12 16:45:57 +01004013 if (dev->orig_node) {
4014 target_mod = lys_node_module(dev->orig_node);
4015 } else {
Radek Krejci0ec51da2016-12-14 16:42:03 +01004016 LOGINT;
4017 return;
Radek Krejcib2541a32016-12-12 16:45:57 +01004018 }
4019 lys_switch_deviation(dev, module);
4020
4021 /* clear the deviation flag if possible */
4022 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
4023 if ((mod == module) || (mod == target_mod)) {
4024 continue;
Pavol Vican64d0b762016-08-25 10:44:59 +02004025 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004026
Radek Krejcib2541a32016-12-12 16:45:57 +01004027 for (j = 0; j < mod->deviation_size; ++j) {
4028 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
4029 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
4030 /* some other module deviation targets the inspected module, flag remains */
Michal Vasko89563fc2016-07-28 16:19:35 +02004031 break;
4032 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004033 }
Michal Vasko89563fc2016-07-28 16:19:35 +02004034
Radek Krejcib2541a32016-12-12 16:45:57 +01004035 if (j < mod->deviation_size) {
4036 break;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004037 }
4038 }
4039
Radek Krejcib2541a32016-12-12 16:45:57 +01004040 if (!mod) {
4041 target_mod->deviated = 0;
4042 }
4043}
4044
4045static void
4046remove_aug(struct lys_node_augment *augment)
4047{
4048 struct lys_node *last, *elem;
4049
4050 if (!augment->target) {
4051 /* skip not resolved augments */
4052 return;
4053 }
4054
4055 elem = augment->child;
4056 if (elem) {
4057 LY_TREE_FOR(elem, last) {
4058 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
4059 break;
4060 }
4061 }
4062 /* elem is first augment child, last is the last child */
4063
4064 /* parent child ptr */
4065 if (augment->target->child == elem) {
4066 augment->target->child = last->next;
4067 }
4068
4069 /* parent child next ptr */
4070 if (elem->prev->next) {
4071 elem->prev->next = last->next;
4072 }
4073
4074 /* parent child prev ptr */
4075 if (last->next) {
4076 last->next->prev = elem->prev;
4077 } else if (augment->target->child) {
4078 augment->target->child->prev = elem->prev;
4079 }
4080
4081 /* update augment children themselves */
4082 elem->prev = last;
4083 last->next = NULL;
4084 }
4085
Radek Krejci0ec51da2016-12-14 16:42:03 +01004086 /* augment->target still keeps the resolved target, but for lys_augment_free()
4087 * we have to keep information that this augment is not applied to free its data */
4088 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01004089}
4090
4091void
4092lys_sub_module_remove_devs_augs(struct lys_module *module)
4093{
4094 uint8_t u, v;
4095
4096 /* remove applied deviations */
4097 for (u = 0; u < module->deviation_size; ++u) {
4098 remove_dev(&module->deviation[u], module);
4099 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004100 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01004101 for (u = 0; u < module->augment_size; ++u) {
4102 remove_aug(&module->augment[u]);
4103 }
4104
4105 /* remove deviation and augments defined in submodules */
4106 for (v = 0; v < module->inc_size; ++v) {
4107 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
4108 remove_dev(&module->inc[v].submodule->deviation[u], module);
Radek Krejcidbc15262016-06-16 14:58:29 +02004109 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004110
Radek Krejcib2541a32016-12-12 16:45:57 +01004111 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4112 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004113 }
4114 }
4115}
4116
Radek Krejci27fe55e2016-09-13 17:13:35 +02004117static int
4118lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
4119{
4120 struct lys_node *root, *next, *node;
4121 uint8_t i;
4122
4123 for (i = 0; i < module->augment_size; i++) {
4124 /* apply augment */
Michal Vaskoa1911b92016-09-19 14:57:34 +02004125 if (!module->augment[i].target
4126 && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004127 return -1;
4128 }
4129 }
4130 LY_TREE_FOR(module->data, root) {
4131 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
4132 LY_TREE_DFS_BEGIN(root, next, node) {
4133 if (node->nodetype == LYS_GROUPING) {
4134 goto nextsibling;
4135 }
4136 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4137 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
4138 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
4139 UNRES_TYPE_LEAFREF, node) == -1) {
4140 return EXIT_FAILURE;
4141 }
4142 }
4143 }
4144
4145 /* modified LY_TREE_DFS_END */
4146 next = node->child;
4147 /* child exception for leafs, leaflists and anyxml without children */
4148 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4149 next = NULL;
4150 }
4151 if (!next) {
4152nextsibling:
4153 /* no children */
4154 if (node == root) {
4155 /* we are done, root has no children */
4156 break;
4157 }
4158 /* try siblings */
4159 next = node->next;
4160 }
4161 while (!next) {
4162 /* parent is already processed, go to its sibling */
4163 node = lys_parent(node);
4164 /* no siblings, go back through parents */
4165 if (lys_parent(node) == lys_parent(root)) {
4166 /* we are done, no next element to process */
4167 break;
4168 }
4169 next = node->next;
4170 }
4171 }
4172 }
4173
4174 return EXIT_SUCCESS;
4175}
4176
4177API int
4178lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02004179{
4180 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004181 struct unres_schema *unres;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004182 int i, j, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02004183
Radek Krejci27fe55e2016-09-13 17:13:35 +02004184 if (!module) {
4185 ly_errno = LY_EINVAL;
4186 return EXIT_FAILURE;
4187 }
4188
4189 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004190
4191 if (module->disabled) {
4192 disabled = 1;
4193 lys_set_enabled(module);
4194 }
4195
Michal Vasko26055752016-05-03 11:36:31 +02004196 if (module->implemented) {
4197 return EXIT_SUCCESS;
4198 }
4199
4200 ctx = module->ctx;
4201
4202 for (i = 0; i < ctx->models.used; ++i) {
4203 if (module == ctx->models.list[i]) {
4204 continue;
4205 }
4206
4207 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
4208 LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004209 if (disabled) {
4210 /* set it back disabled */
4211 lys_set_disabled(module);
4212 }
Michal Vasko26055752016-05-03 11:36:31 +02004213 return EXIT_FAILURE;
4214 }
4215 }
4216
Radek Krejci27fe55e2016-09-13 17:13:35 +02004217 unres = calloc(1, sizeof *unres);
4218 if (!unres) {
4219 LOGMEM;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004220 if (disabled) {
4221 /* set it back disabled */
4222 lys_set_disabled(module);
4223 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004224 return EXIT_FAILURE;
4225 }
4226 /* recursively make the module implemented */
4227 ((struct lys_module *)module)->implemented = 1;
4228 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
4229 goto error;
4230 }
4231 /* process augments in submodules */
4232 for (i = 0; i < module->inc_size; ++i) {
4233 if (!module->inc[i].submodule) {
4234 continue;
4235 }
4236 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
4237 /* apply augment */
Radek Krejcic8c22532016-11-09 15:11:24 +01004238 if (!module->inc[i].submodule->augment[j].target
4239 && (unres_schema_add_node((struct lys_module *)module->inc[j].submodule, unres,
4240 &module->inc[i].submodule->augment[j], UNRES_AUGMENT, NULL) == -1)) {
Radek Krejci0ec51da2016-12-14 16:42:03 +01004241
Radek Krejci27fe55e2016-09-13 17:13:35 +02004242 goto error;
4243 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004244 }
4245 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004246 /* try again resolve augments in other modules possibly augmenting this one,
4247 * since we have just enabled it
4248 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004249 /* resolve rest of unres items */
4250 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
4251 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004252 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004253 unres_schema_free((struct lys_module *)module, &unres);
Michal Vasko26055752016-05-03 11:36:31 +02004254
4255 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004256
4257error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004258
4259 if (disabled) {
4260 /* set it back disabled */
4261 lys_set_disabled(module);
4262 }
4263
Radek Krejci27fe55e2016-09-13 17:13:35 +02004264 ((struct lys_module *)module)->implemented = 0;
4265 unres_schema_free((struct lys_module *)module, &unres);
4266 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004267}
4268
4269void
4270lys_submodule_module_data_free(struct lys_submodule *submodule)
4271{
4272 struct lys_node *next, *elem;
4273
4274 /* remove parsed data */
4275 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
4276 if (elem->module == (struct lys_module *)submodule) {
4277 lys_node_free(elem, NULL, 0);
4278 }
4279 }
4280}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02004281
4282int
4283lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf)
4284{
4285 uint8_t i;
4286
4287 for (i = 0; i < list->keys_size; i++) {
4288 if (list->keys[i] == leaf) {
4289 return i + 1;
4290 }
4291 }
4292
4293 return 0;
4294}
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004295
4296API char *
4297lys_path(const struct lys_node *node)
4298{
4299 char *buf_backup = NULL, *buf = ly_buf(), *result = NULL;
4300 uint16_t index = LY_BUF_SIZE - 1;
4301
4302 if (!node) {
4303 LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__);
4304 return NULL;
4305 }
4306
4307 /* backup the shared internal buffer */
4308 if (ly_buf_used && buf[0]) {
4309 buf_backup = strndup(buf, LY_BUF_SIZE - 1);
4310 }
4311 ly_buf_used++;
4312
4313 /* build the path */
4314 buf[index] = '\0';
Michal Vasko5efa25c2017-01-10 11:34:30 +01004315 ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index, 0);
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004316 result = strdup(&buf[index]);
4317
4318 /* restore the shared internal buffer */
4319 if (buf_backup) {
4320 strcpy(buf, buf_backup);
4321 free(buf_backup);
4322 }
4323 ly_buf_used--;
4324
4325 return result;
4326}
Radek Krejci9ad23f42016-10-31 15:46:52 +01004327