blob: bc91983b05c3aea84dbd27d303f2936d43daa98d [file] [log] [blame]
Radek Krejcic6704c82015-10-06 11:12:45 +02001
Radek Krejcida04f4a2015-05-21 12:54:09 +02002/**
Michal Vasko2d162e12015-09-24 14:33:29 +02003 * @file tree_schema.c
Radek Krejcida04f4a2015-05-21 12:54:09 +02004 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko2d162e12015-09-24 14:33:29 +02005 * @brief Manipulation with libyang schema data structures
Radek Krejcida04f4a2015-05-21 12:54:09 +02006 *
7 * Copyright (c) 2015 CESNET, z.s.p.o.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of the Company nor the names of its contributors
19 * may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020022#define _GNU_SOURCE
Radek Krejcida04f4a2015-05-21 12:54:09 +020023
Radek Krejci812b10a2015-05-28 16:48:25 +020024#include <assert.h>
Radek Krejci5a988152015-07-15 11:16:26 +020025#include <ctype.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020026#include <stdlib.h>
27#include <sys/mman.h>
28#include <sys/stat.h>
Radek Krejci8bc9ca02015-06-04 15:52:46 +020029#include <string.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020030
31#include "common.h"
32#include "context.h"
Radek Krejci3045cf32015-05-28 10:58:52 +020033#include "parser.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020034#include "resolve.h"
Michal Vaskofc5744d2015-10-22 12:09:34 +020035#include "xml_internal.h"
Radek Krejci8bc9ca02015-06-04 15:52:46 +020036#include "tree_internal.h"
Radek Krejcieab784a2015-08-27 09:56:53 +020037#include "validation.h"
Radek Krejciefaeba32015-05-27 14:30:57 +020038
Michal Vaskoa76ee152015-08-17 15:38:22 +020039static const struct internal_modules int_mods = {
40 .modules = {
41 {"ietf-yang-types", "2013-07-15"},
42 {"ietf-inet-types", "2013-07-15"},
43 {"ietf-yang-library", "2015-07-03"}
44 },
45 .count = LY_INTERNAL_MODULE_COUNT
46};
47
Radek Krejci48061fb2015-08-05 15:41:07 +020048API struct lys_feature *
49lys_is_disabled(struct lys_node *node, int recursive)
50{
51 int i;
52
53check:
54 if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) {
55 /* input/output does not have if-feature, so skip them */
56
57 /* check local if-features */
58 for (i = 0; i < node->features_size; i++) {
59 if (!(node->features[i]->flags & LYS_FENABLED)) {
60 return node->features[i];
61 }
62 }
63 }
64
65 if (!recursive) {
66 return NULL;
67 }
68
69 /* go through parents */
70 if (node->nodetype == LYS_AUGMENT) {
71 /* go to parent actually means go to the target node */
72 node = ((struct lys_node_augment *)node)->target;
Radek Krejci48061fb2015-08-05 15:41:07 +020073 } else if (node->parent) {
74 node = node->parent;
Radek Krejci074bf852015-08-19 14:22:16 +020075 } else {
76 return NULL;
Radek Krejci48061fb2015-08-05 15:41:07 +020077 }
78
Radek Krejci074bf852015-08-19 14:22:16 +020079 if (recursive == 2) {
80 /* continue only if the node cannot have a data instance */
81 if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) {
82 return NULL;
83 }
84 }
85 goto check;
Radek Krejci48061fb2015-08-05 15:41:07 +020086}
87
Michal Vasko1dca6882015-10-22 14:29:42 +020088int
Michal Vasko165dc4a2015-10-23 09:44:27 +020089lys_get_sibling(struct lys_module *mod, struct lys_node *siblings, const char *mod_name, int mod_name_len,
90 const char *name, int nam_len, LYS_NODE type, struct lys_node **ret)
Michal Vasko1dca6882015-10-22 14:29:42 +020091{
92 struct lys_node *node, *old_siblings = NULL;
93 struct lys_module *prefix_mod, *cur_mod;
94 int in_submod;
Michal Vasko1dca6882015-10-22 14:29:42 +020095
96 assert(siblings && name);
Michal Vasko165dc4a2015-10-23 09:44:27 +020097 assert(!(type & (LYS_USES | LYS_GROUPING)));
Michal Vasko1dca6882015-10-22 14:29:42 +020098
99 /* find the beginning */
100 while (siblings->prev->next) {
101 siblings = siblings->prev;
102 }
103
104 /* fill the name length in case the caller is so indifferent */
105 if (!nam_len) {
106 nam_len = strlen(name);
107 }
108
109 /* we start with the module itself, submodules come later */
110 in_submod = 0;
111
112 /* set prefix_mod correctly */
113 if (mod_name) {
Michal Vaskob6729c62015-10-21 12:09:47 +0200114 prefix_mod = lys_get_import_module(siblings->module, NULL, 0, mod_name, mod_name_len);
Michal Vasko1dca6882015-10-22 14:29:42 +0200115 if (!prefix_mod) {
116 return -1;
117 }
118 cur_mod = prefix_mod;
119 /* it is not our module */
120 if (cur_mod != mod) {
121 old_siblings = siblings;
122 siblings = cur_mod->data;
123 }
124 } else {
125 if (mod) {
126 prefix_mod = mod;
127 } else {
128 prefix_mod = siblings->module;
129 }
130 if (prefix_mod->type) {
131 prefix_mod = ((struct lys_submodule *)prefix_mod)->belongsto;
132 }
133 cur_mod = prefix_mod;
134 }
135
136 while (1) {
137 /* try to find the node */
138 node = NULL;
139 while ((node = lys_getnext(node, siblings->parent, cur_mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) {
140 if (!type || (node->nodetype & type)) {
141 /* module check */
142 if (!node->module->type) {
143 if (cur_mod != node->module) {
144 continue;
145 }
146 } else {
147 /* both are submodules */
148 if (cur_mod->type) {
149 if (cur_mod != node->module) {
150 continue;
151 }
152 } else {
153 if (cur_mod != ((struct lys_submodule *)node->module)->belongsto) {
154 continue;
155 }
156 }
157 }
158
159 /* direct name check */
160 if ((node->name == name) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) {
161 if (ret) {
162 *ret = node;
163 }
164 return EXIT_SUCCESS;
165 }
166 }
167 }
168
169 /* The original siblings may be valid,
170 * it's a special case when we're looking
171 * for a node from an augment.
172 */
Michal Vasko165dc4a2015-10-23 09:44:27 +0200173 if ((type & LYS_AUGMENT) && old_siblings) {
Michal Vasko1dca6882015-10-22 14:29:42 +0200174 siblings = old_siblings;
175 old_siblings = NULL;
176 continue;
177 }
178
179 /* we're not top-level, search ended */
180 if (siblings->parent) {
181 break;
182 }
183
184 /* let's try the submodules */
185 if (in_submod == prefix_mod->inc_size) {
186 break;
187 }
188 cur_mod = (struct lys_module *)prefix_mod->inc[in_submod].submodule;
189 siblings = cur_mod->data;
190 ++in_submod;
191 }
192
193 return EXIT_FAILURE;
194}
195
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200196int
197lys_get_data_sibling(struct lys_module *mod, struct lys_node *siblings, const char *name, LYS_NODE type,
198 struct lys_node **ret)
199{
200 struct lys_node *node;
201 struct lys_module *cur_mod;
202 int in_submod;
203
204 assert(siblings && name);
205 assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT)));
206
207 /* find the beginning */
208 while (siblings->prev->next) {
209 siblings = siblings->prev;
210 }
211
212 /* we start with the module itself, submodules come later */
213 in_submod = 0;
214
215 if (!mod) {
216 mod = siblings->module;
217 }
218 cur_mod = mod;
219
220 while (1) {
221 /* try to find the node */
222 node = NULL;
223 while ((node = lys_getnext(node, siblings->parent, cur_mod, 0))) {
224 if (!type || (node->nodetype & type)) {
225 /* module check */
226 if (!node->module->type) {
227 if (cur_mod != node->module) {
228 continue;
229 }
230 } else {
231 /* both are submodules */
232 if (cur_mod->type) {
233 if (cur_mod != node->module) {
234 continue;
235 }
236 } else {
237 if (cur_mod != ((struct lys_submodule *)node->module)->belongsto) {
238 continue;
239 }
240 }
241 }
242
243 /* direct name check */
244 if ((node->name == name) || !strcmp(node->name, name)) {
245 if (ret) {
246 *ret = node;
247 }
248 return EXIT_SUCCESS;
249 }
250 }
251 }
252
253 /* we're not top-level, search ended */
254 if (siblings->parent) {
255 break;
256 }
257
258 /* let's try the submodules */
259 if (in_submod == mod->inc_size) {
260 break;
261 }
262 cur_mod = (struct lys_module *)mod->inc[in_submod].submodule;
263 siblings = cur_mod->data;
264 ++in_submod;
265 }
266
267 return EXIT_FAILURE;
268}
269
Michal Vasko1dca6882015-10-22 14:29:42 +0200270API struct lys_node *
Radek Krejci8bc87f62015-09-02 16:19:05 +0200271lys_getnext(struct lys_node *last, struct lys_node *parent, struct lys_module *module, int options)
Radek Krejci7f40ce32015-08-12 20:38:46 +0200272{
273 struct lys_node *next;
274
Radek Krejci8bc87f62015-09-02 16:19:05 +0200275 if (!last) {
276 /* first call */
277
278 /* get know where to start */
279 if (parent) {
280 /* schema subtree */
281 next = last = parent->child;
282 } else {
283 /* top level data */
284 assert(module);
285 next = last = module->data;
286 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200287 } else {
Radek Krejci8bc87f62015-09-02 16:19:05 +0200288 /* continue after the last returned value */
289 next = last->next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200290 }
291
292repeat:
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
300 while (!next) {
Michal Vasko7c386e72015-10-07 15:13:33 +0200301 if (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;
308 }
309
310 switch (next->nodetype) {
Michal Vaskob6eedf02015-10-22 16:07:03 +0200311 case LYS_INPUT:
312 case LYS_OUTPUT:
313 if (options & LYS_GETNEXT_WITHINOUT) {
314 return next;
315 } else {
316 next = next->child;
317 goto repeat;
318 }
319 break;
320
Michal Vaskoa5835e92015-10-20 15:07:39 +0200321 case LYS_CASE:
Michal Vasko1dca6882015-10-22 14:29:42 +0200322 if (options & LYS_GETNEXT_WITHCASE) {
323 return next;
Michal Vaskob6eedf02015-10-22 16:07:03 +0200324 } else {
325 next = next->child;
326 goto repeat;
Michal Vasko1dca6882015-10-22 14:29:42 +0200327 }
Michal Vaskob6eedf02015-10-22 16:07:03 +0200328 break;
329
Michal Vasko1dca6882015-10-22 14:29:42 +0200330 case LYS_USES:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200331 /* go into */
332 next = next->child;
333 goto repeat;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200334
Radek Krejcidfcae7d2015-10-20 17:13:01 +0200335 case LYS_RPC:
336 case LYS_NOTIF:
Radek Krejci8bc87f62015-09-02 16:19:05 +0200337 case LYS_CONTAINER:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200338 case LYS_LEAF:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200339 case LYS_ANYXML:
Radek Krejci14a11a62015-08-17 17:27:38 +0200340 case LYS_LIST:
341 case LYS_LEAFLIST:
Radek Krejci7f40ce32015-08-12 20:38:46 +0200342 return next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200343
344 case LYS_CHOICE:
345 if (options & LYS_GETNEXT_WITHCHOICE) {
346 return next;
347 } else {
348 /* go into */
349 next = next->child;
350 goto repeat;
351 }
352 break;
353
Radek Krejci7f40ce32015-08-12 20:38:46 +0200354 default:
355 /* we should not be here */
356 return NULL;
357 }
Radek Krejci8bc87f62015-09-02 16:19:05 +0200358
359
360}
361
362static struct lys_node *
363check_mand_getnext(struct lys_node *last, struct lys_node *parent)
364{
365 struct lys_node *next;
Radek Krejci8bc87f62015-09-02 16:19:05 +0200366 next = lys_getnext(last, parent, NULL, LYS_GETNEXT_WITHCHOICE);
367
Radek Krejci4b6c2112015-10-06 12:48:34 +0200368repeat:
Radek Krejci8bc87f62015-09-02 16:19:05 +0200369 if (next && next->nodetype == LYS_CONTAINER) {
370 if (((struct lys_node_container *)next)->presence) {
371 /* mandatory elements under the non-existing presence
372 * container are not mandatory - 7.6.5, rule 1 */
373 next = next->next;
374 } else {
375 /* go into */
376 next = next->child;
377 }
378 goto repeat;
379 }
380
381 return next;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200382}
383
384static struct lys_node *
385check_mand_check(struct lys_node *node, struct lys_node *stop, struct lyd_node *data)
386{
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200387 struct lys_node *siter = NULL, *parent = NULL;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200388 struct lyd_node *diter = NULL;
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200389 struct lyd_set *set = NULL;
390 unsigned int i;
Radek Krejci14a11a62015-08-17 17:27:38 +0200391 uint32_t minmax;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200392
393 if (node->flags & LYS_MAND_TRUE) {
394 switch (node->nodetype) {
395 case LYS_LEAF:
396 case LYS_ANYXML:
397 case LYS_CHOICE:
398 if (node->parent->nodetype == LYS_CASE) {
399 /* 7.6.5, rule 2 */
400 /* 7.9.4, rule 1 */
401 if (node->parent->parent->parent == data->schema) {
402 /* the only case the node's siblings can exist is that the
403 * data node passed originaly to ly_check_mandatory()
404 * had this choice as a child
405 */
406 /* try to find the node's siblings in data */
407 LY_TREE_FOR(data->child, diter) {
408 LY_TREE_FOR(node->parent->child, siter) {
409 if (siter == diter->schema) {
410 /* some sibling exists, rule applies */
411 break;
412 }
413 }
414 if (siter) {
415 break;
416 }
417 }
418 }
419 if (!siter) {
420 /* no sibling exists */
421 return NULL;
422 }
423 } else {
424 for(parent = node->parent; parent != stop; parent = parent->parent) {
425 if (parent->nodetype != LYS_CONTAINER) {
426 /* 7.6.5, rule 1, checking presence is not needed
427 * since it is done in check_mand_getnext()
428 */
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200429 lyd_set_free(set);
Radek Krejci7f40ce32015-08-12 20:38:46 +0200430 return NULL;
431 }
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200432 /* add the parent to the list for searching in data tree */
433 if (!set) {
434 set = lyd_set_new();
435 }
436 lyd_set_add(set, (struct lyd_node *)parent);
Radek Krejci7f40ce32015-08-12 20:38:46 +0200437 }
438 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200439
440 /* search for instance */
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200441 if (set) {
442 for (i = 0; i < set->number; i++) {
443 LY_TREE_FOR(data->child, diter) {
444 if (diter->schema == (struct lys_node *)(set->set[i])) {
445 break;
446 }
447 }
448 if (!diter) {
449 /* instance not found */
450 node = (struct lys_node *)(set->set[i]);
451 lyd_set_free(set);
452 return node;
453 }
454 data = diter;
455 }
456 lyd_set_free(set);
457 }
458
Radek Krejci14a11a62015-08-17 17:27:38 +0200459 LY_TREE_FOR(data->child, diter) {
460 if (diter->schema == node) {
461 return NULL;
462 }
463 }
Radek Krejci50c5b0e2015-08-21 15:43:45 +0200464
Radek Krejci14a11a62015-08-17 17:27:38 +0200465 /* instance not found */
Radek Krejci7f40ce32015-08-12 20:38:46 +0200466 /* 7.6.5, rule 3 (or 2) */
467 /* 7.9.4, rule 2 */
468 return node;
469 default:
470 /* error */
471 break;
472 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200473 } else if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
474 /* search for number of instances */
475 minmax = 0;
476 LY_TREE_FOR(data->child, diter) {
477 if (diter->schema == node) {
478 minmax++;
479 }
480 }
481
482 /* check the specified constraints */
483 if (node->nodetype == LYS_LIST) {
484 if (((struct lys_node_list *)node)->min && minmax < ((struct lys_node_list *)node)->min) {
485 return node;
486 }
487
488 if (((struct lys_node_list *)node)->max && minmax > ((struct lys_node_list *)node)->max) {
489 return node;
490 }
491 } else if (node->nodetype == LYS_LEAFLIST) {
492 if (((struct lys_node_leaflist *)node)->min && minmax < ((struct lys_node_leaflist *)node)->min) {
493 return node;
494 }
495
496 if (((struct lys_node_leaflist *)node)->max && minmax > ((struct lys_node_leaflist *)node)->max) {
497 return node;
498 }
499 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200500 }
501
502 return NULL;
503}
504
505struct lys_node *
506ly_check_mandatory(struct lyd_node *data)
507{
Radek Krejci14a11a62015-08-17 17:27:38 +0200508 struct lys_node *siter, *saux, *saux2, *result, *parent = NULL, *parent2;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200509 struct lyd_node *diter;
510 int found;
511
512 siter = data->schema->child;
513
514repeat:
515 while (siter) {
Radek Krejci074bf852015-08-19 14:22:16 +0200516 if (lys_is_disabled(siter, 2)) {
517 siter = siter->next;
518 continue;
519 }
520
Radek Krejci7f40ce32015-08-12 20:38:46 +0200521 switch (siter->nodetype) {
522 case LYS_CONTAINER:
523 case LYS_LEAF:
524 case LYS_ANYXML:
Radek Krejci14a11a62015-08-17 17:27:38 +0200525 case LYS_LIST:
526 case LYS_LEAFLIST:
527 /* check if there is some mandatory node; first test the siter itself ... */
Radek Krejci7f40ce32015-08-12 20:38:46 +0200528 result = check_mand_check(siter, siter->parent, data);
529 if (result) {
530 return result;
531 }
532 /* ... and then the subtree */
Radek Krejci14a11a62015-08-17 17:27:38 +0200533 if (parent && siter->nodetype == LYS_CONTAINER && !((struct lys_node_container *)siter)->presence) {
534 saux = NULL;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200535 while ((saux = check_mand_getnext(saux, siter))) {
536 result = check_mand_check(saux, siter, data);
537 if (result) {
538 return result;
539 }
540 }
541 }
542 siter = siter->next;
543 break;
544 case LYS_CHOICE:
Radek Krejci14a11a62015-08-17 17:27:38 +0200545 /* search for instance */
546 saux = siter;
547 siter = siter->child;
548 found = 0;
549 parent2 = NULL;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200550repeat_choice:
Radek Krejci074bf852015-08-19 14:22:16 +0200551 while (siter) {
552 if (lys_is_disabled(siter, 2)) {
553 siter = siter->next;
554 continue;
555 }
556
Radek Krejci14a11a62015-08-17 17:27:38 +0200557 switch (siter->nodetype) {
558 case LYS_CONTAINER:
559 case LYS_LEAF:
560 case LYS_LEAFLIST:
561 case LYS_LIST:
562 case LYS_ANYXML:
563 LY_TREE_FOR(data->child, diter) {
564 if (diter->schema == siter) {
Radek Krejci7f40ce32015-08-12 20:38:46 +0200565 break;
566 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200567 }
568 if (diter) {
569 /* got instance */
570 /* check presence of mandatory siblings */
Radek Krejci65d0a652015-08-27 13:09:42 +0200571 if (parent2 && parent2->nodetype == LYS_CASE) {
Radek Krejci14a11a62015-08-17 17:27:38 +0200572 saux2 = NULL;
Radek Krejci37bda002015-08-27 11:23:56 +0200573 while ((saux2 = check_mand_getnext(saux2, parent2))) {
574 result = check_mand_check(saux2, parent2, data);
Radek Krejci14a11a62015-08-17 17:27:38 +0200575 if (result) {
576 return result;
577 }
578 }
579 }
580 siter = parent2 = NULL;
581 found = 1;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200582 break;
583 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200584 siter = siter->next;
585 break;
586 case LYS_CASE:
587 case LYS_CHOICE:
588 case LYS_USES:
589 /* go into */
Radek Krejci37bda002015-08-27 11:23:56 +0200590 if (!parent2) {
591 parent2 = siter;
592 }
Radek Krejci14a11a62015-08-17 17:27:38 +0200593 siter = siter->child;
594 break;
595 case LYS_AUGMENT:
596 case LYS_GROUPING:
597 /* skip */
598 siter = siter->next;
599 break;
600 default:
601 /* error */
602 break;
Radek Krejci7f40ce32015-08-12 20:38:46 +0200603 }
Radek Krejci7f40ce32015-08-12 20:38:46 +0200604 }
605
Radek Krejci14a11a62015-08-17 17:27:38 +0200606 if (parent2) {
607 siter = parent2->next;
608 if (parent2->parent == saux) {
609 parent2 = NULL;
610 } else {
611 parent2 = parent2->parent;
612 }
613 goto repeat_choice;
614 }
615
Radek Krejci074bf852015-08-19 14:22:16 +0200616 if (!found && (saux->flags & LYS_MAND_TRUE)) {
Radek Krejci14a11a62015-08-17 17:27:38 +0200617 return saux;
618 }
619
620 /* go to next */
621 siter = saux->next;
622
Radek Krejci7f40ce32015-08-12 20:38:46 +0200623 break;
624 case LYS_USES:
625 case LYS_CASE:
626 /* go into */
627 parent = siter;
628 siter = siter->child;
629 break;
630 default:
631 /* can ignore, go to next */
632 siter = siter->next;
633 break;
634 }
635 }
636
637 if (parent) {
638 siter = parent->next;
639 if (parent->parent == data->schema) {
640 parent = NULL;
641 } else {
642 parent = parent->parent;
643 }
644 goto repeat;
645 }
646
647 return NULL;
648}
649
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200650void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200651lys_node_unlink(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200652{
Radek Krejci76512572015-08-04 09:47:08 +0200653 struct lys_node *parent, *first;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200654
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200655 if (!node) {
656 return;
657 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200658
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200659 /* unlink from data model if necessary */
660 if (node->module) {
661 if (node->module->data == node) {
662 node->module->data = node->next;
663 }
664 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200665
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200666 /* store pointers to important nodes */
667 parent = node->parent;
Michal Vasko3a9943b2015-09-23 11:33:50 +0200668 if (parent && (parent->nodetype == LYS_AUGMENT)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200669 /* handle augments - first, unlink it from the augment parent ... */
670 if (parent->child == node) {
671 parent->child = node->next;
672 }
673 /* and then continue with the target parent */
Radek Krejci76512572015-08-04 09:47:08 +0200674 parent = ((struct lys_node_augment *)parent)->target;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200675 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200676
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200677 /* unlink from parent */
678 if (parent) {
679 if (parent->child == node) {
680 parent->child = node->next;
681 }
682 node->parent = NULL;
683 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200684
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200685 /* unlink from siblings */
686 if (node->prev == node) {
687 /* there are no more siblings */
688 return;
689 }
690 if (node->next) {
691 node->next->prev = node->prev;
692 } else {
693 /* unlinking the last element */
694 if (parent) {
695 first = parent->child;
696 } else {
697 first = node;
Radek Krejci10c760e2015-08-14 14:45:43 +0200698 while (first->prev->next) {
Michal Vasko276f96b2015-09-23 11:34:28 +0200699 first = first->prev;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200700 }
701 }
702 first->prev = node->prev;
703 }
704 if (node->prev->next) {
705 node->prev->next = node->next;
706 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200707
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200708 /* clean up the unlinked element */
709 node->next = NULL;
710 node->prev = node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200711}
712
Michal Vasko563ef092015-09-04 13:17:23 +0200713struct lys_node_grp *
714lys_find_grouping_up(const char *name, struct lys_node *start, int in_submodules)
715{
716 struct lys_node *par_iter, *iter, *stop;
717 int i;
718
719 for (par_iter = start; par_iter; par_iter = par_iter->parent) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200720 /* top-level augment, look into module (uses augment is handled correctly below) */
721 if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) {
722 par_iter = par_iter->parent->module->data;
723 if (!par_iter) {
Michal Vaskoccbdb4e2015-10-21 15:09:02 +0200724 break;
725 }
726 }
727
Michal Vasko6f929da2015-10-02 16:23:25 +0200728 if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) {
Michal Vasko563ef092015-09-04 13:17:23 +0200729 continue;
730 }
731
732 for (iter = par_iter, stop = NULL; iter; iter = iter->prev) {
733 if (!stop) {
734 stop = par_iter;
735 } else if (iter == stop) {
736 break;
737 }
738 if (iter->nodetype != LYS_GROUPING) {
739 continue;
740 }
741
Radek Krejcif8426a72015-10-31 23:14:03 +0100742 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200743 return (struct lys_node_grp *)iter;
744 }
745 }
746 }
747
748 if (in_submodules) {
749 for (i = 0; i < start->module->inc_size; ++i) {
750 for (iter = start->module->inc[i].submodule->data; iter; iter = iter->next) {
751 if (iter->nodetype != LYS_GROUPING) {
752 continue;
753 }
754
Radek Krejcif8426a72015-10-31 23:14:03 +0100755 if (!strcmp(name, iter->name)) {
Michal Vasko563ef092015-09-04 13:17:23 +0200756 return (struct lys_node_grp *)iter;
757 }
758 }
759 }
760 }
761
762 return NULL;
763}
764
Radek Krejci10c760e2015-08-14 14:45:43 +0200765/*
766 * get next grouping in the root's subtree, in the
767 * first call, tha last is NULL
768 */
769static struct lys_node_grp *
Michal Vasko563ef092015-09-04 13:17:23 +0200770lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200771{
Radek Krejci10c760e2015-08-14 14:45:43 +0200772 struct lys_node *last = (struct lys_node *)lastgrp;
773 struct lys_node *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200774
Radek Krejci10c760e2015-08-14 14:45:43 +0200775 assert(root);
776
777 if (!last) {
778 last = root;
779 }
780
781 while (1) {
782 if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
783 next = last->child;
784 } else {
785 next = NULL;
786 }
787 if (!next) {
788 if (last == root) {
789 /* we are done */
790 return NULL;
791 }
792
793 /* no children, go to siblings */
794 next = last->next;
795 }
796 while (!next) {
797 /* go back through parents */
798 if (last->parent == root) {
799 /* we are done */
800 return NULL;
801 }
802 last = last->parent;
803 next = last->next;
804 }
805
806 if (next->nodetype == LYS_GROUPING) {
807 return (struct lys_node_grp *)next;
808 }
809
810 last = next;
811 }
812}
813
Michal Vasko0d343d12015-08-24 14:57:36 +0200814/* logs directly */
Radek Krejci10c760e2015-08-14 14:45:43 +0200815int
Radek Krejci07911992015-08-14 15:13:31 +0200816lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module)
817{
Michal Vasko563ef092015-09-04 13:17:23 +0200818 struct lys_node *start, *stop, *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200819 struct lys_node_grp *grp;
820 int down;
821
822 assert(node);
823
824 if (!parent) {
825 assert(module);
826 } else {
827 module = parent->module;
828 }
829
830 switch (node->nodetype) {
831 case LYS_GROUPING:
832 /* 6.2.1, rule 6 */
833 if (parent) {
834 if (parent->child) {
835 down = 1;
836 start = parent->child;
837 } else {
838 down = 0;
839 start = parent;
840 }
841 } else {
842 down = 1;
843 start = module->data;
844 }
845 /* go up */
Michal Vasko563ef092015-09-04 13:17:23 +0200846 if (lys_find_grouping_up(node->name, start, 0)) {
847 LOGVAL(LYE_DUPID, 0, "grouping", node->name);
848 return EXIT_FAILURE;
Radek Krejci07911992015-08-14 15:13:31 +0200849 }
850 /* go down, because grouping can be defined after e.g. container in which is collision */
851 if (down) {
852 for (iter = start, stop = NULL; iter; iter = iter->prev) {
853 if (!stop) {
854 stop = start;
855 } else if (iter == stop) {
856 break;
857 }
858 if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) {
859 continue;
860 }
861
862 grp = NULL;
863 while ((grp = lys_get_next_grouping(grp, iter))) {
864 if (node->name == grp->name) {
865 LOGVAL(LYE_DUPID, 0, "grouping", node->name);
866 return EXIT_FAILURE;
867 }
868 }
869 }
870 }
871 break;
872 case LYS_LEAF:
873 case LYS_LEAFLIST:
874 case LYS_LIST:
875 case LYS_CONTAINER:
876 case LYS_CHOICE:
877 case LYS_ANYXML:
878 /* 6.2.1, rule 7 */
879 if (parent) {
880 iter = parent;
881 while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE))) {
882 iter = iter->parent;
883 }
884 if (!iter) {
885 stop = NULL;
886 iter = module->data;
887 } else {
888 stop = iter;
889 iter = iter->child;
890 }
891 } else {
892 stop = NULL;
893 iter = module->data;
894 }
895 while (iter) {
896 if (iter->nodetype & (LYS_USES | LYS_CASE)) {
897 iter = iter->child;
898 continue;
899 }
900
901 if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYXML)) {
902 if (iter->module == node->module && iter->name == node->name) {
903 LOGVAL(LYE_SPEC, 0, "Duplicated child identifier \"%s\" in \"%s\".", node->name,
904 stop ? stop->name : "(sub)module");
905 return EXIT_FAILURE;
906 }
907 }
908
909 /* special case for choice - we must check the choice's name as
910 * well as the names of nodes under the choice
911 */
912 if (iter->nodetype == LYS_CHOICE) {
913 iter = iter->child;
914 continue;
915 }
916
917 /* go to siblings */
918 if (!iter->next) {
919 /* no sibling, go to parent's sibling */
920 do {
921 iter = iter->parent;
922 if (iter && iter->next) {
923 break;
924 }
925 } while (iter != stop);
926
927 if (iter == stop) {
928 break;
929 }
930 }
931 iter = iter->next;
932 }
933 break;
934 case LYS_CASE:
935 /* 6.2.1, rule 8 */
936 LY_TREE_FOR(parent->child, iter) {
937 if (!(iter->nodetype & (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
938 continue;
939 }
940
941 if (iter->module == node->module && iter->name == node->name) {
942 LOGVAL(LYE_DUPID, 0, "case", node->name);
943 return EXIT_FAILURE;
944 }
945 }
946 break;
947 default:
948 /* no check needed */
949 break;
950 }
951
952 return EXIT_SUCCESS;
953}
954
Michal Vasko0d343d12015-08-24 14:57:36 +0200955/* logs directly */
Radek Krejci07911992015-08-14 15:13:31 +0200956int
Radek Krejci10c760e2015-08-14 14:45:43 +0200957lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child)
958{
Radek Krejci92720552015-10-05 15:28:27 +0200959 struct lys_node *iter;
Radek Krejci07911992015-08-14 15:13:31 +0200960 int type;
Radek Krejci10c760e2015-08-14 14:45:43 +0200961
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200962 assert(child);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200963
Radek Krejci10c760e2015-08-14 14:45:43 +0200964 if (parent) {
965 type = parent->nodetype;
966 module = parent->module;
967 } else {
968 assert(module);
969 type = 0;
Radek Krejci10c760e2015-08-14 14:45:43 +0200970 }
971
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200972 /* checks */
Radek Krejci10c760e2015-08-14 14:45:43 +0200973 switch (type) {
Radek Krejci76512572015-08-04 09:47:08 +0200974 case LYS_CONTAINER:
975 case LYS_LIST:
976 case LYS_GROUPING:
977 case LYS_USES:
978 case LYS_INPUT:
979 case LYS_OUTPUT:
980 case LYS_NOTIF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200981 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +0200982 (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF |
983 LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200984 LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).",
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200985 strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name);
986 return EXIT_FAILURE;
987 }
Radek Krejci10c760e2015-08-14 14:45:43 +0200988
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200989 break;
Radek Krejci76512572015-08-04 09:47:08 +0200990 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200991 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +0200992 (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +0200993 LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"choice\" %s.",
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200994 strnodetype(child->nodetype), parent->name);
995 return EXIT_FAILURE;
996 }
997 break;
Radek Krejci76512572015-08-04 09:47:08 +0200998 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200999 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +02001000 (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001001 LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"case\" %s.",
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001002 strnodetype(child->nodetype), parent->name);
1003 return EXIT_FAILURE;
1004 }
1005 break;
Radek Krejci76512572015-08-04 09:47:08 +02001006 case LYS_RPC:
1007 if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001008 LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"rpc\" %s.",
Michal Vasko38d01f72015-06-15 09:41:06 +02001009 strnodetype(child->nodetype), parent->name);
1010 return EXIT_FAILURE;
1011 }
1012 break;
Radek Krejci76512572015-08-04 09:47:08 +02001013 case LYS_LEAF:
1014 case LYS_LEAFLIST:
1015 case LYS_ANYXML:
Radek Krejci10c760e2015-08-14 14:45:43 +02001016 LOGVAL(LYE_SPEC, 0, "The \"%s\" statement (%s) cannot have any data substatement.",
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001017 strnodetype(parent->nodetype), parent->name);
1018 return EXIT_FAILURE;
Radek Krejci76512572015-08-04 09:47:08 +02001019 case LYS_AUGMENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001020 if (!(child->nodetype &
Radek Krejci76512572015-08-04 09:47:08 +02001021 (LYS_ANYXML | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF
1022 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) {
Michal Vaskoe7fc19c2015-08-05 16:24:39 +02001023 LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).",
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001024 strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name);
1025 return EXIT_FAILURE;
1026 }
Michal Vasko591e0b22015-08-13 13:53:43 +02001027 break;
1028 case LYS_UNKNOWN:
Radek Krejci10c760e2015-08-14 14:45:43 +02001029 /* top level */
1030 if (!(child->nodetype &
1031 (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING
1032 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) {
Radek Krejci2c0ca272015-08-14 15:31:01 +02001033 LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in (sub)module \"%s\"",
1034 strnodetype(child->nodetype), module->name);
Radek Krejci10c760e2015-08-14 14:45:43 +02001035 return EXIT_FAILURE;
1036 }
1037
1038 break;;
1039 }
1040
1041 /* check identifier uniqueness */
Radek Krejci07911992015-08-14 15:13:31 +02001042 if (lys_check_id(child, parent, module)) {
1043 return EXIT_FAILURE;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001044 }
Radek Krejcib7155b52015-06-10 17:03:01 +02001045
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001046 if (child->parent) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001047 lys_node_unlink(child);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001048 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001049
Radek Krejci10c760e2015-08-14 14:45:43 +02001050 if (!parent) {
Radek Krejci92720552015-10-05 15:28:27 +02001051 if (module->data) {
1052 module->data->prev->next = child;
1053 child->prev = module->data->prev;
1054 module->data->prev = child;
Radek Krejci10c760e2015-08-14 14:45:43 +02001055 } else {
Radek Krejci92720552015-10-05 15:28:27 +02001056 module->data = child;
Radek Krejci10c760e2015-08-14 14:45:43 +02001057 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001058 } else {
Radek Krejci10c760e2015-08-14 14:45:43 +02001059 if (!parent->child) {
1060 /* the only/first child of the parent */
1061 parent->child = child;
1062 child->parent = parent;
1063 iter = child;
1064 } else {
1065 /* add a new child at the end of parent's child list */
1066 iter = parent->child->prev;
1067 iter->next = child;
1068 child->prev = iter;
1069 }
1070 while (iter->next) {
1071 iter = iter->next;
1072 iter->parent = parent;
1073 }
1074 parent->child->prev = iter;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001075 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001076
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001077 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001078}
1079
Radek Krejcib8048692015-08-05 13:36:34 +02001080API struct lys_module *
Radek Krejcia9167ef2015-08-03 11:01:11 +02001081lys_parse(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001082{
Michal Vaskof02e3742015-08-05 16:27:02 +02001083 struct unres_schema *unres;
Radek Krejci0b5805d2015-08-13 09:38:02 +02001084 struct lys_module *mod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001085
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001086 if (!ctx || !data) {
1087 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1088 return NULL;
1089 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001090
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001091 unres = calloc(1, sizeof *unres);
1092
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001093 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001094 case LYS_IN_YIN:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001095 mod = yin_read_module(ctx, data, 1, unres);
1096 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001097 case LYS_IN_YANG:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001098 default:
Radek Krejci0b5805d2015-08-13 09:38:02 +02001099 /* TODO */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001100 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001101 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001102
Michal Vasko0bd29d12015-08-19 11:45:49 +02001103 if (mod && unres->count && resolve_unres_schema(mod, unres)) {
Michal Vasko13b15832015-08-19 11:04:48 +02001104 lys_free(mod, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001105 mod = NULL;
1106 }
1107 free(unres->item);
1108 free(unres->type);
Radek Krejci1d82ef62015-08-07 14:44:40 +02001109 free(unres->str_snode);
Michal Vaskoc07187d2015-08-13 15:20:57 +02001110#ifndef NDEBUG
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001111 free(unres->line);
Michal Vaskoc07187d2015-08-13 15:20:57 +02001112#endif
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001113 free(unres);
1114
1115 return mod;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001116}
1117
Radek Krejcib8048692015-08-05 13:36:34 +02001118struct lys_submodule *
Radek Krejci1d82ef62015-08-07 14:44:40 +02001119lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, int implement)
Radek Krejciefaeba32015-05-27 14:30:57 +02001120{
Michal Vaskof02e3742015-08-05 16:27:02 +02001121 struct unres_schema *unres;
Radek Krejci0b5805d2015-08-13 09:38:02 +02001122 struct lys_submodule *submod = NULL;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001123
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001124 assert(module);
1125 assert(data);
Radek Krejciefaeba32015-05-27 14:30:57 +02001126
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001127 unres = calloc(1, sizeof *unres);
1128
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001129 switch (format) {
Radek Krejcia9167ef2015-08-03 11:01:11 +02001130 case LYS_IN_YIN:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001131 submod = yin_read_submodule(module, data, implement, unres);
1132 break;
Radek Krejcia9167ef2015-08-03 11:01:11 +02001133 case LYS_IN_YANG:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001134 default:
Radek Krejci0b5805d2015-08-13 09:38:02 +02001135 /* TODO */
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001136 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001137 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001138
Michal Vasko0bd29d12015-08-19 11:45:49 +02001139 if (submod && unres->count && resolve_unres_schema((struct lys_module *)submod, unres)) {
Michal Vasko13b15832015-08-19 11:04:48 +02001140 lys_submodule_free(submod, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001141 submod = NULL;
1142 }
1143 free(unres->item);
1144 free(unres->type);
Radek Krejci1d82ef62015-08-07 14:44:40 +02001145 free(unres->str_snode);
Michal Vaskoc07187d2015-08-13 15:20:57 +02001146#ifndef NDEBUG
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001147 free(unres->line);
Michal Vaskoc07187d2015-08-13 15:20:57 +02001148#endif
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001149 free(unres);
1150
1151 return submod;
Radek Krejciefaeba32015-05-27 14:30:57 +02001152}
1153
Radek Krejcib8048692015-08-05 13:36:34 +02001154API struct lys_module *
Radek Krejcia9167ef2015-08-03 11:01:11 +02001155lys_read(struct ly_ctx *ctx, int fd, LYS_INFORMAT format)
Radek Krejci63a91a92015-07-29 13:31:04 +02001156{
Radek Krejcib8048692015-08-05 13:36:34 +02001157 struct lys_module *module;
Radek Krejci63a91a92015-07-29 13:31:04 +02001158 struct stat sb;
1159 char *addr;
1160
1161 if (!ctx || fd < 0) {
1162 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1163 return NULL;
1164 }
1165
1166 /*
1167 * TODO
1168 * This is just a temporary solution to make working automatic search for
1169 * imported modules. This doesn't work e.g. for streams (stdin)
1170 */
1171 fstat(fd, &sb);
1172 addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1173 module = lys_parse(ctx, addr, format);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001174 munmap(addr, sb.st_size);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001175
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001176 return module;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001177}
1178
Radek Krejcib8048692015-08-05 13:36:34 +02001179struct lys_submodule *
Radek Krejci1d82ef62015-08-07 14:44:40 +02001180lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, int implement)
Radek Krejciefaeba32015-05-27 14:30:57 +02001181{
Radek Krejcib8048692015-08-05 13:36:34 +02001182 struct lys_submodule *submodule;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001183 struct stat sb;
1184 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
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001189 /*
1190 * TODO
1191 * This is just a temporary solution to make working automatic search for
1192 * imported modules. This doesn't work e.g. for streams (stdin)
1193 */
1194 fstat(fd, &sb);
1195 addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1196 /* TODO addr error check */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001197 submodule = lys_submodule_parse(module, addr, format, implement);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001198 munmap(addr, sb.st_size);
Radek Krejciefaeba32015-05-27 14:30:57 +02001199
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001200 return submodule;
Radek Krejciefaeba32015-05-27 14:30:57 +02001201
1202}
1203
Radek Krejci1d82ef62015-08-07 14:44:40 +02001204static struct lys_restr *
1205lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001206{
Radek Krejci1574a8d2015-08-03 14:16:52 +02001207 struct lys_restr *result;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001208 int i;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001209
Radek Krejci3733a802015-06-19 13:43:21 +02001210 if (!size) {
1211 return NULL;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001212 }
Radek Krejci3733a802015-06-19 13:43:21 +02001213
1214 result = calloc(size, sizeof *result);
1215 for (i = 0; i < size; i++) {
1216 result[i].expr = lydict_insert(ctx, old[i].expr, 0);
1217 result[i].dsc = lydict_insert(ctx, old[i].dsc, 0);
1218 result[i].ref = lydict_insert(ctx, old[i].ref, 0);
1219 result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0);
1220 result[i].emsg = lydict_insert(ctx, old[i].emsg, 0);
1221 }
1222
1223 return result;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001224}
1225
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001226void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001227lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr)
Radek Krejci0bd5db42015-06-19 13:30:07 +02001228{
1229 assert(ctx);
1230 if (!restr) {
1231 return;
1232 }
1233
1234 lydict_remove(ctx, restr->expr);
1235 lydict_remove(ctx, restr->dsc);
1236 lydict_remove(ctx, restr->ref);
1237 lydict_remove(ctx, restr->eapptag);
1238 lydict_remove(ctx, restr->emsg);
1239}
1240
Michal Vaskob84f88a2015-09-24 13:16:10 +02001241static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02001242lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old,
Michal Vaskof02e3742015-08-05 16:27:02 +02001243 struct unres_schema *unres)
Radek Krejci3733a802015-06-19 13:43:21 +02001244{
1245 int i;
1246
Michal Vasko1dca6882015-10-22 14:29:42 +02001247 new->module_name = lydict_insert(mod->ctx, old->module_name, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001248 new->base = old->base;
1249 new->der = old->der;
1250
Michal Vasko0bd29d12015-08-19 11:45:49 +02001251 i = unres_schema_find(unres, old, UNRES_TYPE_DER);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001252 if (i != -1) {
1253 /* HACK for unres */
Radek Krejci1574a8d2015-08-03 14:16:52 +02001254 new->der = (struct lys_tpdf *)parent;
Michal Vaskob84f88a2015-09-24 13:16:10 +02001255 /* all these unres additions can fail even though they did not before */
Michal Vasko0bd29d12015-08-19 11:45:49 +02001256 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_DER, unres->str_snode[i], 0) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001257 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001258 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001259 return EXIT_SUCCESS;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001260 }
1261
Radek Krejci3733a802015-06-19 13:43:21 +02001262 switch (new->base) {
1263 case LY_TYPE_BINARY:
Radek Krejci425adf02015-06-26 16:23:28 +02001264 if (old->info.binary.length) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001265 new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1);
Radek Krejci425adf02015-06-26 16:23:28 +02001266 }
Radek Krejci3733a802015-06-19 13:43:21 +02001267 break;
1268
1269 case LY_TYPE_BITS:
1270 new->info.bits.count = old->info.bits.count;
1271 if (new->info.bits.count) {
1272 new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit);
1273 for (i = 0; i < new->info.bits.count; i++) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001274 new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0);
1275 new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0);
1276 new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0);
Radek Krejci3733a802015-06-19 13:43:21 +02001277 new->info.bits.bit[i].status = old->info.bits.bit[i].status;
1278 new->info.bits.bit[i].pos = old->info.bits.bit[i].pos;
1279 }
1280 }
1281 break;
1282
Radek Krejcif9401c32015-06-26 16:47:36 +02001283 case LY_TYPE_DEC64:
1284 new->info.dec64.dig = old->info.dec64.dig;
1285 if (old->info.dec64.range) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001286 new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1);
Radek Krejcif9401c32015-06-26 16:47:36 +02001287 }
1288 break;
1289
Radek Krejci3733a802015-06-19 13:43:21 +02001290 case LY_TYPE_ENUM:
1291 new->info.enums.count = old->info.enums.count;
1292 if (new->info.enums.count) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001293 new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm);
Radek Krejci3733a802015-06-19 13:43:21 +02001294 for (i = 0; i < new->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001295 new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0);
1296 new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0);
1297 new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0);
1298 new->info.enums.enm[i].status = old->info.enums.enm[i].status;
1299 new->info.enums.enm[i].value = old->info.enums.enm[i].value;
Radek Krejci3733a802015-06-19 13:43:21 +02001300 }
1301 }
1302 break;
1303
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001304 case LY_TYPE_IDENT:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001305 if (old->info.ident.ref) {
1306 new->info.ident.ref = old->info.ident.ref;
1307 } else {
Michal Vasko0bd29d12015-08-19 11:45:49 +02001308 i = unres_schema_find(unres, old, UNRES_TYPE_IDENTREF);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001309 assert(i != -1);
Michal Vasko0bd29d12015-08-19 11:45:49 +02001310 if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i], 0) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001311 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001312 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001313 }
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001314 break;
1315
Radek Krejciaf351422015-06-19 14:49:38 +02001316 case LY_TYPE_INST:
1317 new->info.inst.req = old->info.inst.req;
1318 break;
1319
Radek Krejcif2860132015-06-20 12:37:20 +02001320 case LY_TYPE_INT8:
1321 case LY_TYPE_INT16:
1322 case LY_TYPE_INT32:
1323 case LY_TYPE_INT64:
1324 case LY_TYPE_UINT8:
1325 case LY_TYPE_UINT16:
1326 case LY_TYPE_UINT32:
1327 case LY_TYPE_UINT64:
Radek Krejci425adf02015-06-26 16:23:28 +02001328 if (old->info.num.range) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001329 new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1);
Radek Krejci425adf02015-06-26 16:23:28 +02001330 }
Radek Krejcif2860132015-06-20 12:37:20 +02001331 break;
1332
Radek Krejcidc4c1412015-06-19 15:39:54 +02001333 case LY_TYPE_LEAFREF:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001334 new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0);
Michal Vasko0bd29d12015-08-19 11:45:49 +02001335 if (unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent, 0) == -1) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001336 return -1;
Michal Vasko49168a22015-08-17 16:35:41 +02001337 }
Radek Krejcidc4c1412015-06-19 15:39:54 +02001338 break;
1339
Radek Krejci3733a802015-06-19 13:43:21 +02001340 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +02001341 if (old->info.str.length) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001342 new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001343 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02001344 new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count);
Radek Krejci3733a802015-06-19 13:43:21 +02001345 break;
1346
Radek Krejcie4c366b2015-07-02 10:11:31 +02001347 case LY_TYPE_UNION:
1348 new->info.uni.count = old->info.uni.count;
1349 if (new->info.uni.count) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001350 new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001351 for (i = 0; i < new->info.uni.count; i++) {
Michal Vaskob84f88a2015-09-24 13:16:10 +02001352 if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), unres)) {
1353 return -1;
1354 }
Radek Krejcie4c366b2015-07-02 10:11:31 +02001355 }
1356 }
1357 break;
1358
Radek Krejci3733a802015-06-19 13:43:21 +02001359 default:
Radek Krejcic7c85532015-07-02 10:16:54 +02001360 /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci3733a802015-06-19 13:43:21 +02001361 break;
1362 }
Michal Vaskob84f88a2015-09-24 13:16:10 +02001363
1364 return EXIT_SUCCESS;
Radek Krejci3733a802015-06-19 13:43:21 +02001365}
1366
1367void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001368lys_type_free(struct ly_ctx *ctx, struct lys_type *type)
Radek Krejci5a065542015-05-22 15:02:07 +02001369{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001370 int i;
Radek Krejci5a065542015-05-22 15:02:07 +02001371
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001372 assert(ctx);
1373 if (!type) {
1374 return;
1375 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001376
Michal Vasko1dca6882015-10-22 14:29:42 +02001377 lydict_remove(ctx, type->module_name);
Radek Krejci5a065542015-05-22 15:02:07 +02001378
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001379 switch (type->base) {
Radek Krejci0bd5db42015-06-19 13:30:07 +02001380 case LY_TYPE_BINARY:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001381 lys_restr_free(ctx, type->info.binary.length);
Radek Krejci0bd5db42015-06-19 13:30:07 +02001382 free(type->info.binary.length);
1383 break;
Radek Krejci994b6f62015-06-18 16:47:27 +02001384 case LY_TYPE_BITS:
1385 for (i = 0; i < type->info.bits.count; i++) {
1386 lydict_remove(ctx, type->info.bits.bit[i].name);
1387 lydict_remove(ctx, type->info.bits.bit[i].dsc);
1388 lydict_remove(ctx, type->info.bits.bit[i].ref);
1389 }
1390 free(type->info.bits.bit);
1391 break;
Radek Krejcif9401c32015-06-26 16:47:36 +02001392
1393 case LY_TYPE_DEC64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001394 lys_restr_free(ctx, type->info.dec64.range);
Radek Krejcif9401c32015-06-26 16:47:36 +02001395 free(type->info.dec64.range);
1396 break;
1397
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001398 case LY_TYPE_ENUM:
1399 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02001400 lydict_remove(ctx, type->info.enums.enm[i].name);
1401 lydict_remove(ctx, type->info.enums.enm[i].dsc);
1402 lydict_remove(ctx, type->info.enums.enm[i].ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001403 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001404 free(type->info.enums.enm);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001405 break;
Radek Krejcidc4c1412015-06-19 15:39:54 +02001406
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001407 case LY_TYPE_INT8:
1408 case LY_TYPE_INT16:
1409 case LY_TYPE_INT32:
1410 case LY_TYPE_INT64:
1411 case LY_TYPE_UINT8:
1412 case LY_TYPE_UINT16:
1413 case LY_TYPE_UINT32:
1414 case LY_TYPE_UINT64:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001415 lys_restr_free(ctx, type->info.num.range);
Radek Krejci6fcb9dd2015-06-22 10:16:37 +02001416 free(type->info.num.range);
1417 break;
1418
Radek Krejcidc4c1412015-06-19 15:39:54 +02001419 case LY_TYPE_LEAFREF:
1420 lydict_remove(ctx, type->info.lref.path);
1421 break;
1422
Radek Krejci3733a802015-06-19 13:43:21 +02001423 case LY_TYPE_STRING:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001424 lys_restr_free(ctx, type->info.str.length);
Radek Krejci3733a802015-06-19 13:43:21 +02001425 free(type->info.str.length);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001426 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001427 lys_restr_free(ctx, &type->info.str.patterns[i]);
Radek Krejci5fbc9162015-06-19 14:11:11 +02001428 }
1429 free(type->info.str.patterns);
Radek Krejci3733a802015-06-19 13:43:21 +02001430 break;
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001431
Radek Krejcie4c366b2015-07-02 10:11:31 +02001432 case LY_TYPE_UNION:
1433 for (i = 0; i < type->info.uni.count; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001434 lys_type_free(ctx, &type->info.uni.types[i]);
Radek Krejcie4c366b2015-07-02 10:11:31 +02001435 }
Radek Krejci1574a8d2015-08-03 14:16:52 +02001436 free(type->info.uni.types);
Radek Krejci4a7b5ee2015-06-19 14:56:43 +02001437 break;
1438
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001439 default:
Radek Krejcic7c85532015-07-02 10:16:54 +02001440 /* nothing to do for LY_TYPE_IDENT, LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001441 break;
1442 }
Radek Krejci5a065542015-05-22 15:02:07 +02001443}
1444
Radek Krejci1d82ef62015-08-07 14:44:40 +02001445static void
1446lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001447{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001448 assert(ctx);
1449 if (!tpdf) {
1450 return;
1451 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001452
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001453 lydict_remove(ctx, tpdf->name);
1454 lydict_remove(ctx, tpdf->dsc);
1455 lydict_remove(ctx, tpdf->ref);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001456
Radek Krejci1d82ef62015-08-07 14:44:40 +02001457 lys_type_free(ctx, &tpdf->type);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001458
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001459 lydict_remove(ctx, tpdf->units);
1460 lydict_remove(ctx, tpdf->dflt);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02001461}
1462
Michal Vaskob84f88a2015-09-24 13:16:10 +02001463static struct lys_tpdf *
1464lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres)
1465{
1466 struct lys_tpdf *result;
1467 int i, j;
1468
1469 if (!size) {
1470 return NULL;
1471 }
1472
1473 result = calloc(size, sizeof *result);
1474 for (i = 0; i < size; i++) {
1475 result[i].name = lydict_insert(mod->ctx, old[i].name, 0);
1476 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1477 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
1478 result[i].flags = old[i].flags;
1479 result[i].module = old[i].module;
1480
1481 if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), unres)) {
1482 for (j = 0; j <= i; ++j) {
1483 lys_tpdf_free(mod->ctx, &result[j]);
1484 }
1485 free(result);
1486 return NULL;
1487 }
1488
1489 result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0);
1490 result[i].units = lydict_insert(mod->ctx, old[i].units, 0);
1491 }
1492
1493 return result;
1494}
1495
Radek Krejci1d82ef62015-08-07 14:44:40 +02001496static struct lys_when *
1497lys_when_dup(struct ly_ctx *ctx, struct lys_when *old)
Radek Krejci00768f42015-06-18 17:04:04 +02001498{
Radek Krejci76512572015-08-04 09:47:08 +02001499 struct lys_when *new;
Radek Krejci00768f42015-06-18 17:04:04 +02001500
1501 if (!old) {
1502 return NULL;
1503 }
1504
1505 new = calloc(1, sizeof *new);
1506 new->cond = lydict_insert(ctx, old->cond, 0);
1507 new->dsc = lydict_insert(ctx, old->dsc, 0);
1508 new->ref = lydict_insert(ctx, old->ref, 0);
1509
1510 return new;
1511}
1512
Michal Vasko0308dd62015-10-07 09:14:40 +02001513void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001514lys_when_free(struct ly_ctx *ctx, struct lys_when *w)
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001515{
1516 if (!w) {
1517 return;
1518 }
1519
1520 lydict_remove(ctx, w->cond);
1521 lydict_remove(ctx, w->dsc);
1522 lydict_remove(ctx, w->ref);
1523
1524 free(w);
1525}
1526
Radek Krejcib7f5e412015-08-13 10:15:51 +02001527static void
1528lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment aug)
1529{
Michal Vaskoc4c2e212015-09-23 11:34:41 +02001530 struct lys_node *next, *sub;
1531
1532 /* children from a resolved uses */
1533 LY_TREE_FOR_SAFE(aug.child, next, sub) {
1534 lys_node_free(sub);
1535 }
1536
Radek Krejcib7f5e412015-08-13 10:15:51 +02001537 lydict_remove(ctx, aug.target_name);
1538 lydict_remove(ctx, aug.dsc);
1539 lydict_remove(ctx, aug.ref);
1540
1541 free(aug.features);
1542
1543 lys_when_free(ctx, aug.when);
1544
Michal Vasko7d356a52015-08-19 15:06:31 +02001545 /* Do not free the children, they were appended somewhere and their
1546 * new parent will take care of them.
1547 */
Radek Krejcib7f5e412015-08-13 10:15:51 +02001548}
1549
Radek Krejci76512572015-08-04 09:47:08 +02001550static struct lys_node_augment *
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001551lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size)
Radek Krejci106efc02015-06-10 14:36:27 +02001552{
Radek Krejci76512572015-08-04 09:47:08 +02001553 struct lys_node_augment *new = NULL;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001554 struct lys_node *old_child, *new_child;
1555 int i;
Radek Krejci106efc02015-06-10 14:36:27 +02001556
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001557 if (!size) {
1558 return NULL;
1559 }
Radek Krejci106efc02015-06-10 14:36:27 +02001560
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001561 new = calloc(size, sizeof *new);
1562 for (i = 0; i < size; i++) {
1563 new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0);
1564 new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0);
1565 new[i].ref = lydict_insert(module->ctx, old[i].ref, 0);
1566 new[i].flags = old[i].flags;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001567 new[i].module = old[i].module;
Michal Vasko591e0b22015-08-13 13:53:43 +02001568 new[i].nodetype = old[i].nodetype;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001569 /* this must succeed, it was already resolved once */
1570 if (resolve_schema_nodeid(new[i].target_name, parent->child, new[i].module, LYS_AUGMENT, &new[i].target)) {
1571 LOGINT;
1572 free(new);
1573 return NULL;
1574 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001575 new[i].parent = parent;
Radek Krejci106efc02015-06-10 14:36:27 +02001576
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001577 /* Correct the augment nodes.
1578 * This function can only be called from lys_node_dup() with uses
1579 * being the node duplicated, so we must have a case of grouping
1580 * with a uses with augments. The augmented nodes have already been
1581 * copied and everything is almost fine except their parent is wrong
Michal Vaskoa5900c52015-09-24 13:17:11 +02001582 * (it was set to their actual data parent, not an augment), and
1583 * the new augment does not have child pointer to its augment nodes,
1584 * so we just correct it.
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001585 */
1586 LY_TREE_FOR(new[i].target->child, new_child) {
1587 if (new_child->name == old[i].child->name) {
1588 break;
Radek Krejcib7f5e412015-08-13 10:15:51 +02001589 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001590 }
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001591 assert(new_child);
Michal Vaskoa5900c52015-09-24 13:17:11 +02001592 new[i].child = new_child;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02001593 LY_TREE_FOR(old[i].child, old_child) {
1594 /* all augment nodes were connected as siblings, there can be no more after this */
1595 if (old_child->parent != (struct lys_node *)&old[i]) {
1596 break;
1597 }
1598
1599 assert(old_child->name == new_child->name);
1600
1601 new_child->parent = (struct lys_node *)&new[i];
1602 new_child = new_child->next;
1603 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001604 }
Radek Krejci106efc02015-06-10 14:36:27 +02001605
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001606 return new;
Radek Krejci106efc02015-06-10 14:36:27 +02001607}
1608
Radek Krejci76512572015-08-04 09:47:08 +02001609static struct lys_refine *
Michal Vasko0d204592015-10-07 09:50:04 +02001610lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size)
Michal Vasko1982cad2015-06-08 15:49:30 +02001611{
Radek Krejci76512572015-08-04 09:47:08 +02001612 struct lys_refine *result;
Michal Vasko0d204592015-10-07 09:50:04 +02001613 int i;
Michal Vasko1982cad2015-06-08 15:49:30 +02001614
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001615 if (!size) {
1616 return NULL;
1617 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001618
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001619 result = calloc(size, sizeof *result);
1620 for (i = 0; i < size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001621 result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001622 result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0);
1623 result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001624 result[i].flags = old[i].flags;
1625 result[i].target_type = old[i].target_type;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001626
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001627 result[i].must_size = old[i].must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001628 result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001629
Radek Krejci76512572015-08-04 09:47:08 +02001630 if (result[i].target_type & (LYS_LEAF | LYS_CHOICE)) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001631 result[i].mod.dflt = lydict_insert(mod->ctx, old[i].mod.dflt, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001632 } else if (result[i].target_type == LYS_CONTAINER) {
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02001633 result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0);
Radek Krejci76512572015-08-04 09:47:08 +02001634 } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001635 result[i].mod.list = old[i].mod.list;
1636 }
1637 }
Michal Vasko1982cad2015-06-08 15:49:30 +02001638
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001639 return result;
Michal Vasko1982cad2015-06-08 15:49:30 +02001640}
1641
Radek Krejci1d82ef62015-08-07 14:44:40 +02001642static void
1643lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +02001644{
Radek Krejcia52656e2015-08-05 13:41:50 +02001645 struct lys_ident_der *der;
Radek Krejci6793db02015-05-22 17:49:54 +02001646
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001647 assert(ctx);
1648 if (!ident) {
1649 return;
1650 }
Radek Krejci812b10a2015-05-28 16:48:25 +02001651
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001652 /*
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001653 * if caller free only a single data model which is used (its identity is
1654 * reference from identity in another module), this silly freeing can lead
1655 * to segmentation fault. But without noting if the module is used by some
1656 * other, it cannot be solved.
Radek Krejcia3390692015-06-16 14:13:31 +02001657 *
1658 * Possible solution is to not allow caller to remove particular schema
Radek Krejci1d82ef62015-08-07 14:44:40 +02001659 * from the context. This is the current approach.
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001660 */
1661 while (ident->der) {
1662 der = ident->der;
1663 ident->der = der->next;
1664 free(der);
1665 }
Radek Krejci6793db02015-05-22 17:49:54 +02001666
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001667 lydict_remove(ctx, ident->name);
1668 lydict_remove(ctx, ident->dsc);
1669 lydict_remove(ctx, ident->ref);
Radek Krejci6793db02015-05-22 17:49:54 +02001670
1671}
1672
Radek Krejci1d82ef62015-08-07 14:44:40 +02001673static void
1674lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001675{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001676 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001677
Radek Krejcid12f57b2015-08-06 10:43:39 +02001678 /* handle only specific parts for LYS_GROUPING */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001679 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001680 lys_tpdf_free(ctx, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001681 }
1682 free(grp->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001683}
1684
Radek Krejci1d82ef62015-08-07 14:44:40 +02001685static void
Radek Krejcid12f57b2015-08-06 10:43:39 +02001686lys_rpc_inout_free(struct ly_ctx *ctx, struct lys_node_rpc_inout *io)
1687{
1688 int i;
1689
1690 /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */
1691 for (i = 0; i < io->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001692 lys_tpdf_free(ctx, &io->tpdf[i]);
Radek Krejcid12f57b2015-08-06 10:43:39 +02001693 }
1694 free(io->tpdf);
1695}
1696
Radek Krejci1d82ef62015-08-07 14:44:40 +02001697static void
1698lys_anyxml_free(struct ly_ctx *ctx, struct lys_node_anyxml *anyxml)
Radek Krejci537cf382015-06-04 11:07:01 +02001699{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001700 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001701
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001702 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001703 lys_restr_free(ctx, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001704 }
1705 free(anyxml->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001706
Radek Krejci1d82ef62015-08-07 14:44:40 +02001707 lys_when_free(ctx, anyxml->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001708}
1709
Radek Krejci1d82ef62015-08-07 14:44:40 +02001710static void
1711lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf)
Radek Krejci5a065542015-05-22 15:02:07 +02001712{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001713 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001714
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001715 for (i = 0; i < leaf->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001716 lys_restr_free(ctx, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001717 }
1718 free(leaf->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001719
Radek Krejci1d82ef62015-08-07 14:44:40 +02001720 lys_when_free(ctx, leaf->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001721
Radek Krejci1d82ef62015-08-07 14:44:40 +02001722 lys_type_free(ctx, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001723 lydict_remove(ctx, leaf->units);
1724 lydict_remove(ctx, leaf->dflt);
Radek Krejci5a065542015-05-22 15:02:07 +02001725}
1726
Radek Krejci1d82ef62015-08-07 14:44:40 +02001727static void
1728lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist)
Radek Krejci5a065542015-05-22 15:02:07 +02001729{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001730 int i;
Radek Krejci537cf382015-06-04 11:07:01 +02001731
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001732 for (i = 0; i < llist->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001733 lys_restr_free(ctx, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001734 }
1735 free(llist->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001736
Radek Krejci1d82ef62015-08-07 14:44:40 +02001737 lys_when_free(ctx, llist->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001738
Radek Krejci1d82ef62015-08-07 14:44:40 +02001739 lys_type_free(ctx, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001740 lydict_remove(ctx, llist->units);
Radek Krejci5a065542015-05-22 15:02:07 +02001741}
1742
Radek Krejci1d82ef62015-08-07 14:44:40 +02001743static void
1744lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001745{
Radek Krejci581ce772015-11-10 17:22:40 +01001746 int i, j;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001747
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001748 /* handle only specific parts for LY_NODE_LIST */
1749 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001750 lys_tpdf_free(ctx, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001751 }
1752 free(list->tpdf);
Radek Krejci537cf382015-06-04 11:07:01 +02001753
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001754 for (i = 0; i < list->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001755 lys_restr_free(ctx, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001756 }
1757 free(list->must);
Radek Krejci537cf382015-06-04 11:07:01 +02001758
Radek Krejci1d82ef62015-08-07 14:44:40 +02001759 lys_when_free(ctx, list->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001760
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001761 for (i = 0; i < list->unique_size; i++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001762 for (j = 0; j > list->unique[i].expr_size; j++) {
1763 lydict_remove(ctx, list->unique[i].expr[j]);
1764 }
1765 free(list->unique[i].expr);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001766 }
1767 free(list->unique);
Radek Krejcid7f0d012015-05-25 15:04:52 +02001768
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001769 free(list->keys);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001770}
1771
Radek Krejci1d82ef62015-08-07 14:44:40 +02001772static void
1773lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001774{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001775 int i;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001776
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001777 /* handle only specific parts for LY_NODE_CONTAINER */
1778 lydict_remove(ctx, cont->presence);
Radek Krejci800af702015-06-02 13:46:01 +02001779
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001780 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001781 lys_tpdf_free(ctx, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001782 }
1783 free(cont->tpdf);
Radek Krejci800af702015-06-02 13:46:01 +02001784
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001785 for (i = 0; i < cont->must_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001786 lys_restr_free(ctx, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001787 }
1788 free(cont->must);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001789
Radek Krejci1d82ef62015-08-07 14:44:40 +02001790 lys_when_free(ctx, cont->when);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001791}
1792
Radek Krejci1d82ef62015-08-07 14:44:40 +02001793static void
1794lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f)
Radek Krejci3cf9e222015-06-18 11:37:50 +02001795{
1796 lydict_remove(ctx, f->name);
1797 lydict_remove(ctx, f->dsc);
1798 lydict_remove(ctx, f->ref);
1799 free(f->features);
1800}
1801
Radek Krejci1d82ef62015-08-07 14:44:40 +02001802static void
1803lys_deviation_free(struct ly_ctx *ctx, struct lys_deviation *dev)
Radek Krejcieb00f512015-07-01 16:44:58 +02001804{
Radek Krejci581ce772015-11-10 17:22:40 +01001805 int i, j, k;
Radek Krejcieb00f512015-07-01 16:44:58 +02001806
1807 lydict_remove(ctx, dev->target_name);
1808 lydict_remove(ctx, dev->dsc);
1809 lydict_remove(ctx, dev->ref);
1810
1811 for (i = 0; i < dev->deviate_size; i++) {
1812 lydict_remove(ctx, dev->deviate[i].dflt);
1813 lydict_remove(ctx, dev->deviate[i].units);
1814
1815 if (dev->deviate[i].mod == LY_DEVIATE_DEL) {
1816 for (j = 0; j < dev->deviate[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001817 lys_restr_free(ctx, &dev->deviate[i].must[j]);
Radek Krejcieb00f512015-07-01 16:44:58 +02001818 }
1819 free(dev->deviate[i].must);
1820
1821 for (j = 0; j < dev->deviate[i].unique_size; j++) {
Radek Krejci581ce772015-11-10 17:22:40 +01001822 for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) {
1823 lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]);
1824 }
1825 free(dev->deviate[j].unique[j].expr);
Radek Krejcieb00f512015-07-01 16:44:58 +02001826 }
1827 free(dev->deviate[i].unique);
1828 }
1829 }
1830 free(dev->deviate);
1831}
1832
Radek Krejci1d82ef62015-08-07 14:44:40 +02001833static void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001834lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses)
Radek Krejcie1fa8582015-06-08 09:46:45 +02001835{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001836 int i, j;
Radek Krejcie1fa8582015-06-08 09:46:45 +02001837
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001838 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci76512572015-08-04 09:47:08 +02001839 lydict_remove(ctx, uses->refine[i].target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001840 lydict_remove(ctx, uses->refine[i].dsc);
1841 lydict_remove(ctx, uses->refine[i].ref);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001842
Michal Vaskoa275c0a2015-09-24 09:55:42 +02001843 for (j = 0; j < uses->refine[i].must_size; j++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001844 lys_restr_free(ctx, &uses->refine[i].must[j]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001845 }
1846 free(uses->refine[i].must);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001847
Radek Krejci76512572015-08-04 09:47:08 +02001848 if (uses->refine[i].target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001849 lydict_remove(ctx, uses->refine[i].mod.dflt);
Radek Krejci76512572015-08-04 09:47:08 +02001850 } else if (uses->refine[i].target_type & LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001851 lydict_remove(ctx, uses->refine[i].mod.presence);
1852 }
1853 }
1854 free(uses->refine);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001855
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001856 for (i = 0; i < uses->augment_size; i++) {
Radek Krejcib7f5e412015-08-13 10:15:51 +02001857 lys_augment_free(ctx, uses->augment[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001858 }
1859 free(uses->augment);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001860
Radek Krejci1d82ef62015-08-07 14:44:40 +02001861 lys_when_free(ctx, uses->when);
Radek Krejcie1fa8582015-06-08 09:46:45 +02001862}
1863
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001864void
Radek Krejci1d82ef62015-08-07 14:44:40 +02001865lys_node_free(struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001866{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001867 struct ly_ctx *ctx;
Radek Krejci76512572015-08-04 09:47:08 +02001868 struct lys_node *sub, *next;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001869
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001870 if (!node) {
1871 return;
1872 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001873
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001874 assert(node->module);
1875 assert(node->module->ctx);
Radek Krejci812b10a2015-05-28 16:48:25 +02001876
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001877 ctx = node->module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001878
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001879 /* common part */
1880 LY_TREE_FOR_SAFE(node->child, next, sub) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02001881 lys_node_free(sub);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001882 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001883
Radek Krejcid12f57b2015-08-06 10:43:39 +02001884 if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
1885 free(node->features);
1886 lydict_remove(ctx, node->name);
1887 lydict_remove(ctx, node->dsc);
1888 lydict_remove(ctx, node->ref);
1889 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001890
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001891 /* specific part */
1892 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02001893 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001894 lys_container_free(ctx, (struct lys_node_container *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001895 break;
Radek Krejci76512572015-08-04 09:47:08 +02001896 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001897 lys_when_free(ctx, ((struct lys_node_choice *)node)->when);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001898 break;
Radek Krejci76512572015-08-04 09:47:08 +02001899 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001900 lys_leaf_free(ctx, (struct lys_node_leaf *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001901 break;
Radek Krejci76512572015-08-04 09:47:08 +02001902 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001903 lys_leaflist_free(ctx, (struct lys_node_leaflist *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001904 break;
Radek Krejci76512572015-08-04 09:47:08 +02001905 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001906 lys_list_free(ctx, (struct lys_node_list *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001907 break;
Radek Krejci76512572015-08-04 09:47:08 +02001908 case LYS_ANYXML:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001909 lys_anyxml_free(ctx, (struct lys_node_anyxml *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001910 break;
Radek Krejci76512572015-08-04 09:47:08 +02001911 case LYS_USES:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001912 lys_uses_free(ctx, (struct lys_node_uses *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001913 break;
Radek Krejci76512572015-08-04 09:47:08 +02001914 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001915 lys_when_free(ctx, ((struct lys_node_case *)node)->when);
Radek Krejcib0af6ba2015-06-18 15:01:03 +02001916 break;
Radek Krejci76512572015-08-04 09:47:08 +02001917 case LYS_AUGMENT:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001918 /* do nothing */
1919 break;
Radek Krejci76512572015-08-04 09:47:08 +02001920 case LYS_GROUPING:
1921 case LYS_RPC:
Radek Krejci76512572015-08-04 09:47:08 +02001922 case LYS_NOTIF:
Radek Krejci1d82ef62015-08-07 14:44:40 +02001923 lys_grp_free(ctx, (struct lys_node_grp *)node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001924 break;
Radek Krejcid12f57b2015-08-06 10:43:39 +02001925
1926 case LYS_INPUT:
1927 case LYS_OUTPUT:
1928 lys_rpc_inout_free(ctx, (struct lys_node_rpc_inout *)node);
1929 break;
Michal Vasko591e0b22015-08-13 13:53:43 +02001930 case LYS_UNKNOWN:
1931 LOGINT;
1932 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001933 }
Radek Krejci5a065542015-05-22 15:02:07 +02001934
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001935 /* again common part */
Radek Krejci1d82ef62015-08-07 14:44:40 +02001936 lys_node_unlink(node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001937 free(node);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001938}
1939
Michal Vasko8ce24d72015-10-21 11:27:26 +02001940struct lys_module *
Michal Vaskob6729c62015-10-21 12:09:47 +02001941lys_get_import_module(struct lys_module *module, const char *prefix, int pref_len, const char *name, int name_len)
Michal Vasko8ce24d72015-10-21 11:27:26 +02001942{
Michal Vaskob6729c62015-10-21 12:09:47 +02001943 int i, match;
1944
1945 assert(prefix || name);
1946 if (prefix && !pref_len) {
1947 pref_len = strlen(prefix);
1948 }
1949 if (name && !name_len) {
1950 name_len = strlen(name);
1951 }
Michal Vasko8ce24d72015-10-21 11:27:26 +02001952
Michal Vaskod8da86b2015-10-21 13:35:37 +02001953 if ((!prefix || (!strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len]))
1954 && (!name || (!strncmp(module->name, name, name_len) && !module->name[name_len]))) {
1955 return module;
1956 }
1957
Michal Vasko8ce24d72015-10-21 11:27:26 +02001958 for (i = 0; i < module->imp_size; ++i) {
Michal Vaskob6729c62015-10-21 12:09:47 +02001959 match = 0;
1960 if (!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) {
1961 match = 1;
1962 }
1963 if (match && (!name
1964 || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) {
Michal Vasko8ce24d72015-10-21 11:27:26 +02001965 return module->imp[i].module;
1966 }
1967 }
1968
1969 return NULL;
1970}
1971
Michal Vasko13b15832015-08-19 11:04:48 +02001972/* free_int_mods - flag whether to free the internal modules as well */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001973static void
Michal Vasko13b15832015-08-19 11:04:48 +02001974module_free_common(struct lys_module *module, int free_int_mods)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001975{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001976 struct ly_ctx *ctx;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001977 unsigned int i;
Radek Krejcidce51452015-06-16 15:20:08 +02001978 int j, l;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001979
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001980 assert(module->ctx);
1981 ctx = module->ctx;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001982
Radek Krejcidce51452015-06-16 15:20:08 +02001983 /* as first step, free the imported modules */
1984 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoa76ee152015-08-17 15:38:22 +02001985 /* do not free internal modules */
Michal Vasko18bd09b2015-08-19 15:07:00 +02001986 if (!free_int_mods) {
1987 for (j = 0; j < int_mods.count; ++j) {
Michal Vaskod83c03a2015-08-21 09:09:24 +02001988 if (module->imp[i].module && !strcmp(int_mods.modules[j].name, module->imp[i].module->name)
Michal Vasko18bd09b2015-08-19 15:07:00 +02001989 && module->imp[i].module->rev
1990 && !strcmp(int_mods.modules[j].revision, module->imp[i].module->rev[0].date)) {
1991 break;
1992 }
Michal Vaskoa76ee152015-08-17 15:38:22 +02001993 }
Michal Vasko18bd09b2015-08-19 15:07:00 +02001994 if (j < int_mods.count) {
1995 continue;
1996 }
Michal Vaskoa76ee152015-08-17 15:38:22 +02001997 }
1998
Radek Krejcidce51452015-06-16 15:20:08 +02001999 /* get the imported module from the context and then free,
2000 * this check is necessary because the imported module can
2001 * be already removed
2002 */
2003 l = ctx->models.used;
2004 for (j = 0; j < l; j++) {
2005 if (ctx->models.list[j] == module->imp[i].module) {
Michal Vasko13b15832015-08-19 11:04:48 +02002006 lys_free(module->imp[i].module, free_int_mods);
Radek Krejcidce51452015-06-16 15:20:08 +02002007 break;
2008 }
2009 }
2010 }
2011 free(module->imp);
2012
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002013 while (module->data) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002014 lys_node_free(module->data);
Radek Krejci21181962015-06-30 14:11:00 +02002015 }
Radek Krejci5a065542015-05-22 15:02:07 +02002016
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002017 lydict_remove(ctx, module->dsc);
2018 lydict_remove(ctx, module->ref);
2019 lydict_remove(ctx, module->org);
2020 lydict_remove(ctx, module->contact);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002021
Radek Krejcieb00f512015-07-01 16:44:58 +02002022 /* revisions */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002023 for (i = 0; i < module->rev_size; i++) {
2024 lydict_remove(ctx, module->rev[i].dsc);
2025 lydict_remove(ctx, module->rev[i].ref);
2026 }
2027 free(module->rev);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002028
Radek Krejcieb00f512015-07-01 16:44:58 +02002029 /* identities */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002030 for (i = 0; i < module->ident_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002031 lys_ident_free(ctx, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002032 }
2033 module->ident_size = 0;
2034 free(module->ident);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002035
Radek Krejcieb00f512015-07-01 16:44:58 +02002036 /* typedefs */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002037 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002038 lys_tpdf_free(ctx, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002039 }
2040 free(module->tpdf);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002041
Radek Krejcieb00f512015-07-01 16:44:58 +02002042 /* include */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002043 for (i = 0; i < module->inc_size; i++) {
Michal Vasko13b15832015-08-19 11:04:48 +02002044 lys_submodule_free(module->inc[i].submodule, free_int_mods);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002045 }
2046 free(module->inc);
Radek Krejciefaeba32015-05-27 14:30:57 +02002047
Radek Krejcieb00f512015-07-01 16:44:58 +02002048 /* augment */
Radek Krejcif5be10f2015-06-16 13:29:36 +02002049 for (i = 0; i < module->augment_size; i++) {
Radek Krejcib7f5e412015-08-13 10:15:51 +02002050 lys_augment_free(ctx, module->augment[i]);
Radek Krejcif5be10f2015-06-16 13:29:36 +02002051 }
2052 free(module->augment);
2053
Radek Krejcieb00f512015-07-01 16:44:58 +02002054 /* features */
Radek Krejci3cf9e222015-06-18 11:37:50 +02002055 for (i = 0; i < module->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002056 lys_feature_free(ctx, &module->features[i]);
Radek Krejci3cf9e222015-06-18 11:37:50 +02002057 }
2058 free(module->features);
2059
Radek Krejcieb00f512015-07-01 16:44:58 +02002060 /* deviations */
2061 for (i = 0; i < module->deviation_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002062 lys_deviation_free(ctx, &module->deviation[i]);
Radek Krejcieb00f512015-07-01 16:44:58 +02002063 }
2064 free(module->deviation);
2065
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002066 lydict_remove(ctx, module->name);
Radek Krejciefaeba32015-05-27 14:30:57 +02002067}
2068
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002069void
Michal Vasko13b15832015-08-19 11:04:48 +02002070lys_submodule_free(struct lys_submodule *submodule, int free_int_mods)
Radek Krejciefaeba32015-05-27 14:30:57 +02002071{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002072 if (!submodule) {
2073 return;
2074 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002075
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002076 submodule->inc_size = 0;
2077 free(submodule->inc);
2078 submodule->inc = NULL;
Radek Krejcif3886932015-06-04 17:36:06 +02002079
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002080 /* common part with struct ly_module */
Michal Vasko13b15832015-08-19 11:04:48 +02002081 module_free_common((struct lys_module *)submodule, free_int_mods);
Radek Krejciefaeba32015-05-27 14:30:57 +02002082
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002083 /* no specific items to free */
Radek Krejciefaeba32015-05-27 14:30:57 +02002084
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002085 free(submodule);
Radek Krejciefaeba32015-05-27 14:30:57 +02002086}
2087
Radek Krejci76512572015-08-04 09:47:08 +02002088struct lys_node *
Michal Vasko71e1aa82015-08-12 12:17:51 +02002089lys_node_dup(struct lys_module *module, struct lys_node *node, uint8_t flags, uint8_t nacm, int recursive,
Michal Vaskof02e3742015-08-05 16:27:02 +02002090 struct unres_schema *unres)
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002091{
Radek Krejci76512572015-08-04 09:47:08 +02002092 struct lys_node *retval = NULL, *aux, *child;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002093 struct ly_ctx *ctx = module->ctx;
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002094 int i, j, rc;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002095
Michal Vaskoc07187d2015-08-13 15:20:57 +02002096 struct lys_node_container *cont = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002097 struct lys_node_container *cont_orig = (struct lys_node_container *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002098 struct lys_node_choice *choice = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002099 struct lys_node_choice *choice_orig = (struct lys_node_choice *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002100 struct lys_node_leaf *leaf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002101 struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002102 struct lys_node_leaflist *llist = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002103 struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002104 struct lys_node_list *list = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002105 struct lys_node_list *list_orig = (struct lys_node_list *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002106 struct lys_node_anyxml *anyxml = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002107 struct lys_node_anyxml *anyxml_orig = (struct lys_node_anyxml *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002108 struct lys_node_uses *uses = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002109 struct lys_node_uses *uses_orig = (struct lys_node_uses *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002110 struct lys_node_grp *grp = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002111 struct lys_node_grp *grp_orig = (struct lys_node_grp *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002112 struct lys_node_rpc *rpc = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002113 struct lys_node_rpc *rpc_orig = (struct lys_node_rpc *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002114 struct lys_node_rpc_inout *io = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002115 struct lys_node_rpc_inout *io_orig = (struct lys_node_rpc_inout *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002116 struct lys_node_rpc *ntf = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002117 struct lys_node_rpc *ntf_orig = (struct lys_node_rpc *)node;
Michal Vaskoc07187d2015-08-13 15:20:57 +02002118 struct lys_node_case *cs = NULL;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002119 struct lys_node_case *cs_orig = (struct lys_node_case *)node;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002120
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002121 /* we cannot just duplicate memory since the strings are stored in
2122 * dictionary and we need to update dictionary counters.
2123 */
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002124
Radek Krejci1d82ef62015-08-07 14:44:40 +02002125 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002126 case LYS_CONTAINER:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002127 cont = calloc(1, sizeof *cont);
Radek Krejci76512572015-08-04 09:47:08 +02002128 retval = (struct lys_node *)cont;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002129 break;
2130
Radek Krejci76512572015-08-04 09:47:08 +02002131 case LYS_CHOICE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002132 choice = calloc(1, sizeof *choice);
Radek Krejci76512572015-08-04 09:47:08 +02002133 retval = (struct lys_node *)choice;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002134 break;
2135
Radek Krejci76512572015-08-04 09:47:08 +02002136 case LYS_LEAF:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002137 leaf = calloc(1, sizeof *leaf);
Radek Krejci76512572015-08-04 09:47:08 +02002138 retval = (struct lys_node *)leaf;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002139 break;
2140
Radek Krejci76512572015-08-04 09:47:08 +02002141 case LYS_LEAFLIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002142 llist = calloc(1, sizeof *llist);
Radek Krejci76512572015-08-04 09:47:08 +02002143 retval = (struct lys_node *)llist;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002144 break;
2145
Radek Krejci76512572015-08-04 09:47:08 +02002146 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002147 list = calloc(1, sizeof *list);
Radek Krejci76512572015-08-04 09:47:08 +02002148 retval = (struct lys_node *)list;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002149 break;
2150
Radek Krejci76512572015-08-04 09:47:08 +02002151 case LYS_ANYXML:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002152 anyxml = calloc(1, sizeof *anyxml);
Radek Krejci76512572015-08-04 09:47:08 +02002153 retval = (struct lys_node *)anyxml;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002154 break;
2155
Radek Krejci76512572015-08-04 09:47:08 +02002156 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002157 uses = calloc(1, sizeof *uses);
Radek Krejci76512572015-08-04 09:47:08 +02002158 retval = (struct lys_node *)uses;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002159 break;
2160
Radek Krejci76512572015-08-04 09:47:08 +02002161 case LYS_CASE:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002162 cs = calloc(1, sizeof *cs);
Radek Krejci76512572015-08-04 09:47:08 +02002163 retval = (struct lys_node *)cs;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002164 break;
2165
Radek Krejci76512572015-08-04 09:47:08 +02002166 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002167 grp = calloc(1, sizeof *grp);
2168 retval = (struct lys_node *)grp;
2169 break;
2170
Radek Krejci76512572015-08-04 09:47:08 +02002171 case LYS_RPC:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002172 rpc = calloc(1, sizeof *rpc);
2173 retval = (struct lys_node *)rpc;
2174 break;
2175
Radek Krejci76512572015-08-04 09:47:08 +02002176 case LYS_INPUT:
2177 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002178 io = calloc(1, sizeof *io);
2179 retval = (struct lys_node *)io;
2180 break;
2181
Radek Krejci76512572015-08-04 09:47:08 +02002182 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002183 ntf = calloc(1, sizeof *ntf);
2184 retval = (struct lys_node *)ntf;
Michal Vasko38d01f72015-06-15 09:41:06 +02002185 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002186
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002187 default:
Michal Vaskod23ce592015-08-06 09:55:37 +02002188 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002189 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002190 }
Radek Krejcib388c152015-06-04 17:03:03 +02002191
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002192 /*
2193 * duplicate generic part of the structure
2194 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002195 retval->name = lydict_insert(ctx, node->name, 0);
2196 retval->dsc = lydict_insert(ctx, node->dsc, 0);
2197 retval->ref = lydict_insert(ctx, node->ref, 0);
Michal Vasko71e1aa82015-08-12 12:17:51 +02002198 retval->nacm = nacm;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002199 retval->flags = node->flags;
Radek Krejci1574a8d2015-08-03 14:16:52 +02002200 if (!(retval->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002201 /* set parent's config flag */
Radek Krejci1574a8d2015-08-03 14:16:52 +02002202 retval->flags |= flags & LYS_CONFIG_MASK;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002203 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002204
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002205 retval->module = module;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002206 retval->nodetype = node->nodetype;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002207
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002208 retval->prev = retval;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002209
Radek Krejci1d82ef62015-08-07 14:44:40 +02002210 retval->features_size = node->features_size;
Radek Krejci3cf9e222015-06-18 11:37:50 +02002211 retval->features = calloc(retval->features_size, sizeof *retval->features);
Radek Krejci1d82ef62015-08-07 14:44:40 +02002212 for (i = 0; i < node->features_size; ++i) {
Michal Vasko0bd29d12015-08-19 11:45:49 +02002213 if (unres_schema_dup(module, unres, &node->features[i], UNRES_IFFEAT, &retval->features[i])) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002214 retval->features[i] = node->features[i];
Michal Vaskod23ce592015-08-06 09:55:37 +02002215 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002216 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002217
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002218 if (recursive) {
2219 /* go recursively */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002220 LY_TREE_FOR(node->child, child) {
Michal Vasko71e1aa82015-08-12 12:17:51 +02002221 aux = lys_node_dup(module, child, retval->flags, retval->nacm, 1, unres);
Radek Krejci10c760e2015-08-14 14:45:43 +02002222 if (!aux || lys_node_addchild(retval, NULL, aux)) {
Michal Vasko49168a22015-08-17 16:35:41 +02002223 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002224 }
2225 }
2226 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002227
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002228 /*
2229 * duplicate specific part of the structure
2230 */
Radek Krejci1d82ef62015-08-07 14:44:40 +02002231 switch (node->nodetype) {
Radek Krejci76512572015-08-04 09:47:08 +02002232 case LYS_CONTAINER:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002233 if (cont_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002234 cont->when = lys_when_dup(ctx, cont_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002235 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002236 cont->presence = lydict_insert(ctx, cont_orig->presence, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002237
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002238 cont->must_size = cont_orig->must_size;
2239 cont->tpdf_size = cont_orig->tpdf_size;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002240
Radek Krejci1d82ef62015-08-07 14:44:40 +02002241 cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size);
Radek Krejci1d82ef62015-08-07 14:44:40 +02002242 cont->tpdf = lys_tpdf_dup(module, node->parent, cont_orig->tpdf, cont->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002243 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002244
Radek Krejci76512572015-08-04 09:47:08 +02002245 case LYS_CHOICE:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002246 if (choice_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002247 choice->when = lys_when_dup(ctx, choice_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002248 }
2249
2250 if (choice_orig->dflt) {
Michal Vasko165dc4a2015-10-23 09:44:27 +02002251 rc = lys_get_sibling(choice->module, choice->child, NULL, 0, choice_orig->dflt->name, 0, LYS_ANYXML
Radek Krejci76512572015-08-04 09:47:08 +02002252 | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST
Michal Vasko3ab70fc2015-08-17 14:06:23 +02002253 | LYS_LIST, &choice->dflt);
Michal Vasko49168a22015-08-17 16:35:41 +02002254 if (rc) {
2255 if (rc == EXIT_FAILURE) {
2256 LOGINT;
2257 }
2258 goto error;
2259 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002260 } else {
Michal Vaskoc68a2a22015-08-06 09:57:24 +02002261 /* useless to check return value, we don't know whether
2262 * there really wasn't any default defined or it just hasn't
2263 * been resolved, we just hope for the best :)
2264 */
Michal Vasko0bd29d12015-08-19 11:45:49 +02002265 unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002266 }
2267 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002268
Radek Krejci76512572015-08-04 09:47:08 +02002269 case LYS_LEAF:
Michal Vaskob84f88a2015-09-24 13:16:10 +02002270 if (lys_type_dup(module, node->parent, &(leaf->type), &(leaf_orig->type), unres)) {
2271 goto error;
2272 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002273 leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0);
2274
2275 if (leaf_orig->dflt) {
2276 leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0);
Michal Vasko0bd29d12015-08-19 11:45:49 +02002277 if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt, 0) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002278 goto error;
2279 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002280 }
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002281
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002282 leaf->must_size = leaf_orig->must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002283 leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size);
Radek Krejci00768f42015-06-18 17:04:04 +02002284
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002285 if (leaf_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002286 leaf->when = lys_when_dup(ctx, leaf_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002287 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002288 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002289
Radek Krejci76512572015-08-04 09:47:08 +02002290 case LYS_LEAFLIST:
Michal Vaskob84f88a2015-09-24 13:16:10 +02002291 if (lys_type_dup(module, node->parent, &(llist->type), &(llist_orig->type), unres)) {
2292 goto error;
2293 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002294 llist->units = lydict_insert(module->ctx, llist_orig->units, 0);
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002295
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002296 llist->min = llist_orig->min;
2297 llist->max = llist_orig->max;
2298
2299 llist->must_size = llist_orig->must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002300 llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size);
Radek Krejci00768f42015-06-18 17:04:04 +02002301
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002302 if (llist_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002303 llist->when = lys_when_dup(ctx, llist_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002304 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002305 break;
2306
Radek Krejci76512572015-08-04 09:47:08 +02002307 case LYS_LIST:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002308 list->min = list_orig->min;
2309 list->max = list_orig->max;
2310
2311 list->must_size = list_orig->must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002312 list->must = lys_restr_dup(ctx, list_orig->must, list->must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002313
Radek Krejci581ce772015-11-10 17:22:40 +01002314 list->tpdf_size = list_orig->tpdf_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002315 list->tpdf = lys_tpdf_dup(module, node->parent, list_orig->tpdf, list->tpdf_size, unres);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002316
Radek Krejci581ce772015-11-10 17:22:40 +01002317 list->keys_size = list_orig->keys_size;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002318 if (list->keys_size) {
2319 list->keys = calloc(list->keys_size, sizeof *list->keys);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002320
2321 /* we managed to resolve it before, resolve it again manually */
2322 if (list_orig->keys[0]) {
2323 for (i = 0; i < list->keys_size; ++i) {
Michal Vasko165dc4a2015-10-23 09:44:27 +02002324 rc = lys_get_sibling(list->module, list->child, NULL, 0, list_orig->keys[i]->name, 0, LYS_LEAF,
2325 (struct lys_node **)&list->keys[i]);
Michal Vasko49168a22015-08-17 16:35:41 +02002326 if (rc) {
2327 if (rc == EXIT_FAILURE) {
2328 LOGINT;
2329 }
2330 goto error;
2331 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002332 }
2333 /* it was not resolved yet, add unres copy */
2334 } else {
Michal Vasko0bd29d12015-08-19 11:45:49 +02002335 if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) {
Michal Vaskod23ce592015-08-06 09:55:37 +02002336 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002337 goto error;
Michal Vaskod23ce592015-08-06 09:55:37 +02002338 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002339 }
2340 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002341
Radek Krejci581ce772015-11-10 17:22:40 +01002342 list->unique_size = list_orig->unique_size;
2343 list->unique = malloc(list->unique_size * sizeof *list->unique);
2344 for (i = 0; i < list->unique_size; ++i) {
2345 list->unique[i].expr_size = list_orig->unique[i].expr_size;
2346 list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr);
2347 for (j = 0; j < list->unique[i].expr_size; j++) {
2348 list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0);
2349
2350 /* if it stays in unres list, duplicate it also there */
Michal Vasko0bd29d12015-08-19 11:45:49 +02002351 unres_schema_dup(module, unres, &list_orig->unique[i], UNRES_LIST_UNIQ, &list->unique[i]);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002352 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002353 }
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002354
2355 if (list_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002356 list->when = lys_when_dup(ctx, list_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002357 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002358 break;
2359
Radek Krejci76512572015-08-04 09:47:08 +02002360 case LYS_ANYXML:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002361 anyxml->must_size = anyxml_orig->must_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002362 anyxml->must = lys_restr_dup(ctx, anyxml_orig->must, anyxml->must_size);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002363
2364 if (anyxml_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002365 anyxml->when = lys_when_dup(ctx, anyxml_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002366 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002367 break;
2368
Radek Krejci76512572015-08-04 09:47:08 +02002369 case LYS_USES:
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002370 uses->grp = uses_orig->grp;
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002371
2372 if (uses_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002373 uses->when = lys_when_dup(ctx, uses_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002374 }
2375
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002376 uses->refine_size = uses_orig->refine_size;
Michal Vasko0d204592015-10-07 09:50:04 +02002377 uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002378 uses->augment_size = uses_orig->augment_size;
Michal Vaskoffa45cb2015-08-21 12:58:04 +02002379 uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size);
Michal Vaskof8c128d2015-08-06 15:25:28 +02002380 if (!uses->child) {
Michal Vasko0bd29d12015-08-19 11:45:49 +02002381 if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL, 0) == -1) {
Michal Vasko49168a22015-08-17 16:35:41 +02002382 goto error;
2383 }
Michal Vaskof8c128d2015-08-06 15:25:28 +02002384 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002385 break;
2386
Radek Krejci76512572015-08-04 09:47:08 +02002387 case LYS_CASE:
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002388 if (cs_orig->when) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002389 cs->when = lys_when_dup(ctx, cs_orig->when);
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +02002390 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002391 break;
2392
Radek Krejci76512572015-08-04 09:47:08 +02002393 case LYS_GROUPING:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002394 grp->tpdf_size = grp_orig->tpdf_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002395 grp->tpdf = lys_tpdf_dup(module, node->parent, grp_orig->tpdf, grp->tpdf_size, unres);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002396 break;
2397
Radek Krejci76512572015-08-04 09:47:08 +02002398 case LYS_RPC:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002399 rpc->tpdf_size = rpc_orig->tpdf_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002400 rpc->tpdf = lys_tpdf_dup(module, node->parent, rpc_orig->tpdf, rpc->tpdf_size, unres);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002401 break;
2402
Radek Krejci76512572015-08-04 09:47:08 +02002403 case LYS_INPUT:
2404 case LYS_OUTPUT:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002405 io->tpdf_size = io_orig->tpdf_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002406 io->tpdf = lys_tpdf_dup(module, node->parent, io_orig->tpdf, io->tpdf_size, unres);
Radek Krejcid12f57b2015-08-06 10:43:39 +02002407 break;
2408
Radek Krejci76512572015-08-04 09:47:08 +02002409 case LYS_NOTIF:
Radek Krejcid12f57b2015-08-06 10:43:39 +02002410 ntf->tpdf_size = ntf_orig->tpdf_size;
Radek Krejci1d82ef62015-08-07 14:44:40 +02002411 ntf->tpdf = lys_tpdf_dup(module, node->parent, ntf_orig->tpdf, ntf->tpdf_size, unres);
Radek Krejcia01e5432015-06-16 10:35:25 +02002412 break;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002413
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002414 default:
Radek Krejci00768f42015-06-18 17:04:04 +02002415 /* LY_NODE_AUGMENT */
Michal Vaskod23ce592015-08-06 09:55:37 +02002416 LOGINT;
Michal Vasko49168a22015-08-17 16:35:41 +02002417 goto error;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002418 }
2419
2420 return retval;
Michal Vasko49168a22015-08-17 16:35:41 +02002421
2422error:
2423
2424 lys_node_free(retval);
2425 return NULL;
Radek Krejci8bc9ca02015-06-04 15:52:46 +02002426}
2427
Michal Vasko13b15832015-08-19 11:04:48 +02002428void
2429lys_free(struct lys_module *module, int free_int_mods)
Radek Krejciefaeba32015-05-27 14:30:57 +02002430{
Radek Krejcidce51452015-06-16 15:20:08 +02002431 struct ly_ctx *ctx;
2432 int i;
2433
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002434 if (!module) {
2435 return;
2436 }
Radek Krejciefaeba32015-05-27 14:30:57 +02002437
Radek Krejcidce51452015-06-16 15:20:08 +02002438 /* remove schema from the context */
2439 ctx = module->ctx;
2440 if (ctx->models.used) {
2441 for (i = 0; i < ctx->models.used; i++) {
2442 if (ctx->models.list[i] == module) {
2443 /* replace the position in the list by the last module in the list */
2444 ctx->models.used--;
2445 ctx->models.list[i] = ctx->models.list[ctx->models.used];
2446 ctx->models.list[ctx->models.used] = NULL;
2447 /* we are done */
2448 break;
2449 }
2450 }
2451 }
2452
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002453 /* common part with struct ly_submodule */
Michal Vasko13b15832015-08-19 11:04:48 +02002454 module_free_common(module, free_int_mods);
Radek Krejciefaeba32015-05-27 14:30:57 +02002455
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002456 /* specific items to free */
2457 lydict_remove(module->ctx, module->ns);
2458 lydict_remove(module->ctx, module->prefix);
Radek Krejci6793db02015-05-22 17:49:54 +02002459
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02002460 free(module);
Radek Krejcida04f4a2015-05-21 12:54:09 +02002461}
Radek Krejci7e97c352015-06-19 16:26:34 +02002462
2463/*
2464 * op: 1 - enable, 0 - disable
2465 */
2466static int
Radek Krejci1d82ef62015-08-07 14:44:40 +02002467lys_features_change(struct lys_module *module, const char *name, int op)
Radek Krejci7e97c352015-06-19 16:26:34 +02002468{
2469 int all = 0;
2470 int i, j, k;
2471
2472 if (!module || !name || !strlen(name)) {
2473 return EXIT_FAILURE;
2474 }
2475
2476 if (!strcmp(name, "*")) {
2477 /* enable all */
2478 all = 1;
2479 }
2480
2481 /* module itself */
2482 for (i = 0; i < module->features_size; i++) {
2483 if (all || !strcmp(module->features[i].name, name)) {
2484 if (op) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002485 module->features[i].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002486 /* enable referenced features (recursion) */
2487 for (k = 0; k < module->features[i].features_size; k++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +02002488 lys_features_change(module->features[i].features[k]->module,
Radek Krejci7e97c352015-06-19 16:26:34 +02002489 module->features[i].features[k]->name, op);
2490 }
2491 } else {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002492 module->features[i].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002493 }
2494 if (!all) {
2495 return EXIT_SUCCESS;
2496 }
2497 }
2498 }
2499
2500 /* submodules */
2501 for (j = 0; j < module->inc_size; j++) {
2502 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2503 if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) {
2504 if (op) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002505 module->inc[j].submodule->features[i].flags |= LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002506 } else {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002507 module->inc[j].submodule->features[i].flags &= ~LYS_FENABLED;
Radek Krejci7e97c352015-06-19 16:26:34 +02002508 }
2509 if (!all) {
2510 return EXIT_SUCCESS;
2511 }
2512 }
2513 }
2514 }
2515
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002516 /* TODO submodules of submodules ... */
2517
Radek Krejci7e97c352015-06-19 16:26:34 +02002518 if (all) {
2519 return EXIT_SUCCESS;
2520 } else {
2521 return EXIT_FAILURE;
2522 }
2523}
2524
2525API int
Radek Krejcib8048692015-08-05 13:36:34 +02002526lys_features_enable(struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002527{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002528 return lys_features_change(module, feature, 1);
Radek Krejci7e97c352015-06-19 16:26:34 +02002529}
2530
2531API int
Radek Krejcib8048692015-08-05 13:36:34 +02002532lys_features_disable(struct lys_module *module, const char *feature)
Radek Krejci7e97c352015-06-19 16:26:34 +02002533{
Radek Krejci1d82ef62015-08-07 14:44:40 +02002534 return lys_features_change(module, feature, 0);
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002535}
2536
2537API int
Radek Krejcib8048692015-08-05 13:36:34 +02002538lys_features_state(struct lys_module *module, const char *feature)
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002539{
2540 int i, j;
2541
2542 if (!module || !feature) {
2543 return -1;
2544 }
2545
2546 /* search for the specified feature */
2547 /* module itself */
2548 for (i = 0; i < module->features_size; i++) {
2549 if (!strcmp(feature, module->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002550 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002551 return 1;
2552 } else {
2553 return 0;
2554 }
2555 }
2556 }
2557
2558 /* submodules */
2559 for (j = 0; j < module->inc_size; j++) {
2560 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
2561 if (!strcmp(feature, module->inc[j].submodule->features[i].name)) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002562 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002563 return 1;
2564 } else {
2565 return 0;
2566 }
2567 }
2568 }
2569 }
2570
2571 /* TODO submodules of submodules ... */
2572
2573 /* feature definition not found */
2574 return -1;
Radek Krejci7e97c352015-06-19 16:26:34 +02002575}
Michal Vasko2367e7c2015-07-07 11:33:44 +02002576
Radek Krejci96a10da2015-07-30 11:00:14 +02002577API const char **
Radek Krejcib8048692015-08-05 13:36:34 +02002578lys_features_list(struct lys_module *module, uint8_t **states)
Michal Vasko2367e7c2015-07-07 11:33:44 +02002579{
Radek Krejci96a10da2015-07-30 11:00:14 +02002580 const char **result = NULL;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002581 int i, j;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002582 unsigned int count;
2583
2584 if (!module) {
2585 return NULL;
2586 }
2587
2588 count = module->features_size;
2589 for (i = 0; i < module->inc_size; i++) {
2590 count += module->inc[i].submodule->features_size;
2591 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002592 result = malloc((count + 1) * sizeof *result);
2593 if (states) {
2594 *states = malloc((count + 1) * sizeof **states);
Michal Vasko2367e7c2015-07-07 11:33:44 +02002595 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002596 count = 0;
2597
2598 /* module itself */
2599 for (i = 0; i < module->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002600 result[count] = module->features[i].name;
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002601 if (states) {
Radek Krejci1574a8d2015-08-03 14:16:52 +02002602 if (module->features[i].flags & LYS_FENABLED) {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002603 (*states)[count] = 1;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002604 } else {
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002605 (*states)[count] = 0;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002606 }
2607 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002608 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002609 }
2610
2611 /* submodules */
2612 for (j = 0; j < module->inc_size; j++) {
2613 for (i = 0; i < module->inc[j].submodule->features_size; i++) {
Radek Krejci96a10da2015-07-30 11:00:14 +02002614 result[count] = module->inc[j].submodule->features[i].name;
Radek Krejci374b94e2015-08-13 09:44:22 +02002615 if (states) {
2616 if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) {
2617 (*states)[count] = 1;
2618 } else {
2619 (*states)[count] = 0;
2620 }
Michal Vasko2367e7c2015-07-07 11:33:44 +02002621 }
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002622 count++;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002623 }
2624 }
2625
Radek Krejcie98bb4b2015-07-30 14:21:41 +02002626 /* TODO submodules of submodules ... */
2627
2628 /* terminating NULL byte */
Michal Vasko2367e7c2015-07-07 11:33:44 +02002629 result[count] = NULL;
Michal Vasko2367e7c2015-07-07 11:33:44 +02002630
2631 return result;
2632}
Michal Vaskobaefb032015-09-24 14:52:10 +02002633
2634API struct lys_node *
2635lys_parent(struct lys_node *node)
2636{
2637 if (!node || !node->parent) {
2638 return NULL;
2639 }
2640
2641 if (node->parent->nodetype == LYS_AUGMENT) {
2642 return ((struct lys_node_augment *)node->parent)->target;
2643 }
2644
2645 return node->parent;
2646}