Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 2 | * @file tree_schema.c |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 4 | * @brief Manipulation with libyang schema data structures |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 13 | */ |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 19 | #include <limits.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <sys/mman.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 23 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 24 | #include <fcntl.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 25 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <errno.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 28 | |
| 29 | #include "common.h" |
| 30 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 31 | #include "parser.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 32 | #include "resolve.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 33 | #include "xml.h" |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 34 | #include "xpath.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 35 | #include "xml_internal.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 36 | #include "tree_internal.h" |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 37 | #include "validation.h" |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 38 | #include "parser_yang.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 39 | |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 40 | static int |
| 41 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 42 | int tpdftype, struct unres_schema *unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 43 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 44 | API const struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 45 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 46 | { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 47 | int i; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 48 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 49 | if (!node) { |
| 50 | return NULL; |
| 51 | } |
| 52 | |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 53 | check: |
| 54 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 55 | /* input/output does not have if-feature, so skip them */ |
| 56 | |
| 57 | /* check local if-features */ |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 58 | for (i = 0; i < node->iffeature_size; i++) { |
Radek Krejci | 69b8d92 | 2016-07-27 13:13:41 +0200 | [diff] [blame] | 59 | if (!resolve_iffeature(&node->iffeature[i])) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 60 | return node; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 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 Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 73 | } else if (node->parent) { |
| 74 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 75 | } else { |
| 76 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 79 | 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 Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 88 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 89 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 90 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 91 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 92 | const struct lys_node *node, *parent = NULL; |
| 93 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 94 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 95 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 96 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 97 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 98 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 99 | /* fill the lengths in case the caller is so indifferent */ |
| 100 | if (!mod_name_len) { |
| 101 | mod_name_len = strlen(mod_name); |
| 102 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 103 | if (!nam_len) { |
| 104 | nam_len = strlen(name); |
| 105 | } |
| 106 | |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 107 | while (siblings && (siblings->nodetype == LYS_USES)) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 108 | siblings = siblings->child; |
| 109 | } |
Michal Vasko | 9e635ac | 2016-10-17 11:44:09 +0200 | [diff] [blame] | 110 | if (!siblings) { |
| 111 | /* unresolved uses */ |
| 112 | return EXIT_FAILURE; |
| 113 | } |
| 114 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 115 | if (siblings->nodetype == LYS_GROUPING) { |
| 116 | for (node = siblings; (node->nodetype == LYS_GROUPING) && (node->prev != siblings); node = node->prev); |
| 117 | if (node->nodetype == LYS_GROUPING) { |
| 118 | /* we went through all the siblings, only groupings there - no valid sibling */ |
| 119 | return EXIT_FAILURE; |
| 120 | } |
| 121 | /* update siblings to be valid */ |
| 122 | siblings = node; |
| 123 | } |
| 124 | |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 125 | /* set parent correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 126 | parent = lys_parent(siblings); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 127 | |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 128 | /* go up all uses */ |
| 129 | while (parent && (parent->nodetype == LYS_USES)) { |
| 130 | parent = lys_parent(parent); |
Michal Vasko | 4cf7dba | 2016-10-14 09:42:01 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 133 | if (!parent) { |
Michal Vasko | 680f8b4 | 2016-10-17 10:27:37 +0200 | [diff] [blame] | 134 | /* handle situation when there is a top-level uses referencing a foreign grouping */ |
| 135 | for (node = siblings; lys_parent(node) && (node->nodetype == LYS_USES); node = lys_parent(node)); |
| 136 | mod = lys_node_module(node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 139 | /* try to find the node */ |
| 140 | node = NULL; |
| 141 | while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 142 | if (!type || (node->nodetype & type)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 143 | /* module name comparison */ |
| 144 | node_mod_name = lys_node_module(node)->name; |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 145 | if (!ly_strequal(node_mod_name, mod_name, 1) && (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len])) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 146 | continue; |
| 147 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 148 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 149 | /* direct name check */ |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 150 | if (ly_strequal(node->name, name, 1) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 151 | if (ret) { |
| 152 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 153 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 154 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return EXIT_FAILURE; |
| 160 | } |
| 161 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 162 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 163 | lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type, |
| 164 | const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 165 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 166 | const struct lys_node *node; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 167 | |
| 168 | assert(siblings && name); |
| 169 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 170 | |
| 171 | /* find the beginning */ |
| 172 | while (siblings->prev->next) { |
| 173 | siblings = siblings->prev; |
| 174 | } |
| 175 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 176 | if (!mod) { |
| 177 | mod = siblings->module; |
| 178 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 179 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 180 | /* try to find the node */ |
| 181 | node = NULL; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 182 | while ((node = lys_getnext(node, lys_parent(siblings), mod, 0))) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 183 | if (!type || (node->nodetype & type)) { |
| 184 | /* module check */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 185 | if (lys_node_module(node) != lys_main_module(mod)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 186 | continue; |
| 187 | } |
| 188 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 189 | /* direct name check */ |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 190 | if (ly_strequal(node->name, name, 0)) { |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 191 | if (ret) { |
| 192 | *ret = node; |
| 193 | } |
| 194 | return EXIT_SUCCESS; |
| 195 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 196 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return EXIT_FAILURE; |
| 200 | } |
| 201 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 202 | API const struct lys_node * |
| 203 | lys_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module, int options) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 204 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 205 | const struct lys_node *next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 206 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 207 | if (!last) { |
| 208 | /* first call */ |
| 209 | |
| 210 | /* get know where to start */ |
| 211 | if (parent) { |
| 212 | /* schema subtree */ |
| 213 | next = last = parent->child; |
| 214 | } else { |
| 215 | /* top level data */ |
| 216 | assert(module); |
| 217 | next = last = module->data; |
| 218 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 219 | } else if ((last->nodetype == LYS_USES) && (options & LYS_GETNEXT_INTOUSES) && last->child) { |
| 220 | /* continue with uses content */ |
| 221 | next = last->child; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 222 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 223 | /* continue after the last returned value */ |
| 224 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | repeat: |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 228 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 229 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 230 | return next; |
| 231 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 232 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 235 | if (!next) { /* cover case when parent is augment */ |
| 236 | if (!last || last->parent == parent || lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 237 | /* no next element */ |
| 238 | return NULL; |
| 239 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 240 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 241 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 242 | goto repeat; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 243 | } else { |
| 244 | last = next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 248 | case LYS_INPUT: |
| 249 | case LYS_OUTPUT: |
| 250 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 251 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 252 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 253 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 254 | } else { |
| 255 | next = next->next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 256 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 257 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 258 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 259 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 260 | if (options & LYS_GETNEXT_WITHCASE) { |
| 261 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 262 | } else if (next->child) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 263 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 264 | } else { |
| 265 | next = next->next; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 266 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 267 | goto repeat; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 268 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 269 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 270 | /* go into */ |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 271 | if (options & LYS_GETNEXT_WITHUSES) { |
| 272 | return next; |
| 273 | } else if (next->child) { |
| 274 | next = next->child; |
| 275 | } else { |
| 276 | next = next->next; |
| 277 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 278 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 279 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 280 | case LYS_RPC: |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 281 | case LYS_ACTION: |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 282 | case LYS_NOTIF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 283 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 284 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 285 | case LYS_ANYDATA: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 286 | case LYS_LIST: |
| 287 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 288 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 289 | |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 290 | case LYS_CONTAINER: |
| 291 | if (!((struct lys_node_container *)next)->presence && (options & LYS_GETNEXT_INTONPCONT)) { |
| 292 | if (next->child) { |
| 293 | /* go into */ |
| 294 | next = next->child; |
| 295 | } else { |
| 296 | next = next->next; |
| 297 | } |
| 298 | goto repeat; |
| 299 | } else { |
| 300 | return next; |
| 301 | } |
| 302 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 303 | case LYS_CHOICE: |
| 304 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 305 | return next; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 306 | } else if (next->child) { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 307 | /* go into */ |
| 308 | next = next->child; |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 309 | } else { |
| 310 | next = next->next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 311 | } |
Radek Krejci | 972724f | 2016-08-12 15:24:40 +0200 | [diff] [blame] | 312 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 313 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 314 | default: |
| 315 | /* we should not be here */ |
| 316 | return NULL; |
| 317 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 318 | |
| 319 | |
| 320 | } |
| 321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 322 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 323 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 324 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 325 | struct lys_node *parent, *first; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 326 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 327 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 328 | if (!node) { |
| 329 | return; |
| 330 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 331 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 332 | /* unlink from data model if necessary */ |
| 333 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 334 | /* get main module with data tree */ |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 335 | main_module = lys_node_module(node); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 336 | if (main_module->data == node) { |
| 337 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 338 | } |
| 339 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 340 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 341 | /* store pointers to important nodes */ |
| 342 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 343 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 344 | /* handle augments - first, unlink it from the augment parent ... */ |
| 345 | if (parent->child == node) { |
| 346 | parent->child = node->next; |
| 347 | } |
| 348 | /* and then continue with the target parent */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 349 | parent = ((struct lys_node_augment *)parent)->target; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 350 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 351 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 352 | /* unlink from parent */ |
| 353 | if (parent) { |
| 354 | if (parent->child == node) { |
| 355 | parent->child = node->next; |
| 356 | } |
| 357 | node->parent = NULL; |
| 358 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 359 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 360 | /* unlink from siblings */ |
| 361 | if (node->prev == node) { |
| 362 | /* there are no more siblings */ |
| 363 | return; |
| 364 | } |
| 365 | if (node->next) { |
| 366 | node->next->prev = node->prev; |
| 367 | } else { |
| 368 | /* unlinking the last element */ |
| 369 | if (parent) { |
| 370 | first = parent->child; |
| 371 | } else { |
| 372 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 373 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 374 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | first->prev = node->prev; |
| 378 | } |
| 379 | if (node->prev->next) { |
| 380 | node->prev->next = node->next; |
| 381 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 382 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 383 | /* clean up the unlinked element */ |
| 384 | node->next = NULL; |
| 385 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 388 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 389 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 390 | { |
| 391 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 392 | |
| 393 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 394 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 395 | if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) { |
| 396 | par_iter = par_iter->parent->module->data; |
| 397 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
Michal Vasko | 6f929da | 2015-10-02 16:23:25 +0200 | [diff] [blame] | 402 | if (par_iter->parent && (par_iter->parent->nodetype & (LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_USES))) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 403 | continue; |
| 404 | } |
| 405 | |
| 406 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 407 | if (!stop) { |
| 408 | stop = par_iter; |
| 409 | } else if (iter == stop) { |
| 410 | break; |
| 411 | } |
| 412 | if (iter->nodetype != LYS_GROUPING) { |
| 413 | continue; |
| 414 | } |
| 415 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 416 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 417 | return (struct lys_node_grp *)iter; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 422 | return NULL; |
| 423 | } |
| 424 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 425 | /* |
| 426 | * get next grouping in the root's subtree, in the |
| 427 | * first call, tha last is NULL |
| 428 | */ |
| 429 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 430 | lys_get_next_grouping(struct lys_node_grp *lastgrp, struct lys_node *root) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 431 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 432 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 433 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 434 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 435 | assert(root); |
| 436 | |
| 437 | if (!last) { |
| 438 | last = root; |
| 439 | } |
| 440 | |
| 441 | while (1) { |
| 442 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 443 | next = last->child; |
| 444 | } else { |
| 445 | next = NULL; |
| 446 | } |
| 447 | if (!next) { |
| 448 | if (last == root) { |
| 449 | /* we are done */ |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | /* no children, go to siblings */ |
| 454 | next = last->next; |
| 455 | } |
| 456 | while (!next) { |
| 457 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 458 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 459 | /* we are done */ |
| 460 | return NULL; |
| 461 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 462 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 463 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | if (next->nodetype == LYS_GROUPING) { |
| 467 | return (struct lys_node_grp *)next; |
| 468 | } |
| 469 | |
| 470 | last = next; |
| 471 | } |
| 472 | } |
| 473 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 474 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 475 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 476 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 477 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 478 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 479 | struct lys_node_grp *grp; |
| 480 | int down; |
| 481 | |
| 482 | assert(node); |
| 483 | |
| 484 | if (!parent) { |
| 485 | assert(module); |
| 486 | } else { |
| 487 | module = parent->module; |
| 488 | } |
| 489 | |
| 490 | switch (node->nodetype) { |
| 491 | case LYS_GROUPING: |
| 492 | /* 6.2.1, rule 6 */ |
| 493 | if (parent) { |
| 494 | if (parent->child) { |
| 495 | down = 1; |
| 496 | start = parent->child; |
| 497 | } else { |
| 498 | down = 0; |
| 499 | start = parent; |
| 500 | } |
| 501 | } else { |
| 502 | down = 1; |
| 503 | start = module->data; |
| 504 | } |
| 505 | /* go up */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 506 | if (lys_find_grouping_up(node->name, start)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 507 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "grouping", node->name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 508 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 509 | } |
| 510 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 511 | if (down) { |
| 512 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 513 | if (!stop) { |
| 514 | stop = start; |
| 515 | } else if (iter == stop) { |
| 516 | break; |
| 517 | } |
| 518 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | grp = NULL; |
| 523 | while ((grp = lys_get_next_grouping(grp, iter))) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 524 | if (ly_strequal(node->name, grp->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 525 | LOGVAL(LYE_DUPID,LY_VLOG_LYS, node, "grouping", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 526 | return EXIT_FAILURE; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | break; |
| 532 | case LYS_LEAF: |
| 533 | case LYS_LEAFLIST: |
| 534 | case LYS_LIST: |
| 535 | case LYS_CONTAINER: |
| 536 | case LYS_CHOICE: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 537 | case LYS_ANYDATA: |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 538 | /* 6.2.1, rule 7 */ |
| 539 | if (parent) { |
| 540 | iter = parent; |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 541 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE | LYS_AUGMENT))) { |
| 542 | if (iter->nodetype == LYS_AUGMENT) { |
| 543 | if (((struct lys_node_augment *)iter)->target) { |
| 544 | /* augment is resolved, go up */ |
| 545 | iter = ((struct lys_node_augment *)iter)->target; |
| 546 | continue; |
| 547 | } |
| 548 | /* augment is not resolved, this is the final parent */ |
| 549 | break; |
| 550 | } |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 551 | iter = iter->parent; |
| 552 | } |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 553 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 554 | if (!iter) { |
| 555 | stop = NULL; |
| 556 | iter = module->data; |
| 557 | } else { |
| 558 | stop = iter; |
| 559 | iter = iter->child; |
| 560 | } |
| 561 | } else { |
| 562 | stop = NULL; |
| 563 | iter = module->data; |
| 564 | } |
| 565 | while (iter) { |
| 566 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 567 | iter = iter->child; |
| 568 | continue; |
| 569 | } |
| 570 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 571 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYDATA)) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 572 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 573 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, strnodetype(node->nodetype), node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 574 | return EXIT_FAILURE; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /* special case for choice - we must check the choice's name as |
| 579 | * well as the names of nodes under the choice |
| 580 | */ |
| 581 | if (iter->nodetype == LYS_CHOICE) { |
| 582 | iter = iter->child; |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | /* go to siblings */ |
| 587 | if (!iter->next) { |
| 588 | /* no sibling, go to parent's sibling */ |
| 589 | do { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 590 | /* for parent LYS_AUGMENT */ |
| 591 | if (iter->parent == stop) { |
| 592 | iter = stop; |
| 593 | break; |
| 594 | } |
| 595 | iter = lys_parent(iter); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 596 | if (iter && iter->next) { |
| 597 | break; |
| 598 | } |
| 599 | } while (iter != stop); |
| 600 | |
| 601 | if (iter == stop) { |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | iter = iter->next; |
| 606 | } |
| 607 | break; |
| 608 | case LYS_CASE: |
| 609 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 610 | if (parent) { |
| 611 | start = parent->child; |
| 612 | } else { |
| 613 | start = module->data; |
| 614 | } |
| 615 | |
| 616 | LY_TREE_FOR(start, iter) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 617 | if (!(iter->nodetype & (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 618 | continue; |
| 619 | } |
| 620 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 621 | if (iter->module == node->module && ly_strequal(iter->name, node->name, 1)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 622 | LOGVAL(LYE_DUPID, LY_VLOG_LYS, node, "case", node->name); |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 623 | return EXIT_FAILURE; |
| 624 | } |
| 625 | } |
| 626 | break; |
| 627 | default: |
| 628 | /* no check needed */ |
| 629 | break; |
| 630 | } |
| 631 | |
| 632 | return EXIT_SUCCESS; |
| 633 | } |
| 634 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 635 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 636 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 637 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 638 | { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 639 | struct lys_node *iter; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 640 | struct lys_node_inout *in, *out, *inout; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 641 | int type; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 642 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 643 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 644 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 645 | if (parent) { |
| 646 | type = parent->nodetype; |
| 647 | module = parent->module; |
| 648 | } else { |
| 649 | assert(module); |
Radek Krejci | fe3a138 | 2016-11-04 10:30:11 +0100 | [diff] [blame] | 650 | assert(!(child->nodetype & (LYS_INPUT | LYS_OUTPUT))); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 651 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 654 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 655 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 656 | case LYS_CONTAINER: |
| 657 | case LYS_LIST: |
| 658 | case LYS_GROUPING: |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 659 | case LYS_USES: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 660 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 661 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Michal Vasko | b15cae2 | 2016-09-15 09:40:56 +0200 | [diff] [blame] | 662 | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION | LYS_NOTIF))) { |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 663 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 664 | return EXIT_FAILURE; |
| 665 | } |
| 666 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 667 | case LYS_INPUT: |
| 668 | case LYS_OUTPUT: |
| 669 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 670 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 671 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 672 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 673 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 674 | return EXIT_FAILURE; |
| 675 | } |
| 676 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 677 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 678 | if (!(child->nodetype & |
Pavol Vican | 47a1b0a | 2016-09-20 10:18:24 +0200 | [diff] [blame] | 679 | (LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 680 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "choice"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 681 | return EXIT_FAILURE; |
| 682 | } |
| 683 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 684 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 685 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 686 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 687 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "case"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 688 | return EXIT_FAILURE; |
| 689 | } |
| 690 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 691 | case LYS_RPC: |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 692 | case LYS_ACTION: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 693 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 694 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "rpc"); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 695 | return EXIT_FAILURE; |
| 696 | } |
| 697 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 698 | case LYS_LEAF: |
| 699 | case LYS_LEAFLIST: |
| 700 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 701 | case LYS_ANYDATA: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 702 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
| 703 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, NULL, "The \"%s\" statement cannot have any data substatement.", |
Michal Vasko | 6ea3e36 | 2016-03-11 10:25:36 +0100 | [diff] [blame] | 704 | strnodetype(parent->nodetype)); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 705 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 706 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 707 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 708 | (LYS_ANYDATA | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
Michal Vasko | ca7cbc4 | 2016-07-01 11:36:53 +0200 | [diff] [blame] | 709 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_ACTION))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 710 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), strnodetype(parent->nodetype)); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 711 | return EXIT_FAILURE; |
| 712 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 713 | break; |
| 714 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 715 | /* top level */ |
| 716 | if (!(child->nodetype & |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 717 | (LYS_ANYDATA | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 718 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 719 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, parent, strnodetype(child->nodetype), "(sub)module"); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 720 | return EXIT_FAILURE; |
| 721 | } |
| 722 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 723 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /* check identifier uniqueness */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 727 | if (lys_check_id(child, parent, module)) { |
| 728 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 729 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 730 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 731 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 732 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 733 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 734 | |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 735 | if (child->nodetype & (LYS_INPUT | LYS_OUTPUT)) { |
| 736 | /* replace the implicit input/output node */ |
| 737 | if (child->nodetype == LYS_OUTPUT) { |
| 738 | inout = (struct lys_node_inout *)parent->child->next; |
| 739 | } else { /* LYS_INPUT */ |
| 740 | inout = (struct lys_node_inout *)parent->child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 741 | parent->child = child; |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 742 | } |
| 743 | if (inout->next) { |
| 744 | child->next = inout->next; |
| 745 | inout->next->prev = child; |
| 746 | inout->next = NULL; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 747 | } else { |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 748 | parent->child->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 749 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 750 | child->prev = inout->prev; |
| 751 | if (inout->prev->next) { |
| 752 | inout->prev->next = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 753 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 754 | inout->prev = (struct lys_node *)inout; |
| 755 | child->parent = parent; |
| 756 | inout->parent = NULL; |
| 757 | lys_node_free((struct lys_node *)inout, NULL, 0); |
| 758 | } else { |
| 759 | /* connect the child correctly */ |
| 760 | if (!parent) { |
| 761 | if (module->data) { |
| 762 | module->data->prev->next = child; |
| 763 | child->prev = module->data->prev; |
| 764 | module->data->prev = child; |
| 765 | } else { |
| 766 | module->data = child; |
| 767 | } |
| 768 | } else { |
| 769 | if (!parent->child) { |
| 770 | /* the only/first child of the parent */ |
| 771 | parent->child = child; |
| 772 | child->parent = parent; |
| 773 | iter = child; |
| 774 | } else { |
| 775 | /* add a new child at the end of parent's child list */ |
| 776 | iter = parent->child->prev; |
| 777 | iter->next = child; |
| 778 | child->prev = iter; |
| 779 | } |
| 780 | while (iter->next) { |
| 781 | iter = iter->next; |
| 782 | iter->parent = parent; |
| 783 | } |
| 784 | parent->child->prev = iter; |
| 785 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 786 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 787 | |
Michal Vasko | e022a56 | 2016-09-27 14:24:15 +0200 | [diff] [blame] | 788 | /* check config value (but ignore them in groupings and augments) */ |
| 789 | for (iter = parent; iter && !(iter->nodetype & (LYS_GROUPING | LYS_AUGMENT)); iter = iter->parent); |
| 790 | if (parent && !iter) { |
Michal Vasko | e1e351e | 2016-08-25 12:13:39 +0200 | [diff] [blame] | 791 | for (iter = child; iter && !(iter->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC)); iter = iter->parent); |
| 792 | if (!iter && (parent->flags & LYS_CONFIG_R) && (child->flags & LYS_CONFIG_W)) { |
| 793 | LOGVAL(LYE_INARG, LY_VLOG_LYS, child, "true", "config"); |
| 794 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, child, "State nodes cannot have configuration nodes as children."); |
| 795 | return EXIT_FAILURE; |
| 796 | } |
| 797 | } |
| 798 | |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 799 | /* propagate information about status data presence */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 800 | if ((child->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA)) && |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 801 | (child->flags & LYS_INCL_STATUS)) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 802 | for(iter = parent; iter; iter = lys_parent(iter)) { |
Radek Krejci | 4177150 | 2016-04-14 17:52:32 +0200 | [diff] [blame] | 803 | /* store it only into container or list - the only data inner nodes */ |
| 804 | if (iter->nodetype & (LYS_CONTAINER | LYS_LIST)) { |
| 805 | if (iter->flags & LYS_INCL_STATUS) { |
| 806 | /* done, someone else set it already from here */ |
| 807 | break; |
| 808 | } |
| 809 | /* set flag about including status data */ |
| 810 | iter->flags |= LYS_INCL_STATUS; |
| 811 | } |
| 812 | } |
| 813 | } |
Radek Krejci | 41a349b | 2016-10-24 19:21:59 +0200 | [diff] [blame] | 814 | |
| 815 | /* create implicit input/output nodes to have available them as possible target for augment */ |
| 816 | if (child->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 817 | in = calloc(1, sizeof *in); |
| 818 | in->nodetype = LYS_INPUT; |
| 819 | in->name = lydict_insert(child->module->ctx, "input", 5); |
| 820 | out = calloc(1, sizeof *out); |
| 821 | out->name = lydict_insert(child->module->ctx, "output", 6); |
| 822 | out->nodetype = LYS_OUTPUT; |
| 823 | in->module = out->module = child->module; |
| 824 | in->parent = out->parent = child; |
| 825 | in->flags = out->flags = LYS_IMPLICIT; |
| 826 | in->next = (struct lys_node *)out; |
| 827 | in->prev = (struct lys_node *)out; |
| 828 | out->prev = (struct lys_node *)in; |
| 829 | child->child = (struct lys_node *)in; |
| 830 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 831 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 834 | static const struct lys_module * |
| 835 | lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int internal) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 836 | { |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 837 | char *enlarged_data = NULL; |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 838 | struct lys_module *mod = NULL; |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 839 | unsigned int len; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 840 | |
Radek Krejci | 00a0e71 | 2016-10-26 10:24:46 +0200 | [diff] [blame] | 841 | ly_err_clean(1); |
Radek Krejci | f347abc | 2016-06-22 10:18:47 +0200 | [diff] [blame] | 842 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 843 | if (!ctx || !data) { |
| 844 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 845 | return NULL; |
| 846 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 847 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 848 | if (!internal && format == LYS_IN_YANG) { |
| 849 | /* enlarge data by 2 bytes for flex */ |
| 850 | len = strlen(data); |
| 851 | enlarged_data = malloc((len + 2) * sizeof *enlarged_data); |
| 852 | if (!enlarged_data) { |
| 853 | LOGMEM; |
| 854 | return NULL; |
| 855 | } |
| 856 | memcpy(enlarged_data, data, len); |
| 857 | enlarged_data[len] = enlarged_data[len + 1] = '\0'; |
| 858 | data = enlarged_data; |
| 859 | } |
| 860 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 861 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 862 | case LYS_IN_YIN: |
Radek Krejci | ff4874d | 2016-03-07 12:30:50 +0100 | [diff] [blame] | 863 | mod = yin_read_module(ctx, data, NULL, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 864 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 865 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 866 | mod = yang_read_module(ctx, data, 0, NULL, 1); |
| 867 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 868 | default: |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 869 | LOGERR(LY_EINVAL, "Invalid schema input format."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 870 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 871 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 872 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 873 | free(enlarged_data); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 874 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 875 | } |
| 876 | |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 877 | API const struct lys_module * |
| 878 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
| 879 | { |
| 880 | return lys_parse_mem_(ctx, data, format, 0); |
| 881 | } |
| 882 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 883 | struct lys_submodule * |
| 884 | lys_submodule_parse(struct lys_module *module, const char *data, LYS_INFORMAT format, struct unres_schema *unres) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 885 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 886 | struct lys_submodule *submod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 887 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 888 | assert(module); |
| 889 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 890 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 891 | /* get the main module */ |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 892 | module = lys_main_module(module); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 893 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 894 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 895 | case LYS_IN_YIN: |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 896 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 897 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 898 | case LYS_IN_YANG: |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 899 | submod = yang_read_submodule(module, data, 0, unres); |
| 900 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 901 | default: |
Radek Krejci | 90a550a | 2016-04-13 16:00:58 +0200 | [diff] [blame] | 902 | assert(0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 903 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 904 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 905 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 906 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 907 | } |
| 908 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 909 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 910 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 911 | { |
| 912 | int fd; |
| 913 | const struct lys_module *ret; |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 914 | const char *rev, *dot, *filename; |
| 915 | size_t len; |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 916 | |
| 917 | if (!ctx || !path) { |
| 918 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 919 | return NULL; |
| 920 | } |
| 921 | |
| 922 | fd = open(path, O_RDONLY); |
| 923 | if (fd == -1) { |
| 924 | LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
| 925 | return NULL; |
| 926 | } |
| 927 | |
| 928 | ret = lys_parse_fd(ctx, fd, format); |
| 929 | close(fd); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 930 | |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 931 | if (!ret) { |
| 932 | /* error */ |
| 933 | return NULL; |
| 934 | } |
| 935 | |
| 936 | /* check that name and revision match filename */ |
| 937 | filename = strrchr(path, '/'); |
| 938 | if (!filename) { |
| 939 | filename = path; |
| 940 | } else { |
| 941 | filename++; |
| 942 | } |
| 943 | rev = strchr(filename, '@'); |
| 944 | dot = strrchr(filename, '.'); |
| 945 | |
| 946 | /* name */ |
| 947 | len = strlen(ret->name); |
| 948 | if (strncmp(filename, ret->name, len) || |
| 949 | ((rev && rev != &filename[len]) || (!rev && dot != &filename[len]))) { |
Radek Krejci | c0c8230 | 2016-10-25 14:21:33 +0200 | [diff] [blame] | 950 | LOGWRN("File name \"%s\" does not match module name \"%s\".", filename, ret->name); |
Radek Krejci | d80c860 | 2016-10-25 11:56:03 +0200 | [diff] [blame] | 951 | } |
| 952 | if (rev) { |
| 953 | len = dot - ++rev; |
| 954 | if (!ret->rev_size || len != 10 || strncmp(ret->rev[0].date, rev, len)) { |
| 955 | LOGWRN("File name \"%s\" does not match module revision \"%s\".", filename, |
| 956 | ret->rev_size ? ret->rev[0].date : "none"); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if (!ret->filepath) { |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 961 | /* store URI */ |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 962 | ((struct lys_module *)ret)->filepath = lydict_insert(ctx, path, 0); |
Radek Krejci | 23f5de5 | 2016-02-25 15:53:17 +0100 | [diff] [blame] | 963 | } |
| 964 | |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | API const struct lys_module * |
| 969 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 970 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 971 | const struct lys_module *module; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 972 | struct stat sb; |
| 973 | char *addr; |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 974 | char buf[PATH_MAX]; |
| 975 | int len; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 976 | |
| 977 | if (!ctx || fd < 0) { |
| 978 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 979 | return NULL; |
| 980 | } |
| 981 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 982 | if (fstat(fd, &sb) == -1) { |
| 983 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 984 | return NULL; |
| 985 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 986 | if (!S_ISREG(sb.st_mode)) { |
| 987 | LOGERR(LY_EINVAL, "Invalid parameter, input file is not a regular file"); |
| 988 | return NULL; |
| 989 | } |
| 990 | |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 991 | if (!sb.st_size) { |
| 992 | LOGERR(LY_EINVAL, "File empty."); |
| 993 | return NULL; |
| 994 | } |
| 995 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 996 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 997 | if (addr == MAP_FAILED) { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 998 | LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 999 | return NULL; |
| 1000 | } |
Radek Krejci | a1df168 | 2016-04-11 14:56:59 +0200 | [diff] [blame] | 1001 | module = lys_parse_mem_(ctx, addr, format, 1); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1002 | munmap(addr, sb.st_size + 2); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1003 | |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 1004 | if (module && !module->filepath) { |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1005 | /* get URI if there is /proc */ |
| 1006 | addr = NULL; |
Radek Krejci | 15412ca | 2016-03-03 11:16:52 +0100 | [diff] [blame] | 1007 | if (asprintf(&addr, "/proc/self/fd/%d", fd) != -1) { |
| 1008 | if ((len = readlink(addr, buf, PATH_MAX - 1)) > 0) { |
| 1009 | ((struct lys_module *)module)->filepath = lydict_insert(ctx, buf, len); |
| 1010 | } |
| 1011 | free(addr); |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1012 | } |
Radek Krejci | b051f72 | 2016-02-25 15:12:21 +0100 | [diff] [blame] | 1013 | } |
| 1014 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1015 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1018 | struct lys_submodule * |
| 1019 | lys_submodule_read(struct lys_module *module, int fd, LYS_INFORMAT format, struct unres_schema *unres) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1020 | { |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1021 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1022 | struct stat sb; |
| 1023 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1024 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1025 | assert(module); |
| 1026 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1027 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1028 | if (fstat(fd, &sb) == -1) { |
| 1029 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1030 | return NULL; |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1031 | } |
Michal Vasko | 164d901 | 2016-04-01 10:16:59 +0200 | [diff] [blame] | 1032 | |
| 1033 | if (!sb.st_size) { |
| 1034 | LOGERR(LY_EINVAL, "File empty."); |
| 1035 | return NULL; |
| 1036 | } |
| 1037 | |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1038 | addr = mmap(NULL, sb.st_size + 2, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1039 | if (addr == MAP_FAILED) { |
| 1040 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__); |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1041 | return NULL; |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1042 | } |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1043 | submodule = lys_submodule_parse(module, addr, format, unres); |
Pavol Vican | f7cc285 | 2016-03-22 23:27:35 +0100 | [diff] [blame] | 1044 | munmap(addr, sb.st_size + 2); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1045 | |
Michal Vasko | 5a721fd | 2016-02-16 12:16:48 +0100 | [diff] [blame] | 1046 | return submodule; |
| 1047 | |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1050 | static struct lys_restr * |
| 1051 | lys_restr_dup(struct ly_ctx *ctx, struct lys_restr *old, int size) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1052 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1053 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1054 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1055 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1056 | if (!size) { |
| 1057 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1058 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1059 | |
| 1060 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1061 | if (!result) { |
Radek Krejci | aa2a893 | 2016-02-17 15:03:14 +0100 | [diff] [blame] | 1062 | LOGMEM; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1063 | return NULL; |
| 1064 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1065 | for (i = 0; i < size; i++) { |
| 1066 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1067 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1068 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1069 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1070 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1071 | } |
| 1072 | |
| 1073 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1076 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1077 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1078 | { |
| 1079 | assert(ctx); |
| 1080 | if (!restr) { |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | lydict_remove(ctx, restr->expr); |
| 1085 | lydict_remove(ctx, restr->dsc); |
| 1086 | lydict_remove(ctx, restr->ref); |
| 1087 | lydict_remove(ctx, restr->eapptag); |
| 1088 | lydict_remove(ctx, restr->emsg); |
| 1089 | } |
| 1090 | |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1091 | static void |
| 1092 | lys_iffeature_free(struct lys_iffeature *iffeature, uint8_t iffeature_size) |
| 1093 | { |
| 1094 | uint8_t i; |
| 1095 | |
| 1096 | for (i = 0; i < iffeature_size; ++i) { |
| 1097 | free(iffeature[i].expr); |
| 1098 | free(iffeature[i].features); |
| 1099 | } |
| 1100 | free(iffeature); |
| 1101 | } |
| 1102 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1103 | static int |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1104 | type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1105 | LY_DATA_TYPE base, int tpdftype, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1106 | { |
| 1107 | int i; |
| 1108 | |
| 1109 | switch (base) { |
| 1110 | case LY_TYPE_BINARY: |
| 1111 | if (old->info.binary.length) { |
| 1112 | new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1); |
| 1113 | } |
| 1114 | break; |
| 1115 | |
| 1116 | case LY_TYPE_BITS: |
| 1117 | new->info.bits.count = old->info.bits.count; |
| 1118 | if (new->info.bits.count) { |
| 1119 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
| 1120 | if (!new->info.bits.bit) { |
| 1121 | LOGMEM; |
| 1122 | return -1; |
| 1123 | } |
| 1124 | for (i = 0; i < new->info.bits.count; i++) { |
| 1125 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1126 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1127 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
| 1128 | new->info.bits.bit[i].flags = old->info.bits.bit[i].flags; |
| 1129 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1130 | } |
| 1131 | } |
| 1132 | break; |
| 1133 | |
| 1134 | case LY_TYPE_DEC64: |
| 1135 | new->info.dec64.dig = old->info.dec64.dig; |
Radek Krejci | 8c3b4b6 | 2016-06-17 14:32:12 +0200 | [diff] [blame] | 1136 | new->info.dec64.div = old->info.dec64.div; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1137 | if (old->info.dec64.range) { |
| 1138 | new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1); |
| 1139 | } |
| 1140 | break; |
| 1141 | |
| 1142 | case LY_TYPE_ENUM: |
| 1143 | new->info.enums.count = old->info.enums.count; |
| 1144 | if (new->info.enums.count) { |
| 1145 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
| 1146 | if (!new->info.enums.enm) { |
| 1147 | LOGMEM; |
| 1148 | return -1; |
| 1149 | } |
| 1150 | for (i = 0; i < new->info.enums.count; i++) { |
| 1151 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1152 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1153 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1154 | new->info.enums.enm[i].flags = old->info.enums.enm[i].flags; |
| 1155 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
| 1156 | } |
| 1157 | } |
| 1158 | break; |
| 1159 | |
| 1160 | case LY_TYPE_IDENT: |
Michal Vasko | affc37f | 2016-10-10 18:05:03 +0000 | [diff] [blame] | 1161 | new->info.ident.count = old->info.ident.count; |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1162 | if (old->info.ident.count) { |
| 1163 | new->info.ident.ref = malloc(old->info.ident.count * sizeof *new->info.ident.ref); |
| 1164 | if (!new->info.ident.ref) { |
| 1165 | LOGMEM; |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1166 | return -1; |
| 1167 | } |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1168 | memcpy(new->info.ident.ref, old->info.ident.ref, old->info.ident.count * sizeof *new->info.ident.ref); |
| 1169 | } else { |
| 1170 | /* there can be several unresolved base identities, duplicate them all */ |
| 1171 | i = -1; |
| 1172 | while ((i = unres_schema_find(unres, i, old, UNRES_TYPE_IDENTREF)) != -1) { |
| 1173 | if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i]) == -1) { |
| 1174 | return -1; |
| 1175 | } |
| 1176 | --i; |
| 1177 | } |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1178 | } |
| 1179 | break; |
| 1180 | |
| 1181 | case LY_TYPE_INST: |
| 1182 | new->info.inst.req = old->info.inst.req; |
| 1183 | break; |
| 1184 | |
| 1185 | case LY_TYPE_INT8: |
| 1186 | case LY_TYPE_INT16: |
| 1187 | case LY_TYPE_INT32: |
| 1188 | case LY_TYPE_INT64: |
| 1189 | case LY_TYPE_UINT8: |
| 1190 | case LY_TYPE_UINT16: |
| 1191 | case LY_TYPE_UINT32: |
| 1192 | case LY_TYPE_UINT64: |
| 1193 | if (old->info.num.range) { |
| 1194 | new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1); |
| 1195 | } |
| 1196 | break; |
| 1197 | |
| 1198 | case LY_TYPE_LEAFREF: |
| 1199 | if (old->info.lref.path) { |
| 1200 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
Michal Vasko | 5d63140 | 2016-07-21 13:15:15 +0200 | [diff] [blame] | 1201 | if (!tpdftype && unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent) == -1) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1202 | return -1; |
| 1203 | } |
| 1204 | } |
| 1205 | break; |
| 1206 | |
| 1207 | case LY_TYPE_STRING: |
| 1208 | if (old->info.str.length) { |
| 1209 | new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1); |
| 1210 | } |
| 1211 | new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count); |
| 1212 | new->info.str.pat_count = old->info.str.pat_count; |
| 1213 | break; |
| 1214 | |
| 1215 | case LY_TYPE_UNION: |
| 1216 | new->info.uni.count = old->info.uni.count; |
| 1217 | if (new->info.uni.count) { |
| 1218 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
| 1219 | if (!new->info.uni.types) { |
| 1220 | LOGMEM; |
| 1221 | return -1; |
| 1222 | } |
| 1223 | for (i = 0; i < new->info.uni.count; i++) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1224 | if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), tpdftype, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1225 | return -1; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | break; |
| 1230 | |
| 1231 | default: |
| 1232 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
| 1233 | break; |
| 1234 | } |
| 1235 | return EXIT_SUCCESS; |
| 1236 | } |
| 1237 | |
| 1238 | struct yang_type * |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1239 | lys_yang_type_dup(struct lys_module *module, struct lys_node *parent, struct yang_type *old, struct lys_type *type, |
| 1240 | int tpdftype, struct unres_schema *unres) |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1241 | { |
| 1242 | struct yang_type *new; |
| 1243 | |
| 1244 | new = calloc(1, sizeof *new); |
| 1245 | if (!new) { |
| 1246 | LOGMEM; |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | new->flags = old->flags; |
| 1250 | new->base = old->base; |
Pavol Vican | 6658629 | 2016-04-07 11:38:52 +0200 | [diff] [blame] | 1251 | new->name = lydict_insert(module->ctx, old->name, 0); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1252 | new->type = type; |
| 1253 | if (!new->name) { |
| 1254 | LOGMEM; |
| 1255 | goto error; |
| 1256 | } |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1257 | if (type_dup(module, parent, type, old->type, new->base, tpdftype, unres)) { |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1258 | new->type->base = new->base; |
| 1259 | lys_type_free(module->ctx, new->type); |
| 1260 | memset(&new->type->info, 0, sizeof new->type->info); |
| 1261 | goto error; |
| 1262 | } |
| 1263 | return new; |
| 1264 | |
| 1265 | error: |
| 1266 | free(new); |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | |
| 1270 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1271 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1272 | int tpdftype, struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1273 | { |
| 1274 | int i; |
| 1275 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1276 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1277 | new->base = old->base; |
| 1278 | new->der = old->der; |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1279 | new->parent = (struct lys_tpdf *)parent; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1280 | |
Michal Vasko | 878e38d | 2016-09-05 12:17:53 +0200 | [diff] [blame] | 1281 | i = unres_schema_find(unres, -1, old, tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1282 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1283 | /* HACK (serious one) for unres */ |
| 1284 | /* nothing else we can do but duplicate it immediately */ |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1285 | if (((struct lyxml_elem *)old->der)->flags & LY_YANG_STRUCTURE_FLAG) { |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1286 | new->der = (struct lys_tpdf *)lys_yang_type_dup(mod, parent, (struct yang_type *)old->der, new, tpdftype, unres); |
Pavol Vican | acb9d0d | 2016-04-04 13:57:23 +0200 | [diff] [blame] | 1287 | } else { |
| 1288 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
| 1289 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1290 | /* all these unres additions can fail even though they did not before */ |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1291 | if (!new->der || unres_schema_add_node(mod, unres, new, |
Michal Vasko | 5d63140 | 2016-07-21 13:15:15 +0200 | [diff] [blame] | 1292 | tpdftype ? UNRES_TYPE_DER_TPDF : UNRES_TYPE_DER, parent) == -1) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1293 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1294 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1295 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1298 | return type_dup(mod, parent, new, old, new->base, tpdftype, unres); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1302 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1303 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1304 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1305 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1306 | assert(ctx); |
| 1307 | if (!type) { |
| 1308 | return; |
| 1309 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1310 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1311 | lydict_remove(ctx, type->module_name); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1312 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1313 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1314 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1315 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1316 | free(type->info.binary.length); |
| 1317 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1318 | case LY_TYPE_BITS: |
| 1319 | for (i = 0; i < type->info.bits.count; i++) { |
| 1320 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1321 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1322 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1323 | lys_iffeature_free(type->info.bits.bit[i].iffeature, type->info.bits.bit[i].iffeature_size); |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1324 | } |
| 1325 | free(type->info.bits.bit); |
| 1326 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1327 | |
| 1328 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1329 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1330 | free(type->info.dec64.range); |
| 1331 | break; |
| 1332 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1333 | case LY_TYPE_ENUM: |
| 1334 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1335 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1336 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1337 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1338 | lys_iffeature_free(type->info.enums.enm[i].iffeature, type->info.enums.enm[i].iffeature_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1339 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1340 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1341 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1342 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1343 | case LY_TYPE_INT8: |
| 1344 | case LY_TYPE_INT16: |
| 1345 | case LY_TYPE_INT32: |
| 1346 | case LY_TYPE_INT64: |
| 1347 | case LY_TYPE_UINT8: |
| 1348 | case LY_TYPE_UINT16: |
| 1349 | case LY_TYPE_UINT32: |
| 1350 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1351 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1352 | free(type->info.num.range); |
| 1353 | break; |
| 1354 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1355 | case LY_TYPE_LEAFREF: |
| 1356 | lydict_remove(ctx, type->info.lref.path); |
| 1357 | break; |
| 1358 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1359 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1360 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1361 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1362 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1363 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1364 | } |
| 1365 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1366 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1367 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1368 | case LY_TYPE_UNION: |
| 1369 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1370 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1371 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1372 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1373 | break; |
| 1374 | |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 1375 | case LY_TYPE_IDENT: |
| 1376 | free(type->info.ident.ref); |
| 1377 | break; |
| 1378 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1379 | default: |
Michal Vasko | d328219 | 2016-09-05 11:27:57 +0200 | [diff] [blame] | 1380 | /* nothing to do for LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1381 | break; |
| 1382 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1385 | static void |
| 1386 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1387 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1388 | assert(ctx); |
| 1389 | if (!tpdf) { |
| 1390 | return; |
| 1391 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1392 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1393 | lydict_remove(ctx, tpdf->name); |
| 1394 | lydict_remove(ctx, tpdf->dsc); |
| 1395 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1396 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1397 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1398 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1399 | lydict_remove(ctx, tpdf->units); |
| 1400 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1403 | static struct lys_tpdf * |
| 1404 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1405 | { |
| 1406 | struct lys_tpdf *result; |
| 1407 | int i, j; |
| 1408 | |
| 1409 | if (!size) { |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | |
| 1413 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1414 | if (!result) { |
| 1415 | LOGMEM; |
| 1416 | return NULL; |
| 1417 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1418 | for (i = 0; i < size; i++) { |
| 1419 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1420 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1421 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1422 | result[i].flags = old[i].flags; |
| 1423 | result[i].module = old[i].module; |
| 1424 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 1425 | if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), 1, unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1426 | for (j = 0; j <= i; ++j) { |
| 1427 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1428 | } |
| 1429 | free(result); |
| 1430 | return NULL; |
| 1431 | } |
| 1432 | |
| 1433 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1434 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1435 | } |
| 1436 | |
| 1437 | return result; |
| 1438 | } |
| 1439 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1440 | static struct lys_when * |
| 1441 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1442 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1443 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1444 | |
| 1445 | if (!old) { |
| 1446 | return NULL; |
| 1447 | } |
| 1448 | |
| 1449 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1450 | if (!new) { |
| 1451 | LOGMEM; |
| 1452 | return NULL; |
| 1453 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1454 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1455 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1456 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1457 | |
| 1458 | return new; |
| 1459 | } |
| 1460 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1461 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1462 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1463 | { |
| 1464 | if (!w) { |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | lydict_remove(ctx, w->cond); |
| 1469 | lydict_remove(ctx, w->dsc); |
| 1470 | lydict_remove(ctx, w->ref); |
| 1471 | |
| 1472 | free(w); |
| 1473 | } |
| 1474 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1475 | static void |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1476 | lys_augment_free(struct ly_ctx *ctx, struct lys_node_augment *aug, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1477 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1478 | struct lys_node *next, *sub; |
| 1479 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1480 | /* children from a resolved augment are freed under the target node */ |
Michal Vasko | d2fbade | 2016-05-02 17:14:00 +0200 | [diff] [blame] | 1481 | if (!aug->target) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1482 | LY_TREE_FOR_SAFE(aug->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1483 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1484 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1485 | } |
| 1486 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1487 | lydict_remove(ctx, aug->target_name); |
| 1488 | lydict_remove(ctx, aug->dsc); |
| 1489 | lydict_remove(ctx, aug->ref); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1490 | |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1491 | lys_iffeature_free(aug->iffeature, aug->iffeature_size); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1492 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1493 | lys_when_free(ctx, aug->when); |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1496 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1497 | lys_augment_dup(struct lys_module *module, struct lys_node *parent, struct lys_node_augment *old, int size) |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1498 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1499 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1500 | struct lys_node *old_child, *new_child; |
| 1501 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1502 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1503 | if (!size) { |
| 1504 | return NULL; |
| 1505 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1506 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1507 | new = calloc(size, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1508 | if (!new) { |
| 1509 | LOGMEM; |
| 1510 | return NULL; |
| 1511 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1512 | for (i = 0; i < size; i++) { |
| 1513 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1514 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1515 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1516 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1517 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1518 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1519 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1520 | /* this must succeed, it was already resolved once */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1521 | if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, |
| 1522 | (const struct lys_node **)&new[i].target)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1523 | LOGINT; |
| 1524 | free(new); |
| 1525 | return NULL; |
| 1526 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1527 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1528 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1529 | /* Correct the augment nodes. |
| 1530 | * This function can only be called from lys_node_dup() with uses |
| 1531 | * being the node duplicated, so we must have a case of grouping |
| 1532 | * with a uses with augments. The augmented nodes have already been |
| 1533 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1534 | * (it was set to their actual data parent, not an augment), and |
| 1535 | * the new augment does not have child pointer to its augment nodes, |
| 1536 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1537 | */ |
| 1538 | LY_TREE_FOR(new[i].target->child, new_child) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1539 | if (ly_strequal(new_child->name, old[i].child->name, 1)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1540 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1541 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1542 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1543 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1544 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1545 | LY_TREE_FOR(old[i].child, old_child) { |
| 1546 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1547 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1548 | break; |
| 1549 | } |
| 1550 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 1551 | assert(ly_strequal(old_child->name, new_child->name, 1)); |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1552 | |
| 1553 | new_child->parent = (struct lys_node *)&new[i]; |
| 1554 | new_child = new_child->next; |
| 1555 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1556 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1557 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1558 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1561 | static const char ** |
| 1562 | lys_dflt_dup(struct ly_ctx *ctx, const char **old, int size) |
| 1563 | { |
| 1564 | int i; |
| 1565 | const char **result; |
| 1566 | |
| 1567 | if (!size) { |
| 1568 | return NULL; |
| 1569 | } |
| 1570 | |
| 1571 | result = calloc(size, sizeof *result); |
| 1572 | if (!result) { |
| 1573 | LOGMEM; |
| 1574 | return NULL; |
| 1575 | } |
| 1576 | |
| 1577 | for (i = 0; i < size; i++) { |
| 1578 | result[i] = lydict_insert(ctx, old[i], 0); |
| 1579 | } |
| 1580 | return result; |
| 1581 | } |
| 1582 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1583 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1584 | lys_refine_dup(struct lys_module *mod, struct lys_refine *old, int size) |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1585 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1586 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1587 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1588 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1589 | if (!size) { |
| 1590 | return NULL; |
| 1591 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1592 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1593 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1594 | if (!result) { |
| 1595 | LOGMEM; |
| 1596 | return NULL; |
| 1597 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1598 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1599 | result[i].target_name = lydict_insert(mod->ctx, old[i].target_name, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1600 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1601 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1602 | result[i].flags = old[i].flags; |
| 1603 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1604 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1605 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1606 | result[i].must = lys_restr_dup(mod->ctx, old[i].must, old[i].must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1607 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1608 | result[i].dflt_size = old[i].dflt_size; |
| 1609 | result[i].dflt = lys_dflt_dup(mod->ctx, old[i].dflt, old[i].dflt_size); |
| 1610 | |
| 1611 | if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1612 | result[i].mod.presence = lydict_insert(mod->ctx, old[i].mod.presence, 0); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1613 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1614 | result[i].mod.list = old[i].mod.list; |
| 1615 | } |
| 1616 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1617 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1618 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1619 | } |
| 1620 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1621 | static void |
| 1622 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1623 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1624 | assert(ctx); |
| 1625 | if (!ident) { |
| 1626 | return; |
| 1627 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1628 | |
Radek Krejci | 018f1f5 | 2016-08-03 16:01:20 +0200 | [diff] [blame] | 1629 | free(ident->base); |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1630 | ly_set_free(ident->der); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1631 | lydict_remove(ctx, ident->name); |
| 1632 | lydict_remove(ctx, ident->dsc); |
| 1633 | lydict_remove(ctx, ident->ref); |
Radek Krejci | f1ee2e2 | 2016-08-02 16:36:48 +0200 | [diff] [blame] | 1634 | lys_iffeature_free(ident->iffeature, ident->iffeature_size); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1635 | |
| 1636 | } |
| 1637 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1638 | static void |
| 1639 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1640 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1641 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1642 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1643 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1644 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1645 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1646 | } |
| 1647 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1650 | static void |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1651 | lys_inout_free(struct ly_ctx *ctx, struct lys_node_inout *io) |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1652 | { |
| 1653 | int i; |
| 1654 | |
| 1655 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 1656 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1657 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1658 | } |
| 1659 | free(io->tpdf); |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 1660 | |
| 1661 | for (i = 0; i < io->must_size; i++) { |
| 1662 | lys_restr_free(ctx, &io->must[i]); |
| 1663 | } |
| 1664 | free(io->must); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1665 | } |
| 1666 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1667 | static void |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 1668 | lys_notif_free(struct ly_ctx *ctx, struct lys_node_notif *notif) |
| 1669 | { |
| 1670 | int i; |
| 1671 | |
| 1672 | for (i = 0; i < notif->must_size; i++) { |
| 1673 | lys_restr_free(ctx, ¬if->must[i]); |
| 1674 | } |
| 1675 | free(notif->must); |
| 1676 | |
| 1677 | for (i = 0; i < notif->tpdf_size; i++) { |
| 1678 | lys_tpdf_free(ctx, ¬if->tpdf[i]); |
| 1679 | } |
| 1680 | free(notif->tpdf); |
| 1681 | } |
| 1682 | static void |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1683 | lys_anydata_free(struct ly_ctx *ctx, struct lys_node_anydata *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1684 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1685 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1686 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1687 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1688 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1689 | } |
| 1690 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1691 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1692 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1693 | } |
| 1694 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1695 | static void |
| 1696 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1697 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1698 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1699 | |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 1700 | /* leafref backlinks */ |
| 1701 | ly_set_free((struct ly_set *)leaf->backlinks); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1702 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1703 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1704 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1705 | } |
| 1706 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1707 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1708 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1709 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1710 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1711 | lydict_remove(ctx, leaf->units); |
| 1712 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1715 | static void |
| 1716 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1717 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1718 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1719 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1720 | if (llist->child) { |
| 1721 | /* leafref backlinks */ |
| 1722 | ly_set_free((struct ly_set *)llist->child); |
| 1723 | } |
| 1724 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1725 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1726 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1727 | } |
| 1728 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1729 | |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1730 | for (i = 0; i < llist->dflt_size; i++) { |
| 1731 | lydict_remove(ctx, llist->dflt[i]); |
| 1732 | } |
| 1733 | free(llist->dflt); |
| 1734 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1735 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1736 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1737 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1738 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1741 | static void |
| 1742 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1743 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1744 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1745 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1746 | /* handle only specific parts for LY_NODE_LIST */ |
| 1747 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1748 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1749 | } |
| 1750 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1751 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1752 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1753 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1754 | } |
| 1755 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1756 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1757 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1758 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1759 | for (i = 0; i < list->unique_size; i++) { |
Michal Vasko | f5e940a | 2016-06-07 09:39:26 +0200 | [diff] [blame] | 1760 | for (j = 0; j < list->unique[i].expr_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1761 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 1762 | } |
| 1763 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1764 | } |
| 1765 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1766 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1767 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1768 | } |
| 1769 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1770 | static void |
| 1771 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1772 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1773 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1774 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1775 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1776 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1777 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1778 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1779 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1780 | } |
| 1781 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1782 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1783 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1784 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1785 | } |
| 1786 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1787 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1788 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1791 | static void |
| 1792 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1793 | { |
| 1794 | lydict_remove(ctx, f->name); |
| 1795 | lydict_remove(ctx, f->dsc); |
| 1796 | lydict_remove(ctx, f->ref); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1797 | lys_iffeature_free(f->iffeature, f->iffeature_size); |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 1798 | ly_set_free(f->depfeatures); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1799 | } |
| 1800 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1801 | static void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1802 | lys_deviation_free(struct lys_module *module, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1803 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1804 | int i, j, k; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1805 | struct ly_ctx *ctx; |
| 1806 | struct lys_node *next, *elem; |
| 1807 | |
| 1808 | ctx = module->ctx; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1809 | |
| 1810 | lydict_remove(ctx, dev->target_name); |
| 1811 | lydict_remove(ctx, dev->dsc); |
| 1812 | lydict_remove(ctx, dev->ref); |
| 1813 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 1814 | if (!dev->deviate) { |
| 1815 | return ; |
| 1816 | } |
| 1817 | |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 1818 | /* the module was freed, but we only need the context from orig_node, use ours */ |
| 1819 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 1820 | /* it's actually a node subtree, we need to update modules on all the nodes :-/ */ |
| 1821 | LY_TREE_DFS_BEGIN(dev->orig_node, next, elem) { |
| 1822 | elem->module = module; |
| 1823 | |
| 1824 | LY_TREE_DFS_END(dev->orig_node, next, elem); |
| 1825 | } |
| 1826 | lys_node_free(dev->orig_node, NULL, 0); |
| 1827 | } else { |
| 1828 | /* it's just a shallow copy, freeing one node */ |
| 1829 | dev->orig_node->module = module; |
| 1830 | lys_node_free(dev->orig_node, NULL, 1); |
| 1831 | } |
| 1832 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1833 | for (i = 0; i < dev->deviate_size; i++) { |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 1834 | for (j = 0; j < dev->deviate[i].dflt_size; j++) { |
Pavol Vican | 38321d0 | 2016-08-16 14:56:02 +0200 | [diff] [blame] | 1835 | lydict_remove(ctx, dev->deviate[i].dflt[j]); |
Radek Krejci | d5a5c28 | 2016-08-15 15:38:08 +0200 | [diff] [blame] | 1836 | } |
| 1837 | free(dev->deviate[i].dflt); |
| 1838 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1839 | lydict_remove(ctx, dev->deviate[i].units); |
| 1840 | |
| 1841 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1842 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1843 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1844 | } |
| 1845 | free(dev->deviate[i].must); |
| 1846 | |
| 1847 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1848 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 1849 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 1850 | } |
Michal Vasko | 0238ccf | 2016-03-07 15:02:18 +0100 | [diff] [blame] | 1851 | free(dev->deviate[i].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1852 | } |
| 1853 | free(dev->deviate[i].unique); |
| 1854 | } |
| 1855 | } |
| 1856 | free(dev->deviate); |
| 1857 | } |
| 1858 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1859 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1860 | lys_uses_free(struct ly_ctx *ctx, struct lys_node_uses *uses, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1861 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1862 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1863 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1864 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1865 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1866 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1867 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1868 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 1869 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1870 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1871 | } |
| 1872 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1873 | |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1874 | for (j = 0; j < uses->refine[i].dflt_size; j++) { |
Pavol Vican | 855ca62 | 2016-09-05 13:07:54 +0200 | [diff] [blame] | 1875 | lydict_remove(ctx, uses->refine[i].dflt[j]); |
Pavol Vican | 3e7c73a | 2016-08-17 10:24:11 +0200 | [diff] [blame] | 1876 | } |
| 1877 | free(uses->refine[i].dflt); |
| 1878 | |
| 1879 | if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1880 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1881 | } |
| 1882 | } |
| 1883 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1884 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1885 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 1886 | lys_augment_free(ctx, &uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1887 | } |
| 1888 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1889 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1890 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1893 | void |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1894 | lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv), int shallow) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1895 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1896 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1897 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1898 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1899 | if (!node) { |
| 1900 | return; |
| 1901 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1902 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1903 | assert(node->module); |
| 1904 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1905 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1906 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1907 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1908 | /* remove private object */ |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 1909 | if (node->priv && private_destructor) { |
| 1910 | private_destructor(node, node->priv); |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1913 | /* common part */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1914 | lydict_remove(ctx, node->name); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1915 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 1916 | lys_iffeature_free(node->iffeature, node->iffeature_size); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1917 | lydict_remove(ctx, node->dsc); |
| 1918 | lydict_remove(ctx, node->ref); |
| 1919 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1920 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1921 | if (!shallow && !(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1922 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 1923 | lys_node_free(sub, private_destructor, 0); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1927 | /* specific part */ |
| 1928 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1929 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1930 | lys_container_free(ctx, (struct lys_node_container *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1931 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1932 | case LYS_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1933 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1934 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1935 | case LYS_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1936 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1937 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1938 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1939 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1940 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1941 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1942 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1943 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1944 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1945 | case LYS_ANYDATA: |
| 1946 | lys_anydata_free(ctx, (struct lys_node_anydata *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1947 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1948 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1949 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1950 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1951 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1952 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1953 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1954 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1955 | /* do nothing */ |
| 1956 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1957 | case LYS_GROUPING: |
| 1958 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1959 | case LYS_ACTION: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1960 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1961 | break; |
Pavol Vican | 7cff97e | 2016-08-09 14:56:08 +0200 | [diff] [blame] | 1962 | case LYS_NOTIF: |
| 1963 | lys_notif_free(ctx, (struct lys_node_notif *)node); |
| 1964 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1965 | case LYS_INPUT: |
| 1966 | case LYS_OUTPUT: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 1967 | lys_inout_free(ctx, (struct lys_node_inout *)node); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1968 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1969 | case LYS_UNKNOWN: |
| 1970 | LOGINT; |
| 1971 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1972 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1973 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1974 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1975 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1976 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1979 | const struct lys_module * |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 1980 | lys_get_implemented_module(const struct lys_module *mod) |
| 1981 | { |
| 1982 | struct ly_ctx *ctx; |
| 1983 | int i; |
| 1984 | |
| 1985 | if (!mod || mod->implemented) { |
| 1986 | /* invalid argument or the module itself is implemented */ |
| 1987 | return mod; |
| 1988 | } |
| 1989 | |
| 1990 | ctx = mod->ctx; |
| 1991 | for (i = 0; i < ctx->models.used; i++) { |
| 1992 | if (!ctx->models.list[i]->implemented) { |
| 1993 | continue; |
| 1994 | } |
| 1995 | |
| 1996 | if (ly_strequal(mod->name, ctx->models.list[i]->name, 1)) { |
| 1997 | /* we have some revision of the module implemented */ |
| 1998 | return ctx->models.list[i]; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | /* we have no revision of the module implemented, return the module itself, |
| 2003 | * it is up to the caller to set the module implemented when needed */ |
| 2004 | return mod; |
| 2005 | } |
| 2006 | |
| 2007 | const struct lys_module * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2008 | lys_get_import_module(const struct lys_module *module, const char *prefix, int pref_len, const char *name, int name_len) |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2009 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2010 | const struct lys_module *main_module; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2011 | char *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2012 | int i; |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2013 | |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2014 | assert(!prefix || !name); |
| 2015 | |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2016 | if (prefix && !pref_len) { |
| 2017 | pref_len = strlen(prefix); |
| 2018 | } |
| 2019 | if (name && !name_len) { |
| 2020 | name_len = strlen(name); |
| 2021 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2022 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 2023 | main_module = lys_main_module(module); |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame] | 2024 | |
| 2025 | /* module own prefix, submodule own prefix, (sub)module own name */ |
| 2026 | if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len]) |
| 2027 | || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2028 | && (!name || (!strncmp(main_module->name, name, name_len) && !main_module->name[name_len]))) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2029 | return main_module; |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 2030 | } |
| 2031 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2032 | /* standard import */ |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2033 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 2034 | if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) |
| 2035 | && (!name || (!strncmp(module->imp[i].module->name, name, name_len) && !module->imp[i].module->name[name_len]))) { |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2036 | return module->imp[i].module; |
| 2037 | } |
| 2038 | } |
| 2039 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 2040 | /* module required by a foreign grouping, deviation, or submodule */ |
| 2041 | if (name) { |
| 2042 | str = strndup(name, name_len); |
| 2043 | if (!str) { |
| 2044 | LOGMEM; |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | main_module = ly_ctx_get_module(module->ctx, str, NULL); |
| 2048 | free(str); |
| 2049 | return main_module; |
| 2050 | } |
| 2051 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 2052 | return NULL; |
| 2053 | } |
| 2054 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2055 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2056 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2057 | module_free_common(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2058 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2059 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2060 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2061 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2062 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2063 | assert(module->ctx); |
| 2064 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2065 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2066 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2067 | for (i = 0; i < module->imp_size; i++) { |
Michal Vasko | a99c163 | 2016-06-06 16:23:52 +0200 | [diff] [blame] | 2068 | lydict_remove(ctx, module->imp[i].prefix); |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2069 | lydict_remove(ctx, module->imp[i].dsc); |
| 2070 | lydict_remove(ctx, module->imp[i].ref); |
Radek Krejci | 225376f | 2016-02-16 17:36:22 +0100 | [diff] [blame] | 2071 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2072 | free(module->imp); |
| 2073 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2074 | /* submodules don't have data tree, the data nodes |
| 2075 | * are placed in the main module altogether */ |
| 2076 | if (!module->type) { |
| 2077 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2078 | lys_node_free(iter, private_destructor, 0); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2079 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2080 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2081 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2082 | lydict_remove(ctx, module->dsc); |
| 2083 | lydict_remove(ctx, module->ref); |
| 2084 | lydict_remove(ctx, module->org); |
| 2085 | lydict_remove(ctx, module->contact); |
Radek Krejci | a77904e | 2016-02-25 16:23:45 +0100 | [diff] [blame] | 2086 | lydict_remove(ctx, module->filepath); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2087 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2088 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2089 | for (i = 0; i < module->rev_size; i++) { |
| 2090 | lydict_remove(ctx, module->rev[i].dsc); |
| 2091 | lydict_remove(ctx, module->rev[i].ref); |
| 2092 | } |
| 2093 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2094 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2095 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2096 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2097 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2098 | } |
| 2099 | module->ident_size = 0; |
| 2100 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2101 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2102 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2103 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2104 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2105 | } |
| 2106 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2107 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2108 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2109 | for (i = 0; i < module->inc_size; i++) { |
Michal Vasko | 8bfe381 | 2016-07-27 13:37:52 +0200 | [diff] [blame] | 2110 | lydict_remove(ctx, module->inc[i].dsc); |
| 2111 | lydict_remove(ctx, module->inc[i].ref); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2112 | /* complete submodule free is done only from main module since |
| 2113 | * submodules propagate their includes to the main module */ |
| 2114 | if (!module->type) { |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2115 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2116 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2117 | } |
| 2118 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2119 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2120 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2121 | for (i = 0; i < module->augment_size; i++) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 2122 | lys_augment_free(ctx, &module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2123 | } |
| 2124 | free(module->augment); |
| 2125 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2126 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2127 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2128 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2129 | } |
| 2130 | free(module->features); |
| 2131 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2132 | /* deviations */ |
| 2133 | for (i = 0; i < module->deviation_size; i++) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2134 | lys_deviation_free(module, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2135 | } |
| 2136 | free(module->deviation); |
| 2137 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2138 | lydict_remove(ctx, module->name); |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 2139 | lydict_remove(ctx, module->prefix); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2140 | } |
| 2141 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2142 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2143 | lys_submodule_free(struct lys_submodule *submodule, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2144 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2145 | if (!submodule) { |
| 2146 | return; |
| 2147 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2148 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2149 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2150 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2151 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2152 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2153 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2154 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2155 | } |
| 2156 | |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2157 | static int |
| 2158 | ingrouping(const struct lys_node *node) |
| 2159 | { |
| 2160 | const struct lys_node *iter = node; |
| 2161 | assert(node); |
| 2162 | |
| 2163 | for(iter = node; iter && iter->nodetype != LYS_GROUPING; iter = lys_parent(iter)); |
| 2164 | if (!iter) { |
| 2165 | return 0; |
| 2166 | } else { |
| 2167 | return 1; |
| 2168 | } |
| 2169 | } |
| 2170 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2171 | /* |
| 2172 | * final: 0 - do not change config flags; 1 - inherit config flags from the parent; 2 - remove config flags |
| 2173 | */ |
| 2174 | static struct lys_node * |
| 2175 | lys_node_dup_recursion(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm, |
| 2176 | struct unres_schema *unres, int shallow, int finalize) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2177 | { |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame^] | 2178 | struct lys_node *retval = NULL, *iter, *p; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2179 | struct ly_ctx *ctx = module->ctx; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2180 | int i, j, rc; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2181 | unsigned int size, size1, size2; |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2182 | struct unres_list_uniq *unique_info; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2183 | uint16_t flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2184 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2185 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2186 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2187 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2188 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2189 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2190 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2191 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2192 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2193 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2194 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2195 | struct lys_node_anydata *any = NULL; |
| 2196 | struct lys_node_anydata *any_orig = (struct lys_node_anydata *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2197 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2198 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2199 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2200 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2201 | struct lys_node_rpc_action *rpc = NULL; |
| 2202 | struct lys_node_rpc_action *rpc_orig = (struct lys_node_rpc_action *)node; |
| 2203 | struct lys_node_inout *io = NULL; |
| 2204 | struct lys_node_inout *io_orig = (struct lys_node_inout *)node; |
| 2205 | struct lys_node_rpc_action *ntf = NULL; |
| 2206 | struct lys_node_rpc_action *ntf_orig = (struct lys_node_rpc_action *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2207 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2208 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2209 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2210 | /* we cannot just duplicate memory since the strings are stored in |
| 2211 | * dictionary and we need to update dictionary counters. |
| 2212 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2213 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2214 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2215 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2216 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2217 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2218 | break; |
| 2219 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2220 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2221 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2222 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2223 | break; |
| 2224 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2225 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2226 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2227 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2228 | break; |
| 2229 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2230 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2231 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2232 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2233 | break; |
| 2234 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2235 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2236 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2237 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2240 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2241 | case LYS_ANYDATA: |
| 2242 | any = calloc(1, sizeof *any); |
| 2243 | retval = (struct lys_node *)any; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2244 | break; |
| 2245 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2246 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2247 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2248 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2249 | break; |
| 2250 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2251 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2252 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2253 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2254 | break; |
| 2255 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2256 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2257 | grp = calloc(1, sizeof *grp); |
| 2258 | retval = (struct lys_node *)grp; |
| 2259 | break; |
| 2260 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2261 | case LYS_RPC: |
Michal Vasko | 44fb638 | 2016-06-29 11:12:27 +0200 | [diff] [blame] | 2262 | case LYS_ACTION: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2263 | rpc = calloc(1, sizeof *rpc); |
| 2264 | retval = (struct lys_node *)rpc; |
| 2265 | break; |
| 2266 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2267 | case LYS_INPUT: |
| 2268 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2269 | io = calloc(1, sizeof *io); |
| 2270 | retval = (struct lys_node *)io; |
| 2271 | break; |
| 2272 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2273 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2274 | ntf = calloc(1, sizeof *ntf); |
| 2275 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2276 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2277 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2278 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2279 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2280 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2281 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2282 | |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2283 | if (!retval) { |
| 2284 | LOGMEM; |
| 2285 | return NULL; |
| 2286 | } |
| 2287 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2288 | /* |
| 2289 | * duplicate generic part of the structure |
| 2290 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2291 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2292 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2293 | retval->ref = lydict_insert(ctx, node->ref, 0); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2294 | retval->nacm = nacm; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2295 | retval->flags = node->flags; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2296 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2297 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2298 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2299 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2300 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2301 | |
Radek Krejci | 0621404 | 2016-11-04 16:25:58 +0100 | [diff] [blame] | 2302 | if (node->iffeature_size) { |
| 2303 | retval->iffeature_size = node->iffeature_size; |
| 2304 | retval->iffeature = calloc(retval->iffeature_size, sizeof *retval->iffeature); |
| 2305 | if (!retval->iffeature) { |
| 2306 | LOGMEM; |
| 2307 | goto error; |
| 2308 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2309 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2310 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2311 | if (!shallow) { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2312 | for (i = 0; i < node->iffeature_size; ++i) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2313 | resolve_iffeature_getsizes(&node->iffeature[i], &size1, &size2); |
| 2314 | if (size1) { |
| 2315 | /* there is something to duplicate */ |
| 2316 | |
| 2317 | /* duplicate compiled expression */ |
| 2318 | size = (size1 / 4) + (size1 % 4) ? 1 : 0; |
| 2319 | retval->iffeature[i].expr = malloc(size * sizeof *retval->iffeature[i].expr); |
| 2320 | memcpy(retval->iffeature[i].expr, node->iffeature[i].expr, size * sizeof *retval->iffeature[i].expr); |
| 2321 | |
| 2322 | /* list of feature pointer must be updated to point to the resulting tree */ |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2323 | retval->iffeature[i].features = calloc(size2, sizeof *retval->iffeature[i].features); |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2324 | for (j = 0; (unsigned int)j < size2; j++) { |
| 2325 | rc = unres_schema_dup(module, unres, &node->iffeature[i].features[j], UNRES_IFFEAT, |
| 2326 | &retval->iffeature[i].features[j]); |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2327 | if (rc == EXIT_FAILURE) { |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2328 | /* feature is resolved in origin, so copy it |
| 2329 | * - duplication is used for instantiating groupings |
| 2330 | * and if-feature inside grouping is supposed to be |
| 2331 | * resolved inside the original grouping, so we want |
| 2332 | * to keep pointers to features from the grouping |
| 2333 | * context */ |
| 2334 | retval->iffeature[i].features[j] = node->iffeature[i].features[j]; |
| 2335 | } else if (rc == -1) { |
| 2336 | goto error; |
Radek Krejci | cbb473e | 2016-09-16 14:48:32 +0200 | [diff] [blame] | 2337 | } /* else unres was duplicated */ |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2338 | } |
| 2339 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2340 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2341 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2342 | /* inherit config flags */ |
Radek Krejci | 7b9f566 | 2016-11-07 16:28:37 +0100 | [diff] [blame^] | 2343 | p = parent; |
| 2344 | do { |
| 2345 | for (iter = p; iter && (iter->nodetype == LYS_USES); iter = iter->parent); |
| 2346 | } while (iter && iter->nodetype == LYS_AUGMENT && (p = lys_parent(iter))); |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2347 | if (iter) { |
| 2348 | flags = iter->flags & LYS_CONFIG_MASK; |
| 2349 | } else { |
| 2350 | /* default */ |
| 2351 | flags = LYS_CONFIG_W; |
| 2352 | } |
| 2353 | |
| 2354 | switch (finalize) { |
| 2355 | case 1: |
| 2356 | /* inherit config flags */ |
| 2357 | if (retval->flags & LYS_CONFIG_SET) { |
| 2358 | /* skip nodes with an explicit config value */ |
| 2359 | if ((flags & LYS_CONFIG_R) && (retval->flags & LYS_CONFIG_W)) { |
| 2360 | LOGVAL(LYE_INARG, LY_VLOG_LYS, retval, "true", "config"); |
| 2361 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, retval, "State nodes cannot have configuration nodes as children."); |
| 2362 | goto error; |
| 2363 | } |
| 2364 | break; |
| 2365 | } |
| 2366 | |
| 2367 | if (retval->nodetype != LYS_USES) { |
| 2368 | retval->flags = (retval->flags & ~LYS_CONFIG_MASK) | flags; |
| 2369 | } |
| 2370 | break; |
| 2371 | case 2: |
| 2372 | /* erase config flags */ |
| 2373 | retval->flags &= ~LYS_CONFIG_MASK; |
| 2374 | retval->flags &= ~LYS_CONFIG_SET; |
| 2375 | break; |
| 2376 | } |
| 2377 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2378 | /* connect it to the parent */ |
| 2379 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2380 | goto error; |
| 2381 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2382 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2383 | /* go recursively */ |
| 2384 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2385 | LY_TREE_FOR(node->child, iter) { |
| 2386 | if (!lys_node_dup_recursion(module, retval, iter, retval->nacm, unres, 0, finalize)) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2387 | goto error; |
| 2388 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2389 | } |
| 2390 | } |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2391 | |
| 2392 | if (finalize == 1) { |
| 2393 | /* check that configuration lists have keys |
| 2394 | * - we really want to check keys_size in original node, because the keys are |
| 2395 | * not yet resolved here, it is done below in nodetype specific part */ |
| 2396 | if ((retval->nodetype == LYS_LIST) && (retval->flags & LYS_CONFIG_W) |
| 2397 | && !((struct lys_node_list *)node)->keys_size) { |
| 2398 | LOGVAL(LYE_MISSCHILDSTMT, LY_VLOG_LYS, retval, "key", "list"); |
| 2399 | goto error; |
| 2400 | } |
| 2401 | } |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2402 | } else { |
Michal Vasko | c5c26b0 | 2016-06-29 11:10:29 +0200 | [diff] [blame] | 2403 | memcpy(retval->iffeature, node->iffeature, retval->iffeature_size * sizeof *retval->iffeature); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2406 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2407 | * duplicate specific part of the structure |
| 2408 | */ |
| 2409 | switch (node->nodetype) { |
| 2410 | case LYS_CONTAINER: |
| 2411 | if (cont_orig->when) { |
| 2412 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2413 | } |
| 2414 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2415 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2416 | cont->must_size = cont_orig->must_size; |
| 2417 | cont->tpdf_size = cont_orig->tpdf_size; |
| 2418 | |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2419 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2420 | cont->tpdf = lys_tpdf_dup(module, lys_parent(node), cont_orig->tpdf, cont->tpdf_size, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2421 | break; |
| 2422 | |
| 2423 | case LYS_CHOICE: |
| 2424 | if (choice_orig->when) { |
| 2425 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2428 | if (!shallow) { |
| 2429 | if (choice_orig->dflt) { |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2430 | rc = lys_get_sibling(choice->child, lys_node_module(retval)->name, 0, choice_orig->dflt->name, 0, |
| 2431 | LYS_ANYDATA | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST, |
| 2432 | (const struct lys_node **)&choice->dflt); |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2433 | if (rc) { |
| 2434 | if (rc == EXIT_FAILURE) { |
| 2435 | LOGINT; |
| 2436 | } |
| 2437 | goto error; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2438 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2439 | } else { |
| 2440 | /* useless to check return value, we don't know whether |
| 2441 | * there really wasn't any default defined or it just hasn't |
| 2442 | * been resolved, we just hope for the best :) |
| 2443 | */ |
| 2444 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2445 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2446 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2447 | choice->dflt = choice_orig->dflt; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2448 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2449 | break; |
| 2450 | |
| 2451 | case LYS_LEAF: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2452 | if (lys_type_dup(module, retval, &(leaf->type), &(leaf_orig->type), ingrouping(retval), unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2453 | goto error; |
| 2454 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2455 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2456 | |
| 2457 | if (leaf_orig->dflt) { |
| 2458 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2459 | if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, |
| 2460 | (struct lys_node *)(&leaf->dflt)) == -1) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2461 | goto error; |
| 2462 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | leaf->must_size = leaf_orig->must_size; |
| 2466 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2467 | |
| 2468 | if (leaf_orig->when) { |
| 2469 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2470 | } |
| 2471 | break; |
| 2472 | |
| 2473 | case LYS_LEAFLIST: |
Radek Krejci | 3a5501d | 2016-07-18 22:03:34 +0200 | [diff] [blame] | 2474 | if (lys_type_dup(module, retval, &(llist->type), &(llist_orig->type), ingrouping(retval), unres)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2475 | goto error; |
| 2476 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2477 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
| 2478 | |
| 2479 | llist->min = llist_orig->min; |
| 2480 | llist->max = llist_orig->max; |
| 2481 | |
| 2482 | llist->must_size = llist_orig->must_size; |
| 2483 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2484 | |
Radek Krejci | 5167320 | 2016-11-01 17:00:32 +0100 | [diff] [blame] | 2485 | llist->dflt_size = llist_orig->dflt_size; |
| 2486 | llist->dflt = malloc(llist->dflt_size * sizeof *llist->dflt); |
| 2487 | for (i = 0; i < llist->dflt_size; i++) { |
| 2488 | llist->dflt[i] = lydict_insert(ctx, llist_orig->dflt[i], 0); |
| 2489 | if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT, |
| 2490 | (struct lys_node *)(&llist->dflt[i])) == -1) { |
| 2491 | goto error; |
| 2492 | } |
| 2493 | } |
| 2494 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2495 | if (llist_orig->when) { |
| 2496 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2497 | } |
| 2498 | break; |
| 2499 | |
| 2500 | case LYS_LIST: |
| 2501 | list->min = list_orig->min; |
| 2502 | list->max = list_orig->max; |
| 2503 | |
| 2504 | list->must_size = list_orig->must_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2505 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2506 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2507 | list->tpdf_size = list_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2508 | list->tpdf = lys_tpdf_dup(module, lys_parent(node), list_orig->tpdf, list->tpdf_size, unres); |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2509 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2510 | list->keys_size = list_orig->keys_size; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2511 | if (list->keys_size) { |
| 2512 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 2513 | list->keys_str = lydict_insert(ctx, list_orig->keys_str, 0); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2514 | if (!list->keys) { |
| 2515 | LOGMEM; |
| 2516 | goto error; |
| 2517 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2518 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2519 | if (!shallow) { |
Radek Krejci | 5c08a99 | 2016-11-02 13:30:04 +0100 | [diff] [blame] | 2520 | /* the keys are going to be resolved only if the list is instantiated in data tree, not just |
| 2521 | * in another grouping */ |
| 2522 | for (iter = parent; iter && iter->nodetype != LYS_GROUPING; iter = iter->parent); |
| 2523 | if (!iter && unres_schema_add_node(module, unres, list, UNRES_LIST_KEYS, NULL) == -1) { |
| 2524 | goto error; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2525 | } |
Michal Vasko | 0ea4103 | 2015-06-16 08:53:55 +0200 | [diff] [blame] | 2526 | } else { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2527 | memcpy(list->keys, list_orig->keys, list->keys_size * sizeof *list->keys); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2528 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2529 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2530 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2531 | list->unique_size = list_orig->unique_size; |
| 2532 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2533 | if (!list->unique) { |
| 2534 | LOGMEM; |
| 2535 | goto error; |
| 2536 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2537 | for (i = 0; i < list->unique_size; ++i) { |
| 2538 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2539 | list->unique[i].expr = malloc(list->unique[i].expr_size * sizeof *list->unique[i].expr); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2540 | if (!list->unique[i].expr) { |
| 2541 | LOGMEM; |
| 2542 | goto error; |
| 2543 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2544 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2545 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 2546 | |
| 2547 | /* if it stays in unres list, duplicate it also there */ |
Radek Krejci | d09d1a5 | 2016-08-11 14:05:45 +0200 | [diff] [blame] | 2548 | unique_info = malloc(sizeof *unique_info); |
| 2549 | unique_info->list = (struct lys_node *)list; |
| 2550 | unique_info->expr = list->unique[i].expr[j]; |
| 2551 | unique_info->trg_type = &list->unique[i].trg_type; |
| 2552 | unres_schema_dup(module, unres, &list_orig, UNRES_LIST_UNIQ, unique_info); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2553 | } |
| 2554 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2555 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2556 | if (list_orig->when) { |
| 2557 | list->when = lys_when_dup(ctx, list_orig->when); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2558 | } |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2559 | break; |
| 2560 | |
| 2561 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2562 | case LYS_ANYDATA: |
| 2563 | any->must_size = any_orig->must_size; |
| 2564 | any->must = lys_restr_dup(ctx, any_orig->must, any->must_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2565 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 2566 | if (any_orig->when) { |
| 2567 | any->when = lys_when_dup(ctx, any_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2568 | } |
| 2569 | break; |
| 2570 | |
| 2571 | case LYS_USES: |
| 2572 | uses->grp = uses_orig->grp; |
| 2573 | |
| 2574 | if (uses_orig->when) { |
| 2575 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 2579 | uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2580 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2581 | if (!shallow) { |
| 2582 | uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size); |
Michal Vasko | cda5171 | 2016-05-19 15:22:11 +0200 | [diff] [blame] | 2583 | if (!uses->grp || uses->grp->nacm) { |
| 2584 | assert(!uses->child); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2585 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL) == -1) { |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2586 | goto error; |
| 2587 | } |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2588 | } |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2589 | } else { |
| 2590 | memcpy(uses->augment, uses_orig->augment, uses->augment_size * sizeof *uses->augment); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2591 | } |
| 2592 | break; |
| 2593 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2594 | case LYS_CASE: |
| 2595 | if (cs_orig->when) { |
| 2596 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2597 | } |
| 2598 | break; |
| 2599 | |
| 2600 | case LYS_GROUPING: |
| 2601 | grp->tpdf_size = grp_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2602 | grp->tpdf = lys_tpdf_dup(module, lys_parent(node), grp_orig->tpdf, grp->tpdf_size, unres); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2603 | break; |
| 2604 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 2605 | case LYS_ACTION: |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2606 | case LYS_RPC: |
| 2607 | rpc->tpdf_size = rpc_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2608 | rpc->tpdf = lys_tpdf_dup(module, lys_parent(node), rpc_orig->tpdf, rpc->tpdf_size, unres); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2609 | break; |
| 2610 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2611 | case LYS_INPUT: |
| 2612 | case LYS_OUTPUT: |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2613 | io->tpdf_size = io_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2614 | io->tpdf = lys_tpdf_dup(module, lys_parent(node), io_orig->tpdf, io->tpdf_size, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2615 | break; |
| 2616 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2617 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2618 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2619 | ntf->tpdf = lys_tpdf_dup(module, lys_parent(node), ntf_orig->tpdf, ntf->tpdf_size, unres); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2620 | break; |
| 2621 | |
Radek Krejci | 9693540 | 2016-11-04 16:27:28 +0100 | [diff] [blame] | 2622 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2623 | default: |
| 2624 | /* LY_NODE_AUGMENT */ |
| 2625 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2626 | goto error; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2627 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2628 | |
| 2629 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2630 | |
| 2631 | error: |
| 2632 | |
Michal Vasko | d51d6ad | 2016-02-16 13:24:31 +0100 | [diff] [blame] | 2633 | lys_node_free(retval, NULL, 0); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2634 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2635 | } |
| 2636 | |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2637 | struct lys_node * |
| 2638 | lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t nacm, |
| 2639 | struct unres_schema *unres, int shallow) |
| 2640 | { |
| 2641 | struct lys_node *p = NULL; |
Radek Krejci | 3c48d04 | 2016-11-07 14:42:36 +0100 | [diff] [blame] | 2642 | int finalize = 0, has_xpath; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2643 | struct lys_node *result, *iter, *next; |
| 2644 | |
| 2645 | if (!shallow) { |
| 2646 | /* get know where in schema tre we are to know what should be done during instantiation of the grouping */ |
| 2647 | for (p = parent; |
| 2648 | p && !(p->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT | LYS_RPC | LYS_ACTION | LYS_GROUPING)); |
| 2649 | p = lys_parent(p)); |
| 2650 | finalize = p ? ((p->nodetype == LYS_GROUPING) ? 0 : 2) : 1; |
| 2651 | } |
| 2652 | |
| 2653 | result = lys_node_dup_recursion(module, parent, node, nacm, unres, shallow, finalize); |
| 2654 | if (finalize) { |
| 2655 | /* check xpath expressions in the instantiated tree */ |
| 2656 | for (iter = next = parent->child; iter; iter = next) { |
Radek Krejci | 3c48d04 | 2016-11-07 14:42:36 +0100 | [diff] [blame] | 2657 | |
| 2658 | switch (iter->nodetype) { |
| 2659 | case LYS_AUGMENT: |
| 2660 | if (((struct lys_node_augment *)iter)->when) { |
| 2661 | has_xpath = 1; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2662 | } |
Radek Krejci | 3c48d04 | 2016-11-07 14:42:36 +0100 | [diff] [blame] | 2663 | break; |
| 2664 | case LYS_CASE: |
| 2665 | if (((struct lys_node_case *)iter)->when) { |
| 2666 | has_xpath = 1; |
| 2667 | } |
| 2668 | break; |
| 2669 | case LYS_CHOICE: |
| 2670 | if (((struct lys_node_choice *)iter)->when) { |
| 2671 | has_xpath = 1; |
| 2672 | } |
| 2673 | break; |
| 2674 | case LYS_ANYDATA: |
| 2675 | if (((struct lys_node_anydata *)iter)->when || ((struct lys_node_anydata *)iter)->must_size) { |
| 2676 | has_xpath = 1; |
| 2677 | } |
| 2678 | break; |
| 2679 | case LYS_LEAF: |
| 2680 | if (((struct lys_node_leaf *)iter)->when || ((struct lys_node_leaf *)iter)->must_size) { |
| 2681 | has_xpath = 1; |
| 2682 | } |
| 2683 | break; |
| 2684 | case LYS_LEAFLIST: |
| 2685 | if (((struct lys_node_leaflist *)iter)->when || ((struct lys_node_leaflist *)iter)->must_size) { |
| 2686 | has_xpath = 1; |
| 2687 | } |
| 2688 | break; |
| 2689 | case LYS_LIST: |
| 2690 | if (((struct lys_node_list *)iter)->when || ((struct lys_node_list *)iter)->must_size) { |
| 2691 | has_xpath = 1; |
| 2692 | } |
| 2693 | break; |
| 2694 | case LYS_CONTAINER: |
| 2695 | if (((struct lys_node_container *)iter)->when || ((struct lys_node_container *)iter)->must_size) { |
| 2696 | has_xpath = 1; |
| 2697 | } |
| 2698 | break; |
| 2699 | case LYS_INPUT: |
| 2700 | case LYS_OUTPUT: |
| 2701 | if (((struct lys_node_inout *)iter)->must_size) { |
| 2702 | has_xpath = 1; |
| 2703 | } |
| 2704 | break; |
| 2705 | case LYS_NOTIF: |
| 2706 | if (((struct lys_node_notif *)iter)->must_size) { |
| 2707 | has_xpath = 1; |
| 2708 | } |
| 2709 | break; |
| 2710 | case LYS_USES: |
| 2711 | if (((struct lys_node_uses *)iter)->when) { |
| 2712 | has_xpath = 1; |
| 2713 | } |
| 2714 | break; |
| 2715 | default: |
| 2716 | has_xpath = 0; |
| 2717 | } |
| 2718 | if (has_xpath && unres_schema_add_node(module, unres, iter, UNRES_XPATH, NULL) == -1) { |
| 2719 | /* invalid xpath */ |
| 2720 | return NULL; |
Radek Krejci | d2ac35f | 2016-10-21 23:08:28 +0200 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /* select next item */ |
| 2724 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA | LYS_GROUPING)) { |
| 2725 | /* child exception for leafs, leaflists and anyxml without children, ignore groupings */ |
| 2726 | next = NULL; |
| 2727 | } else { |
| 2728 | next = iter->child; |
| 2729 | } |
| 2730 | if (!next) { |
| 2731 | /* no children, try siblings */ |
| 2732 | next = iter->next; |
| 2733 | } |
| 2734 | while (!next) { |
| 2735 | /* parent is already processed, go to its sibling */ |
| 2736 | iter = lys_parent(iter); |
| 2737 | if (iter == parent) { |
| 2738 | /* we are done, no next element to process */ |
| 2739 | break; |
| 2740 | } |
| 2741 | next = iter->next; |
| 2742 | } |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | return result; |
| 2747 | } |
| 2748 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2749 | void |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2750 | lys_node_switch(struct lys_node *dst, struct lys_node *src) |
| 2751 | { |
| 2752 | struct lys_node *child; |
| 2753 | |
Michal Vasko | b42b697 | 2016-06-06 14:21:30 +0200 | [diff] [blame] | 2754 | assert((dst->module == src->module) && ly_strequal(dst->name, src->name, 1) && (dst->nodetype == src->nodetype)); |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2755 | |
| 2756 | /* sibling next */ |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 2757 | if (dst->prev->next) { |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2758 | dst->prev->next = src; |
| 2759 | } |
| 2760 | |
| 2761 | /* sibling prev */ |
| 2762 | if (dst->next) { |
| 2763 | dst->next->prev = src; |
Michal Vasko | 8328da8 | 2016-08-25 09:27:22 +0200 | [diff] [blame] | 2764 | } else { |
| 2765 | for (child = dst->prev; child->prev->next; child = child->prev); |
| 2766 | child->prev = src; |
Michal Vasko | ff006c1 | 2016-02-17 11:15:19 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | /* next */ |
| 2770 | src->next = dst->next; |
| 2771 | dst->next = NULL; |
| 2772 | |
| 2773 | /* prev */ |
| 2774 | if (dst->prev != dst) { |
| 2775 | src->prev = dst->prev; |
| 2776 | } |
| 2777 | dst->prev = dst; |
| 2778 | |
| 2779 | /* parent child */ |
| 2780 | if (dst->parent && (dst->parent->child == dst)) { |
| 2781 | dst->parent->child = src; |
| 2782 | } |
| 2783 | |
| 2784 | /* parent */ |
| 2785 | src->parent = dst->parent; |
| 2786 | dst->parent = NULL; |
| 2787 | |
| 2788 | /* child parent */ |
| 2789 | LY_TREE_FOR(dst->child, child) { |
| 2790 | if (child->parent == dst) { |
| 2791 | child->parent = src; |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | /* child */ |
| 2796 | src->child = dst->child; |
| 2797 | dst->child = NULL; |
| 2798 | } |
| 2799 | |
| 2800 | void |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2801 | lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int remove_from_ctx) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2802 | { |
| 2803 | struct ly_ctx *ctx; |
| 2804 | int i; |
| 2805 | |
| 2806 | if (!module) { |
| 2807 | return; |
| 2808 | } |
| 2809 | |
| 2810 | /* remove schema from the context */ |
| 2811 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2812 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2813 | for (i = 0; i < ctx->models.used; i++) { |
| 2814 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2815 | /* move all the models to not change the order in the list */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2816 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2817 | memmove(&ctx->models.list[i], ctx->models.list[i + 1], (ctx->models.used - i) * sizeof *ctx->models.list); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2818 | ctx->models.list[ctx->models.used] = NULL; |
| 2819 | /* we are done */ |
| 2820 | break; |
| 2821 | } |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2826 | module_free_common(module, private_destructor); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2827 | |
| 2828 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2829 | lydict_remove(ctx, module->ns); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2830 | |
| 2831 | free(module); |
| 2832 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2833 | |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2834 | static void |
| 2835 | lys_features_disable_recursive(struct lys_feature *f) |
| 2836 | { |
| 2837 | unsigned int i; |
| 2838 | struct lys_feature *depf; |
| 2839 | |
| 2840 | /* disable the feature */ |
| 2841 | f->flags &= ~LYS_FENABLED; |
| 2842 | |
| 2843 | /* by disabling feature we have to disable also all features that depends on this feature */ |
| 2844 | if (f->depfeatures) { |
| 2845 | for (i = 0; i < f->depfeatures->number; i++) { |
| 2846 | depf = (struct lys_feature *)f->depfeatures->set.g[i]; |
| 2847 | if (depf->flags & LYS_FENABLED) { |
| 2848 | lys_features_disable_recursive(depf); |
| 2849 | } |
| 2850 | } |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2855 | /* |
| 2856 | * op: 1 - enable, 0 - disable |
| 2857 | */ |
| 2858 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2859 | lys_features_change(const struct lys_module *module, const char *name, int op) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2860 | { |
| 2861 | int all = 0; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2862 | int i, j, k; |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 2863 | int progress, faili, failj, failk; |
| 2864 | |
| 2865 | uint8_t fsize; |
| 2866 | struct lys_feature *f; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2867 | |
| 2868 | if (!module || !name || !strlen(name)) { |
| 2869 | return EXIT_FAILURE; |
| 2870 | } |
| 2871 | |
| 2872 | if (!strcmp(name, "*")) { |
| 2873 | /* enable all */ |
| 2874 | all = 1; |
| 2875 | } |
| 2876 | |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 2877 | progress = failk = 1; |
| 2878 | while (progress && failk) { |
| 2879 | for (i = -1, failk = progress = 0; i < module->inc_size; i++) { |
| 2880 | if (i == -1) { |
| 2881 | fsize = module->features_size; |
| 2882 | f = module->features; |
| 2883 | } else { |
| 2884 | fsize = module->inc[i].submodule->features_size; |
| 2885 | f = module->inc[i].submodule->features; |
| 2886 | } |
| 2887 | |
| 2888 | for (j = 0; j < fsize; j++) { |
| 2889 | if (all || !strcmp(f[j].name, name)) { |
| 2890 | /* skip already set features */ |
| 2891 | if (op && (f[j].flags & LYS_FENABLED)) { |
| 2892 | continue; |
| 2893 | } else if (!op && !(f[j].flags & LYS_FENABLED)) { |
| 2894 | continue; |
| 2895 | } |
| 2896 | |
| 2897 | if (op) { |
| 2898 | /* check referenced features if they are enabled */ |
| 2899 | for (k = 0; k < f[j].iffeature_size; k++) { |
| 2900 | if (!resolve_iffeature(&f[j].iffeature[k])) { |
| 2901 | if (all) { |
| 2902 | faili = i; |
| 2903 | failj = j; |
| 2904 | failk = k + 1; |
| 2905 | break; |
| 2906 | } else { |
| 2907 | LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
| 2908 | f[j].name, k + 1); |
| 2909 | return EXIT_FAILURE; |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | if (k == f[j].iffeature_size) { |
| 2915 | /* the last check passed, do the change */ |
| 2916 | f[j].flags |= LYS_FENABLED; |
| 2917 | progress++; |
| 2918 | } |
| 2919 | } else { |
Radek Krejci | 9de2c04 | 2016-10-19 16:53:06 +0200 | [diff] [blame] | 2920 | lys_features_disable_recursive(&f[j]); |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 2921 | progress++; |
| 2922 | } |
| 2923 | if (!all) { |
| 2924 | /* stop in case changing a single feature */ |
| 2925 | return EXIT_SUCCESS; |
Radek Krejci | 9ff0a92 | 2016-07-14 13:08:05 +0200 | [diff] [blame] | 2926 | } |
| 2927 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | } |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 2931 | if (failk) { |
| 2932 | /* print info about the last failing feature */ |
| 2933 | LOGERR(LY_EINVAL, "Feature \"%s\" is disabled by its %d. if-feature condition.", |
| 2934 | faili == -1 ? module->features[failj].name : module->inc[faili].submodule->features[failj].name, failk); |
| 2935 | return EXIT_FAILURE; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | if (all) { |
| 2939 | return EXIT_SUCCESS; |
| 2940 | } else { |
Radek Krejci | a889c1f | 2016-10-19 15:50:11 +0200 | [diff] [blame] | 2941 | /* the specified feature not found */ |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2942 | return EXIT_FAILURE; |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2947 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2948 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2949 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2953 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2954 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2955 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2959 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2960 | { |
| 2961 | int i, j; |
| 2962 | |
| 2963 | if (!module || !feature) { |
| 2964 | return -1; |
| 2965 | } |
| 2966 | |
| 2967 | /* search for the specified feature */ |
| 2968 | /* module itself */ |
| 2969 | for (i = 0; i < module->features_size; i++) { |
| 2970 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2971 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2972 | return 1; |
| 2973 | } else { |
| 2974 | return 0; |
| 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | /* submodules */ |
| 2980 | for (j = 0; j < module->inc_size; j++) { |
| 2981 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2982 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2983 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2984 | return 1; |
| 2985 | } else { |
| 2986 | return 0; |
| 2987 | } |
| 2988 | } |
| 2989 | } |
| 2990 | } |
| 2991 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2992 | /* feature definition not found */ |
| 2993 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2994 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2995 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2996 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2997 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2998 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2999 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3000 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3001 | unsigned int count; |
| 3002 | |
| 3003 | if (!module) { |
| 3004 | return NULL; |
| 3005 | } |
| 3006 | |
| 3007 | count = module->features_size; |
| 3008 | for (i = 0; i < module->inc_size; i++) { |
| 3009 | count += module->inc[i].submodule->features_size; |
| 3010 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3011 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3012 | if (!result) { |
| 3013 | LOGMEM; |
| 3014 | return NULL; |
| 3015 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3016 | if (states) { |
| 3017 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3018 | if (!(*states)) { |
| 3019 | LOGMEM; |
| 3020 | free(result); |
| 3021 | return NULL; |
| 3022 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3023 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3024 | count = 0; |
| 3025 | |
| 3026 | /* module itself */ |
| 3027 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3028 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3029 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3030 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3031 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3032 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3033 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3034 | } |
| 3035 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3036 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | /* submodules */ |
| 3040 | for (j = 0; j < module->inc_size; j++) { |
| 3041 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 3042 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 3043 | if (states) { |
| 3044 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 3045 | (*states)[count] = 1; |
| 3046 | } else { |
| 3047 | (*states)[count] = 0; |
| 3048 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3049 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3050 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3051 | } |
| 3052 | } |
| 3053 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 3054 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3055 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 3056 | |
| 3057 | return result; |
| 3058 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3059 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3060 | API struct lys_module * |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3061 | lys_node_module(const struct lys_node *node) |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3062 | { |
| 3063 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 3064 | } |
| 3065 | |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 3066 | API struct lys_module * |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 3067 | lys_main_module(const struct lys_module *module) |
Michal Vasko | 320e853 | 2016-02-15 13:11:57 +0100 | [diff] [blame] | 3068 | { |
| 3069 | return (module->type ? ((struct lys_submodule *)module)->belongsto : (struct lys_module *)module); |
| 3070 | } |
| 3071 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3072 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3073 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 3074 | { |
| 3075 | if (!node || !node->parent) { |
| 3076 | return NULL; |
| 3077 | } |
| 3078 | |
| 3079 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 3080 | return ((struct lys_node_augment *)node->parent)->target; |
| 3081 | } |
| 3082 | |
| 3083 | return node->parent; |
| 3084 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3085 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3086 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3087 | lys_set_private(const struct lys_node *node, void *priv) |
| 3088 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3089 | void *prev; |
| 3090 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3091 | if (!node) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3092 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 3093 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3094 | } |
| 3095 | |
Mislav Novakovic | b5529e5 | 2016-02-29 11:42:43 +0100 | [diff] [blame] | 3096 | prev = node->priv; |
| 3097 | ((struct lys_node *)node)->priv = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 3098 | |
| 3099 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 3100 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3101 | |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3102 | int |
| 3103 | lys_leaf_add_leafref_target(struct lys_node_leaf *leafref_target, struct lys_node *leafref) |
| 3104 | { |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3105 | struct lys_node_leaf *iter = leafref_target; |
| 3106 | |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3107 | if (!(leafref_target->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3108 | LOGINT; |
| 3109 | return -1; |
| 3110 | } |
| 3111 | |
Pavol Vican | 9317515 | 2016-08-30 15:34:44 +0200 | [diff] [blame] | 3112 | /* check for config flag */ |
| 3113 | if ((leafref->flags & LYS_CONFIG_W) && (leafref_target->flags & LYS_CONFIG_R)) { |
| 3114 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, leafref, |
| 3115 | "The %s is config but refers to a non-config %s.", |
| 3116 | strnodetype(leafref->nodetype), strnodetype(leafref_target->nodetype)); |
| 3117 | return -1; |
| 3118 | } |
Radek Krejci | d8fb03c | 2016-06-13 15:52:22 +0200 | [diff] [blame] | 3119 | /* check for cycles */ |
| 3120 | while (iter && iter->type.base == LY_TYPE_LEAFREF) { |
| 3121 | if ((void *)iter == (void *)leafref) { |
| 3122 | /* cycle detected */ |
| 3123 | LOGVAL(LYE_CIRC_LEAFREFS, LY_VLOG_LYS, leafref); |
| 3124 | return -1; |
| 3125 | } |
| 3126 | iter = iter->type.info.lref.target; |
| 3127 | } |
| 3128 | |
| 3129 | /* create fake child - the ly_set structure to hold the list of |
Michal Vasko | 48a573d | 2016-07-01 11:46:02 +0200 | [diff] [blame] | 3130 | * leafrefs referencing the leaf(-list) */ |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3131 | if (!leafref_target->backlinks) { |
| 3132 | leafref_target->backlinks = (void*)ly_set_new(); |
| 3133 | if (!leafref_target->backlinks) { |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3134 | LOGMEM; |
| 3135 | return -1; |
| 3136 | } |
| 3137 | } |
Radek Krejci | 85a54be | 2016-10-20 12:39:56 +0200 | [diff] [blame] | 3138 | ly_set_add((struct ly_set *)leafref_target->backlinks, leafref, 0); |
Michal Vasko | 01c6fd2 | 2016-05-20 11:43:05 +0200 | [diff] [blame] | 3139 | |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3143 | /* not needed currently */ |
| 3144 | #if 0 |
| 3145 | |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3146 | static const char * |
| 3147 | lys_data_path_reverse(const struct lys_node *node, char * const buf, uint32_t buf_len) |
| 3148 | { |
| 3149 | struct lys_module *prev_mod; |
| 3150 | uint32_t str_len, mod_len, buf_idx; |
| 3151 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3152 | if (!(node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3153 | LOGINT; |
| 3154 | return NULL; |
| 3155 | } |
| 3156 | |
| 3157 | buf_idx = buf_len - 1; |
| 3158 | buf[buf_idx] = '\0'; |
| 3159 | |
| 3160 | while (node) { |
| 3161 | if (lys_parent(node)) { |
| 3162 | prev_mod = lys_node_module(lys_parent(node)); |
| 3163 | } else { |
| 3164 | prev_mod = NULL; |
| 3165 | } |
| 3166 | |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 3167 | if (node->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3168 | str_len = strlen(node->name); |
| 3169 | |
| 3170 | if (prev_mod != node->module) { |
| 3171 | mod_len = strlen(node->module->name); |
| 3172 | } else { |
| 3173 | mod_len = 0; |
| 3174 | } |
| 3175 | |
| 3176 | if (buf_idx < 1 + (mod_len ? mod_len + 1 : 0) + str_len) { |
| 3177 | LOGINT; |
| 3178 | return NULL; |
| 3179 | } |
| 3180 | |
| 3181 | buf_idx -= 1 + (mod_len ? mod_len + 1 : 0) + str_len; |
| 3182 | |
| 3183 | buf[buf_idx] = '/'; |
| 3184 | if (mod_len) { |
| 3185 | memcpy(buf + buf_idx + 1, node->module->name, mod_len); |
| 3186 | buf[buf_idx + 1 + mod_len] = ':'; |
| 3187 | } |
| 3188 | memcpy(buf + buf_idx + 1 + (mod_len ? mod_len + 1 : 0), node->name, str_len); |
| 3189 | } |
| 3190 | |
| 3191 | node = lys_parent(node); |
| 3192 | } |
| 3193 | |
| 3194 | return buf + buf_idx; |
| 3195 | } |
| 3196 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3197 | #endif |
| 3198 | |
| 3199 | API struct ly_set * |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 3200 | lys_find_xpath(const struct lys_node *node, const char *expr, int options) |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3201 | { |
| 3202 | struct lyxp_set set; |
| 3203 | struct ly_set *ret_set; |
| 3204 | uint32_t i; |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 3205 | int opts; |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3206 | |
| 3207 | if (!node || !expr) { |
| 3208 | ly_errno = LY_EINVAL; |
| 3209 | return NULL; |
| 3210 | } |
| 3211 | |
| 3212 | memset(&set, 0, sizeof set); |
| 3213 | |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 3214 | opts = LYXP_SNODE; |
| 3215 | if (options & LYS_FIND_OUTPUT) { |
| 3216 | opts |= LYXP_SNODE_OUTPUT; |
| 3217 | } |
| 3218 | |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3219 | /* node and nodetype won't matter at all since it is absolute */ |
Michal Vasko | db1da03 | 2016-09-08 10:07:38 +0200 | [diff] [blame] | 3220 | if (lyxp_atomize(expr, node, LYXP_NODE_ELEM, &set, opts)) { |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 3221 | free(set.val.snodes); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | |
| 3225 | ret_set = ly_set_new(); |
| 3226 | |
| 3227 | for (i = 0; i < set.used; ++i) { |
| 3228 | if (!set.val.snodes[i].in_ctx) { |
| 3229 | continue; |
| 3230 | } |
| 3231 | assert(set.val.snodes[i].in_ctx == 1); |
| 3232 | |
| 3233 | switch (set.val.snodes[i].type) { |
| 3234 | case LYXP_NODE_ELEM: |
| 3235 | if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) { |
| 3236 | ly_set_free(ret_set); |
| 3237 | free(set.val.snodes); |
| 3238 | return NULL; |
| 3239 | } |
| 3240 | break; |
| 3241 | default: |
| 3242 | /* ignore roots, text and attr should not ever appear */ |
| 3243 | break; |
| 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | free(set.val.snodes); |
| 3248 | return ret_set; |
| 3249 | } |
| 3250 | |
| 3251 | API struct ly_set * |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3252 | lys_xpath_atomize(const struct lys_node *cur_snode, enum lyxp_node_type cur_snode_type, const char *expr, int options) |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3253 | { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3254 | struct lyxp_set set; |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3255 | struct ly_set *ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3256 | uint32_t i; |
| 3257 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3258 | if (!cur_snode || !expr) { |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3259 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3260 | } |
| 3261 | |
Michal Vasko | b94a5e4 | 2016-09-08 14:01:56 +0200 | [diff] [blame] | 3262 | /* adjust the root */ |
| 3263 | if ((cur_snode_type == LYXP_NODE_ROOT) || (cur_snode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 3264 | do { |
| 3265 | cur_snode = lys_getnext(NULL, NULL, lys_node_module(cur_snode), 0); |
| 3266 | } while ((cur_snode_type == LYXP_NODE_ROOT_CONFIG) && (cur_snode->flags & LYS_CONFIG_R)); |
| 3267 | } |
| 3268 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3269 | memset(&set, 0, sizeof set); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3270 | |
| 3271 | if (options & LYXP_MUST) { |
| 3272 | options &= ~LYXP_MUST; |
| 3273 | options |= LYXP_SNODE_MUST; |
| 3274 | } else if (options & LYXP_WHEN) { |
| 3275 | options &= ~LYXP_WHEN; |
| 3276 | options |= LYXP_SNODE_WHEN; |
| 3277 | } else { |
| 3278 | options |= LYXP_SNODE; |
| 3279 | } |
| 3280 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3281 | if (lyxp_atomize(expr, cur_snode, cur_snode_type, &set, options)) { |
| 3282 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3283 | return NULL; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3284 | } |
| 3285 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3286 | ret_set = ly_set_new(); |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3287 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3288 | for (i = 0; i < set.used; ++i) { |
| 3289 | switch (set.val.snodes[i].type) { |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3290 | case LYXP_NODE_ELEM: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3291 | if (ly_set_add(ret_set, set.val.snodes[i].snode, LY_SET_OPT_USEASLIST) == -1) { |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3292 | ly_set_free(ret_set); |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3293 | free(set.val.snodes); |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3294 | return NULL; |
| 3295 | } |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3296 | break; |
| 3297 | default: |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3298 | /* ignore roots, text and attr should not ever appear */ |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3299 | break; |
| 3300 | } |
| 3301 | } |
| 3302 | |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3303 | free(set.val.snodes); |
| 3304 | return ret_set; |
| 3305 | } |
| 3306 | |
| 3307 | API struct ly_set * |
| 3308 | lys_node_xpath_atomize(const struct lys_node *node, int options) |
| 3309 | { |
| 3310 | const struct lys_node *next, *elem, *parent, *tmp; |
| 3311 | struct lyxp_set set; |
| 3312 | struct ly_set *ret_set; |
| 3313 | uint16_t i; |
| 3314 | |
| 3315 | if (!node) { |
| 3316 | return NULL; |
| 3317 | } |
| 3318 | |
| 3319 | for (parent = node; parent && !(parent->nodetype & (LYS_NOTIF | LYS_INPUT | LYS_OUTPUT)); parent = lys_parent(parent)); |
| 3320 | if (!parent) { |
| 3321 | /* not in input, output, or notification */ |
| 3322 | return NULL; |
| 3323 | } |
| 3324 | |
| 3325 | ret_set = ly_set_new(); |
Radek Krejci | d9af8d2 | 2016-09-07 18:44:26 +0200 | [diff] [blame] | 3326 | if (!ret_set) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3327 | return NULL; |
| 3328 | } |
| 3329 | |
| 3330 | LY_TREE_DFS_BEGIN(node, next, elem) { |
Michal Vasko | e9914d1 | 2016-10-07 14:32:37 +0200 | [diff] [blame] | 3331 | if ((options & LYXP_NO_LOCAL) && !(elem->flags & LYS_VALID_DEP)) { |
Michal Vasko | 508a50d | 2016-09-07 14:50:33 +0200 | [diff] [blame] | 3332 | /* elem has no dependencies from other subtrees and local nodes get discarded */ |
| 3333 | goto next_iter; |
| 3334 | } |
| 3335 | |
| 3336 | if (lyxp_node_atomize(elem, &set)) { |
| 3337 | ly_set_free(ret_set); |
| 3338 | free(set.val.snodes); |
| 3339 | return NULL; |
| 3340 | } |
| 3341 | |
| 3342 | for (i = 0; i < set.used; ++i) { |
| 3343 | switch (set.val.snodes[i].type) { |
| 3344 | case LYXP_NODE_ELEM: |
| 3345 | if (options & LYXP_NO_LOCAL) { |
| 3346 | for (tmp = set.val.snodes[i].snode; tmp && (tmp != parent); tmp = lys_parent(tmp)); |
| 3347 | if (tmp) { |
| 3348 | /* in local subtree, discard */ |
| 3349 | break; |
| 3350 | } |
| 3351 | } |
| 3352 | if (ly_set_add(ret_set, set.val.snodes[i].snode, 0) == -1) { |
| 3353 | ly_set_free(ret_set); |
| 3354 | free(set.val.snodes); |
| 3355 | return NULL; |
| 3356 | } |
| 3357 | break; |
| 3358 | default: |
| 3359 | /* ignore roots, text and attr should not ever appear */ |
| 3360 | break; |
| 3361 | } |
| 3362 | } |
| 3363 | |
| 3364 | free(set.val.snodes); |
| 3365 | if (!(options & LYXP_RECURSIVE)) { |
| 3366 | break; |
| 3367 | } |
| 3368 | next_iter: |
| 3369 | LY_TREE_DFS_END(node, next, elem); |
| 3370 | } |
| 3371 | |
Michal Vasko | 8548e08 | 2016-07-22 12:00:18 +0200 | [diff] [blame] | 3372 | return ret_set; |
Michal Vasko | 5b3492c | 2016-07-20 09:37:40 +0200 | [diff] [blame] | 3373 | } |
| 3374 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3375 | static void |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3376 | lys_switch_deviation(struct lys_deviation *dev, const struct lys_module *target_module) |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3377 | { |
| 3378 | int ret; |
| 3379 | char *parent_path; |
| 3380 | struct lys_node *target; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3381 | |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 3382 | if (!dev->deviate) { |
| 3383 | return ; |
| 3384 | } |
| 3385 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3386 | if (dev->deviate[0].mod == LY_DEVIATE_NO) { |
| 3387 | if (dev->orig_node) { |
| 3388 | /* removing not-supported deviation ... */ |
| 3389 | if (strrchr(dev->target_name, '/') != dev->target_name) { |
| 3390 | /* ... from a parent */ |
| 3391 | parent_path = strndup(dev->target_name, strrchr(dev->target_name, '/') - dev->target_name); |
| 3392 | |
| 3393 | target = NULL; |
| 3394 | ret = resolve_augment_schema_nodeid(parent_path, NULL, target_module, (const struct lys_node **)&target); |
| 3395 | free(parent_path); |
| 3396 | if (ret || !target) { |
| 3397 | LOGINT; |
| 3398 | return; |
| 3399 | } |
| 3400 | |
| 3401 | lys_node_addchild(target, NULL, dev->orig_node); |
| 3402 | } else { |
| 3403 | /* ... from top-level data */ |
| 3404 | lys_node_addchild(NULL, (struct lys_module *)target_module, dev->orig_node); |
| 3405 | } |
| 3406 | |
| 3407 | dev->orig_node = NULL; |
| 3408 | } else { |
| 3409 | /* adding not-supported deviation */ |
| 3410 | target = NULL; |
| 3411 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 3412 | if (ret || !target) { |
| 3413 | LOGINT; |
| 3414 | return; |
| 3415 | } |
| 3416 | |
| 3417 | lys_node_unlink(target); |
| 3418 | dev->orig_node = target; |
| 3419 | } |
| 3420 | } else { |
| 3421 | target = NULL; |
| 3422 | ret = resolve_augment_schema_nodeid(dev->target_name, NULL, target_module, (const struct lys_node **)&target); |
| 3423 | if (ret || !target) { |
| 3424 | LOGINT; |
| 3425 | return; |
| 3426 | } |
| 3427 | |
| 3428 | lys_node_switch(target, dev->orig_node); |
| 3429 | dev->orig_node = target; |
| 3430 | } |
| 3431 | } |
| 3432 | |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3433 | /* temporarily removes or applies deviations, updates module deviation flag accordingly */ |
| 3434 | void |
| 3435 | lys_switch_deviations(struct lys_module *module) |
| 3436 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3437 | uint32_t i = 0, j; |
| 3438 | const struct lys_module *mod; |
| 3439 | const char *ptr; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3440 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3441 | if (module->deviated) { |
| 3442 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 3443 | if (mod == module) { |
| 3444 | continue; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3445 | } |
| 3446 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3447 | for (j = 0; j < mod->deviation_size; ++j) { |
| 3448 | ptr = strstr(mod->deviation[j].target_name, module->name); |
| 3449 | if (ptr && ptr[strlen(module->name)] == ':') { |
| 3450 | lys_switch_deviation(&mod->deviation[j], module); |
| 3451 | } |
| 3452 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3453 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3454 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3455 | if (module->deviated == 2) { |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3456 | module->deviated = 1; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3457 | } else { |
| 3458 | module->deviated = 2; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | /* not needed currently, but tested and working */ |
| 3464 | #if 0 |
| 3465 | |
| 3466 | void |
| 3467 | lys_sub_module_apply_devs_augs(struct lys_module *module) |
| 3468 | { |
| 3469 | int i; |
| 3470 | struct lys_node_augment *aug; |
| 3471 | struct lys_node *last; |
| 3472 | |
| 3473 | /* re-apply deviations */ |
| 3474 | for (i = 0; i < module->deviation_size; ++i) { |
| 3475 | lys_switch_deviation(&module->deviation[i], module); |
| 3476 | assert(module->deviation[i].orig_node); |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3477 | lys_node_module(module->deviation[i].orig_node)->deviated = 1; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | /* re-apply augments */ |
| 3481 | for (i = 0; i < module->augment_size; ++i) { |
| 3482 | aug = &module->augment[i]; |
| 3483 | assert(aug->target); |
| 3484 | |
| 3485 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3486 | if (aug->target->child) { |
| 3487 | last = aug->target->child->prev; |
| 3488 | last->next = aug->child; |
| 3489 | aug->target->child->prev = aug->child->prev; |
| 3490 | aug->child->prev = last; |
| 3491 | } else { |
| 3492 | aug->target->child = aug->child; |
| 3493 | } |
| 3494 | } |
| 3495 | } |
| 3496 | |
| 3497 | #endif |
| 3498 | |
| 3499 | void |
| 3500 | lys_sub_module_remove_devs_augs(struct lys_module *module) |
| 3501 | { |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3502 | uint32_t i = 0, j; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3503 | struct lys_node *last, *elem; |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3504 | const struct lys_module *mod; |
| 3505 | struct lys_module *target_mod; |
| 3506 | const char *ptr; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3507 | |
| 3508 | /* remove applied deviations */ |
| 3509 | for (i = 0; i < module->deviation_size; ++i) { |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 3510 | if (module->deviation[i].orig_node) { |
| 3511 | target_mod = lys_node_module(module->deviation[i].orig_node); |
| 3512 | } else { |
| 3513 | target_mod = (struct lys_module *)lys_get_import_module(module, NULL, 0, module->deviation[i].target_name + 1, |
| 3514 | strcspn(module->deviation[i].target_name, ":") - 1); |
Radek Krejci | 0fa54e9 | 2016-09-14 14:01:05 +0200 | [diff] [blame] | 3515 | target_mod = (struct lys_module *)lys_get_implemented_module(target_mod); |
Pavol Vican | 64d0b76 | 2016-08-25 10:44:59 +0200 | [diff] [blame] | 3516 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3517 | lys_switch_deviation(&module->deviation[i], module); |
| 3518 | |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3519 | /* clear the deviation flag if possible */ |
| 3520 | while ((mod = ly_ctx_get_module_iter(module->ctx, &i))) { |
| 3521 | if ((mod == module) || (mod == target_mod)) { |
| 3522 | continue; |
| 3523 | } |
| 3524 | |
| 3525 | for (j = 0; j < mod->deviation_size; ++j) { |
| 3526 | ptr = strstr(mod->deviation[j].target_name, target_mod->name); |
| 3527 | if (ptr && (ptr[strlen(target_mod->name)] == ':')) { |
| 3528 | /* some other module deviation targets the inspected module, flag remains */ |
| 3529 | break; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3530 | } |
| 3531 | } |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3532 | |
| 3533 | if (j < mod->deviation_size) { |
| 3534 | break; |
| 3535 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3536 | } |
Michal Vasko | 89563fc | 2016-07-28 16:19:35 +0200 | [diff] [blame] | 3537 | |
| 3538 | if (!mod) { |
| 3539 | target_mod->deviated = 0; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | /* remove applied augments */ |
| 3544 | for (i = 0; i < module->augment_size; ++i) { |
Radek Krejci | dbc1526 | 2016-06-16 14:58:29 +0200 | [diff] [blame] | 3545 | if (!module->augment[i].target) { |
| 3546 | /* skip not resolved augments */ |
| 3547 | continue; |
| 3548 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3549 | |
| 3550 | elem = module->augment[i].child; |
| 3551 | if (elem) { |
| 3552 | LY_TREE_FOR(elem, last) { |
| 3553 | if (!last->next || (last->next->parent != (struct lys_node *)&module->augment[i])) { |
| 3554 | break; |
| 3555 | } |
| 3556 | } |
| 3557 | /* elem is first augment child, last is the last child */ |
| 3558 | |
| 3559 | /* parent child ptr */ |
| 3560 | if (module->augment[i].target->child == elem) { |
| 3561 | module->augment[i].target->child = last->next; |
| 3562 | } |
| 3563 | |
| 3564 | /* parent child next ptr */ |
| 3565 | if (elem->prev->next) { |
| 3566 | elem->prev->next = last->next; |
| 3567 | } |
| 3568 | |
| 3569 | /* parent child prev ptr */ |
| 3570 | if (last->next) { |
| 3571 | last->next->prev = elem->prev; |
| 3572 | } else if (module->augment[i].target->child) { |
| 3573 | module->augment[i].target->child->prev = elem->prev; |
| 3574 | } |
| 3575 | |
| 3576 | /* update augment children themselves */ |
| 3577 | elem->prev = last; |
| 3578 | last->next = NULL; |
| 3579 | } |
Michal Vasko | b8f7132 | 2016-05-03 11:39:56 +0200 | [diff] [blame] | 3580 | |
| 3581 | /* needs to be NULL for lys_augment_free() to free the children */ |
| 3582 | module->augment[i].target = NULL; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3583 | } |
| 3584 | } |
| 3585 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3586 | static int |
| 3587 | lys_set_implemented_recursion(struct lys_module *module, struct unres_schema *unres) |
| 3588 | { |
| 3589 | struct lys_node *root, *next, *node; |
| 3590 | uint8_t i; |
| 3591 | |
| 3592 | for (i = 0; i < module->augment_size; i++) { |
| 3593 | /* apply augment */ |
Michal Vasko | a1911b9 | 2016-09-19 14:57:34 +0200 | [diff] [blame] | 3594 | if (!module->augment[i].target |
| 3595 | && (unres_schema_add_node(module, unres, &module->augment[i], UNRES_AUGMENT, NULL) == -1)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3596 | return -1; |
| 3597 | } |
| 3598 | } |
| 3599 | LY_TREE_FOR(module->data, root) { |
| 3600 | /* handle leafrefs and recursively change the implemented flags in the leafref targets */ |
| 3601 | LY_TREE_DFS_BEGIN(root, next, node) { |
| 3602 | if (node->nodetype == LYS_GROUPING) { |
| 3603 | goto nextsibling; |
| 3604 | } |
| 3605 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3606 | if (((struct lys_node_leaf *)node)->type.base == LY_TYPE_LEAFREF) { |
| 3607 | if (unres_schema_add_node(module, unres, &((struct lys_node_leaf *)node)->type, |
| 3608 | UNRES_TYPE_LEAFREF, node) == -1) { |
| 3609 | return EXIT_FAILURE; |
| 3610 | } |
| 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | /* modified LY_TREE_DFS_END */ |
| 3615 | next = node->child; |
| 3616 | /* child exception for leafs, leaflists and anyxml without children */ |
| 3617 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 3618 | next = NULL; |
| 3619 | } |
| 3620 | if (!next) { |
| 3621 | nextsibling: |
| 3622 | /* no children */ |
| 3623 | if (node == root) { |
| 3624 | /* we are done, root has no children */ |
| 3625 | break; |
| 3626 | } |
| 3627 | /* try siblings */ |
| 3628 | next = node->next; |
| 3629 | } |
| 3630 | while (!next) { |
| 3631 | /* parent is already processed, go to its sibling */ |
| 3632 | node = lys_parent(node); |
| 3633 | /* no siblings, go back through parents */ |
| 3634 | if (lys_parent(node) == lys_parent(root)) { |
| 3635 | /* we are done, no next element to process */ |
| 3636 | break; |
| 3637 | } |
| 3638 | next = node->next; |
| 3639 | } |
| 3640 | } |
| 3641 | } |
| 3642 | |
| 3643 | return EXIT_SUCCESS; |
| 3644 | } |
| 3645 | |
| 3646 | API int |
| 3647 | lys_set_implemented(const struct lys_module *module) |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3648 | { |
| 3649 | struct ly_ctx *ctx; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3650 | struct unres_schema *unres; |
| 3651 | int i, j; |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3652 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3653 | if (!module) { |
| 3654 | ly_errno = LY_EINVAL; |
| 3655 | return EXIT_FAILURE; |
| 3656 | } |
| 3657 | |
| 3658 | module = lys_main_module(module); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3659 | if (module->implemented) { |
| 3660 | return EXIT_SUCCESS; |
| 3661 | } |
| 3662 | |
| 3663 | ctx = module->ctx; |
| 3664 | |
| 3665 | for (i = 0; i < ctx->models.used; ++i) { |
| 3666 | if (module == ctx->models.list[i]) { |
| 3667 | continue; |
| 3668 | } |
| 3669 | |
| 3670 | if (!strcmp(module->name, ctx->models.list[i]->name) && ctx->models.list[i]->implemented) { |
| 3671 | LOGERR(LY_EINVAL, "Module \"%s\" in another revision already implemented.", module->name); |
| 3672 | return EXIT_FAILURE; |
| 3673 | } |
| 3674 | } |
| 3675 | |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3676 | unres = calloc(1, sizeof *unres); |
| 3677 | if (!unres) { |
| 3678 | LOGMEM; |
| 3679 | return EXIT_FAILURE; |
| 3680 | } |
| 3681 | /* recursively make the module implemented */ |
| 3682 | ((struct lys_module *)module)->implemented = 1; |
| 3683 | if (lys_set_implemented_recursion((struct lys_module *)module, unres)) { |
| 3684 | goto error; |
| 3685 | } |
| 3686 | /* process augments in submodules */ |
| 3687 | for (i = 0; i < module->inc_size; ++i) { |
| 3688 | if (!module->inc[i].submodule) { |
| 3689 | continue; |
| 3690 | } |
| 3691 | for (j = 0; j < module->inc[i].submodule->augment_size; j++) { |
| 3692 | /* apply augment */ |
Michal Vasko | a1911b9 | 2016-09-19 14:57:34 +0200 | [diff] [blame] | 3693 | if (!module->inc[i].submodule->augment[i].target |
| 3694 | && (unres_schema_add_node((struct lys_module *)module->inc[i].submodule, unres, |
| 3695 | &module->inc[i].submodule->augment[i], UNRES_AUGMENT, NULL) == -1)) { |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3696 | goto error; |
| 3697 | } |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3698 | } |
| 3699 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3700 | /* resolve rest of unres items */ |
| 3701 | if (unres->count && resolve_unres_schema((struct lys_module *)module, unres)) { |
| 3702 | goto error; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3703 | } |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3704 | unres_schema_free((struct lys_module *)module, &unres); |
Michal Vasko | 2605575 | 2016-05-03 11:36:31 +0200 | [diff] [blame] | 3705 | |
| 3706 | return EXIT_SUCCESS; |
Radek Krejci | 27fe55e | 2016-09-13 17:13:35 +0200 | [diff] [blame] | 3707 | |
| 3708 | error: |
| 3709 | ((struct lys_module *)module)->implemented = 0; |
| 3710 | unres_schema_free((struct lys_module *)module, &unres); |
| 3711 | return EXIT_FAILURE; |
Michal Vasko | 9eb6dd0 | 2016-05-02 14:52:40 +0200 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | void |
| 3715 | lys_submodule_module_data_free(struct lys_submodule *submodule) |
| 3716 | { |
| 3717 | struct lys_node *next, *elem; |
| 3718 | |
| 3719 | /* remove parsed data */ |
| 3720 | LY_TREE_FOR_SAFE(submodule->belongsto->data, next, elem) { |
| 3721 | if (elem->module == (struct lys_module *)submodule) { |
| 3722 | lys_node_free(elem, NULL, 0); |
| 3723 | } |
| 3724 | } |
| 3725 | } |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 3726 | |
| 3727 | int |
| 3728 | lys_is_key(struct lys_node_list *list, struct lys_node_leaf *leaf) |
| 3729 | { |
| 3730 | uint8_t i; |
| 3731 | |
| 3732 | for (i = 0; i < list->keys_size; i++) { |
| 3733 | if (list->keys[i] == leaf) { |
| 3734 | return i + 1; |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | return 0; |
| 3739 | } |
Radek Krejci | 0a0b1fc | 2016-10-18 15:57:37 +0200 | [diff] [blame] | 3740 | |
| 3741 | API char * |
| 3742 | lys_path(const struct lys_node *node) |
| 3743 | { |
| 3744 | char *buf_backup = NULL, *buf = ly_buf(), *result = NULL; |
| 3745 | uint16_t index = LY_BUF_SIZE - 1; |
| 3746 | |
| 3747 | if (!node) { |
| 3748 | LOGERR(LY_EINVAL, "%s: NULL node parameter", __func__); |
| 3749 | return NULL; |
| 3750 | } |
| 3751 | |
| 3752 | /* backup the shared internal buffer */ |
| 3753 | if (ly_buf_used && buf[0]) { |
| 3754 | buf_backup = strndup(buf, LY_BUF_SIZE - 1); |
| 3755 | } |
| 3756 | ly_buf_used++; |
| 3757 | |
| 3758 | /* build the path */ |
| 3759 | buf[index] = '\0'; |
| 3760 | ly_vlog_build_path_reverse(LY_VLOG_LYS, node, buf, &index); |
| 3761 | result = strdup(&buf[index]); |
| 3762 | |
| 3763 | /* restore the shared internal buffer */ |
| 3764 | if (buf_backup) { |
| 3765 | strcpy(buf, buf_backup); |
| 3766 | free(buf_backup); |
| 3767 | } |
| 3768 | ly_buf_used--; |
| 3769 | |
| 3770 | return result; |
| 3771 | } |
Radek Krejci | 9ad23f4 | 2016-10-31 15:46:52 +0100 | [diff] [blame] | 3772 | |