blob: 109e112a915405bab56970a6e9004540c0835f8d [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 *
Michal Vasko53b7da02018-02-13 15:28:42 +01006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejcida04f4a2015-05-21 12:54:09 +02007 *
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
Michal Vaskob0bbf5f2018-02-16 09:35:59 +010017#ifdef __APPLE__
18# include <sys/param.h>
19#endif
Radek Krejci812b10a2015-05-28 16:48:25 +020020#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020021#include <ctype.h>
Radek Krejcib051f722016-02-25 15:12:21 +010022#include <limits.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <stdlib.h>
24#include <sys/mman.h>
Michal Vasko662610a2015-12-07 11:25:45 +010025#include <sys/types.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020026#include <sys/stat.h>
Michal Vasko662610a2015-12-07 11:25:45 +010027#include <fcntl.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020028#include <string.h>
Michal Vasko662610a2015-12-07 11:25:45 +010029#include <unistd.h>
30#include <errno.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020031
32#include "common.h"
33#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020034#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020035#include "resolve.h"
Michal Vasko88c29542015-11-27 14:57:53 +010036#include "xml.h"
Michal Vasko5b3492c2016-07-20 09:37:40 +020037#include "xpath.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020038#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020039#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020040#include "validation.h"
Pavol Vicanf7cc2852016-03-22 23:27:35 +010041#include "parser_yang.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020042
Radek Krejci8d6b7422017-02-03 14:42:13 +010043static int lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci5138e9f2017-04-12 13:10:46 +020044 int in_grp, int shallow, struct unres_schema *unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +020045
Radek Krejci65aca412018-01-24 11:23:06 +010046API const struct lys_node_list *
47lys_is_key(const struct lys_node_leaf *node, uint8_t *index)
48{
49 struct lys_node *parent = (struct lys_node *)node;
50 struct lys_node_list *list;
51 uint8_t i;
52
53 if (!node || node->nodetype != LYS_LEAF) {
54 return NULL;
55 }
56
57 do {
58 parent = lys_parent(parent);
59 } while (parent && parent->nodetype == LYS_USES);
60
61 if (!parent || parent->nodetype != LYS_LIST) {
62 return NULL;
63 }
64
65 list = (struct lys_node_list*)parent;
66 for (i = 0; i < list->keys_size; i++) {
67 if (list->keys[i] == node) {
68 if (index) {
69 (*index) = i;
70 }
71 return list;
72 }
73 }
74 return NULL;
75}
76
Radek Krejci9ff0a922016-07-14 13:08:05 +020077API const struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +010078lys_is_disabled(const struct lys_node *node, int recursive)
Radek Krejci48061fb2015-08-05 15:41:07 +020079{
Radek Krejci9ff0a922016-07-14 13:08:05 +020080 int i;
Radek Krejci48061fb2015-08-05 15:41:07 +020081
Radek Krejci27fe55e2016-09-13 17:13:35 +020082 if (!node) {
83 return NULL;
84 }
85
Radek Krejci48061fb2015-08-05 15:41:07 +020086check:
87 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
88 /* input/output does not have if-feature, so skip them */
89
90 /* check local if-features */
Michal Vaskoc5c26b02016-06-29 11:10:29 +020091 for (i = 0; i < node->iffeature_size; i++) {
Radek Krejci69b8d922016-07-27 13:13:41 +020092 if (!resolve_iffeature(&node->iffeature[i])) {
Radek Krejci9ff0a922016-07-14 13:08:05 +020093 return node;
Radek Krejci48061fb2015-08-05 15:41:07 +020094 }
95 }
96 }
97
98 if (!recursive) {
99 return NULL;
100 }
101
102 /* go through parents */
103 if (node->nodetype == LYS_AUGMENT) {
104 /* go to parent actually means go to the target node */
105 node = ((struct lys_node_augment *)node)->target;
Michal Vasko17549192017-03-13 10:19:33 +0100106 if (!node) {
107 /* unresolved augment, let's say it's enabled */
108 return NULL;
109 }
Radek Krejci48061fb2015-08-05 15:41:07 +0200110 } else if (node->parent) {
111 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +0200112 } else {
113 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +0200114 }
115
Radek Krejci074bf852015-08-19 14:22:16 +0200116 if (recursive == 2) {
117 /* continue only if the node cannot have a data instance */
118 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
119 return NULL;
120 }
121 }
122 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +0200123}
124
Michal Vasko1dca6882015-10-22 14:29:42 +0200125int
Michal Vasko36cbaa42015-12-14 13:15:48 +0100126lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name,
127 int nam_len, LYS_NODE type, const struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +0200128{
Radek Krejcic071c542016-01-27 14:57:51 +0100129 const struct lys_node *node, *parent = NULL;
130 const struct lys_module *mod = NULL;
Michal Vasko36cbaa42015-12-14 13:15:48 +0100131 const char *node_mod_name;
Michal Vasko1dca6882015-10-22 14:29:42 +0200132
Michal Vasko36cbaa42015-12-14 13:15:48 +0100133 assert(siblings && mod_name && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +0200134 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +0200135
Michal Vasko36cbaa42015-12-14 13:15:48 +0100136 /* fill the lengths in case the caller is so indifferent */
137 if (!mod_name_len) {
138 mod_name_len = strlen(mod_name);
139 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200140 if (!nam_len) {
141 nam_len = strlen(name);
142 }
143
Michal Vasko9e635ac2016-10-17 11:44:09 +0200144 while (siblings && (siblings->nodetype == LYS_USES)) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200145 siblings = siblings->child;
146 }
Michal Vasko9e635ac2016-10-17 11:44:09 +0200147 if (!siblings) {
148 /* unresolved uses */
149 return EXIT_FAILURE;
150 }
151
Michal Vasko680f8b42016-10-17 10:27:37 +0200152 if (siblings->nodetype == LYS_GROUPING) {
153 for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev);
154 if (node->nodetype == LYS_GROUPING) {
155 /* we went through all the siblings, only groupings there - no valid sibling */
156 return EXIT_FAILURE;
157 }
158 /* update siblings to be valid */
159 siblings = node;
160 }
161
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200162 /* set parent correctly */
Radek Krejcic071c542016-01-27 14:57:51 +0100163 parent = lys_parent(siblings);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200164
Michal Vasko680f8b42016-10-17 10:27:37 +0200165 /* go up all uses */
166 while (parent && (parent->nodetype == LYS_USES)) {
167 parent = lys_parent(parent);
Michal Vasko4cf7dba2016-10-14 09:42:01 +0200168 }
169
Radek Krejcic071c542016-01-27 14:57:51 +0100170 if (!parent) {
Michal Vasko680f8b42016-10-17 10:27:37 +0200171 /* handle situation when there is a top-level uses referencing a foreign grouping */
172 for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node));
173 mod = lys_node_module(node);
Michal Vasko1dca6882015-10-22 14:29:42 +0200174 }
175
Radek Krejcic071c542016-01-27 14:57:51 +0100176 /* try to find the node */
177 node = NULL;
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100178 while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) {
Radek Krejcic071c542016-01-27 14:57:51 +0100179 if (!type || (node->nodetype & type)) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100180 /* module name comparison */
181 node_mod_name = lys_node_module(node)->name;
Michal Vaskob42b6972016-06-06 14:21:30 +0200182 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 +0100183 continue;
184 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200185
Radek Krejcic071c542016-01-27 14:57:51 +0100186 /* direct name check */
Michal Vaskob42b6972016-06-06 14:21:30 +0200187 if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
Radek Krejcic071c542016-01-27 14:57:51 +0100188 if (ret) {
189 *ret = node;
Michal Vasko1dca6882015-10-22 14:29:42 +0200190 }
Radek Krejcic071c542016-01-27 14:57:51 +0100191 return EXIT_SUCCESS;
Michal Vasko1dca6882015-10-22 14:29:42 +0200192 }
193 }
Michal Vasko1dca6882015-10-22 14:29:42 +0200194 }
195
196 return EXIT_FAILURE;
197}
198
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200199int
Michal Vaskobb520442017-05-23 10:55:18 +0200200lys_getnext_data(const struct lys_module *mod, const struct lys_node *parent, const char *name, int nam_len,
201 LYS_NODE type, const struct lys_node **ret)
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200202{
Michal Vaskobb520442017-05-23 10:55:18 +0200203 const struct lys_node *node;
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200204
Michal Vaskobb520442017-05-23 10:55:18 +0200205 assert((mod || parent) && name);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200206 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
207
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200208 if (!mod) {
Michal Vaskobb520442017-05-23 10:55:18 +0200209 mod = lys_node_module(parent);
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200210 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200211
Michal Vasko4f0dad02016-02-15 14:08:23 +0100212 /* try to find the node */
213 node = NULL;
Michal Vasko24476fa2017-03-08 12:33:48 +0100214 while ((node = lys_getnext(node, parent, mod, 0))) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100215 if (!type || (node->nodetype & type)) {
216 /* module check */
Radek Krejcic4283442016-04-22 09:19:27 +0200217 if (lys_node_module(node) != lys_main_module(mod)) {
Radek Krejcic071c542016-01-27 14:57:51 +0100218 continue;
219 }
220
Michal Vasko4f0dad02016-02-15 14:08:23 +0100221 /* direct name check */
Michal Vasko0f99d3e2017-01-10 10:50:40 +0100222 if (!strncmp(node->name, name, nam_len) && !node->name[nam_len]) {
Michal Vasko4f0dad02016-02-15 14:08:23 +0100223 if (ret) {
224 *ret = node;
225 }
226 return EXIT_SUCCESS;
227 }
Radek Krejcic071c542016-01-27 14:57:51 +0100228 }
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200229 }
230
231 return EXIT_FAILURE;
232}
233
Michal Vasko1e62a092015-12-01 12:27:20 +0100234API const struct lys_node *
235lys_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 +0200236{
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100237 const struct lys_node *next, *aug_parent;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100238 struct lys_node **snode;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200239
Michal Vasko75702cd2018-02-12 11:27:09 +0100240 if ((!parent && !module) || (module && module->type) || (parent && (parent->nodetype == LYS_USES) && !(options & LYS_GETNEXT_PARENTUSES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100241 LOGARG;
Michal Vasko24476fa2017-03-08 12:33:48 +0100242 return NULL;
243 }
244
Radek Krejci8bc87f62015-09-02 16:19:05 +0200245 if (!last) {
246 /* first call */
247
248 /* get know where to start */
249 if (parent) {
250 /* schema subtree */
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100251 snode = lys_child(parent, LYS_UNKNOWN);
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100252 /* do not return anything if the augment does not have any children */
Radek Krejcibb08db32017-07-03 11:29:17 +0200253 if (!snode || !(*snode) || ((parent->nodetype == LYS_AUGMENT) && ((*snode)->parent != parent))) {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +0100254 return NULL;
255 }
256 next = last = *snode;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200257 } else {
258 /* top level data */
Michal Vaskocb45f472018-02-12 10:47:42 +0100259 if (!(options & LYS_GETNEXT_NOSTATECHECK) && (module->disabled || !module->implemented)) {
260 /* nothing to return from a disabled/imported module */
261 return NULL;
262 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200263 next = last = module->data;
264 }
Radek Krejci972724f2016-08-12 15:24:40 +0200265 } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) {
266 /* continue with uses content */
267 next = last->child;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200268 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200269 /* continue after the last returned value */
270 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200271 }
272
273repeat:
Michal Vasko5a9c24b2017-03-13 09:25:58 +0100274 if (parent && (parent->nodetype == LYS_AUGMENT) && next) {
275 /* do not return anything outside the parent augment */
276 aug_parent = next->parent;
277 do {
278 while (aug_parent && (aug_parent->nodetype != LYS_AUGMENT)) {
279 aug_parent = aug_parent->parent;
280 }
281 if (aug_parent) {
282 if (aug_parent == parent) {
283 break;
284 }
285 aug_parent = ((struct lys_node_augment *)aug_parent)->target;
286 }
287
288 } while (aug_parent);
289 if (!aug_parent) {
290 return NULL;
291 }
292 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200293 while (next && (next->nodetype == LYS_GROUPING)) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200294 if (options & LYS_GETNEXT_WITHGROUPING) {
295 return next;
296 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200297 next = next->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200298 }
299
Radek Krejci972724f2016-08-12 15:24:40 +0200300 if (!next) { /* cover case when parent is augment */
301 if (!last || last->parent == parent || lys_parent(last) == parent) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200302 /* no next element */
303 return NULL;
304 }
Michal Vasko7c386e72015-10-07 15:13:33 +0200305 last = lys_parent(last);
Radek Krejci8bc87f62015-09-02 16:19:05 +0200306 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200307 goto repeat;
Radek Krejci972724f2016-08-12 15:24:40 +0200308 } else {
309 last = next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200310 }
311
Michal Vaskocb45f472018-02-12 10:47:42 +0100312 if (!(options & LYS_GETNEXT_NOSTATECHECK) && lys_is_disabled(next, 0)) {
313 next = next->next;
314 goto repeat;
315 }
316
Radek Krejci7f40ce32015-08-12 20:38:46 +0200317 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200318 case LYS_INPUT:
319 case LYS_OUTPUT:
320 if (options & LYS_GETNEXT_WITHINOUT) {
321 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200322 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200323 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200324 } else {
325 next = next->next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200326 }
Radek Krejci972724f2016-08-12 15:24:40 +0200327 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200328
Michal Vaskoa5835e92015-10-20 15:07:39 +0200329 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200330 if (options & LYS_GETNEXT_WITHCASE) {
331 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200332 } else if (next->child) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200333 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200334 } else {
335 next = next->next;
Michal Vasko1dca6882015-10-22 14:29:42 +0200336 }
Radek Krejci972724f2016-08-12 15:24:40 +0200337 goto repeat;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200338
Michal Vasko1dca6882015-10-22 14:29:42 +0200339 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200340 /* go into */
Radek Krejci972724f2016-08-12 15:24:40 +0200341 if (options & LYS_GETNEXT_WITHUSES) {
342 return next;
343 } else if (next->child) {
344 next = next->child;
345 } else {
346 next = next->next;
347 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200348 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200349
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200350 case LYS_RPC:
Michal Vaskob1b19442016-07-13 12:26:01 +0200351 case LYS_ACTION:
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200352 case LYS_NOTIF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200353 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200354 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200355 case LYS_ANYDATA:
Radek Krejci14a11a62015-08-17 17:27:38 +0200356 case LYS_LIST:
357 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200358 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200359
Radek Krejci972724f2016-08-12 15:24:40 +0200360 case LYS_CONTAINER:
361 if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) {
362 if (next->child) {
363 /* go into */
364 next = next->child;
365 } else {
366 next = next->next;
367 }
368 goto repeat;
369 } else {
370 return next;
371 }
372
Radek Krejci8bc87f62015-09-02 16:19:05 +0200373 case LYS_CHOICE:
374 if (options & LYS_GETNEXT_WITHCHOICE) {
375 return next;
Radek Krejci972724f2016-08-12 15:24:40 +0200376 } else if (next->child) {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200377 /* go into */
378 next = next->child;
Radek Krejci972724f2016-08-12 15:24:40 +0200379 } else {
380 next = next->next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200381 }
Radek Krejci972724f2016-08-12 15:24:40 +0200382 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200383
Radek Krejci7f40ce32015-08-12 20:38:46 +0200384 default:
385 /* we should not be here */
386 return NULL;
387 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200388}
389
Radek Krejcibf285832017-01-26 16:05:41 +0100390void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200391lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200392{
Radek Krejcif95b6292017-02-13 15:57:37 +0100393 struct lys_node *parent, *first, **pp;
Radek Krejcic071c542016-01-27 14:57:51 +0100394 struct lys_module *main_module;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200395
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200396 if (!node) {
397 return;
398 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200399
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200400 /* unlink from data model if necessary */
401 if (node->module) {
Radek Krejcic071c542016-01-27 14:57:51 +0100402 /* get main module with data tree */
Michal Vasko4f0dad02016-02-15 14:08:23 +0100403 main_module = lys_node_module(node);
Radek Krejcic071c542016-01-27 14:57:51 +0100404 if (main_module->data == node) {
405 main_module->data = node->next;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200406 }
407 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200408
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200409 /* store pointers to important nodes */
410 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200411 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200412 /* handle augments - first, unlink it from the augment parent ... */
413 if (parent->child == node) {
Radek Krejcic9d78692017-08-24 17:17:18 +0200414 parent->child = (node->next && node->next->parent == parent) ? node->next : NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200415 }
Radek Krejcifec2e142017-01-05 15:19:03 +0100416
417 if (parent->flags & LYS_NOTAPPLIED) {
418 /* data are not connected in the target, so we cannot continue with the target as a parent */
419 parent = NULL;
420 } else {
421 /* data are connected in target, so we will continue with the target as a parent */
422 parent = ((struct lys_node_augment *)parent)->target;
423 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200424 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200425
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200426 /* unlink from parent */
427 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100428 if (parent->nodetype == LYS_EXT) {
429 pp = (struct lys_node **)lys_ext_complex_get_substmt(lys_snode2stmt(node->nodetype),
430 (struct lys_ext_instance_complex*)parent, NULL);
431 if (*pp == node) {
432 *pp = node->next;
433 }
434 } else if (parent->child == node) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200435 parent->child = node->next;
436 }
437 node->parent = NULL;
438 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200439
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200440 /* unlink from siblings */
441 if (node->prev == node) {
442 /* there are no more siblings */
443 return;
444 }
445 if (node->next) {
446 node->next->prev = node->prev;
447 } else {
448 /* unlinking the last element */
449 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100450 if (parent->nodetype == LYS_EXT) {
451 first = *(struct lys_node **)pp;
452 } else {
453 first = parent->child;
454 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200455 } else {
456 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200457 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200458 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200459 }
460 }
461 first->prev = node->prev;
462 }
463 if (node->prev->next) {
464 node->prev->next = node->next;
465 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200466
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200467 /* clean up the unlinked element */
468 node->next = NULL;
469 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200470}
471
Michal Vasko563ef092015-09-04 13:17:23 +0200472struct lys_node_grp *
Radek Krejcic071c542016-01-27 14:57:51 +0100473lys_find_grouping_up(const char *name, struct lys_node *start)
Michal Vasko563ef092015-09-04 13:17:23 +0200474{
475 struct lys_node *par_iter, *iter, *stop;
Michal Vasko563ef092015-09-04 13:17:23 +0200476
477 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200478 /* top-level augment, look into module (uses augment is handled correctly below) */
479 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
Radek Krejci115fa882017-03-01 16:15:07 +0100480 par_iter = lys_main_module(par_iter->parent->module)->data;
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200481 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200482 break;
483 }
484 }
485
Michal Vasko6f929da2015-10-02 16:23:25 +0200486 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200487 continue;
488 }
489
490 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
491 if (!stop) {
492 stop = par_iter;
493 } else if (iter == stop) {
494 break;
495 }
496 if (iter->nodetype != LYS_GROUPING) {
497 continue;
498 }
499
Radek Krejcif8426a72015-10-31 23:14:03 +0100500 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200501 return (struct lys_node_grp *)iter;
502 }
503 }
504 }
505
Michal Vasko563ef092015-09-04 13:17:23 +0200506 return NULL;
507}
508
Radek Krejci10c760e2015-08-14 14:45:43 +0200509/*
510 * get next grouping in the root's subtree, in the
511 * first call, tha last is NULL
512 */
513static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200514lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200515{
Radek Krejci10c760e2015-08-14 14:45:43 +0200516 struct lys_node *last = (struct lys_node *)lastgrp;
517 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200518
Radek Krejci10c760e2015-08-14 14:45:43 +0200519 assert(root);
520
521 if (!last) {
522 last = root;
523 }
524
525 while (1) {
526 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
527 next = last->child;
528 } else {
529 next = NULL;
530 }
531 if (!next) {
532 if (last == root) {
533 /* we are done */
534 return NULL;
535 }
536
537 /* no children, go to siblings */
538 next = last->next;
539 }
540 while (!next) {
541 /* go back through parents */
Radek Krejcic071c542016-01-27 14:57:51 +0100542 if (lys_parent(last) == root) {
Radek Krejci10c760e2015-08-14 14:45:43 +0200543 /* we are done */
544 return NULL;
545 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200546 next = last->next;
Radek Krejcic071c542016-01-27 14:57:51 +0100547 last = lys_parent(last);
Radek Krejci10c760e2015-08-14 14:45:43 +0200548 }
549
550 if (next->nodetype == LYS_GROUPING) {
551 return (struct lys_node_grp *)next;
552 }
553
554 last = next;
555 }
556}
557
Michal Vasko0d343d12015-08-24 14:57:36 +0200558/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200559int
Radek Krejci07911992015-08-14 15:13:31 +0200560lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
561{
Michal Vasko563ef092015-09-04 13:17:23 +0200562 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200563 struct lys_node_grp *grp;
Radek Krejcif95b6292017-02-13 15:57:37 +0100564 int down, up;
Radek Krejci07911992015-08-14 15:13:31 +0200565
566 assert(node);
567
568 if (!parent) {
569 assert(module);
570 } else {
571 module = parent->module;
572 }
Radek Krejci115fa882017-03-01 16:15:07 +0100573 module = lys_main_module(module);
Radek Krejci07911992015-08-14 15:13:31 +0200574
575 switch (node->nodetype) {
576 case LYS_GROUPING:
577 /* 6.2.1, rule 6 */
578 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100579 start = *lys_child(parent, LYS_GROUPING);
580 if (!start) {
Radek Krejci07911992015-08-14 15:13:31 +0200581 down = 0;
582 start = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100583 } else {
584 down = 1;
585 }
586 if (parent->nodetype == LYS_EXT) {
587 up = 0;
588 } else {
589 up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200590 }
591 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100592 down = up = 1;
Radek Krejci07911992015-08-14 15:13:31 +0200593 start = module->data;
594 }
595 /* go up */
Radek Krejcif95b6292017-02-13 15:57:37 +0100596 if (up && lys_find_grouping_up(node->name, start)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100597 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name);
Michal Vasko563ef092015-09-04 13:17:23 +0200598 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200599 }
600 /* go down, because grouping can be defined after e.g. container in which is collision */
601 if (down) {
602 for (iter = start, stop = NULL; iter; iter = iter->prev) {
603 if (!stop) {
604 stop = start;
605 } else if (iter == stop) {
606 break;
607 }
608 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
609 continue;
610 }
611
612 grp = NULL;
613 while ((grp = lys_get_next_grouping(grp, iter))) {
Radek Krejci749190d2016-02-18 16:26:25 +0100614 if (ly_strequal(node->name, grp->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100615 LOGVAL(module->ctx, LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200616 return EXIT_FAILURE;
617 }
618 }
619 }
620 }
621 break;
622 case LYS_LEAF:
623 case LYS_LEAFLIST:
624 case LYS_LIST:
625 case LYS_CONTAINER:
626 case LYS_CHOICE:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200627 case LYS_ANYDATA:
Radek Krejci07911992015-08-14 15:13:31 +0200628 /* 6.2.1, rule 7 */
629 if (parent) {
630 iter = parent;
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200631 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) {
632 if (iter->nodetype == LYS_AUGMENT) {
633 if (((struct lys_node_augment *)iter)->target) {
634 /* augment is resolved, go up */
635 iter = ((struct lys_node_augment *)iter)->target;
636 continue;
637 }
638 /* augment is not resolved, this is the final parent */
639 break;
640 }
Radek Krejci07911992015-08-14 15:13:31 +0200641 iter = iter->parent;
642 }
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200643
Radek Krejci07911992015-08-14 15:13:31 +0200644 if (!iter) {
645 stop = NULL;
646 iter = module->data;
Radek Krejcif95b6292017-02-13 15:57:37 +0100647 } else if (iter->nodetype == LYS_EXT) {
648 stop = iter;
649 iter = *lys_child(iter, node->nodetype);
Radek Krejci07911992015-08-14 15:13:31 +0200650 } else {
651 stop = iter;
652 iter = iter->child;
653 }
654 } else {
655 stop = NULL;
656 iter = module->data;
657 }
658 while (iter) {
659 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
660 iter = iter->child;
661 continue;
662 }
663
Radek Krejcibf2abff2016-08-23 15:51:52 +0200664 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) {
Radek Krejci749190d2016-02-18 16:26:25 +0100665 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100666 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200667 return EXIT_FAILURE;
668 }
669 }
670
671 /* special case for choice - we must check the choice's name as
672 * well as the names of nodes under the choice
673 */
674 if (iter->nodetype == LYS_CHOICE) {
675 iter = iter->child;
676 continue;
677 }
678
679 /* go to siblings */
680 if (!iter->next) {
681 /* no sibling, go to parent's sibling */
682 do {
Michal Vasko2afc1ca2016-05-03 11:38:53 +0200683 /* for parent LYS_AUGMENT */
684 if (iter->parent == stop) {
685 iter = stop;
686 break;
687 }
688 iter = lys_parent(iter);
Radek Krejci07911992015-08-14 15:13:31 +0200689 if (iter && iter->next) {
690 break;
691 }
692 } while (iter != stop);
693
694 if (iter == stop) {
695 break;
696 }
697 }
698 iter = iter->next;
699 }
700 break;
701 case LYS_CASE:
702 /* 6.2.1, rule 8 */
Radek Krejcic071c542016-01-27 14:57:51 +0100703 if (parent) {
Radek Krejcif95b6292017-02-13 15:57:37 +0100704 start = *lys_child(parent, LYS_CASE);
Radek Krejcic071c542016-01-27 14:57:51 +0100705 } else {
706 start = module->data;
707 }
708
709 LY_TREE_FOR(start, iter) {
Radek Krejcibf2abff2016-08-23 15:51:52 +0200710 if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Radek Krejci07911992015-08-14 15:13:31 +0200711 continue;
712 }
713
Radek Krejci749190d2016-02-18 16:26:25 +0100714 if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100715 LOGVAL(module->ctx, LYE_DUPID, LY_VLOG_LYS, node, "case", node->name);
Radek Krejci07911992015-08-14 15:13:31 +0200716 return EXIT_FAILURE;
717 }
718 }
719 break;
720 default:
721 /* no check needed */
722 break;
723 }
724
725 return EXIT_SUCCESS;
726}
727
Michal Vasko0d343d12015-08-24 14:57:36 +0200728/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200729int
Radek Krejci10c760e2015-08-14 14:45:43 +0200730lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
731{
Michal Vasko53b7da02018-02-13 15:28:42 +0100732 struct ly_ctx *ctx = child->module->ctx;
Radek Krejcic9d78692017-08-24 17:17:18 +0200733 struct lys_node *iter, **pchild;
Radek Krejci41a349b2016-10-24 19:21:59 +0200734 struct lys_node_inout *in, *out, *inout;
Radek Krejci744c2d42017-03-26 13:30:00 -0500735 struct lys_node_case *c;
736 int type, shortcase = 0;
Radek Krejcif95b6292017-02-13 15:57:37 +0100737 void *p;
738 struct lyext_substmt *info = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200739
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200740 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200741
Radek Krejci10c760e2015-08-14 14:45:43 +0200742 if (parent) {
743 type = parent->nodetype;
744 module = parent->module;
745 } else {
746 assert(module);
Radek Krejcife3a1382016-11-04 10:30:11 +0100747 assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT)));
Radek Krejci10c760e2015-08-14 14:45:43 +0200748 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200749 }
750
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200751 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200752 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200753 case LYS_CONTAINER:
754 case LYS_LIST:
755 case LYS_GROUPING:
Radek Krejci96935402016-11-04 16:27:28 +0100756 case LYS_USES:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200757 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200758 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Michal Vaskob15cae22016-09-15 09:40:56 +0200759 LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100760 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200761 return EXIT_FAILURE;
762 }
763 break;
Radek Krejci76512572015-08-04 09:47:08 +0200764 case LYS_INPUT:
765 case LYS_OUTPUT:
766 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200767 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200768 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
Radek Krejci76512572015-08-04 09:47:08 +0200769 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100770 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200771 return EXIT_FAILURE;
772 }
773 break;
Radek Krejci76512572015-08-04 09:47:08 +0200774 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200775 if (!(child->nodetype &
Pavol Vican47a1b0a2016-09-20 10:18:24 +0200776 (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100777 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778 return EXIT_FAILURE;
779 }
Radek Krejci744c2d42017-03-26 13:30:00 -0500780 if (child->nodetype != LYS_CASE) {
781 shortcase = 1;
782 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200783 break;
Radek Krejci76512572015-08-04 09:47:08 +0200784 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200785 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200786 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100787 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200788 return EXIT_FAILURE;
789 }
790 break;
Radek Krejci76512572015-08-04 09:47:08 +0200791 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200792 case LYS_ACTION:
Radek Krejci76512572015-08-04 09:47:08 +0200793 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100794 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc");
Michal Vasko38d01f72015-06-15 09:41:06 +0200795 return EXIT_FAILURE;
796 }
797 break;
Radek Krejci76512572015-08-04 09:47:08 +0200798 case LYS_LEAF:
799 case LYS_LEAFLIST:
800 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +0200801 case LYS_ANYDATA:
Michal Vasko53b7da02018-02-13 15:28:42 +0100802 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
803 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "The \"%s\" statement cannot have any data substatement.",
Michal Vasko6ea3e362016-03-11 10:25:36 +0100804 strnodetype(parent->nodetype));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200805 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +0200806 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200807 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200808 (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
Michal Vaskodb017262017-01-24 13:10:04 +0100809 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100810 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype));
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200811 return EXIT_FAILURE;
812 }
Michal Vasko591e0b22015-08-13 13:53:43 +0200813 break;
814 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +0200815 /* top level */
816 if (!(child->nodetype &
Radek Krejcibf2abff2016-08-23 15:51:52 +0200817 (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
Radek Krejci10c760e2015-08-14 14:45:43 +0200818 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100819 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module");
Radek Krejci10c760e2015-08-14 14:45:43 +0200820 return EXIT_FAILURE;
821 }
Radek Krejcif95b6292017-02-13 15:57:37 +0100822 break;
823 case LYS_EXT:
824 /* plugin-defined */
825 p = lys_ext_complex_get_substmt(lys_snode2stmt(child->nodetype), (struct lys_ext_instance_complex*)parent, &info);
826 if (!p) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100827 LOGVAL(ctx, LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype),
Radek Krejcif95b6292017-02-13 15:57:37 +0100828 ((struct lys_ext_instance_complex*)parent)->def->name);
829 return EXIT_FAILURE;
830 }
831 /* TODO check cardinality */
Radek Krejcic071c542016-01-27 14:57:51 +0100832 break;
Radek Krejci10c760e2015-08-14 14:45:43 +0200833 }
834
835 /* check identifier uniqueness */
Michal Vasko15a43372017-09-25 14:12:42 +0200836 if (!(module->ctx->models.flags & LY_CTX_TRUSTED) && lys_check_id(child, parent, module)) {
Radek Krejci07911992015-08-14 15:13:31 +0200837 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200838 }
Radek Krejcib7155b52015-06-10 17:03:01 +0200839
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200840 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200841 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200842 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200843
Radek Krejcif95b6292017-02-13 15:57:37 +0100844 if ((child->nodetype & (LYS_INPUT | LYS_OUTPUT)) && parent->nodetype != LYS_EXT) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200845 /* replace the implicit input/output node */
846 if (child->nodetype == LYS_OUTPUT) {
847 inout = (struct lys_node_inout *)parent->child->next;
848 } else { /* LYS_INPUT */
849 inout = (struct lys_node_inout *)parent->child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200850 parent->child = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200851 }
852 if (inout->next) {
853 child->next = inout->next;
854 inout->next->prev = child;
855 inout->next = NULL;
Radek Krejci10c760e2015-08-14 14:45:43 +0200856 } else {
Radek Krejci41a349b2016-10-24 19:21:59 +0200857 parent->child->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200858 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200859 child->prev = inout->prev;
860 if (inout->prev->next) {
861 inout->prev->next = child;
Radek Krejci10c760e2015-08-14 14:45:43 +0200862 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200863 inout->prev = (struct lys_node *)inout;
864 child->parent = parent;
865 inout->parent = NULL;
866 lys_node_free((struct lys_node *)inout, NULL, 0);
867 } else {
Radek Krejci744c2d42017-03-26 13:30:00 -0500868 if (shortcase) {
869 /* create the implicit case to allow it to serve as a target of the augments,
870 * it won't be printed, but it will be present in the tree */
871 c = calloc(1, sizeof *c);
Michal Vasko53b7da02018-02-13 15:28:42 +0100872 LY_CHECK_ERR_RETURN(!c, LOGMEM(ctx), EXIT_FAILURE);
Radek Krejci744c2d42017-03-26 13:30:00 -0500873 c->name = lydict_insert(module->ctx, child->name, 0);
874 c->flags = LYS_IMPLICIT;
875 c->module = module;
876 c->nodetype = LYS_CASE;
877 c->prev = (struct lys_node*)c;
878 lys_node_addchild(parent, module, (struct lys_node*)c);
879 parent = (struct lys_node*)c;
880 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200881 /* connect the child correctly */
882 if (!parent) {
883 if (module->data) {
884 module->data->prev->next = child;
885 child->prev = module->data->prev;
886 module->data->prev = child;
887 } else {
888 module->data = child;
889 }
890 } else {
Radek Krejcif95b6292017-02-13 15:57:37 +0100891 pchild = lys_child(parent, child->nodetype);
892 assert(pchild);
893
Radek Krejcic9d78692017-08-24 17:17:18 +0200894 child->parent = parent;
Radek Krejcif95b6292017-02-13 15:57:37 +0100895 if (!(*pchild)) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200896 /* the only/first child of the parent */
Radek Krejcif95b6292017-02-13 15:57:37 +0100897 *pchild = child;
Radek Krejci41a349b2016-10-24 19:21:59 +0200898 iter = child;
899 } else {
900 /* add a new child at the end of parent's child list */
Radek Krejcif95b6292017-02-13 15:57:37 +0100901 iter = (*pchild)->prev;
Radek Krejci41a349b2016-10-24 19:21:59 +0200902 iter->next = child;
903 child->prev = iter;
904 }
905 while (iter->next) {
906 iter = iter->next;
907 iter->parent = parent;
908 }
Radek Krejcic9d78692017-08-24 17:17:18 +0200909 (*pchild)->prev = iter;
Radek Krejci41a349b2016-10-24 19:21:59 +0200910 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200911 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200912
Michal Vaskoe022a562016-09-27 14:24:15 +0200913 /* check config value (but ignore them in groupings and augments) */
Radek Krejcif95b6292017-02-13 15:57:37 +0100914 for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT | LYS_EXT)); iter = iter->parent);
Michal Vaskoe022a562016-09-27 14:24:15 +0200915 if (parent && !iter) {
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200916 for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent);
917 if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100918 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, child, "true", "config");
919 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Michal Vaskoe1e351e2016-08-25 12:13:39 +0200920 return EXIT_FAILURE;
921 }
922 }
923
Radek Krejci41771502016-04-14 17:52:32 +0200924 /* propagate information about status data presence */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200925 if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) &&
Radek Krejci41771502016-04-14 17:52:32 +0200926 (child->flags & LYS_INCL_STATUS)) {
Michal Vaskodcf98e62016-05-05 17:53:53 +0200927 for(iter = parent; iter; iter = lys_parent(iter)) {
Radek Krejci41771502016-04-14 17:52:32 +0200928 /* store it only into container or list - the only data inner nodes */
929 if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) {
930 if (iter->flags & LYS_INCL_STATUS) {
931 /* done, someone else set it already from here */
932 break;
933 }
934 /* set flag about including status data */
935 iter->flags |= LYS_INCL_STATUS;
936 }
937 }
938 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200939
940 /* create implicit input/output nodes to have available them as possible target for augment */
Radek Krejci60251232017-08-24 17:13:08 +0200941 if ((child->nodetype & (LYS_RPC | LYS_ACTION)) && !child->child) {
Radek Krejci41a349b2016-10-24 19:21:59 +0200942 in = calloc(1, sizeof *in);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200943 out = calloc(1, sizeof *out);
944 if (!in || !out) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100945 LOGMEM(ctx);
Radek Krejcia8d111f2017-05-31 13:57:37 +0200946 free(in);
947 free(out);
948 return EXIT_FAILURE;
949 }
Radek Krejci41a349b2016-10-24 19:21:59 +0200950 in->nodetype = LYS_INPUT;
951 in->name = lydict_insert(child->module->ctx, "input", 5);
Radek Krejci41a349b2016-10-24 19:21:59 +0200952 out->nodetype = LYS_OUTPUT;
Radek Krejcia8d111f2017-05-31 13:57:37 +0200953 out->name = lydict_insert(child->module->ctx, "output", 6);
Radek Krejci41a349b2016-10-24 19:21:59 +0200954 in->module = out->module = child->module;
955 in->parent = out->parent = child;
956 in->flags = out->flags = LYS_IMPLICIT;
957 in->next = (struct lys_node *)out;
958 in->prev = (struct lys_node *)out;
959 out->prev = (struct lys_node *)in;
960 child->child = (struct lys_node *)in;
961 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200962 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200963}
964
Michal Vasko29245662017-04-18 15:56:31 +0200965const struct lys_module *
Michal Vaskofb98dc42018-01-11 13:38:28 +0100966lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const char *revision, int internal, int implement)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200967{
Radek Krejcia1df1682016-04-11 14:56:59 +0200968 char *enlarged_data = NULL;
Radek Krejci0b5805d2015-08-13 09:38:02 +0200969 struct lys_module *mod = NULL;
Radek Krejcia1df1682016-04-11 14:56:59 +0200970 unsigned int len;
Radek Krejcif347abc2016-06-22 10:18:47 +0200971
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200972 if (!ctx || !data) {
Michal Vasko53b7da02018-02-13 15:28:42 +0100973 LOGARG;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200974 return NULL;
975 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200976
Radek Krejcia1df1682016-04-11 14:56:59 +0200977 if (!internal && format == LYS_IN_YANG) {
978 /* enlarge data by 2 bytes for flex */
979 len = strlen(data);
980 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Michal Vasko53b7da02018-02-13 15:28:42 +0100981 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(ctx), NULL);
Radek Krejcia1df1682016-04-11 14:56:59 +0200982 memcpy(enlarged_data, data, len);
983 enlarged_data[len] = enlarged_data[len + 1] = '\0';
984 data = enlarged_data;
985 }
986
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200987 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +0200988 case LYS_IN_YIN:
Michal Vaskofb98dc42018-01-11 13:38:28 +0100989 mod = yin_read_module(ctx, data, revision, implement);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200990 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +0200991 case LYS_IN_YANG:
Michal Vaskofb98dc42018-01-11 13:38:28 +0100992 mod = yang_read_module(ctx, data, 0, revision, implement);
Pavol Vicanf7cc2852016-03-22 23:27:35 +0100993 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200994 default:
Michal Vasko53b7da02018-02-13 15:28:42 +0100995 LOGERR(ctx, LY_EINVAL, "Invalid schema input format.");
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +0200996 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200998
Radek Krejcia1df1682016-04-11 14:56:59 +0200999 free(enlarged_data);
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001000
1001 /* hack for NETCONF's edit-config's operation attribute. It is not defined in the schema, but since libyang
1002 * implements YANG metadata (annotations), we need its definition. Because the ietf-netconf schema is not the
1003 * internal part of libyang, we cannot add the annotation into the schema source, but we do it here to have
1004 * the anotation definitions available in the internal schema structure. There is another hack in schema
1005 * printers to do not print this internally added annotation. */
1006 if (mod && ly_strequal(mod->name, "ietf-netconf", 0)) {
Radek Krejci5b190662017-04-13 08:56:14 +02001007 if (lyp_add_ietf_netconf_annotations(mod)) {
Michal Vasko10681e82018-01-16 14:54:16 +01001008 lys_free(mod, NULL, 1, 1);
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001009 return NULL;
1010 }
Radek Krejcia68ddeb2017-02-24 12:49:44 +01001011 }
1012
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001013 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001014}
1015
Radek Krejcia1df1682016-04-11 14:56:59 +02001016API const struct lys_module *
1017lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
1018{
Michal Vaskofb98dc42018-01-11 13:38:28 +01001019 return lys_parse_mem_(ctx, data, format, NULL, 0, 1);
Radek Krejcia1df1682016-04-11 14:56:59 +02001020}
1021
Michal Vasko5a721fd2016-02-16 12:16:48 +01001022struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001023lys_sub_parse_mem(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001024{
Michal Vasko5b998712017-01-26 10:34:06 +01001025 char *enlarged_data = NULL;
Michal Vasko5a721fd2016-02-16 12:16:48 +01001026 struct lys_submodule *submod = NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001027 unsigned int len;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001028
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001029 assert(module);
1030 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001031
Michal Vasko5b998712017-01-26 10:34:06 +01001032 if (format == LYS_IN_YANG) {
1033 /* enlarge data by 2 bytes for flex */
1034 len = strlen(data);
1035 enlarged_data = malloc((len + 2) * sizeof *enlarged_data);
Michal Vasko53b7da02018-02-13 15:28:42 +01001036 LY_CHECK_ERR_RETURN(!enlarged_data, LOGMEM(module->ctx), NULL);
Michal Vasko5b998712017-01-26 10:34:06 +01001037 memcpy(enlarged_data, data, len);
1038 enlarged_data[len] = enlarged_data[len + 1] = '\0';
1039 data = enlarged_data;
1040 }
1041
Radek Krejcic071c542016-01-27 14:57:51 +01001042 /* get the main module */
Radek Krejcic4283442016-04-22 09:19:27 +02001043 module = lys_main_module(module);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001044
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001045 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001046 case LYS_IN_YIN:
Michal Vasko5a721fd2016-02-16 12:16:48 +01001047 submod = yin_read_submodule(module, data, unres);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001048 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001049 case LYS_IN_YANG:
Pavol Vicanf7cc2852016-03-22 23:27:35 +01001050 submod = yang_read_submodule(module, data, 0, unres);
1051 break;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001052 default:
Radek Krejci90a550a2016-04-13 16:00:58 +02001053 assert(0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001054 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001055 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001056
Michal Vasko5b998712017-01-26 10:34:06 +01001057 free(enlarged_data);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001058 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001059}
1060
Michal Vasko1e62a092015-12-01 12:27:20 +01001061API const struct lys_module *
Michal Vasko662610a2015-12-07 11:25:45 +01001062lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format)
1063{
1064 int fd;
1065 const struct lys_module *ret;
Radek Krejcid80c8602016-10-25 11:56:03 +02001066 const char *rev, *dot, *filename;
1067 size_t len;
Michal Vasko662610a2015-12-07 11:25:45 +01001068
1069 if (!ctx || !path) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001070 LOGARG;
Michal Vasko662610a2015-12-07 11:25:45 +01001071 return NULL;
1072 }
1073
1074 fd = open(path, O_RDONLY);
1075 if (fd == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001076 LOGERR(ctx, LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno));
Michal Vasko662610a2015-12-07 11:25:45 +01001077 return NULL;
1078 }
1079
1080 ret = lys_parse_fd(ctx, fd, format);
1081 close(fd);
Radek Krejci23f5de52016-02-25 15:53:17 +01001082
Radek Krejcid80c8602016-10-25 11:56:03 +02001083 if (!ret) {
1084 /* error */
1085 return NULL;
1086 }
1087
1088 /* check that name and revision match filename */
1089 filename = strrchr(path, '/');
1090 if (!filename) {
1091 filename = path;
1092 } else {
1093 filename++;
1094 }
1095 rev = strchr(filename, '@');
1096 dot = strrchr(filename, '.');
1097
1098 /* name */
1099 len = strlen(ret->name);
1100 if (strncmp(filename, ret->name, len) ||
1101 ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001102 LOGWRN(ctx, "File name \"%s\" does not match module name \"%s\".", filename, ret->name);
Radek Krejcid80c8602016-10-25 11:56:03 +02001103 }
1104 if (rev) {
1105 len = dot - ++rev;
1106 if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001107 LOGWRN(ctx, "File name \"%s\" does not match module revision \"%s\".", filename,
Radek Krejcid80c8602016-10-25 11:56:03 +02001108 ret->rev_size ? ret->rev[0].date : "none");
1109 }
1110 }
1111
1112 if (!ret->filepath) {
Radek Krejci23f5de52016-02-25 15:53:17 +01001113 /* store URI */
Radek Krejcia77904e2016-02-25 16:23:45 +01001114 ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0);
Radek Krejci23f5de52016-02-25 15:53:17 +01001115 }
1116
Michal Vasko662610a2015-12-07 11:25:45 +01001117 return ret;
1118}
1119
1120API const struct lys_module *
1121lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001122{
Michal Vaskofb98dc42018-01-11 13:38:28 +01001123 return lys_parse_fd_(ctx, fd, format, NULL, 1);
1124}
1125
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001126static void
1127lys_parse_set_filename(struct ly_ctx *ctx, const char **filename, int fd)
1128{
1129 int len;
1130#ifdef __APPLE__
1131 char path[MAXPATHLEN];
1132#else
1133 char path[PATH_MAX], proc_path[32];
1134#endif
1135
1136#ifdef __APPLE__
1137 if (fcntl(fd, F_GETPATH, path) != -1) {
1138 *filename = lydict_insert(ctx, path, 0);
1139 }
1140#else
1141 /* get URI if there is /proc */
1142 sprintf(proc_path, "/proc/self/fd/%d", fd);
1143 if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
1144 *filename = lydict_insert(ctx, path, len);
1145 }
1146#endif
1147}
1148
Michal Vaskofb98dc42018-01-11 13:38:28 +01001149const struct lys_module *
1150lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement)
1151{
Michal Vasko1e62a092015-12-01 12:27:20 +01001152 const struct lys_module *module;
Radek Krejci0fb11502017-01-31 16:45:42 +01001153 size_t length;
Radek Krejci63a91a92015-07-29 13:31:04 +02001154 char *addr;
1155
1156 if (!ctx || fd < 0) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001157 LOGARG;
Radek Krejci63a91a92015-07-29 13:31:04 +02001158 return NULL;
1159 }
1160
Michal Vasko53b7da02018-02-13 15:28:42 +01001161 if (lyp_mmap(ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) {
1162 LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Pavol Vicane36ea262015-11-12 11:57:47 +01001163 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001164 } else if (!addr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001165 LOGERR(ctx, LY_EINVAL, "Empty schema file.");
Pavol Vicane36ea262015-11-12 11:57:47 +01001166 return NULL;
1167 }
Radek Krejci0fb11502017-01-31 16:45:42 +01001168
Michal Vaskofb98dc42018-01-11 13:38:28 +01001169 module = lys_parse_mem_(ctx, addr, format, revision, 1, implement);
Radek Krejci0fb11502017-01-31 16:45:42 +01001170 lyp_munmap(addr, length);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001171
Radek Krejcia77904e2016-02-25 16:23:45 +01001172 if (module && !module->filepath) {
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001173 lys_parse_set_filename(ctx, (const char **)&module->filepath, fd);
Radek Krejcib051f722016-02-25 15:12:21 +01001174 }
1175
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001176 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001177}
1178
Michal Vasko5a721fd2016-02-16 12:16:48 +01001179struct lys_submodule *
Michal Vasko5b998712017-01-26 10:34:06 +01001180lys_sub_parse_fd(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres)
Radek Krejciefaeba32015-05-27 14:30:57 +02001181{
Michal Vasko5a721fd2016-02-16 12:16:48 +01001182 struct lys_submodule *submodule;
Radek Krejci0fb11502017-01-31 16:45:42 +01001183 size_t length;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001184 char *addr;
Radek Krejciefaeba32015-05-27 14:30:57 +02001185
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001186 assert(module);
1187 assert(fd >= 0);
Radek Krejciefaeba32015-05-27 14:30:57 +02001188
Michal Vasko53b7da02018-02-13 15:28:42 +01001189 if (lyp_mmap(module->ctx, fd, format == LYS_IN_YANG ? 1 : 0, &length, (void **)&addr)) {
1190 LOGERR(module->ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__);
Michal Vasko5a721fd2016-02-16 12:16:48 +01001191 return NULL;
Radek Krejci10c216a2017-02-01 10:36:00 +01001192 } else if (!addr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001193 LOGERR(module->ctx, LY_EINVAL, "Empty submodule schema file.");
Michal Vasko2e7241e2016-02-15 16:06:34 +01001194 return NULL;
Pavol Vicane36ea262015-11-12 11:57:47 +01001195 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001196
Michal Vasko5b998712017-01-26 10:34:06 +01001197 /* get the main module */
1198 module = lys_main_module(module);
1199
1200 switch (format) {
1201 case LYS_IN_YIN:
1202 submodule = yin_read_submodule(module, addr, unres);
1203 break;
1204 case LYS_IN_YANG:
1205 submodule = yang_read_submodule(module, addr, 0, unres);
1206 break;
1207 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01001208 LOGINT(module->ctx);
Michal Vasko85d41522017-02-24 09:49:16 +01001209 return NULL;
Michal Vasko5b998712017-01-26 10:34:06 +01001210 }
1211
Radek Krejcic645a3a2017-01-31 16:59:00 +01001212 lyp_munmap(addr, length);
Michal Vaskob0bbf5f2018-02-16 09:35:59 +01001213
1214 if (submodule && !submodule->filepath) {
1215 lys_parse_set_filename(module->ctx, (const char **)&submodule->filepath, fd);
1216 }
1217
Michal Vasko5a721fd2016-02-16 12:16:48 +01001218 return submodule;
1219
Radek Krejciefaeba32015-05-27 14:30:57 +02001220}
1221
Radek Krejcibf285832017-01-26 16:05:41 +01001222int
1223lys_ext_iter(struct lys_ext_instance **ext, uint8_t ext_size, uint8_t start, LYEXT_SUBSTMT substmt)
1224{
1225 unsigned int u;
1226
1227 for (u = start; u < ext_size; u++) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001228 if (ext[u]->insubstmt == substmt) {
Radek Krejcibf285832017-01-26 16:05:41 +01001229 return u;
1230 }
1231 }
1232
1233 return -1;
1234}
1235
Radek Krejcifdc0d702017-01-23 15:58:38 +01001236/*
1237 * duplicate extension instance
1238 */
1239int
Michal Vasko17e8ba32018-02-15 10:58:56 +01001240lys_ext_dup(struct ly_ctx *ctx, struct lys_module *mod, struct lys_ext_instance **orig, uint8_t size, void *parent,
1241 LYEXT_PAR parent_type, struct lys_ext_instance ***new, int shallow, struct unres_schema *unres)
Radek Krejcifdc0d702017-01-23 15:58:38 +01001242{
1243 int i;
1244 uint8_t u = 0;
1245 struct lys_ext_instance **result;
1246 struct unres_ext *info, *info_orig;
fanchanghu8d86f6b2017-06-10 12:49:54 +08001247 size_t len;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001248
1249 assert(new);
1250
1251 if (!size) {
1252 if (orig) {
Michal Vasko17e8ba32018-02-15 10:58:56 +01001253 LOGINT(ctx);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001254 return EXIT_FAILURE;
1255 }
1256 (*new) = NULL;
1257 return EXIT_SUCCESS;
1258 }
1259
1260 (*new) = result = calloc(size, sizeof *result);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001261 LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), EXIT_FAILURE);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001262 for (u = 0; u < size; u++) {
1263 if (orig[u]) {
1264 /* resolved extension instance, just duplicate it */
Radek Krejci8de8f612017-02-16 15:03:32 +01001265 switch(orig[u]->ext_type) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001266 case LYEXT_FLAG:
1267 result[u] = malloc(sizeof(struct lys_ext_instance));
Michal Vasko17e8ba32018-02-15 10:58:56 +01001268 LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001269 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001270 case LYEXT_COMPLEX:
fanchanghu8d86f6b2017-06-10 12:49:54 +08001271 len = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->instance_size;
1272 result[u] = calloc(1, len);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001273 LY_CHECK_ERR_GOTO(!result[u], LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001274
Radek Krejcifebdad72017-02-06 11:35:51 +01001275 ((struct lys_ext_instance_complex*)result[u])->substmt = ((struct lyext_plugin_complex*)orig[u]->def->plugin)->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001276 /* TODO duplicate data in extension instance content */
fanchanghu8d86f6b2017-06-10 12:49:54 +08001277 memcpy((void*)result[u] + sizeof(**orig), (void*)orig[u] + sizeof(**orig), len - sizeof(**orig));
Radek Krejci8d6b7422017-02-03 14:42:13 +01001278 break;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001279 }
1280 /* generic part */
1281 result[u]->def = orig[u]->def;
fanchanghu8d86f6b2017-06-10 12:49:54 +08001282 result[u]->flags = LYEXT_OPT_CONTENT;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001283 result[u]->arg_value = lydict_insert(ctx, orig[u]->arg_value, 0);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001284 result[u]->parent = parent;
1285 result[u]->parent_type = parent_type;
Radek Krejcifebdad72017-02-06 11:35:51 +01001286 result[u]->insubstmt = orig[u]->insubstmt;
1287 result[u]->insubstmt_index = orig[u]->insubstmt_index;
Radek Krejci8de8f612017-02-16 15:03:32 +01001288 result[u]->ext_type = orig[u]->ext_type;
Radek Krejci7f1d47e2017-04-12 15:29:02 +02001289 result[u]->priv = NULL;
1290 result[u]->nodetype = LYS_EXT;
1291 result[u]->module = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001292
1293 /* extensions */
Radek Krejcifdc0d702017-01-23 15:58:38 +01001294 result[u]->ext_size = orig[u]->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001295 if (lys_ext_dup(ctx, mod, orig[u]->ext, orig[u]->ext_size, result[u],
Radek Krejci5138e9f2017-04-12 13:10:46 +02001296 LYEXT_PAR_EXTINST, &result[u]->ext, shallow, unres)) {
Radek Krejcifdc0d702017-01-23 15:58:38 +01001297 goto error;
1298 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001299
1300 /* in case of shallow copy (duplication for deviation), duplicate only the link to private data
1301 * in a new copy, otherwise (grouping instantiation) do not duplicate the private data */
1302 if (shallow) {
1303 result[u]->priv = orig[u]->priv;
1304 }
Radek Krejcifdc0d702017-01-23 15:58:38 +01001305 } else {
1306 /* original extension is not yet resolved, so duplicate it in unres */
1307 i = unres_schema_find(unres, -1, &orig, UNRES_EXT);
1308 if (i == -1) {
1309 /* extension not found in unres */
Michal Vasko17e8ba32018-02-15 10:58:56 +01001310 LOGINT(ctx);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001311 goto error;
1312 }
1313 info_orig = unres->str_snode[i];
1314 info = malloc(sizeof *info);
Michal Vasko17e8ba32018-02-15 10:58:56 +01001315 LY_CHECK_ERR_GOTO(!info, LOGMEM(ctx), error);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001316 info->datatype = info_orig->datatype;
1317 if (info->datatype == LYS_IN_YIN) {
Michal Vasko17e8ba32018-02-15 10:58:56 +01001318 info->data.yin = lyxml_dup_elem(ctx, info_orig->data.yin, NULL, 1);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001319 } /* else TODO YANG */
1320 info->parent = parent;
Radek Krejci8d6b7422017-02-03 14:42:13 +01001321 info->mod = mod;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001322 info->parent_type = parent_type;
1323 info->ext_index = u;
1324 if (unres_schema_add_node(info->mod, unres, new, UNRES_EXT, (struct lys_node *)info) == -1) {
1325 goto error;
1326 }
1327 }
1328 }
1329
1330 return EXIT_SUCCESS;
1331
1332error:
1333 (*new) = NULL;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001334 lys_extension_instances_free(ctx, result, u, NULL);
Radek Krejcifdc0d702017-01-23 15:58:38 +01001335 return EXIT_FAILURE;
1336}
1337
Radek Krejci1d82ef62015-08-07 14:44:40 +02001338static struct lys_restr *
Radek Krejci5138e9f2017-04-12 13:10:46 +02001339lys_restr_dup(struct lys_module *mod, struct lys_restr *old, int size, int shallow, struct unres_schema *unres)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001340{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001341 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001342 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001343
Radek Krejci3733a802015-06-19 13:43:21 +02001344 if (!size) {
1345 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001346 }
Radek Krejci3733a802015-06-19 13:43:21 +02001347
1348 result = calloc(size, sizeof *result);
Michal Vasko53b7da02018-02-13 15:28:42 +01001349 LY_CHECK_ERR_RETURN(!result, LOGMEM(mod->ctx), NULL);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001350
Radek Krejci3733a802015-06-19 13:43:21 +02001351 for (i = 0; i < size; i++) {
Radek Krejci77f22b22017-01-17 15:23:03 +01001352 result[i].ext_size = old[i].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001353 lys_ext_dup(mod->ctx, mod, old[i].ext, old[i].ext_size, &result[i], LYEXT_PAR_RESTR, &result[i].ext, shallow, unres);
Radek Krejci8d6b7422017-02-03 14:42:13 +01001354 result[i].expr = lydict_insert(mod->ctx, old[i].expr, 0);
1355 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1356 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1357 result[i].eapptag = lydict_insert(mod->ctx, old[i].eapptag, 0);
1358 result[i].emsg = lydict_insert(mod->ctx, old[i].emsg, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001359 }
1360
1361 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001362}
1363
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001364void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001365lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr,
1366 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci0bd5db42015-06-19 13:30:07 +02001367{
1368 assert(ctx);
1369 if (!restr) {
1370 return;
1371 }
1372
Radek Krejci5138e9f2017-04-12 13:10:46 +02001373 lys_extension_instances_free(ctx, restr->ext, restr->ext_size, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001374 lydict_remove(ctx, restr->expr);
1375 lydict_remove(ctx, restr->dsc);
1376 lydict_remove(ctx, restr->ref);
1377 lydict_remove(ctx, restr->eapptag);
1378 lydict_remove(ctx, restr->emsg);
1379}
1380
Pavol Vican05810b62016-11-23 14:07:22 +01001381void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001382lys_iffeature_free(struct ly_ctx *ctx, struct lys_iffeature *iffeature, uint8_t iffeature_size,
Frank Rimplerc4db1c72017-09-12 12:56:39 +00001383 int shallow, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001384{
1385 uint8_t i;
1386
1387 for (i = 0; i < iffeature_size; ++i) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001388 lys_extension_instances_free(ctx, iffeature[i].ext, iffeature[i].ext_size, private_destructor);
Michal Vasko15a43372017-09-25 14:12:42 +02001389 if (!shallow) {
Frank Rimpler2a503f52017-09-12 15:21:18 +00001390 free(iffeature[i].expr);
1391 free(iffeature[i].features);
1392 }
Radek Krejcif1ee2e22016-08-02 16:36:48 +02001393 }
1394 free(iffeature);
1395}
1396
Michal Vaskob84f88a2015-09-24 13:16:10 +02001397static int
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001398type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001399 LY_DATA_TYPE base, int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001400{
1401 int i;
Radek Krejcidce5f972017-09-12 15:47:49 +02001402 unsigned int u;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001403
1404 switch (base) {
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001405 case LY_TYPE_BINARY:
1406 if (old->info.binary.length) {
1407 new->info.binary.length = lys_restr_dup(mod, old->info.binary.length, 1, shallow, unres);
1408 }
1409 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001410
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001411 case LY_TYPE_BITS:
1412 new->info.bits.count = old->info.bits.count;
1413 if (new->info.bits.count) {
1414 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
Michal Vasko53b7da02018-02-13 15:28:42 +01001415 LY_CHECK_ERR_RETURN(!new->info.bits.bit, LOGMEM(mod->ctx), -1);
Radek Krejcia8d111f2017-05-31 13:57:37 +02001416
Radek Krejcidce5f972017-09-12 15:47:49 +02001417 for (u = 0; u < new->info.bits.count; u++) {
1418 new->info.bits.bit[u].name = lydict_insert(mod->ctx, old->info.bits.bit[u].name, 0);
1419 new->info.bits.bit[u].dsc = lydict_insert(mod->ctx, old->info.bits.bit[u].dsc, 0);
1420 new->info.bits.bit[u].ref = lydict_insert(mod->ctx, old->info.bits.bit[u].ref, 0);
1421 new->info.bits.bit[u].flags = old->info.bits.bit[u].flags;
1422 new->info.bits.bit[u].pos = old->info.bits.bit[u].pos;
1423 new->info.bits.bit[u].ext_size = old->info.bits.bit[u].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001424 if (lys_ext_dup(mod->ctx, mod, old->info.bits.bit[u].ext, old->info.bits.bit[u].ext_size,
Radek Krejcidce5f972017-09-12 15:47:49 +02001425 &new->info.bits.bit[u], LYEXT_PAR_TYPE_BIT,
1426 &new->info.bits.bit[u].ext, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001427 return -1;
1428 }
1429 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001430 }
1431 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001432
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001433 case LY_TYPE_DEC64:
1434 new->info.dec64.dig = old->info.dec64.dig;
1435 new->info.dec64.div = old->info.dec64.div;
1436 if (old->info.dec64.range) {
1437 new->info.dec64.range = lys_restr_dup(mod, old->info.dec64.range, 1, shallow, unres);
1438 }
1439 break;
1440
1441 case LY_TYPE_ENUM:
1442 new->info.enums.count = old->info.enums.count;
1443 if (new->info.enums.count) {
1444 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
Michal Vasko53b7da02018-02-13 15:28:42 +01001445 LY_CHECK_ERR_RETURN(!new->info.enums.enm, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001446
Radek Krejcidce5f972017-09-12 15:47:49 +02001447 for (u = 0; u < new->info.enums.count; u++) {
1448 new->info.enums.enm[u].name = lydict_insert(mod->ctx, old->info.enums.enm[u].name, 0);
1449 new->info.enums.enm[u].dsc = lydict_insert(mod->ctx, old->info.enums.enm[u].dsc, 0);
1450 new->info.enums.enm[u].ref = lydict_insert(mod->ctx, old->info.enums.enm[u].ref, 0);
1451 new->info.enums.enm[u].flags = old->info.enums.enm[u].flags;
1452 new->info.enums.enm[u].value = old->info.enums.enm[u].value;
1453 new->info.enums.enm[u].ext_size = old->info.enums.enm[u].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001454 if (lys_ext_dup(mod->ctx, mod, old->info.enums.enm[u].ext, old->info.enums.enm[u].ext_size,
Radek Krejcidce5f972017-09-12 15:47:49 +02001455 &new->info.enums.enm[u], LYEXT_PAR_TYPE_ENUM,
1456 &new->info.enums.enm[u].ext, shallow, unres)) {
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001457 return -1;
1458 }
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001459 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001460 }
1461 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001462
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001463 case LY_TYPE_IDENT:
1464 new->info.ident.count = old->info.ident.count;
1465 if (old->info.ident.count) {
1466 new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref);
Michal Vasko53b7da02018-02-13 15:28:42 +01001467 LY_CHECK_ERR_RETURN(!new->info.ident.ref, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001468 memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref);
1469 } else {
1470 /* there can be several unresolved base identities, duplicate them all */
1471 i = -1;
1472 do {
1473 i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF);
1474 if (i != -1) {
1475 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001476 return -1;
1477 }
1478 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001479 --i;
1480 } while (i > -1);
1481 }
1482 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001483
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001484 case LY_TYPE_INST:
1485 new->info.inst.req = old->info.inst.req;
1486 break;
1487
1488 case LY_TYPE_INT8:
1489 case LY_TYPE_INT16:
1490 case LY_TYPE_INT32:
1491 case LY_TYPE_INT64:
1492 case LY_TYPE_UINT8:
1493 case LY_TYPE_UINT16:
1494 case LY_TYPE_UINT32:
1495 case LY_TYPE_UINT64:
1496 if (old->info.num.range) {
1497 new->info.num.range = lys_restr_dup(mod, old->info.num.range, 1, shallow, unres);
1498 }
1499 break;
1500
1501 case LY_TYPE_LEAFREF:
1502 if (old->info.lref.path) {
1503 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
1504 if (!in_grp && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) {
1505 return -1;
1506 }
1507 }
1508 break;
1509
1510 case LY_TYPE_STRING:
1511 if (old->info.str.length) {
1512 new->info.str.length = lys_restr_dup(mod, old->info.str.length, 1, shallow, unres);
1513 }
Radek Krejcib53154b2017-07-19 09:14:13 +02001514 if (old->info.str.pat_count) {
1515 new->info.str.patterns = lys_restr_dup(mod, old->info.str.patterns, old->info.str.pat_count, shallow, unres);
1516 new->info.str.pat_count = old->info.str.pat_count;
Michal Vaskofcd974b2017-08-22 10:17:49 +02001517#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02001518 if (!in_grp) {
1519 new->info.str.patterns_pcre = malloc(new->info.str.pat_count * 2 * sizeof *new->info.str.patterns_pcre);
Michal Vasko53b7da02018-02-13 15:28:42 +01001520 LY_CHECK_ERR_RETURN(!new->info.str.patterns_pcre, LOGMEM(mod->ctx), -1);
Radek Krejcia4c107d2017-10-27 14:19:26 +02001521 for (u = 0; u < new->info.str.pat_count; u++) {
Michal Vaskoa26db302018-02-14 15:22:10 +01001522 if (lyp_precompile_pattern(mod->ctx, &new->info.str.patterns[u].expr[1],
Radek Krejcia4c107d2017-10-27 14:19:26 +02001523 (pcre**)&new->info.str.patterns_pcre[2 * u],
1524 (pcre_extra**)&new->info.str.patterns_pcre[2 * u + 1])) {
Radek Krejcib53154b2017-07-19 09:14:13 +02001525 free(new->info.str.patterns_pcre);
1526 new->info.str.patterns_pcre = NULL;
1527 return -1;
1528 }
1529 }
1530 }
Michal Vaskofcd974b2017-08-22 10:17:49 +02001531#endif
Radek Krejcib53154b2017-07-19 09:14:13 +02001532 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001533 break;
1534
1535 case LY_TYPE_UNION:
1536 new->info.uni.has_ptr_type = old->info.uni.has_ptr_type;
1537 new->info.uni.count = old->info.uni.count;
1538 if (new->info.uni.count) {
1539 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
Michal Vasko53b7da02018-02-13 15:28:42 +01001540 LY_CHECK_ERR_RETURN(!new->info.uni.types, LOGMEM(mod->ctx), -1);
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001541
Radek Krejcidce5f972017-09-12 15:47:49 +02001542 for (u = 0; u < new->info.uni.count; u++) {
1543 if (lys_type_dup(mod, parent, &(new->info.uni.types[u]), &(old->info.uni.types[u]), in_grp,
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001544 shallow, unres)) {
1545 return -1;
1546 }
1547 }
1548 }
1549 break;
1550
1551 default:
1552 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
1553 break;
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001554 }
Michal Vaskob4ab1cb2017-06-30 13:14:54 +02001555
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001556 return EXIT_SUCCESS;
1557}
1558
1559struct yang_type *
Radek Krejci3a5501d2016-07-18 22:03:34 +02001560lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001561 int in_grp, int shallow, struct unres_schema *unres)
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001562{
1563 struct yang_type *new;
1564
1565 new = calloc(1, sizeof *new);
Michal Vasko53b7da02018-02-13 15:28:42 +01001566 LY_CHECK_ERR_RETURN(!new, LOGMEM(module->ctx), NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001567 new->flags = old->flags;
1568 new->base = old->base;
Pavol Vican66586292016-04-07 11:38:52 +02001569 new->name = lydict_insert(module->ctx, old->name, 0);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001570 new->type = type;
1571 if (!new->name) {
Michal Vasko53b7da02018-02-13 15:28:42 +01001572 LOGMEM(module->ctx);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001573 goto error;
1574 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02001575 if (type_dup(module, parent, type, old->type, new->base, in_grp, shallow, unres)) {
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001576 new->type->base = new->base;
Radek Krejci5138e9f2017-04-12 13:10:46 +02001577 lys_type_free(module->ctx, new->type, NULL);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001578 memset(&new->type->info, 0, sizeof new->type->info);
1579 goto error;
1580 }
1581 return new;
1582
Michal Vasko53b7da02018-02-13 15:28:42 +01001583error:
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001584 free(new);
1585 return NULL;
1586}
1587
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001588int
1589lys_copy_union_leafrefs(struct lys_module *mod, struct lys_node *parent, struct lys_type *type, struct lys_type *prev_new,
1590 struct unres_schema *unres)
1591{
1592 struct lys_type new;
Radek Krejcidce5f972017-09-12 15:47:49 +02001593 unsigned int i, top_type;
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001594 struct lys_ext_instance **ext;
1595 uint8_t ext_size;
1596 void *reloc;
1597
1598 if (!prev_new) {
1599 /* this is the "top-level" type, meaning it is a real type and no typedef directly above */
1600 top_type = 1;
1601
1602 memset(&new, 0, sizeof new);
1603
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001604 new.base = type->base;
1605 new.parent = (struct lys_tpdf *)parent;
1606
1607 prev_new = &new;
1608 } else {
1609 /* this is not top-level type, just a type of a typedef */
1610 top_type = 0;
1611 }
1612
Radek Krejci9c5cb6d2017-08-09 11:15:23 +02001613 assert(type->der);
1614 if (type->der->module) {
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001615 /* typedef, skip it, but keep the extensions */
1616 ext_size = type->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001617 if (lys_ext_dup(mod->ctx, mod, type->ext, type->ext_size, prev_new, LYEXT_PAR_TYPE, &ext, 0, unres)) {
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001618 return -1;
1619 }
1620 if (prev_new->ext) {
1621 reloc = realloc(prev_new->ext, (prev_new->ext_size + ext_size) * sizeof *prev_new->ext);
Michal Vasko53b7da02018-02-13 15:28:42 +01001622 LY_CHECK_ERR_RETURN(!reloc, LOGMEM(mod->ctx), -1);
Radek Krejci70379e22017-08-09 11:21:07 +02001623 prev_new->ext = reloc;
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001624
1625 memcpy(prev_new->ext + prev_new->ext_size, ext, ext_size * sizeof *ext);
1626 free(ext);
1627
1628 prev_new->ext_size += ext_size;
1629 } else {
1630 prev_new->ext = ext;
1631 prev_new->ext_size = ext_size;
1632 }
1633
1634 if (lys_copy_union_leafrefs(mod, parent, &type->der->type, prev_new, unres)) {
1635 return -1;
1636 }
1637 } else {
1638 /* type, just make a deep copy */
1639 switch (type->base) {
1640 case LY_TYPE_UNION:
1641 prev_new->info.uni.has_ptr_type = type->info.uni.has_ptr_type;
1642 prev_new->info.uni.count = type->info.uni.count;
1643 /* this cannot be a typedef anymore */
1644 assert(prev_new->info.uni.count);
1645
1646 prev_new->info.uni.types = calloc(prev_new->info.uni.count, sizeof *prev_new->info.uni.types);
Michal Vasko53b7da02018-02-13 15:28:42 +01001647 LY_CHECK_ERR_RETURN(!prev_new->info.uni.types, LOGMEM(mod->ctx), -1);
Michal Vaskoc5c55ca2017-06-30 13:15:18 +02001648
1649 for (i = 0; i < prev_new->info.uni.count; i++) {
1650 if (lys_copy_union_leafrefs(mod, parent, &(type->info.uni.types[i]), &(prev_new->info.uni.types[i]), unres)) {
1651 return -1;
1652 }
1653 }
1654
1655 prev_new->der = type->der;
1656 break;
1657 default:
1658 if (lys_type_dup(mod, parent, prev_new, type, 0, 0, unres)) {
1659 return -1;
1660 }
1661 break;
1662 }
1663 }
1664
1665 if (top_type) {
1666 memcpy(type, prev_new, sizeof *type);
1667 }
1668 return EXIT_SUCCESS;
1669}
1670
Radek Krejci43ce4b72017-01-04 11:02:38 +01001671API const void *
1672lys_ext_instance_substmt(const struct lys_ext_instance *ext)
1673{
1674 if (!ext) {
1675 return NULL;
1676 }
1677
Radek Krejcifebdad72017-02-06 11:35:51 +01001678 switch (ext->insubstmt) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001679 case LYEXT_SUBSTMT_SELF:
1680 case LYEXT_SUBSTMT_MODIFIER:
1681 case LYEXT_SUBSTMT_VERSION:
1682 return NULL;
1683 case LYEXT_SUBSTMT_ARGUMENT:
1684 if (ext->parent_type == LYEXT_PAR_EXT) {
1685 return ((struct lys_ext_instance*)ext->parent)->arg_value;
1686 }
1687 break;
1688 case LYEXT_SUBSTMT_BASE:
1689 if (ext->parent_type == LYEXT_PAR_TYPE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001690 return ((struct lys_type*)ext->parent)->info.ident.ref[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001691 } else if (ext->parent_type == LYEXT_PAR_IDENT) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001692 return ((struct lys_ident*)ext->parent)->base[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001693 }
1694 break;
1695 case LYEXT_SUBSTMT_BELONGSTO:
1696 if (ext->parent_type == LYEXT_PAR_MODULE && ((struct lys_module*)ext->parent)->type) {
1697 return ((struct lys_submodule*)ext->parent)->belongsto;
1698 }
1699 break;
1700 case LYEXT_SUBSTMT_CONFIG:
1701 case LYEXT_SUBSTMT_MANDATORY:
1702 if (ext->parent_type == LYEXT_PAR_NODE) {
1703 return &((struct lys_node*)ext->parent)->flags;
1704 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1705 return &((struct lys_deviate*)ext->parent)->flags;
1706 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1707 return &((struct lys_refine*)ext->parent)->flags;
1708 }
1709 break;
1710 case LYEXT_SUBSTMT_CONTACT:
1711 if (ext->parent_type == LYEXT_PAR_MODULE) {
1712 return ((struct lys_module*)ext->parent)->contact;
1713 }
1714 break;
1715 case LYEXT_SUBSTMT_DEFAULT:
1716 if (ext->parent_type == LYEXT_PAR_NODE) {
1717 switch (((struct lys_node*)ext->parent)->nodetype) {
1718 case LYS_LEAF:
1719 case LYS_LEAFLIST:
1720 /* in case of leaf, the index is supposed to be 0, so it will return the
1721 * correct pointer despite the leaf structure does not have dflt as array */
Radek Krejcifebdad72017-02-06 11:35:51 +01001722 return ((struct lys_node_leaflist*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001723 case LYS_CHOICE:
1724 return ((struct lys_node_choice*)ext->parent)->dflt;
1725 default:
1726 /* internal error */
1727 break;
1728 }
1729 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1730 return ((struct lys_tpdf*)ext->parent)->dflt;
1731 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001732 return ((struct lys_deviate*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001733 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001734 return &((struct lys_refine*)ext->parent)->dflt[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001735 }
1736 break;
1737 case LYEXT_SUBSTMT_DESCRIPTION:
1738 switch (ext->parent_type) {
1739 case LYEXT_PAR_NODE:
1740 return ((struct lys_node*)ext->parent)->dsc;
1741 case LYEXT_PAR_MODULE:
1742 return ((struct lys_module*)ext->parent)->dsc;
1743 case LYEXT_PAR_IMPORT:
1744 return ((struct lys_import*)ext->parent)->dsc;
1745 case LYEXT_PAR_INCLUDE:
1746 return ((struct lys_include*)ext->parent)->dsc;
1747 case LYEXT_PAR_EXT:
1748 return ((struct lys_ext*)ext->parent)->dsc;
1749 case LYEXT_PAR_FEATURE:
1750 return ((struct lys_feature*)ext->parent)->dsc;
1751 case LYEXT_PAR_TPDF:
1752 return ((struct lys_tpdf*)ext->parent)->dsc;
1753 case LYEXT_PAR_TYPE_BIT:
1754 return ((struct lys_type_bit*)ext->parent)->dsc;
1755 case LYEXT_PAR_TYPE_ENUM:
1756 return ((struct lys_type_enum*)ext->parent)->dsc;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001757 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001758 return ((struct lys_restr*)ext->parent)->dsc;
1759 case LYEXT_PAR_WHEN:
1760 return ((struct lys_when*)ext->parent)->dsc;
1761 case LYEXT_PAR_IDENT:
1762 return ((struct lys_ident*)ext->parent)->dsc;
1763 case LYEXT_PAR_DEVIATION:
1764 return ((struct lys_deviation*)ext->parent)->dsc;
1765 case LYEXT_PAR_REVISION:
1766 return ((struct lys_revision*)ext->parent)->dsc;
1767 case LYEXT_PAR_REFINE:
1768 return ((struct lys_refine*)ext->parent)->dsc;
1769 default:
1770 break;
1771 }
1772 break;
1773 case LYEXT_SUBSTMT_ERRTAG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001774 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001775 return ((struct lys_restr*)ext->parent)->eapptag;
1776 }
1777 break;
1778 case LYEXT_SUBSTMT_ERRMSG:
Radek Krejcifdc0d702017-01-23 15:58:38 +01001779 if (ext->parent_type == LYEXT_PAR_RESTR) {
Radek Krejci43ce4b72017-01-04 11:02:38 +01001780 return ((struct lys_restr*)ext->parent)->emsg;
1781 }
1782 break;
1783 case LYEXT_SUBSTMT_DIGITS:
1784 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_DEC64) {
1785 return &((struct lys_type*)ext->parent)->info.dec64.dig;
1786 }
1787 break;
1788 case LYEXT_SUBSTMT_KEY:
1789 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1790 return ((struct lys_node_list*)ext->parent)->keys;
1791 }
1792 break;
1793 case LYEXT_SUBSTMT_MAX:
1794 if (ext->parent_type == LYEXT_PAR_NODE) {
1795 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1796 return &((struct lys_node_list*)ext->parent)->max;
1797 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1798 return &((struct lys_node_leaflist*)ext->parent)->max;
1799 }
1800 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1801 return &((struct lys_refine*)ext->parent)->mod.list.max;
1802 }
1803 break;
1804 case LYEXT_SUBSTMT_MIN:
1805 if (ext->parent_type == LYEXT_PAR_NODE) {
1806 if (((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
1807 return &((struct lys_node_list*)ext->parent)->min;
1808 } else if (((struct lys_node*)ext->parent)->nodetype == LYS_LEAFLIST) {
1809 return &((struct lys_node_leaflist*)ext->parent)->min;
1810 }
1811 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1812 return &((struct lys_refine*)ext->parent)->mod.list.min;
1813 }
1814 break;
1815 case LYEXT_SUBSTMT_NAMESPACE:
1816 if (ext->parent_type == LYEXT_PAR_MODULE && !((struct lys_module*)ext->parent)->type) {
1817 return ((struct lys_module*)ext->parent)->ns;
1818 }
1819 break;
1820 case LYEXT_SUBSTMT_ORDEREDBY:
1821 if (ext->parent_type == LYEXT_PAR_NODE &&
1822 (((struct lys_node*)ext->parent)->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
1823 return &((struct lys_node_list*)ext->parent)->flags;
1824 }
1825 break;
1826 case LYEXT_SUBSTMT_ORGANIZATION:
1827 if (ext->parent_type == LYEXT_PAR_MODULE) {
1828 return ((struct lys_module*)ext->parent)->org;
1829 }
1830 break;
1831 case LYEXT_SUBSTMT_PATH:
1832 if (ext->parent_type == LYEXT_PAR_TYPE && ((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1833 return ((struct lys_type*)ext->parent)->info.lref.path;
1834 }
1835 break;
1836 case LYEXT_SUBSTMT_POSITION:
1837 if (ext->parent_type == LYEXT_PAR_TYPE_BIT) {
1838 return &((struct lys_type_bit*)ext->parent)->pos;
1839 }
1840 break;
1841 case LYEXT_SUBSTMT_PREFIX:
1842 if (ext->parent_type == LYEXT_PAR_MODULE) {
1843 /* covers also lys_submodule */
1844 return ((struct lys_module*)ext->parent)->prefix;
1845 } else if (ext->parent_type == LYEXT_PAR_IMPORT) {
1846 return ((struct lys_import*)ext->parent)->prefix;
1847 }
1848 break;
1849 case LYEXT_SUBSTMT_PRESENCE:
1850 if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_CONTAINER) {
1851 return ((struct lys_node_container*)ext->parent)->presence;
1852 } else if (ext->parent_type == LYEXT_PAR_REFINE) {
1853 return ((struct lys_refine*)ext->parent)->mod.presence;
1854 }
1855 break;
1856 case LYEXT_SUBSTMT_REFERENCE:
1857 switch (ext->parent_type) {
1858 case LYEXT_PAR_NODE:
1859 return ((struct lys_node*)ext->parent)->ref;
1860 case LYEXT_PAR_MODULE:
1861 return ((struct lys_module*)ext->parent)->ref;
1862 case LYEXT_PAR_IMPORT:
1863 return ((struct lys_import*)ext->parent)->ref;
1864 case LYEXT_PAR_INCLUDE:
1865 return ((struct lys_include*)ext->parent)->ref;
1866 case LYEXT_PAR_EXT:
1867 return ((struct lys_ext*)ext->parent)->ref;
1868 case LYEXT_PAR_FEATURE:
1869 return ((struct lys_feature*)ext->parent)->ref;
1870 case LYEXT_PAR_TPDF:
1871 return ((struct lys_tpdf*)ext->parent)->ref;
1872 case LYEXT_PAR_TYPE_BIT:
1873 return ((struct lys_type_bit*)ext->parent)->ref;
1874 case LYEXT_PAR_TYPE_ENUM:
1875 return ((struct lys_type_enum*)ext->parent)->ref;
Radek Krejcifdc0d702017-01-23 15:58:38 +01001876 case LYEXT_PAR_RESTR:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001877 return ((struct lys_restr*)ext->parent)->ref;
1878 case LYEXT_PAR_WHEN:
1879 return ((struct lys_when*)ext->parent)->ref;
1880 case LYEXT_PAR_IDENT:
1881 return ((struct lys_ident*)ext->parent)->ref;
1882 case LYEXT_PAR_DEVIATION:
1883 return ((struct lys_deviation*)ext->parent)->ref;
1884 case LYEXT_PAR_REVISION:
1885 return ((struct lys_revision*)ext->parent)->ref;
1886 case LYEXT_PAR_REFINE:
1887 return ((struct lys_refine*)ext->parent)->ref;
1888 default:
1889 break;
1890 }
1891 break;
Radek Krejcibe336392017-02-07 10:54:24 +01001892 case LYEXT_SUBSTMT_REQINSTANCE:
Radek Krejci43ce4b72017-01-04 11:02:38 +01001893 if (ext->parent_type == LYEXT_PAR_TYPE) {
1894 if (((struct lys_type*)ext->parent)->base == LY_TYPE_LEAFREF) {
1895 return &((struct lys_type*)ext->parent)->info.lref.req;
1896 } else if (((struct lys_type*)ext->parent)->base == LY_TYPE_INST) {
1897 return &((struct lys_type*)ext->parent)->info.inst.req;
1898 }
1899 }
1900 break;
1901 case LYEXT_SUBSTMT_REVISIONDATE:
1902 if (ext->parent_type == LYEXT_PAR_IMPORT) {
1903 return ((struct lys_import*)ext->parent)->rev;
1904 } else if (ext->parent_type == LYEXT_PAR_INCLUDE) {
1905 return ((struct lys_include*)ext->parent)->rev;
1906 }
1907 break;
1908 case LYEXT_SUBSTMT_STATUS:
1909 switch (ext->parent_type) {
1910 case LYEXT_PAR_NODE:
1911 case LYEXT_PAR_IDENT:
1912 case LYEXT_PAR_TPDF:
1913 case LYEXT_PAR_EXT:
1914 case LYEXT_PAR_FEATURE:
1915 case LYEXT_PAR_TYPE_ENUM:
1916 case LYEXT_PAR_TYPE_BIT:
1917 /* in all structures the flags member is at the same offset */
1918 return &((struct lys_node*)ext->parent)->flags;
1919 default:
1920 break;
1921 }
1922 break;
1923 case LYEXT_SUBSTMT_UNIQUE:
1924 if (ext->parent_type == LYEXT_PAR_DEVIATE) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001925 return &((struct lys_deviate*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001926 } else if (ext->parent_type == LYEXT_PAR_NODE && ((struct lys_node*)ext->parent)->nodetype == LYS_LIST) {
Radek Krejcifebdad72017-02-06 11:35:51 +01001927 return &((struct lys_node_list*)ext->parent)->unique[ext->insubstmt_index];
Radek Krejci43ce4b72017-01-04 11:02:38 +01001928 }
1929 break;
1930 case LYEXT_SUBSTMT_UNITS:
1931 if (ext->parent_type == LYEXT_PAR_NODE &&
1932 (((struct lys_node*)ext->parent)->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
1933 /* units is at the same offset in both lys_node_leaf and lys_node_leaflist */
1934 return ((struct lys_node_leaf*)ext->parent)->units;
1935 } else if (ext->parent_type == LYEXT_PAR_TPDF) {
1936 return ((struct lys_tpdf*)ext->parent)->units;
1937 } else if (ext->parent_type == LYEXT_PAR_DEVIATE) {
1938 return ((struct lys_deviate*)ext->parent)->units;
1939 }
1940 break;
1941 case LYEXT_SUBSTMT_VALUE:
1942 if (ext->parent_type == LYEXT_PAR_TYPE_ENUM) {
1943 return &((struct lys_type_enum*)ext->parent)->value;
1944 }
1945 break;
1946 case LYEXT_SUBSTMT_YINELEM:
1947 if (ext->parent_type == LYEXT_PAR_EXT) {
1948 return &((struct lys_ext*)ext->parent)->flags;
1949 }
1950 break;
1951 }
Michal Vasko53b7da02018-02-13 15:28:42 +01001952 LOGINT(ext->module->ctx);
Radek Krejci43ce4b72017-01-04 11:02:38 +01001953 return NULL;
Radek Krejcie534c132016-11-23 13:32:31 +01001954}
1955
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001956static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001957lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Radek Krejci5138e9f2017-04-12 13:10:46 +02001958 int in_grp, int shallow, struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001959{
1960 int i;
1961
Radek Krejci3733a802015-06-19 13:43:21 +02001962 new->base = old->base;
1963 new->der = old->der;
Radek Krejci3a5501d2016-07-18 22:03:34 +02001964 new->parent = (struct lys_tpdf *)parent;
Radek Krejcif0bb3602017-01-25 17:05:08 +01001965 new->ext_size = old->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01001966 if (lys_ext_dup(mod->ctx, mod, old->ext, old->ext_size, new, LYEXT_PAR_TYPE, &new->ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01001967 return -1;
Radek Krejcie534c132016-11-23 13:32:31 +01001968 }
Radek Krejci3733a802015-06-19 13:43:21 +02001969
Michal Vasko1c007172017-03-10 10:20:44 +01001970 i = unres_schema_find(unres, -1, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001971 if (i != -1) {
Michal Vasko88c29542015-11-27 14:57:53 +01001972 /* HACK (serious one) for unres */
1973 /* nothing else we can do but duplicate it immediately */
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001974 if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02001975 new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, in_grp,
1976 shallow, unres);
Pavol Vicanacb9d0d2016-04-04 13:57:23 +02001977 } else {
1978 new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1);
1979 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001980 /* all these unres additions can fail even though they did not before */
Michal Vasko1c007172017-03-10 10:20:44 +01001981 if (!new->der || (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent) == -1)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001982 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001983 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001984 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001985 }
1986
Radek Krejci5138e9f2017-04-12 13:10:46 +02001987 return type_dup(mod, parent, new, old, new->base, in_grp, shallow, unres);
Radek Krejci3733a802015-06-19 13:43:21 +02001988}
1989
1990void
Radek Krejci5138e9f2017-04-12 13:10:46 +02001991lys_type_free(struct ly_ctx *ctx, struct lys_type *type,
1992 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02001993{
Radek Krejcidce5f972017-09-12 15:47:49 +02001994 unsigned int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001995
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001996 assert(ctx);
1997 if (!type) {
1998 return;
1999 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002000
Radek Krejci5138e9f2017-04-12 13:10:46 +02002001 lys_extension_instances_free(ctx, type->ext, type->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002002
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002003 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02002004 case LY_TYPE_BINARY:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002005 lys_restr_free(ctx, type->info.binary.length, private_destructor);
Radek Krejci0bd5db42015-06-19 13:30:07 +02002006 free(type->info.binary.length);
2007 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02002008 case LY_TYPE_BITS:
2009 for (i = 0; i < type->info.bits.count; i++) {
2010 lydict_remove(ctx, type->info.bits.bit[i].name);
2011 lydict_remove(ctx, type->info.bits.bit[i].dsc);
2012 lydict_remove(ctx, type->info.bits.bit[i].ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002013 lys_iffeature_free(ctx, type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size, 0,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002014 private_destructor);
2015 lys_extension_instances_free(ctx, type->info.bits.bit[i].ext, type->info.bits.bit[i].ext_size,
2016 private_destructor);
Radek Krejci994b6f62015-06-18 16:47:27 +02002017 }
2018 free(type->info.bits.bit);
2019 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02002020
2021 case LY_TYPE_DEC64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002022 lys_restr_free(ctx, type->info.dec64.range, private_destructor);
Radek Krejcif9401c32015-06-26 16:47:36 +02002023 free(type->info.dec64.range);
2024 break;
2025
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002026 case LY_TYPE_ENUM:
2027 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002028 lydict_remove(ctx, type->info.enums.enm[i].name);
2029 lydict_remove(ctx, type->info.enums.enm[i].dsc);
2030 lydict_remove(ctx, type->info.enums.enm[i].ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002031 lys_iffeature_free(ctx, type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size, 0,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002032 private_destructor);
2033 lys_extension_instances_free(ctx, type->info.enums.enm[i].ext, type->info.enums.enm[i].ext_size,
2034 private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002035 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02002036 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002037 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02002038
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02002039 case LY_TYPE_INT8:
2040 case LY_TYPE_INT16:
2041 case LY_TYPE_INT32:
2042 case LY_TYPE_INT64:
2043 case LY_TYPE_UINT8:
2044 case LY_TYPE_UINT16:
2045 case LY_TYPE_UINT32:
2046 case LY_TYPE_UINT64:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002047 lys_restr_free(ctx, type->info.num.range, private_destructor);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02002048 free(type->info.num.range);
2049 break;
2050
Radek Krejcidc4c1412015-06-19 15:39:54 +02002051 case LY_TYPE_LEAFREF:
2052 lydict_remove(ctx, type->info.lref.path);
2053 break;
2054
Radek Krejci3733a802015-06-19 13:43:21 +02002055 case LY_TYPE_STRING:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002056 lys_restr_free(ctx, type->info.str.length, private_destructor);
Radek Krejci3733a802015-06-19 13:43:21 +02002057 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02002058 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002059 lys_restr_free(ctx, &type->info.str.patterns[i], private_destructor);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002060#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02002061 if (type->info.str.patterns_pcre) {
2062 pcre_free((pcre*)type->info.str.patterns_pcre[2 * i]);
2063 pcre_free_study((pcre_extra*)type->info.str.patterns_pcre[2 * i + 1]);
2064 }
Michal Vaskofcd974b2017-08-22 10:17:49 +02002065#endif
Radek Krejci5fbc9162015-06-19 14:11:11 +02002066 }
2067 free(type->info.str.patterns);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002068#ifdef LY_ENABLED_CACHE
Radek Krejcib53154b2017-07-19 09:14:13 +02002069 free(type->info.str.patterns_pcre);
Michal Vaskofcd974b2017-08-22 10:17:49 +02002070#endif
Radek Krejci3733a802015-06-19 13:43:21 +02002071 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02002072
Radek Krejcie4c366b2015-07-02 10:11:31 +02002073 case LY_TYPE_UNION:
2074 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002075 lys_type_free(ctx, &type->info.uni.types[i], private_destructor);
Radek Krejcie4c366b2015-07-02 10:11:31 +02002076 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02002077 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02002078 break;
2079
Michal Vaskod3282192016-09-05 11:27:57 +02002080 case LY_TYPE_IDENT:
2081 free(type->info.ident.ref);
2082 break;
2083
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002084 default:
Michal Vaskod3282192016-09-05 11:27:57 +02002085 /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002086 break;
2087 }
Radek Krejci5a065542015-05-22 15:02:07 +02002088}
2089
Radek Krejci1d82ef62015-08-07 14:44:40 +02002090static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002091lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf,
2092 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002093{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002094 assert(ctx);
2095 if (!tpdf) {
2096 return;
2097 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002098
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002099 lydict_remove(ctx, tpdf->name);
2100 lydict_remove(ctx, tpdf->dsc);
2101 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002102
Radek Krejci5138e9f2017-04-12 13:10:46 +02002103 lys_type_free(ctx, &tpdf->type, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002104
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002105 lydict_remove(ctx, tpdf->units);
2106 lydict_remove(ctx, tpdf->dflt);
Radek Krejcie534c132016-11-23 13:32:31 +01002107
Radek Krejci5138e9f2017-04-12 13:10:46 +02002108 lys_extension_instances_free(ctx, tpdf->ext, tpdf->ext_size, private_destructor);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002109}
2110
Radek Krejci1d82ef62015-08-07 14:44:40 +02002111static struct lys_when *
Radek Krejci5138e9f2017-04-12 13:10:46 +02002112lys_when_dup(struct lys_module *mod, struct lys_when *old, int shallow, struct unres_schema *unres)
Radek Krejci00768f42015-06-18 17:04:04 +02002113{
Radek Krejci76512572015-08-04 09:47:08 +02002114 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02002115
2116 if (!old) {
2117 return NULL;
2118 }
2119
2120 new = calloc(1, sizeof *new);
Michal Vasko53b7da02018-02-13 15:28:42 +01002121 LY_CHECK_ERR_RETURN(!new, LOGMEM(mod->ctx), NULL);
Radek Krejci8d6b7422017-02-03 14:42:13 +01002122 new->cond = lydict_insert(mod->ctx, old->cond, 0);
2123 new->dsc = lydict_insert(mod->ctx, old->dsc, 0);
2124 new->ref = lydict_insert(mod->ctx, old->ref, 0);
Radek Krejcif0bb3602017-01-25 17:05:08 +01002125 new->ext_size = old->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002126 lys_ext_dup(mod->ctx, mod, old->ext, old->ext_size, new, LYEXT_PAR_WHEN, &new->ext, shallow, unres);
Radek Krejci00768f42015-06-18 17:04:04 +02002127
2128 return new;
2129}
2130
Michal Vasko0308dd62015-10-07 09:14:40 +02002131void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002132lys_when_free(struct ly_ctx *ctx, struct lys_when *w,
2133 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002134{
2135 if (!w) {
2136 return;
2137 }
2138
Radek Krejci5138e9f2017-04-12 13:10:46 +02002139 lys_extension_instances_free(ctx, w->ext, w->ext_size, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002140 lydict_remove(ctx, w->cond);
2141 lydict_remove(ctx, w->dsc);
2142 lydict_remove(ctx, w->ref);
2143
2144 free(w);
2145}
2146
Radek Krejcib7f5e412015-08-13 10:15:51 +02002147static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002148lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug,
2149 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcib7f5e412015-08-13 10:15:51 +02002150{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002151 struct lys_node *next, *sub;
2152
Radek Krejcic071c542016-01-27 14:57:51 +01002153 /* children from a resolved augment are freed under the target node */
Radek Krejci0ec51da2016-12-14 16:42:03 +01002154 if (!aug->target || (aug->flags & LYS_NOTAPPLIED)) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002155 LY_TREE_FOR_SAFE(aug->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002156 lys_node_free(sub, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002157 }
Michal Vaskoc4c2e212015-09-23 11:34:41 +02002158 }
2159
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002160 lydict_remove(ctx, aug->target_name);
2161 lydict_remove(ctx, aug->dsc);
2162 lydict_remove(ctx, aug->ref);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002163
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002164 lys_iffeature_free(ctx, aug->iffeature, aug->iffeature_size, 0, private_destructor);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002165 lys_extension_instances_free(ctx, aug->ext, aug->ext_size, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002166
Radek Krejci5138e9f2017-04-12 13:10:46 +02002167 lys_when_free(ctx, aug->when, private_destructor);
Radek Krejcib7f5e412015-08-13 10:15:51 +02002168}
2169
Radek Krejci1d82ef62015-08-07 14:44:40 +02002170static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002171lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident,
2172 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci6793db02015-05-22 17:49:54 +02002173{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002174 assert(ctx);
2175 if (!ident) {
2176 return;
2177 }
Radek Krejci812b10a2015-05-28 16:48:25 +02002178
Radek Krejci018f1f52016-08-03 16:01:20 +02002179 free(ident->base);
Radek Krejci85a54be2016-10-20 12:39:56 +02002180 ly_set_free(ident->der);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002181 lydict_remove(ctx, ident->name);
2182 lydict_remove(ctx, ident->dsc);
2183 lydict_remove(ctx, ident->ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002184 lys_iffeature_free(ctx, ident->iffeature, ident->iffeature_size, 0, private_destructor);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002185 lys_extension_instances_free(ctx, ident->ext, ident->ext_size, private_destructor);
Radek Krejci6793db02015-05-22 17:49:54 +02002186
2187}
2188
Radek Krejci1d82ef62015-08-07 14:44:40 +02002189static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002190lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp,
2191 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002192{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002193 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002194
Radek Krejcid12f57b2015-08-06 10:43:39 +02002195 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002196 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002197 lys_tpdf_free(ctx, &grp->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002198 }
2199 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02002200}
2201
Radek Krejci1d82ef62015-08-07 14:44:40 +02002202static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002203lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io,
2204 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcid12f57b2015-08-06 10:43:39 +02002205{
2206 int i;
2207
2208 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
2209 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002210 lys_tpdf_free(ctx, &io->tpdf[i], private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002211 }
2212 free(io->tpdf);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002213
2214 for (i = 0; i < io->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002215 lys_restr_free(ctx, &io->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002216 }
2217 free(io->must);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002218}
2219
Radek Krejci1d82ef62015-08-07 14:44:40 +02002220static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002221lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif,
2222 void (*private_destructor)(const struct lys_node *node, void *priv))
Pavol Vican7cff97e2016-08-09 14:56:08 +02002223{
2224 int i;
2225
2226 for (i = 0; i < notif->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002227 lys_restr_free(ctx, &notif->must[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002228 }
2229 free(notif->must);
2230
2231 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002232 lys_tpdf_free(ctx, &notif->tpdf[i], private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002233 }
2234 free(notif->tpdf);
2235}
2236static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002237lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml,
2238 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci537cf382015-06-04 11:07:01 +02002239{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002240 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002241
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002242 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002243 lys_restr_free(ctx, &anyxml->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002244 }
2245 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002246
Radek Krejci5138e9f2017-04-12 13:10:46 +02002247 lys_when_free(ctx, anyxml->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002248}
2249
Radek Krejci1d82ef62015-08-07 14:44:40 +02002250static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002251lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf,
2252 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002253{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002254 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002255
Radek Krejci85a54be2016-10-20 12:39:56 +02002256 /* leafref backlinks */
2257 ly_set_free((struct ly_set *)leaf->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002258
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002259 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002260 lys_restr_free(ctx, &leaf->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002261 }
2262 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002263
Radek Krejci5138e9f2017-04-12 13:10:46 +02002264 lys_when_free(ctx, leaf->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002265
Radek Krejci5138e9f2017-04-12 13:10:46 +02002266 lys_type_free(ctx, &leaf->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002267 lydict_remove(ctx, leaf->units);
2268 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02002269}
2270
Radek Krejci1d82ef62015-08-07 14:44:40 +02002271static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002272lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist,
2273 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci5a065542015-05-22 15:02:07 +02002274{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002275 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02002276
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002277 if (llist->backlinks) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002278 /* leafref backlinks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01002279 ly_set_free(llist->backlinks);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002280 }
2281
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002282 for (i = 0; i < llist->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002283 lys_restr_free(ctx, &llist->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002284 }
2285 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002286
Pavol Vican38321d02016-08-16 14:56:02 +02002287 for (i = 0; i < llist->dflt_size; i++) {
2288 lydict_remove(ctx, llist->dflt[i]);
2289 }
2290 free(llist->dflt);
2291
Radek Krejci5138e9f2017-04-12 13:10:46 +02002292 lys_when_free(ctx, llist->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002293
Radek Krejci5138e9f2017-04-12 13:10:46 +02002294 lys_type_free(ctx, &llist->type, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002295 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02002296}
2297
Radek Krejci1d82ef62015-08-07 14:44:40 +02002298static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002299lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list,
2300 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002301{
Radek Krejci581ce772015-11-10 17:22:40 +01002302 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002303
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002304 /* handle only specific parts for LY_NODE_LIST */
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002305 lys_when_free(ctx, list->when, private_destructor);
Radek Krejci537cf382015-06-04 11:07:01 +02002306
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002307 for (i = 0; i < list->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002308 lys_restr_free(ctx, &list->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002309 }
2310 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02002311
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002312 for (i = 0; i < list->tpdf_size; i++) {
2313 lys_tpdf_free(ctx, &list->tpdf[i], private_destructor);
2314 }
2315 free(list->tpdf);
2316
2317 free(list->keys);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002318
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002319 for (i = 0; i < list->unique_size; i++) {
Michal Vaskof5e940a2016-06-07 09:39:26 +02002320 for (j = 0; j < list->unique[i].expr_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002321 lydict_remove(ctx, list->unique[i].expr[j]);
2322 }
2323 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002324 }
2325 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02002326
Michal Vaskoaaeab1d2018-02-16 09:36:46 +01002327 lydict_remove(ctx, list->keys_str);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002328}
2329
Radek Krejci1d82ef62015-08-07 14:44:40 +02002330static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002331lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont,
2332 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002333{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002334 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002335
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002336 /* handle only specific parts for LY_NODE_CONTAINER */
2337 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02002338
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002339 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002340 lys_tpdf_free(ctx, &cont->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002341 }
2342 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02002343
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002344 for (i = 0; i < cont->must_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002345 lys_restr_free(ctx, &cont->must[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002346 }
2347 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002348
Radek Krejci5138e9f2017-04-12 13:10:46 +02002349 lys_when_free(ctx, cont->when, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002350}
2351
Radek Krejci1d82ef62015-08-07 14:44:40 +02002352static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002353lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f,
2354 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci3cf9e222015-06-18 11:37:50 +02002355{
2356 lydict_remove(ctx, f->name);
2357 lydict_remove(ctx, f->dsc);
2358 lydict_remove(ctx, f->ref);
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002359 lys_iffeature_free(ctx, f->iffeature, f->iffeature_size, 0, private_destructor);
Radek Krejci9de2c042016-10-19 16:53:06 +02002360 ly_set_free(f->depfeatures);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002361 lys_extension_instances_free(ctx, f->ext, f->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002362}
2363
2364static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002365lys_extension_free(struct ly_ctx *ctx, struct lys_ext *e,
2366 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie534c132016-11-23 13:32:31 +01002367{
2368 lydict_remove(ctx, e->name);
2369 lydict_remove(ctx, e->dsc);
2370 lydict_remove(ctx, e->ref);
2371 lydict_remove(ctx, e->argument);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002372 lys_extension_instances_free(ctx, e->ext, e->ext_size, private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002373}
2374
Radek Krejci1d82ef62015-08-07 14:44:40 +02002375static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002376lys_deviation_free(struct lys_module *module, struct lys_deviation *dev,
2377 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcieb00f512015-07-01 16:44:58 +02002378{
Radek Krejci581ce772015-11-10 17:22:40 +01002379 int i, j, k;
Michal Vaskoff006c12016-02-17 11:15:19 +01002380 struct ly_ctx *ctx;
2381 struct lys_node *next, *elem;
2382
2383 ctx = module->ctx;
Radek Krejcieb00f512015-07-01 16:44:58 +02002384
2385 lydict_remove(ctx, dev->target_name);
2386 lydict_remove(ctx, dev->dsc);
2387 lydict_remove(ctx, dev->ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002388 lys_extension_instances_free(ctx, dev->ext, dev->ext_size, private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002389
Pavol Vican64d0b762016-08-25 10:44:59 +02002390 if (!dev->deviate) {
2391 return ;
2392 }
2393
Michal Vaskoff006c12016-02-17 11:15:19 +01002394 /* the module was freed, but we only need the context from orig_node, use ours */
2395 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
2396 /* it's actually a node subtree, we need to update modules on all the nodes :-/ */
2397 LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) {
2398 elem->module = module;
2399
2400 LY_TREE_DFS_END(dev->orig_node, next, elem);
2401 }
2402 lys_node_free(dev->orig_node, NULL, 0);
2403 } else {
2404 /* it's just a shallow copy, freeing one node */
2405 dev->orig_node->module = module;
2406 lys_node_free(dev->orig_node, NULL, 1);
2407 }
2408
Radek Krejcieb00f512015-07-01 16:44:58 +02002409 for (i = 0; i < dev->deviate_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002410 lys_extension_instances_free(ctx, dev->deviate[i].ext, dev->deviate[i].ext_size, private_destructor);
Radek Krejci3c4b0ea2017-01-24 16:00:47 +01002411
Radek Krejcid5a5c282016-08-15 15:38:08 +02002412 for (j = 0; j < dev->deviate[i].dflt_size; j++) {
Pavol Vican38321d02016-08-16 14:56:02 +02002413 lydict_remove(ctx, dev->deviate[i].dflt[j]);
Radek Krejcid5a5c282016-08-15 15:38:08 +02002414 }
2415 free(dev->deviate[i].dflt);
2416
Radek Krejcieb00f512015-07-01 16:44:58 +02002417 lydict_remove(ctx, dev->deviate[i].units);
2418
2419 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
2420 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002421 lys_restr_free(ctx, &dev->deviate[i].must[j], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002422 }
2423 free(dev->deviate[i].must);
2424
2425 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01002426 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
2427 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
2428 }
Michal Vasko0238ccf2016-03-07 15:02:18 +01002429 free(dev->deviate[i].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02002430 }
2431 free(dev->deviate[i].unique);
2432 }
2433 }
2434 free(dev->deviate);
2435}
2436
Radek Krejci1d82ef62015-08-07 14:44:40 +02002437static void
Radek Krejci5138e9f2017-04-12 13:10:46 +02002438lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses,
2439 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcie1fa8582015-06-08 09:46:45 +02002440{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002441 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02002442
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002443 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02002444 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002445 lydict_remove(ctx, uses->refine[i].dsc);
2446 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002447
Radek Krejcifde04bd2017-09-13 16:38:38 +02002448 lys_iffeature_free(ctx, uses->refine[i].iffeature, uses->refine[i].iffeature_size, 0, private_destructor);
2449
Michal Vaskoa275c0a2015-09-24 09:55:42 +02002450 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002451 lys_restr_free(ctx, &uses->refine[i].must[j], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002452 }
2453 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002454
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002455 for (j = 0; j < uses->refine[i].dflt_size; j++) {
Pavol Vican855ca622016-09-05 13:07:54 +02002456 lydict_remove(ctx, uses->refine[i].dflt[j]);
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002457 }
2458 free(uses->refine[i].dflt);
2459
Radek Krejci5138e9f2017-04-12 13:10:46 +02002460 lys_extension_instances_free(ctx, uses->refine[i].ext, uses->refine[i].ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002461
Pavol Vican3e7c73a2016-08-17 10:24:11 +02002462 if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002463 lydict_remove(ctx, uses->refine[i].mod.presence);
2464 }
2465 }
2466 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002467
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002468 for (i = 0; i < uses->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002469 lys_augment_free(ctx, &uses->augment[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002470 }
2471 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002472
Radek Krejci5138e9f2017-04-12 13:10:46 +02002473 lys_when_free(ctx, uses->when, private_destructor);
Radek Krejcie1fa8582015-06-08 09:46:45 +02002474}
2475
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002476void
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002477lys_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 +02002478{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002479 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02002480 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002481
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002482 if (!node) {
2483 return;
2484 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002485
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002486 assert(node->module);
2487 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02002488
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002489 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002490
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002491 /* remove private object */
Mislav Novakovicb5529e52016-02-29 11:42:43 +01002492 if (node->priv && private_destructor) {
2493 private_destructor(node, node->priv);
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002494 }
2495
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002496 /* common part */
Michal Vasko0a1aaa42016-04-19 09:48:25 +02002497 lydict_remove(ctx, node->name);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002498 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
Frank Rimplerc4db1c72017-09-12 12:56:39 +00002499 lys_iffeature_free(ctx, node->iffeature, node->iffeature_size, shallow, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002500 lydict_remove(ctx, node->dsc);
2501 lydict_remove(ctx, node->ref);
2502 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02002503
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002504 if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejci46c4cd72016-01-21 15:13:52 +01002505 LY_TREE_FOR_SAFE(node->child, next, sub) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002506 lys_node_free(sub, private_destructor, 0);
Radek Krejci46c4cd72016-01-21 15:13:52 +01002507 }
2508 }
2509
Radek Krejci5138e9f2017-04-12 13:10:46 +02002510 lys_extension_instances_free(ctx, node->ext, node->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002511
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002512 /* specific part */
2513 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002514 case LYS_CONTAINER:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002515 lys_container_free(ctx, (struct lys_node_container *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002516 break;
Radek Krejci76512572015-08-04 09:47:08 +02002517 case LYS_CHOICE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002518 lys_when_free(ctx, ((struct lys_node_choice *)node)->when, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002519 break;
Radek Krejci76512572015-08-04 09:47:08 +02002520 case LYS_LEAF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002521 lys_leaf_free(ctx, (struct lys_node_leaf *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002522 break;
Radek Krejci76512572015-08-04 09:47:08 +02002523 case LYS_LEAFLIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002524 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002525 break;
Radek Krejci76512572015-08-04 09:47:08 +02002526 case LYS_LIST:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002527 lys_list_free(ctx, (struct lys_node_list *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002528 break;
Radek Krejci76512572015-08-04 09:47:08 +02002529 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002530 case LYS_ANYDATA:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002531 lys_anydata_free(ctx, (struct lys_node_anydata *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002532 break;
Radek Krejci76512572015-08-04 09:47:08 +02002533 case LYS_USES:
Radek Krejcifa0b5e02016-02-04 13:57:03 +01002534 lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002535 break;
Radek Krejci76512572015-08-04 09:47:08 +02002536 case LYS_CASE:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002537 lys_when_free(ctx, ((struct lys_node_case *)node)->when, private_destructor);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02002538 break;
Radek Krejci76512572015-08-04 09:47:08 +02002539 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002540 /* do nothing */
2541 break;
Radek Krejci76512572015-08-04 09:47:08 +02002542 case LYS_GROUPING:
2543 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002544 case LYS_ACTION:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002545 lys_grp_free(ctx, (struct lys_node_grp *)node, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002546 break;
Pavol Vican7cff97e2016-08-09 14:56:08 +02002547 case LYS_NOTIF:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002548 lys_notif_free(ctx, (struct lys_node_notif *)node, private_destructor);
Pavol Vican7cff97e2016-08-09 14:56:08 +02002549 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02002550 case LYS_INPUT:
2551 case LYS_OUTPUT:
Radek Krejci5138e9f2017-04-12 13:10:46 +02002552 lys_inout_free(ctx, (struct lys_node_inout *)node, private_destructor);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002553 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01002554 case LYS_EXT:
Michal Vasko591e0b22015-08-13 13:53:43 +02002555 case LYS_UNKNOWN:
Michal Vasko53b7da02018-02-13 15:28:42 +01002556 LOGINT(ctx);
Michal Vasko591e0b22015-08-13 13:53:43 +02002557 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002558 }
Radek Krejci5a065542015-05-22 15:02:07 +02002559
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002560 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002561 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002562 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002563}
2564
Radek Krejci2eee5c02016-12-06 19:18:05 +01002565API struct lys_module *
2566lys_implemented_module(const struct lys_module *mod)
Radek Krejci0fa54e92016-09-14 14:01:05 +02002567{
2568 struct ly_ctx *ctx;
2569 int i;
2570
2571 if (!mod || mod->implemented) {
2572 /* invalid argument or the module itself is implemented */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002573 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002574 }
2575
2576 ctx = mod->ctx;
2577 for (i = 0; i < ctx->models.used; i++) {
2578 if (!ctx->models.list[i]->implemented) {
2579 continue;
2580 }
2581
2582 if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) {
2583 /* we have some revision of the module implemented */
2584 return ctx->models.list[i];
2585 }
2586 }
2587
2588 /* we have no revision of the module implemented, return the module itself,
2589 * it is up to the caller to set the module implemented when needed */
Radek Krejci2eee5c02016-12-06 19:18:05 +01002590 return (struct lys_module *)mod;
Radek Krejci0fa54e92016-09-14 14:01:05 +02002591}
2592
Michal Vasko13b15832015-08-19 11:04:48 +02002593/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002594static void
Michal Vaskob746fff2016-02-11 11:37:50 +01002595module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejcida04f4a2015-05-21 12:54:09 +02002596{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002597 struct ly_ctx *ctx;
Radek Krejcic071c542016-01-27 14:57:51 +01002598 struct lys_node *next, *iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002599 unsigned int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002600
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002601 assert(module->ctx);
2602 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02002603
Michal Vaskob746fff2016-02-11 11:37:50 +01002604 /* just free the import array, imported modules will stay in the context */
Radek Krejci225376f2016-02-16 17:36:22 +01002605 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa99c1632016-06-06 16:23:52 +02002606 lydict_remove(ctx, module->imp[i].prefix);
Michal Vasko8bfe3812016-07-27 13:37:52 +02002607 lydict_remove(ctx, module->imp[i].dsc);
2608 lydict_remove(ctx, module->imp[i].ref);
Radek Krejci5138e9f2017-04-12 13:10:46 +02002609 lys_extension_instances_free(ctx, module->imp[i].ext, module->imp[i].ext_size, private_destructor);
Radek Krejci225376f2016-02-16 17:36:22 +01002610 }
Radek Krejcidce51452015-06-16 15:20:08 +02002611 free(module->imp);
2612
Radek Krejcic071c542016-01-27 14:57:51 +01002613 /* submodules don't have data tree, the data nodes
2614 * are placed in the main module altogether */
2615 if (!module->type) {
2616 LY_TREE_FOR_SAFE(module->data, next, iter) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002617 lys_node_free(iter, private_destructor, 0);
Radek Krejcic071c542016-01-27 14:57:51 +01002618 }
Radek Krejci21181962015-06-30 14:11:00 +02002619 }
Radek Krejci5a065542015-05-22 15:02:07 +02002620
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002621 lydict_remove(ctx, module->dsc);
2622 lydict_remove(ctx, module->ref);
2623 lydict_remove(ctx, module->org);
2624 lydict_remove(ctx, module->contact);
Radek Krejcia77904e2016-02-25 16:23:45 +01002625 lydict_remove(ctx, module->filepath);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002626
Radek Krejcieb00f512015-07-01 16:44:58 +02002627 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002628 for (i = 0; i < module->rev_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002629 lys_extension_instances_free(ctx, module->rev[i].ext, module->rev[i].ext_size, private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002630 lydict_remove(ctx, module->rev[i].dsc);
2631 lydict_remove(ctx, module->rev[i].ref);
2632 }
2633 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002634
Radek Krejcieb00f512015-07-01 16:44:58 +02002635 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002636 for (i = 0; i < module->ident_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002637 lys_ident_free(ctx, &module->ident[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002638 }
2639 module->ident_size = 0;
2640 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002641
Radek Krejcieb00f512015-07-01 16:44:58 +02002642 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002643 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002644 lys_tpdf_free(ctx, &module->tpdf[i], private_destructor);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002645 }
2646 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002647
Radek Krejcie534c132016-11-23 13:32:31 +01002648 /* extension instances */
Radek Krejci5138e9f2017-04-12 13:10:46 +02002649 lys_extension_instances_free(ctx, module->ext, module->ext_size, private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002650
Radek Krejcieb00f512015-07-01 16:44:58 +02002651 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002652 for (i = 0; i < module->augment_size; i++) {
Michal Vasko9eb6dd02016-05-02 14:52:40 +02002653 lys_augment_free(ctx, &module->augment[i], private_destructor);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002654 }
2655 free(module->augment);
2656
Radek Krejcieb00f512015-07-01 16:44:58 +02002657 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002658 for (i = 0; i < module->features_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002659 lys_feature_free(ctx, &module->features[i], private_destructor);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002660 }
2661 free(module->features);
2662
Radek Krejcieb00f512015-07-01 16:44:58 +02002663 /* deviations */
2664 for (i = 0; i < module->deviation_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002665 lys_deviation_free(module, &module->deviation[i], private_destructor);
Radek Krejcieb00f512015-07-01 16:44:58 +02002666 }
2667 free(module->deviation);
2668
Radek Krejcie534c132016-11-23 13:32:31 +01002669 /* extensions */
2670 for (i = 0; i < module->extensions_size; i++) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002671 lys_extension_free(ctx, &module->extensions[i], private_destructor);
Radek Krejcie534c132016-11-23 13:32:31 +01002672 }
2673 free(module->extensions);
2674
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002675 lydict_remove(ctx, module->name);
Radek Krejci4f78b532016-02-17 13:43:00 +01002676 lydict_remove(ctx, module->prefix);
Radek Krejciefaeba32015-05-27 14:30:57 +02002677}
2678
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002679void
Michal Vaskob746fff2016-02-11 11:37:50 +01002680lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejciefaeba32015-05-27 14:30:57 +02002681{
Michal Vasko10681e82018-01-16 14:54:16 +01002682 int i;
2683
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002684 if (!submodule) {
2685 return;
2686 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002687
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002688 /* common part with struct ly_module */
Michal Vaskob746fff2016-02-11 11:37:50 +01002689 module_free_common((struct lys_module *)submodule, private_destructor);
Radek Krejciefaeba32015-05-27 14:30:57 +02002690
Michal Vasko10681e82018-01-16 14:54:16 +01002691 /* include */
2692 for (i = 0; i < submodule->inc_size; i++) {
2693 lydict_remove(submodule->ctx, submodule->inc[i].dsc);
2694 lydict_remove(submodule->ctx, submodule->inc[i].ref);
2695 lys_extension_instances_free(submodule->ctx, submodule->inc[i].ext, submodule->inc[i].ext_size, private_destructor);
2696 /* complete submodule free is done only from main module since
2697 * submodules propagate their includes to the main module */
2698 }
2699 free(submodule->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002700
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002701 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002702}
2703
Radek Krejcib53154b2017-07-19 09:14:13 +02002704int
2705lys_ingrouping(const struct lys_node *node)
Radek Krejci3a5501d2016-07-18 22:03:34 +02002706{
2707 const struct lys_node *iter = node;
2708 assert(node);
2709
2710 for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter));
2711 if (!iter) {
2712 return 0;
2713 } else {
2714 return 1;
2715 }
2716}
2717
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002718/*
2719 * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags
2720 */
2721static struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01002722lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002723 struct unres_schema *unres, int shallow, int finalize)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002724{
Radek Krejci7b9f5662016-11-07 16:28:37 +01002725 struct lys_node *retval = NULL, *iter, *p;
Michal Vaskofe31cc02017-03-10 15:52:31 +01002726 struct lys_module *tmp_mod;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002727 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002728 int i, j, rc;
Radek Krejci9ff0a922016-07-14 13:08:05 +02002729 unsigned int size, size1, size2;
Radek Krejcid09d1a52016-08-11 14:05:45 +02002730 struct unres_list_uniq *unique_info;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002731 uint16_t flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002732
Michal Vaskoc07187d2015-08-13 15:20:57 +02002733 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002734 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002735 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002736 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002737 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002738 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002739 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002740 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002741 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002742 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Radek Krejcibf2abff2016-08-23 15:51:52 +02002743 struct lys_node_anydata *any = NULL;
2744 struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002745 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002746 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vasko44fb6382016-06-29 11:12:27 +02002747 struct lys_node_rpc_action *rpc = NULL;
Michal Vasko44fb6382016-06-29 11:12:27 +02002748 struct lys_node_inout *io = NULL;
Radek Krejcic43151c2017-03-12 07:10:52 +01002749 struct lys_node_notif *ntf = NULL;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002750 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002751 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002752
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002753 /* we cannot just duplicate memory since the strings are stored in
2754 * dictionary and we need to update dictionary counters.
2755 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002756
Radek Krejci1d82ef62015-08-07 14:44:40 +02002757 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002758 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002759 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002760 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002761 break;
2762
Radek Krejci76512572015-08-04 09:47:08 +02002763 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002764 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002765 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002766 break;
2767
Radek Krejci76512572015-08-04 09:47:08 +02002768 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002769 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002770 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002771 break;
2772
Radek Krejci76512572015-08-04 09:47:08 +02002773 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002774 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002775 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002776 break;
2777
Radek Krejci76512572015-08-04 09:47:08 +02002778 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002779 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002780 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002781 break;
2782
Radek Krejci76512572015-08-04 09:47:08 +02002783 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02002784 case LYS_ANYDATA:
2785 any = calloc(1, sizeof *any);
2786 retval = (struct lys_node *)any;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002787 break;
2788
Radek Krejci76512572015-08-04 09:47:08 +02002789 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002790 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002791 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002792 break;
2793
Radek Krejci76512572015-08-04 09:47:08 +02002794 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002795 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002796 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002797 break;
2798
Radek Krejci76512572015-08-04 09:47:08 +02002799 case LYS_RPC:
Michal Vasko44fb6382016-06-29 11:12:27 +02002800 case LYS_ACTION:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002801 rpc = calloc(1, sizeof *rpc);
2802 retval = (struct lys_node *)rpc;
2803 break;
2804
Radek Krejci76512572015-08-04 09:47:08 +02002805 case LYS_INPUT:
2806 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002807 io = calloc(1, sizeof *io);
2808 retval = (struct lys_node *)io;
2809 break;
2810
Radek Krejci76512572015-08-04 09:47:08 +02002811 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002812 ntf = calloc(1, sizeof *ntf);
2813 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002814 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002815
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002816 default:
Michal Vasko53b7da02018-02-13 15:28:42 +01002817 LOGINT(ctx);
Michal Vasko49168a22015-08-17 16:35:41 +02002818 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002819 }
Michal Vasko53b7da02018-02-13 15:28:42 +01002820 LY_CHECK_ERR_RETURN(!retval, LOGMEM(ctx), NULL);
Michal Vasko253035f2015-12-17 16:58:13 +01002821
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002822 /*
2823 * duplicate generic part of the structure
2824 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002825 retval->name = lydict_insert(ctx, node->name, 0);
2826 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2827 retval->ref = lydict_insert(ctx, node->ref, 0);
2828 retval->flags = node->flags;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002829
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002830 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002831 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002832
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002833 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002834
Radek Krejcif0bb3602017-01-25 17:05:08 +01002835 retval->ext_size = node->ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002836 if (lys_ext_dup(ctx, module, node->ext, node->ext_size, retval, LYEXT_PAR_NODE, &retval->ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002837 goto error;
2838 }
2839
Radek Krejci06214042016-11-04 16:25:58 +01002840 if (node->iffeature_size) {
2841 retval->iffeature_size = node->iffeature_size;
2842 retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature);
Michal Vasko53b7da02018-02-13 15:28:42 +01002843 LY_CHECK_ERR_GOTO(!retval->iffeature, LOGMEM(ctx), error);
Michal Vasko253035f2015-12-17 16:58:13 +01002844 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002845
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002846 if (!shallow) {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002847 for (i = 0; i < node->iffeature_size; ++i) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002848 resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2);
2849 if (size1) {
2850 /* there is something to duplicate */
2851
2852 /* duplicate compiled expression */
2853 size = (size1 / 4) + (size1 % 4) ? 1 : 0;
2854 retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01002855 LY_CHECK_ERR_GOTO(!retval->iffeature[i].expr, LOGMEM(ctx), error);
Radek Krejci9ff0a922016-07-14 13:08:05 +02002856 memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr);
2857
2858 /* list of feature pointer must be updated to point to the resulting tree */
Radek Krejcicbb473e2016-09-16 14:48:32 +02002859 retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features);
Michal Vasko53b7da02018-02-13 15:28:42 +01002860 LY_CHECK_ERR_GOTO(!retval->iffeature[i].features, LOGMEM(ctx); free(retval->iffeature[i].expr), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002861
Radek Krejci9ff0a922016-07-14 13:08:05 +02002862 for (j = 0; (unsigned int)j < size2; j++) {
2863 rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT,
2864 &retval->iffeature[i].features[j]);
Radek Krejcicbb473e2016-09-16 14:48:32 +02002865 if (rc == EXIT_FAILURE) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02002866 /* feature is resolved in origin, so copy it
2867 * - duplication is used for instantiating groupings
2868 * and if-feature inside grouping is supposed to be
2869 * resolved inside the original grouping, so we want
2870 * to keep pointers to features from the grouping
2871 * context */
2872 retval->iffeature[i].features[j] = node->iffeature[i].features[j];
2873 } else if (rc == -1) {
2874 goto error;
Radek Krejcicbb473e2016-09-16 14:48:32 +02002875 } /* else unres was duplicated */
Radek Krejci9ff0a922016-07-14 13:08:05 +02002876 }
2877 }
Radek Krejcif0bb3602017-01-25 17:05:08 +01002878
2879 /* duplicate if-feature's extensions */
2880 retval->iffeature[i].ext_size = node->iffeature[i].ext_size;
Michal Vasko17e8ba32018-02-15 10:58:56 +01002881 if (lys_ext_dup(ctx, module, node->iffeature[i].ext, node->iffeature[i].ext_size,
Radek Krejci5138e9f2017-04-12 13:10:46 +02002882 &retval->iffeature[i], LYEXT_PAR_IFFEATURE, &retval->iffeature[i].ext, shallow, unres)) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002883 goto error;
2884 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002885 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002886
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002887 /* inherit config flags */
Radek Krejci7b9f5662016-11-07 16:28:37 +01002888 p = parent;
2889 do {
2890 for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent);
2891 } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter)));
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002892 if (iter) {
2893 flags = iter->flags & LYS_CONFIG_MASK;
2894 } else {
2895 /* default */
2896 flags = LYS_CONFIG_W;
2897 }
2898
2899 switch (finalize) {
2900 case 1:
2901 /* inherit config flags */
2902 if (retval->flags & LYS_CONFIG_SET) {
2903 /* skip nodes with an explicit config value */
2904 if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002905 LOGVAL(ctx, LYE_INARG, LY_VLOG_LYS, retval, "true", "config");
2906 LOGVAL(ctx, LYE_SPEC, LY_VLOG_PREV, NULL, "State nodes cannot have configuration nodes as children.");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002907 goto error;
2908 }
2909 break;
2910 }
2911
2912 if (retval->nodetype != LYS_USES) {
2913 retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags;
2914 }
Radek Krejci2cc25322017-09-06 16:32:02 +02002915
2916 /* inherit status */
Radek Krejcie2807ea2017-09-07 10:52:21 +02002917 if ((parent->flags & LYS_STATUS_MASK) > (retval->flags & LYS_STATUS_MASK)) {
2918 /* but do it only in case the parent has a stonger status */
2919 retval->flags &= ~LYS_STATUS_MASK;
2920 retval->flags |= (parent->flags & LYS_STATUS_MASK);
2921 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002922 break;
2923 case 2:
2924 /* erase config flags */
2925 retval->flags &= ~LYS_CONFIG_MASK;
2926 retval->flags &= ~LYS_CONFIG_SET;
2927 break;
2928 }
2929
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002930 /* connect it to the parent */
2931 if (lys_node_addchild(parent, retval->module, retval)) {
2932 goto error;
2933 }
Radek Krejcidce51452015-06-16 15:20:08 +02002934
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002935 /* go recursively */
2936 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002937 LY_TREE_FOR(node->child, iter) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01002938 if (iter->nodetype & LYS_GROUPING) {
2939 /* do not instantiate groupings */
2940 continue;
2941 }
Radek Krejci6ff885d2017-01-03 14:06:22 +01002942 if (!lys_node_dup_recursion(module, retval, iter, unres, 0, finalize)) {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002943 goto error;
2944 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002945 }
2946 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002947
2948 if (finalize == 1) {
2949 /* check that configuration lists have keys
2950 * - we really want to check keys_size in original node, because the keys are
2951 * not yet resolved here, it is done below in nodetype specific part */
2952 if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W)
2953 && !((struct lys_node_list *)node)->keys_size) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002954 LOGVAL(ctx, LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list");
Radek Krejcid2ac35f2016-10-21 23:08:28 +02002955 goto error;
2956 }
2957 }
Michal Vaskoff006c12016-02-17 11:15:19 +01002958 } else {
Michal Vaskoc5c26b02016-06-29 11:10:29 +02002959 memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002960 }
2961
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002962 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002963 * duplicate specific part of the structure
2964 */
2965 switch (node->nodetype) {
2966 case LYS_CONTAINER:
2967 if (cont_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002968 cont->when = lys_when_dup(module, cont_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002969 LY_CHECK_GOTO(!cont->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002970 }
2971 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002972
Radek Krejcia8d111f2017-05-31 13:57:37 +02002973 if (cont_orig->must) {
2974 cont->must = lys_restr_dup(module, cont_orig->must, cont_orig->must_size, shallow, unres);
2975 LY_CHECK_GOTO(!cont->must, error);
2976 cont->must_size = cont_orig->must_size;
2977 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002978
Radek Krejcif0bb3602017-01-25 17:05:08 +01002979 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
2980
2981 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002982 case LYS_CHOICE:
2983 if (choice_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02002984 choice->when = lys_when_dup(module, choice_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02002985 LY_CHECK_GOTO(!choice->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002986 }
2987
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002988 if (!shallow) {
2989 if (choice_orig->dflt) {
Radek Krejcibf2abff2016-08-23 15:51:52 +02002990 rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0,
2991 LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST,
2992 (const struct lys_node **)&choice->dflt);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002993 if (rc) {
2994 if (rc == EXIT_FAILURE) {
Michal Vasko53b7da02018-02-13 15:28:42 +01002995 LOGINT(ctx);
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002996 }
2997 goto error;
Michal Vasko49168a22015-08-17 16:35:41 +02002998 }
Michal Vaskod51d6ad2016-02-16 13:24:31 +01002999 } else {
3000 /* useless to check return value, we don't know whether
3001 * there really wasn't any default defined or it just hasn't
3002 * been resolved, we just hope for the best :)
3003 */
3004 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Michal Vasko49168a22015-08-17 16:35:41 +02003005 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003006 } else {
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003007 choice->dflt = choice_orig->dflt;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003008 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003009 break;
3010
3011 case LYS_LEAF:
Radek Krejcib53154b2017-07-19 09:14:13 +02003012 if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), lys_ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02003013 goto error;
3014 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003015 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
3016
3017 if (leaf_orig->dflt) {
3018 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Radek Krejcib53154b2017-07-19 09:14:13 +02003019 if (!lys_ingrouping(retval) || (leaf->type.base != LY_TYPE_LEAFREF)) {
Radek Krejci968f6942017-03-25 15:55:01 -05003020 /* problem is when it is an identityref referencing an identity from a module
3021 * and we are using the grouping in a different module */
Radek Krejcid06f5a42017-03-25 16:03:42 -05003022 if (leaf->type.base == LY_TYPE_IDENT) {
Michal Vaskofe31cc02017-03-10 15:52:31 +01003023 tmp_mod = leaf_orig->module;
3024 } else {
3025 tmp_mod = module;
3026 }
3027 if (unres_schema_add_node(tmp_mod, unres, &leaf->type, UNRES_TYPE_DFLT,
3028 (struct lys_node *)(&leaf->dflt)) == -1) {
3029 goto error;
3030 }
Michal Vasko49168a22015-08-17 16:35:41 +02003031 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003032 }
3033
Radek Krejcia8d111f2017-05-31 13:57:37 +02003034 if (leaf_orig->must) {
3035 leaf->must = lys_restr_dup(module, leaf_orig->must, leaf_orig->must_size, shallow, unres);
3036 LY_CHECK_GOTO(!leaf->must, error);
3037 leaf->must_size = leaf_orig->must_size;
3038 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003039
3040 if (leaf_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003041 leaf->when = lys_when_dup(module, leaf_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003042 LY_CHECK_GOTO(!leaf->when, error);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003043 }
3044 break;
3045
3046 case LYS_LEAFLIST:
Radek Krejcib53154b2017-07-19 09:14:13 +02003047 if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), lys_ingrouping(retval), shallow, unres)) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02003048 goto error;
3049 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003050 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
3051
3052 llist->min = llist_orig->min;
3053 llist->max = llist_orig->max;
3054
Radek Krejcia8d111f2017-05-31 13:57:37 +02003055 if (llist_orig->must) {
3056 llist->must = lys_restr_dup(module, llist_orig->must, llist_orig->must_size, shallow, unres);
3057 LY_CHECK_GOTO(!llist->must, error);
3058 llist->must_size = llist_orig->must_size;
3059 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003060
Radek Krejcia8d111f2017-05-31 13:57:37 +02003061 if (llist_orig->dflt) {
3062 llist->dflt = malloc(llist_orig->dflt_size * sizeof *llist->dflt);
Michal Vasko53b7da02018-02-13 15:28:42 +01003063 LY_CHECK_ERR_GOTO(!llist->dflt, LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003064 llist->dflt_size = llist_orig->dflt_size;
Radek Krejcia8d111f2017-05-31 13:57:37 +02003065
Radek Krejcic699e422017-06-02 15:18:58 +02003066 for (i = 0; i < llist->dflt_size; i++) {
3067 llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0);
Radek Krejcib53154b2017-07-19 09:14:13 +02003068 if (!lys_ingrouping(retval) || (llist->type.base != LY_TYPE_LEAFREF)) {
Radek Krejcic699e422017-06-02 15:18:58 +02003069 if ((llist->type.base == LY_TYPE_IDENT) && !strchr(llist->dflt[i], ':') && (module != llist_orig->module)) {
3070 tmp_mod = llist_orig->module;
3071 } else {
3072 tmp_mod = module;
3073 }
3074 if (unres_schema_add_node(tmp_mod, unres, &llist->type, UNRES_TYPE_DFLT,
3075 (struct lys_node *)(&llist->dflt[i])) == -1) {
3076 goto error;
3077 }
Michal Vaskofe31cc02017-03-10 15:52:31 +01003078 }
Radek Krejci51673202016-11-01 17:00:32 +01003079 }
3080 }
3081
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003082 if (llist_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003083 llist->when = lys_when_dup(module, llist_orig->when, shallow, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003084 }
3085 break;
3086
3087 case LYS_LIST:
3088 list->min = list_orig->min;
3089 list->max = list_orig->max;
3090
Radek Krejcia8d111f2017-05-31 13:57:37 +02003091 if (list_orig->must) {
3092 list->must = lys_restr_dup(module, list_orig->must, list_orig->must_size, shallow, unres);
3093 LY_CHECK_GOTO(!list->must, error);
3094 list->must_size = list_orig->must_size;
3095 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003096
Radek Krejcif0bb3602017-01-25 17:05:08 +01003097 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Michal Vasko38d01f72015-06-15 09:41:06 +02003098
Radek Krejcia8d111f2017-05-31 13:57:37 +02003099 if (list_orig->keys_size) {
3100 list->keys = calloc(list_orig->keys_size, sizeof *list->keys);
Michal Vasko53b7da02018-02-13 15:28:42 +01003101 LY_CHECK_ERR_GOTO(!list->keys, LOGMEM(ctx), error);
Radek Krejci5c08a992016-11-02 13:30:04 +01003102 list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003103 list->keys_size = list_orig->keys_size;
Michal Vasko0ea41032015-06-16 08:53:55 +02003104
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003105 if (!shallow) {
Radek Krejci5c08a992016-11-02 13:30:04 +01003106 /* the keys are going to be resolved only if the list is instantiated in data tree, not just
3107 * in another grouping */
3108 for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent);
3109 if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) {
3110 goto error;
Michal Vasko38d01f72015-06-15 09:41:06 +02003111 }
Michal Vasko0ea41032015-06-16 08:53:55 +02003112 } else {
Radek Krejcia8d111f2017-05-31 13:57:37 +02003113 memcpy(list->keys, list_orig->keys, list_orig->keys_size * sizeof *list->keys);
Radek Krejcia01e5432015-06-16 10:35:25 +02003114 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02003115 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003116
Radek Krejcia8d111f2017-05-31 13:57:37 +02003117 if (list_orig->unique) {
3118 list->unique = malloc(list_orig->unique_size * sizeof *list->unique);
Michal Vasko53b7da02018-02-13 15:28:42 +01003119 LY_CHECK_ERR_GOTO(!list->unique, LOGMEM(ctx), error);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003120 list->unique_size = list_orig->unique_size;
Radek Krejci581ce772015-11-10 17:22:40 +01003121
Radek Krejcic699e422017-06-02 15:18:58 +02003122 for (i = 0; i < list->unique_size; ++i) {
3123 list->unique[i].expr = malloc(list_orig->unique[i].expr_size * sizeof *list->unique[i].expr);
Michal Vasko53b7da02018-02-13 15:28:42 +01003124 LY_CHECK_ERR_GOTO(!list->unique[i].expr, LOGMEM(ctx), error);
Radek Krejcic699e422017-06-02 15:18:58 +02003125 list->unique[i].expr_size = list_orig->unique[i].expr_size;
3126 for (j = 0; j < list->unique[i].expr_size; j++) {
3127 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
3128
3129 /* if it stays in unres list, duplicate it also there */
3130 unique_info = malloc(sizeof *unique_info);
Michal Vasko53b7da02018-02-13 15:28:42 +01003131 LY_CHECK_ERR_GOTO(!unique_info, LOGMEM(ctx), error);
Radek Krejcic699e422017-06-02 15:18:58 +02003132 unique_info->list = (struct lys_node *)list;
3133 unique_info->expr = list->unique[i].expr[j];
3134 unique_info->trg_type = &list->unique[i].trg_type;
3135 unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info);
3136 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003137 }
3138 }
Radek Krejciefaeba32015-05-27 14:30:57 +02003139
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003140 if (list_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003141 list->when = lys_when_dup(module, list_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003142 LY_CHECK_GOTO(!list->when, error);
Radek Krejciefaeba32015-05-27 14:30:57 +02003143 }
Radek Krejcidce51452015-06-16 15:20:08 +02003144 break;
3145
3146 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02003147 case LYS_ANYDATA:
Radek Krejcia8d111f2017-05-31 13:57:37 +02003148 if (any_orig->must) {
3149 any->must = lys_restr_dup(module, any_orig->must, any_orig->must_size, shallow, unres);
3150 LY_CHECK_GOTO(!any->must, error);
3151 any->must_size = any_orig->must_size;
3152 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003153
Radek Krejcibf2abff2016-08-23 15:51:52 +02003154 if (any_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003155 any->when = lys_when_dup(module, any_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003156 LY_CHECK_GOTO(!any->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003157 }
3158 break;
3159
3160 case LYS_USES:
3161 uses->grp = uses_orig->grp;
3162
3163 if (uses_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003164 uses->when = lys_when_dup(module, uses_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003165 LY_CHECK_GOTO(!uses->when, error);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003166 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01003167 /* it is not needed to duplicate refine, nor augment. They are already applied to the uses children */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003168 break;
3169
3170 case LYS_CASE:
3171 if (cs_orig->when) {
Radek Krejci5138e9f2017-04-12 13:10:46 +02003172 cs->when = lys_when_dup(module, cs_orig->when, shallow, unres);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003173 LY_CHECK_GOTO(!cs->when, error);
Radek Krejcidce51452015-06-16 15:20:08 +02003174 }
3175 break;
3176
Radek Krejci96935402016-11-04 16:27:28 +01003177 case LYS_ACTION:
Radek Krejcidce51452015-06-16 15:20:08 +02003178 case LYS_RPC:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02003179 case LYS_INPUT:
3180 case LYS_OUTPUT:
Radek Krejcida04f4a2015-05-21 12:54:09 +02003181 case LYS_NOTIF:
Radek Krejcif0bb3602017-01-25 17:05:08 +01003182 /* typedefs are not needed in instantiated grouping, nor the deviation's shallow copy */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003183 break;
3184
3185 default:
3186 /* LY_NODE_AUGMENT */
Michal Vasko53b7da02018-02-13 15:28:42 +01003187 LOGINT(ctx);
Michal Vasko49168a22015-08-17 16:35:41 +02003188 goto error;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003189 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02003190
3191 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02003192
3193error:
Michal Vaskod51d6ad2016-02-16 13:24:31 +01003194 lys_node_free(retval, NULL, 0);
Michal Vasko49168a22015-08-17 16:35:41 +02003195 return NULL;
Radek Krejcida04f4a2015-05-21 12:54:09 +02003196}
3197
Radek Krejcib3142312016-11-09 11:04:12 +01003198int
3199lys_has_xpath(const struct lys_node *node)
3200{
3201 assert(node);
3202
3203 switch (node->nodetype) {
3204 case LYS_AUGMENT:
3205 if (((struct lys_node_augment *)node)->when) {
3206 return 1;
3207 }
3208 break;
3209 case LYS_CASE:
3210 if (((struct lys_node_case *)node)->when) {
3211 return 1;
3212 }
3213 break;
3214 case LYS_CHOICE:
3215 if (((struct lys_node_choice *)node)->when) {
3216 return 1;
3217 }
3218 break;
3219 case LYS_ANYDATA:
3220 if (((struct lys_node_anydata *)node)->when || ((struct lys_node_anydata *)node)->must_size) {
3221 return 1;
3222 }
3223 break;
3224 case LYS_LEAF:
3225 if (((struct lys_node_leaf *)node)->when || ((struct lys_node_leaf *)node)->must_size) {
3226 return 1;
3227 }
3228 break;
3229 case LYS_LEAFLIST:
3230 if (((struct lys_node_leaflist *)node)->when || ((struct lys_node_leaflist *)node)->must_size) {
3231 return 1;
3232 }
3233 break;
3234 case LYS_LIST:
3235 if (((struct lys_node_list *)node)->when || ((struct lys_node_list *)node)->must_size) {
3236 return 1;
3237 }
3238 break;
3239 case LYS_CONTAINER:
3240 if (((struct lys_node_container *)node)->when || ((struct lys_node_container *)node)->must_size) {
3241 return 1;
3242 }
3243 break;
3244 case LYS_INPUT:
3245 case LYS_OUTPUT:
3246 if (((struct lys_node_inout *)node)->must_size) {
3247 return 1;
3248 }
3249 break;
3250 case LYS_NOTIF:
3251 if (((struct lys_node_notif *)node)->must_size) {
3252 return 1;
3253 }
3254 break;
3255 case LYS_USES:
3256 if (((struct lys_node_uses *)node)->when) {
3257 return 1;
3258 }
3259 break;
3260 default:
3261 /* does not have XPath */
3262 break;
3263 }
3264
3265 return 0;
3266}
3267
Michal Vasko568b1952018-01-30 15:53:30 +01003268int
3269lys_type_is_local(const struct lys_type *type)
3270{
3271 if (!type->der->module) {
3272 /* build-in type */
3273 return 1;
3274 }
3275 /* type->parent can be either a typedef or leaf/leaf-list, but module pointers are compatible */
3276 return (lys_main_module(type->der->module) == lys_main_module(((struct lys_tpdf *)type->parent)->module));
3277}
3278
Radek Krejci2cc25322017-09-06 16:32:02 +02003279/*
3280 * shallow -
3281 * - do not inherit status from the parent
3282 */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003283struct lys_node *
Radek Krejci6ff885d2017-01-03 14:06:22 +01003284lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node,
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003285 struct unres_schema *unres, int shallow)
3286{
3287 struct lys_node *p = NULL;
Radek Krejcib3142312016-11-09 11:04:12 +01003288 int finalize = 0;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003289 struct lys_node *result, *iter, *next;
3290
3291 if (!shallow) {
Radek Krejcif0bb3602017-01-25 17:05:08 +01003292 /* get know where in schema tree we are to know what should be done during instantiation of the grouping */
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003293 for (p = parent;
3294 p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING));
3295 p = lys_parent(p));
3296 finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1;
3297 }
3298
Radek Krejci6ff885d2017-01-03 14:06:22 +01003299 result = lys_node_dup_recursion(module, parent, node, unres, shallow, finalize);
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003300 if (finalize) {
3301 /* check xpath expressions in the instantiated tree */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003302 for (iter = next = result; iter; iter = next) {
Radek Krejcib3142312016-11-09 11:04:12 +01003303 if (lys_has_xpath(iter) && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) {
Radek Krejci3c48d042016-11-07 14:42:36 +01003304 /* invalid xpath */
3305 return NULL;
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003306 }
3307
3308 /* select next item */
3309 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) {
3310 /* child exception for leafs, leaflists and anyxml without children, ignore groupings */
3311 next = NULL;
3312 } else {
3313 next = iter->child;
3314 }
3315 if (!next) {
3316 /* no children, try siblings */
Radek Krejci7212e0a2017-03-08 15:58:22 +01003317 if (iter == result) {
3318 /* we are done, no next element to process */
3319 break;
3320 }
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003321 next = iter->next;
3322 }
3323 while (!next) {
3324 /* parent is already processed, go to its sibling */
3325 iter = lys_parent(iter);
Radek Krejci7212e0a2017-03-08 15:58:22 +01003326 if (lys_parent(iter) == lys_parent(result)) {
Radek Krejcid2ac35f2016-10-21 23:08:28 +02003327 /* we are done, no next element to process */
3328 break;
3329 }
3330 next = iter->next;
3331 }
3332 }
3333 }
3334
3335 return result;
3336}
3337
Michal Vasko13b15832015-08-19 11:04:48 +02003338void
Michal Vaskoff006c12016-02-17 11:15:19 +01003339lys_node_switch(struct lys_node *dst, struct lys_node *src)
3340{
3341 struct lys_node *child;
3342
Michal Vaskob42b6972016-06-06 14:21:30 +02003343 assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype));
Michal Vaskoff006c12016-02-17 11:15:19 +01003344
3345 /* sibling next */
Michal Vasko8328da82016-08-25 09:27:22 +02003346 if (dst->prev->next) {
Michal Vaskoff006c12016-02-17 11:15:19 +01003347 dst->prev->next = src;
3348 }
3349
3350 /* sibling prev */
3351 if (dst->next) {
3352 dst->next->prev = src;
Michal Vasko8328da82016-08-25 09:27:22 +02003353 } else {
3354 for (child = dst->prev; child->prev->next; child = child->prev);
3355 child->prev = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003356 }
3357
3358 /* next */
3359 src->next = dst->next;
3360 dst->next = NULL;
3361
3362 /* prev */
3363 if (dst->prev != dst) {
3364 src->prev = dst->prev;
3365 }
3366 dst->prev = dst;
3367
3368 /* parent child */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003369 if (dst->parent) {
3370 if (dst->parent->child == dst) {
3371 dst->parent->child = src;
3372 }
Radek Krejci115fa882017-03-01 16:15:07 +01003373 } else if (lys_main_module(dst->module)->data == dst) {
3374 lys_main_module(dst->module)->data = src;
Michal Vaskoff006c12016-02-17 11:15:19 +01003375 }
3376
3377 /* parent */
Radek Krejci30bfcd22017-01-27 16:54:48 +01003378 src->parent = dst->parent; dst->parent = NULL;
3379
Michal Vaskoff006c12016-02-17 11:15:19 +01003380
3381 /* child parent */
3382 LY_TREE_FOR(dst->child, child) {
3383 if (child->parent == dst) {
3384 child->parent = src;
3385 }
3386 }
3387
3388 /* child */
3389 src->child = dst->child;
3390 dst->child = NULL;
Radek Krejcif0bb3602017-01-25 17:05:08 +01003391
3392 /* node-specific data */
3393 switch (dst->nodetype) {
3394 case LYS_CONTAINER:
3395 ((struct lys_node_container *)src)->tpdf_size = ((struct lys_node_container *)dst)->tpdf_size;
3396 ((struct lys_node_container *)src)->tpdf = ((struct lys_node_container *)dst)->tpdf;
3397 ((struct lys_node_container *)dst)->tpdf_size = 0;
3398 ((struct lys_node_container *)dst)->tpdf = NULL;
3399 break;
3400 case LYS_LIST:
3401 ((struct lys_node_list *)src)->tpdf_size = ((struct lys_node_list *)dst)->tpdf_size;
3402 ((struct lys_node_list *)src)->tpdf = ((struct lys_node_list *)dst)->tpdf;
3403 ((struct lys_node_list *)dst)->tpdf_size = 0;
3404 ((struct lys_node_list *)dst)->tpdf = NULL;
3405 break;
3406 case LYS_RPC:
3407 case LYS_ACTION:
3408 ((struct lys_node_rpc_action *)src)->tpdf_size = ((struct lys_node_rpc_action *)dst)->tpdf_size;
3409 ((struct lys_node_rpc_action *)src)->tpdf = ((struct lys_node_rpc_action *)dst)->tpdf;
3410 ((struct lys_node_rpc_action *)dst)->tpdf_size = 0;
3411 ((struct lys_node_rpc_action *)dst)->tpdf = NULL;
3412 break;
3413 case LYS_NOTIF:
3414 ((struct lys_node_notif *)src)->tpdf_size = ((struct lys_node_notif *)dst)->tpdf_size;
3415 ((struct lys_node_notif *)src)->tpdf = ((struct lys_node_notif *)dst)->tpdf;
3416 ((struct lys_node_notif *)dst)->tpdf_size = 0;
3417 ((struct lys_node_notif *)dst)->tpdf = NULL;
3418 break;
3419 case LYS_INPUT:
3420 case LYS_OUTPUT:
3421 ((struct lys_node_inout *)src)->tpdf_size = ((struct lys_node_inout *)dst)->tpdf_size;
3422 ((struct lys_node_inout *)src)->tpdf = ((struct lys_node_inout *)dst)->tpdf;
3423 ((struct lys_node_inout *)dst)->tpdf_size = 0;
3424 ((struct lys_node_inout *)dst)->tpdf = NULL;
3425 break;
3426 default:
3427 /* nothing special */
3428 break;
3429 }
3430
Michal Vaskoff006c12016-02-17 11:15:19 +01003431}
3432
3433void
Michal Vasko10681e82018-01-16 14:54:16 +01003434lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int free_subs, int remove_from_ctx)
Radek Krejcida04f4a2015-05-21 12:54:09 +02003435{
3436 struct ly_ctx *ctx;
3437 int i;
3438
3439 if (!module) {
3440 return;
3441 }
3442
3443 /* remove schema from the context */
3444 ctx = module->ctx;
Michal Vasko627975a2016-02-11 11:39:03 +01003445 if (remove_from_ctx && ctx->models.used) {
Radek Krejcida04f4a2015-05-21 12:54:09 +02003446 for (i = 0; i < ctx->models.used; i++) {
3447 if (ctx->models.list[i] == module) {
Michal Vasko627975a2016-02-11 11:39:03 +01003448 /* move all the models to not change the order in the list */
Radek Krejcida04f4a2015-05-21 12:54:09 +02003449 ctx->models.used--;
Michal Vasko627975a2016-02-11 11:39:03 +01003450 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 +02003451 ctx->models.list[ctx->models.used] = NULL;
3452 /* we are done */
3453 break;
3454 }
3455 }
3456 }
3457
3458 /* common part with struct ly_submodule */
Michal Vaskob746fff2016-02-11 11:37:50 +01003459 module_free_common(module, private_destructor);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003460
Michal Vasko10681e82018-01-16 14:54:16 +01003461 /* include */
3462 for (i = 0; i < module->inc_size; i++) {
3463 lydict_remove(ctx, module->inc[i].dsc);
3464 lydict_remove(ctx, module->inc[i].ref);
3465 lys_extension_instances_free(ctx, module->inc[i].ext, module->inc[i].ext_size, private_destructor);
3466 /* complete submodule free is done only from main module since
3467 * submodules propagate their includes to the main module */
3468 if (free_subs) {
3469 lys_submodule_free(module->inc[i].submodule, private_destructor);
3470 }
3471 }
3472 free(module->inc);
3473
Radek Krejcida04f4a2015-05-21 12:54:09 +02003474 /* specific items to free */
Michal Vaskob746fff2016-02-11 11:37:50 +01003475 lydict_remove(ctx, module->ns);
Radek Krejcida04f4a2015-05-21 12:54:09 +02003476
3477 free(module);
3478}
Radek Krejci7e97c352015-06-19 16:26:34 +02003479
Radek Krejci9de2c042016-10-19 16:53:06 +02003480static void
3481lys_features_disable_recursive(struct lys_feature *f)
3482{
3483 unsigned int i;
3484 struct lys_feature *depf;
3485
3486 /* disable the feature */
3487 f->flags &= ~LYS_FENABLED;
3488
3489 /* by disabling feature we have to disable also all features that depends on this feature */
3490 if (f->depfeatures) {
3491 for (i = 0; i < f->depfeatures->number; i++) {
3492 depf = (struct lys_feature *)f->depfeatures->set.g[i];
3493 if (depf->flags & LYS_FENABLED) {
3494 lys_features_disable_recursive(depf);
3495 }
3496 }
3497 }
3498}
3499
3500
Radek Krejci7e97c352015-06-19 16:26:34 +02003501/*
3502 * op: 1 - enable, 0 - disable
3503 */
3504static int
Michal Vasko1e62a092015-12-01 12:27:20 +01003505lys_features_change(const struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02003506{
3507 int all = 0;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003508 int i, j, k;
Radek Krejcia889c1f2016-10-19 15:50:11 +02003509 int progress, faili, failj, failk;
3510
3511 uint8_t fsize;
3512 struct lys_feature *f;
Radek Krejci7e97c352015-06-19 16:26:34 +02003513
3514 if (!module || !name || !strlen(name)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003515 LOGARG;
Radek Krejci7e97c352015-06-19 16:26:34 +02003516 return EXIT_FAILURE;
3517 }
3518
3519 if (!strcmp(name, "*")) {
3520 /* enable all */
3521 all = 1;
3522 }
3523
Radek Krejcia889c1f2016-10-19 15:50:11 +02003524 progress = failk = 1;
3525 while (progress && failk) {
3526 for (i = -1, failk = progress = 0; i < module->inc_size; i++) {
3527 if (i == -1) {
3528 fsize = module->features_size;
3529 f = module->features;
3530 } else {
3531 fsize = module->inc[i].submodule->features_size;
3532 f = module->inc[i].submodule->features;
3533 }
3534
3535 for (j = 0; j < fsize; j++) {
3536 if (all || !strcmp(f[j].name, name)) {
Michal Vasko34c3b552016-11-21 09:07:43 +01003537 if ((op && (f[j].flags & LYS_FENABLED)) || (!op && !(f[j].flags & LYS_FENABLED))) {
3538 if (all) {
3539 /* skip already set features */
3540 continue;
3541 } else {
3542 /* feature already set correctly */
3543 return EXIT_SUCCESS;
3544 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003545 }
3546
3547 if (op) {
3548 /* check referenced features if they are enabled */
3549 for (k = 0; k < f[j].iffeature_size; k++) {
3550 if (!resolve_iffeature(&f[j].iffeature[k])) {
3551 if (all) {
3552 faili = i;
3553 failj = j;
3554 failk = k + 1;
3555 break;
3556 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01003557 LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
Radek Krejcia889c1f2016-10-19 15:50:11 +02003558 f[j].name, k + 1);
3559 return EXIT_FAILURE;
3560 }
3561 }
3562 }
3563
3564 if (k == f[j].iffeature_size) {
3565 /* the last check passed, do the change */
3566 f[j].flags |= LYS_FENABLED;
3567 progress++;
3568 }
3569 } else {
Radek Krejci9de2c042016-10-19 16:53:06 +02003570 lys_features_disable_recursive(&f[j]);
Radek Krejcia889c1f2016-10-19 15:50:11 +02003571 progress++;
3572 }
3573 if (!all) {
3574 /* stop in case changing a single feature */
3575 return EXIT_SUCCESS;
Radek Krejci9ff0a922016-07-14 13:08:05 +02003576 }
3577 }
Radek Krejci7e97c352015-06-19 16:26:34 +02003578 }
3579 }
3580 }
Radek Krejcia889c1f2016-10-19 15:50:11 +02003581 if (failk) {
3582 /* print info about the last failing feature */
Michal Vasko53b7da02018-02-13 15:28:42 +01003583 LOGERR(module->ctx, LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.",
Radek Krejcia889c1f2016-10-19 15:50:11 +02003584 faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk);
3585 return EXIT_FAILURE;
Radek Krejci7e97c352015-06-19 16:26:34 +02003586 }
3587
3588 if (all) {
3589 return EXIT_SUCCESS;
3590 } else {
Radek Krejcia889c1f2016-10-19 15:50:11 +02003591 /* the specified feature not found */
Radek Krejci7e97c352015-06-19 16:26:34 +02003592 return EXIT_FAILURE;
3593 }
3594}
3595
3596API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003597lys_features_enable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003598{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003599 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02003600}
3601
3602API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003603lys_features_disable(const struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02003604{
Radek Krejci1d82ef62015-08-07 14:44:40 +02003605 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003606}
3607
3608API int
Michal Vasko1e62a092015-12-01 12:27:20 +01003609lys_features_state(const struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003610{
3611 int i, j;
3612
3613 if (!module || !feature) {
3614 return -1;
3615 }
3616
3617 /* search for the specified feature */
3618 /* module itself */
3619 for (i = 0; i < module->features_size; i++) {
3620 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003621 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003622 return 1;
3623 } else {
3624 return 0;
3625 }
3626 }
3627 }
3628
3629 /* submodules */
3630 for (j = 0; j < module->inc_size; j++) {
3631 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
3632 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003633 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003634 return 1;
3635 } else {
3636 return 0;
3637 }
3638 }
3639 }
3640 }
3641
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003642 /* feature definition not found */
3643 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02003644}
Michal Vasko2367e7c2015-07-07 11:33:44 +02003645
Radek Krejci96a10da2015-07-30 11:00:14 +02003646API const char **
Michal Vasko1e62a092015-12-01 12:27:20 +01003647lys_features_list(const struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02003648{
Radek Krejci96a10da2015-07-30 11:00:14 +02003649 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003650 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003651 unsigned int count;
3652
3653 if (!module) {
3654 return NULL;
3655 }
3656
3657 count = module->features_size;
3658 for (i = 0; i < module->inc_size; i++) {
3659 count += module->inc[i].submodule->features_size;
3660 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003661 result = malloc((count + 1) * sizeof *result);
Michal Vasko53b7da02018-02-13 15:28:42 +01003662 LY_CHECK_ERR_RETURN(!result, LOGMEM(module->ctx), NULL);
Radek Krejcia8d111f2017-05-31 13:57:37 +02003663
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003664 if (states) {
3665 *states = malloc((count + 1) * sizeof **states);
Michal Vasko53b7da02018-02-13 15:28:42 +01003666 LY_CHECK_ERR_RETURN(!(*states), LOGMEM(module->ctx); free(result), NULL);
Michal Vasko2367e7c2015-07-07 11:33:44 +02003667 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003668 count = 0;
3669
3670 /* module itself */
3671 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003672 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003673 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02003674 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003675 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003676 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003677 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003678 }
3679 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003680 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003681 }
3682
3683 /* submodules */
3684 for (j = 0; j < module->inc_size; j++) {
3685 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02003686 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02003687 if (states) {
3688 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
3689 (*states)[count] = 1;
3690 } else {
3691 (*states)[count] = 0;
3692 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02003693 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003694 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003695 }
3696 }
3697
Radek Krejcie98bb4b2015-07-30 14:21:41 +02003698 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02003699 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02003700
3701 return result;
3702}
Michal Vaskobaefb032015-09-24 14:52:10 +02003703
Radek Krejci6910a032016-04-13 10:06:21 +02003704API struct lys_module *
Michal Vasko320e8532016-02-15 13:11:57 +01003705lys_node_module(const struct lys_node *node)
Radek Krejcic071c542016-01-27 14:57:51 +01003706{
Michal Vaskof53187d2017-01-13 13:23:14 +01003707 if (!node) {
3708 return NULL;
3709 }
3710
Radek Krejcic071c542016-01-27 14:57:51 +01003711 return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module;
3712}
3713
Radek Krejci6910a032016-04-13 10:06:21 +02003714API struct lys_module *
Radek Krejcic4283442016-04-22 09:19:27 +02003715lys_main_module(const struct lys_module *module)
Michal Vasko320e8532016-02-15 13:11:57 +01003716{
Michal Vaskof53187d2017-01-13 13:23:14 +01003717 if (!module) {
3718 return NULL;
3719 }
3720
Michal Vasko320e8532016-02-15 13:11:57 +01003721 return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module);
3722}
3723
Michal Vaskobaefb032015-09-24 14:52:10 +02003724API struct lys_node *
Michal Vasko1e62a092015-12-01 12:27:20 +01003725lys_parent(const struct lys_node *node)
Michal Vaskobaefb032015-09-24 14:52:10 +02003726{
Radek Krejcif95b6292017-02-13 15:57:37 +01003727 struct lys_node *parent;
3728
3729 if (!node) {
Michal Vaskobaefb032015-09-24 14:52:10 +02003730 return NULL;
3731 }
3732
Radek Krejcif95b6292017-02-13 15:57:37 +01003733 if (node->nodetype == LYS_EXT) {
3734 if (((struct lys_ext_instance_complex*)node)->parent_type != LYEXT_PAR_NODE) {
3735 return NULL;
3736 }
3737 parent = (struct lys_node*)((struct lys_ext_instance_complex*)node)->parent;
3738 } else if (!node->parent) {
3739 return NULL;
3740 } else {
3741 parent = node->parent;
Michal Vaskobaefb032015-09-24 14:52:10 +02003742 }
3743
Radek Krejcif95b6292017-02-13 15:57:37 +01003744 if (parent->nodetype == LYS_AUGMENT) {
3745 return ((struct lys_node_augment *)parent)->target;
3746 } else {
3747 return parent;
3748 }
3749}
3750
3751struct lys_node **
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003752lys_child(const struct lys_node *node, LYS_NODE nodetype)
Radek Krejcif95b6292017-02-13 15:57:37 +01003753{
3754 void *pp;
3755 assert(node);
3756
3757 if (node->nodetype == LYS_EXT) {
3758 pp = lys_ext_complex_get_substmt(lys_snode2stmt(nodetype), (struct lys_ext_instance_complex*)node, NULL);
3759 if (!pp) {
3760 return NULL;
3761 }
3762 return (struct lys_node **)pp;
Michal Vasko54d4c202017-08-09 14:09:18 +02003763 } else if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Radek Krejcif7fe2cb2017-08-09 10:27:12 +02003764 return NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01003765 } else {
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01003766 return (struct lys_node **)&node->child;
Radek Krejcif95b6292017-02-13 15:57:37 +01003767 }
Michal Vaskobaefb032015-09-24 14:52:10 +02003768}
Michal Vasko1b229152016-01-13 11:28:38 +01003769
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003770API void *
Michal Vasko1b229152016-01-13 11:28:38 +01003771lys_set_private(const struct lys_node *node, void *priv)
3772{
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003773 void *prev;
3774
Michal Vasko1b229152016-01-13 11:28:38 +01003775 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003776 LOGARG;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003777 return NULL;
Michal Vasko1b229152016-01-13 11:28:38 +01003778 }
3779
Mislav Novakovicb5529e52016-02-29 11:42:43 +01003780 prev = node->priv;
3781 ((struct lys_node *)node)->priv = priv;
Radek Krejcifa0b5e02016-02-04 13:57:03 +01003782
3783 return prev;
Michal Vasko1b229152016-01-13 11:28:38 +01003784}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02003785
Michal Vasko01c6fd22016-05-20 11:43:05 +02003786int
3787lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref)
3788{
Michal Vasko53b7da02018-02-13 15:28:42 +01003789 struct lys_node_leaf *iter;
3790 struct ly_ctx *ctx = leafref_target->module->ctx;
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003791
Michal Vasko48a573d2016-07-01 11:46:02 +02003792 if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003793 LOGINT(ctx);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003794 return -1;
3795 }
3796
Pavol Vican93175152016-08-30 15:34:44 +02003797 /* check for config flag */
Radek Krejcic688ca02017-03-20 12:54:39 +01003798 if (((struct lys_node_leaf*)leafref)->type.info.lref.req != -1 &&
3799 (leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003800 LOGVAL(ctx, LYE_SPEC, LY_VLOG_LYS, leafref,
Radek Krejcid831dd42017-03-16 12:59:30 +01003801 "The leafref %s is config but refers to a non-config %s.",
Pavol Vican93175152016-08-30 15:34:44 +02003802 strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype));
3803 return -1;
3804 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003805 /* check for cycles */
Michal Vasko53b7da02018-02-13 15:28:42 +01003806 for (iter = leafref_target; iter && iter->type.base == LY_TYPE_LEAFREF; iter = iter->type.info.lref.target) {
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003807 if ((void *)iter == (void *)leafref) {
3808 /* cycle detected */
Michal Vasko53b7da02018-02-13 15:28:42 +01003809 LOGVAL(ctx, LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref);
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003810 return -1;
3811 }
Radek Krejcid8fb03c2016-06-13 15:52:22 +02003812 }
3813
3814 /* create fake child - the ly_set structure to hold the list of
Michal Vasko48a573d2016-07-01 11:46:02 +02003815 * leafrefs referencing the leaf(-list) */
Radek Krejci85a54be2016-10-20 12:39:56 +02003816 if (!leafref_target->backlinks) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003817 leafref_target->backlinks = (void *)ly_set_new();
Radek Krejci85a54be2016-10-20 12:39:56 +02003818 if (!leafref_target->backlinks) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003819 LOGMEM(ctx);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003820 return -1;
3821 }
3822 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003823 ly_set_add(leafref_target->backlinks, leafref, 0);
Michal Vasko01c6fd22016-05-20 11:43:05 +02003824
3825 return 0;
3826}
3827
Michal Vasko8548e082016-07-22 12:00:18 +02003828/* not needed currently */
3829#if 0
3830
Michal Vasko5b3492c2016-07-20 09:37:40 +02003831static const char *
3832lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len)
3833{
3834 struct lys_module *prev_mod;
3835 uint32_t str_len, mod_len, buf_idx;
3836
Radek Krejcibf2abff2016-08-23 15:51:52 +02003837 if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003838 LOGINT;
3839 return NULL;
3840 }
3841
3842 buf_idx = buf_len - 1;
3843 buf[buf_idx] = '\0';
3844
3845 while (node) {
3846 if (lys_parent(node)) {
3847 prev_mod = lys_node_module(lys_parent(node));
3848 } else {
3849 prev_mod = NULL;
3850 }
3851
Radek Krejcibf2abff2016-08-23 15:51:52 +02003852 if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003853 str_len = strlen(node->name);
3854
3855 if (prev_mod != node->module) {
3856 mod_len = strlen(node->module->name);
3857 } else {
3858 mod_len = 0;
3859 }
3860
3861 if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) {
3862 LOGINT;
3863 return NULL;
3864 }
3865
3866 buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len;
3867
3868 buf[buf_idx] = '/';
3869 if (mod_len) {
3870 memcpy(buf + buf_idx + 1, node->module->name, mod_len);
3871 buf[buf_idx + 1 + mod_len] = ':';
3872 }
3873 memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len);
3874 }
3875
3876 node = lys_parent(node);
3877 }
3878
3879 return buf + buf_idx;
3880}
3881
Michal Vasko8548e082016-07-22 12:00:18 +02003882#endif
3883
3884API struct ly_set *
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003885lys_xpath_atomize(const struct lys_node *ctx_node, enum lyxp_node_type ctx_node_type, const char *expr, int options)
Michal Vasko5b3492c2016-07-20 09:37:40 +02003886{
Michal Vasko508a50d2016-09-07 14:50:33 +02003887 struct lyxp_set set;
Michal Vasko8548e082016-07-22 12:00:18 +02003888 struct ly_set *ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003889 uint32_t i;
3890
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003891 if (!ctx_node || !expr) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003892 LOGARG;
Michal Vasko8548e082016-07-22 12:00:18 +02003893 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003894 }
3895
Michal Vaskob94a5e42016-09-08 14:01:56 +02003896 /* adjust the root */
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003897 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
Michal Vaskob94a5e42016-09-08 14:01:56 +02003898 do {
Michal Vaskocb45f472018-02-12 10:47:42 +01003899 ctx_node = lys_getnext(NULL, NULL, lys_node_module(ctx_node), LYS_GETNEXT_NOSTATECHECK);
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003900 } while ((ctx_node_type == LYXP_NODE_ROOT_CONFIG) && (ctx_node->flags & LYS_CONFIG_R));
Michal Vaskob94a5e42016-09-08 14:01:56 +02003901 }
3902
Michal Vasko508a50d2016-09-07 14:50:33 +02003903 memset(&set, 0, sizeof set);
Michal Vasko5b3492c2016-07-20 09:37:40 +02003904
3905 if (options & LYXP_MUST) {
3906 options &= ~LYXP_MUST;
3907 options |= LYXP_SNODE_MUST;
3908 } else if (options & LYXP_WHEN) {
3909 options &= ~LYXP_WHEN;
3910 options |= LYXP_SNODE_WHEN;
3911 } else {
3912 options |= LYXP_SNODE;
3913 }
3914
Michal Vasko9ff79aa2017-07-03 14:10:32 +02003915 if (lyxp_atomize(expr, ctx_node, ctx_node_type, &set, options, NULL)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003916 free(set.val.snodes);
Michal Vasko53b7da02018-02-13 15:28:42 +01003917 LOGVAL(ctx_node->module->ctx, LYE_SPEC, LY_VLOG_LYS, ctx_node, "Resolving XPath expression \"%s\" failed.", expr);
Michal Vasko8548e082016-07-22 12:00:18 +02003918 return NULL;
Michal Vasko5b3492c2016-07-20 09:37:40 +02003919 }
3920
Michal Vasko8548e082016-07-22 12:00:18 +02003921 ret_set = ly_set_new();
Michal Vasko5b3492c2016-07-20 09:37:40 +02003922
Michal Vasko508a50d2016-09-07 14:50:33 +02003923 for (i = 0; i < set.used; ++i) {
3924 switch (set.val.snodes[i].type) {
Michal Vasko5b3492c2016-07-20 09:37:40 +02003925 case LYXP_NODE_ELEM:
Michal Vasko508a50d2016-09-07 14:50:33 +02003926 if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) {
Michal Vasko8548e082016-07-22 12:00:18 +02003927 ly_set_free(ret_set);
Michal Vasko508a50d2016-09-07 14:50:33 +02003928 free(set.val.snodes);
Michal Vasko8548e082016-07-22 12:00:18 +02003929 return NULL;
3930 }
Michal Vasko5b3492c2016-07-20 09:37:40 +02003931 break;
3932 default:
Michal Vasko508a50d2016-09-07 14:50:33 +02003933 /* ignore roots, text and attr should not ever appear */
Michal Vasko5b3492c2016-07-20 09:37:40 +02003934 break;
3935 }
3936 }
3937
Michal Vasko508a50d2016-09-07 14:50:33 +02003938 free(set.val.snodes);
3939 return ret_set;
3940}
3941
3942API struct ly_set *
3943lys_node_xpath_atomize(const struct lys_node *node, int options)
3944{
3945 const struct lys_node *next, *elem, *parent, *tmp;
3946 struct lyxp_set set;
3947 struct ly_set *ret_set;
3948 uint16_t i;
3949
3950 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01003951 LOGARG;
Michal Vasko508a50d2016-09-07 14:50:33 +02003952 return NULL;
3953 }
3954
3955 for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent));
3956 if (!parent) {
3957 /* not in input, output, or notification */
3958 return NULL;
3959 }
3960
3961 ret_set = ly_set_new();
Radek Krejcid9af8d22016-09-07 18:44:26 +02003962 if (!ret_set) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003963 return NULL;
3964 }
3965
3966 LY_TREE_DFS_BEGIN(node, next, elem) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01003967 if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_XPATH_DEP)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003968 /* elem has no dependencies from other subtrees and local nodes get discarded */
3969 goto next_iter;
3970 }
3971
Michal Vaskof96dfb62017-08-17 12:23:49 +02003972 if (lyxp_node_atomize(elem, &set, 0)) {
Michal Vasko508a50d2016-09-07 14:50:33 +02003973 ly_set_free(ret_set);
3974 free(set.val.snodes);
3975 return NULL;
3976 }
3977
3978 for (i = 0; i < set.used; ++i) {
3979 switch (set.val.snodes[i].type) {
3980 case LYXP_NODE_ELEM:
3981 if (options & LYXP_NO_LOCAL) {
3982 for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp));
3983 if (tmp) {
3984 /* in local subtree, discard */
3985 break;
3986 }
3987 }
3988 if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) {
3989 ly_set_free(ret_set);
3990 free(set.val.snodes);
3991 return NULL;
3992 }
3993 break;
3994 default:
3995 /* ignore roots, text and attr should not ever appear */
3996 break;
3997 }
3998 }
3999
4000 free(set.val.snodes);
4001 if (!(options & LYXP_RECURSIVE)) {
4002 break;
4003 }
4004next_iter:
4005 LY_TREE_DFS_END(node, next, elem);
4006 }
4007
Michal Vasko8548e082016-07-22 12:00:18 +02004008 return ret_set;
Michal Vasko5b3492c2016-07-20 09:37:40 +02004009}
4010
Michal Vasko44ab1462017-05-18 13:18:36 +02004011/* logs */
4012int
4013apply_aug(struct lys_node_augment *augment, struct unres_schema *unres)
Radek Krejci0ec51da2016-12-14 16:42:03 +01004014{
Michal Vasko44ab1462017-05-18 13:18:36 +02004015 struct lys_node *child, *parent;
4016 int clear_config;
4017 unsigned int u;
Michal Vaskod02e30e2018-01-22 13:35:48 +01004018 uint8_t *v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004019 struct lys_ext_instance *ext;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004020
4021 assert(augment->target && (augment->flags & LYS_NOTAPPLIED));
4022
Radek Krejcic9d78692017-08-24 17:17:18 +02004023 if (!augment->child) {
4024 /* nothing to apply */
4025 goto success;
4026 }
4027
Michal Vaskobb520442017-05-23 10:55:18 +02004028 /* check that all the modules are implemented */
4029 for (parent = augment->target; parent; parent = lys_parent(parent)) {
4030 if (!lys_node_module(parent)->implemented) {
4031 if (lys_set_implemented(lys_node_module(parent))) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004032 LOGERR(augment->module->ctx, ly_errno, "Making the augment target module \"%s\" implemented failed.",
4033 lys_node_module(parent)->name);
Michal Vaskobb520442017-05-23 10:55:18 +02004034 return -1;
4035 }
Michal Vaskobb520442017-05-23 10:55:18 +02004036 }
4037 }
4038
Radek Krejci0ec51da2016-12-14 16:42:03 +01004039 /* reconnect augmenting data into the target - add them to the target child list */
4040 if (augment->target->child) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004041 child = augment->target->child->prev;
4042 child->next = augment->child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004043 augment->target->child->prev = augment->child->prev;
Michal Vasko44ab1462017-05-18 13:18:36 +02004044 augment->child->prev = child;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004045 } else {
4046 augment->target->child = augment->child;
4047 }
4048
Michal Vasko44ab1462017-05-18 13:18:36 +02004049 /* inherit config information from actual parent */
4050 for (parent = augment->target; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); parent = lys_parent(parent));
4051 clear_config = (parent) ? 1 : 0;
4052 LY_TREE_FOR(augment->child, child) {
4053 if (inherit_config_flag(child, augment->target->flags & LYS_CONFIG_MASK, clear_config)) {
4054 return -1;
4055 }
4056 }
4057
4058 /* inherit extensions if any */
4059 for (u = 0; u < augment->target->ext_size; u++) {
4060 ext = augment->target->ext[u]; /* shortcut */
4061 if (ext && ext->def->plugin && (ext->def->plugin->flags & LYEXT_OPT_INHERIT)) {
Michal Vaskod02e30e2018-01-22 13:35:48 +01004062 v = malloc(sizeof *v);
Michal Vasko53b7da02018-02-13 15:28:42 +01004063 LY_CHECK_ERR_RETURN(!v, LOGMEM(augment->module->ctx), -1);
Michal Vaskod02e30e2018-01-22 13:35:48 +01004064 *v = u;
4065 if (unres_schema_add_node(lys_main_module(augment->module), unres, &augment->target->ext,
4066 UNRES_EXT_FINALIZE, (struct lys_node *)v) == -1) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004067 /* something really bad happend since the extension finalization is not actually
4068 * being resolved while adding into unres, so something more serious with the unres
4069 * list itself must happened */
4070 return -1;
4071 }
4072 }
4073 }
4074
Radek Krejcic9d78692017-08-24 17:17:18 +02004075success:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004076 /* remove the flag about not applicability */
4077 augment->flags &= ~LYS_NOTAPPLIED;
Michal Vasko44ab1462017-05-18 13:18:36 +02004078 return EXIT_SUCCESS;
Radek Krejci0ec51da2016-12-14 16:42:03 +01004079}
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004080
Radek Krejcib2541a32016-12-12 16:45:57 +01004081static void
4082remove_aug(struct lys_node_augment *augment)
4083{
4084 struct lys_node *last, *elem;
4085
Michal Vaskof1aa47d2017-09-21 12:09:29 +02004086 if ((augment->flags & LYS_NOTAPPLIED) || !augment->target) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004087 /* skip already not applied augment */
Radek Krejcib2541a32016-12-12 16:45:57 +01004088 return;
4089 }
4090
4091 elem = augment->child;
4092 if (elem) {
4093 LY_TREE_FOR(elem, last) {
4094 if (!last->next || (last->next->parent != (struct lys_node *)augment)) {
4095 break;
4096 }
4097 }
4098 /* elem is first augment child, last is the last child */
4099
4100 /* parent child ptr */
4101 if (augment->target->child == elem) {
4102 augment->target->child = last->next;
4103 }
4104
4105 /* parent child next ptr */
4106 if (elem->prev->next) {
4107 elem->prev->next = last->next;
4108 }
4109
4110 /* parent child prev ptr */
4111 if (last->next) {
4112 last->next->prev = elem->prev;
4113 } else if (augment->target->child) {
4114 augment->target->child->prev = elem->prev;
4115 }
4116
4117 /* update augment children themselves */
4118 elem->prev = last;
4119 last->next = NULL;
4120 }
4121
Radek Krejci0ec51da2016-12-14 16:42:03 +01004122 /* augment->target still keeps the resolved target, but for lys_augment_free()
4123 * we have to keep information that this augment is not applied to free its data */
4124 augment->flags |= LYS_NOTAPPLIED;
Radek Krejcib2541a32016-12-12 16:45:57 +01004125}
4126
Radek Krejci30bfcd22017-01-27 16:54:48 +01004127/*
4128 * @param[in] module - the module where the deviation is defined
4129 */
4130static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004131lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004132{
Radek Krejcic9d78692017-08-24 17:17:18 +02004133 int ret, reapply = 0;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004134 char *parent_path;
4135 struct lys_node *target = NULL, *parent;
Michal Vasko50576712017-07-28 12:28:33 +02004136 struct ly_set *set;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004137
4138 if (!dev->deviate) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004139 return;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004140 }
4141
4142 if (dev->deviate[0].mod == LY_DEVIATE_NO) {
4143 if (dev->orig_node) {
4144 /* removing not-supported deviation ... */
4145 if (strrchr(dev->target_name, '/') != dev->target_name) {
4146 /* ... from a parent */
4147
4148 /* reconnect to its previous position */
4149 parent = dev->orig_node->parent;
Michal Vaskoa1074a52018-01-03 12:18:53 +01004150 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejcic9d78692017-08-24 17:17:18 +02004151 dev->orig_node->parent = NULL;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004152 /* the original node was actually from augment, we have to get know if the augment is
4153 * applied (its module is enabled and implemented). If yes, the node will be connected
4154 * to the augment and the linkage with the target will be fixed if needed, otherwise
4155 * it will be connected only to the augment */
Radek Krejcic9d78692017-08-24 17:17:18 +02004156 if (!(parent->flags & LYS_NOTAPPLIED)) {
4157 /* start with removing augment if applied before adding nodes, we have to make sure
4158 * that everything will be connect correctly */
4159 remove_aug((struct lys_node_augment *)parent);
4160 reapply = 1;
4161 }
4162 /* connect the deviated node back into the augment */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004163 lys_node_addchild(parent, NULL, dev->orig_node);
Radek Krejcic9d78692017-08-24 17:17:18 +02004164 if (reapply) {
Radek Krejci30bfcd22017-01-27 16:54:48 +01004165 /* augment is supposed to be applied, so fix pointers in target and the status of the original node */
Michal Vasko823fbe02017-12-14 11:01:40 +01004166 parent->flags |= LYS_NOTAPPLIED; /* allow apply_aug() */
4167 apply_aug((struct lys_node_augment *)parent, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004168 }
Michal Vaskoa1074a52018-01-03 12:18:53 +01004169 } else if (parent && (parent->nodetype == LYS_USES)) {
4170 /* uses child */
4171 lys_node_addchild(parent, NULL, dev->orig_node);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004172 } else {
4173 /* non-augment, non-toplevel */
4174 parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name);
Michal Vasko50576712017-07-28 12:28:33 +02004175 ret = resolve_schema_nodeid(parent_path, NULL, module, &set, 0, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004176 free(parent_path);
Michal Vasko50576712017-07-28 12:28:33 +02004177 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004178 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004179 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004180 return;
4181 }
Michal Vasko50576712017-07-28 12:28:33 +02004182 target = set->set.s[0];
4183 ly_set_free(set);
4184
Radek Krejci30bfcd22017-01-27 16:54:48 +01004185 lys_node_addchild(target, NULL, dev->orig_node);
4186 }
4187 } else {
4188 /* ... from top-level data */
4189 lys_node_addchild(NULL, (struct lys_module *)dev->orig_node->module, dev->orig_node);
4190 }
4191
4192 dev->orig_node = NULL;
4193 } else {
4194 /* adding not-supported deviation */
Michal Vasko50576712017-07-28 12:28:33 +02004195 ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
4196 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004197 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004198 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004199 return;
4200 }
Michal Vasko50576712017-07-28 12:28:33 +02004201 target = set->set.s[0];
4202 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004203
4204 /* unlink and store the original node */
4205 parent = target->parent;
4206 lys_node_unlink(target);
Michal Vaskoa1074a52018-01-03 12:18:53 +01004207 if (parent && (parent->nodetype & (LYS_AUGMENT | LYS_USES))) {
Radek Krejci30bfcd22017-01-27 16:54:48 +01004208 /* hack for augment, because when the original will be sometime reconnected back, we actually need
4209 * to reconnect it to both - the augment and its target (which is deduced from the deviations target
4210 * path), so we need to remember the augment as an addition */
Michal Vaskoa1074a52018-01-03 12:18:53 +01004211 /* we also need to remember the parent uses so that we connect it back to it when switching deviation state */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004212 target->parent = parent;
4213 }
4214 dev->orig_node = target;
4215 }
4216 } else {
Michal Vasko50576712017-07-28 12:28:33 +02004217 ret = resolve_schema_nodeid(dev->target_name, NULL, module, &set, 0, 1);
4218 if (ret == -1) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004219 LOGINT(module->ctx);
Michal Vasko50576712017-07-28 12:28:33 +02004220 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004221 return;
4222 }
Michal Vasko50576712017-07-28 12:28:33 +02004223 target = set->set.s[0];
4224 ly_set_free(set);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004225
4226 lys_node_switch(target, dev->orig_node);
4227 dev->orig_node = target;
4228 }
4229}
4230
4231/* temporarily removes or applies deviations, updates module deviation flag accordingly */
4232void
4233lys_switch_deviations(struct lys_module *module)
4234{
4235 uint32_t i = 0, j;
4236 const struct lys_module *mod;
4237 const char *ptr;
Michal Vasko44ab1462017-05-18 13:18:36 +02004238 struct unres_schema *unres;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004239
4240 if (module->deviated) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004241 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004242 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Michal Vasko44ab1462017-05-18 13:18:36 +02004243
Radek Krejci30bfcd22017-01-27 16:54:48 +01004244 while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) {
4245 if (mod == module) {
4246 continue;
4247 }
4248
4249 for (j = 0; j < mod->deviation_size; ++j) {
4250 ptr = strstr(mod->deviation[j].target_name, module->name);
4251 if (ptr && ptr[strlen(module->name)] == ':') {
Michal Vasko44ab1462017-05-18 13:18:36 +02004252 lys_switch_deviation(&mod->deviation[j], mod, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004253 }
4254 }
4255 }
4256
4257 if (module->deviated == 2) {
4258 module->deviated = 1;
4259 } else {
4260 module->deviated = 2;
4261 }
Radek Krejci29eac3d2017-06-01 16:50:02 +02004262 for (j = 0; j < module->inc_size; j++) {
4263 if (module->inc[j].submodule->deviated) {
4264 module->inc[j].submodule->deviated = module->deviated;
4265 }
4266 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004267
4268 if (unres->count) {
4269 resolve_unres_schema(module, unres);
4270 }
4271 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004272 }
4273}
4274
4275static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004276apply_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004277{
Michal Vasko44ab1462017-05-18 13:18:36 +02004278 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004279
4280 assert(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004281 lys_node_module(dev->orig_node)->deviated = 1; /* main module */
4282 dev->orig_node->module->deviated = 1; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004283}
4284
4285static void
Michal Vasko44ab1462017-05-18 13:18:36 +02004286remove_dev(struct lys_deviation *dev, const struct lys_module *module, struct unres_schema *unres)
Radek Krejci30bfcd22017-01-27 16:54:48 +01004287{
4288 uint32_t idx = 0, j;
4289 const struct lys_module *mod;
Radek Krejci29eac3d2017-06-01 16:50:02 +02004290 struct lys_module *target_mod, *target_submod;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004291 const char *ptr;
4292
4293 if (dev->orig_node) {
4294 target_mod = lys_node_module(dev->orig_node);
Radek Krejci29eac3d2017-06-01 16:50:02 +02004295 target_submod = dev->orig_node->module;
Radek Krejci30bfcd22017-01-27 16:54:48 +01004296 } else {
Michal Vasko53b7da02018-02-13 15:28:42 +01004297 LOGINT(module->ctx);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004298 return;
4299 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004300 lys_switch_deviation(dev, module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004301
4302 /* clear the deviation flag if possible */
4303 while ((mod = ly_ctx_get_module_iter(module->ctx, &idx))) {
4304 if ((mod == module) || (mod == target_mod)) {
4305 continue;
4306 }
4307
4308 for (j = 0; j < mod->deviation_size; ++j) {
4309 ptr = strstr(mod->deviation[j].target_name, target_mod->name);
4310 if (ptr && (ptr[strlen(target_mod->name)] == ':')) {
4311 /* some other module deviation targets the inspected module, flag remains */
4312 break;
4313 }
4314 }
4315
4316 if (j < mod->deviation_size) {
4317 break;
4318 }
4319 }
4320
4321 if (!mod) {
Radek Krejci29eac3d2017-06-01 16:50:02 +02004322 target_mod->deviated = 0; /* main module */
4323 target_submod->deviated = 0; /* possible submodule */
Radek Krejci30bfcd22017-01-27 16:54:48 +01004324 }
4325}
4326
4327void
4328lys_sub_module_apply_devs_augs(struct lys_module *module)
4329{
4330 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004331 struct unres_schema *unres;
4332
4333 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004334 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Radek Krejci30bfcd22017-01-27 16:54:48 +01004335
4336 /* remove applied deviations */
4337 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004338 apply_dev(&module->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004339 }
4340 /* remove applied augments */
4341 for (u = 0; u < module->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004342 apply_aug(&module->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004343 }
4344
4345 /* remove deviation and augments defined in submodules */
4346 for (v = 0; v < module->inc_size; ++v) {
4347 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004348 apply_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004349 }
4350
4351 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004352 apply_aug(&module->inc[v].submodule->augment[u], unres);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004353 }
4354 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004355
4356 if (unres->count) {
4357 resolve_unres_schema(module, unres);
4358 }
4359 /* nothing else left to do even if something is not resolved */
4360 unres_schema_free(module, &unres, 1);
Radek Krejci30bfcd22017-01-27 16:54:48 +01004361}
4362
Radek Krejcib2541a32016-12-12 16:45:57 +01004363void
4364lys_sub_module_remove_devs_augs(struct lys_module *module)
4365{
4366 uint8_t u, v;
Michal Vasko44ab1462017-05-18 13:18:36 +02004367 struct unres_schema *unres;
4368
4369 unres = calloc(1, sizeof *unres);
Michal Vasko53b7da02018-02-13 15:28:42 +01004370 LY_CHECK_ERR_RETURN(!unres, LOGMEM(module->ctx), );
Radek Krejcib2541a32016-12-12 16:45:57 +01004371
4372 /* remove applied deviations */
4373 for (u = 0; u < module->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004374 remove_dev(&module->deviation[u], module, unres);
Radek Krejcib2541a32016-12-12 16:45:57 +01004375 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004376 /* remove applied augments */
Radek Krejcib2541a32016-12-12 16:45:57 +01004377 for (u = 0; u < module->augment_size; ++u) {
4378 remove_aug(&module->augment[u]);
4379 }
4380
4381 /* remove deviation and augments defined in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004382 for (v = 0; v < module->inc_size && module->inc[v].submodule; ++v) {
Radek Krejcib2541a32016-12-12 16:45:57 +01004383 for (u = 0; u < module->inc[v].submodule->deviation_size; ++u) {
Michal Vasko44ab1462017-05-18 13:18:36 +02004384 remove_dev(&module->inc[v].submodule->deviation[u], module, unres);
Radek Krejcidbc15262016-06-16 14:58:29 +02004385 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004386
Radek Krejcib2541a32016-12-12 16:45:57 +01004387 for (u = 0; u < module->inc[v].submodule->augment_size; ++u) {
4388 remove_aug(&module->inc[v].submodule->augment[u]);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004389 }
4390 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004391
4392 if (unres->count) {
4393 resolve_unres_schema(module, unres);
4394 }
4395 /* nothing else left to do even if something is not resolved */
4396 unres_schema_free(module, &unres, 1);
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004397}
4398
Radek Krejci27fe55e2016-09-13 17:13:35 +02004399static int
4400lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres)
4401{
4402 struct lys_node *root, *next, *node;
Radek Krejci9e6af732017-04-27 14:40:25 +02004403 uint16_t i, j;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004404
4405 for (i = 0; i < module->augment_size; i++) {
4406 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004407 assert(module->augment[i].target);
4408 if (apply_aug(&module->augment[i], unres)) {
4409 return EXIT_FAILURE;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004410 }
4411 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004412
4413 /* identities */
4414 for (i = 0; i < module->ident_size; i++) {
4415 for (j = 0; j < module->ident[i].base_size; j++) {
4416 resolve_identity_backlink_update(&module->ident[i], module->ident[i].base[j]);
4417 }
4418 }
4419
Radek Krejci27fe55e2016-09-13 17:13:35 +02004420 LY_TREE_FOR(module->data, root) {
4421 /* handle leafrefs and recursively change the implemented flags in the leafref targets */
4422 LY_TREE_DFS_BEGIN(root, next, node) {
4423 if (node->nodetype == LYS_GROUPING) {
4424 goto nextsibling;
4425 }
4426 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4427 if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) {
4428 if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type,
4429 UNRES_TYPE_LEAFREF, node) == -1) {
4430 return EXIT_FAILURE;
4431 }
4432 }
4433 }
4434
4435 /* modified LY_TREE_DFS_END */
4436 next = node->child;
4437 /* child exception for leafs, leaflists and anyxml without children */
4438 if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
4439 next = NULL;
4440 }
4441 if (!next) {
4442nextsibling:
4443 /* no children */
4444 if (node == root) {
4445 /* we are done, root has no children */
4446 break;
4447 }
4448 /* try siblings */
4449 next = node->next;
4450 }
4451 while (!next) {
4452 /* parent is already processed, go to its sibling */
4453 node = lys_parent(node);
4454 /* no siblings, go back through parents */
4455 if (lys_parent(node) == lys_parent(root)) {
4456 /* we are done, no next element to process */
4457 break;
4458 }
4459 next = node->next;
4460 }
4461 }
4462 }
4463
4464 return EXIT_SUCCESS;
4465}
4466
4467API int
4468lys_set_implemented(const struct lys_module *module)
Michal Vasko26055752016-05-03 11:36:31 +02004469{
4470 struct ly_ctx *ctx;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004471 struct unres_schema *unres;
Radek Krejci9e6af732017-04-27 14:40:25 +02004472 int i, j, k, disabled = 0;
Michal Vasko26055752016-05-03 11:36:31 +02004473
Radek Krejci27fe55e2016-09-13 17:13:35 +02004474 if (!module) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004475 LOGARG;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004476 return EXIT_FAILURE;
4477 }
4478
4479 module = lys_main_module(module);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004480
4481 if (module->disabled) {
4482 disabled = 1;
4483 lys_set_enabled(module);
4484 }
4485
Michal Vasko26055752016-05-03 11:36:31 +02004486 if (module->implemented) {
4487 return EXIT_SUCCESS;
4488 }
4489
4490 ctx = module->ctx;
4491
4492 for (i = 0; i < ctx->models.used; ++i) {
4493 if (module == ctx->models.list[i]) {
4494 continue;
4495 }
4496
4497 if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004498 LOGERR(ctx, LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004499 if (disabled) {
4500 /* set it back disabled */
4501 lys_set_disabled(module);
4502 }
Michal Vasko26055752016-05-03 11:36:31 +02004503 return EXIT_FAILURE;
4504 }
4505 }
4506
Radek Krejci27fe55e2016-09-13 17:13:35 +02004507 unres = calloc(1, sizeof *unres);
4508 if (!unres) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004509 LOGMEM(ctx);
Radek Krejci0ec51da2016-12-14 16:42:03 +01004510 if (disabled) {
4511 /* set it back disabled */
4512 lys_set_disabled(module);
4513 }
Radek Krejci27fe55e2016-09-13 17:13:35 +02004514 return EXIT_FAILURE;
4515 }
4516 /* recursively make the module implemented */
4517 ((struct lys_module *)module)->implemented = 1;
4518 if (lys_set_implemented_recursion((struct lys_module *)module, unres)) {
4519 goto error;
4520 }
4521 /* process augments in submodules */
Radek Krejcid4c1d0f2017-01-19 16:11:38 +01004522 for (i = 0; i < module->inc_size && module->inc[i].submodule; ++i) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004523 for (j = 0; j < module->inc[i].submodule->augment_size; j++) {
4524 /* apply augment */
Michal Vasko44ab1462017-05-18 13:18:36 +02004525 assert(module->inc[i].submodule->augment[j].target);
4526 if (apply_aug(&module->inc[i].submodule->augment[j], unres)) {
Radek Krejci27fe55e2016-09-13 17:13:35 +02004527 goto error;
4528 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004529 }
Radek Krejci9e6af732017-04-27 14:40:25 +02004530
4531 /* identities */
4532 for (j = 0; j < module->inc[i].submodule->ident_size; j++) {
4533 for (k = 0; k < module->inc[i].submodule->ident[j].base_size; k++) {
4534 resolve_identity_backlink_update(&module->inc[i].submodule->ident[j],
4535 module->inc[i].submodule->ident[j].base[k]);
4536 }
4537 }
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004538 }
Radek Krejcidf46e222016-11-08 11:57:37 +01004539 /* try again resolve augments in other modules possibly augmenting this one,
4540 * since we have just enabled it
4541 */
Radek Krejci27fe55e2016-09-13 17:13:35 +02004542 /* resolve rest of unres items */
4543 if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) {
4544 goto error;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004545 }
Michal Vasko44ab1462017-05-18 13:18:36 +02004546 unres_schema_free(NULL, &unres, 0);
Michal Vasko26055752016-05-03 11:36:31 +02004547
Radek Krejci29eac3d2017-06-01 16:50:02 +02004548 /* reflect implemented flag in submodules */
4549 for (i = 0; i < module->inc_size; i++) {
4550 module->inc[i].submodule->implemented = 1;
4551 }
4552
Michal Vaskobe136f62017-09-21 12:08:39 +02004553 LOGVRB("Module \"%s%s%s\" now implemented.", module->name, (module->rev_size ? "@" : ""),
4554 (module->rev_size ? module->rev[0].date : ""));
Michal Vasko26055752016-05-03 11:36:31 +02004555 return EXIT_SUCCESS;
Radek Krejci27fe55e2016-09-13 17:13:35 +02004556
4557error:
Radek Krejci0ec51da2016-12-14 16:42:03 +01004558 if (disabled) {
4559 /* set it back disabled */
4560 lys_set_disabled(module);
4561 }
4562
Radek Krejci27fe55e2016-09-13 17:13:35 +02004563 ((struct lys_module *)module)->implemented = 0;
Michal Vasko44ab1462017-05-18 13:18:36 +02004564 unres_schema_free((struct lys_module *)module, &unres, 1);
Radek Krejci27fe55e2016-09-13 17:13:35 +02004565 return EXIT_FAILURE;
Michal Vasko9eb6dd02016-05-02 14:52:40 +02004566}
4567
4568void
4569lys_submodule_module_data_free(struct lys_submodule *submodule)
4570{
4571 struct lys_node *next, *elem;
4572
4573 /* remove parsed data */
4574 LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) {
4575 if (elem->module == (struct lys_module *)submodule) {
4576 lys_node_free(elem, NULL, 0);
4577 }
4578 }
4579}
Radek Krejcia1c33bf2016-09-07 12:38:49 +02004580
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004581API char *
Michal Vasko395b0a02018-01-22 09:36:20 +01004582lys_path(const struct lys_node *node, int options)
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004583{
Michal Vasko53b7da02018-02-13 15:28:42 +01004584 char *buf = NULL;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004585
4586 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004587 LOGARG;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004588 return NULL;
4589 }
4590
Michal Vasko53b7da02018-02-13 15:28:42 +01004591 if (ly_vlog_build_path(LY_VLOG_LYS, node, &buf, !options)) {
Michal Vasko59631dd2017-10-02 11:56:11 +02004592 return NULL;
4593 }
4594
Michal Vasko53b7da02018-02-13 15:28:42 +01004595 return buf;
Radek Krejci0a0b1fc2016-10-18 15:57:37 +02004596}
Radek Krejci9ad23f42016-10-31 15:46:52 +01004597
Michal Vasko50576712017-07-28 12:28:33 +02004598API char *
4599lys_data_path(const struct lys_node *node)
4600{
Michal Vasko53b7da02018-02-13 15:28:42 +01004601 char *result = NULL, buf[1024];
Michal Vasko50576712017-07-28 12:28:33 +02004602 int i, used;
4603 struct ly_set *set;
4604 const struct lys_module *prev_mod;
4605
4606 if (!node) {
Michal Vasko53b7da02018-02-13 15:28:42 +01004607 LOGARG;
Michal Vasko50576712017-07-28 12:28:33 +02004608 return NULL;
4609 }
4610
Michal Vasko50576712017-07-28 12:28:33 +02004611 set = ly_set_new();
Michal Vasko53b7da02018-02-13 15:28:42 +01004612 LY_CHECK_ERR_GOTO(!set, LOGMEM(node->module->ctx), cleanup);
Michal Vasko50576712017-07-28 12:28:33 +02004613
4614 while (node) {
4615 ly_set_add(set, (void *)node, 0);
4616 do {
4617 node = lys_parent(node);
4618 } while (node && (node->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
4619 }
4620
4621 prev_mod = NULL;
4622 used = 0;
4623 for (i = set->number - 1; i > -1; --i) {
4624 node = set->set.s[i];
4625 used += sprintf(buf + used, "/%s%s%s", (lys_node_module(node) == prev_mod ? "" : lys_node_module(node)->name),
4626 (lys_node_module(node) == prev_mod ? "" : ":"), node->name);
4627 prev_mod = lys_node_module(node);
4628 }
4629
4630 result = strdup(buf);
Michal Vasko53b7da02018-02-13 15:28:42 +01004631 LY_CHECK_ERR_GOTO(!result, LOGMEM(node->module->ctx), cleanup);
Michal Vasko50576712017-07-28 12:28:33 +02004632
Michal Vasko53b7da02018-02-13 15:28:42 +01004633cleanup:
Michal Vasko50576712017-07-28 12:28:33 +02004634 ly_set_free(set);
Michal Vasko50576712017-07-28 12:28:33 +02004635 return result;
4636}
4637
Michal Vaskobb520442017-05-23 10:55:18 +02004638struct lys_node_augment *
4639lys_getnext_target_aug(struct lys_node_augment *last, const struct lys_module *mod, const struct lys_node *aug_target)
4640{
4641 int i, j, last_found;
4642
4643 if (!last) {
4644 last_found = 1;
4645 } else {
4646 last_found = 0;
4647 }
4648
4649 /* search module augments */
4650 for (i = 0; i < mod->augment_size; ++i) {
4651 if (!mod->augment[i].target) {
4652 /* still unresolved, skip */
4653 continue;
4654 }
4655
4656 if (mod->augment[i].target == aug_target) {
4657 if (last_found) {
4658 /* next match after last */
4659 return &mod->augment[i];
4660 }
4661
4662 if (&mod->augment[i] == last) {
4663 last_found = 1;
4664 }
4665 }
4666 }
4667
4668 /* search submodule augments */
4669 for (i = 0; i < mod->inc_size; ++i) {
4670 for (j = 0; j < mod->inc[i].submodule->augment_size; ++j) {
4671 if (!mod->inc[i].submodule->augment[j].target) {
4672 continue;
4673 }
4674
4675 if (mod->inc[i].submodule->augment[j].target == aug_target) {
4676 if (last_found) {
4677 /* next match after last */
4678 return &mod->inc[i].submodule->augment[j];
4679 }
4680
4681 if (&mod->inc[i].submodule->augment[j] == last) {
4682 last_found = 1;
4683 }
4684 }
4685 }
4686 }
4687
4688 return NULL;
4689}
4690
Michal Vasko50576712017-07-28 12:28:33 +02004691API struct ly_set *
4692lys_find_path(const struct lys_module *cur_module, const struct lys_node *cur_node, const char *path)
4693{
4694 struct ly_set *ret;
4695 int rc;
4696
4697 if ((!cur_module && !cur_node) || !path) {
4698 return NULL;
4699 }
4700
4701 rc = resolve_schema_nodeid(path, cur_node, cur_module, &ret, 1, 1);
4702 if (rc == -1) {
4703 return NULL;
4704 }
4705
4706 return ret;
4707}
4708
Radek Krejci8d6b7422017-02-03 14:42:13 +01004709static void
4710lys_extcomplex_free_str(struct ly_ctx *ctx, struct lys_ext_instance_complex *ext, LY_STMT stmt)
4711{
4712 struct lyext_substmt *info;
Radek Krejcib71243e2017-02-08 16:20:08 +01004713 const char **str, ***a;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004714 int c;
4715
4716 str = lys_ext_complex_get_substmt(stmt, ext, &info);
4717 if (!str || !(*str)) {
4718 return;
4719 }
4720 if (info->cardinality >= LY_STMT_CARD_SOME) {
4721 /* we have array */
Radek Krejcib71243e2017-02-08 16:20:08 +01004722 a = (const char ***)str;
Radek Krejci56c80412017-02-09 10:44:16 +01004723 for (str = (*(const char ***)str), c = 0; str[c]; c++) {
4724 lydict_remove(ctx, str[c]);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004725 }
Radek Krejci56c80412017-02-09 10:44:16 +01004726 free(a[0]);
4727 if (stmt == LY_STMT_BELONGSTO) {
4728 for (str = a[1], c = 0; str[c]; c++) {
4729 lydict_remove(ctx, str[c]);
4730 }
4731 free(a[1]);
PavolVican99c70722017-02-18 17:25:52 +01004732 } else if (stmt == LY_STMT_ARGUMENT) {
4733 free(a[1]);
Radek Krejci56c80412017-02-09 10:44:16 +01004734 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004735 } else {
Radek Krejci56c80412017-02-09 10:44:16 +01004736 lydict_remove(ctx, str[0]);
4737 if (stmt == LY_STMT_BELONGSTO) {
4738 lydict_remove(ctx, str[1]);
4739 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004740 }
4741}
4742void
Radek Krejci5138e9f2017-04-12 13:10:46 +02004743lys_extension_instances_free(struct ly_ctx *ctx, struct lys_ext_instance **e, unsigned int size,
4744 void (*private_destructor)(const struct lys_node *node, void *priv))
Radek Krejci8d6b7422017-02-03 14:42:13 +01004745{
Radek Krejcif8d05c22017-02-10 15:33:35 +01004746 unsigned int i, j, k;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004747 struct lyext_substmt *substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004748 void **pp, **start;
Radek Krejcif95b6292017-02-13 15:57:37 +01004749 struct lys_node *siter, *snext;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004750
4751#define EXTCOMPLEX_FREE_STRUCT(STMT, TYPE, FUNC, FREE, ARGS...) \
Radek Krejcib84686f2017-02-09 16:04:55 +01004752 pp = lys_ext_complex_get_substmt(STMT, (struct lys_ext_instance_complex *)e[i], NULL); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004753 if (!pp || !(*pp)) { break; } \
Radek Krejcib84686f2017-02-09 16:04:55 +01004754 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */ \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004755 for (start = pp = *pp; *pp; pp++) { \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004756 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004757 if (FREE) { free(*pp); } \
4758 } \
4759 free(start); \
4760 } else { /* single item */ \
Radek Krejci5138e9f2017-04-12 13:10:46 +02004761 FUNC(ctx, (TYPE *)(*pp), ##ARGS, private_destructor); \
Radek Krejci8d6b7422017-02-03 14:42:13 +01004762 if (FREE) { free(*pp); } \
4763 }
4764
4765 if (!size || !e || !(*e)) {
4766 return;
4767 }
4768
4769 for (i = 0; i < size; i++) {
4770 if (!e[i]) {
4771 continue;
4772 }
4773
4774 if (e[i]->flags & (LYEXT_OPT_INHERIT)) {
4775 /* no free, this is just a shadow copy of the original extension instance */
4776 } else {
4777 if (e[i]->flags & (LYEXT_OPT_YANG)) {
4778 free(e[i]->def); /* remove name of instance extension */
PavolVican19dc6152017-02-06 12:04:15 +01004779 e[i]->def = NULL;
PavolVicandb0e8172017-02-20 00:46:09 +01004780 yang_free_ext_data((struct yang_ext_substmt *)e[i]->parent); /* remove backup part of yang file */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004781 }
Radek Krejci5138e9f2017-04-12 13:10:46 +02004782 /* remove private object */
4783 if (e[i]->priv && private_destructor) {
4784 private_destructor((struct lys_node*)e[i], e[i]->priv);
4785 }
4786 lys_extension_instances_free(ctx, e[i]->ext, e[i]->ext_size, private_destructor);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004787 lydict_remove(ctx, e[i]->arg_value);
4788 }
4789
fanchanghu8d86f6b2017-06-10 12:49:54 +08004790 if (e[i]->def && e[i]->def->plugin && e[i]->def->plugin->type == LYEXT_COMPLEX
4791 && ((e[i]->flags & LYEXT_OPT_CONTENT) == 0)) {
Radek Krejcifebdad72017-02-06 11:35:51 +01004792 substmt = ((struct lys_ext_instance_complex *)e[i])->substmt;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004793 for (j = 0; substmt[j].stmt; j++) {
4794 switch(substmt[j].stmt) {
4795 case LY_STMT_DESCRIPTION:
4796 case LY_STMT_REFERENCE:
4797 case LY_STMT_UNITS:
Radek Krejcib71243e2017-02-08 16:20:08 +01004798 case LY_STMT_ARGUMENT:
4799 case LY_STMT_DEFAULT:
4800 case LY_STMT_ERRTAG:
4801 case LY_STMT_ERRMSG:
4802 case LY_STMT_PREFIX:
4803 case LY_STMT_NAMESPACE:
4804 case LY_STMT_PRESENCE:
4805 case LY_STMT_REVISIONDATE:
4806 case LY_STMT_KEY:
4807 case LY_STMT_BASE:
4808 case LY_STMT_BELONGSTO:
4809 case LY_STMT_CONTACT:
4810 case LY_STMT_ORGANIZATION:
4811 case LY_STMT_PATH:
Radek Krejci8d6b7422017-02-03 14:42:13 +01004812 lys_extcomplex_free_str(ctx, (struct lys_ext_instance_complex *)e[i], substmt[j].stmt);
4813 break;
4814 case LY_STMT_TYPE:
4815 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPE, struct lys_type, lys_type_free, 1);
4816 break;
Radek Krejci63fc0962017-02-15 13:20:18 +01004817 case LY_STMT_TYPEDEF:
4818 EXTCOMPLEX_FREE_STRUCT(LY_STMT_TYPEDEF, struct lys_tpdf, lys_tpdf_free, 1);
4819 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004820 case LY_STMT_IFFEATURE:
Frank Rimplerc4db1c72017-09-12 12:56:39 +00004821 EXTCOMPLEX_FREE_STRUCT(LY_STMT_IFFEATURE, struct lys_iffeature, lys_iffeature_free, 0, 1, 0);
Radek Krejci8d6b7422017-02-03 14:42:13 +01004822 break;
Radek Krejci5496fae2017-02-10 13:26:48 +01004823 case LY_STMT_MAX:
4824 case LY_STMT_MIN:
4825 case LY_STMT_POSITION:
PavolVican2ed9f4e2017-02-16 00:08:45 +01004826 case LY_STMT_VALUE:
Radek Krejcif8d05c22017-02-10 15:33:35 +01004827 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
Radek Krejci716cd7a2017-02-15 12:23:41 +01004828 if (substmt[j].cardinality >= LY_STMT_CARD_SOME && *pp) {
Radek Krejcif8d05c22017-02-10 15:33:35 +01004829 for(k = 0; ((uint32_t**)(*pp))[k]; k++) {
4830 free(((uint32_t**)(*pp))[k]);
4831 }
4832 }
4833 free(*pp);
4834 break;
4835 case LY_STMT_DIGITS:
Radek Krejcib84686f2017-02-09 16:04:55 +01004836 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4837 /* free the array */
4838 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4839 free(*pp);
4840 }
Radek Krejci8d6b7422017-02-03 14:42:13 +01004841 break;
Radek Krejci37f9ba32017-02-10 16:50:35 +01004842 case LY_STMT_MODULE:
4843 /* modules are part of the context, so they will be freed there */
4844 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) {
4845 /* free the array */
4846 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4847 free(*pp);
4848 }
4849 break;
Radek Krejcif95b6292017-02-13 15:57:37 +01004850 case LY_STMT_ACTION:
Radek Krejcib31762b2017-02-15 10:48:42 +01004851 case LY_STMT_ANYDATA:
4852 case LY_STMT_ANYXML:
4853 case LY_STMT_CASE:
4854 case LY_STMT_CHOICE:
4855 case LY_STMT_CONTAINER:
4856 case LY_STMT_GROUPING:
4857 case LY_STMT_INPUT:
4858 case LY_STMT_LEAF:
4859 case LY_STMT_LEAFLIST:
4860 case LY_STMT_LIST:
4861 case LY_STMT_NOTIFICATION:
4862 case LY_STMT_OUTPUT:
4863 case LY_STMT_RPC:
4864 case LY_STMT_USES:
Radek Krejcif95b6292017-02-13 15:57:37 +01004865 pp = (void**)&((struct lys_ext_instance_complex *)e[i])->content[substmt[j].offset];
4866 LY_TREE_FOR_SAFE((struct lys_node *)(*pp), snext, siter) {
4867 lys_node_free(siter, NULL, 0);
4868 }
Radek Krejcib31762b2017-02-15 10:48:42 +01004869 *pp = NULL;
Radek Krejcif95b6292017-02-13 15:57:37 +01004870 break;
Radek Krejcic3f1b6f2017-02-15 10:51:10 +01004871 case LY_STMT_UNIQUE:
4872 pp = lys_ext_complex_get_substmt(LY_STMT_UNIQUE, (struct lys_ext_instance_complex *)e[i], NULL);
4873 if (!pp || !(*pp)) {
4874 break;
4875 }
4876 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4877 for (start = pp = *pp; *pp; pp++) {
4878 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4879 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4880 }
4881 free((*(struct lys_unique**)pp)->expr);
4882 free(*pp);
4883 }
4884 free(start);
4885 } else { /* single item */
4886 for (k = 0; k < (*(struct lys_unique**)pp)->expr_size; k++) {
4887 lydict_remove(ctx, (*(struct lys_unique**)pp)->expr[k]);
4888 }
4889 free((*(struct lys_unique**)pp)->expr);
4890 free(*pp);
4891 }
4892 break;
Radek Krejciaa9c5202017-02-15 16:10:14 +01004893 case LY_STMT_LENGTH:
4894 case LY_STMT_MUST:
4895 case LY_STMT_PATTERN:
4896 case LY_STMT_RANGE:
4897 EXTCOMPLEX_FREE_STRUCT(substmt[j].stmt, struct lys_restr, lys_restr_free, 1);
4898 break;
Radek Krejcic5cc5302017-02-16 10:07:46 +01004899 case LY_STMT_WHEN:
4900 EXTCOMPLEX_FREE_STRUCT(LY_STMT_WHEN, struct lys_when, lys_when_free, 0);
4901 break;
Radek Krejci7417a082017-02-16 11:07:59 +01004902 case LY_STMT_REVISION:
4903 pp = lys_ext_complex_get_substmt(LY_STMT_REVISION, (struct lys_ext_instance_complex *)e[i], NULL);
4904 if (!pp || !(*pp)) {
4905 break;
4906 }
4907 if (substmt[j].cardinality >= LY_STMT_CARD_SOME) { /* process array */
4908 for (start = pp = *pp; *pp; pp++) {
4909 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4910 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4911 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004912 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004913 free(*pp);
4914 }
4915 free(start);
4916 } else { /* single item */
4917 lydict_remove(ctx, (*(struct lys_revision**)pp)->dsc);
4918 lydict_remove(ctx, (*(struct lys_revision**)pp)->ref);
4919 lys_extension_instances_free(ctx, (*(struct lys_revision**)pp)->ext,
Radek Krejci5138e9f2017-04-12 13:10:46 +02004920 (*(struct lys_revision**)pp)->ext_size, private_destructor);
Radek Krejci7417a082017-02-16 11:07:59 +01004921 free(*pp);
4922 }
4923 break;
Radek Krejci8d6b7422017-02-03 14:42:13 +01004924 default:
Radek Krejcib84686f2017-02-09 16:04:55 +01004925 /* nothing to free */
Radek Krejci8d6b7422017-02-03 14:42:13 +01004926 break;
4927 }
4928 }
4929 }
4930
4931 free(e[i]);
4932 }
4933 free(e);
4934
4935#undef EXTCOMPLEX_FREE_STRUCT
4936}