Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 3 | * @file tree_schema.c |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 4 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 5 | * @brief Manipulation with libyang schema data structures |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of the Company nor the names of its contributors |
| 19 | * may be used to endorse or promote products derived from this |
| 20 | * software without specific prior written permission. |
| 21 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | #define _GNU_SOURCE |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 24 | #include <assert.h> |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 25 | #include <ctype.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <sys/mman.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 28 | #include <sys/types.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 29 | #include <sys/stat.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 30 | #include <fcntl.h> |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 31 | #include <string.h> |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | #include <errno.h> |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 34 | |
| 35 | #include "common.h" |
| 36 | #include "context.h" |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 37 | #include "parser.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 38 | #include "resolve.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 39 | #include "xml.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 40 | #include "xml_internal.h" |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 41 | #include "tree_internal.h" |
Radek Krejci | eab784a | 2015-08-27 09:56:53 +0200 | [diff] [blame] | 42 | #include "validation.h" |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 43 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 44 | API const struct lys_feature * |
| 45 | lys_is_disabled(const struct lys_node *node, int recursive) |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 46 | { |
| 47 | int i; |
| 48 | |
| 49 | check: |
| 50 | if (node->nodetype != LYS_INPUT && node->nodetype != LYS_OUTPUT) { |
| 51 | /* input/output does not have if-feature, so skip them */ |
| 52 | |
| 53 | /* check local if-features */ |
| 54 | for (i = 0; i < node->features_size; i++) { |
| 55 | if (!(node->features[i]->flags & LYS_FENABLED)) { |
| 56 | return node->features[i]; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (!recursive) { |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | /* go through parents */ |
| 66 | if (node->nodetype == LYS_AUGMENT) { |
| 67 | /* go to parent actually means go to the target node */ |
| 68 | node = ((struct lys_node_augment *)node)->target; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 69 | } else if (node->parent) { |
| 70 | node = node->parent; |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 71 | } else { |
| 72 | return NULL; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 75 | if (recursive == 2) { |
| 76 | /* continue only if the node cannot have a data instance */ |
| 77 | if (node->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST)) { |
| 78 | return NULL; |
| 79 | } |
| 80 | } |
| 81 | goto check; |
Radek Krejci | 48061fb | 2015-08-05 15:41:07 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 84 | int |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 85 | lys_get_sibling(const struct lys_node *siblings, const char *mod_name, int mod_name_len, const char *name, |
| 86 | int nam_len, LYS_NODE type, const struct lys_node **ret) |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 87 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 88 | const struct lys_node *node, *parent = NULL; |
| 89 | const struct lys_module *mod = NULL; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 90 | const char *node_mod_name; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 91 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 92 | assert(siblings && mod_name && name); |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 93 | assert(!(type & (LYS_USES | LYS_GROUPING))); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 94 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 95 | /* fill the lengths in case the caller is so indifferent */ |
| 96 | if (!mod_name_len) { |
| 97 | mod_name_len = strlen(mod_name); |
| 98 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 99 | if (!nam_len) { |
| 100 | nam_len = strlen(name); |
| 101 | } |
| 102 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 103 | /* set mod correctly */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 104 | parent = lys_parent(siblings); |
| 105 | if (!parent) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 106 | mod = siblings->module; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 109 | /* try to find the node */ |
| 110 | node = NULL; |
| 111 | while ((node = lys_getnext(node, parent, mod, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 112 | if (!type || (node->nodetype & type)) { |
| 113 | /* modules need to always be checked, we want to skip augments (no other reason to check them) */ |
| 114 | if (node->module->type) { |
| 115 | node_mod_name = ((struct lys_submodule *)node->module)->belongsto->name; |
| 116 | } else { |
| 117 | node_mod_name = node->module->name; |
| 118 | } |
| 119 | if (strncmp(node_mod_name, mod_name, mod_name_len) || node_mod_name[mod_name_len]) { |
| 120 | continue; |
| 121 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 122 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 123 | /* direct name check */ |
| 124 | if ((node->name == name) || (!strncmp(node->name, name, nam_len) && !node->name[nam_len])) { |
| 125 | if (ret) { |
| 126 | *ret = node; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 127 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 128 | return EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 129 | } |
| 130 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return EXIT_FAILURE; |
| 134 | } |
| 135 | |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 136 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 137 | lys_get_data_sibling(const struct lys_module *mod, const struct lys_node *siblings, const char *name, LYS_NODE type, |
| 138 | const struct lys_node **ret) |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 139 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 140 | const struct lys_node *node; |
| 141 | const struct lys_module *cur_mod; |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 142 | int in_submod; |
| 143 | |
| 144 | assert(siblings && name); |
| 145 | assert(!(type & (LYS_AUGMENT | LYS_USES | LYS_GROUPING | LYS_CHOICE | LYS_CASE | LYS_INPUT | LYS_OUTPUT))); |
| 146 | |
| 147 | /* find the beginning */ |
| 148 | while (siblings->prev->next) { |
| 149 | siblings = siblings->prev; |
| 150 | } |
| 151 | |
| 152 | /* we start with the module itself, submodules come later */ |
| 153 | in_submod = 0; |
| 154 | |
| 155 | if (!mod) { |
| 156 | mod = siblings->module; |
| 157 | } |
| 158 | cur_mod = mod; |
| 159 | |
| 160 | while (1) { |
| 161 | /* try to find the node */ |
| 162 | node = NULL; |
| 163 | while ((node = lys_getnext(node, siblings->parent, cur_mod, 0))) { |
| 164 | if (!type || (node->nodetype & type)) { |
| 165 | /* module check */ |
| 166 | if (!node->module->type) { |
| 167 | if (cur_mod != node->module) { |
| 168 | continue; |
| 169 | } |
| 170 | } else { |
| 171 | /* both are submodules */ |
| 172 | if (cur_mod->type) { |
| 173 | if (cur_mod != node->module) { |
| 174 | continue; |
| 175 | } |
| 176 | } else { |
| 177 | if (cur_mod != ((struct lys_submodule *)node->module)->belongsto) { |
| 178 | continue; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /* direct name check */ |
| 184 | if ((node->name == name) || !strcmp(node->name, name)) { |
| 185 | if (ret) { |
| 186 | *ret = node; |
| 187 | } |
| 188 | return EXIT_SUCCESS; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* we're not top-level, search ended */ |
| 194 | if (siblings->parent) { |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | /* let's try the submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 199 | while(in_submod != mod->inc_size) { |
| 200 | if (!mod->inc[in_submod].submodule) { |
| 201 | in_submod++; |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | cur_mod = (struct lys_module *)mod->inc[in_submod].submodule; |
| 206 | siblings = cur_mod->data; |
| 207 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 208 | if (in_submod == mod->inc_size) { |
| 209 | break; |
| 210 | } |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 211 | ++in_submod; |
| 212 | } |
| 213 | |
| 214 | return EXIT_FAILURE; |
| 215 | } |
| 216 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 217 | API const struct lys_node * |
| 218 | 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] | 219 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 220 | const struct lys_node *next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 221 | |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 222 | if (!last) { |
| 223 | /* first call */ |
| 224 | |
| 225 | /* get know where to start */ |
| 226 | if (parent) { |
| 227 | /* schema subtree */ |
| 228 | next = last = parent->child; |
| 229 | } else { |
| 230 | /* top level data */ |
| 231 | assert(module); |
| 232 | next = last = module->data; |
| 233 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 234 | } else { |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 235 | /* continue after the last returned value */ |
| 236 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | repeat: |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 240 | while (next && (next->nodetype == LYS_GROUPING)) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 241 | if (options & LYS_GETNEXT_WITHGROUPING) { |
| 242 | return next; |
| 243 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 244 | next = next->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Radek Krejci | c4ccbd9 | 2016-01-07 13:13:33 +0100 | [diff] [blame] | 247 | if (!next) { |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 248 | if (lys_parent(last) == parent) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 249 | /* no next element */ |
| 250 | return NULL; |
| 251 | } |
Michal Vasko | 7c386e7 | 2015-10-07 15:13:33 +0200 | [diff] [blame] | 252 | last = lys_parent(last); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 253 | next = last->next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 254 | goto repeat; |
| 255 | } |
| 256 | |
| 257 | switch (next->nodetype) { |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 258 | case LYS_INPUT: |
| 259 | case LYS_OUTPUT: |
| 260 | if (options & LYS_GETNEXT_WITHINOUT) { |
| 261 | return next; |
| 262 | } else { |
| 263 | next = next->child; |
| 264 | goto repeat; |
| 265 | } |
| 266 | break; |
| 267 | |
Michal Vasko | a5835e9 | 2015-10-20 15:07:39 +0200 | [diff] [blame] | 268 | case LYS_CASE: |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 269 | if (options & LYS_GETNEXT_WITHCASE) { |
| 270 | return next; |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 271 | } else { |
| 272 | next = next->child; |
| 273 | goto repeat; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 274 | } |
Michal Vasko | b6eedf0 | 2015-10-22 16:07:03 +0200 | [diff] [blame] | 275 | break; |
| 276 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 277 | case LYS_USES: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 278 | /* go into */ |
| 279 | next = next->child; |
| 280 | goto repeat; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 281 | |
Radek Krejci | dfcae7d | 2015-10-20 17:13:01 +0200 | [diff] [blame] | 282 | case LYS_RPC: |
| 283 | case LYS_NOTIF: |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 284 | case LYS_CONTAINER: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 285 | case LYS_LEAF: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 286 | case LYS_ANYXML: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 287 | case LYS_LIST: |
| 288 | case LYS_LEAFLIST: |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 289 | return next; |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 290 | |
| 291 | case LYS_CHOICE: |
| 292 | if (options & LYS_GETNEXT_WITHCHOICE) { |
| 293 | return next; |
| 294 | } else { |
| 295 | /* go into */ |
| 296 | next = next->child; |
| 297 | goto repeat; |
| 298 | } |
| 299 | break; |
| 300 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 301 | default: |
| 302 | /* we should not be here */ |
| 303 | return NULL; |
| 304 | } |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 305 | |
| 306 | |
| 307 | } |
| 308 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 309 | static const struct lys_node * |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 310 | check_mand_getnext(const struct lys_node *last, const struct lys_node *parent, const struct lys_module *module) |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 311 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 312 | const struct lys_node *next; |
| 313 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 314 | next = lys_getnext(last, parent, module, LYS_GETNEXT_WITHCHOICE); |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 315 | |
Radek Krejci | 4b6c211 | 2015-10-06 12:48:34 +0200 | [diff] [blame] | 316 | repeat: |
Radek Krejci | 8bc87f6 | 2015-09-02 16:19:05 +0200 | [diff] [blame] | 317 | if (next && next->nodetype == LYS_CONTAINER) { |
| 318 | if (((struct lys_node_container *)next)->presence) { |
| 319 | /* mandatory elements under the non-existing presence |
| 320 | * container are not mandatory - 7.6.5, rule 1 */ |
| 321 | next = next->next; |
| 322 | } else { |
| 323 | /* go into */ |
| 324 | next = next->child; |
| 325 | } |
| 326 | goto repeat; |
| 327 | } |
| 328 | |
| 329 | return next; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 332 | static const struct lys_node * |
| 333 | check_mand_check(const struct lys_node *node, const struct lys_node *stop, const struct lyd_node *data) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 334 | { |
Radek Krejci | 50c5b0e | 2015-08-21 15:43:45 +0200 | [diff] [blame] | 335 | struct lys_node *siter = NULL, *parent = NULL; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 336 | struct lyd_node *diter = NULL; |
Radek Krejci | dc15443 | 2016-01-21 11:10:59 +0100 | [diff] [blame] | 337 | struct ly_set *set = NULL; |
Radek Krejci | 50c5b0e | 2015-08-21 15:43:45 +0200 | [diff] [blame] | 338 | unsigned int i; |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 339 | uint32_t minmax; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 340 | |
| 341 | if (node->flags & LYS_MAND_TRUE) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 342 | if (!data) { |
| 343 | /* we have no data but a mandatory node */ |
| 344 | return node; |
| 345 | } |
| 346 | switch (node->nodetype) { |
| 347 | case LYS_LEAF: |
| 348 | case LYS_ANYXML: |
| 349 | case LYS_CHOICE: |
| 350 | if (node->parent && node->parent->nodetype == LYS_CASE) { |
| 351 | /* 7.6.5, rule 2 */ |
| 352 | /* 7.9.4, rule 1 */ |
| 353 | if (node->parent->parent->parent == data->schema) { |
| 354 | /* the only case the node's siblings can exist is that the |
| 355 | * data node passed originally to ly_check_mandatory() |
| 356 | * had this choice as a child |
| 357 | */ |
| 358 | /* try to find the node's siblings in data */ |
| 359 | LY_TREE_FOR(data->child, diter) { |
| 360 | LY_TREE_FOR(node->parent->child, siter) { |
| 361 | if (siter == diter->schema) { |
| 362 | /* some sibling exists, rule applies */ |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | if (siter) { |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | if (!siter) { |
| 372 | /* no sibling exists */ |
| 373 | return NULL; |
| 374 | } |
| 375 | } else { |
Radek Krejci | 7eb1213 | 2016-02-01 10:55:23 +0100 | [diff] [blame] | 376 | for (parent = node->parent; parent && parent != stop; parent = parent->parent) { |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 377 | if (parent->nodetype != LYS_CONTAINER) { |
| 378 | /* 7.6.5, rule 1, checking presence is not needed |
| 379 | * since it is done in check_mand_getnext() |
| 380 | */ |
| 381 | ly_set_free(set); |
| 382 | return NULL; |
| 383 | } |
| 384 | /* add the parent to the list for searching in data tree */ |
| 385 | if (!set) { |
| 386 | set = ly_set_new(); |
| 387 | } |
| 388 | /* ignore return - memory error is logged and we will |
| 389 | * check at least the rest of nodes we have */ |
| 390 | (void)ly_set_add(set, parent); |
| 391 | } |
| 392 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 393 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 394 | /* search for instance */ |
| 395 | if (set) { |
| 396 | for (i = 0; i < set->number; i++) { |
| 397 | LY_TREE_FOR(data->child, diter) { |
| 398 | if (diter->schema == set->sset[i]) { |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | if (!diter) { |
| 403 | /* instance not found */ |
| 404 | node = set->sset[i]; |
| 405 | ly_set_free(set); |
| 406 | return node; |
| 407 | } |
| 408 | data = diter; |
| 409 | } |
| 410 | ly_set_free(set); |
| 411 | } |
Radek Krejci | 50c5b0e | 2015-08-21 15:43:45 +0200 | [diff] [blame] | 412 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 413 | LY_TREE_FOR(data->child, diter) { |
| 414 | if (diter->schema == node) { |
| 415 | return NULL; |
| 416 | } |
| 417 | } |
Radek Krejci | 50c5b0e | 2015-08-21 15:43:45 +0200 | [diff] [blame] | 418 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 419 | /* instance not found */ |
| 420 | /* 7.6.5, rule 3 (or 2) */ |
| 421 | /* 7.9.4, rule 2 */ |
| 422 | return node; |
| 423 | default: |
| 424 | /* error */ |
| 425 | break; |
| 426 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 427 | } else if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 428 | /* search for number of instances */ |
| 429 | minmax = 0; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 430 | if (data) { |
| 431 | LY_TREE_FOR(data->child, diter) { |
| 432 | if (diter->schema == node) { |
| 433 | minmax++; |
| 434 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /* check the specified constraints */ |
| 439 | if (node->nodetype == LYS_LIST) { |
| 440 | if (((struct lys_node_list *)node)->min && minmax < ((struct lys_node_list *)node)->min) { |
| 441 | return node; |
| 442 | } |
| 443 | |
| 444 | if (((struct lys_node_list *)node)->max && minmax > ((struct lys_node_list *)node)->max) { |
| 445 | return node; |
| 446 | } |
| 447 | } else if (node->nodetype == LYS_LEAFLIST) { |
| 448 | if (((struct lys_node_leaflist *)node)->min && minmax < ((struct lys_node_leaflist *)node)->min) { |
| 449 | return node; |
| 450 | } |
| 451 | |
| 452 | if (((struct lys_node_leaflist *)node)->max && minmax > ((struct lys_node_leaflist *)node)->max) { |
| 453 | return node; |
| 454 | } |
| 455 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | return NULL; |
| 459 | } |
| 460 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 461 | const struct lys_node * |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 462 | ly_check_mandatory(const struct lyd_node *data, const struct lys_node *schema) |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 463 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 464 | const struct lys_node *siter, *saux, *saux2, *result, *parent = NULL, *parent2; |
| 465 | const struct lyd_node *diter; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 466 | int found; |
| 467 | |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 468 | assert(data || schema); |
| 469 | |
| 470 | if (!data) { /* !data && schema */ |
| 471 | siter = schema; |
| 472 | } else { /* data && !schema */ |
| 473 | schema = siter = data->schema->child; |
| 474 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 475 | |
| 476 | repeat: |
| 477 | while (siter) { |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 478 | if (lys_is_disabled(siter, 2)) { |
| 479 | siter = siter->next; |
| 480 | continue; |
| 481 | } |
| 482 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 483 | switch (siter->nodetype) { |
| 484 | case LYS_CONTAINER: |
| 485 | case LYS_LEAF: |
| 486 | case LYS_ANYXML: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 487 | case LYS_LIST: |
| 488 | case LYS_LEAFLIST: |
| 489 | /* check if there is some mandatory node; first test the siter itself ... */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 490 | result = check_mand_check(siter, siter->parent, data); |
| 491 | if (result) { |
| 492 | return result; |
| 493 | } |
| 494 | /* ... and then the subtree */ |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 495 | if (parent && siter->nodetype == LYS_CONTAINER && !((struct lys_node_container *)siter)->presence) { |
| 496 | saux = NULL; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 497 | while ((saux = check_mand_getnext(saux, siter, NULL))) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 498 | result = check_mand_check(saux, siter, data); |
| 499 | if (result) { |
| 500 | return result; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | siter = siter->next; |
| 505 | break; |
| 506 | case LYS_CHOICE: |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 507 | /* search for instance */ |
| 508 | saux = siter; |
| 509 | siter = siter->child; |
| 510 | found = 0; |
| 511 | parent2 = NULL; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 512 | repeat_choice: |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 513 | while (siter && data) { |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 514 | if (lys_is_disabled(siter, 2)) { |
| 515 | siter = siter->next; |
| 516 | continue; |
| 517 | } |
| 518 | |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 519 | switch (siter->nodetype) { |
| 520 | case LYS_CONTAINER: |
| 521 | case LYS_LEAF: |
| 522 | case LYS_LEAFLIST: |
| 523 | case LYS_LIST: |
| 524 | case LYS_ANYXML: |
| 525 | LY_TREE_FOR(data->child, diter) { |
| 526 | if (diter->schema == siter) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 527 | break; |
| 528 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 529 | } |
| 530 | if (diter) { |
| 531 | /* got instance */ |
| 532 | /* check presence of mandatory siblings */ |
Radek Krejci | 65d0a65 | 2015-08-27 13:09:42 +0200 | [diff] [blame] | 533 | if (parent2 && parent2->nodetype == LYS_CASE) { |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 534 | saux2 = NULL; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 535 | while ((saux2 = check_mand_getnext(saux2, parent2, NULL))) { |
Radek Krejci | 37bda00 | 2015-08-27 11:23:56 +0200 | [diff] [blame] | 536 | result = check_mand_check(saux2, parent2, data); |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 537 | if (result) { |
| 538 | return result; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | siter = parent2 = NULL; |
| 543 | found = 1; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 544 | break; |
| 545 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 546 | siter = siter->next; |
| 547 | break; |
| 548 | case LYS_CASE: |
| 549 | case LYS_CHOICE: |
| 550 | case LYS_USES: |
| 551 | /* go into */ |
Radek Krejci | 37bda00 | 2015-08-27 11:23:56 +0200 | [diff] [blame] | 552 | if (!parent2) { |
| 553 | parent2 = siter; |
| 554 | } |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 555 | siter = siter->child; |
| 556 | break; |
| 557 | case LYS_AUGMENT: |
| 558 | case LYS_GROUPING: |
| 559 | /* skip */ |
| 560 | siter = siter->next; |
| 561 | break; |
| 562 | default: |
| 563 | /* error */ |
| 564 | break; |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 565 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 568 | if (parent2) { |
| 569 | siter = parent2->next; |
| 570 | if (parent2->parent == saux) { |
| 571 | parent2 = NULL; |
| 572 | } else { |
| 573 | parent2 = parent2->parent; |
| 574 | } |
| 575 | goto repeat_choice; |
| 576 | } |
| 577 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame] | 578 | if (!found && (saux->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 579 | return saux; |
| 580 | } |
| 581 | |
| 582 | /* go to next */ |
| 583 | siter = saux->next; |
| 584 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 585 | break; |
| 586 | case LYS_USES: |
| 587 | case LYS_CASE: |
| 588 | /* go into */ |
| 589 | parent = siter; |
| 590 | siter = siter->child; |
| 591 | break; |
| 592 | default: |
| 593 | /* can ignore, go to next */ |
| 594 | siter = siter->next; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (parent) { |
| 600 | siter = parent->next; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 601 | if (parent->parent == schema) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 602 | parent = NULL; |
| 603 | } else { |
| 604 | parent = parent->parent; |
| 605 | } |
| 606 | goto repeat; |
| 607 | } |
| 608 | |
| 609 | return NULL; |
| 610 | } |
| 611 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 612 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 613 | lys_node_unlink(struct lys_node *node) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 614 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 615 | struct lys_node *parent, *first; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 616 | struct lys_module *main_module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 617 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 618 | if (!node) { |
| 619 | return; |
| 620 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 621 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 622 | /* unlink from data model if necessary */ |
| 623 | if (node->module) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 624 | /* get main module with data tree */ |
| 625 | for (main_module = node->module; main_module->type; main_module = ((struct lys_submodule *)main_module)->belongsto); |
| 626 | if (main_module->data == node) { |
| 627 | main_module->data = node->next; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 628 | } |
| 629 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 630 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 631 | /* store pointers to important nodes */ |
| 632 | parent = node->parent; |
Michal Vasko | 3a9943b | 2015-09-23 11:33:50 +0200 | [diff] [blame] | 633 | if (parent && (parent->nodetype == LYS_AUGMENT)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 634 | /* handle augments - first, unlink it from the augment parent ... */ |
| 635 | if (parent->child == node) { |
| 636 | parent->child = node->next; |
| 637 | } |
| 638 | /* and then continue with the target parent */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 639 | parent = ((struct lys_node_augment *)parent)->target; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 640 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 641 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 642 | /* unlink from parent */ |
| 643 | if (parent) { |
| 644 | if (parent->child == node) { |
| 645 | parent->child = node->next; |
| 646 | } |
| 647 | node->parent = NULL; |
| 648 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 649 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 650 | /* unlink from siblings */ |
| 651 | if (node->prev == node) { |
| 652 | /* there are no more siblings */ |
| 653 | return; |
| 654 | } |
| 655 | if (node->next) { |
| 656 | node->next->prev = node->prev; |
| 657 | } else { |
| 658 | /* unlinking the last element */ |
| 659 | if (parent) { |
| 660 | first = parent->child; |
| 661 | } else { |
| 662 | first = node; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 663 | while (first->prev->next) { |
Michal Vasko | 276f96b | 2015-09-23 11:34:28 +0200 | [diff] [blame] | 664 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | first->prev = node->prev; |
| 668 | } |
| 669 | if (node->prev->next) { |
| 670 | node->prev->next = node->next; |
| 671 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 672 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 673 | /* clean up the unlinked element */ |
| 674 | node->next = NULL; |
| 675 | node->prev = node; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 678 | struct lys_node_grp * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 679 | lys_find_grouping_up(const char *name, struct lys_node *start) |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 680 | { |
| 681 | struct lys_node *par_iter, *iter, *stop; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 682 | |
| 683 | for (par_iter = start; par_iter; par_iter = par_iter->parent) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 684 | /* top-level augment, look into module (uses augment is handled correctly below) */ |
| 685 | if (par_iter->parent && !par_iter->parent->parent && (par_iter->parent->nodetype == LYS_AUGMENT)) { |
| 686 | par_iter = par_iter->parent->module->data; |
| 687 | if (!par_iter) { |
Michal Vasko | ccbdb4e | 2015-10-21 15:09:02 +0200 | [diff] [blame] | 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
Michal Vasko | 6f929da | 2015-10-02 16:23:25 +0200 | [diff] [blame] | 692 | 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] | 693 | continue; |
| 694 | } |
| 695 | |
| 696 | for (iter = par_iter, stop = NULL; iter; iter = iter->prev) { |
| 697 | if (!stop) { |
| 698 | stop = par_iter; |
| 699 | } else if (iter == stop) { |
| 700 | break; |
| 701 | } |
| 702 | if (iter->nodetype != LYS_GROUPING) { |
| 703 | continue; |
| 704 | } |
| 705 | |
Radek Krejci | f8426a7 | 2015-10-31 23:14:03 +0100 | [diff] [blame] | 706 | if (!strcmp(name, iter->name)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 707 | return (struct lys_node_grp *)iter; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 712 | return NULL; |
| 713 | } |
| 714 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 715 | /* |
| 716 | * get next grouping in the root's subtree, in the |
| 717 | * first call, tha last is NULL |
| 718 | */ |
| 719 | static struct lys_node_grp * |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 720 | 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] | 721 | { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 722 | struct lys_node *last = (struct lys_node *)lastgrp; |
| 723 | struct lys_node *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 724 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 725 | assert(root); |
| 726 | |
| 727 | if (!last) { |
| 728 | last = root; |
| 729 | } |
| 730 | |
| 731 | while (1) { |
| 732 | if ((last->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 733 | next = last->child; |
| 734 | } else { |
| 735 | next = NULL; |
| 736 | } |
| 737 | if (!next) { |
| 738 | if (last == root) { |
| 739 | /* we are done */ |
| 740 | return NULL; |
| 741 | } |
| 742 | |
| 743 | /* no children, go to siblings */ |
| 744 | next = last->next; |
| 745 | } |
| 746 | while (!next) { |
| 747 | /* go back through parents */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 748 | if (lys_parent(last) == root) { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 749 | /* we are done */ |
| 750 | return NULL; |
| 751 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 752 | next = last->next; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 753 | last = lys_parent(last); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (next->nodetype == LYS_GROUPING) { |
| 757 | return (struct lys_node_grp *)next; |
| 758 | } |
| 759 | |
| 760 | last = next; |
| 761 | } |
| 762 | } |
| 763 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 764 | /* logs directly */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 765 | int |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 766 | lys_check_id(struct lys_node *node, struct lys_node *parent, struct lys_module *module) |
| 767 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 768 | struct lys_node *start, *stop, *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 769 | struct lys_node_grp *grp; |
| 770 | int down; |
| 771 | |
| 772 | assert(node); |
| 773 | |
| 774 | if (!parent) { |
| 775 | assert(module); |
| 776 | } else { |
| 777 | module = parent->module; |
| 778 | } |
| 779 | |
| 780 | switch (node->nodetype) { |
| 781 | case LYS_GROUPING: |
| 782 | /* 6.2.1, rule 6 */ |
| 783 | if (parent) { |
| 784 | if (parent->child) { |
| 785 | down = 1; |
| 786 | start = parent->child; |
| 787 | } else { |
| 788 | down = 0; |
| 789 | start = parent; |
| 790 | } |
| 791 | } else { |
| 792 | down = 1; |
| 793 | start = module->data; |
| 794 | } |
| 795 | /* go up */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 796 | if (lys_find_grouping_up(node->name, start)) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 797 | LOGVAL(LYE_DUPID, 0, "grouping", node->name); |
| 798 | return EXIT_FAILURE; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 799 | } |
| 800 | /* go down, because grouping can be defined after e.g. container in which is collision */ |
| 801 | if (down) { |
| 802 | for (iter = start, stop = NULL; iter; iter = iter->prev) { |
| 803 | if (!stop) { |
| 804 | stop = start; |
| 805 | } else if (iter == stop) { |
| 806 | break; |
| 807 | } |
| 808 | if (!(iter->nodetype & (LYS_CONTAINER | LYS_CHOICE | LYS_LIST | LYS_GROUPING | LYS_INPUT | LYS_OUTPUT))) { |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | grp = NULL; |
| 813 | while ((grp = lys_get_next_grouping(grp, iter))) { |
| 814 | if (node->name == grp->name) { |
| 815 | LOGVAL(LYE_DUPID, 0, "grouping", node->name); |
| 816 | return EXIT_FAILURE; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | break; |
| 822 | case LYS_LEAF: |
| 823 | case LYS_LEAFLIST: |
| 824 | case LYS_LIST: |
| 825 | case LYS_CONTAINER: |
| 826 | case LYS_CHOICE: |
| 827 | case LYS_ANYXML: |
| 828 | /* 6.2.1, rule 7 */ |
| 829 | if (parent) { |
| 830 | iter = parent; |
| 831 | while (iter && (iter->nodetype & (LYS_USES | LYS_CASE | LYS_CHOICE))) { |
| 832 | iter = iter->parent; |
| 833 | } |
| 834 | if (!iter) { |
| 835 | stop = NULL; |
| 836 | iter = module->data; |
| 837 | } else { |
| 838 | stop = iter; |
| 839 | iter = iter->child; |
| 840 | } |
| 841 | } else { |
| 842 | stop = NULL; |
| 843 | iter = module->data; |
| 844 | } |
| 845 | while (iter) { |
| 846 | if (iter->nodetype & (LYS_USES | LYS_CASE)) { |
| 847 | iter = iter->child; |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CONTAINER | LYS_CHOICE | LYS_ANYXML)) { |
| 852 | if (iter->module == node->module && iter->name == node->name) { |
| 853 | LOGVAL(LYE_SPEC, 0, "Duplicated child identifier \"%s\" in \"%s\".", node->name, |
| 854 | stop ? stop->name : "(sub)module"); |
| 855 | return EXIT_FAILURE; |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /* special case for choice - we must check the choice's name as |
| 860 | * well as the names of nodes under the choice |
| 861 | */ |
| 862 | if (iter->nodetype == LYS_CHOICE) { |
| 863 | iter = iter->child; |
| 864 | continue; |
| 865 | } |
| 866 | |
| 867 | /* go to siblings */ |
| 868 | if (!iter->next) { |
| 869 | /* no sibling, go to parent's sibling */ |
| 870 | do { |
| 871 | iter = iter->parent; |
| 872 | if (iter && iter->next) { |
| 873 | break; |
| 874 | } |
| 875 | } while (iter != stop); |
| 876 | |
| 877 | if (iter == stop) { |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | iter = iter->next; |
| 882 | } |
| 883 | break; |
| 884 | case LYS_CASE: |
| 885 | /* 6.2.1, rule 8 */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 886 | if (parent) { |
| 887 | start = parent->child; |
| 888 | } else { |
| 889 | start = module->data; |
| 890 | } |
| 891 | |
| 892 | LY_TREE_FOR(start, iter) { |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 893 | if (!(iter->nodetype & (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 894 | continue; |
| 895 | } |
| 896 | |
| 897 | if (iter->module == node->module && iter->name == node->name) { |
| 898 | LOGVAL(LYE_DUPID, 0, "case", node->name); |
| 899 | return EXIT_FAILURE; |
| 900 | } |
| 901 | } |
| 902 | break; |
| 903 | default: |
| 904 | /* no check needed */ |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | return EXIT_SUCCESS; |
| 909 | } |
| 910 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 911 | /* logs directly */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 912 | int |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 913 | lys_node_addchild(struct lys_node *parent, struct lys_module *module, struct lys_node *child) |
| 914 | { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 915 | struct lys_node *iter; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 916 | int type; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 917 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 918 | assert(child); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 919 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 920 | if (parent) { |
| 921 | type = parent->nodetype; |
| 922 | module = parent->module; |
| 923 | } else { |
| 924 | assert(module); |
| 925 | type = 0; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 928 | /* checks */ |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 929 | switch (type) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 930 | case LYS_CONTAINER: |
| 931 | case LYS_LIST: |
| 932 | case LYS_GROUPING: |
| 933 | case LYS_USES: |
| 934 | case LYS_INPUT: |
| 935 | case LYS_OUTPUT: |
| 936 | case LYS_NOTIF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 937 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 938 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_GROUPING | LYS_LEAF | |
| 939 | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 940 | LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).", |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 941 | strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name); |
| 942 | return EXIT_FAILURE; |
| 943 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 944 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 945 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 946 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 947 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 948 | (LYS_ANYXML | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 949 | LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"choice\" %s.", |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 950 | strnodetype(child->nodetype), parent->name); |
| 951 | return EXIT_FAILURE; |
| 952 | } |
| 953 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 954 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 955 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 956 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 957 | LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"case\" %s.", |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 958 | strnodetype(child->nodetype), parent->name); |
| 959 | return EXIT_FAILURE; |
| 960 | } |
| 961 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 962 | case LYS_RPC: |
| 963 | if (!(child->nodetype & (LYS_INPUT | LYS_OUTPUT | LYS_GROUPING))) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 964 | LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"rpc\" %s.", |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 965 | strnodetype(child->nodetype), parent->name); |
| 966 | return EXIT_FAILURE; |
| 967 | } |
| 968 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 969 | case LYS_LEAF: |
| 970 | case LYS_LEAFLIST: |
| 971 | case LYS_ANYXML: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 972 | LOGVAL(LYE_SPEC, 0, "The \"%s\" statement (%s) cannot have any data substatement.", |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 973 | strnodetype(parent->nodetype), parent->name); |
| 974 | return EXIT_FAILURE; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 975 | case LYS_AUGMENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 976 | if (!(child->nodetype & |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 977 | (LYS_ANYXML | LYS_CASE | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF |
| 978 | | LYS_LEAFLIST | LYS_LIST | LYS_USES))) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 979 | LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in \"%s\" (%s).", |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 980 | strnodetype(child->nodetype), strnodetype(parent->nodetype), parent->name); |
| 981 | return EXIT_FAILURE; |
| 982 | } |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 983 | break; |
| 984 | case LYS_UNKNOWN: |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 985 | /* top level */ |
| 986 | if (!(child->nodetype & |
| 987 | (LYS_ANYXML | LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_GROUPING |
| 988 | | LYS_LEAFLIST | LYS_LIST | LYS_USES | LYS_RPC | LYS_NOTIF | LYS_AUGMENT))) { |
Radek Krejci | 2c0ca27 | 2015-08-14 15:31:01 +0200 | [diff] [blame] | 989 | LOGVAL(LYE_SPEC, 0, "Unexpected substatement \"%s\" in (sub)module \"%s\"", |
| 990 | strnodetype(child->nodetype), module->name); |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 991 | return EXIT_FAILURE; |
| 992 | } |
| 993 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 994 | break; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* check identifier uniqueness */ |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 998 | if (lys_check_id(child, parent, module)) { |
| 999 | return EXIT_FAILURE; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1000 | } |
Radek Krejci | b7155b5 | 2015-06-10 17:03:01 +0200 | [diff] [blame] | 1001 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1002 | if (child->parent) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1003 | lys_node_unlink(child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1004 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1005 | |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1006 | if (!parent) { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 1007 | if (module->data) { |
| 1008 | module->data->prev->next = child; |
| 1009 | child->prev = module->data->prev; |
| 1010 | module->data->prev = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1011 | } else { |
Radek Krejci | 9272055 | 2015-10-05 15:28:27 +0200 | [diff] [blame] | 1012 | module->data = child; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1013 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1014 | } else { |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1015 | if (!parent->child) { |
| 1016 | /* the only/first child of the parent */ |
| 1017 | parent->child = child; |
| 1018 | child->parent = parent; |
| 1019 | iter = child; |
| 1020 | } else { |
| 1021 | /* add a new child at the end of parent's child list */ |
| 1022 | iter = parent->child->prev; |
| 1023 | iter->next = child; |
| 1024 | child->prev = iter; |
| 1025 | } |
| 1026 | while (iter->next) { |
| 1027 | iter = iter->next; |
| 1028 | iter->parent = parent; |
| 1029 | } |
| 1030 | parent->child->prev = iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1031 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1032 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1033 | return EXIT_SUCCESS; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1036 | API const struct lys_module * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1037 | lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1038 | { |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1039 | struct lys_module *mod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1040 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1041 | if (!ctx || !data) { |
| 1042 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1043 | return NULL; |
| 1044 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1045 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1046 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1047 | case LYS_IN_YIN: |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1048 | mod = yin_read_module(ctx, data, 1); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1049 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1050 | case LYS_IN_YANG: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1051 | default: |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1052 | /* TODO */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1053 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1054 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1055 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1056 | return mod; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1059 | struct lys_submodule * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1060 | 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] | 1061 | { |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1062 | struct lys_submodule *submod = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1063 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1064 | assert(module); |
| 1065 | assert(data); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1066 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1067 | /* get the main module */ |
| 1068 | while(module->type) { |
| 1069 | module = ((struct lys_submodule *)module)->belongsto; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1070 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1071 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1072 | switch (format) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1073 | case LYS_IN_YIN: |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1074 | submod = yin_read_submodule(module, data, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1075 | break; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 1076 | case LYS_IN_YANG: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1077 | default: |
Radek Krejci | 0b5805d | 2015-08-13 09:38:02 +0200 | [diff] [blame] | 1078 | /* TODO */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1079 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1080 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1081 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1082 | return submod; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1085 | API const struct lys_module * |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1086 | lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format) |
| 1087 | { |
| 1088 | int fd; |
| 1089 | const struct lys_module *ret; |
| 1090 | |
| 1091 | if (!ctx || !path) { |
| 1092 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | |
| 1096 | fd = open(path, O_RDONLY); |
| 1097 | if (fd == -1) { |
| 1098 | LOGERR(LY_ESYS, "Opening file \"%s\" failed (%s).", path, strerror(errno)); |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
| 1102 | ret = lys_parse_fd(ctx, fd, format); |
| 1103 | close(fd); |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | API const struct lys_module * |
| 1108 | lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format) |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1109 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1110 | const struct lys_module *module; |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 1111 | struct stat sb; |
| 1112 | char *addr; |
| 1113 | |
| 1114 | if (!ctx || fd < 0) { |
| 1115 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1116 | return NULL; |
| 1117 | } |
| 1118 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1119 | if (fstat(fd, &sb) == -1) { |
| 1120 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 1121 | return NULL; |
| 1122 | } |
Radek Krejci | 3006be0 | 2015-12-17 11:24:33 +0100 | [diff] [blame] | 1123 | addr = mmap(NULL, sb.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1124 | if (addr == MAP_FAILED) { |
Michal Vasko | 662610a | 2015-12-07 11:25:45 +0100 | [diff] [blame] | 1125 | LOGERR(LY_EMEM, "Map file into memory failed (%s()).",__func__); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1126 | return NULL; |
| 1127 | } |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1128 | module = lys_parse_mem(ctx, addr, format); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1129 | munmap(addr, sb.st_size); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1130 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1131 | return module; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1134 | struct lys_submodule * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1135 | 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] | 1136 | { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1137 | struct lys_submodule *submodule; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1138 | struct stat sb; |
| 1139 | char *addr; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1140 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1141 | assert(module); |
| 1142 | assert(fd >= 0); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1143 | |
Radek Krejci | 10a833c | 2015-12-16 15:28:37 +0100 | [diff] [blame] | 1144 | if (fstat(fd, &sb) == -1) { |
| 1145 | LOGERR(LY_ESYS, "Failed to stat the file descriptor (%s).", strerror(errno)); |
| 1146 | return NULL; |
| 1147 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1148 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Pavol Vican | e36ea26 | 2015-11-12 11:57:47 +0100 | [diff] [blame] | 1149 | if (addr == MAP_FAILED) { |
| 1150 | LOGERR(LY_EMEM,"Map file into memory failed (%s()).",__func__); |
| 1151 | return NULL; |
| 1152 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1153 | submodule = lys_submodule_parse(module, addr, format, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1154 | munmap(addr, sb.st_size); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1155 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1156 | return submodule; |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 1157 | |
| 1158 | } |
| 1159 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1160 | static struct lys_restr * |
| 1161 | 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] | 1162 | { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1163 | struct lys_restr *result; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1164 | int i; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1165 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1166 | if (!size) { |
| 1167 | return NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1168 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1169 | |
| 1170 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1171 | if (!result) { |
| 1172 | return NULL; |
| 1173 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1174 | for (i = 0; i < size; i++) { |
| 1175 | result[i].expr = lydict_insert(ctx, old[i].expr, 0); |
| 1176 | result[i].dsc = lydict_insert(ctx, old[i].dsc, 0); |
| 1177 | result[i].ref = lydict_insert(ctx, old[i].ref, 0); |
| 1178 | result[i].eapptag = lydict_insert(ctx, old[i].eapptag, 0); |
| 1179 | result[i].emsg = lydict_insert(ctx, old[i].emsg, 0); |
| 1180 | } |
| 1181 | |
| 1182 | return result; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1185 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1186 | lys_restr_free(struct ly_ctx *ctx, struct lys_restr *restr) |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1187 | { |
| 1188 | assert(ctx); |
| 1189 | if (!restr) { |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | lydict_remove(ctx, restr->expr); |
| 1194 | lydict_remove(ctx, restr->dsc); |
| 1195 | lydict_remove(ctx, restr->ref); |
| 1196 | lydict_remove(ctx, restr->eapptag); |
| 1197 | lydict_remove(ctx, restr->emsg); |
| 1198 | } |
| 1199 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1200 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1201 | lys_type_dup(struct lys_module *mod, struct lys_node *parent, struct lys_type *new, struct lys_type *old, |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1202 | struct unres_schema *unres) |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1203 | { |
| 1204 | int i; |
| 1205 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1206 | new->module_name = lydict_insert(mod->ctx, old->module_name, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1207 | new->base = old->base; |
| 1208 | new->der = old->der; |
| 1209 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1210 | i = unres_schema_find(unres, old, UNRES_TYPE_DER); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1211 | if (i != -1) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1212 | /* HACK (serious one) for unres */ |
| 1213 | /* nothing else we can do but duplicate it immediately */ |
| 1214 | new->der = (struct lys_tpdf *)lyxml_dup_elem(mod->ctx, (struct lyxml_elem *)old->der, NULL, 1); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1215 | new->parent = (struct lys_tpdf *)parent; |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1216 | /* all these unres additions can fail even though they did not before */ |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1217 | if (unres_schema_add_node(mod, unres, new, UNRES_TYPE_DER, parent, 0)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1218 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1219 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1220 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1221 | } |
| 1222 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1223 | switch (new->base) { |
| 1224 | case LY_TYPE_BINARY: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1225 | if (old->info.binary.length) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1226 | new->info.binary.length = lys_restr_dup(mod->ctx, old->info.binary.length, 1); |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1227 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1228 | break; |
| 1229 | |
| 1230 | case LY_TYPE_BITS: |
| 1231 | new->info.bits.count = old->info.bits.count; |
| 1232 | if (new->info.bits.count) { |
| 1233 | new->info.bits.bit = calloc(new->info.bits.count, sizeof *new->info.bits.bit); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1234 | if (!new->info.bits.bit) { |
| 1235 | LOGMEM; |
| 1236 | return -1; |
| 1237 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1238 | for (i = 0; i < new->info.bits.count; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1239 | new->info.bits.bit[i].name = lydict_insert(mod->ctx, old->info.bits.bit[i].name, 0); |
| 1240 | new->info.bits.bit[i].dsc = lydict_insert(mod->ctx, old->info.bits.bit[i].dsc, 0); |
| 1241 | new->info.bits.bit[i].ref = lydict_insert(mod->ctx, old->info.bits.bit[i].ref, 0); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1242 | new->info.bits.bit[i].status = old->info.bits.bit[i].status; |
| 1243 | new->info.bits.bit[i].pos = old->info.bits.bit[i].pos; |
| 1244 | } |
| 1245 | } |
| 1246 | break; |
| 1247 | |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1248 | case LY_TYPE_DEC64: |
| 1249 | new->info.dec64.dig = old->info.dec64.dig; |
| 1250 | if (old->info.dec64.range) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1251 | new->info.dec64.range = lys_restr_dup(mod->ctx, old->info.dec64.range, 1); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1252 | } |
| 1253 | break; |
| 1254 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1255 | case LY_TYPE_ENUM: |
| 1256 | new->info.enums.count = old->info.enums.count; |
| 1257 | if (new->info.enums.count) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1258 | new->info.enums.enm = calloc(new->info.enums.count, sizeof *new->info.enums.enm); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1259 | if (!new->info.enums.enm) { |
| 1260 | LOGMEM; |
| 1261 | return -1; |
| 1262 | } |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1263 | for (i = 0; i < new->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1264 | new->info.enums.enm[i].name = lydict_insert(mod->ctx, old->info.enums.enm[i].name, 0); |
| 1265 | new->info.enums.enm[i].dsc = lydict_insert(mod->ctx, old->info.enums.enm[i].dsc, 0); |
| 1266 | new->info.enums.enm[i].ref = lydict_insert(mod->ctx, old->info.enums.enm[i].ref, 0); |
| 1267 | new->info.enums.enm[i].status = old->info.enums.enm[i].status; |
| 1268 | new->info.enums.enm[i].value = old->info.enums.enm[i].value; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | break; |
| 1272 | |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1273 | case LY_TYPE_IDENT: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1274 | if (old->info.ident.ref) { |
| 1275 | new->info.ident.ref = old->info.ident.ref; |
| 1276 | } else { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 1277 | i = unres_schema_find(unres, old, UNRES_TYPE_IDENTREF); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1278 | assert(i != -1); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1279 | if (unres_schema_add_str(mod, unres, new, UNRES_TYPE_IDENTREF, unres->str_snode[i], 0)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1280 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1281 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1282 | } |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1283 | break; |
| 1284 | |
Radek Krejci | af35142 | 2015-06-19 14:49:38 +0200 | [diff] [blame] | 1285 | case LY_TYPE_INST: |
| 1286 | new->info.inst.req = old->info.inst.req; |
| 1287 | break; |
| 1288 | |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 1289 | case LY_TYPE_INT8: |
| 1290 | case LY_TYPE_INT16: |
| 1291 | case LY_TYPE_INT32: |
| 1292 | case LY_TYPE_INT64: |
| 1293 | case LY_TYPE_UINT8: |
| 1294 | case LY_TYPE_UINT16: |
| 1295 | case LY_TYPE_UINT32: |
| 1296 | case LY_TYPE_UINT64: |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1297 | if (old->info.num.range) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1298 | new->info.num.range = lys_restr_dup(mod->ctx, old->info.num.range, 1); |
Radek Krejci | 425adf0 | 2015-06-26 16:23:28 +0200 | [diff] [blame] | 1299 | } |
Radek Krejci | f286013 | 2015-06-20 12:37:20 +0200 | [diff] [blame] | 1300 | break; |
| 1301 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1302 | case LY_TYPE_LEAFREF: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1303 | new->info.lref.path = lydict_insert(mod->ctx, old->info.lref.path, 0); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 1304 | if (unres_schema_add_node(mod, unres, new, UNRES_TYPE_LEAFREF, parent, 0)) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1305 | return -1; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 1306 | } |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1307 | break; |
| 1308 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1309 | case LY_TYPE_STRING: |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1310 | if (old->info.str.length) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1311 | new->info.str.length = lys_restr_dup(mod->ctx, old->info.str.length, 1); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1312 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1313 | new->info.str.patterns = lys_restr_dup(mod->ctx, old->info.str.patterns, old->info.str.pat_count); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1314 | break; |
| 1315 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1316 | case LY_TYPE_UNION: |
| 1317 | new->info.uni.count = old->info.uni.count; |
| 1318 | if (new->info.uni.count) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1319 | new->info.uni.types = calloc(new->info.uni.count, sizeof *new->info.uni.types); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1320 | if (!new->info.uni.types) { |
| 1321 | LOGMEM; |
| 1322 | return -1; |
| 1323 | } |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1324 | for (i = 0; i < new->info.uni.count; i++) { |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1325 | if (lys_type_dup(mod, parent, &(new->info.uni.types[i]), &(old->info.uni.types[i]), unres)) { |
| 1326 | return -1; |
| 1327 | } |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | break; |
| 1331 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1332 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1333 | /* nothing to do for LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1334 | break; |
| 1335 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1336 | |
| 1337 | return EXIT_SUCCESS; |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1341 | lys_type_free(struct ly_ctx *ctx, struct lys_type *type) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1342 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1343 | int i; |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1344 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1345 | assert(ctx); |
| 1346 | if (!type) { |
| 1347 | return; |
| 1348 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1349 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1350 | lydict_remove(ctx, type->module_name); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1351 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1352 | switch (type->base) { |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1353 | case LY_TYPE_BINARY: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1354 | lys_restr_free(ctx, type->info.binary.length); |
Radek Krejci | 0bd5db4 | 2015-06-19 13:30:07 +0200 | [diff] [blame] | 1355 | free(type->info.binary.length); |
| 1356 | break; |
Radek Krejci | 994b6f6 | 2015-06-18 16:47:27 +0200 | [diff] [blame] | 1357 | case LY_TYPE_BITS: |
| 1358 | for (i = 0; i < type->info.bits.count; i++) { |
| 1359 | lydict_remove(ctx, type->info.bits.bit[i].name); |
| 1360 | lydict_remove(ctx, type->info.bits.bit[i].dsc); |
| 1361 | lydict_remove(ctx, type->info.bits.bit[i].ref); |
| 1362 | } |
| 1363 | free(type->info.bits.bit); |
| 1364 | break; |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1365 | |
| 1366 | case LY_TYPE_DEC64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1367 | lys_restr_free(ctx, type->info.dec64.range); |
Radek Krejci | f9401c3 | 2015-06-26 16:47:36 +0200 | [diff] [blame] | 1368 | free(type->info.dec64.range); |
| 1369 | break; |
| 1370 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1371 | case LY_TYPE_ENUM: |
| 1372 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1373 | lydict_remove(ctx, type->info.enums.enm[i].name); |
| 1374 | lydict_remove(ctx, type->info.enums.enm[i].dsc); |
| 1375 | lydict_remove(ctx, type->info.enums.enm[i].ref); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1376 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1377 | free(type->info.enums.enm); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1378 | break; |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1379 | |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1380 | case LY_TYPE_INT8: |
| 1381 | case LY_TYPE_INT16: |
| 1382 | case LY_TYPE_INT32: |
| 1383 | case LY_TYPE_INT64: |
| 1384 | case LY_TYPE_UINT8: |
| 1385 | case LY_TYPE_UINT16: |
| 1386 | case LY_TYPE_UINT32: |
| 1387 | case LY_TYPE_UINT64: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1388 | lys_restr_free(ctx, type->info.num.range); |
Radek Krejci | 6fcb9dd | 2015-06-22 10:16:37 +0200 | [diff] [blame] | 1389 | free(type->info.num.range); |
| 1390 | break; |
| 1391 | |
Radek Krejci | dc4c141 | 2015-06-19 15:39:54 +0200 | [diff] [blame] | 1392 | case LY_TYPE_LEAFREF: |
| 1393 | lydict_remove(ctx, type->info.lref.path); |
| 1394 | break; |
| 1395 | |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1396 | case LY_TYPE_STRING: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1397 | lys_restr_free(ctx, type->info.str.length); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1398 | free(type->info.str.length); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1399 | for (i = 0; i < type->info.str.pat_count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1400 | lys_restr_free(ctx, &type->info.str.patterns[i]); |
Radek Krejci | 5fbc916 | 2015-06-19 14:11:11 +0200 | [diff] [blame] | 1401 | } |
| 1402 | free(type->info.str.patterns); |
Radek Krejci | 3733a80 | 2015-06-19 13:43:21 +0200 | [diff] [blame] | 1403 | break; |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1404 | |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1405 | case LY_TYPE_UNION: |
| 1406 | for (i = 0; i < type->info.uni.count; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1407 | lys_type_free(ctx, &type->info.uni.types[i]); |
Radek Krejci | e4c366b | 2015-07-02 10:11:31 +0200 | [diff] [blame] | 1408 | } |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1409 | free(type->info.uni.types); |
Radek Krejci | 4a7b5ee | 2015-06-19 14:56:43 +0200 | [diff] [blame] | 1410 | break; |
| 1411 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1412 | default: |
Radek Krejci | c7c8553 | 2015-07-02 10:16:54 +0200 | [diff] [blame] | 1413 | /* nothing to do for LY_TYPE_IDENT, LY_TYPE_INST, LY_TYPE_BOOL, LY_TYPE_EMPTY */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1418 | static void |
| 1419 | lys_tpdf_free(struct ly_ctx *ctx, struct lys_tpdf *tpdf) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1420 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1421 | assert(ctx); |
| 1422 | if (!tpdf) { |
| 1423 | return; |
| 1424 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1425 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1426 | lydict_remove(ctx, tpdf->name); |
| 1427 | lydict_remove(ctx, tpdf->dsc); |
| 1428 | lydict_remove(ctx, tpdf->ref); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1429 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1430 | lys_type_free(ctx, &tpdf->type); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1431 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1432 | lydict_remove(ctx, tpdf->units); |
| 1433 | lydict_remove(ctx, tpdf->dflt); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1436 | static struct lys_tpdf * |
| 1437 | lys_tpdf_dup(struct lys_module *mod, struct lys_node *parent, struct lys_tpdf *old, int size, struct unres_schema *unres) |
| 1438 | { |
| 1439 | struct lys_tpdf *result; |
| 1440 | int i, j; |
| 1441 | |
| 1442 | if (!size) { |
| 1443 | return NULL; |
| 1444 | } |
| 1445 | |
| 1446 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1447 | if (!result) { |
| 1448 | LOGMEM; |
| 1449 | return NULL; |
| 1450 | } |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 1451 | for (i = 0; i < size; i++) { |
| 1452 | result[i].name = lydict_insert(mod->ctx, old[i].name, 0); |
| 1453 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1454 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
| 1455 | result[i].flags = old[i].flags; |
| 1456 | result[i].module = old[i].module; |
| 1457 | |
| 1458 | if (lys_type_dup(mod, parent, &(result[i].type), &(old[i].type), unres)) { |
| 1459 | for (j = 0; j <= i; ++j) { |
| 1460 | lys_tpdf_free(mod->ctx, &result[j]); |
| 1461 | } |
| 1462 | free(result); |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | |
| 1466 | result[i].dflt = lydict_insert(mod->ctx, old[i].dflt, 0); |
| 1467 | result[i].units = lydict_insert(mod->ctx, old[i].units, 0); |
| 1468 | } |
| 1469 | |
| 1470 | return result; |
| 1471 | } |
| 1472 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1473 | static struct lys_when * |
| 1474 | lys_when_dup(struct ly_ctx *ctx, struct lys_when *old) |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1475 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1476 | struct lys_when *new; |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1477 | |
| 1478 | if (!old) { |
| 1479 | return NULL; |
| 1480 | } |
| 1481 | |
| 1482 | new = calloc(1, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1483 | if (!new) { |
| 1484 | LOGMEM; |
| 1485 | return NULL; |
| 1486 | } |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 1487 | new->cond = lydict_insert(ctx, old->cond, 0); |
| 1488 | new->dsc = lydict_insert(ctx, old->dsc, 0); |
| 1489 | new->ref = lydict_insert(ctx, old->ref, 0); |
| 1490 | |
| 1491 | return new; |
| 1492 | } |
| 1493 | |
Michal Vasko | 0308dd6 | 2015-10-07 09:14:40 +0200 | [diff] [blame] | 1494 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1495 | lys_when_free(struct ly_ctx *ctx, struct lys_when *w) |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1496 | { |
| 1497 | if (!w) { |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | lydict_remove(ctx, w->cond); |
| 1502 | lydict_remove(ctx, w->dsc); |
| 1503 | lydict_remove(ctx, w->ref); |
| 1504 | |
| 1505 | free(w); |
| 1506 | } |
| 1507 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1508 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1509 | 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] | 1510 | { |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1511 | struct lys_node *next, *sub; |
| 1512 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1513 | /* children from a resolved augment are freed under the target node */ |
| 1514 | if (!aug.target) { |
| 1515 | LY_TREE_FOR_SAFE(aug.child, next, sub) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1516 | lys_node_free(sub, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1517 | } |
Michal Vasko | c4c2e21 | 2015-09-23 11:34:41 +0200 | [diff] [blame] | 1518 | } |
| 1519 | |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1520 | lydict_remove(ctx, aug.target_name); |
| 1521 | lydict_remove(ctx, aug.dsc); |
| 1522 | lydict_remove(ctx, aug.ref); |
| 1523 | |
| 1524 | free(aug.features); |
| 1525 | |
| 1526 | lys_when_free(ctx, aug.when); |
| 1527 | |
Michal Vasko | 7d356a5 | 2015-08-19 15:06:31 +0200 | [diff] [blame] | 1528 | /* Do not free the children, they were appended somewhere and their |
| 1529 | * new parent will take care of them. |
| 1530 | */ |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1531 | } |
| 1532 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1533 | static struct lys_node_augment * |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1534 | 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] | 1535 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1536 | struct lys_node_augment *new = NULL; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1537 | struct lys_node *old_child, *new_child; |
| 1538 | int i; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1539 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1540 | if (!size) { |
| 1541 | return NULL; |
| 1542 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1543 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1544 | new = calloc(size, sizeof *new); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1545 | if (!new) { |
| 1546 | LOGMEM; |
| 1547 | return NULL; |
| 1548 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1549 | for (i = 0; i < size; i++) { |
| 1550 | new[i].target_name = lydict_insert(module->ctx, old[i].target_name, 0); |
| 1551 | new[i].dsc = lydict_insert(module->ctx, old[i].dsc, 0); |
| 1552 | new[i].ref = lydict_insert(module->ctx, old[i].ref, 0); |
| 1553 | new[i].flags = old[i].flags; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1554 | new[i].module = old[i].module; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1555 | new[i].nodetype = old[i].nodetype; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1556 | /* this must succeed, it was already resolved once */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1557 | if (resolve_augment_schema_nodeid(new[i].target_name, parent->child, NULL, |
| 1558 | (const struct lys_node **)&new[i].target)) { |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1559 | LOGINT; |
| 1560 | free(new); |
| 1561 | return NULL; |
| 1562 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1563 | new[i].parent = parent; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1564 | |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1565 | /* Correct the augment nodes. |
| 1566 | * This function can only be called from lys_node_dup() with uses |
| 1567 | * being the node duplicated, so we must have a case of grouping |
| 1568 | * with a uses with augments. The augmented nodes have already been |
| 1569 | * copied and everything is almost fine except their parent is wrong |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1570 | * (it was set to their actual data parent, not an augment), and |
| 1571 | * the new augment does not have child pointer to its augment nodes, |
| 1572 | * so we just correct it. |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1573 | */ |
| 1574 | LY_TREE_FOR(new[i].target->child, new_child) { |
| 1575 | if (new_child->name == old[i].child->name) { |
| 1576 | break; |
Radek Krejci | b7f5e41 | 2015-08-13 10:15:51 +0200 | [diff] [blame] | 1577 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1578 | } |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1579 | assert(new_child); |
Michal Vasko | a5900c5 | 2015-09-24 13:17:11 +0200 | [diff] [blame] | 1580 | new[i].child = new_child; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 1581 | LY_TREE_FOR(old[i].child, old_child) { |
| 1582 | /* all augment nodes were connected as siblings, there can be no more after this */ |
| 1583 | if (old_child->parent != (struct lys_node *)&old[i]) { |
| 1584 | break; |
| 1585 | } |
| 1586 | |
| 1587 | assert(old_child->name == new_child->name); |
| 1588 | |
| 1589 | new_child->parent = (struct lys_node *)&new[i]; |
| 1590 | new_child = new_child->next; |
| 1591 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1592 | } |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1593 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1594 | return new; |
Radek Krejci | 106efc0 | 2015-06-10 14:36:27 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1597 | static struct lys_refine * |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1598 | 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] | 1599 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1600 | struct lys_refine *result; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 1601 | int i; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1602 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1603 | if (!size) { |
| 1604 | return NULL; |
| 1605 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1606 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1607 | result = calloc(size, sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1608 | if (!result) { |
| 1609 | LOGMEM; |
| 1610 | return NULL; |
| 1611 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1612 | for (i = 0; i < size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1613 | 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] | 1614 | result[i].dsc = lydict_insert(mod->ctx, old[i].dsc, 0); |
| 1615 | result[i].ref = lydict_insert(mod->ctx, old[i].ref, 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1616 | result[i].flags = old[i].flags; |
| 1617 | result[i].target_type = old[i].target_type; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1618 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1619 | result[i].must_size = old[i].must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1620 | 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] | 1621 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1622 | if (result[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1623 | result[i].mod.dflt = lydict_insert(mod->ctx, old[i].mod.dflt, 0); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1624 | } else if (result[i].target_type == LYS_CONTAINER) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1625 | 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] | 1626 | } else if (result[i].target_type & (LYS_LIST | LYS_LEAFLIST)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1627 | result[i].mod.list = old[i].mod.list; |
| 1628 | } |
| 1629 | } |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1630 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1631 | return result; |
Michal Vasko | 1982cad | 2015-06-08 15:49:30 +0200 | [diff] [blame] | 1632 | } |
| 1633 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1634 | static void |
| 1635 | lys_ident_free(struct ly_ctx *ctx, struct lys_ident *ident) |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1636 | { |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 1637 | struct lys_ident_der *der; |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1638 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1639 | assert(ctx); |
| 1640 | if (!ident) { |
| 1641 | return; |
| 1642 | } |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1643 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1644 | /* |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1645 | * if caller free only a single data model which is used (its identity is |
| 1646 | * reference from identity in another module), this silly freeing can lead |
| 1647 | * to segmentation fault. But without noting if the module is used by some |
| 1648 | * other, it cannot be solved. |
Radek Krejci | a339069 | 2015-06-16 14:13:31 +0200 | [diff] [blame] | 1649 | * |
| 1650 | * Possible solution is to not allow caller to remove particular schema |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1651 | * from the context. This is the current approach. |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1652 | */ |
| 1653 | while (ident->der) { |
| 1654 | der = ident->der; |
| 1655 | ident->der = der->next; |
| 1656 | free(der); |
| 1657 | } |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1658 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1659 | lydict_remove(ctx, ident->name); |
| 1660 | lydict_remove(ctx, ident->dsc); |
| 1661 | lydict_remove(ctx, ident->ref); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 1662 | |
| 1663 | } |
| 1664 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1665 | static void |
| 1666 | lys_grp_free(struct ly_ctx *ctx, struct lys_node_grp *grp) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1667 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1668 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1669 | |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1670 | /* handle only specific parts for LYS_GROUPING */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1671 | for (i = 0; i < grp->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1672 | lys_tpdf_free(ctx, &grp->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1673 | } |
| 1674 | free(grp->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1677 | static void |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1678 | lys_rpc_inout_free(struct ly_ctx *ctx, struct lys_node_rpc_inout *io) |
| 1679 | { |
| 1680 | int i; |
| 1681 | |
| 1682 | /* handle only specific parts for LYS_INPUT and LYS_OUTPUT */ |
| 1683 | for (i = 0; i < io->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1684 | lys_tpdf_free(ctx, &io->tpdf[i]); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1685 | } |
| 1686 | free(io->tpdf); |
| 1687 | } |
| 1688 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1689 | static void |
| 1690 | lys_anyxml_free(struct ly_ctx *ctx, struct lys_node_anyxml *anyxml) |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1691 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1692 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1693 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1694 | for (i = 0; i < anyxml->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1695 | lys_restr_free(ctx, &anyxml->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1696 | } |
| 1697 | free(anyxml->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1698 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1699 | lys_when_free(ctx, anyxml->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1702 | static void |
| 1703 | lys_leaf_free(struct ly_ctx *ctx, struct lys_node_leaf *leaf) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1704 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1705 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1706 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1707 | if (leaf->child) { |
| 1708 | /* leafref backlinks */ |
| 1709 | ly_set_free((struct ly_set *)leaf->child); |
| 1710 | } |
| 1711 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1712 | for (i = 0; i < leaf->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1713 | lys_restr_free(ctx, &leaf->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1714 | } |
| 1715 | free(leaf->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1716 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1717 | lys_when_free(ctx, leaf->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1718 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1719 | lys_type_free(ctx, &leaf->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1720 | lydict_remove(ctx, leaf->units); |
| 1721 | lydict_remove(ctx, leaf->dflt); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1724 | static void |
| 1725 | lys_leaflist_free(struct ly_ctx *ctx, struct lys_node_leaflist *llist) |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1726 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1727 | int i; |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1728 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1729 | if (llist->child) { |
| 1730 | /* leafref backlinks */ |
| 1731 | ly_set_free((struct ly_set *)llist->child); |
| 1732 | } |
| 1733 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1734 | for (i = 0; i < llist->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1735 | lys_restr_free(ctx, &llist->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1736 | } |
| 1737 | free(llist->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1738 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1739 | lys_when_free(ctx, llist->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1740 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1741 | lys_type_free(ctx, &llist->type); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1742 | lydict_remove(ctx, llist->units); |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1745 | static void |
| 1746 | lys_list_free(struct ly_ctx *ctx, struct lys_node_list *list) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1747 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1748 | int i, j; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1749 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1750 | /* handle only specific parts for LY_NODE_LIST */ |
| 1751 | for (i = 0; i < list->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1752 | lys_tpdf_free(ctx, &list->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1753 | } |
| 1754 | free(list->tpdf); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1755 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1756 | for (i = 0; i < list->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1757 | lys_restr_free(ctx, &list->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1758 | } |
| 1759 | free(list->must); |
Radek Krejci | 537cf38 | 2015-06-04 11:07:01 +0200 | [diff] [blame] | 1760 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1761 | lys_when_free(ctx, list->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1762 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1763 | for (i = 0; i < list->unique_size; i++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1764 | for (j = 0; j > list->unique[i].expr_size; j++) { |
| 1765 | lydict_remove(ctx, list->unique[i].expr[j]); |
| 1766 | } |
| 1767 | free(list->unique[i].expr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1768 | } |
| 1769 | free(list->unique); |
Radek Krejci | d7f0d01 | 2015-05-25 15:04:52 +0200 | [diff] [blame] | 1770 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1771 | free(list->keys); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1774 | static void |
| 1775 | lys_container_free(struct ly_ctx *ctx, struct lys_node_container *cont) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1776 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1777 | int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1778 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1779 | /* handle only specific parts for LY_NODE_CONTAINER */ |
| 1780 | lydict_remove(ctx, cont->presence); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1781 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1782 | for (i = 0; i < cont->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1783 | lys_tpdf_free(ctx, &cont->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1784 | } |
| 1785 | free(cont->tpdf); |
Radek Krejci | 800af70 | 2015-06-02 13:46:01 +0200 | [diff] [blame] | 1786 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1787 | for (i = 0; i < cont->must_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1788 | lys_restr_free(ctx, &cont->must[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1789 | } |
| 1790 | free(cont->must); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1791 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1792 | lys_when_free(ctx, cont->when); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1793 | } |
| 1794 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1795 | static void |
| 1796 | lys_feature_free(struct ly_ctx *ctx, struct lys_feature *f) |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 1797 | { |
| 1798 | lydict_remove(ctx, f->name); |
| 1799 | lydict_remove(ctx, f->dsc); |
| 1800 | lydict_remove(ctx, f->ref); |
| 1801 | free(f->features); |
| 1802 | } |
| 1803 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1804 | static void |
| 1805 | lys_deviation_free(struct ly_ctx *ctx, struct lys_deviation *dev) |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1806 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1807 | int i, j, k; |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1808 | |
| 1809 | lydict_remove(ctx, dev->target_name); |
| 1810 | lydict_remove(ctx, dev->dsc); |
| 1811 | lydict_remove(ctx, dev->ref); |
| 1812 | |
| 1813 | for (i = 0; i < dev->deviate_size; i++) { |
| 1814 | lydict_remove(ctx, dev->deviate[i].dflt); |
| 1815 | lydict_remove(ctx, dev->deviate[i].units); |
| 1816 | |
| 1817 | if (dev->deviate[i].mod == LY_DEVIATE_DEL) { |
| 1818 | for (j = 0; j < dev->deviate[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1819 | lys_restr_free(ctx, &dev->deviate[i].must[j]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1820 | } |
| 1821 | free(dev->deviate[i].must); |
| 1822 | |
| 1823 | for (j = 0; j < dev->deviate[i].unique_size; j++) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 1824 | for (k = 0; k < dev->deviate[i].unique[j].expr_size; k++) { |
| 1825 | lydict_remove(ctx, dev->deviate[i].unique[j].expr[k]); |
| 1826 | } |
| 1827 | free(dev->deviate[j].unique[j].expr); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 1828 | } |
| 1829 | free(dev->deviate[i].unique); |
| 1830 | } |
| 1831 | } |
| 1832 | free(dev->deviate); |
| 1833 | } |
| 1834 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1835 | static void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1836 | 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] | 1837 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1838 | int i, j; |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1839 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1840 | for (i = 0; i < uses->refine_size; i++) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1841 | lydict_remove(ctx, uses->refine[i].target_name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1842 | lydict_remove(ctx, uses->refine[i].dsc); |
| 1843 | lydict_remove(ctx, uses->refine[i].ref); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1844 | |
Michal Vasko | a275c0a | 2015-09-24 09:55:42 +0200 | [diff] [blame] | 1845 | for (j = 0; j < uses->refine[i].must_size; j++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1846 | lys_restr_free(ctx, &uses->refine[i].must[j]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1847 | } |
| 1848 | free(uses->refine[i].must); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1849 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1850 | if (uses->refine[i].target_type & (LYS_LEAF | LYS_CHOICE)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1851 | lydict_remove(ctx, uses->refine[i].mod.dflt); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1852 | } else if (uses->refine[i].target_type & LYS_CONTAINER) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1853 | lydict_remove(ctx, uses->refine[i].mod.presence); |
| 1854 | } |
| 1855 | } |
| 1856 | free(uses->refine); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1857 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1858 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1859 | lys_augment_free(ctx, uses->augment[i], private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1860 | } |
| 1861 | free(uses->augment); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1862 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1863 | lys_when_free(ctx, uses->when); |
Radek Krejci | e1fa858 | 2015-06-08 09:46:45 +0200 | [diff] [blame] | 1864 | } |
| 1865 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1866 | void |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1867 | lys_node_free(struct lys_node *node, void (*private_destructor)(const struct lys_node *node, void *priv)) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1868 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1869 | struct ly_ctx *ctx; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1870 | struct lys_node *sub, *next; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1871 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1872 | if (!node) { |
| 1873 | return; |
| 1874 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1875 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1876 | assert(node->module); |
| 1877 | assert(node->module->ctx); |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 1878 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1879 | ctx = node->module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1880 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1881 | /* remove private object */ |
| 1882 | if (node->private && private_destructor) { |
| 1883 | private_destructor(node, node->private); |
| 1884 | } |
| 1885 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1886 | /* common part */ |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1887 | if (!(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) { |
| 1888 | free(node->features); |
| 1889 | lydict_remove(ctx, node->name); |
| 1890 | lydict_remove(ctx, node->dsc); |
| 1891 | lydict_remove(ctx, node->ref); |
| 1892 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1893 | |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1894 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 1895 | LY_TREE_FOR_SAFE(node->child, next, sub) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1896 | lys_node_free(sub, private_destructor); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1900 | /* specific part */ |
| 1901 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1902 | case LYS_CONTAINER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1903 | lys_container_free(ctx, (struct lys_node_container *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1904 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1905 | case LYS_CHOICE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1906 | lys_when_free(ctx, ((struct lys_node_choice *)node)->when); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1907 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1908 | case LYS_LEAF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1909 | lys_leaf_free(ctx, (struct lys_node_leaf *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1910 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1911 | case LYS_LEAFLIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1912 | lys_leaflist_free(ctx, (struct lys_node_leaflist *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1913 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1914 | case LYS_LIST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1915 | lys_list_free(ctx, (struct lys_node_list *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1916 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1917 | case LYS_ANYXML: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1918 | lys_anyxml_free(ctx, (struct lys_node_anyxml *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1919 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1920 | case LYS_USES: |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 1921 | lys_uses_free(ctx, (struct lys_node_uses *)node, private_destructor); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1922 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1923 | case LYS_CASE: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1924 | lys_when_free(ctx, ((struct lys_node_case *)node)->when); |
Radek Krejci | b0af6ba | 2015-06-18 15:01:03 +0200 | [diff] [blame] | 1925 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1926 | case LYS_AUGMENT: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1927 | /* do nothing */ |
| 1928 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1929 | case LYS_GROUPING: |
| 1930 | case LYS_RPC: |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1931 | case LYS_NOTIF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1932 | lys_grp_free(ctx, (struct lys_node_grp *)node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1933 | break; |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 1934 | |
| 1935 | case LYS_INPUT: |
| 1936 | case LYS_OUTPUT: |
| 1937 | lys_rpc_inout_free(ctx, (struct lys_node_rpc_inout *)node); |
| 1938 | break; |
Michal Vasko | 591e0b2 | 2015-08-13 13:53:43 +0200 | [diff] [blame] | 1939 | case LYS_UNKNOWN: |
| 1940 | LOGINT; |
| 1941 | break; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1942 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 1943 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1944 | /* again common part */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1945 | lys_node_unlink(node); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1946 | free(node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1947 | } |
| 1948 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 1949 | const struct lys_module * |
| 1950 | 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] | 1951 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1952 | const struct lys_module *main_module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1953 | int i; |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1954 | |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame^] | 1955 | assert(!prefix || !name); |
| 1956 | |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1957 | if (prefix && !pref_len) { |
| 1958 | pref_len = strlen(prefix); |
| 1959 | } |
| 1960 | if (name && !name_len) { |
| 1961 | name_len = strlen(name); |
| 1962 | } |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1963 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1964 | main_module = (module->type ? ((struct lys_submodule *)module)->belongsto : module); |
Michal Vasko | a7789a8 | 2016-02-11 15:42:55 +0100 | [diff] [blame^] | 1965 | |
| 1966 | /* module own prefix, submodule own prefix, (sub)module own name */ |
| 1967 | if ((!prefix || (!module->type && !strncmp(main_module->prefix, prefix, pref_len) && !main_module->prefix[pref_len]) |
| 1968 | || (module->type && !strncmp(module->prefix, prefix, pref_len) && !module->prefix[pref_len])) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1969 | && (!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] | 1970 | return main_module; |
Michal Vasko | d8da86b | 2015-10-21 13:35:37 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Michal Vasko | 8ce24d7 | 2015-10-21 11:27:26 +0200 | [diff] [blame] | 1973 | for (i = 0; i < module->imp_size; ++i) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1974 | if ((!prefix || (!strncmp(module->imp[i].prefix, prefix, pref_len) && !module->imp[i].prefix[pref_len])) |
| 1975 | && (!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] | 1976 | return module->imp[i].module; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | return NULL; |
| 1981 | } |
| 1982 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 1983 | /* free_int_mods - flag whether to free the internal modules as well */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1984 | static void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 1985 | 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] | 1986 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1987 | struct ly_ctx *ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1988 | struct lys_node *next, *iter; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1989 | unsigned int i; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1990 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1991 | assert(module->ctx); |
| 1992 | ctx = module->ctx; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 1993 | |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 1994 | /* just free the import array, imported modules will stay in the context */ |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 1995 | free(module->imp); |
| 1996 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 1997 | /* submodules don't have data tree, the data nodes |
| 1998 | * are placed in the main module altogether */ |
| 1999 | if (!module->type) { |
| 2000 | LY_TREE_FOR_SAFE(module->data, next, iter) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2001 | lys_node_free(iter, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2002 | } |
Radek Krejci | 2118196 | 2015-06-30 14:11:00 +0200 | [diff] [blame] | 2003 | } |
Radek Krejci | 5a06554 | 2015-05-22 15:02:07 +0200 | [diff] [blame] | 2004 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2005 | lydict_remove(ctx, module->dsc); |
| 2006 | lydict_remove(ctx, module->ref); |
| 2007 | lydict_remove(ctx, module->org); |
| 2008 | lydict_remove(ctx, module->contact); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2009 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2010 | /* revisions */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2011 | for (i = 0; i < module->rev_size; i++) { |
| 2012 | lydict_remove(ctx, module->rev[i].dsc); |
| 2013 | lydict_remove(ctx, module->rev[i].ref); |
| 2014 | } |
| 2015 | free(module->rev); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2016 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2017 | /* identities */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2018 | for (i = 0; i < module->ident_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2019 | lys_ident_free(ctx, &module->ident[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2020 | } |
| 2021 | module->ident_size = 0; |
| 2022 | free(module->ident); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2023 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2024 | /* typedefs */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2025 | for (i = 0; i < module->tpdf_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2026 | lys_tpdf_free(ctx, &module->tpdf[i]); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2027 | } |
| 2028 | free(module->tpdf); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2029 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2030 | /* include */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2031 | for (i = 0; i < module->inc_size; i++) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2032 | /* complete submodule free is done only from main module since |
| 2033 | * submodules propagate their includes to the main module */ |
| 2034 | if (!module->type) { |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2035 | lys_submodule_free(module->inc[i].submodule, private_destructor); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2036 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2037 | } |
| 2038 | free(module->inc); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2039 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2040 | /* augment */ |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2041 | for (i = 0; i < module->augment_size; i++) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2042 | lys_augment_free(ctx, module->augment[i], private_destructor); |
Radek Krejci | f5be10f | 2015-06-16 13:29:36 +0200 | [diff] [blame] | 2043 | } |
| 2044 | free(module->augment); |
| 2045 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2046 | /* features */ |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2047 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2048 | lys_feature_free(ctx, &module->features[i]); |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2049 | } |
| 2050 | free(module->features); |
| 2051 | |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2052 | /* deviations */ |
| 2053 | for (i = 0; i < module->deviation_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2054 | lys_deviation_free(ctx, &module->deviation[i]); |
Radek Krejci | eb00f51 | 2015-07-01 16:44:58 +0200 | [diff] [blame] | 2055 | } |
| 2056 | free(module->deviation); |
| 2057 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2058 | lydict_remove(ctx, module->name); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2061 | void |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2062 | 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] | 2063 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2064 | if (!submodule) { |
| 2065 | return; |
| 2066 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2067 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2068 | /* common part with struct ly_module */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2069 | module_free_common((struct lys_module *)submodule, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2070 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2071 | /* no specific items to free */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2072 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2073 | free(submodule); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2076 | struct lys_node * |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2077 | lys_node_dup(struct lys_module *module, struct lys_node *parent, const struct lys_node *node, uint8_t flags, |
| 2078 | uint8_t nacm, struct unres_schema *unres) |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2079 | { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2080 | struct lys_node *retval = NULL, *child; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2081 | struct ly_ctx *ctx = module->ctx; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2082 | const char *modname; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2083 | int i, j, rc; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2084 | |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2085 | struct lys_node_container *cont = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2086 | struct lys_node_container *cont_orig = (struct lys_node_container *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2087 | struct lys_node_choice *choice = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2088 | struct lys_node_choice *choice_orig = (struct lys_node_choice *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2089 | struct lys_node_leaf *leaf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2090 | struct lys_node_leaf *leaf_orig = (struct lys_node_leaf *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2091 | struct lys_node_leaflist *llist = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2092 | struct lys_node_leaflist *llist_orig = (struct lys_node_leaflist *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2093 | struct lys_node_list *list = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2094 | struct lys_node_list *list_orig = (struct lys_node_list *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2095 | struct lys_node_anyxml *anyxml = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2096 | struct lys_node_anyxml *anyxml_orig = (struct lys_node_anyxml *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2097 | struct lys_node_uses *uses = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2098 | struct lys_node_uses *uses_orig = (struct lys_node_uses *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2099 | struct lys_node_grp *grp = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2100 | struct lys_node_grp *grp_orig = (struct lys_node_grp *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2101 | struct lys_node_rpc *rpc = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2102 | struct lys_node_rpc *rpc_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2103 | struct lys_node_rpc_inout *io = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2104 | struct lys_node_rpc_inout *io_orig = (struct lys_node_rpc_inout *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2105 | struct lys_node_rpc *ntf = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2106 | struct lys_node_rpc *ntf_orig = (struct lys_node_rpc *)node; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 2107 | struct lys_node_case *cs = NULL; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2108 | struct lys_node_case *cs_orig = (struct lys_node_case *)node; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2109 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2110 | /* we cannot just duplicate memory since the strings are stored in |
| 2111 | * dictionary and we need to update dictionary counters. |
| 2112 | */ |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2113 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2114 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2115 | case LYS_CONTAINER: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2116 | cont = calloc(1, sizeof *cont); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2117 | retval = (struct lys_node *)cont; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2118 | break; |
| 2119 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2120 | case LYS_CHOICE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2121 | choice = calloc(1, sizeof *choice); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2122 | retval = (struct lys_node *)choice; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2123 | break; |
| 2124 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2125 | case LYS_LEAF: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2126 | leaf = calloc(1, sizeof *leaf); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2127 | retval = (struct lys_node *)leaf; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2128 | break; |
| 2129 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2130 | case LYS_LEAFLIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2131 | llist = calloc(1, sizeof *llist); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2132 | retval = (struct lys_node *)llist; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2133 | break; |
| 2134 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2135 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2136 | list = calloc(1, sizeof *list); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2137 | retval = (struct lys_node *)list; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2138 | break; |
| 2139 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2140 | case LYS_ANYXML: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2141 | anyxml = calloc(1, sizeof *anyxml); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2142 | retval = (struct lys_node *)anyxml; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2143 | break; |
| 2144 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2145 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2146 | uses = calloc(1, sizeof *uses); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2147 | retval = (struct lys_node *)uses; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2148 | break; |
| 2149 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2150 | case LYS_CASE: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2151 | cs = calloc(1, sizeof *cs); |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2152 | retval = (struct lys_node *)cs; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2153 | break; |
| 2154 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2155 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2156 | grp = calloc(1, sizeof *grp); |
| 2157 | retval = (struct lys_node *)grp; |
| 2158 | break; |
| 2159 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2160 | case LYS_RPC: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2161 | rpc = calloc(1, sizeof *rpc); |
| 2162 | retval = (struct lys_node *)rpc; |
| 2163 | break; |
| 2164 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2165 | case LYS_INPUT: |
| 2166 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2167 | io = calloc(1, sizeof *io); |
| 2168 | retval = (struct lys_node *)io; |
| 2169 | break; |
| 2170 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2171 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2172 | ntf = calloc(1, sizeof *ntf); |
| 2173 | retval = (struct lys_node *)ntf; |
Michal Vasko | 38d01f7 | 2015-06-15 09:41:06 +0200 | [diff] [blame] | 2174 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2175 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2176 | default: |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2177 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2178 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2179 | } |
Radek Krejci | b388c15 | 2015-06-04 17:03:03 +0200 | [diff] [blame] | 2180 | |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2181 | if (!retval) { |
| 2182 | LOGMEM; |
| 2183 | return NULL; |
| 2184 | } |
| 2185 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2186 | /* |
| 2187 | * duplicate generic part of the structure |
| 2188 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2189 | retval->name = lydict_insert(ctx, node->name, 0); |
| 2190 | retval->dsc = lydict_insert(ctx, node->dsc, 0); |
| 2191 | retval->ref = lydict_insert(ctx, node->ref, 0); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2192 | retval->nacm = nacm; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2193 | retval->flags = node->flags; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2194 | if (!(retval->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2195 | /* set parent's config flag */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2196 | retval->flags |= flags & LYS_CONFIG_MASK; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2197 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2198 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2199 | retval->module = module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2200 | retval->nodetype = node->nodetype; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2201 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2202 | retval->prev = retval; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2203 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2204 | retval->features_size = node->features_size; |
Radek Krejci | 3cf9e22 | 2015-06-18 11:37:50 +0200 | [diff] [blame] | 2205 | retval->features = calloc(retval->features_size, sizeof *retval->features); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2206 | if (!retval->features) { |
| 2207 | LOGMEM; |
| 2208 | goto error; |
| 2209 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2210 | for (i = 0; i < node->features_size; ++i) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2211 | retval->features[i] = (struct lys_feature *)retval; |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2212 | if (unres_schema_dup(module, unres, &node->features[i], UNRES_IFFEAT, &retval->features[i])) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2213 | retval->features[i] = node->features[i]; |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2214 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2215 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2216 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2217 | /* connect it to the parent */ |
| 2218 | if (lys_node_addchild(parent, retval->module, retval)) { |
| 2219 | goto error; |
| 2220 | } |
| 2221 | |
| 2222 | /* go recursively */ |
| 2223 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2224 | LY_TREE_FOR(node->child, child) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2225 | if (!lys_node_dup(module, retval, child, retval->flags, retval->nacm, unres)) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2226 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2230 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * duplicate specific part of the structure |
| 2233 | */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2234 | switch (node->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2235 | case LYS_CONTAINER: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2236 | if (cont_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2237 | cont->when = lys_when_dup(ctx, cont_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2238 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2239 | cont->presence = lydict_insert(ctx, cont_orig->presence, 0); |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2240 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2241 | cont->must_size = cont_orig->must_size; |
| 2242 | cont->tpdf_size = cont_orig->tpdf_size; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2243 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2244 | cont->must = lys_restr_dup(ctx, cont_orig->must, cont->must_size); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2245 | cont->tpdf = lys_tpdf_dup(module, node->parent, cont_orig->tpdf, cont->tpdf_size, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2246 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2247 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2248 | case LYS_CHOICE: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2249 | if (choice_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2250 | choice->when = lys_when_dup(ctx, choice_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | if (choice_orig->dflt) { |
Radek Krejci | f70a3a6 | 2016-01-27 16:39:58 +0100 | [diff] [blame] | 2254 | modname = choice->module->type ? ((struct lys_submodule *)choice->module)->belongsto->name : choice->module->name; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2255 | rc = lys_get_sibling(choice->child, modname, 0, choice_orig->dflt->name, 0, LYS_ANYXML |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2256 | | LYS_CASE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2257 | | LYS_LIST, (const struct lys_node **)&choice->dflt); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2258 | if (rc) { |
| 2259 | if (rc == EXIT_FAILURE) { |
| 2260 | LOGINT; |
| 2261 | } |
| 2262 | goto error; |
| 2263 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2264 | } else { |
Michal Vasko | c68a2a2 | 2015-08-06 09:57:24 +0200 | [diff] [blame] | 2265 | /* useless to check return value, we don't know whether |
| 2266 | * there really wasn't any default defined or it just hasn't |
| 2267 | * been resolved, we just hope for the best :) |
| 2268 | */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2269 | unres_schema_dup(module, unres, choice_orig, UNRES_CHOICE_DFLT, choice); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2270 | } |
| 2271 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2272 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2273 | case LYS_LEAF: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2274 | if (lys_type_dup(module, node->parent, &(leaf->type), &(leaf_orig->type), unres)) { |
| 2275 | goto error; |
| 2276 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2277 | leaf->units = lydict_insert(module->ctx, leaf_orig->units, 0); |
| 2278 | |
| 2279 | if (leaf_orig->dflt) { |
| 2280 | leaf->dflt = lydict_insert(ctx, leaf_orig->dflt, 0); |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2281 | if (unres_schema_add_str(module, unres, &leaf->type, UNRES_TYPE_DFLT, leaf->dflt, 0) == -1) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2282 | goto error; |
| 2283 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2284 | } |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2285 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2286 | leaf->must_size = leaf_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2287 | leaf->must = lys_restr_dup(ctx, leaf_orig->must, leaf->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2288 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2289 | if (leaf_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2290 | leaf->when = lys_when_dup(ctx, leaf_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2291 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2292 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2293 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2294 | case LYS_LEAFLIST: |
Michal Vasko | b84f88a | 2015-09-24 13:16:10 +0200 | [diff] [blame] | 2295 | if (lys_type_dup(module, node->parent, &(llist->type), &(llist_orig->type), unres)) { |
| 2296 | goto error; |
| 2297 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2298 | llist->units = lydict_insert(module->ctx, llist_orig->units, 0); |
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 | llist->min = llist_orig->min; |
| 2301 | llist->max = llist_orig->max; |
| 2302 | |
| 2303 | llist->must_size = llist_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2304 | llist->must = lys_restr_dup(ctx, llist_orig->must, llist->must_size); |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2305 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2306 | if (llist_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2307 | llist->when = lys_when_dup(ctx, llist_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2308 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2309 | break; |
| 2310 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2311 | case LYS_LIST: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2312 | list->min = list_orig->min; |
| 2313 | list->max = list_orig->max; |
| 2314 | |
| 2315 | list->must_size = list_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2316 | list->must = lys_restr_dup(ctx, list_orig->must, list->must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2317 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2318 | list->tpdf_size = list_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2319 | list->tpdf = lys_tpdf_dup(module, node->parent, list_orig->tpdf, list->tpdf_size, unres); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2320 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2321 | list->keys_size = list_orig->keys_size; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2322 | if (list->keys_size) { |
| 2323 | list->keys = calloc(list->keys_size, sizeof *list->keys); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2324 | if (!list->keys) { |
| 2325 | LOGMEM; |
| 2326 | goto error; |
| 2327 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2328 | |
| 2329 | /* we managed to resolve it before, resolve it again manually */ |
| 2330 | if (list_orig->keys[0]) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2331 | modname = list->module->type ? ((struct lys_submodule *)list->module)->belongsto->name : list->module->name; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2332 | for (i = 0; i < list->keys_size; ++i) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2333 | rc = lys_get_sibling(list->child, modname, 0, list_orig->keys[i]->name, 0, LYS_LEAF, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2334 | (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2335 | if (rc) { |
| 2336 | if (rc == EXIT_FAILURE) { |
| 2337 | LOGINT; |
| 2338 | } |
| 2339 | goto error; |
| 2340 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2341 | } |
| 2342 | /* it was not resolved yet, add unres copy */ |
| 2343 | } else { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2344 | if (unres_schema_dup(module, unres, list_orig, UNRES_LIST_KEYS, list)) { |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2345 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2346 | goto error; |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2347 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2348 | } |
| 2349 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2350 | |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2351 | list->unique_size = list_orig->unique_size; |
| 2352 | list->unique = malloc(list->unique_size * sizeof *list->unique); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2353 | if (!list->unique) { |
| 2354 | LOGMEM; |
| 2355 | goto error; |
| 2356 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2357 | for (i = 0; i < list->unique_size; ++i) { |
| 2358 | list->unique[i].expr_size = list_orig->unique[i].expr_size; |
| 2359 | 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] | 2360 | if (!list->unique[i].expr) { |
| 2361 | LOGMEM; |
| 2362 | goto error; |
| 2363 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2364 | for (j = 0; j < list->unique[i].expr_size; j++) { |
| 2365 | list->unique[i].expr[j] = lydict_insert(ctx, list_orig->unique[i].expr[j], 0); |
| 2366 | |
| 2367 | /* if it stays in unres list, duplicate it also there */ |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2368 | unres_schema_dup(module, unres, &list_orig->unique[i], UNRES_LIST_UNIQ, &list->unique[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2369 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2370 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2371 | |
| 2372 | if (list_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2373 | list->when = lys_when_dup(ctx, list_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2374 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2375 | break; |
| 2376 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2377 | case LYS_ANYXML: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2378 | anyxml->must_size = anyxml_orig->must_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2379 | anyxml->must = lys_restr_dup(ctx, anyxml_orig->must, anyxml->must_size); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2380 | |
| 2381 | if (anyxml_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2382 | anyxml->when = lys_when_dup(ctx, anyxml_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2383 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2384 | break; |
| 2385 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2386 | case LYS_USES: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2387 | uses->grp = uses_orig->grp; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2388 | |
| 2389 | if (uses_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2390 | uses->when = lys_when_dup(ctx, uses_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2393 | uses->refine_size = uses_orig->refine_size; |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 2394 | uses->refine = lys_refine_dup(module, uses_orig->refine, uses_orig->refine_size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2395 | uses->augment_size = uses_orig->augment_size; |
Michal Vasko | ffa45cb | 2015-08-21 12:58:04 +0200 | [diff] [blame] | 2396 | uses->augment = lys_augment_dup(module, (struct lys_node *)uses, uses_orig->augment, uses_orig->augment_size); |
Michal Vasko | f8c128d | 2015-08-06 15:25:28 +0200 | [diff] [blame] | 2397 | if (!uses->child) { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 2398 | if (unres_schema_add_node(module, unres, uses, UNRES_USES, NULL, 0) == -1) { |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2399 | goto error; |
| 2400 | } |
Michal Vasko | f8c128d | 2015-08-06 15:25:28 +0200 | [diff] [blame] | 2401 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2402 | break; |
| 2403 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2404 | case LYS_CASE: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2405 | if (cs_orig->when) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2406 | cs->when = lys_when_dup(ctx, cs_orig->when); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2407 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2408 | break; |
| 2409 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2410 | case LYS_GROUPING: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2411 | grp->tpdf_size = grp_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2412 | grp->tpdf = lys_tpdf_dup(module, node->parent, grp_orig->tpdf, grp->tpdf_size, unres); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2413 | break; |
| 2414 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2415 | case LYS_RPC: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2416 | rpc->tpdf_size = rpc_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2417 | rpc->tpdf = lys_tpdf_dup(module, node->parent, rpc_orig->tpdf, rpc->tpdf_size, unres); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2418 | break; |
| 2419 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2420 | case LYS_INPUT: |
| 2421 | case LYS_OUTPUT: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2422 | io->tpdf_size = io_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2423 | io->tpdf = lys_tpdf_dup(module, node->parent, io_orig->tpdf, io->tpdf_size, unres); |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2424 | break; |
| 2425 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2426 | case LYS_NOTIF: |
Radek Krejci | d12f57b | 2015-08-06 10:43:39 +0200 | [diff] [blame] | 2427 | ntf->tpdf_size = ntf_orig->tpdf_size; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2428 | ntf->tpdf = lys_tpdf_dup(module, node->parent, ntf_orig->tpdf, ntf->tpdf_size, unres); |
Radek Krejci | a01e543 | 2015-06-16 10:35:25 +0200 | [diff] [blame] | 2429 | break; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2430 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2431 | default: |
Radek Krejci | 00768f4 | 2015-06-18 17:04:04 +0200 | [diff] [blame] | 2432 | /* LY_NODE_AUGMENT */ |
Michal Vasko | d23ce59 | 2015-08-06 09:55:37 +0200 | [diff] [blame] | 2433 | LOGINT; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2434 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | return retval; |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2438 | |
| 2439 | error: |
| 2440 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2441 | lys_node_free(retval, NULL); |
Michal Vasko | 49168a2 | 2015-08-17 16:35:41 +0200 | [diff] [blame] | 2442 | return NULL; |
Radek Krejci | 8bc9ca0 | 2015-06-04 15:52:46 +0200 | [diff] [blame] | 2443 | } |
| 2444 | |
Michal Vasko | 13b1583 | 2015-08-19 11:04:48 +0200 | [diff] [blame] | 2445 | void |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2446 | lys_free(struct lys_module *module, void (*private_destructor)(const struct lys_node *node, void *priv), int remove_from_ctx) |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2447 | { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2448 | struct ly_ctx *ctx; |
| 2449 | int i; |
| 2450 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2451 | if (!module) { |
| 2452 | return; |
| 2453 | } |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2454 | |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2455 | /* remove schema from the context */ |
| 2456 | ctx = module->ctx; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2457 | if (remove_from_ctx && ctx->models.used) { |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2458 | for (i = 0; i < ctx->models.used; i++) { |
| 2459 | if (ctx->models.list[i] == module) { |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2460 | /* move all the models to not change the order in the list */ |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2461 | ctx->models.used--; |
Michal Vasko | 627975a | 2016-02-11 11:39:03 +0100 | [diff] [blame] | 2462 | memmove(&ctx->models.list[i], ctx->models.list[i + 1], (ctx->models.used - i) * sizeof *ctx->models.list); |
Radek Krejci | dce5145 | 2015-06-16 15:20:08 +0200 | [diff] [blame] | 2463 | ctx->models.list[ctx->models.used] = NULL; |
| 2464 | /* we are done */ |
| 2465 | break; |
| 2466 | } |
| 2467 | } |
| 2468 | } |
| 2469 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2470 | /* common part with struct ly_submodule */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2471 | module_free_common(module, private_destructor); |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 2472 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2473 | /* specific items to free */ |
Michal Vasko | b746fff | 2016-02-11 11:37:50 +0100 | [diff] [blame] | 2474 | lydict_remove(ctx, module->ns); |
| 2475 | lydict_remove(ctx, module->prefix); |
Radek Krejci | 6793db0 | 2015-05-22 17:49:54 +0200 | [diff] [blame] | 2476 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 2477 | free(module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 2478 | } |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2479 | |
| 2480 | /* |
| 2481 | * op: 1 - enable, 0 - disable |
| 2482 | */ |
| 2483 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2484 | 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] | 2485 | { |
| 2486 | int all = 0; |
| 2487 | int i, j, k; |
| 2488 | |
| 2489 | if (!module || !name || !strlen(name)) { |
| 2490 | return EXIT_FAILURE; |
| 2491 | } |
| 2492 | |
| 2493 | if (!strcmp(name, "*")) { |
| 2494 | /* enable all */ |
| 2495 | all = 1; |
| 2496 | } |
| 2497 | |
| 2498 | /* module itself */ |
| 2499 | for (i = 0; i < module->features_size; i++) { |
| 2500 | if (all || !strcmp(module->features[i].name, name)) { |
| 2501 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2502 | module->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2503 | /* enable referenced features (recursion) */ |
| 2504 | for (k = 0; k < module->features[i].features_size; k++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2505 | lys_features_change(module->features[i].features[k]->module, |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2506 | module->features[i].features[k]->name, op); |
| 2507 | } |
| 2508 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2509 | module->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2510 | } |
| 2511 | if (!all) { |
| 2512 | return EXIT_SUCCESS; |
| 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | /* submodules */ |
| 2518 | for (j = 0; j < module->inc_size; j++) { |
| 2519 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2520 | if (all || !strcmp(module->inc[j].submodule->features[i].name, name)) { |
| 2521 | if (op) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2522 | module->inc[j].submodule->features[i].flags |= LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2523 | } else { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2524 | module->inc[j].submodule->features[i].flags &= ~LYS_FENABLED; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2525 | } |
| 2526 | if (!all) { |
| 2527 | return EXIT_SUCCESS; |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | } |
| 2532 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2533 | /* TODO submodules of submodules ... */ |
| 2534 | |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2535 | if (all) { |
| 2536 | return EXIT_SUCCESS; |
| 2537 | } else { |
| 2538 | return EXIT_FAILURE; |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2543 | lys_features_enable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2544 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2545 | return lys_features_change(module, feature, 1); |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2549 | lys_features_disable(const struct lys_module *module, const char *feature) |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2550 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2551 | return lys_features_change(module, feature, 0); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | API int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2555 | lys_features_state(const struct lys_module *module, const char *feature) |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2556 | { |
| 2557 | int i, j; |
| 2558 | |
| 2559 | if (!module || !feature) { |
| 2560 | return -1; |
| 2561 | } |
| 2562 | |
| 2563 | /* search for the specified feature */ |
| 2564 | /* module itself */ |
| 2565 | for (i = 0; i < module->features_size; i++) { |
| 2566 | if (!strcmp(feature, module->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2567 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2568 | return 1; |
| 2569 | } else { |
| 2570 | return 0; |
| 2571 | } |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | /* submodules */ |
| 2576 | for (j = 0; j < module->inc_size; j++) { |
| 2577 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
| 2578 | if (!strcmp(feature, module->inc[j].submodule->features[i].name)) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2579 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2580 | return 1; |
| 2581 | } else { |
| 2582 | return 0; |
| 2583 | } |
| 2584 | } |
| 2585 | } |
| 2586 | } |
| 2587 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2588 | /* feature definition not found */ |
| 2589 | return -1; |
Radek Krejci | 7e97c35 | 2015-06-19 16:26:34 +0200 | [diff] [blame] | 2590 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2591 | |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2592 | API const char ** |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2593 | lys_features_list(const struct lys_module *module, uint8_t **states) |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2594 | { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2595 | const char **result = NULL; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2596 | int i, j; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2597 | unsigned int count; |
| 2598 | |
| 2599 | if (!module) { |
| 2600 | return NULL; |
| 2601 | } |
| 2602 | |
| 2603 | count = module->features_size; |
| 2604 | for (i = 0; i < module->inc_size; i++) { |
| 2605 | count += module->inc[i].submodule->features_size; |
| 2606 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2607 | result = malloc((count + 1) * sizeof *result); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2608 | if (!result) { |
| 2609 | LOGMEM; |
| 2610 | return NULL; |
| 2611 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2612 | if (states) { |
| 2613 | *states = malloc((count + 1) * sizeof **states); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2614 | if (!(*states)) { |
| 2615 | LOGMEM; |
| 2616 | free(result); |
| 2617 | return NULL; |
| 2618 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2619 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2620 | count = 0; |
| 2621 | |
| 2622 | /* module itself */ |
| 2623 | for (i = 0; i < module->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2624 | result[count] = module->features[i].name; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2625 | if (states) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2626 | if (module->features[i].flags & LYS_FENABLED) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2627 | (*states)[count] = 1; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2628 | } else { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2629 | (*states)[count] = 0; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2630 | } |
| 2631 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2632 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | /* submodules */ |
| 2636 | for (j = 0; j < module->inc_size; j++) { |
| 2637 | for (i = 0; i < module->inc[j].submodule->features_size; i++) { |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 2638 | result[count] = module->inc[j].submodule->features[i].name; |
Radek Krejci | 374b94e | 2015-08-13 09:44:22 +0200 | [diff] [blame] | 2639 | if (states) { |
| 2640 | if (module->inc[j].submodule->features[i].flags & LYS_FENABLED) { |
| 2641 | (*states)[count] = 1; |
| 2642 | } else { |
| 2643 | (*states)[count] = 0; |
| 2644 | } |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2645 | } |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2646 | count++; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 2650 | /* terminating NULL byte */ |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2651 | result[count] = NULL; |
Michal Vasko | 2367e7c | 2015-07-07 11:33:44 +0200 | [diff] [blame] | 2652 | |
| 2653 | return result; |
| 2654 | } |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2655 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2656 | struct lys_module * |
| 2657 | lys_mainmodule(const struct lys_node *node) |
| 2658 | { |
| 2659 | return node->module->type ? ((struct lys_submodule *)node->module)->belongsto : node->module; |
| 2660 | } |
| 2661 | |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2662 | API struct lys_node * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2663 | lys_parent(const struct lys_node *node) |
Michal Vasko | baefb03 | 2015-09-24 14:52:10 +0200 | [diff] [blame] | 2664 | { |
| 2665 | if (!node || !node->parent) { |
| 2666 | return NULL; |
| 2667 | } |
| 2668 | |
| 2669 | if (node->parent->nodetype == LYS_AUGMENT) { |
| 2670 | return ((struct lys_node_augment *)node->parent)->target; |
| 2671 | } |
| 2672 | |
| 2673 | return node->parent; |
| 2674 | } |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2675 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2676 | API void * |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2677 | lys_set_private(const struct lys_node *node, void *priv) |
| 2678 | { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2679 | void *prev; |
| 2680 | |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2681 | if (!node) { |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2682 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 2683 | return NULL; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2684 | } |
| 2685 | |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2686 | prev = node->private; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2687 | ((struct lys_node *)node)->private = priv; |
Radek Krejci | fa0b5e0 | 2016-02-04 13:57:03 +0100 | [diff] [blame] | 2688 | |
| 2689 | return prev; |
Michal Vasko | 1b22915 | 2016-01-13 11:28:38 +0100 | [diff] [blame] | 2690 | } |