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